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