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