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
14e1051a39Sopenharmony_cistatic const char *newkey_f;
15e1051a39Sopenharmony_cistatic const char *server_cert_f;
16e1051a39Sopenharmony_cistatic const char *pkcs10_f;
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_citypedef struct test_fixture {
19e1051a39Sopenharmony_ci    const char *test_case_name;
20e1051a39Sopenharmony_ci    OSSL_CMP_CTX *cmp_ctx;
21e1051a39Sopenharmony_ci    /* for msg create tests */
22e1051a39Sopenharmony_ci    int bodytype;
23e1051a39Sopenharmony_ci    int err_code;
24e1051a39Sopenharmony_ci    /* for certConf */
25e1051a39Sopenharmony_ci    int fail_info;
26e1051a39Sopenharmony_ci    /* for protection tests */
27e1051a39Sopenharmony_ci    OSSL_CMP_MSG *msg;
28e1051a39Sopenharmony_ci    int expected;
29e1051a39Sopenharmony_ci    /* for error and response messages */
30e1051a39Sopenharmony_ci    OSSL_CMP_PKISI *si;
31e1051a39Sopenharmony_ci} CMP_MSG_TEST_FIXTURE;
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_cistatic OSSL_LIB_CTX *libctx = NULL;
34e1051a39Sopenharmony_cistatic OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_cistatic unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_cistatic void tear_down(CMP_MSG_TEST_FIXTURE *fixture)
39e1051a39Sopenharmony_ci{
40e1051a39Sopenharmony_ci    OSSL_CMP_CTX_free(fixture->cmp_ctx);
41e1051a39Sopenharmony_ci    OSSL_CMP_MSG_free(fixture->msg);
42e1051a39Sopenharmony_ci    OSSL_CMP_PKISI_free(fixture->si);
43e1051a39Sopenharmony_ci    OPENSSL_free(fixture);
44e1051a39Sopenharmony_ci}
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci#define SET_OPT_UNPROTECTED_SEND(ctx, val) \
47e1051a39Sopenharmony_ci    OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
48e1051a39Sopenharmony_cistatic CMP_MSG_TEST_FIXTURE *set_up(const char *const test_case_name)
49e1051a39Sopenharmony_ci{
50e1051a39Sopenharmony_ci    CMP_MSG_TEST_FIXTURE *fixture;
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
53e1051a39Sopenharmony_ci        return NULL;
54e1051a39Sopenharmony_ci    fixture->test_case_name = test_case_name;
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci    if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))
57e1051a39Sopenharmony_ci            || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))
58e1051a39Sopenharmony_ci            || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
59e1051a39Sopenharmony_ci                                                           ref, sizeof(ref)))) {
60e1051a39Sopenharmony_ci        tear_down(fixture);
61e1051a39Sopenharmony_ci        return NULL;
62e1051a39Sopenharmony_ci    }
63e1051a39Sopenharmony_ci    return fixture;
64e1051a39Sopenharmony_ci}
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_cistatic EVP_PKEY *newkey = NULL;
67e1051a39Sopenharmony_cistatic X509 *cert = NULL;
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci#define EXECUTE_MSG_CREATION_TEST(expr) \
70e1051a39Sopenharmony_ci    do { \
71e1051a39Sopenharmony_ci        OSSL_CMP_MSG *msg = NULL; \
72e1051a39Sopenharmony_ci        int good = fixture->expected != 0 ? \
73e1051a39Sopenharmony_ci            TEST_ptr(msg = (expr)) && TEST_true(valid_asn1_encoding(msg)) : \
74e1051a39Sopenharmony_ci            TEST_ptr_null(msg = (expr)); \
75e1051a39Sopenharmony_ci \
76e1051a39Sopenharmony_ci        OSSL_CMP_MSG_free(msg); \
77e1051a39Sopenharmony_ci        ERR_print_errors_fp(stderr); \
78e1051a39Sopenharmony_ci        return good; \
79e1051a39Sopenharmony_ci    } while (0)
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci/*-
82e1051a39Sopenharmony_ci * The following tests call a cmp message creation function.
83e1051a39Sopenharmony_ci * if fixture->expected != 0:
84e1051a39Sopenharmony_ci *         returns 1 if the message is created and syntactically correct.
85e1051a39Sopenharmony_ci * if fixture->expected == 0
86e1051a39Sopenharmony_ci *         returns 1 if message creation returns NULL
87e1051a39Sopenharmony_ci */
88e1051a39Sopenharmony_cistatic int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
89e1051a39Sopenharmony_ci{
90e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture->cmp_ctx,
91e1051a39Sopenharmony_ci                                                   fixture->bodytype,
92e1051a39Sopenharmony_ci                                                   NULL));
93e1051a39Sopenharmony_ci}
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_cistatic int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE *fixture)
96e1051a39Sopenharmony_ci{
97e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture->cmp_ctx, fixture->si,
98e1051a39Sopenharmony_ci                                                 fixture->err_code,
99e1051a39Sopenharmony_ci                                                 "details", 0));
100e1051a39Sopenharmony_ci}
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_cistatic int execute_rr_create_test(CMP_MSG_TEST_FIXTURE *fixture)
103e1051a39Sopenharmony_ci{
104e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture->cmp_ctx));
105e1051a39Sopenharmony_ci}
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_cistatic int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE *fixture)
108e1051a39Sopenharmony_ci{
109e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new
110e1051a39Sopenharmony_ci                              (fixture->cmp_ctx, OSSL_CMP_CERTREQID,
111e1051a39Sopenharmony_ci                               fixture->fail_info, NULL));
112e1051a39Sopenharmony_ci}
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_cistatic int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture)
115e1051a39Sopenharmony_ci{
116e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx));
117e1051a39Sopenharmony_ci}
118e1051a39Sopenharmony_ci
119e1051a39Sopenharmony_cistatic int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
120e1051a39Sopenharmony_ci{
121e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711));
122e1051a39Sopenharmony_ci}
123e1051a39Sopenharmony_ci
124e1051a39Sopenharmony_cistatic int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture)
125e1051a39Sopenharmony_ci{
126e1051a39Sopenharmony_ci    EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
127e1051a39Sopenharmony_ci                              (fixture->cmp_ctx, fixture->bodytype));
128e1051a39Sopenharmony_ci}
129e1051a39Sopenharmony_ci
130e1051a39Sopenharmony_cistatic int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
131e1051a39Sopenharmony_ci{
132e1051a39Sopenharmony_ci    if (!EVP_PKEY_up_ref(pkey))
133e1051a39Sopenharmony_ci        return 0;
134e1051a39Sopenharmony_ci
135e1051a39Sopenharmony_ci    if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) {
136e1051a39Sopenharmony_ci        EVP_PKEY_free(pkey);
137e1051a39Sopenharmony_ci        return 0;
138e1051a39Sopenharmony_ci    }
139e1051a39Sopenharmony_ci    return 1;
140e1051a39Sopenharmony_ci}
141e1051a39Sopenharmony_ci
142e1051a39Sopenharmony_cistatic int test_cmp_create_ir_protection_set(void)
143e1051a39Sopenharmony_ci{
144e1051a39Sopenharmony_ci    OSSL_CMP_CTX *ctx;
145e1051a39Sopenharmony_ci    unsigned char secret[16];
146e1051a39Sopenharmony_ci
147e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ci    ctx = fixture->cmp_ctx;
150e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_IR;
151e1051a39Sopenharmony_ci    fixture->err_code = -1;
152e1051a39Sopenharmony_ci    fixture->expected = 1;
153e1051a39Sopenharmony_ci    if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0))
154e1051a39Sopenharmony_ci            || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
155e1051a39Sopenharmony_ci            || !TEST_true(set1_newPkey(ctx, newkey))
156e1051a39Sopenharmony_ci            || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret,
157e1051a39Sopenharmony_ci                                                        sizeof(secret)))) {
158e1051a39Sopenharmony_ci        tear_down(fixture);
159e1051a39Sopenharmony_ci        fixture = NULL;
160e1051a39Sopenharmony_ci    }
161e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
162e1051a39Sopenharmony_ci    return result;
163e1051a39Sopenharmony_ci}
164e1051a39Sopenharmony_ci
165e1051a39Sopenharmony_cistatic int test_cmp_create_ir_protection_fails(void)
166e1051a39Sopenharmony_ci{
167e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
168e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_IR;
169e1051a39Sopenharmony_ci    fixture->err_code = -1;
170e1051a39Sopenharmony_ci    fixture->expected = 0;
171e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey))
172e1051a39Sopenharmony_ci            || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
173e1051a39Sopenharmony_ci            /* newkey used by default for signing does not match cert: */
174e1051a39Sopenharmony_ci            || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
175e1051a39Sopenharmony_ci        tear_down(fixture);
176e1051a39Sopenharmony_ci        fixture = NULL;
177e1051a39Sopenharmony_ci    }
178e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
179e1051a39Sopenharmony_ci    return result;
180e1051a39Sopenharmony_ci}
181e1051a39Sopenharmony_ci
182e1051a39Sopenharmony_cistatic int test_cmp_create_cr_without_key(void)
183e1051a39Sopenharmony_ci{
184e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
185e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_CR;
186e1051a39Sopenharmony_ci    fixture->err_code = -1;
187e1051a39Sopenharmony_ci    fixture->expected = 0;
188e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
189e1051a39Sopenharmony_ci    return result;
190e1051a39Sopenharmony_ci}
191e1051a39Sopenharmony_ci
192e1051a39Sopenharmony_cistatic int test_cmp_create_cr(void)
193e1051a39Sopenharmony_ci{
194e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
195e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_CR;
196e1051a39Sopenharmony_ci    fixture->err_code = -1;
197e1051a39Sopenharmony_ci    fixture->expected = 1;
198e1051a39Sopenharmony_ci    if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
199e1051a39Sopenharmony_ci        tear_down(fixture);
200e1051a39Sopenharmony_ci        fixture = NULL;
201e1051a39Sopenharmony_ci    }
202e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
203e1051a39Sopenharmony_ci    return result;
204e1051a39Sopenharmony_ci}
205e1051a39Sopenharmony_ci
206e1051a39Sopenharmony_cistatic int test_cmp_create_certreq_with_invalid_bodytype(void)
207e1051a39Sopenharmony_ci{
208e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
209e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_RR;
210e1051a39Sopenharmony_ci    fixture->err_code = -1;
211e1051a39Sopenharmony_ci    fixture->expected = 0;
212e1051a39Sopenharmony_ci    if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
213e1051a39Sopenharmony_ci        tear_down(fixture);
214e1051a39Sopenharmony_ci        fixture = NULL;
215e1051a39Sopenharmony_ci    }
216e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
217e1051a39Sopenharmony_ci    return result;
218e1051a39Sopenharmony_ci}
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_cistatic int test_cmp_create_p10cr(void)
221e1051a39Sopenharmony_ci{
222e1051a39Sopenharmony_ci    OSSL_CMP_CTX *ctx;
223e1051a39Sopenharmony_ci    X509_REQ *p10cr = NULL;
224e1051a39Sopenharmony_ci
225e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
226e1051a39Sopenharmony_ci    ctx = fixture->cmp_ctx;
227e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
228e1051a39Sopenharmony_ci    fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
229e1051a39Sopenharmony_ci    fixture->expected = 1;
230e1051a39Sopenharmony_ci    if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx))
231e1051a39Sopenharmony_ci            || !TEST_true(set1_newPkey(ctx, newkey))
232e1051a39Sopenharmony_ci            || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) {
233e1051a39Sopenharmony_ci        tear_down(fixture);
234e1051a39Sopenharmony_ci        fixture = NULL;
235e1051a39Sopenharmony_ci    }
236e1051a39Sopenharmony_ci    X509_REQ_free(p10cr);
237e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
238e1051a39Sopenharmony_ci    return result;
239e1051a39Sopenharmony_ci}
240e1051a39Sopenharmony_ci
241e1051a39Sopenharmony_cistatic int test_cmp_create_p10cr_null(void)
242e1051a39Sopenharmony_ci{
243e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
244e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
245e1051a39Sopenharmony_ci    fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
246e1051a39Sopenharmony_ci    fixture->expected = 0;
247e1051a39Sopenharmony_ci    if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
248e1051a39Sopenharmony_ci        tear_down(fixture);
249e1051a39Sopenharmony_ci        fixture = NULL;
250e1051a39Sopenharmony_ci    }
251e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
252e1051a39Sopenharmony_ci    return result;
253e1051a39Sopenharmony_ci}
254e1051a39Sopenharmony_ci
255e1051a39Sopenharmony_cistatic int test_cmp_create_kur(void)
256e1051a39Sopenharmony_ci{
257e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
258e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
259e1051a39Sopenharmony_ci    fixture->err_code = -1;
260e1051a39Sopenharmony_ci    fixture->expected = 1;
261e1051a39Sopenharmony_ci    if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))
262e1051a39Sopenharmony_ci            || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
263e1051a39Sopenharmony_ci        tear_down(fixture);
264e1051a39Sopenharmony_ci        fixture = NULL;
265e1051a39Sopenharmony_ci    }
266e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
267e1051a39Sopenharmony_ci    return result;
268e1051a39Sopenharmony_ci}
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_cistatic int test_cmp_create_kur_without_oldcert(void)
271e1051a39Sopenharmony_ci{
272e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
273e1051a39Sopenharmony_ci    fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
274e1051a39Sopenharmony_ci    fixture->err_code = -1;
275e1051a39Sopenharmony_ci    fixture->expected = 0;
276e1051a39Sopenharmony_ci    if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
277e1051a39Sopenharmony_ci        tear_down(fixture);
278e1051a39Sopenharmony_ci        fixture = NULL;
279e1051a39Sopenharmony_ci    }
280e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certreq_create_test, tear_down);
281e1051a39Sopenharmony_ci    return result;
282e1051a39Sopenharmony_ci}
283e1051a39Sopenharmony_ci
284e1051a39Sopenharmony_cistatic int test_cmp_create_certconf(void)
285e1051a39Sopenharmony_ci{
286e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
287e1051a39Sopenharmony_ci    fixture->fail_info = 0;
288e1051a39Sopenharmony_ci    fixture->expected = 1;
289e1051a39Sopenharmony_ci    if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
290e1051a39Sopenharmony_ci                                             X509_dup(cert)))) {
291e1051a39Sopenharmony_ci        tear_down(fixture);
292e1051a39Sopenharmony_ci        fixture = NULL;
293e1051a39Sopenharmony_ci    }
294e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certconf_create_test, tear_down);
295e1051a39Sopenharmony_ci    return result;
296e1051a39Sopenharmony_ci}
297e1051a39Sopenharmony_ci
298e1051a39Sopenharmony_cistatic int test_cmp_create_certconf_badAlg(void)
299e1051a39Sopenharmony_ci{
300e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
301e1051a39Sopenharmony_ci    fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg;
302e1051a39Sopenharmony_ci    fixture->expected = 1;
303e1051a39Sopenharmony_ci    if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
304e1051a39Sopenharmony_ci                                             X509_dup(cert)))) {
305e1051a39Sopenharmony_ci        tear_down(fixture);
306e1051a39Sopenharmony_ci        fixture = NULL;
307e1051a39Sopenharmony_ci    }
308e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certconf_create_test, tear_down);
309e1051a39Sopenharmony_ci    return result;
310e1051a39Sopenharmony_ci}
311e1051a39Sopenharmony_ci
312e1051a39Sopenharmony_cistatic int test_cmp_create_certconf_fail_info_max(void)
313e1051a39Sopenharmony_ci{
314e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
315e1051a39Sopenharmony_ci    fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX;
316e1051a39Sopenharmony_ci    fixture->expected = 1;
317e1051a39Sopenharmony_ci    if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
318e1051a39Sopenharmony_ci                                             X509_dup(cert)))) {
319e1051a39Sopenharmony_ci        tear_down(fixture);
320e1051a39Sopenharmony_ci        fixture = NULL;
321e1051a39Sopenharmony_ci    }
322e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certconf_create_test, tear_down);
323e1051a39Sopenharmony_ci    return result;
324e1051a39Sopenharmony_ci}
325e1051a39Sopenharmony_ci
326e1051a39Sopenharmony_cistatic int test_cmp_create_error_msg(void)
327e1051a39Sopenharmony_ci{
328e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
329e1051a39Sopenharmony_ci    fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
330e1051a39Sopenharmony_ci                                          OSSL_CMP_PKIFAILUREINFO_systemFailure,
331e1051a39Sopenharmony_ci                                          NULL);
332e1051a39Sopenharmony_ci    fixture->err_code = -1;
333e1051a39Sopenharmony_ci    fixture->expected = 1; /* expected: message creation is successful */
334e1051a39Sopenharmony_ci    if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
335e1051a39Sopenharmony_ci        tear_down(fixture);
336e1051a39Sopenharmony_ci        fixture = NULL;
337e1051a39Sopenharmony_ci    }
338e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_errormsg_create_test, tear_down);
339e1051a39Sopenharmony_ci    return result;
340e1051a39Sopenharmony_ci}
341e1051a39Sopenharmony_ci
342e1051a39Sopenharmony_ci
343e1051a39Sopenharmony_cistatic int test_cmp_create_pollreq(void)
344e1051a39Sopenharmony_ci{
345e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
346e1051a39Sopenharmony_ci    fixture->expected = 1;
347e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_pollreq_create_test, tear_down);
348e1051a39Sopenharmony_ci    return result;
349e1051a39Sopenharmony_ci}
350e1051a39Sopenharmony_ci
351e1051a39Sopenharmony_cistatic int test_cmp_create_rr(void)
352e1051a39Sopenharmony_ci{
353e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
354e1051a39Sopenharmony_ci    fixture->expected = 1;
355e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
356e1051a39Sopenharmony_ci        tear_down(fixture);
357e1051a39Sopenharmony_ci        fixture = NULL;
358e1051a39Sopenharmony_ci    }
359e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_rr_create_test, tear_down);
360e1051a39Sopenharmony_ci    return result;
361e1051a39Sopenharmony_ci}
362e1051a39Sopenharmony_ci
363e1051a39Sopenharmony_cistatic int test_cmp_create_genm(void)
364e1051a39Sopenharmony_ci{
365e1051a39Sopenharmony_ci    OSSL_CMP_ITAV *iv = NULL;
366e1051a39Sopenharmony_ci
367e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
368e1051a39Sopenharmony_ci    fixture->expected = 1;
369e1051a39Sopenharmony_ci    iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL);
370e1051a39Sopenharmony_ci    if (!TEST_ptr(iv)
371e1051a39Sopenharmony_ci            || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) {
372e1051a39Sopenharmony_ci        OSSL_CMP_ITAV_free(iv);
373e1051a39Sopenharmony_ci        tear_down(fixture);
374e1051a39Sopenharmony_ci        fixture = NULL;
375e1051a39Sopenharmony_ci    }
376e1051a39Sopenharmony_ci
377e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_genm_create_test, tear_down);
378e1051a39Sopenharmony_ci    return result;
379e1051a39Sopenharmony_ci}
380e1051a39Sopenharmony_ci
381e1051a39Sopenharmony_cistatic int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture)
382e1051a39Sopenharmony_ci{
383e1051a39Sopenharmony_ci    OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
384e1051a39Sopenharmony_ci    OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new();
385e1051a39Sopenharmony_ci    OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new();
386e1051a39Sopenharmony_ci    X509 *certfromresp = NULL;
387e1051a39Sopenharmony_ci    int res = 0;
388e1051a39Sopenharmony_ci
389e1051a39Sopenharmony_ci    if (crepmsg == NULL || cresp == NULL)
390e1051a39Sopenharmony_ci        goto err;
391e1051a39Sopenharmony_ci    if (!ASN1_INTEGER_set(cresp->certReqId, 99))
392e1051a39Sopenharmony_ci        goto err;
393e1051a39Sopenharmony_ci    if ((cresp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL)
394e1051a39Sopenharmony_ci        goto err;
395e1051a39Sopenharmony_ci    cresp->certifiedKeyPair->certOrEncCert->type =
396e1051a39Sopenharmony_ci        OSSL_CMP_CERTORENCCERT_CERTIFICATE;
397e1051a39Sopenharmony_ci    if ((cresp->certifiedKeyPair->certOrEncCert->value.certificate =
398e1051a39Sopenharmony_ci         X509_dup(cert)) == NULL
399e1051a39Sopenharmony_ci            || !sk_OSSL_CMP_CERTRESPONSE_push(crepmsg->response, cresp))
400e1051a39Sopenharmony_ci        goto err;
401e1051a39Sopenharmony_ci    cresp = NULL;
402e1051a39Sopenharmony_ci    read_cresp = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 99);
403e1051a39Sopenharmony_ci    if (!TEST_ptr(read_cresp))
404e1051a39Sopenharmony_ci        goto err;
405e1051a39Sopenharmony_ci    if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 88)))
406e1051a39Sopenharmony_ci        goto err;
407e1051a39Sopenharmony_ci    certfromresp = ossl_cmp_certresponse_get1_cert(ctx, read_cresp);
408e1051a39Sopenharmony_ci    if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0))
409e1051a39Sopenharmony_ci        goto err;
410e1051a39Sopenharmony_ci
411e1051a39Sopenharmony_ci    res = 1;
412e1051a39Sopenharmony_ci err:
413e1051a39Sopenharmony_ci    X509_free(certfromresp);
414e1051a39Sopenharmony_ci    OSSL_CMP_CERTRESPONSE_free(cresp);
415e1051a39Sopenharmony_ci    OSSL_CMP_CERTREPMESSAGE_free(crepmsg);
416e1051a39Sopenharmony_ci    return res;
417e1051a39Sopenharmony_ci}
418e1051a39Sopenharmony_ci
419e1051a39Sopenharmony_cistatic int test_cmp_create_certrep(void)
420e1051a39Sopenharmony_ci{
421e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
422e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_certrep_create, tear_down);
423e1051a39Sopenharmony_ci    return result;
424e1051a39Sopenharmony_ci}
425e1051a39Sopenharmony_ci
426e1051a39Sopenharmony_ci
427e1051a39Sopenharmony_cistatic int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture)
428e1051a39Sopenharmony_ci{
429e1051a39Sopenharmony_ci    OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text");
430e1051a39Sopenharmony_ci    X509_NAME *issuer = X509_NAME_new();
431e1051a39Sopenharmony_ci    ASN1_INTEGER *serial = ASN1_INTEGER_new();
432e1051a39Sopenharmony_ci    OSSL_CRMF_CERTID *cid = NULL;
433e1051a39Sopenharmony_ci    OSSL_CMP_MSG *rpmsg = NULL;
434e1051a39Sopenharmony_ci    int res = 0;
435e1051a39Sopenharmony_ci
436e1051a39Sopenharmony_ci    if (si == NULL || issuer == NULL || serial == NULL)
437e1051a39Sopenharmony_ci        goto err;
438e1051a39Sopenharmony_ci
439e1051a39Sopenharmony_ci    if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC,
440e1051a39Sopenharmony_ci                                    (unsigned char *)"The Issuer", -1, -1, 0)
441e1051a39Sopenharmony_ci            || !ASN1_INTEGER_set(serial, 99)
442e1051a39Sopenharmony_ci            || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL
443e1051a39Sopenharmony_ci            || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL)
444e1051a39Sopenharmony_ci        goto err;
445e1051a39Sopenharmony_ci
446e1051a39Sopenharmony_ci    if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0)))
447e1051a39Sopenharmony_ci        goto err;
448e1051a39Sopenharmony_ci
449e1051a39Sopenharmony_ci    if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0)))
450e1051a39Sopenharmony_ci        goto err;
451e1051a39Sopenharmony_ci
452e1051a39Sopenharmony_ci    res = 1;
453e1051a39Sopenharmony_ci err:
454e1051a39Sopenharmony_ci    ASN1_INTEGER_free(serial);
455e1051a39Sopenharmony_ci    X509_NAME_free(issuer);
456e1051a39Sopenharmony_ci    OSSL_CRMF_CERTID_free(cid);
457e1051a39Sopenharmony_ci    OSSL_CMP_PKISI_free(si);
458e1051a39Sopenharmony_ci    OSSL_CMP_MSG_free(rpmsg);
459e1051a39Sopenharmony_ci    return res;
460e1051a39Sopenharmony_ci}
461e1051a39Sopenharmony_ci
462e1051a39Sopenharmony_cistatic int test_cmp_create_rp(void)
463e1051a39Sopenharmony_ci{
464e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
465e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_rp_create, tear_down);
466e1051a39Sopenharmony_ci    return result;
467e1051a39Sopenharmony_ci}
468e1051a39Sopenharmony_ci
469e1051a39Sopenharmony_cistatic int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture)
470e1051a39Sopenharmony_ci{
471e1051a39Sopenharmony_ci    OSSL_CMP_MSG *pollrep;
472e1051a39Sopenharmony_ci    int res = 0;
473e1051a39Sopenharmony_ci
474e1051a39Sopenharmony_ci    pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000);
475e1051a39Sopenharmony_ci    if (!TEST_ptr(pollrep))
476e1051a39Sopenharmony_ci        return 0;
477e1051a39Sopenharmony_ci    if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
478e1051a39Sopenharmony_ci                                                       value.pollRep, 77)))
479e1051a39Sopenharmony_ci        goto err;
480e1051a39Sopenharmony_ci    if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
481e1051a39Sopenharmony_ci                                                            value.pollRep, 88)))
482e1051a39Sopenharmony_ci        goto err;
483e1051a39Sopenharmony_ci
484e1051a39Sopenharmony_ci    res = 1;
485e1051a39Sopenharmony_ci err:
486e1051a39Sopenharmony_ci    OSSL_CMP_MSG_free(pollrep);
487e1051a39Sopenharmony_ci    return res;
488e1051a39Sopenharmony_ci}
489e1051a39Sopenharmony_ci
490e1051a39Sopenharmony_cistatic int test_cmp_create_pollrep(void)
491e1051a39Sopenharmony_ci{
492e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
493e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_pollrep_create, tear_down);
494e1051a39Sopenharmony_ci    return result;
495e1051a39Sopenharmony_ci}
496e1051a39Sopenharmony_ci
497e1051a39Sopenharmony_cistatic int test_cmp_pkimessage_create(int bodytype)
498e1051a39Sopenharmony_ci{
499e1051a39Sopenharmony_ci    X509_REQ *p10cr = NULL;
500e1051a39Sopenharmony_ci
501e1051a39Sopenharmony_ci    SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
502e1051a39Sopenharmony_ci
503e1051a39Sopenharmony_ci    switch (fixture->bodytype = bodytype) {
504e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_P10CR:
505e1051a39Sopenharmony_ci        fixture->expected = 1;
506e1051a39Sopenharmony_ci        p10cr = load_csr_der(pkcs10_f, libctx);
507e1051a39Sopenharmony_ci        if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) {
508e1051a39Sopenharmony_ci            tear_down(fixture);
509e1051a39Sopenharmony_ci            fixture = NULL;
510e1051a39Sopenharmony_ci        }
511e1051a39Sopenharmony_ci        X509_REQ_free(p10cr);
512e1051a39Sopenharmony_ci        break;
513e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_IR:
514e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_IP:
515e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_CR:
516e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_CP:
517e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_KUR:
518e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_KUP:
519e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_RR:
520e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_RP:
521e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_PKICONF:
522e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_GENM:
523e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_GENP:
524e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_ERROR:
525e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_CERTCONF:
526e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_POLLREQ:
527e1051a39Sopenharmony_ci    case OSSL_CMP_PKIBODY_POLLREP:
528e1051a39Sopenharmony_ci        fixture->expected = 1;
529e1051a39Sopenharmony_ci        break;
530e1051a39Sopenharmony_ci    default:
531e1051a39Sopenharmony_ci        fixture->expected = 0;
532e1051a39Sopenharmony_ci        break;
533e1051a39Sopenharmony_ci    }
534e1051a39Sopenharmony_ci
535e1051a39Sopenharmony_ci    EXECUTE_TEST(execute_pkimessage_create_test, tear_down);
536e1051a39Sopenharmony_ci    return result;
537e1051a39Sopenharmony_ci}
538e1051a39Sopenharmony_ci
539e1051a39Sopenharmony_civoid cleanup_tests(void)
540e1051a39Sopenharmony_ci{
541e1051a39Sopenharmony_ci    EVP_PKEY_free(newkey);
542e1051a39Sopenharmony_ci    X509_free(cert);
543e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(default_null_provider);
544e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(provider);
545e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(libctx);
546e1051a39Sopenharmony_ci}
547e1051a39Sopenharmony_ci
548e1051a39Sopenharmony_ci#define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
549e1051a39Sopenharmony_ciOPT_TEST_DECLARE_USAGE(USAGE)
550e1051a39Sopenharmony_ci
551e1051a39Sopenharmony_ciint setup_tests(void)
552e1051a39Sopenharmony_ci{
553e1051a39Sopenharmony_ci    if (!test_skip_common_options()) {
554e1051a39Sopenharmony_ci        TEST_error("Error parsing test options\n");
555e1051a39Sopenharmony_ci        return 0;
556e1051a39Sopenharmony_ci    }
557e1051a39Sopenharmony_ci
558e1051a39Sopenharmony_ci    if (!TEST_ptr(newkey_f = test_get_argument(0))
559e1051a39Sopenharmony_ci            || !TEST_ptr(server_cert_f = test_get_argument(1))
560e1051a39Sopenharmony_ci            || !TEST_ptr(pkcs10_f = test_get_argument(2))) {
561e1051a39Sopenharmony_ci        TEST_error("usage: cmp_msg_test %s", USAGE);
562e1051a39Sopenharmony_ci        return 0;
563e1051a39Sopenharmony_ci    }
564e1051a39Sopenharmony_ci
565e1051a39Sopenharmony_ci    if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
566e1051a39Sopenharmony_ci        return 0;
567e1051a39Sopenharmony_ci
568e1051a39Sopenharmony_ci    if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx))
569e1051a39Sopenharmony_ci            || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx))
570e1051a39Sopenharmony_ci            || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
571e1051a39Sopenharmony_ci        cleanup_tests();
572e1051a39Sopenharmony_ci        return 0;
573e1051a39Sopenharmony_ci    }
574e1051a39Sopenharmony_ci
575e1051a39Sopenharmony_ci    /* Message creation tests */
576e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype);
577e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_ir_protection_fails);
578e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_ir_protection_set);
579e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_error_msg);
580e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_certconf);
581e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_certconf_badAlg);
582e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_certconf_fail_info_max);
583e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_kur);
584e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_kur_without_oldcert);
585e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_cr);
586e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_cr_without_key);
587e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_p10cr);
588e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_p10cr_null);
589e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_pollreq);
590e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_rr);
591e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_rp);
592e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_genm);
593e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_certrep);
594e1051a39Sopenharmony_ci    ADD_TEST(test_cmp_create_pollrep);
595e1051a39Sopenharmony_ci    ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create,
596e1051a39Sopenharmony_ci                            OSSL_CMP_PKIBODY_POLLREP + 1);
597e1051a39Sopenharmony_ci    return 1;
598e1051a39Sopenharmony_ci}
599