1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/bio.h>
12*e0c4386eSCy Schubert #include <openssl/pem.h>
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert #include "testutil.h"
15*e0c4386eSCy Schubert #include "internal/nelem.h"
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubert typedef struct {
18*e0c4386eSCy Schubert const char *raw;
19*e0c4386eSCy Schubert const char *encoded;
20*e0c4386eSCy Schubert } TESTDATA;
21*e0c4386eSCy Schubert
22*e0c4386eSCy Schubert static TESTDATA b64_pem_data[] = {
23*e0c4386eSCy Schubert { "hello world",
24*e0c4386eSCy Schubert "aGVsbG8gd29ybGQ=" },
25*e0c4386eSCy Schubert { "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input",
26*e0c4386eSCy Schubert "YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" }
27*e0c4386eSCy Schubert };
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubert static const char *pemtype = "PEMTESTDATA";
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert static char *pemfile;
32*e0c4386eSCy Schubert
test_b64(int idx)33*e0c4386eSCy Schubert static int test_b64(int idx)
34*e0c4386eSCy Schubert {
35*e0c4386eSCy Schubert BIO *b = BIO_new(BIO_s_mem());
36*e0c4386eSCy Schubert char *name = NULL, *header = NULL;
37*e0c4386eSCy Schubert unsigned char *data = NULL;
38*e0c4386eSCy Schubert long len;
39*e0c4386eSCy Schubert int ret = 0;
40*e0c4386eSCy Schubert const char *raw = b64_pem_data[idx].raw;
41*e0c4386eSCy Schubert const char *encoded = b64_pem_data[idx].encoded;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert if (!TEST_ptr(b)
44*e0c4386eSCy Schubert || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
45*e0c4386eSCy Schubert || !TEST_true(BIO_printf(b, "%s\n", encoded))
46*e0c4386eSCy Schubert || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
47*e0c4386eSCy Schubert || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
48*e0c4386eSCy Schubert PEM_FLAG_ONLY_B64)))
49*e0c4386eSCy Schubert goto err;
50*e0c4386eSCy Schubert if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0)
51*e0c4386eSCy Schubert || !TEST_int_eq(len, strlen(raw))
52*e0c4386eSCy Schubert || !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0))
53*e0c4386eSCy Schubert goto err;
54*e0c4386eSCy Schubert ret = 1;
55*e0c4386eSCy Schubert err:
56*e0c4386eSCy Schubert BIO_free(b);
57*e0c4386eSCy Schubert OPENSSL_free(name);
58*e0c4386eSCy Schubert OPENSSL_free(header);
59*e0c4386eSCy Schubert OPENSSL_free(data);
60*e0c4386eSCy Schubert return ret;
61*e0c4386eSCy Schubert }
62*e0c4386eSCy Schubert
test_invalid(void)63*e0c4386eSCy Schubert static int test_invalid(void)
64*e0c4386eSCy Schubert {
65*e0c4386eSCy Schubert BIO *b = BIO_new(BIO_s_mem());
66*e0c4386eSCy Schubert char *name = NULL, *header = NULL;
67*e0c4386eSCy Schubert unsigned char *data = NULL;
68*e0c4386eSCy Schubert long len;
69*e0c4386eSCy Schubert const char *encoded = b64_pem_data[0].encoded;
70*e0c4386eSCy Schubert
71*e0c4386eSCy Schubert if (!TEST_ptr(b)
72*e0c4386eSCy Schubert || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
73*e0c4386eSCy Schubert || !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded))
74*e0c4386eSCy Schubert || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
75*e0c4386eSCy Schubert /* Expected to fail due to non-base64 character */
76*e0c4386eSCy Schubert || TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
77*e0c4386eSCy Schubert PEM_FLAG_ONLY_B64))) {
78*e0c4386eSCy Schubert BIO_free(b);
79*e0c4386eSCy Schubert return 0;
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert BIO_free(b);
82*e0c4386eSCy Schubert OPENSSL_free(name);
83*e0c4386eSCy Schubert OPENSSL_free(header);
84*e0c4386eSCy Schubert OPENSSL_free(data);
85*e0c4386eSCy Schubert return 1;
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert
test_cert_key_cert(void)88*e0c4386eSCy Schubert static int test_cert_key_cert(void)
89*e0c4386eSCy Schubert {
90*e0c4386eSCy Schubert EVP_PKEY *key;
91*e0c4386eSCy Schubert
92*e0c4386eSCy Schubert if (!TEST_ptr(key = load_pkey_pem(pemfile, NULL)))
93*e0c4386eSCy Schubert return 0;
94*e0c4386eSCy Schubert
95*e0c4386eSCy Schubert EVP_PKEY_free(key);
96*e0c4386eSCy Schubert return 1;
97*e0c4386eSCy Schubert }
98*e0c4386eSCy Schubert
test_empty_payload(void)99*e0c4386eSCy Schubert static int test_empty_payload(void)
100*e0c4386eSCy Schubert {
101*e0c4386eSCy Schubert BIO *b;
102*e0c4386eSCy Schubert static char *emptypay =
103*e0c4386eSCy Schubert "-----BEGIN CERTIFICATE-----\n"
104*e0c4386eSCy Schubert "-\n" /* Base64 EOF character */
105*e0c4386eSCy Schubert "-----END CERTIFICATE-----";
106*e0c4386eSCy Schubert char *name = NULL, *header = NULL;
107*e0c4386eSCy Schubert unsigned char *data = NULL;
108*e0c4386eSCy Schubert long len;
109*e0c4386eSCy Schubert int ret = 0;
110*e0c4386eSCy Schubert
111*e0c4386eSCy Schubert b = BIO_new_mem_buf(emptypay, strlen(emptypay));
112*e0c4386eSCy Schubert if (!TEST_ptr(b))
113*e0c4386eSCy Schubert return 0;
114*e0c4386eSCy Schubert
115*e0c4386eSCy Schubert /* Expected to fail because the payload is empty */
116*e0c4386eSCy Schubert if (!TEST_false(PEM_read_bio_ex(b, &name, &header, &data, &len, 0)))
117*e0c4386eSCy Schubert goto err;
118*e0c4386eSCy Schubert
119*e0c4386eSCy Schubert ret = 1;
120*e0c4386eSCy Schubert err:
121*e0c4386eSCy Schubert OPENSSL_free(name);
122*e0c4386eSCy Schubert OPENSSL_free(header);
123*e0c4386eSCy Schubert OPENSSL_free(data);
124*e0c4386eSCy Schubert BIO_free(b);
125*e0c4386eSCy Schubert return ret;
126*e0c4386eSCy Schubert }
127*e0c4386eSCy Schubert
test_protected_params(void)128*e0c4386eSCy Schubert static int test_protected_params(void)
129*e0c4386eSCy Schubert {
130*e0c4386eSCy Schubert BIO *b;
131*e0c4386eSCy Schubert static char *protectedpay =
132*e0c4386eSCy Schubert "-----BEGIN RSA PRIVATE KEY-----\n"
133*e0c4386eSCy Schubert "Proc-Type: 4,ENCRYPTED\n"
134*e0c4386eSCy Schubert "DEK-Info: AES-256-CBC,4A44448ED28992710556549B35100CEA\n"
135*e0c4386eSCy Schubert "\n"
136*e0c4386eSCy Schubert "Xw3INxKeH+rUUF57mjATpvj6zknVhedwrlRmRvnwlLv5wqIy5Ae4UVLPh7SUswfC\n"
137*e0c4386eSCy Schubert "-----END RSA PRIVATE KEY-----\n";
138*e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
139*e0c4386eSCy Schubert int ret = 0;
140*e0c4386eSCy Schubert
141*e0c4386eSCy Schubert b = BIO_new_mem_buf(protectedpay, strlen(protectedpay));
142*e0c4386eSCy Schubert if (!TEST_ptr(b))
143*e0c4386eSCy Schubert return 0;
144*e0c4386eSCy Schubert
145*e0c4386eSCy Schubert /* Expected to fail because we cannot decrypt protected PEM files */
146*e0c4386eSCy Schubert pkey = PEM_read_bio_Parameters(b, NULL);
147*e0c4386eSCy Schubert if (!TEST_ptr_null(pkey))
148*e0c4386eSCy Schubert goto err;
149*e0c4386eSCy Schubert
150*e0c4386eSCy Schubert ret = 1;
151*e0c4386eSCy Schubert err:
152*e0c4386eSCy Schubert EVP_PKEY_free(pkey);
153*e0c4386eSCy Schubert BIO_free(b);
154*e0c4386eSCy Schubert return ret;
155*e0c4386eSCy Schubert }
156*e0c4386eSCy Schubert
setup_tests(void)157*e0c4386eSCy Schubert int setup_tests(void)
158*e0c4386eSCy Schubert {
159*e0c4386eSCy Schubert if (!TEST_ptr(pemfile = test_get_argument(0)))
160*e0c4386eSCy Schubert return 0;
161*e0c4386eSCy Schubert ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
162*e0c4386eSCy Schubert ADD_TEST(test_invalid);
163*e0c4386eSCy Schubert ADD_TEST(test_cert_key_cert);
164*e0c4386eSCy Schubert ADD_TEST(test_empty_payload);
165*e0c4386eSCy Schubert ADD_TEST(test_protected_params);
166*e0c4386eSCy Schubert return 1;
167*e0c4386eSCy Schubert }
168