2 * Copyright 2007-2020 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 "cmp_testlib.h"
14 #include "apps/cmp_mock_srv.h"
16 #ifndef NDEBUG /* tests need mock server, which is available only if !NDEBUG */
18 static const char *server_key_f
;
19 static const char *server_cert_f
;
20 static const char *client_key_f
;
21 static const char *client_cert_f
;
22 static const char *pkcs10_f
;
24 typedef struct test_fixture
{
25 const char *test_case_name
;
26 OSSL_CMP_CTX
*cmp_ctx
;
27 OSSL_CMP_SRV_CTX
*srv_ctx
;
30 STACK_OF(X509
) *caPubs
;
31 } CMP_SES_TEST_FIXTURE
;
33 static OPENSSL_CTX
*libctx
= NULL
;
34 static OSSL_PROVIDER
*default_null_provider
= NULL
, *provider
= NULL
;
36 static EVP_PKEY
*server_key
= NULL
;
37 static X509
*server_cert
= NULL
;
38 static EVP_PKEY
*client_key
= NULL
;
39 static X509
*client_cert
= NULL
;
40 static unsigned char ref
[CMP_TEST_REFVALUE_LENGTH
];
43 * For these unit tests, the client abandons message protection, and for
44 * error messages the mock server does so as well.
45 * Message protection and verification is tested in cmp_lib_test.c
48 static void tear_down(CMP_SES_TEST_FIXTURE
*fixture
)
50 OSSL_CMP_CTX_free(fixture
->cmp_ctx
);
51 ossl_cmp_mock_srv_free(fixture
->srv_ctx
);
52 sk_X509_free(fixture
->caPubs
);
53 OPENSSL_free(fixture
);
56 static CMP_SES_TEST_FIXTURE
*set_up(const char *const test_case_name
)
58 CMP_SES_TEST_FIXTURE
*fixture
;
59 OSSL_CMP_CTX
*srv_cmp_ctx
= NULL
;
60 OSSL_CMP_CTX
*ctx
= NULL
; /* for client */
62 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
64 fixture
->test_case_name
= test_case_name
;
65 if (!TEST_ptr(fixture
->srv_ctx
= ossl_cmp_mock_srv_new(libctx
, NULL
))
66 || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture
->srv_ctx
, 1)
67 || !ossl_cmp_mock_srv_set1_certOut(fixture
->srv_ctx
, client_cert
)
69 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture
->srv_ctx
)) == NULL
70 || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx
, server_cert
)
71 || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx
, server_key
))
73 if (!TEST_ptr(fixture
->cmp_ctx
= ctx
= OSSL_CMP_CTX_new(libctx
, NULL
))
74 || !OSSL_CMP_CTX_set_log_cb(fixture
->cmp_ctx
, print_to_bio_out
)
75 || !OSSL_CMP_CTX_set_transfer_cb(ctx
, OSSL_CMP_CTX_server_perform
)
76 || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx
, fixture
->srv_ctx
)
77 || !OSSL_CMP_CTX_set_option(ctx
, OSSL_CMP_OPT_UNPROTECTED_SEND
, 1)
78 || !OSSL_CMP_CTX_set_option(ctx
, OSSL_CMP_OPT_UNPROTECTED_ERRORS
, 1)
79 || !OSSL_CMP_CTX_set1_oldCert(ctx
, client_cert
)
80 || !OSSL_CMP_CTX_set1_pkey(ctx
, client_key
)
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
*fixture
)
94 return TEST_int_eq(fixture
->expected
,
95 OSSL_CMP_exec_RR_ses(fixture
->cmp_ctx
) == client_cert
);
98 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE
*fixture
)
100 STACK_OF(OSSL_CMP_ITAV
) *itavs
= NULL
;
101 if (!TEST_ptr(itavs
= OSSL_CMP_exec_GENM_ses(fixture
->cmp_ctx
)))
103 sk_OSSL_CMP_ITAV_pop_free(itavs
, OSSL_CMP_ITAV_free
);
104 /* TODO: check if the returned value is the expected one (same as sent) */
108 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE
*fixture
)
110 X509
*res
= OSSL_CMP_exec_certreq(fixture
->cmp_ctx
,
111 fixture
->req_type
, NULL
);
113 if (fixture
->expected
== 0)
114 return TEST_ptr_null(res
);
116 if (!TEST_ptr(res
) || !TEST_int_eq(X509_cmp(res
, client_cert
), 0))
118 /* TODO: check that cerfConf has been exchanged unless implicitConfirm */
119 if (fixture
->caPubs
!= NULL
) {
120 STACK_OF(X509
) *caPubs
= OSSL_CMP_CTX_get1_caPubs(fixture
->cmp_ctx
);
121 int ret
= TEST_int_eq(STACK_OF_X509_cmp(fixture
->caPubs
, caPubs
), 0);
123 sk_X509_pop_free(caPubs
, X509_free
);
129 static int test_exec_RR_ses(void)
131 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
132 fixture
->expected
= 1;
133 EXECUTE_TEST(execute_exec_RR_ses_test
, tear_down
);
137 static int test_exec_RR_ses_receive_error(void)
139 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
140 ossl_cmp_mock_srv_set_statusInfo(fixture
->srv_ctx
,
141 OSSL_CMP_PKISTATUS_rejection
,
142 OSSL_CMP_CTX_FAILINFO_signerNotTrusted
,
144 ossl_cmp_mock_srv_set_send_error(fixture
->srv_ctx
, 1);
145 fixture
->expected
= 0;
146 EXECUTE_TEST(execute_exec_RR_ses_test
, tear_down
);
150 static int test_exec_IR_ses(void)
152 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
153 fixture
->req_type
= OSSL_CMP_IR
;
154 fixture
->expected
= 1;
155 fixture
->caPubs
= sk_X509_new_null();
156 sk_X509_push(fixture
->caPubs
, server_cert
);
157 sk_X509_push(fixture
->caPubs
, server_cert
);
158 ossl_cmp_mock_srv_set1_caPubsOut(fixture
->srv_ctx
, fixture
->caPubs
);
159 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
163 static const int checkAfter
= 1;
164 static int test_exec_IR_ses_poll(void)
166 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
167 fixture
->req_type
= OSSL_CMP_IR
;
168 fixture
->expected
= 1;
169 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, 2);
170 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, checkAfter
);
171 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
172 /* TODO: check that 2 rounds are done or session takes 2..3 seconds */
176 static int test_exec_IR_ses_poll_timeout(void)
178 const int pollCount
= 3;
179 const int tout
= pollCount
* checkAfter
;
181 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
182 fixture
->req_type
= OSSL_CMP_IR
;
183 fixture
->expected
= 0;
184 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, pollCount
+ 1);
185 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, checkAfter
);
186 OSSL_CMP_CTX_set_option(fixture
->cmp_ctx
, OSSL_CMP_OPT_TOTAL_TIMEOUT
, tout
);
187 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
192 static int test_exec_CR_ses(void)
194 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
195 fixture
->req_type
= OSSL_CMP_CR
;
196 fixture
->expected
= 1;
197 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
201 static int test_exec_CR_ses_implicit_confirm(void)
203 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
204 fixture
->req_type
= OSSL_CMP_CR
;
205 fixture
->expected
= 1;
206 OSSL_CMP_CTX_set_option(fixture
->cmp_ctx
,
207 OSSL_CMP_OPT_IMPLICIT_CONFIRM
, 1);
208 OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture
->srv_ctx
, 1);
209 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
213 static int test_exec_KUR_ses(void)
215 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
216 fixture
->req_type
= OSSL_CMP_KUR
;
217 fixture
->expected
= 1;
218 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
222 static int test_exec_P10CR_ses(void)
224 X509_REQ
*req
= NULL
;
226 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
227 fixture
->req_type
= OSSL_CMP_P10CR
;
228 fixture
->expected
= 1;
229 if (!TEST_ptr(req
= load_csr(pkcs10_f
))
230 || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture
->cmp_ctx
, req
))) {
235 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
239 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE
*fixture
)
241 OSSL_CMP_CTX
*ctx
= fixture
->cmp_ctx
;
243 const int CHECK_AFTER
= 5;
244 const int TYPE
= OSSL_CMP_KUR
;
246 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, 3);
247 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, CHECK_AFTER
);
248 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx
, TYPE
, NULL
, &check_after
))
249 && check_after
== CHECK_AFTER
250 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx
), NULL
)
251 && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx
, TYPE
, NULL
, &check_after
))
252 && check_after
== CHECK_AFTER
253 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx
), NULL
)
254 && TEST_int_eq(fixture
->expected
,
255 OSSL_CMP_try_certreq(ctx
, TYPE
, NULL
, NULL
))
257 X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx
), client_cert
));
260 static int test_try_certreq_poll(void)
262 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
263 fixture
->expected
= 1;
264 EXECUTE_TEST(execute_try_certreq_poll_test
, tear_down
);
268 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE
*fixture
)
270 OSSL_CMP_CTX
*ctx
= fixture
->cmp_ctx
;
272 const int CHECK_AFTER
= INT_MAX
;
273 const int TYPE
= OSSL_CMP_CR
;
275 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, 3);
276 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, CHECK_AFTER
);
277 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx
, TYPE
, NULL
, &check_after
))
278 && check_after
== CHECK_AFTER
279 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx
), NULL
)
280 && TEST_int_eq(fixture
->expected
,
281 OSSL_CMP_try_certreq(ctx
, -1, NULL
, NULL
))
282 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture
->cmp_ctx
), NULL
);
285 static int test_try_certreq_poll_abort(void)
287 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
288 fixture
->expected
= 1;
289 EXECUTE_TEST(execute_try_certreq_poll_abort_test
, tear_down
);
293 static int test_exec_GENM_ses(void)
295 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
296 EXECUTE_TEST(execute_exec_GENM_ses_test
, tear_down
);
300 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE
*fixture
)
303 ossl_cmp_exchange_certConf(fixture
->cmp_ctx
,
304 OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable
,
306 return TEST_int_eq(fixture
->expected
, res
);
309 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE
*fixture
)
312 ossl_cmp_exchange_error(fixture
->cmp_ctx
,
313 OSSL_CMP_PKISTATUS_rejection
,
314 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion
,
315 "foo_status", 999, "foo_details");
317 return TEST_int_eq(fixture
->expected
, res
);
320 static int test_exchange_certConf(void)
322 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
323 fixture
->expected
= 0; /* client should not send certConf immediately */
324 if (!ossl_cmp_ctx_set0_newCert(fixture
->cmp_ctx
, X509_dup(client_cert
))) {
328 EXECUTE_TEST(execute_exchange_certConf_test
, tear_down
);
332 static int test_exchange_error(void)
334 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
335 fixture
->expected
= 1; /* client may send error any time */
336 EXECUTE_TEST(execute_exchange_error_test
, tear_down
);
340 void cleanup_tests(void)
342 X509_free(server_cert
);
343 EVP_PKEY_free(server_key
);
344 X509_free(client_cert
);
345 EVP_PKEY_free(client_key
);
346 OPENSSL_CTX_free(libctx
);
350 # define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
351 OPT_TEST_DECLARE_USAGE(USAGE
)
353 int setup_tests(void)
355 if (!test_skip_common_options()) {
356 TEST_error("Error parsing test options\n");
360 if (!TEST_ptr(server_key_f
= test_get_argument(0))
361 || !TEST_ptr(server_cert_f
= test_get_argument(1))
362 || !TEST_ptr(client_key_f
= test_get_argument(2))
363 || !TEST_ptr(client_cert_f
= test_get_argument(3))
364 || !TEST_ptr(pkcs10_f
= test_get_argument(4))) {
365 TEST_error("usage: cmp_client_test %s", USAGE
);
369 if (!test_get_libctx(&libctx
, &default_null_provider
, &provider
, 5, USAGE
))
372 if (!TEST_ptr(server_key
= load_pem_key(server_key_f
, libctx
))
373 || !TEST_ptr(server_cert
= load_pem_cert(server_cert_f
, libctx
))
374 || !TEST_ptr(client_key
= load_pem_key(client_key_f
, libctx
))
375 || !TEST_ptr(client_cert
= load_pem_cert(client_cert_f
, libctx
))
376 || !TEST_int_eq(1, RAND_bytes_ex(libctx
, ref
, sizeof(ref
)))) {
381 ADD_TEST(test_exec_RR_ses
);
382 ADD_TEST(test_exec_RR_ses_receive_error
);
383 ADD_TEST(test_exec_CR_ses
);
384 ADD_TEST(test_exec_CR_ses_implicit_confirm
);
385 ADD_TEST(test_exec_IR_ses
);
386 ADD_TEST(test_exec_IR_ses_poll
);
387 ADD_TEST(test_exec_IR_ses_poll_timeout
);
388 ADD_TEST(test_exec_KUR_ses
);
389 ADD_TEST(test_exec_P10CR_ses
);
390 ADD_TEST(test_try_certreq_poll
);
391 ADD_TEST(test_try_certreq_poll_abort
);
392 ADD_TEST(test_exec_GENM_ses
);
393 ADD_TEST(test_exchange_certConf
);
394 ADD_TEST(test_exchange_error
);
398 #else /* !defined (NDEBUG) */
400 int setup_tests(void)
402 TEST_note("CMP session tests are disabled in this build (NDEBUG).");