2 * Copyright 2007-2023 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 /* client_key is by default used also for newPkey */
81 || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
82 || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
84 fixture->req_type = -1;
92 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
94 return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
95 OSSL_CMP_PKISTATUS_unspecified)
96 && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
97 fixt->expected == OSSL_CMP_PKISTATUS_accepted)
98 && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
101 static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
103 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
104 ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
105 OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
106 STACK_OF(OSSL_CMP_ITAV) *itavs;
108 OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
109 itavs = OSSL_CMP_exec_GENM_ses(ctx);
111 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
112 return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
113 && fixture->expected == OSSL_CMP_PKISTATUS_accepted ?
114 TEST_ptr(itavs) : TEST_ptr_null(itavs);
117 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
119 return execute_exec_GENM_ses_test_single(fixture)
120 && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
121 && execute_exec_GENM_ses_test_single(fixture);
124 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
126 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
127 X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
128 int status = OSSL_CMP_CTX_get_status(ctx);
130 OSSL_CMP_CTX_print_errors(ctx);
131 if (!TEST_int_eq(status, fixture->expected)
132 && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
133 && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
135 if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
136 return TEST_ptr_null(res);
138 if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
140 if (fixture->caPubs != NULL) {
141 STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
142 int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
144 OSSL_STACK_OF_X509_free(caPubs);
150 static int test_exec_RR_ses(int request_error)
152 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
154 OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
155 fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
156 : OSSL_CMP_PKISTATUS_accepted;
157 EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
161 static int test_exec_RR_ses_ok(void)
163 return test_exec_RR_ses(0);
166 static int test_exec_RR_ses_request_error(void)
168 return test_exec_RR_ses(1);
171 static int test_exec_RR_ses_receive_error(void)
173 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
174 ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
175 OSSL_CMP_PKISTATUS_rejection,
176 OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
178 ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR);
179 fixture->expected = OSSL_CMP_PKISTATUS_rejection;
180 EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
184 static int test_exec_IR_ses(void)
186 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
187 fixture->req_type = OSSL_CMP_IR;
188 fixture->expected = OSSL_CMP_PKISTATUS_accepted;
189 fixture->caPubs = sk_X509_new_null();
190 sk_X509_push(fixture->caPubs, server_cert);
191 sk_X509_push(fixture->caPubs, server_cert);
192 ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
193 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
197 static int test_exec_IR_ses_poll(int check_after, int poll_count,
198 int total_timeout, int expect)
200 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
201 fixture->req_type = OSSL_CMP_IR;
202 fixture->expected = expect;
203 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
204 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
205 OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
206 OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
207 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
211 static int checkAfter = 1;
212 static int test_exec_IR_ses_poll_ok(void)
214 return test_exec_IR_ses_poll(checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted);
217 static int test_exec_IR_ses_poll_no_timeout(void)
219 return test_exec_IR_ses_poll(checkAfter, 1 /* pollCount */, checkAfter + 1,
220 OSSL_CMP_PKISTATUS_accepted);
223 static int test_exec_IR_ses_poll_total_timeout(void)
225 return test_exec_IR_ses_poll(checkAfter + 1, 2 /* pollCount */, checkAfter,
226 OSSL_CMP_PKISTATUS_waiting);
229 static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
231 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
232 fixture->req_type = OSSL_CMP_CR;
233 OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
234 OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
235 OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
236 ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
237 reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
238 fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
239 : OSSL_CMP_PKISTATUS_accepted;
240 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
244 static int test_exec_CR_ses_explicit_confirm(void)
246 return test_exec_CR_ses(0, 0, 0)
247 && test_exec_CR_ses(0, 0, 1 /* reject */);
250 static int test_exec_CR_ses_implicit_confirm(void)
252 return test_exec_CR_ses(1, 0, 0)
253 && test_exec_CR_ses(1, 1 /* granted */, 0);
256 static int test_exec_KUR_ses(int transfer_error, int pubkey, int raverified)
258 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
259 fixture->req_type = OSSL_CMP_KUR;
260 /* ctx->oldCert has already been set */
263 OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
265 EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key;
267 EVP_PKEY_up_ref(key);
268 OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key);
269 OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1);
271 if (pubkey || raverified)
272 OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD,
273 OSSL_CRMF_POPO_RAVERIFIED);
274 fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans :
275 raverified ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted;
276 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
280 static int test_exec_KUR_ses_ok(void)
282 return test_exec_KUR_ses(0, 0, 0);
285 static int test_exec_KUR_ses_transfer_error(void)
287 return test_exec_KUR_ses(1, 0, 0);
290 static int test_exec_KUR_ses_wrong_popo(void)
292 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */
293 return test_exec_KUR_ses(0, 0, 1);
299 static int test_exec_KUR_ses_pub(void)
301 return test_exec_KUR_ses(0, 1, 0);
304 static int test_exec_KUR_ses_wrong_pub(void)
306 return test_exec_KUR_ses(0, 1, 1);
309 static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
312 int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
315 *txt = "not to my taste";
316 fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
321 static int test_exec_P10CR_ses(int reject)
324 X509_REQ *csr = NULL;
326 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
327 fixture->req_type = OSSL_CMP_P10CR;
328 fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
329 : OSSL_CMP_PKISTATUS_accepted;
330 ctx = fixture->cmp_ctx;
331 if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
332 || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
333 || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
334 || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
339 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
343 static int test_exec_P10CR_ses_ok(void)
345 return test_exec_P10CR_ses(0);
348 static int test_exec_P10CR_ses_reject(void)
350 return test_exec_P10CR_ses(1);
353 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
355 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
357 const int CHECK_AFTER = 5;
358 const int TYPE = OSSL_CMP_KUR;
360 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
361 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
362 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
363 && check_after == CHECK_AFTER
364 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
365 && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
366 && check_after == CHECK_AFTER
367 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
368 && TEST_int_eq(fixture->expected,
369 OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
371 X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
374 static int test_try_certreq_poll(void)
376 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
377 fixture->expected = 1;
378 EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
382 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
384 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
386 const int CHECK_AFTER = 99;
387 const int TYPE = OSSL_CMP_CR;
389 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
390 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
391 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
392 && check_after == CHECK_AFTER
393 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
394 && TEST_int_eq(fixture->expected,
395 OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
396 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
399 static int test_try_certreq_poll_abort(void)
401 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
402 fixture->expected = 1;
403 EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
407 static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
409 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
411 OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
413 * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
414 * here because this will correct total_timeout to be >= 0
416 fixture->cmp_ctx->total_timeout = total_timeout;
417 fixture->expected = expect;
418 EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
422 static int test_exec_GENM_ses_ok(void)
424 return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
427 static int test_exec_GENM_ses_transfer_error(void)
429 return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
432 static int test_exec_GENM_ses_total_timeout(void)
434 return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
437 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
440 ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID,
441 OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
444 return TEST_int_eq(fixture->expected, res);
447 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
450 ossl_cmp_exchange_error(fixture->cmp_ctx,
451 OSSL_CMP_PKISTATUS_rejection,
452 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
453 "foo_status", 999, "foo_details");
455 return TEST_int_eq(fixture->expected, res);
458 static int test_exchange_certConf(void)
460 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
461 fixture->expected = 0; /* client should not send certConf immediately */
462 if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
466 EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
470 static int test_exchange_error(void)
472 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
473 fixture->expected = 1; /* client may send error any time */
474 EXECUTE_TEST(execute_exchange_error_test, tear_down);
478 void cleanup_tests(void)
480 X509_free(server_cert);
481 EVP_PKEY_free(server_key);
482 X509_free(client_cert);
483 EVP_PKEY_free(client_key);
484 OSSL_PROVIDER_unload(default_null_provider);
485 OSSL_PROVIDER_unload(provider);
486 OSSL_LIB_CTX_free(libctx);
490 #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
491 OPT_TEST_DECLARE_USAGE(USAGE)
493 int setup_tests(void)
495 if (!test_skip_common_options()) {
496 TEST_error("Error parsing test options\n");
500 if (!TEST_ptr(server_key_f = test_get_argument(0))
501 || !TEST_ptr(server_cert_f = test_get_argument(1))
502 || !TEST_ptr(client_key_f = test_get_argument(2))
503 || !TEST_ptr(client_cert_f = test_get_argument(3))
504 || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
505 TEST_error("usage: cmp_client_test %s", USAGE);
509 if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
512 if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
513 || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
514 || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
515 || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
516 || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
521 ADD_TEST(test_exec_RR_ses_ok);
522 ADD_TEST(test_exec_RR_ses_request_error);
523 ADD_TEST(test_exec_RR_ses_receive_error);
524 ADD_TEST(test_exec_CR_ses_explicit_confirm);
525 ADD_TEST(test_exec_CR_ses_implicit_confirm);
526 ADD_TEST(test_exec_IR_ses);
527 ADD_TEST(test_exec_IR_ses_poll_ok);
528 ADD_TEST(test_exec_IR_ses_poll_no_timeout);
529 ADD_TEST(test_exec_IR_ses_poll_total_timeout);
530 ADD_TEST(test_exec_KUR_ses_ok);
531 ADD_TEST(test_exec_KUR_ses_transfer_error);
532 ADD_TEST(test_exec_KUR_ses_wrong_popo);
533 ADD_TEST(test_exec_KUR_ses_pub);
534 ADD_TEST(test_exec_KUR_ses_wrong_pub);
535 ADD_TEST(test_exec_P10CR_ses_ok);
536 ADD_TEST(test_exec_P10CR_ses_reject);
537 ADD_TEST(test_try_certreq_poll);
538 ADD_TEST(test_try_certreq_poll_abort);
539 ADD_TEST(test_exec_GENM_ses_ok);
540 ADD_TEST(test_exec_GENM_ses_transfer_error);
541 ADD_TEST(test_exec_GENM_ses_total_timeout);
542 ADD_TEST(test_exchange_certConf);
543 ADD_TEST(test_exchange_error);