]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/cmp_vfy_test.c
Introduce X509_add_cert[s] simplifying various additions to cert lists
[thirdparty/openssl.git] / test / cmp_vfy_test.c
1 /*
2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
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 #include "../crypto/crmf/crmf_local.h" /* for manipulating POPO signature */
14 DEFINE_STACK_OF(OSSL_CRMF_MSG)
15
16 static const char *server_f;
17 static const char *client_f;
18 static const char *endentity1_f;
19 static const char *endentity2_f;
20 static const char *root_f;
21 static const char *intermediate_f;
22 static const char *ir_protected_f;
23 static const char *ir_unprotected_f;
24 static const char *ir_rmprotection_f;
25 static const char *ip_waiting_f;
26 static const char *instacert_f;
27 static const char *instaca_f;
28 static const char *ir_protected_0_extracerts;
29 static const char *ir_protected_2_extracerts;
30
31 typedef struct test_fixture {
32 const char *test_case_name;
33 int expected;
34 OSSL_CMP_CTX *cmp_ctx;
35 OSSL_CMP_MSG *msg;
36 X509 *cert;
37 ossl_cmp_allow_unprotected_cb_t allow_unprotected_cb;
38 int additional_arg;
39 } CMP_VFY_TEST_FIXTURE;
40
41 static void tear_down(CMP_VFY_TEST_FIXTURE *fixture)
42 {
43 OSSL_CMP_MSG_free(fixture->msg);
44 OSSL_CMP_CTX_free(fixture->cmp_ctx);
45 OPENSSL_free(fixture);
46 }
47
48 static time_t test_time_valid = 0, test_time_after_expiration = 0;
49
50 static CMP_VFY_TEST_FIXTURE *set_up(const char *const test_case_name)
51 {
52 X509_STORE *ts = X509_STORE_new();
53 CMP_VFY_TEST_FIXTURE *fixture;
54
55 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
56 return NULL;
57 fixture->test_case_name = test_case_name;
58 if (ts == NULL
59 || !TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new())
60 || !OSSL_CMP_CTX_set0_trustedStore(fixture->cmp_ctx, ts)
61 || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)) {
62 tear_down(fixture);
63 X509_STORE_free(ts);
64 return NULL;
65 }
66 X509_VERIFY_PARAM_set_time(X509_STORE_get0_param(ts), test_time_valid);
67 X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb);
68 return fixture;
69 }
70
71 static X509 *srvcert = NULL;
72 static X509 *clcert = NULL;
73 /* chain */
74 static X509 *endentity1 = NULL, *endentity2 = NULL,
75 *intermediate = NULL, *root = NULL;
76 /* INSTA chain */
77 static X509 *insta_cert = NULL, *instaca_cert = NULL;
78
79 static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
80 static OSSL_CMP_MSG *ir_unprotected, *ir_rmprotection;
81
82 static int flip_bit(ASN1_BIT_STRING *bitstr)
83 {
84 int bit_num = 7;
85 int bit = ASN1_BIT_STRING_get_bit(bitstr, bit_num);
86
87 return ASN1_BIT_STRING_set_bit(bitstr, bit_num, !bit);
88 }
89
90 static int execute_verify_popo_test(CMP_VFY_TEST_FIXTURE *fixture)
91 {
92 if ((fixture->msg = load_pkimsg(ir_protected_f)) == NULL)
93 return 0;
94 if (fixture->expected == 0) {
95 const OSSL_CRMF_MSGS *reqs = fixture->msg->body->value.ir;
96 const OSSL_CRMF_MSG *req = sk_OSSL_CRMF_MSG_value(reqs, 0);
97 if (req == NULL || !flip_bit(req->popo->value.signature->signature))
98 return 0;
99 }
100 return TEST_int_eq(fixture->expected,
101 ossl_cmp_verify_popo(fixture->msg,
102 fixture->additional_arg));
103 }
104
105 static int test_verify_popo(void)
106 {
107 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
108 fixture->expected = 1;
109 EXECUTE_TEST(execute_verify_popo_test, tear_down);
110 return result;
111 }
112
113 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
114 static int test_verify_popo_bad(void)
115 {
116 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
117 fixture->expected = 0;
118 EXECUTE_TEST(execute_verify_popo_test, tear_down);
119 return result;
120 }
121 #endif
122
123 static int execute_validate_msg_test(CMP_VFY_TEST_FIXTURE *fixture)
124 {
125 return TEST_int_eq(fixture->expected,
126 ossl_cmp_msg_check_update(fixture->cmp_ctx, fixture->msg,
127 NULL, 0));
128 }
129
130 static int execute_validate_cert_path_test(CMP_VFY_TEST_FIXTURE *fixture)
131 {
132 X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore(fixture->cmp_ctx);
133 int res = TEST_int_eq(fixture->expected,
134 OSSL_CMP_validate_cert_path(fixture->cmp_ctx,
135 ts, fixture->cert));
136
137 OSSL_CMP_CTX_print_errors(fixture->cmp_ctx);
138 return res;
139 }
140
141 static int test_validate_msg_mac_alg_protection(void)
142 {
143 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
144 /* secret value belonging to cmp-test/CMP_IP_waitingStatus_PBM.der */
145 const unsigned char sec_1[] = {
146 '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3',
147 'Q', '-', 'u', 'd', 'N', 'R'
148 };
149
150 fixture->expected = 1;
151 if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_1,
152 sizeof(sec_1)))
153 || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f))) {
154 tear_down(fixture);
155 fixture = NULL;
156 }
157 EXECUTE_TEST(execute_validate_msg_test, tear_down);
158 return result;
159 }
160
161 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
162 static int test_validate_msg_mac_alg_protection_bad(void)
163 {
164 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
165 const unsigned char sec_bad[] = {
166 '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3',
167 'Q', '-', 'u', 'd', 'N', 'r'
168 };
169 fixture->expected = 0;
170
171 if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_bad,
172 sizeof(sec_bad)))
173 || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f))) {
174 tear_down(fixture);
175 fixture = NULL;
176 }
177 EXECUTE_TEST(execute_validate_msg_test, tear_down);
178 return result;
179 }
180 #endif
181
182 static int add_trusted(OSSL_CMP_CTX *ctx, X509 *cert)
183 {
184 return X509_STORE_add_cert(OSSL_CMP_CTX_get0_trustedStore(ctx), cert);
185 }
186
187 static int add_untrusted(OSSL_CMP_CTX *ctx, X509 *cert)
188 {
189 return X509_add_cert(OSSL_CMP_CTX_get0_untrusted_certs(ctx), cert,
190 X509_ADD_FLAG_UP_REF);
191 }
192
193 static int test_validate_msg_signature_partial_chain(int expired)
194 {
195 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
196 X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore(fixture->cmp_ctx);
197
198 fixture->expected = !expired;
199 if (ts == NULL
200 || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
201 || !add_trusted(fixture->cmp_ctx, srvcert)) {
202 tear_down(fixture);
203 fixture = NULL;
204 } else {
205 X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
206 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
207 if (expired)
208 X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration);
209 }
210 EXECUTE_TEST(execute_validate_msg_test, tear_down);
211 return result;
212 }
213
214 static int test_validate_msg_signature_trusted_ok(void)
215 {
216 return test_validate_msg_signature_partial_chain(0);
217 }
218
219 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
220 static int test_validate_msg_signature_trusted_expired(void)
221 {
222 return test_validate_msg_signature_partial_chain(1);
223 }
224 #endif
225
226 static int test_validate_msg_signature_srvcert_wrong(void)
227 {
228 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
229 fixture->expected = 0;
230 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
231 || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, clcert))) {
232 tear_down(fixture);
233 fixture = NULL;
234 }
235 EXECUTE_TEST(execute_validate_msg_test, tear_down);
236 return result;
237 }
238
239 static int test_validate_msg_signature_srvcert(int bad_sig)
240 {
241 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
242 fixture->expected = !bad_sig;
243 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
244 || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, srvcert))
245 || (bad_sig && !flip_bit(fixture->msg->protection))) {
246 tear_down(fixture);
247 fixture = NULL;
248 }
249 EXECUTE_TEST(execute_validate_msg_test, tear_down);
250 return result;
251 }
252
253 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
254 static int test_validate_msg_signature_bad(void)
255 {
256 return test_validate_msg_signature_srvcert(1);
257 }
258 #endif
259
260 static int test_validate_msg_signature_sender_cert_srvcert(void)
261 {
262 return test_validate_msg_signature_srvcert(0);
263 }
264
265 static int test_validate_msg_signature_sender_cert_untrusted(void)
266 {
267 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
268 fixture->expected = 1;
269 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts))
270 || !add_trusted(fixture->cmp_ctx, instaca_cert)
271 || !add_untrusted(fixture->cmp_ctx, insta_cert)) {
272 tear_down(fixture);
273 fixture = NULL;
274 }
275 EXECUTE_TEST(execute_validate_msg_test, tear_down);
276 return result;
277 }
278
279 static int test_validate_msg_signature_sender_cert_trusted(void)
280 {
281 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
282 fixture->expected = 1;
283 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts))
284 || !add_trusted(fixture->cmp_ctx, instaca_cert)
285 || !add_trusted(fixture->cmp_ctx, insta_cert)) {
286 tear_down(fixture);
287 fixture = NULL;
288 }
289 EXECUTE_TEST(execute_validate_msg_test, tear_down);
290 return result;
291 }
292
293 static int test_validate_msg_signature_sender_cert_extracert(void)
294 {
295 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
296 fixture->expected = 1;
297 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_2_extracerts))
298 || !add_trusted(fixture->cmp_ctx, instaca_cert)) {
299 tear_down(fixture);
300 fixture = NULL;
301 }
302 EXECUTE_TEST(execute_validate_msg_test, tear_down);
303 return result;
304 }
305
306
307 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
308 static int test_validate_msg_signature_sender_cert_absent(void)
309 {
310 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
311 fixture->expected = 0;
312 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts))) {
313 tear_down(fixture);
314 fixture = NULL;
315 }
316 EXECUTE_TEST(execute_validate_msg_test, tear_down);
317 return result;
318 }
319 #endif
320
321 static int test_validate_with_sender(const X509_NAME *name, int expected)
322 {
323 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
324 fixture->expected = expected;
325 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
326 || !TEST_true(OSSL_CMP_CTX_set1_expected_sender(fixture->cmp_ctx, name))
327 || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, srvcert))) {
328 tear_down(fixture);
329 fixture = NULL;
330 }
331 EXECUTE_TEST(execute_validate_msg_test, tear_down);
332 return result;
333 }
334
335 static int test_validate_msg_signature_expected_sender(void)
336 {
337 return test_validate_with_sender(X509_get_subject_name(srvcert), 1);
338 }
339
340 static int test_validate_msg_signature_unexpected_sender(void)
341 {
342 return test_validate_with_sender(X509_get_subject_name(root), 0);
343 }
344
345 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
346 static int test_validate_msg_unprotected_request(void)
347 {
348 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
349 fixture->expected = 0;
350 if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f))) {
351 tear_down(fixture);
352 fixture = NULL;
353 }
354 EXECUTE_TEST(execute_validate_msg_test, tear_down);
355 return result;
356 }
357 #endif
358
359 static void setup_path(CMP_VFY_TEST_FIXTURE **fixture, X509 *wrong, int expired)
360 {
361 (*fixture)->cert = endentity2;
362 (*fixture)->expected = wrong == NULL && !expired;
363 if (expired) {
364 X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore((*fixture)->cmp_ctx);
365 X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
366 X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration);
367 }
368 if (!add_trusted((*fixture)->cmp_ctx, wrong == NULL ? root : wrong)
369 || !add_untrusted((*fixture)->cmp_ctx, endentity1)
370 || !add_untrusted((*fixture)->cmp_ctx, intermediate)) {
371 tear_down((*fixture));
372 (*fixture) = NULL;
373 }
374 }
375
376 static int test_validate_cert_path_ok(void)
377 {
378 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
379 setup_path(&fixture, NULL, 0);
380 EXECUTE_TEST(execute_validate_cert_path_test, tear_down);
381 return result;
382 }
383
384 static int test_validate_cert_path_wrong_anchor(void)
385 {
386 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
387 setup_path(&fixture, srvcert /* wrong/non-root cert */, 0);
388 EXECUTE_TEST(execute_validate_cert_path_test, tear_down);
389 return result;
390 }
391
392 static int test_validate_cert_path_expired(void)
393 {
394 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
395 setup_path(&fixture, NULL, 1);
396 EXECUTE_TEST(execute_validate_cert_path_test, tear_down);
397 return result;
398 }
399
400 static int execute_msg_check_test(CMP_VFY_TEST_FIXTURE *fixture)
401 {
402 const OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(fixture->msg);
403 const ASN1_OCTET_STRING *tid = OSSL_CMP_HDR_get0_transactionID(hdr);
404
405 if (!TEST_int_eq(fixture->expected,
406 ossl_cmp_msg_check_update(fixture->cmp_ctx,
407 fixture->msg,
408 fixture->allow_unprotected_cb,
409 fixture->additional_arg)))
410 return 0;
411
412 if (fixture->expected == 0) /* error expected aready during above check */
413 return 1;
414 return
415 TEST_int_eq(0,
416 ASN1_OCTET_STRING_cmp(ossl_cmp_hdr_get0_senderNonce(hdr),
417 fixture->cmp_ctx->recipNonce))
418 && TEST_int_eq(0,
419 ASN1_OCTET_STRING_cmp(tid,
420 fixture->cmp_ctx->transactionID));
421 }
422
423 static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg,
424 int invalid_protection, int allow)
425 {
426 return allow;
427 }
428
429 static void setup_check_update(CMP_VFY_TEST_FIXTURE **fixture, int expected,
430 ossl_cmp_allow_unprotected_cb_t cb, int arg,
431 const unsigned char *trid_data,
432 const unsigned char *nonce_data)
433 {
434 OSSL_CMP_CTX *ctx = (*fixture)->cmp_ctx;
435 int nonce_len = OSSL_CMP_SENDERNONCE_LENGTH;
436
437 (*fixture)->expected = expected;
438 (*fixture)->allow_unprotected_cb = cb;
439 (*fixture)->additional_arg = arg;
440 (*fixture)->msg = OSSL_CMP_MSG_dup(ir_rmprotection);
441 if ((*fixture)->msg == NULL
442 || (nonce_data != NULL
443 && !ossl_cmp_asn1_octet_string_set1_bytes(&ctx->senderNonce,
444 nonce_data, nonce_len))) {
445 tear_down((*fixture));
446 (*fixture) = NULL;
447 } else if (trid_data != NULL) {
448 ASN1_OCTET_STRING *trid = ASN1_OCTET_STRING_new();
449 if (trid == NULL
450 || !ASN1_OCTET_STRING_set(trid, trid_data,
451 OSSL_CMP_TRANSACTIONID_LENGTH)
452 || !OSSL_CMP_CTX_set1_transactionID(ctx, trid)) {
453 tear_down((*fixture));
454 (*fixture) = NULL;
455 }
456 ASN1_OCTET_STRING_free(trid);
457 }
458 }
459
460 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
461 static int test_msg_check_no_protection_no_cb(void)
462 {
463 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
464 setup_check_update(&fixture, 0, NULL, 0, NULL, NULL);
465 EXECUTE_TEST(execute_msg_check_test, tear_down);
466 return result;
467 }
468
469 static int test_msg_check_no_protection_restrictive_cb(void)
470 {
471 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
472 setup_check_update(&fixture, 0, allow_unprotected, 0, NULL, NULL);
473 EXECUTE_TEST(execute_msg_check_test, tear_down);
474 return result;
475 }
476 #endif
477
478 static int test_msg_check_no_protection_permissive_cb(void)
479 {
480 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
481 setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, NULL);
482 EXECUTE_TEST(execute_msg_check_test, tear_down);
483 return result;
484 }
485
486 static int test_msg_check_transaction_id(void)
487 {
488 /* Transaction id belonging to CMP_IR_rmprotection.der */
489 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
490 0x39, 0xB6, 0x90, 0x28, 0xC4, 0xBC, 0x7A, 0xF6,
491 0xBE, 0xC6, 0x4A, 0x88, 0x97, 0xA6, 0x95, 0x0B
492 };
493
494 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
495 setup_check_update(&fixture, 1, allow_unprotected, 1, trans_id, NULL);
496 EXECUTE_TEST(execute_msg_check_test, tear_down);
497 return result;
498 }
499
500 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
501 static int test_msg_check_transaction_id_bad(void)
502 {
503 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
504 setup_check_update(&fixture, 0, allow_unprotected, 1, rand_data, NULL);
505 EXECUTE_TEST(execute_msg_check_test, tear_down);
506 return result;
507 }
508 #endif
509
510 static int test_msg_check_recipient_nonce(void)
511 {
512 /* Recipient nonce belonging to CMP_IP_ir_rmprotection.der */
513 const unsigned char rec_nonce[OSSL_CMP_SENDERNONCE_LENGTH] = {
514 0x48, 0xF1, 0x71, 0x1F, 0xE5, 0xAF, 0x1C, 0x8B,
515 0x21, 0x97, 0x5C, 0x84, 0x74, 0x49, 0xBA, 0x32
516 };
517
518 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
519 setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, rec_nonce);
520 EXECUTE_TEST(execute_msg_check_test, tear_down);
521 return result;
522 }
523
524 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
525 static int test_msg_check_recipient_nonce_bad(void)
526 {
527 SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
528 setup_check_update(&fixture, 0, allow_unprotected, 1, NULL, rand_data);
529 EXECUTE_TEST(execute_msg_check_test, tear_down);
530 return result;
531 }
532 #endif
533
534 void cleanup_tests(void)
535 {
536 X509_free(srvcert);
537 X509_free(clcert);
538 X509_free(endentity1);
539 X509_free(endentity2);
540 X509_free(intermediate);
541 X509_free(root);
542 X509_free(insta_cert);
543 X509_free(instaca_cert);
544 OSSL_CMP_MSG_free(ir_unprotected);
545 OSSL_CMP_MSG_free(ir_rmprotection);
546 return;
547 }
548
549 int setup_tests(void)
550 {
551 /* Set test time stamps */
552 struct tm ts = { 0 };
553
554 ts.tm_year = 2018 - 1900; /* 2018 */
555 ts.tm_mon = 1; /* February */
556 ts.tm_mday = 18; /* 18th */
557 test_time_valid = mktime(&ts); /* February 18th 2018 */
558 ts.tm_year += 10; /* February 18th 2028 */
559 test_time_after_expiration = mktime(&ts);
560
561 if (!test_skip_common_options()) {
562 TEST_error("Error parsing test options\n");
563 return 0;
564 }
565
566 RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
567 if (!TEST_ptr(server_f = test_get_argument(0))
568 || !TEST_ptr(client_f = test_get_argument(1))
569 || !TEST_ptr(endentity1_f = test_get_argument(2))
570 || !TEST_ptr(endentity2_f = test_get_argument(3))
571 || !TEST_ptr(root_f = test_get_argument(4))
572 || !TEST_ptr(intermediate_f = test_get_argument(5))
573 || !TEST_ptr(ir_protected_f = test_get_argument(6))
574 || !TEST_ptr(ir_unprotected_f = test_get_argument(7))
575 || !TEST_ptr(ip_waiting_f = test_get_argument(8))
576 || !TEST_ptr(ir_rmprotection_f = test_get_argument(9))
577 || !TEST_ptr(instacert_f = test_get_argument(10))
578 || !TEST_ptr(instaca_f = test_get_argument(11))
579 || !TEST_ptr(ir_protected_0_extracerts = test_get_argument(12))
580 || !TEST_ptr(ir_protected_2_extracerts = test_get_argument(13))) {
581 TEST_error("usage: cmp_vfy_test server.crt client.crt "
582 "EndEntity1.crt EndEntity2.crt "
583 "Root_CA.crt Intermediate_CA.crt "
584 "CMP_IR_protected.der CMP_IR_unprotected.der "
585 "IP_waitingStatus_PBM.der IR_rmprotection.der "
586 "insta.cert.pem insta_ca.cert.pem "
587 "IR_protected_0_extraCerts.der "
588 "IR_protected_2_extraCerts.der\n");
589 return 0;
590 }
591
592 /* Load certificates for cert chain */
593 if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f))
594 || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f))
595 || !TEST_ptr(root = load_pem_cert(root_f))
596 || !TEST_ptr(intermediate = load_pem_cert(intermediate_f)))
597 goto err;
598
599 if (!TEST_ptr(insta_cert = load_pem_cert(instacert_f))
600 || !TEST_ptr(instaca_cert = load_pem_cert(instaca_f)))
601 goto err;
602
603 /* Load certificates for message validation */
604 if (!TEST_ptr(srvcert = load_pem_cert(server_f))
605 || !TEST_ptr(clcert = load_pem_cert(client_f)))
606 goto err;
607 if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
608 goto err;
609 if (!TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f))
610 || !TEST_ptr(ir_rmprotection = load_pkimsg(ir_rmprotection_f)))
611 goto err;
612
613 /* Message validation tests */
614 ADD_TEST(test_verify_popo);
615 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
616 ADD_TEST(test_verify_popo_bad);
617 #endif
618 ADD_TEST(test_validate_msg_signature_trusted_ok);
619 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
620 ADD_TEST(test_validate_msg_signature_trusted_expired);
621 #endif
622 ADD_TEST(test_validate_msg_signature_srvcert_wrong);
623 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
624 ADD_TEST(test_validate_msg_signature_bad);
625 #endif
626 ADD_TEST(test_validate_msg_signature_sender_cert_srvcert);
627 ADD_TEST(test_validate_msg_signature_sender_cert_untrusted);
628 ADD_TEST(test_validate_msg_signature_sender_cert_trusted);
629 ADD_TEST(test_validate_msg_signature_sender_cert_extracert);
630 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
631 ADD_TEST(test_validate_msg_signature_sender_cert_absent);
632 #endif
633 ADD_TEST(test_validate_msg_signature_expected_sender);
634 ADD_TEST(test_validate_msg_signature_unexpected_sender);
635 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
636 ADD_TEST(test_validate_msg_unprotected_request);
637 #endif
638 ADD_TEST(test_validate_msg_mac_alg_protection);
639 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
640 ADD_TEST(test_validate_msg_mac_alg_protection_bad);
641 #endif
642
643 /* Cert path validation tests */
644 ADD_TEST(test_validate_cert_path_ok);
645 ADD_TEST(test_validate_cert_path_expired);
646 ADD_TEST(test_validate_cert_path_wrong_anchor);
647
648 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
649 ADD_TEST(test_msg_check_no_protection_no_cb);
650 ADD_TEST(test_msg_check_no_protection_restrictive_cb);
651 #endif
652 ADD_TEST(test_msg_check_no_protection_permissive_cb);
653 ADD_TEST(test_msg_check_transaction_id);
654 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
655 ADD_TEST(test_msg_check_transaction_id_bad);
656 #endif
657 ADD_TEST(test_msg_check_recipient_nonce);
658 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
659 ADD_TEST(test_msg_check_recipient_nonce_bad);
660 #endif
661
662 return 1;
663
664 err:
665 cleanup_tests();
666 return 0;
667
668 }