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