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