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