]>
Commit | Line | Data |
---|---|---|
3dbc5156 | 1 | /* |
33388b44 | 2 | * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved. |
3dbc5156 DDO |
3 | * Copyright Nokia 2007-2019 |
4 | * Copyright Siemens AG 2015-2019 | |
5 | * | |
6 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
7 | * this file except in compliance with the License. You can obtain a copy | |
8 | * in the file LICENSE in the source distribution or at | |
9 | * https://www.openssl.org/source/license.html | |
10 | */ | |
11 | ||
12 | #include "cmp_testlib.h" | |
13 | ||
852c2ed2 RS |
14 | DEFINE_STACK_OF(X509) |
15 | ||
3dbc5156 DDO |
16 | static const char *ir_protected_f; |
17 | static const char *ir_unprotected_f; | |
18 | static const char *ip_PBM_f; | |
19 | ||
20 | typedef struct test_fixture { | |
21 | const char *test_case_name; | |
22 | OSSL_CMP_CTX *cmp_ctx; | |
23 | /* for protection tests */ | |
24 | OSSL_CMP_MSG *msg; | |
235595c4 | 25 | OSSL_CMP_PKISI *si; /* for error and response messages */ |
3dbc5156 DDO |
26 | EVP_PKEY *pubkey; |
27 | unsigned char *mem; | |
28 | int memlen; | |
29 | X509 *cert; | |
30 | STACK_OF(X509) *certs; | |
31 | STACK_OF(X509) *chain; | |
32 | int callback_arg; | |
33 | int expected; | |
34 | } CMP_PROTECT_TEST_FIXTURE; | |
35 | ||
4561f15f DDO |
36 | static OPENSSL_CTX *libctx = NULL; |
37 | static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL; | |
38 | ||
3dbc5156 DDO |
39 | static void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture) |
40 | { | |
41 | OSSL_CMP_CTX_free(fixture->cmp_ctx); | |
42 | OSSL_CMP_MSG_free(fixture->msg); | |
3dbc5156 DDO |
43 | OSSL_CMP_PKISI_free(fixture->si); |
44 | ||
45 | OPENSSL_free(fixture->mem); | |
46 | sk_X509_free(fixture->certs); | |
47 | sk_X509_free(fixture->chain); | |
48 | ||
49 | OPENSSL_free(fixture); | |
50 | } | |
51 | ||
52 | static CMP_PROTECT_TEST_FIXTURE *set_up(const char *const test_case_name) | |
53 | { | |
54 | CMP_PROTECT_TEST_FIXTURE *fixture; | |
55 | ||
56 | if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))) | |
57 | return NULL; | |
58 | fixture->test_case_name = test_case_name; | |
4561f15f | 59 | if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))) { |
3dbc5156 DDO |
60 | tear_down(fixture); |
61 | return NULL; | |
62 | } | |
63 | return fixture; | |
64 | } | |
65 | ||
66 | static EVP_PKEY *loadedprivkey = NULL; | |
67 | static EVP_PKEY *loadedpubkey = NULL; | |
68 | static EVP_PKEY *loadedkey = NULL; | |
69 | static X509 *cert = NULL; | |
70 | static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH]; | |
71 | static OSSL_CMP_MSG *ir_unprotected, *ir_protected; | |
72 | static X509 *endentity1 = NULL, *endentity2 = NULL, | |
73 | *root = NULL, *intermediate = NULL; | |
74 | ||
75 | static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE *fixture) | |
76 | { | |
77 | ASN1_BIT_STRING *protection = | |
6d1f50b5 | 78 | ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg); |
3dbc5156 DDO |
79 | int res = TEST_ptr_null(protection); |
80 | ||
81 | ASN1_BIT_STRING_free(protection); | |
82 | return res; | |
83 | } | |
84 | ||
85 | static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture) | |
86 | { | |
87 | ASN1_BIT_STRING *protection = | |
6d1f50b5 | 88 | ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg); |
3dbc5156 | 89 | int res = TEST_ptr(protection) |
235595c4 DDO |
90 | && TEST_true(ASN1_STRING_cmp(protection, |
91 | fixture->msg->protection) == 0); | |
3dbc5156 DDO |
92 | |
93 | ASN1_BIT_STRING_free(protection); | |
94 | return res; | |
95 | } | |
96 | ||
97 | /* | |
98 | * This function works similarly to parts of CMP_verify_signature in cmp_vfy.c, | |
99 | * but without the need for a OSSL_CMP_CTX or a X509 certificate | |
100 | */ | |
101 | static int verify_signature(OSSL_CMP_MSG *msg, | |
102 | ASN1_BIT_STRING *protection, | |
6d1f50b5 | 103 | EVP_PKEY *pkey, EVP_MD *digest) |
3dbc5156 | 104 | { |
642f60d8 | 105 | OSSL_CMP_PROTECTEDPART prot_part; |
3dbc5156 DDO |
106 | unsigned char *prot_part_der = NULL; |
107 | int len; | |
108 | EVP_MD_CTX *ctx = NULL; | |
3dbc5156 DDO |
109 | int res; |
110 | ||
111 | prot_part.header = OSSL_CMP_MSG_get0_header(msg); | |
112 | prot_part.body = msg->body; | |
642f60d8 | 113 | len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der); |
3dbc5156 | 114 | res = |
642f60d8 | 115 | TEST_int_ge(len, 0) |
3dbc5156 DDO |
116 | && TEST_ptr(ctx = EVP_MD_CTX_new()) |
117 | && TEST_true(EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey)) | |
118 | && TEST_int_eq(EVP_DigestVerify(ctx, protection->data, | |
119 | protection->length, | |
120 | prot_part_der, len), 1); | |
121 | /* cleanup */ | |
122 | EVP_MD_CTX_free(ctx); | |
123 | OPENSSL_free(prot_part_der); | |
124 | return res; | |
125 | } | |
126 | ||
127 | /* Calls OSSL_CMP_calc_protection and compares and verifies signature */ | |
128 | static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE * | |
129 | fixture) | |
130 | { | |
131 | ASN1_BIT_STRING *protection = | |
6d1f50b5 | 132 | ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg); |
3dbc5156 DDO |
133 | int ret = (TEST_ptr(protection) |
134 | && TEST_true(ASN1_STRING_cmp(protection, | |
135 | fixture->msg->protection) == 0) | |
136 | && TEST_true(verify_signature(fixture->msg, protection, | |
137 | fixture->pubkey, | |
138 | fixture->cmp_ctx->digest))); | |
139 | ||
140 | ASN1_BIT_STRING_free(protection); | |
141 | return ret; | |
142 | } | |
143 | ||
144 | static int test_cmp_calc_protection_no_key_no_secret(void) | |
145 | { | |
146 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
147 | if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f)) | |
148 | || !TEST_ptr(fixture->msg->header->protectionAlg = | |
149 | X509_ALGOR_new() /* no specific alg needed here */)) { | |
150 | tear_down(fixture); | |
151 | fixture = NULL; | |
152 | } | |
153 | ||
154 | EXECUTE_TEST(execute_calc_protection_fails_test, tear_down); | |
155 | return result; | |
156 | } | |
157 | ||
158 | static int test_cmp_calc_protection_pkey(void) | |
159 | { | |
160 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
161 | fixture->pubkey = loadedpubkey; | |
6d1f50b5 DDO |
162 | if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedprivkey)) |
163 | || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))) { | |
3dbc5156 DDO |
164 | tear_down(fixture); |
165 | fixture = NULL; | |
166 | } | |
167 | EXECUTE_TEST(execute_calc_protection_signature_test, tear_down); | |
168 | return result; | |
169 | } | |
170 | ||
171 | static int test_cmp_calc_protection_pbmac(void) | |
172 | { | |
173 | unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' }; | |
174 | ||
175 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
6d1f50b5 DDO |
176 | if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, |
177 | sec_insta, sizeof(sec_insta))) | |
3dbc5156 DDO |
178 | || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f))) { |
179 | tear_down(fixture); | |
180 | fixture = NULL; | |
181 | } | |
182 | EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down); | |
183 | return result; | |
184 | } | |
185 | static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture) | |
186 | { | |
187 | return TEST_int_eq(fixture->expected, | |
188 | ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg)); | |
189 | } | |
190 | ||
191 | #define SET_OPT_UNPROTECTED_SEND(ctx, val) \ | |
192 | OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val)) | |
193 | static int test_MSG_protect_unprotected_request(void) | |
194 | { | |
195 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
196 | ||
197 | fixture->expected = 1; | |
198 | if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) | |
199 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) { | |
200 | tear_down(fixture); | |
201 | fixture = NULL; | |
202 | } | |
203 | EXECUTE_TEST(execute_MSG_protect_test, tear_down); | |
204 | return result; | |
205 | } | |
206 | ||
207 | static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void) | |
208 | { | |
209 | const size_t size = sizeof(rand_data) / 2; | |
210 | ||
211 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
212 | fixture->expected = 1; | |
213 | ||
6d1f50b5 | 214 | if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) |
3dbc5156 DDO |
215 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)) |
216 | /* | |
217 | * Use half of the 16 bytes of random input | |
218 | * for each reference and secret value | |
219 | */ | |
220 | || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, | |
221 | rand_data, size)) | |
222 | || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, | |
223 | rand_data + size, | |
224 | size))) { | |
225 | tear_down(fixture); | |
226 | fixture = NULL; | |
227 | } | |
228 | EXECUTE_TEST(execute_MSG_protect_test, tear_down); | |
229 | return result; | |
230 | } | |
231 | ||
232 | static int test_MSG_protect_with_certificate_and_key(void) | |
233 | { | |
234 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
235 | fixture->expected = 1; | |
236 | ||
237 | if (!TEST_ptr(fixture->msg = | |
238 | OSSL_CMP_MSG_dup(ir_unprotected)) | |
239 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)) | |
240 | || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, loadedkey)) | |
63f1883d | 241 | || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) { |
3dbc5156 DDO |
242 | tear_down(fixture); |
243 | fixture = NULL; | |
244 | } | |
245 | EXECUTE_TEST(execute_MSG_protect_test, tear_down); | |
246 | return result; | |
247 | } | |
248 | ||
249 | static int test_MSG_protect_certificate_based_without_cert(void) | |
250 | { | |
06cee80a | 251 | OSSL_CMP_CTX *ctx; |
3dbc5156 | 252 | |
06cee80a DDO |
253 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); |
254 | ctx = fixture->cmp_ctx; | |
3dbc5156 DDO |
255 | fixture->expected = 0; |
256 | if (!TEST_ptr(fixture->msg = | |
257 | OSSL_CMP_MSG_dup(ir_unprotected)) | |
258 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0)) | |
259 | || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, loadedkey))) { | |
260 | tear_down(fixture); | |
261 | fixture = NULL; | |
262 | } | |
263 | EVP_PKEY_up_ref(loadedkey); | |
264 | EXECUTE_TEST(execute_MSG_protect_test, tear_down); | |
265 | return result; | |
266 | } | |
267 | ||
268 | static int test_MSG_protect_no_key_no_secret(void) | |
269 | { | |
270 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
271 | fixture->expected = 0; | |
272 | if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) | |
273 | || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) { | |
274 | tear_down(fixture); | |
275 | fixture = NULL; | |
276 | } | |
277 | EXECUTE_TEST(execute_MSG_protect_test, tear_down); | |
278 | return result; | |
279 | } | |
280 | ||
cfca56df DDO |
281 | static int test_MSG_protect_pbmac_no_sender(int with_ref) |
282 | { | |
283 | static unsigned char secret[] = { 47, 11, 8, 15 }; | |
284 | static unsigned char ref[] = { 0xca, 0xfe, 0xba, 0xbe }; | |
285 | ||
286 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
287 | fixture->expected = with_ref; | |
288 | if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected)) | |
289 | || !SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0) | |
290 | || !ossl_cmp_hdr_set1_sender(fixture->msg->header, NULL) | |
291 | || !OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, | |
292 | secret, sizeof(secret)) | |
293 | || (!OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx, | |
294 | with_ref ? ref : NULL, | |
295 | sizeof(ref)))) { | |
296 | tear_down(fixture); | |
297 | fixture = NULL; | |
298 | } | |
299 | EXECUTE_TEST(execute_MSG_protect_test, tear_down); | |
300 | return result; | |
301 | } | |
302 | ||
303 | static int test_MSG_protect_pbmac_no_sender_with_ref(void) | |
304 | { | |
305 | return test_MSG_protect_pbmac_no_sender(1); | |
306 | } | |
307 | ||
308 | static int test_MSG_protect_pbmac_no_sender_no_ref(void) | |
309 | { | |
310 | return test_MSG_protect_pbmac_no_sender(0); | |
311 | } | |
312 | ||
3dbc5156 DDO |
313 | static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture) |
314 | { | |
315 | return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx, | |
316 | fixture->msg)); | |
317 | } | |
318 | ||
319 | static int test_MSG_add_extraCerts(void) | |
320 | { | |
321 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
322 | if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) { | |
323 | tear_down(fixture); | |
324 | fixture = NULL; | |
325 | } | |
326 | EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down); | |
327 | return result; | |
328 | } | |
329 | ||
ef071222 MC |
330 | #ifndef OPENSSL_NO_EC |
331 | /* The cert chain tests use EC certs so we skip them in no-ec builds */ | |
3dbc5156 DDO |
332 | static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture) |
333 | { | |
3dbc5156 | 334 | int ret = 0; |
28e9f62b | 335 | OSSL_CMP_CTX *ctx = fixture->cmp_ctx; |
15076c26 | 336 | X509_STORE *store; |
28e9f62b | 337 | STACK_OF(X509) *chain = |
15076c26 | 338 | ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq, NULL, |
28e9f62b | 339 | fixture->certs, fixture->cert); |
3dbc5156 | 340 | |
28e9f62b | 341 | if (TEST_ptr(chain)) { |
3dbc5156 | 342 | /* Check whether chain built is equal to the expected one */ |
28e9f62b DDO |
343 | ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain)); |
344 | sk_X509_pop_free(chain, X509_free); | |
3dbc5156 | 345 | } |
15076c26 DDO |
346 | if (!ret) |
347 | return 0; | |
348 | ||
349 | if (TEST_ptr(store = X509_STORE_new()) | |
350 | && TEST_true(X509_STORE_add_cert(store, root))) { | |
351 | X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store), | |
352 | X509_V_FLAG_NO_CHECK_TIME); | |
353 | chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq, | |
354 | store, fixture->certs, fixture->cert); | |
355 | ret = TEST_int_eq(fixture->expected, chain != NULL); | |
356 | if (ret && chain != NULL) { | |
357 | /* Check whether chain built is equal to the expected one */ | |
358 | ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain)); | |
359 | sk_X509_pop_free(chain, X509_free); | |
360 | } | |
361 | } | |
362 | X509_STORE_free(store); | |
3dbc5156 DDO |
363 | return ret; |
364 | } | |
365 | ||
366 | static int test_cmp_build_cert_chain(void) | |
367 | { | |
368 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
15076c26 | 369 | fixture->expected = 1; |
3dbc5156 DDO |
370 | fixture->cert = endentity2; |
371 | if (!TEST_ptr(fixture->certs = sk_X509_new_null()) | |
372 | || !TEST_ptr(fixture->chain = sk_X509_new_null()) | |
373 | || !TEST_true(sk_X509_push(fixture->certs, endentity1)) | |
374 | || !TEST_true(sk_X509_push(fixture->certs, root)) | |
375 | || !TEST_true(sk_X509_push(fixture->certs, intermediate)) | |
376 | || !TEST_true(sk_X509_push(fixture->chain, endentity2)) | |
377 | || !TEST_true(sk_X509_push(fixture->chain, intermediate))) { | |
378 | tear_down(fixture); | |
379 | fixture = NULL; | |
380 | } | |
381 | EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); | |
382 | return result; | |
383 | } | |
384 | ||
385 | static int test_cmp_build_cert_chain_missing_intermediate(void) | |
386 | { | |
387 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
15076c26 | 388 | fixture->expected = 0; |
3dbc5156 DDO |
389 | fixture->cert = endentity2; |
390 | if (!TEST_ptr(fixture->certs = sk_X509_new_null()) | |
391 | || !TEST_ptr(fixture->chain = sk_X509_new_null()) | |
392 | || !TEST_true(sk_X509_push(fixture->certs, endentity1)) | |
393 | || !TEST_true(sk_X509_push(fixture->certs, root)) | |
394 | || !TEST_true(sk_X509_push(fixture->chain, endentity2))) { | |
395 | tear_down(fixture); | |
396 | fixture = NULL; | |
397 | } | |
398 | EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); | |
399 | return result; | |
400 | } | |
401 | ||
15076c26 | 402 | static int test_cmp_build_cert_chain_no_root(void) |
3dbc5156 DDO |
403 | { |
404 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
15076c26 | 405 | fixture->expected = 1; |
3dbc5156 DDO |
406 | fixture->cert = endentity2; |
407 | if (!TEST_ptr(fixture->certs = sk_X509_new_null()) | |
408 | || !TEST_ptr(fixture->chain = sk_X509_new_null()) | |
409 | || !TEST_true(sk_X509_push(fixture->certs, endentity1)) | |
410 | || !TEST_true(sk_X509_push(fixture->certs, intermediate)) | |
411 | || !TEST_true(sk_X509_push(fixture->chain, endentity2)) | |
412 | || !TEST_true(sk_X509_push(fixture->chain, intermediate))) { | |
413 | tear_down(fixture); | |
414 | fixture = NULL; | |
415 | } | |
416 | EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); | |
417 | return result; | |
418 | } | |
419 | ||
420 | static int test_cmp_build_cert_chain_no_certs(void) | |
421 | { | |
422 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
15076c26 | 423 | fixture->expected = 0; |
3dbc5156 DDO |
424 | fixture->cert = endentity2; |
425 | if (!TEST_ptr(fixture->certs = sk_X509_new_null()) | |
426 | || !TEST_ptr(fixture->chain = sk_X509_new_null()) | |
427 | || !TEST_true(sk_X509_push(fixture->chain, endentity2))) { | |
428 | tear_down(fixture); | |
429 | fixture = NULL; | |
430 | } | |
431 | EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down); | |
432 | return result; | |
433 | } | |
ef071222 | 434 | #endif /* OPENSSL_NO_EC */ |
3dbc5156 DDO |
435 | |
436 | static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture) | |
437 | { | |
438 | X509_STORE *store = X509_STORE_new(); | |
439 | STACK_OF(X509) *sk = NULL; | |
440 | int res = 0; | |
441 | ||
442 | if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store, | |
443 | fixture->certs, | |
444 | fixture->callback_arg))) | |
445 | goto err; | |
31b28ad9 | 446 | sk = X509_STORE_get1_all_certs(store); |
3dbc5156 DDO |
447 | if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain))) |
448 | goto err; | |
449 | res = 1; | |
450 | err: | |
451 | X509_STORE_free(store); | |
452 | sk_X509_pop_free(sk, X509_free); | |
453 | return res; | |
454 | ||
455 | } | |
456 | ||
457 | static int test_X509_STORE(void) | |
458 | { | |
459 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
235595c4 | 460 | fixture->callback_arg = 0; /* self-issued allowed */ |
3dbc5156 DDO |
461 | if (!TEST_ptr(fixture->certs = sk_X509_new_null()) |
462 | || !sk_X509_push(fixture->certs, endentity1) | |
463 | || !sk_X509_push(fixture->certs, endentity2) | |
464 | || !sk_X509_push(fixture->certs, root) | |
465 | || !sk_X509_push(fixture->certs, intermediate) | |
466 | || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) { | |
467 | tear_down(fixture); | |
468 | fixture = NULL; | |
469 | } | |
470 | EXECUTE_TEST(execute_X509_STORE_test, tear_down); | |
471 | return result; | |
472 | } | |
473 | ||
235595c4 | 474 | static int test_X509_STORE_only_self_issued(void) |
3dbc5156 DDO |
475 | { |
476 | SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up); | |
477 | fixture->certs = sk_X509_new_null(); | |
478 | fixture->chain = sk_X509_new_null(); | |
235595c4 | 479 | fixture->callback_arg = 1; /* only self-issued */ |
3dbc5156 DDO |
480 | if (!TEST_true(sk_X509_push(fixture->certs, endentity1)) |
481 | || !TEST_true(sk_X509_push(fixture->certs, endentity2)) | |
482 | || !TEST_true(sk_X509_push(fixture->certs, root)) | |
483 | || !TEST_true(sk_X509_push(fixture->certs, intermediate)) | |
484 | || !TEST_true(sk_X509_push(fixture->chain, root))) { | |
485 | tear_down(fixture); | |
486 | fixture = NULL; | |
487 | } | |
488 | EXECUTE_TEST(execute_X509_STORE_test, tear_down); | |
489 | return result; | |
490 | } | |
491 | ||
492 | ||
493 | void cleanup_tests(void) | |
494 | { | |
495 | EVP_PKEY_free(loadedprivkey); | |
496 | EVP_PKEY_free(loadedpubkey); | |
497 | EVP_PKEY_free(loadedkey); | |
498 | X509_free(cert); | |
499 | X509_free(endentity1); | |
500 | X509_free(endentity2); | |
501 | X509_free(root); | |
502 | X509_free(intermediate); | |
503 | OSSL_CMP_MSG_free(ir_protected); | |
504 | OSSL_CMP_MSG_free(ir_unprotected); | |
4561f15f | 505 | OPENSSL_CTX_free(libctx); |
3dbc5156 DDO |
506 | } |
507 | ||
4561f15f DDO |
508 | #define USAGE "server.pem IR_protected.der IR_unprotected.der IP_PBM.der " \ |
509 | "server.crt server.pem EndEntity1.crt EndEntity2.crt Root_CA.crt " \ | |
510 | "Intermediate_CA.crt module_name [module_conf_file]\n" | |
511 | OPT_TEST_DECLARE_USAGE(USAGE) | |
512 | ||
3dbc5156 DDO |
513 | int setup_tests(void) |
514 | { | |
515 | char *server_f; | |
516 | char *server_key_f; | |
517 | char *server_cert_f; | |
518 | char *endentity1_f; | |
519 | char *endentity2_f; | |
520 | char *root_f; | |
521 | char *intermediate_f; | |
522 | ||
8d242823 MC |
523 | if (!test_skip_common_options()) { |
524 | TEST_error("Error parsing test options\n"); | |
525 | return 0; | |
526 | } | |
527 | ||
3dbc5156 DDO |
528 | RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH); |
529 | if (!TEST_ptr(server_f = test_get_argument(0)) | |
530 | || !TEST_ptr(ir_protected_f = test_get_argument(1)) | |
531 | || !TEST_ptr(ir_unprotected_f = test_get_argument(2)) | |
532 | || !TEST_ptr(ip_PBM_f = test_get_argument(3)) | |
533 | || !TEST_ptr(server_cert_f = test_get_argument(4)) | |
534 | || !TEST_ptr(server_key_f = test_get_argument(5)) | |
535 | || !TEST_ptr(endentity1_f = test_get_argument(6)) | |
536 | || !TEST_ptr(endentity2_f = test_get_argument(7)) | |
537 | || !TEST_ptr(root_f = test_get_argument(8)) | |
538 | || !TEST_ptr(intermediate_f = test_get_argument(9))) { | |
4561f15f | 539 | TEST_error("usage: cmp_protect_test %s", USAGE); |
3dbc5156 DDO |
540 | return 0; |
541 | } | |
4561f15f DDO |
542 | |
543 | if (!test_get_libctx(&libctx, &default_null_provider, &provider, 10, USAGE)) | |
544 | return 0; | |
545 | ||
9afa0748 | 546 | if (!TEST_ptr(loadedkey = load_pem_key(server_key_f, libctx)) |
4561f15f | 547 | || !TEST_ptr(cert = load_pem_cert(server_cert_f, libctx))) |
3dbc5156 DDO |
548 | return 0; |
549 | ||
9afa0748 | 550 | if (!TEST_ptr(loadedprivkey = load_pem_key(server_f, libctx))) |
3dbc5156 DDO |
551 | return 0; |
552 | if (TEST_true(EVP_PKEY_up_ref(loadedprivkey))) | |
553 | loadedpubkey = loadedprivkey; | |
554 | if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f)) | |
555 | || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f))) | |
556 | return 0; | |
4561f15f DDO |
557 | if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f, libctx)) |
558 | || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f, libctx)) | |
559 | || !TEST_ptr(root = load_pem_cert(root_f, libctx)) | |
560 | || !TEST_ptr(intermediate = load_pem_cert(intermediate_f, libctx))) | |
3dbc5156 DDO |
561 | return 0; |
562 | if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH))) | |
563 | return 0; | |
564 | ||
565 | /* Message protection tests */ | |
566 | ADD_TEST(test_cmp_calc_protection_no_key_no_secret); | |
567 | ADD_TEST(test_cmp_calc_protection_pkey); | |
568 | ADD_TEST(test_cmp_calc_protection_pbmac); | |
569 | ||
570 | ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key); | |
571 | ADD_TEST(test_MSG_protect_with_certificate_and_key); | |
572 | ADD_TEST(test_MSG_protect_certificate_based_without_cert); | |
573 | ADD_TEST(test_MSG_protect_unprotected_request); | |
574 | ADD_TEST(test_MSG_protect_no_key_no_secret); | |
cfca56df DDO |
575 | ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref); |
576 | ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref); | |
3dbc5156 DDO |
577 | ADD_TEST(test_MSG_add_extraCerts); |
578 | ||
ef071222 | 579 | #ifndef OPENSSL_NO_EC |
3dbc5156 | 580 | ADD_TEST(test_cmp_build_cert_chain); |
15076c26 | 581 | ADD_TEST(test_cmp_build_cert_chain_no_root); |
3dbc5156 DDO |
582 | ADD_TEST(test_cmp_build_cert_chain_missing_intermediate); |
583 | ADD_TEST(test_cmp_build_cert_chain_no_certs); | |
ef071222 | 584 | #endif |
3dbc5156 DDO |
585 | |
586 | ADD_TEST(test_X509_STORE); | |
235595c4 | 587 | ADD_TEST(test_X509_STORE_only_self_issued); |
3dbc5156 DDO |
588 | |
589 | return 1; | |
590 | } |