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