2017-04-13 05:24:43 +08:00
|
|
|
/*
|
2021-07-29 22:41:35 +08:00
|
|
|
* Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2017-04-13 05:24:43 +08:00
|
|
|
*
|
2018-12-06 20:05:25 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2017-04-13 05:24:43 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
|
|
|
|
#include "testutil.h"
|
2018-08-08 16:04:18 +08:00
|
|
|
#include "internal/nelem.h"
|
2017-04-13 05:24:43 +08:00
|
|
|
|
2018-08-08 16:04:18 +08:00
|
|
|
typedef struct {
|
|
|
|
const char *raw;
|
|
|
|
const char *encoded;
|
|
|
|
} TESTDATA;
|
2017-04-13 05:24:43 +08:00
|
|
|
|
2018-08-08 16:04:18 +08:00
|
|
|
static TESTDATA b64_pem_data[] = {
|
|
|
|
{ "hello world",
|
|
|
|
"aGVsbG8gd29ybGQ=" },
|
|
|
|
{ "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input",
|
|
|
|
"YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *pemtype = "PEMTESTDATA";
|
|
|
|
|
2021-06-29 23:18:34 +08:00
|
|
|
static char *pemfile;
|
|
|
|
|
2018-08-08 16:04:18 +08:00
|
|
|
static int test_b64(int idx)
|
2017-04-13 05:24:43 +08:00
|
|
|
{
|
|
|
|
BIO *b = BIO_new(BIO_s_mem());
|
|
|
|
char *name = NULL, *header = NULL;
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
long len;
|
|
|
|
int ret = 0;
|
2018-08-08 16:04:18 +08:00
|
|
|
const char *raw = b64_pem_data[idx].raw;
|
|
|
|
const char *encoded = b64_pem_data[idx].encoded;
|
2017-04-13 05:24:43 +08:00
|
|
|
|
|
|
|
if (!TEST_ptr(b)
|
|
|
|
|| !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
|
|
|
|
|| !TEST_true(BIO_printf(b, "%s\n", encoded))
|
|
|
|
|| !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
|
|
|
|
|| !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
|
|
|
|
PEM_FLAG_ONLY_B64)))
|
|
|
|
goto err;
|
2018-08-08 16:04:18 +08:00
|
|
|
if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0)
|
|
|
|
|| !TEST_int_eq(len, strlen(raw))
|
|
|
|
|| !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0))
|
2017-04-13 05:24:43 +08:00
|
|
|
goto err;
|
|
|
|
ret = 1;
|
|
|
|
err:
|
|
|
|
BIO_free(b);
|
|
|
|
OPENSSL_free(name);
|
|
|
|
OPENSSL_free(header);
|
|
|
|
OPENSSL_free(data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_invalid(void)
|
|
|
|
{
|
|
|
|
BIO *b = BIO_new(BIO_s_mem());
|
|
|
|
char *name = NULL, *header = NULL;
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
long len;
|
2018-08-08 16:04:18 +08:00
|
|
|
const char *encoded = b64_pem_data[0].encoded;
|
2017-04-13 05:24:43 +08:00
|
|
|
|
|
|
|
if (!TEST_ptr(b)
|
|
|
|
|| !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
|
|
|
|
|| !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded))
|
|
|
|
|| !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
|
|
|
|
/* Expected to fail due to non-base64 character */
|
|
|
|
|| TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
|
|
|
|
PEM_FLAG_ONLY_B64))) {
|
|
|
|
BIO_free(b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
BIO_free(b);
|
|
|
|
OPENSSL_free(name);
|
|
|
|
OPENSSL_free(header);
|
|
|
|
OPENSSL_free(data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-29 23:18:34 +08:00
|
|
|
static int test_cert_key_cert(void)
|
|
|
|
{
|
|
|
|
EVP_PKEY *key;
|
|
|
|
|
|
|
|
if (!TEST_ptr(key = load_pkey_pem(pemfile, NULL)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EVP_PKEY_free(key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-13 23:02:26 +08:00
|
|
|
static int test_empty_payload(void)
|
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
static char *emptypay =
|
|
|
|
"-----BEGIN CERTIFICATE-----\n"
|
|
|
|
"-\n" /* Base64 EOF character */
|
|
|
|
"-----END CERTIFICATE-----";
|
|
|
|
char *name = NULL, *header = NULL;
|
|
|
|
unsigned char *data = NULL;
|
|
|
|
long len;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
b = BIO_new_mem_buf(emptypay, strlen(emptypay));
|
|
|
|
if (!TEST_ptr(b))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Expected to fail because the payload is empty */
|
|
|
|
if (!TEST_false(PEM_read_bio_ex(b, &name, &header, &data, &len, 0)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
err:
|
|
|
|
OPENSSL_free(name);
|
|
|
|
OPENSSL_free(header);
|
|
|
|
OPENSSL_free(data);
|
|
|
|
BIO_free(b);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:48:27 +08:00
|
|
|
int setup_tests(void)
|
2017-04-13 05:24:43 +08:00
|
|
|
{
|
2021-06-29 23:18:34 +08:00
|
|
|
if (!TEST_ptr(pemfile = test_get_argument(0)))
|
|
|
|
return 0;
|
2018-08-08 16:04:18 +08:00
|
|
|
ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
|
2017-04-13 05:24:43 +08:00
|
|
|
ADD_TEST(test_invalid);
|
2021-06-29 23:18:34 +08:00
|
|
|
ADD_TEST(test_cert_key_cert);
|
2022-12-13 23:02:26 +08:00
|
|
|
ADD_TEST(test_empty_payload);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
2017-04-13 05:24:43 +08:00
|
|
|
}
|