xref: /freebsd-src/crypto/openssl/test/cmsapitest.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2018-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 
12*e0c4386eSCy Schubert #include <openssl/cms.h>
13*e0c4386eSCy Schubert #include <openssl/bio.h>
14*e0c4386eSCy Schubert #include <openssl/x509.h>
15*e0c4386eSCy Schubert #include <openssl/pem.h>
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert #include "testutil.h"
18*e0c4386eSCy Schubert 
19*e0c4386eSCy Schubert static X509 *cert = NULL;
20*e0c4386eSCy Schubert static EVP_PKEY *privkey = NULL;
21*e0c4386eSCy Schubert static char *derin = NULL;
22*e0c4386eSCy Schubert 
test_encrypt_decrypt(const EVP_CIPHER * cipher)23*e0c4386eSCy Schubert static int test_encrypt_decrypt(const EVP_CIPHER *cipher)
24*e0c4386eSCy Schubert {
25*e0c4386eSCy Schubert     int testresult = 0;
26*e0c4386eSCy Schubert     STACK_OF(X509) *certstack = sk_X509_new_null();
27*e0c4386eSCy Schubert     const char *msg = "Hello world";
28*e0c4386eSCy Schubert     BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
29*e0c4386eSCy Schubert     BIO *outmsgbio = BIO_new(BIO_s_mem());
30*e0c4386eSCy Schubert     CMS_ContentInfo* content = NULL;
31*e0c4386eSCy Schubert     char buf[80];
32*e0c4386eSCy Schubert 
33*e0c4386eSCy Schubert     if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
34*e0c4386eSCy Schubert         goto end;
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert     if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
37*e0c4386eSCy Schubert         goto end;
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert     content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
40*e0c4386eSCy Schubert     if (!TEST_ptr(content))
41*e0c4386eSCy Schubert         goto end;
42*e0c4386eSCy Schubert 
43*e0c4386eSCy Schubert     if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
44*e0c4386eSCy Schubert                                CMS_TEXT)))
45*e0c4386eSCy Schubert         goto end;
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert     /* Check we got the message we first started with */
48*e0c4386eSCy Schubert     if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
49*e0c4386eSCy Schubert             || !TEST_int_eq(strcmp(buf, msg), 0))
50*e0c4386eSCy Schubert         goto end;
51*e0c4386eSCy Schubert 
52*e0c4386eSCy Schubert     testresult = 1;
53*e0c4386eSCy Schubert  end:
54*e0c4386eSCy Schubert     sk_X509_free(certstack);
55*e0c4386eSCy Schubert     BIO_free(msgbio);
56*e0c4386eSCy Schubert     BIO_free(outmsgbio);
57*e0c4386eSCy Schubert     CMS_ContentInfo_free(content);
58*e0c4386eSCy Schubert 
59*e0c4386eSCy Schubert     return testresult && TEST_int_eq(ERR_peek_error(), 0);
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert 
test_encrypt_decrypt_aes_cbc(void)62*e0c4386eSCy Schubert static int test_encrypt_decrypt_aes_cbc(void)
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert     return test_encrypt_decrypt(EVP_aes_128_cbc());
65*e0c4386eSCy Schubert }
66*e0c4386eSCy Schubert 
test_encrypt_decrypt_aes_128_gcm(void)67*e0c4386eSCy Schubert static int test_encrypt_decrypt_aes_128_gcm(void)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert     return test_encrypt_decrypt(EVP_aes_128_gcm());
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert 
test_encrypt_decrypt_aes_192_gcm(void)72*e0c4386eSCy Schubert static int test_encrypt_decrypt_aes_192_gcm(void)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert     return test_encrypt_decrypt(EVP_aes_192_gcm());
75*e0c4386eSCy Schubert }
76*e0c4386eSCy Schubert 
test_encrypt_decrypt_aes_256_gcm(void)77*e0c4386eSCy Schubert static int test_encrypt_decrypt_aes_256_gcm(void)
78*e0c4386eSCy Schubert {
79*e0c4386eSCy Schubert     return test_encrypt_decrypt(EVP_aes_256_gcm());
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert 
test_d2i_CMS_bio_NULL(void)82*e0c4386eSCy Schubert static int test_d2i_CMS_bio_NULL(void)
83*e0c4386eSCy Schubert {
84*e0c4386eSCy Schubert     BIO *bio;
85*e0c4386eSCy Schubert     CMS_ContentInfo *cms = NULL;
86*e0c4386eSCy Schubert     int ret = 0;
87*e0c4386eSCy Schubert 
88*e0c4386eSCy Schubert     /*
89*e0c4386eSCy Schubert      * Test data generated using:
90*e0c4386eSCy Schubert      * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \
91*e0c4386eSCy Schubert      * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \
92*e0c4386eSCy Schubert      * -nosmimecap
93*e0c4386eSCy Schubert      */
94*e0c4386eSCy Schubert     static const unsigned char cms_data[] = {
95*e0c4386eSCy Schubert         0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86,
96*e0c4386eSCy Schubert         0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
97*e0c4386eSCy Schubert         0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02,
98*e0c4386eSCy Schubert         0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09,
99*e0c4386eSCy Schubert         0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
100*e0c4386eSCy Schubert         0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48,
101*e0c4386eSCy Schubert         0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f,
102*e0c4386eSCy Schubert         0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
103*e0c4386eSCy Schubert         0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0,
104*e0c4386eSCy Schubert         0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30,
105*e0c4386eSCy Schubert         0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02,
106*e0c4386eSCy Schubert         0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2,
107*e0c4386eSCy Schubert         0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
108*e0c4386eSCy Schubert         0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
109*e0c4386eSCy Schubert         0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
110*e0c4386eSCy Schubert         0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
111*e0c4386eSCy Schubert         0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
112*e0c4386eSCy Schubert         0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
113*e0c4386eSCy Schubert         0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
114*e0c4386eSCy Schubert         0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
115*e0c4386eSCy Schubert         0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
116*e0c4386eSCy Schubert         0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
117*e0c4386eSCy Schubert         0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
118*e0c4386eSCy Schubert         0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
119*e0c4386eSCy Schubert         0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
120*e0c4386eSCy Schubert         0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35,
121*e0c4386eSCy Schubert         0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35,
122*e0c4386eSCy Schubert         0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30,
123*e0c4386eSCy Schubert         0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31,
124*e0c4386eSCy Schubert         0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
125*e0c4386eSCy Schubert         0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
126*e0c4386eSCy Schubert         0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
127*e0c4386eSCy Schubert         0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
128*e0c4386eSCy Schubert         0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
129*e0c4386eSCy Schubert         0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
130*e0c4386eSCy Schubert         0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
131*e0c4386eSCy Schubert         0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
132*e0c4386eSCy Schubert         0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
133*e0c4386eSCy Schubert         0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
134*e0c4386eSCy Schubert         0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
135*e0c4386eSCy Schubert         0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
136*e0c4386eSCy Schubert         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
137*e0c4386eSCy Schubert         0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
138*e0c4386eSCy Schubert         0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
139*e0c4386eSCy Schubert         0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88,
140*e0c4386eSCy Schubert         0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92,
141*e0c4386eSCy Schubert         0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10,
142*e0c4386eSCy Schubert         0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b,
143*e0c4386eSCy Schubert         0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b,
144*e0c4386eSCy Schubert         0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97,
145*e0c4386eSCy Schubert         0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2,
146*e0c4386eSCy Schubert         0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d,
147*e0c4386eSCy Schubert         0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7,
148*e0c4386eSCy Schubert         0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e,
149*e0c4386eSCy Schubert         0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e,
150*e0c4386eSCy Schubert         0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00,
151*e0c4386eSCy Schubert         0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f,
152*e0c4386eSCy Schubert         0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e,
153*e0c4386eSCy Schubert         0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c,
154*e0c4386eSCy Schubert         0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91,
155*e0c4386eSCy Schubert         0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3,
156*e0c4386eSCy Schubert         0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb,
157*e0c4386eSCy Schubert         0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec,
158*e0c4386eSCy Schubert         0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9,
159*e0c4386eSCy Schubert         0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97,
160*e0c4386eSCy Schubert         0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46,
161*e0c4386eSCy Schubert         0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9,
162*e0c4386eSCy Schubert         0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0,
163*e0c4386eSCy Schubert         0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf,
164*e0c4386eSCy Schubert         0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21,
165*e0c4386eSCy Schubert         0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93,
166*e0c4386eSCy Schubert         0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc,
167*e0c4386eSCy Schubert         0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75,
168*e0c4386eSCy Schubert         0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f,
169*e0c4386eSCy Schubert         0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37,
170*e0c4386eSCy Schubert         0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c,
171*e0c4386eSCy Schubert         0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01,
172*e0c4386eSCy Schubert         0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03,
173*e0c4386eSCy Schubert         0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
174*e0c4386eSCy Schubert         0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d,
175*e0c4386eSCy Schubert         0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89,
176*e0c4386eSCy Schubert         0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03,
177*e0c4386eSCy Schubert         0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d,
178*e0c4386eSCy Schubert         0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23,
179*e0c4386eSCy Schubert         0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56,
180*e0c4386eSCy Schubert         0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86,
181*e0c4386eSCy Schubert         0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33,
182*e0c4386eSCy Schubert         0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
183*e0c4386eSCy Schubert         0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
184*e0c4386eSCy Schubert         0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a,
185*e0c4386eSCy Schubert         0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce,
186*e0c4386eSCy Schubert         0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb,
187*e0c4386eSCy Schubert         0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69,
188*e0c4386eSCy Schubert         0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6,
189*e0c4386eSCy Schubert         0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe,
190*e0c4386eSCy Schubert         0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17,
191*e0c4386eSCy Schubert         0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95,
192*e0c4386eSCy Schubert         0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20,
193*e0c4386eSCy Schubert         0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08,
194*e0c4386eSCy Schubert         0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab,
195*e0c4386eSCy Schubert         0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3,
196*e0c4386eSCy Schubert         0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c,
197*e0c4386eSCy Schubert         0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79,
198*e0c4386eSCy Schubert         0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc,
199*e0c4386eSCy Schubert         0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a,
200*e0c4386eSCy Schubert         0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe,
201*e0c4386eSCy Schubert         0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d,
202*e0c4386eSCy Schubert         0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1,
203*e0c4386eSCy Schubert         0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba,
204*e0c4386eSCy Schubert         0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49,
205*e0c4386eSCy Schubert         0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87,
206*e0c4386eSCy Schubert         0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c,
207*e0c4386eSCy Schubert         0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97,
208*e0c4386eSCy Schubert         0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44,
209*e0c4386eSCy Schubert         0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5,
210*e0c4386eSCy Schubert         0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1,
211*e0c4386eSCy Schubert         0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b,
212*e0c4386eSCy Schubert         0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6,
213*e0c4386eSCy Schubert         0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5,
214*e0c4386eSCy Schubert         0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12,
215*e0c4386eSCy Schubert         0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d,
216*e0c4386eSCy Schubert         0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82,
217*e0c4386eSCy Schubert         0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01,
218*e0c4386eSCy Schubert         0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30,
219*e0c4386eSCy Schubert         0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
220*e0c4386eSCy Schubert         0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
221*e0c4386eSCy Schubert         0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
222*e0c4386eSCy Schubert         0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
223*e0c4386eSCy Schubert         0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
224*e0c4386eSCy Schubert         0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
225*e0c4386eSCy Schubert         0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
226*e0c4386eSCy Schubert         0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
227*e0c4386eSCy Schubert         0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
228*e0c4386eSCy Schubert         0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f,
229*e0c4386eSCy Schubert         0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43,
230*e0c4386eSCy Schubert         0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b,
231*e0c4386eSCy Schubert         0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
232*e0c4386eSCy Schubert         0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06,
233*e0c4386eSCy Schubert         0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
234*e0c4386eSCy Schubert         0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86,
235*e0c4386eSCy Schubert         0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30,
236*e0c4386eSCy Schubert         0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
237*e0c4386eSCy Schubert         0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d,
238*e0c4386eSCy Schubert         0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39,
239*e0c4386eSCy Schubert         0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06,
240*e0c4386eSCy Schubert         0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
241*e0c4386eSCy Schubert         0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80,
242*e0c4386eSCy Schubert         0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26,
243*e0c4386eSCy Schubert         0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53,
244*e0c4386eSCy Schubert         0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49,
245*e0c4386eSCy Schubert         0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d,
246*e0c4386eSCy Schubert         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
247*e0c4386eSCy Schubert         0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01,
248*e0c4386eSCy Schubert         0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52,
249*e0c4386eSCy Schubert         0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c,
250*e0c4386eSCy Schubert         0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a,
251*e0c4386eSCy Schubert         0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05,
252*e0c4386eSCy Schubert         0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9,
253*e0c4386eSCy Schubert         0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e,
254*e0c4386eSCy Schubert         0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2,
255*e0c4386eSCy Schubert         0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71,
256*e0c4386eSCy Schubert         0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73,
257*e0c4386eSCy Schubert         0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5,
258*e0c4386eSCy Schubert         0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43,
259*e0c4386eSCy Schubert         0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c,
260*e0c4386eSCy Schubert         0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7,
261*e0c4386eSCy Schubert         0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99,
262*e0c4386eSCy Schubert         0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6,
263*e0c4386eSCy Schubert         0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe,
264*e0c4386eSCy Schubert         0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a,
265*e0c4386eSCy Schubert         0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37,
266*e0c4386eSCy Schubert         0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7,
267*e0c4386eSCy Schubert         0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb,
268*e0c4386eSCy Schubert         0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde,
269*e0c4386eSCy Schubert         0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49,
270*e0c4386eSCy Schubert         0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2,
271*e0c4386eSCy Schubert         0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85,
272*e0c4386eSCy Schubert         0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75,
273*e0c4386eSCy Schubert         0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01,
274*e0c4386eSCy Schubert         0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f,
275*e0c4386eSCy Schubert         0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30,
276*e0c4386eSCy Schubert         0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a,
277*e0c4386eSCy Schubert         0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5,
278*e0c4386eSCy Schubert         0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74,
279*e0c4386eSCy Schubert         0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3,
280*e0c4386eSCy Schubert         0x6a
281*e0c4386eSCy Schubert     };
282*e0c4386eSCy Schubert 
283*e0c4386eSCy Schubert     ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data)))
284*e0c4386eSCy Schubert           && TEST_ptr(cms = d2i_CMS_bio(bio, NULL))
285*e0c4386eSCy Schubert           && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL,
286*e0c4386eSCy Schubert                                   CMS_NO_SIGNER_CERT_VERIFY));
287*e0c4386eSCy Schubert     CMS_ContentInfo_free(cms);
288*e0c4386eSCy Schubert     BIO_free(bio);
289*e0c4386eSCy Schubert     return ret && TEST_int_eq(ERR_peek_error(), 0);
290*e0c4386eSCy Schubert }
291*e0c4386eSCy Schubert 
read_all(BIO * bio,long * p_len)292*e0c4386eSCy Schubert static unsigned char *read_all(BIO *bio, long *p_len)
293*e0c4386eSCy Schubert {
294*e0c4386eSCy Schubert     const int step = 256;
295*e0c4386eSCy Schubert     unsigned char *buf = NULL;
296*e0c4386eSCy Schubert     unsigned char *tmp = NULL;
297*e0c4386eSCy Schubert     int ret;
298*e0c4386eSCy Schubert 
299*e0c4386eSCy Schubert     *p_len = 0;
300*e0c4386eSCy Schubert     for (;;) {
301*e0c4386eSCy Schubert         tmp = OPENSSL_realloc(buf, *p_len + step);
302*e0c4386eSCy Schubert         if (tmp == NULL)
303*e0c4386eSCy Schubert             break;
304*e0c4386eSCy Schubert         buf = tmp;
305*e0c4386eSCy Schubert         ret = BIO_read(bio, buf + *p_len, step);
306*e0c4386eSCy Schubert         if (ret < 0)
307*e0c4386eSCy Schubert             break;
308*e0c4386eSCy Schubert 
309*e0c4386eSCy Schubert         *p_len += ret;
310*e0c4386eSCy Schubert 
311*e0c4386eSCy Schubert         if (ret < step)
312*e0c4386eSCy Schubert             return buf;
313*e0c4386eSCy Schubert     }
314*e0c4386eSCy Schubert 
315*e0c4386eSCy Schubert     /* Error */
316*e0c4386eSCy Schubert     OPENSSL_free(buf);
317*e0c4386eSCy Schubert     *p_len = 0;
318*e0c4386eSCy Schubert     return NULL;
319*e0c4386eSCy Schubert }
320*e0c4386eSCy Schubert 
test_d2i_CMS_decode(const int idx)321*e0c4386eSCy Schubert static int test_d2i_CMS_decode(const int idx)
322*e0c4386eSCy Schubert {
323*e0c4386eSCy Schubert     BIO *bio = NULL;
324*e0c4386eSCy Schubert     CMS_ContentInfo *cms = NULL;
325*e0c4386eSCy Schubert     unsigned char *buf = NULL;
326*e0c4386eSCy Schubert     const unsigned char *tmp = NULL;
327*e0c4386eSCy Schubert     long buf_len = 0;
328*e0c4386eSCy Schubert     int ret = 0;
329*e0c4386eSCy Schubert 
330*e0c4386eSCy Schubert     if (!TEST_ptr(bio = BIO_new_file(derin, "r")))
331*e0c4386eSCy Schubert       goto end;
332*e0c4386eSCy Schubert 
333*e0c4386eSCy Schubert     switch (idx) {
334*e0c4386eSCy Schubert     case 0:
335*e0c4386eSCy Schubert         if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL)))
336*e0c4386eSCy Schubert             goto end;
337*e0c4386eSCy Schubert         break;
338*e0c4386eSCy Schubert     case 1:
339*e0c4386eSCy Schubert         if (!TEST_ptr(buf = read_all(bio, &buf_len)))
340*e0c4386eSCy Schubert             goto end;
341*e0c4386eSCy Schubert         tmp = buf;
342*e0c4386eSCy Schubert         if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len)))
343*e0c4386eSCy Schubert             goto end;
344*e0c4386eSCy Schubert         break;
345*e0c4386eSCy Schubert     }
346*e0c4386eSCy Schubert 
347*e0c4386eSCy Schubert     if (!TEST_int_eq(ERR_peek_error(), 0))
348*e0c4386eSCy Schubert         goto end;
349*e0c4386eSCy Schubert 
350*e0c4386eSCy Schubert     ret = 1;
351*e0c4386eSCy Schubert end:
352*e0c4386eSCy Schubert     CMS_ContentInfo_free(cms);
353*e0c4386eSCy Schubert     BIO_free(bio);
354*e0c4386eSCy Schubert     OPENSSL_free(buf);
355*e0c4386eSCy Schubert 
356*e0c4386eSCy Schubert     return ret;
357*e0c4386eSCy Schubert }
358*e0c4386eSCy Schubert 
359*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile\n")
360*e0c4386eSCy Schubert 
setup_tests(void)361*e0c4386eSCy Schubert int setup_tests(void)
362*e0c4386eSCy Schubert {
363*e0c4386eSCy Schubert     char *certin = NULL, *privkeyin = NULL;
364*e0c4386eSCy Schubert     BIO *certbio = NULL, *privkeybio = NULL;
365*e0c4386eSCy Schubert 
366*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
367*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
368*e0c4386eSCy Schubert         return 0;
369*e0c4386eSCy Schubert     }
370*e0c4386eSCy Schubert 
371*e0c4386eSCy Schubert     if (!TEST_ptr(certin = test_get_argument(0))
372*e0c4386eSCy Schubert             || !TEST_ptr(privkeyin = test_get_argument(1))
373*e0c4386eSCy Schubert             || !TEST_ptr(derin = test_get_argument(2)))
374*e0c4386eSCy Schubert         return 0;
375*e0c4386eSCy Schubert 
376*e0c4386eSCy Schubert     certbio = BIO_new_file(certin, "r");
377*e0c4386eSCy Schubert     if (!TEST_ptr(certbio))
378*e0c4386eSCy Schubert         return 0;
379*e0c4386eSCy Schubert     if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
380*e0c4386eSCy Schubert         BIO_free(certbio);
381*e0c4386eSCy Schubert         return 0;
382*e0c4386eSCy Schubert     }
383*e0c4386eSCy Schubert     BIO_free(certbio);
384*e0c4386eSCy Schubert 
385*e0c4386eSCy Schubert     privkeybio = BIO_new_file(privkeyin, "r");
386*e0c4386eSCy Schubert     if (!TEST_ptr(privkeybio)) {
387*e0c4386eSCy Schubert         X509_free(cert);
388*e0c4386eSCy Schubert         cert = NULL;
389*e0c4386eSCy Schubert         return 0;
390*e0c4386eSCy Schubert     }
391*e0c4386eSCy Schubert     if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
392*e0c4386eSCy Schubert         BIO_free(privkeybio);
393*e0c4386eSCy Schubert         X509_free(cert);
394*e0c4386eSCy Schubert         cert = NULL;
395*e0c4386eSCy Schubert         return 0;
396*e0c4386eSCy Schubert     }
397*e0c4386eSCy Schubert     BIO_free(privkeybio);
398*e0c4386eSCy Schubert 
399*e0c4386eSCy Schubert     ADD_TEST(test_encrypt_decrypt_aes_cbc);
400*e0c4386eSCy Schubert     ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
401*e0c4386eSCy Schubert     ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
402*e0c4386eSCy Schubert     ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
403*e0c4386eSCy Schubert     ADD_TEST(test_d2i_CMS_bio_NULL);
404*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_d2i_CMS_decode, 2);
405*e0c4386eSCy Schubert     return 1;
406*e0c4386eSCy Schubert }
407*e0c4386eSCy Schubert 
cleanup_tests(void)408*e0c4386eSCy Schubert void cleanup_tests(void)
409*e0c4386eSCy Schubert {
410*e0c4386eSCy Schubert     X509_free(cert);
411*e0c4386eSCy Schubert     EVP_PKEY_free(privkey);
412*e0c4386eSCy Schubert }
413