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