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