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