1 /*
2  * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11 
12 #include "helpers/cmp_testlib.h"
13 
14 #include "cmp_mock_srv.h"
15 
16 #ifndef NDEBUG /* tests need mock server, which is available only if !NDEBUG */
17 
18 static const char *server_key_f;
19 static const char *server_cert_f;
20 static const char *client_key_f;
21 static const char *client_cert_f;
22 static const char *pkcs10_f;
23 
24 typedef struct test_fixture {
25     const char *test_case_name;
26     OSSL_CMP_CTX *cmp_ctx;
27     OSSL_CMP_SRV_CTX *srv_ctx;
28     int req_type;
29     int expected;
30     STACK_OF(X509) *caPubs;
31 } CMP_SES_TEST_FIXTURE;
32 
33 static OSSL_LIB_CTX *libctx = NULL;
34 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
35 
36 static EVP_PKEY *server_key = NULL;
37 static X509 *server_cert = NULL;
38 static EVP_PKEY *client_key = NULL;
39 static X509 *client_cert = NULL;
40 static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
41 
42 /*
43  * For these unit tests, the client abandons message protection, and for
44  * error messages the mock server does so as well.
45  * Message protection and verification is tested in cmp_lib_test.c
46  */
47 
tear_down(CMP_SES_TEST_FIXTURE * fixture)48 static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
49 {
50     OSSL_CMP_CTX_free(fixture->cmp_ctx);
51     ossl_cmp_mock_srv_free(fixture->srv_ctx);
52     sk_X509_free(fixture->caPubs);
53     OPENSSL_free(fixture);
54 }
55 
set_up(const char * const test_case_name)56 static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
57 {
58     CMP_SES_TEST_FIXTURE *fixture;
59     OSSL_CMP_CTX *srv_cmp_ctx = NULL;
60     OSSL_CMP_CTX *ctx = NULL; /* for client */
61 
62     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
63         return NULL;
64     fixture->test_case_name = test_case_name;
65     if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
66             || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
67             || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
68             || (srv_cmp_ctx =
69                 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
70             || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
71             || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
72         goto err;
73     if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL))
74             || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
75             || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform)
76             || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
77             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
78             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1)
79             || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
80             || !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
81             /* client_key is by default used also for newPkey */
82             || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
83             || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
84         goto err;
85     fixture->req_type = -1;
86     return fixture;
87 
88  err:
89     tear_down(fixture);
90     return NULL;
91 }
92 
execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE * fixt)93 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
94 {
95     return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
96                        OSSL_CMP_PKISTATUS_unspecified)
97         && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
98                        fixt->expected == OSSL_CMP_PKISTATUS_accepted)
99         && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
100 }
101 
execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE * fixture)102 static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
103 {
104     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
105     ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
106     OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
107     STACK_OF(OSSL_CMP_ITAV) *itavs;
108 
109     OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
110     itavs = OSSL_CMP_exec_GENM_ses(ctx);
111 
112     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
113     return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
114         && fixture->expected == OSSL_CMP_PKISTATUS_accepted ?
115         TEST_ptr(itavs) : TEST_ptr_null(itavs);
116 }
117 
execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE * fixture)118 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
119 {
120     return execute_exec_GENM_ses_test_single(fixture)
121         && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
122         && execute_exec_GENM_ses_test_single(fixture);
123 }
124 
execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE * fixture)125 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
126 {
127     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
128     X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
129     int status = OSSL_CMP_CTX_get_status(ctx);
130 
131     OSSL_CMP_CTX_print_errors(ctx);
132     if (!TEST_int_eq(status, fixture->expected)
133         && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
134              && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
135         return 0;
136     if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
137         return TEST_ptr_null(res);
138 
139     if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
140         return 0;
141     if (fixture->caPubs != NULL) {
142         STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
143         int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
144 
145         sk_X509_pop_free(caPubs, X509_free);
146         return ret;
147     }
148     return 1;
149 }
150 
test_exec_RR_ses(int request_error)151 static int test_exec_RR_ses(int request_error)
152 {
153     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
154     if (request_error)
155         OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
156     fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
157         : OSSL_CMP_PKISTATUS_accepted;
158     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
159     return result;
160 }
161 
test_exec_RR_ses_ok(void)162 static int test_exec_RR_ses_ok(void)
163 {
164     return test_exec_RR_ses(0);
165 }
166 
test_exec_RR_ses_request_error(void)167 static int test_exec_RR_ses_request_error(void)
168 {
169     return test_exec_RR_ses(1);
170 }
171 
test_exec_RR_ses_receive_error(void)172 static int test_exec_RR_ses_receive_error(void)
173 {
174     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
175     ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
176                                      OSSL_CMP_PKISTATUS_rejection,
177                                      OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
178                                      "test string");
179     ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR);
180     fixture->expected = OSSL_CMP_PKISTATUS_rejection;
181     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
182     return result;
183 }
184 
test_exec_IR_ses(void)185 static int test_exec_IR_ses(void)
186 {
187     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
188     fixture->req_type = OSSL_CMP_IR;
189     fixture->expected = OSSL_CMP_PKISTATUS_accepted;
190     fixture->caPubs = sk_X509_new_null();
191     sk_X509_push(fixture->caPubs, server_cert);
192     sk_X509_push(fixture->caPubs, server_cert);
193     ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
194     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
195     return result;
196 }
197 
test_exec_IR_ses_poll(int check_after,int poll_count,int total_timeout,int expect)198 static int test_exec_IR_ses_poll(int check_after, int poll_count,
199                                  int total_timeout, int expect)
200 {
201     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
202     fixture->req_type = OSSL_CMP_IR;
203     fixture->expected = expect;
204     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
205     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
206     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
207                             OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
208     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
209     return result;
210 }
211 
212 static int checkAfter = 1;
test_exec_IR_ses_poll_ok(void)213 static int test_exec_IR_ses_poll_ok(void)
214 {
215     return test_exec_IR_ses_poll(checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted);
216 }
217 
test_exec_IR_ses_poll_no_timeout(void)218 static int test_exec_IR_ses_poll_no_timeout(void)
219 {
220     return test_exec_IR_ses_poll(checkAfter, 1 /* pollCount */, checkAfter + 1,
221                                  OSSL_CMP_PKISTATUS_accepted);
222 }
223 
test_exec_IR_ses_poll_total_timeout(void)224 static int test_exec_IR_ses_poll_total_timeout(void)
225 {
226     return test_exec_IR_ses_poll(checkAfter + 1, 2 /* pollCount */, checkAfter,
227                                  OSSL_CMP_PKISTATUS_waiting);
228 }
229 
test_exec_CR_ses(int implicit_confirm,int granted,int reject)230 static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
231 {
232     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
233     fixture->req_type = OSSL_CMP_CR;
234     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
235                             OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
236     OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
237     ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
238                                     reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
239     fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
240         : OSSL_CMP_PKISTATUS_accepted;
241     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
242     return result;
243 }
244 
test_exec_CR_ses_explicit_confirm(void)245 static int test_exec_CR_ses_explicit_confirm(void)
246 {
247     return test_exec_CR_ses(0, 0, 0)
248         && test_exec_CR_ses(0, 0, 1 /* reject */);
249 }
250 
test_exec_CR_ses_implicit_confirm(void)251 static int test_exec_CR_ses_implicit_confirm(void)
252 {
253     return test_exec_CR_ses(1, 0, 0)
254         && test_exec_CR_ses(1, 1 /* granted */, 0);
255 }
256 
test_exec_KUR_ses(int transfer_error,int pubkey,int raverified)257 static int test_exec_KUR_ses(int transfer_error, int pubkey, int raverified)
258 {
259     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
260     fixture->req_type = OSSL_CMP_KUR;
261     /* ctx->oldCert has already been set */
262 
263     if (transfer_error)
264         OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
265     if (pubkey) {
266         EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key;
267 
268         EVP_PKEY_up_ref(key);
269         OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key);
270         OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1);
271     }
272     if (pubkey || raverified)
273         OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD,
274                                 OSSL_CRMF_POPO_RAVERIFIED);
275     fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans :
276         raverified ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted;
277     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
278     return result;
279 }
280 
test_exec_KUR_ses_ok(void)281 static int test_exec_KUR_ses_ok(void)
282 {
283     return test_exec_KUR_ses(0, 0, 0);
284 }
285 
test_exec_KUR_ses_transfer_error(void)286 static int test_exec_KUR_ses_transfer_error(void)
287 {
288     return test_exec_KUR_ses(1, 0, 0);
289 }
290 
test_exec_KUR_ses_wrong_popo(void)291 static int test_exec_KUR_ses_wrong_popo(void)
292 {
293 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */
294     return test_exec_KUR_ses(0, 0, 1);
295 #else
296     return 1;
297 #endif
298 }
299 
test_exec_KUR_ses_pub(void)300 static int test_exec_KUR_ses_pub(void)
301 {
302     return test_exec_KUR_ses(0, 1, 0);
303 }
304 
test_exec_KUR_ses_wrong_pub(void)305 static int test_exec_KUR_ses_wrong_pub(void)
306 {
307     return test_exec_KUR_ses(0, 1, 1);
308 }
309 
test_certConf_cb(OSSL_CMP_CTX * ctx,X509 * cert,int fail_info,const char ** txt)310 static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
311                             const char **txt)
312 {
313     int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
314 
315     if (*reject) {
316         *txt = "not to my taste";
317         fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
318     }
319     return fail_info;
320 }
321 
test_exec_P10CR_ses(int reject)322 static int test_exec_P10CR_ses(int reject)
323 {
324     OSSL_CMP_CTX *ctx;
325     X509_REQ *csr = NULL;
326 
327     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
328     fixture->req_type = OSSL_CMP_P10CR;
329     fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
330         : OSSL_CMP_PKISTATUS_accepted;
331     ctx = fixture->cmp_ctx;
332     if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
333         || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
334         || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
335         || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
336         tear_down(fixture);
337         fixture = NULL;
338     }
339     X509_REQ_free(csr);
340     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
341     return result;
342 }
343 
test_exec_P10CR_ses_ok(void)344 static int test_exec_P10CR_ses_ok(void)
345 {
346     return test_exec_P10CR_ses(0);
347 }
348 
test_exec_P10CR_ses_reject(void)349 static int test_exec_P10CR_ses_reject(void)
350 {
351     return test_exec_P10CR_ses(1);
352 }
353 
execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE * fixture)354 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
355 {
356     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
357     int check_after;
358     const int CHECK_AFTER = 5;
359     const int TYPE = OSSL_CMP_KUR;
360 
361     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
362     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
363     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
364         && check_after == CHECK_AFTER
365         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
366         && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
367         && check_after == CHECK_AFTER
368         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
369         && TEST_int_eq(fixture->expected,
370                        OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
371         && TEST_int_eq(0,
372                        X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
373 }
374 
test_try_certreq_poll(void)375 static int test_try_certreq_poll(void)
376 {
377     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
378     fixture->expected = 1;
379     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
380     return result;
381 }
382 
execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE * fixture)383 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
384 {
385     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
386     int check_after;
387     const int CHECK_AFTER = 99;
388     const int TYPE = OSSL_CMP_CR;
389 
390     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
391     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
392     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
393         && check_after == CHECK_AFTER
394         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
395         && TEST_int_eq(fixture->expected,
396                        OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
397         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
398 }
399 
test_try_certreq_poll_abort(void)400 static int test_try_certreq_poll_abort(void)
401 {
402     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
403     fixture->expected = 1;
404     EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
405     return result;
406 }
407 
test_exec_GENM_ses(int transfer_error,int total_timeout,int expect)408 static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
409 {
410     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
411     if (transfer_error)
412         OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
413     /*
414      * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
415      * here because this will correct total_timeout to be >= 0
416      */
417     fixture->cmp_ctx->total_timeout = total_timeout;
418     fixture->expected = expect;
419     EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
420     return result;
421 }
422 
test_exec_GENM_ses_ok(void)423 static int test_exec_GENM_ses_ok(void)
424 {
425     return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
426 }
427 
test_exec_GENM_ses_transfer_error(void)428 static int test_exec_GENM_ses_transfer_error(void)
429 {
430     return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
431 }
432 
test_exec_GENM_ses_total_timeout(void)433 static int test_exec_GENM_ses_total_timeout(void)
434 {
435     return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
436 }
437 
execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE * fixture)438 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
439 {
440     int res =
441         ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID,
442                                    OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
443                                    "abcdefg");
444     return TEST_int_eq(fixture->expected, res);
445 }
446 
execute_exchange_error_test(CMP_SES_TEST_FIXTURE * fixture)447 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
448 {
449     int res =
450         ossl_cmp_exchange_error(fixture->cmp_ctx,
451                                 OSSL_CMP_PKISTATUS_rejection,
452                                 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
453                                 "foo_status", 999, "foo_details");
454 
455     return TEST_int_eq(fixture->expected, res);
456 }
457 
test_exchange_certConf(void)458 static int test_exchange_certConf(void)
459 {
460     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
461     fixture->expected = 0; /* client should not send certConf immediately */
462     if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
463         tear_down(fixture);
464         fixture = NULL;
465     }
466     EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
467     return result;
468 }
469 
test_exchange_error(void)470 static int test_exchange_error(void)
471 {
472     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
473     fixture->expected = 1; /* client may send error any time */
474     EXECUTE_TEST(execute_exchange_error_test, tear_down);
475     return result;
476 }
477 
cleanup_tests(void)478 void cleanup_tests(void)
479 {
480     X509_free(server_cert);
481     EVP_PKEY_free(server_key);
482     X509_free(client_cert);
483     EVP_PKEY_free(client_key);
484     OSSL_PROVIDER_unload(default_null_provider);
485     OSSL_PROVIDER_unload(provider);
486     OSSL_LIB_CTX_free(libctx);
487     return;
488 }
489 
490 # define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
OPT_TEST_DECLARE_USAGE(USAGE)491 OPT_TEST_DECLARE_USAGE(USAGE)
492 
493 int setup_tests(void)
494 {
495     if (!test_skip_common_options()) {
496         TEST_error("Error parsing test options\n");
497         return 0;
498     }
499 
500     if (!TEST_ptr(server_key_f = test_get_argument(0))
501             || !TEST_ptr(server_cert_f = test_get_argument(1))
502             || !TEST_ptr(client_key_f = test_get_argument(2))
503             || !TEST_ptr(client_cert_f = test_get_argument(3))
504             || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
505         TEST_error("usage: cmp_client_test %s", USAGE);
506         return 0;
507     }
508 
509     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
510         return 0;
511 
512     if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
513             || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
514             || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
515             || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
516             || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
517         cleanup_tests();
518         return 0;
519     }
520 
521     ADD_TEST(test_exec_RR_ses_ok);
522     ADD_TEST(test_exec_RR_ses_request_error);
523     ADD_TEST(test_exec_RR_ses_receive_error);
524     ADD_TEST(test_exec_CR_ses_explicit_confirm);
525     ADD_TEST(test_exec_CR_ses_implicit_confirm);
526     ADD_TEST(test_exec_IR_ses);
527     ADD_TEST(test_exec_IR_ses_poll_ok);
528     ADD_TEST(test_exec_IR_ses_poll_no_timeout);
529     ADD_TEST(test_exec_IR_ses_poll_total_timeout);
530     ADD_TEST(test_exec_KUR_ses_ok);
531     ADD_TEST(test_exec_KUR_ses_transfer_error);
532     ADD_TEST(test_exec_KUR_ses_wrong_popo);
533     ADD_TEST(test_exec_KUR_ses_pub);
534     ADD_TEST(test_exec_KUR_ses_wrong_pub);
535     ADD_TEST(test_exec_P10CR_ses_ok);
536     ADD_TEST(test_exec_P10CR_ses_reject);
537     ADD_TEST(test_try_certreq_poll);
538     ADD_TEST(test_try_certreq_poll_abort);
539     ADD_TEST(test_exec_GENM_ses_ok);
540     ADD_TEST(test_exec_GENM_ses_transfer_error);
541     ADD_TEST(test_exec_GENM_ses_total_timeout);
542     ADD_TEST(test_exchange_certConf);
543     ADD_TEST(test_exchange_error);
544     return 1;
545 }
546 
547 #else /* !defined (NDEBUG) */
548 
setup_tests(void)549 int setup_tests(void)
550 {
551     TEST_note("CMP session tests are disabled in this build (NDEBUG).");
552     return 1;
553 }
554 
555 #endif
556