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