2 * Copyright 2007-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include "helpers/cmp_testlib.h"
14 #include "cmp_mock_srv.h"
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;
22 typedef struct test_fixture {
23 const char *test_case_name;
24 OSSL_CMP_CTX *cmp_ctx;
25 OSSL_CMP_SRV_CTX *srv_ctx;
28 STACK_OF(X509) *caPubs;
29 } CMP_SES_TEST_FIXTURE;
31 static OSSL_LIB_CTX *libctx = NULL;
32 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
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];
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
46 static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
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);
54 static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
56 CMP_SES_TEST_FIXTURE *fixture;
57 OSSL_CMP_CTX *srv_cmp_ctx = NULL;
58 OSSL_CMP_CTX *ctx = NULL; /* for client */
60 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
62 fixture->test_case_name = test_case_name;
63 if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
64 || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
65 || !ossl_cmp_mock_srv_set1_refCert(fixture->srv_ctx, client_cert)
66 || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
68 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
69 || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
70 || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
72 if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL))
73 || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
74 || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform)
75 || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
76 || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
77 || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1)
78 || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
79 || !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
80 || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
81 || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
83 fixture->req_type = -1;
91 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
93 return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
94 OSSL_CMP_PKISTATUS_unspecified)
95 && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
96 fixt->expected == OSSL_CMP_PKISTATUS_accepted)
97 && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
100 static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
102 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
103 ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
104 OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
105 STACK_OF(OSSL_CMP_ITAV) *itavs;
107 OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
108 itavs = OSSL_CMP_exec_GENM_ses(ctx);
110 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
111 return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
112 && fixture->expected == OSSL_CMP_PKISTATUS_accepted ?
113 TEST_ptr(itavs) : TEST_ptr_null(itavs);
116 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
118 return execute_exec_GENM_ses_test_single(fixture)
119 && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
120 && execute_exec_GENM_ses_test_single(fixture);
123 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
125 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
126 X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
127 int status = OSSL_CMP_CTX_get_status(ctx);
129 if (!TEST_int_eq(status, fixture->expected)
130 && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
131 && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
133 if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
134 return TEST_ptr_null(res);
136 if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
138 if (fixture->caPubs != NULL) {
139 STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
140 int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
142 OSSL_STACK_OF_X509_free(caPubs);
148 static int test_exec_RR_ses(int request_error)
150 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
152 OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
153 fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
154 : OSSL_CMP_PKISTATUS_accepted;
155 EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
159 static int test_exec_RR_ses_ok(void)
161 return test_exec_RR_ses(0);
164 static int test_exec_RR_ses_request_error(void)
166 return test_exec_RR_ses(1);
169 static int test_exec_RR_ses_receive_error(void)
171 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
172 ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
173 OSSL_CMP_PKISTATUS_rejection,
174 OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
176 ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
177 fixture->expected = OSSL_CMP_PKISTATUS_rejection;
178 EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
182 static int test_exec_IR_ses(void)
184 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
185 fixture->req_type = OSSL_CMP_IR;
186 fixture->expected = OSSL_CMP_PKISTATUS_accepted;
187 fixture->caPubs = sk_X509_new_null();
188 sk_X509_push(fixture->caPubs, server_cert);
189 sk_X509_push(fixture->caPubs, server_cert);
190 ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
191 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
195 static int test_exec_IR_ses_poll(int check_after, int poll_count,
196 int total_timeout, int expect)
198 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
199 fixture->req_type = OSSL_CMP_IR;
200 fixture->expected = expect;
201 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
202 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
203 OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
204 OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
205 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
209 static int checkAfter = 1;
210 static int test_exec_IR_ses_poll_ok(void)
212 return test_exec_IR_ses_poll(checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted);
215 static int test_exec_IR_ses_poll_no_timeout(void)
217 return test_exec_IR_ses_poll(checkAfter, 1 /* pollCount */, checkAfter + 1,
218 OSSL_CMP_PKISTATUS_accepted);
221 static int test_exec_IR_ses_poll_total_timeout(void)
223 return test_exec_IR_ses_poll(checkAfter + 1, 2 /* pollCount */, checkAfter,
224 OSSL_CMP_PKISTATUS_waiting);
227 static int test_exec_CR_ses(int implicit_confirm, int granted)
229 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
230 fixture->req_type = OSSL_CMP_CR;
231 fixture->expected = OSSL_CMP_PKISTATUS_accepted;
232 OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
233 OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
234 OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
235 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
239 static int test_exec_CR_ses_explicit_confirm(void)
241 return test_exec_CR_ses(0, 0);
244 static int test_exec_CR_ses_implicit_confirm(void)
246 return test_exec_CR_ses(1, 0)
247 && test_exec_CR_ses(1, 1);
250 static int test_exec_KUR_ses(int transfer_error)
252 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
253 fixture->req_type = OSSL_CMP_KUR;
255 OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
256 fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans
257 : OSSL_CMP_PKISTATUS_accepted;
258 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
262 static int test_exec_KUR_ses_ok(void)
264 return test_exec_KUR_ses(0);
267 static int test_exec_KUR_ses_transfer_error(void)
269 return test_exec_KUR_ses(1);
272 static int test_exec_P10CR_ses(void)
274 X509_REQ *req = NULL;
276 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
277 fixture->req_type = OSSL_CMP_P10CR;
278 fixture->expected = OSSL_CMP_PKISTATUS_accepted;
279 if (!TEST_ptr(req = load_csr_der(pkcs10_f, libctx))
280 || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, req))) {
285 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
289 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
291 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
293 const int CHECK_AFTER = 5;
294 const int TYPE = OSSL_CMP_KUR;
296 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
297 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
298 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
299 && check_after == CHECK_AFTER
300 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
301 && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
302 && check_after == CHECK_AFTER
303 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
304 && TEST_int_eq(fixture->expected,
305 OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
307 X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
310 static int test_try_certreq_poll(void)
312 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
313 fixture->expected = 1;
314 EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
318 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
320 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
322 const int CHECK_AFTER = INT_MAX;
323 const int TYPE = OSSL_CMP_CR;
325 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
326 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
327 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
328 && check_after == CHECK_AFTER
329 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
330 && TEST_int_eq(fixture->expected,
331 OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
332 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
335 static int test_try_certreq_poll_abort(void)
337 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
338 fixture->expected = 1;
339 EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
343 static int test_exec_GENM_ses(int transfer_error)
345 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
347 OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
348 fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans
349 : OSSL_CMP_PKISTATUS_accepted;
350 EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
354 static int test_exec_GENM_ses_ok(void)
356 return test_exec_GENM_ses(0);
359 static int test_exec_GENM_ses_error(void)
361 return test_exec_GENM_ses(1);
364 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
367 ossl_cmp_exchange_certConf(fixture->cmp_ctx,
368 OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
371 return TEST_int_eq(fixture->expected, res);
374 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
377 ossl_cmp_exchange_error(fixture->cmp_ctx,
378 OSSL_CMP_PKISTATUS_rejection,
379 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
380 "foo_status", 999, "foo_details");
382 return TEST_int_eq(fixture->expected, res);
385 static int test_exchange_certConf(void)
387 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
388 fixture->expected = 0; /* client should not send certConf immediately */
389 if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
393 EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
397 static int test_exchange_error(void)
399 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
400 fixture->expected = 1; /* client may send error any time */
401 EXECUTE_TEST(execute_exchange_error_test, tear_down);
405 void cleanup_tests(void)
407 X509_free(server_cert);
408 EVP_PKEY_free(server_key);
409 X509_free(client_cert);
410 EVP_PKEY_free(client_key);
411 OSSL_LIB_CTX_free(libctx);
415 #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
416 OPT_TEST_DECLARE_USAGE(USAGE)
418 int setup_tests(void)
420 if (!test_skip_common_options()) {
421 TEST_error("Error parsing test options\n");
425 if (!TEST_ptr(server_key_f = test_get_argument(0))
426 || !TEST_ptr(server_cert_f = test_get_argument(1))
427 || !TEST_ptr(client_key_f = test_get_argument(2))
428 || !TEST_ptr(client_cert_f = test_get_argument(3))
429 || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
430 TEST_error("usage: cmp_client_test %s", USAGE);
434 if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
437 if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
438 || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
439 || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
440 || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
441 || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
446 ADD_TEST(test_exec_RR_ses_ok);
447 ADD_TEST(test_exec_RR_ses_request_error);
448 ADD_TEST(test_exec_RR_ses_receive_error);
449 ADD_TEST(test_exec_CR_ses_explicit_confirm);
450 ADD_TEST(test_exec_CR_ses_implicit_confirm);
451 ADD_TEST(test_exec_IR_ses);
452 ADD_TEST(test_exec_IR_ses_poll_ok);
453 ADD_TEST(test_exec_IR_ses_poll_no_timeout);
454 ADD_TEST(test_exec_IR_ses_poll_total_timeout);
455 ADD_TEST(test_exec_KUR_ses_ok);
456 ADD_TEST(test_exec_KUR_ses_transfer_error);
457 ADD_TEST(test_exec_P10CR_ses);
458 ADD_TEST(test_try_certreq_poll);
459 ADD_TEST(test_try_certreq_poll_abort);
460 ADD_TEST(test_exec_GENM_ses_ok);
461 ADD_TEST(test_exec_GENM_ses_error);
462 ADD_TEST(test_exchange_certConf);
463 ADD_TEST(test_exchange_error);