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