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
;
29 X509
*(*exec_cert_ses_cb
) (OSSL_CMP_CTX
*);
30 STACK_OF(X509
) *caPubs
;
31 } CMP_SES_TEST_FIXTURE
;
33 static EVP_PKEY
*server_key
= NULL
;
34 static X509
*server_cert
= NULL
;
35 static EVP_PKEY
*client_key
= NULL
;
36 static X509
*client_cert
= NULL
;
37 static unsigned char ref
[CMP_TEST_REFVALUE_LENGTH
];
40 * For these unit tests, the client abandons message protection, and for
41 * error messages the mock server does so as well.
42 * Message protection and verification is tested in cmp_lib_test.c
45 static void tear_down(CMP_SES_TEST_FIXTURE
*fixture
)
47 OSSL_CMP_CTX_free(fixture
->cmp_ctx
);
48 ossl_cmp_mock_srv_free(fixture
->srv_ctx
);
49 sk_X509_free(fixture
->caPubs
);
50 OPENSSL_free(fixture
);
53 static CMP_SES_TEST_FIXTURE
*set_up(const char *const test_case_name
)
55 CMP_SES_TEST_FIXTURE
*fixture
;
56 OSSL_CMP_CTX
*srv_cmp_ctx
= NULL
;
57 OSSL_CMP_CTX
*ctx
= NULL
; /* for client */
59 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
61 fixture
->test_case_name
= test_case_name
;
62 if (!TEST_ptr(fixture
->srv_ctx
= ossl_cmp_mock_srv_new())
63 || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture
->srv_ctx
, 1)
64 || !ossl_cmp_mock_srv_set1_certOut(fixture
->srv_ctx
, client_cert
)
66 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture
->srv_ctx
)) == NULL
67 || !OSSL_CMP_CTX_set1_clCert(srv_cmp_ctx
, server_cert
)
68 || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx
, server_key
))
70 if (!TEST_ptr(fixture
->cmp_ctx
= ctx
= OSSL_CMP_CTX_new())
71 || !OSSL_CMP_CTX_set_log_cb(fixture
->cmp_ctx
, print_to_bio_out
)
72 || !OSSL_CMP_CTX_set_transfer_cb(ctx
, OSSL_CMP_CTX_server_perform
)
73 || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx
, fixture
->srv_ctx
)
74 || !OSSL_CMP_CTX_set_option(ctx
, OSSL_CMP_OPT_UNPROTECTED_SEND
, 1)
75 || !OSSL_CMP_CTX_set_option(ctx
, OSSL_CMP_OPT_UNPROTECTED_ERRORS
, 1)
76 || !OSSL_CMP_CTX_set1_oldCert(ctx
, client_cert
)
77 || !OSSL_CMP_CTX_set1_pkey(ctx
, client_key
)
78 || !OSSL_CMP_CTX_set1_srvCert(ctx
, server_cert
)
79 || !OSSL_CMP_CTX_set1_referenceValue(ctx
, ref
, sizeof(ref
)))
81 fixture
->exec_cert_ses_cb
= NULL
;
89 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE
*fixture
)
91 return TEST_int_eq(fixture
->expected
,
92 OSSL_CMP_exec_RR_ses(fixture
->cmp_ctx
) == client_cert
);
95 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE
*fixture
)
97 STACK_OF(OSSL_CMP_ITAV
) *itavs
= NULL
;
98 if (!TEST_ptr(itavs
= OSSL_CMP_exec_GENM_ses(fixture
->cmp_ctx
)))
100 sk_OSSL_CMP_ITAV_pop_free(itavs
, OSSL_CMP_ITAV_free
);
101 /* TODO: check if the returned value is the expected one (same as sent) */
105 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE
*fixture
)
109 if (fixture
->expected
== 0)
110 return TEST_ptr_null(fixture
->exec_cert_ses_cb(fixture
->cmp_ctx
));
112 if (!TEST_ptr(res
= fixture
->exec_cert_ses_cb(fixture
->cmp_ctx
))
113 || !TEST_int_eq(X509_cmp(res
, client_cert
), 0))
115 /* TODO: check that cerfConf has been exchanged unless implicitConfirm */
116 if (fixture
->caPubs
!= NULL
) {
117 STACK_OF(X509
) *caPubs
= OSSL_CMP_CTX_get1_caPubs(fixture
->cmp_ctx
);
118 int ret
= TEST_int_eq(STACK_OF_X509_cmp(fixture
->caPubs
, caPubs
), 0);
120 sk_X509_pop_free(caPubs
, X509_free
);
126 static int test_exec_RR_ses(void)
128 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
129 fixture
->expected
= 1;
130 EXECUTE_TEST(execute_exec_RR_ses_test
, tear_down
);
134 static int test_exec_RR_ses_receive_error(void)
136 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
137 ossl_cmp_mock_srv_set_statusInfo(fixture
->srv_ctx
,
138 OSSL_CMP_PKISTATUS_rejection
,
139 OSSL_CMP_CTX_FAILINFO_signerNotTrusted
,
141 ossl_cmp_mock_srv_set_send_error(fixture
->srv_ctx
, 1);
142 fixture
->expected
= 0;
143 EXECUTE_TEST(execute_exec_RR_ses_test
, tear_down
);
147 static int test_exec_IR_ses(void)
149 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
150 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_IR_ses
;
151 fixture
->expected
= 1;
152 fixture
->caPubs
= sk_X509_new_null();
153 sk_X509_push(fixture
->caPubs
, server_cert
);
154 sk_X509_push(fixture
->caPubs
, server_cert
);
155 ossl_cmp_mock_srv_set1_caPubsOut(fixture
->srv_ctx
, fixture
->caPubs
);
156 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
160 static const int checkAfter
= 1;
161 static int test_exec_IR_ses_poll(void)
163 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
164 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_IR_ses
;
165 fixture
->expected
= 1;
166 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, 2);
167 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, checkAfter
);
168 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
169 /* TODO: check that 2 rounds are done or session takes 2..3 seconds */
173 static int test_exec_IR_ses_poll_timeout(void)
175 const int pollCount
= 3;
176 const int tout
= pollCount
* checkAfter
;
178 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
179 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_IR_ses
;
180 fixture
->expected
= 0;
181 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, pollCount
+ 1);
182 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, checkAfter
);
183 OSSL_CMP_CTX_set_option(fixture
->cmp_ctx
, OSSL_CMP_OPT_TOTAL_TIMEOUT
, tout
);
184 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
189 static int test_exec_CR_ses(void)
191 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
192 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_CR_ses
;
193 fixture
->expected
= 1;
194 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
198 static int test_exec_CR_ses_implicit_confirm(void)
200 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
201 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_CR_ses
;
202 fixture
->expected
= 1;
203 OSSL_CMP_CTX_set_option(fixture
->cmp_ctx
,
204 OSSL_CMP_OPT_IMPLICIT_CONFIRM
, 1);
205 OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture
->srv_ctx
, 1);
206 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
210 static int test_exec_KUR_ses(void)
212 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
213 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_KUR_ses
;
214 fixture
->expected
= 1;
215 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
219 static int test_exec_P10CR_ses(void)
221 X509_REQ
*req
= NULL
;
223 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
224 fixture
->exec_cert_ses_cb
= OSSL_CMP_exec_P10CR_ses
;
225 fixture
->expected
= 1;
226 if (!TEST_ptr(req
= load_csr(pkcs10_f
))
227 || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture
->cmp_ctx
, req
))) {
232 EXECUTE_TEST(execute_exec_certrequest_ses_test
, tear_down
);
236 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE
*fixture
)
238 OSSL_CMP_CTX
*ctx
= fixture
->cmp_ctx
;
240 const int CHECK_AFTER
= 5;
241 const int TYPE
= OSSL_CMP_KUR
;
243 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, 3);
244 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, CHECK_AFTER
);
245 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx
, TYPE
, &check_after
))
246 && check_after
== CHECK_AFTER
247 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx
), NULL
)
248 && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx
, TYPE
, &check_after
))
249 && check_after
== CHECK_AFTER
250 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx
), NULL
)
251 && TEST_int_eq(fixture
->expected
, OSSL_CMP_try_certreq(ctx
, TYPE
, NULL
))
252 && TEST_int_eq(0, X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx
), client_cert
));
255 static int test_try_certreq_poll(void)
257 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
258 fixture
->expected
= 1;
259 EXECUTE_TEST(execute_try_certreq_poll_test
, tear_down
);
263 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE
*fixture
)
265 OSSL_CMP_CTX
*ctx
= fixture
->cmp_ctx
;
267 const int CHECK_AFTER
= INT_MAX
;
268 const int TYPE
= OSSL_CMP_CR
;
270 ossl_cmp_mock_srv_set_pollCount(fixture
->srv_ctx
, 3);
271 ossl_cmp_mock_srv_set_checkAfterTime(fixture
->srv_ctx
, CHECK_AFTER
);
272 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx
, TYPE
, &check_after
))
273 && check_after
== CHECK_AFTER
274 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx
), NULL
)
275 && TEST_int_eq(fixture
->expected
, OSSL_CMP_try_certreq(ctx
, -1, NULL
))
276 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture
->cmp_ctx
), NULL
);
279 static int test_try_certreq_poll_abort(void)
281 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
282 fixture
->expected
= 1;
283 EXECUTE_TEST(execute_try_certreq_poll_abort_test
, tear_down
);
287 static int test_exec_GENM_ses(void)
289 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
290 EXECUTE_TEST(execute_exec_GENM_ses_test
, tear_down
);
294 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE
*fixture
)
297 ossl_cmp_exchange_certConf(fixture
->cmp_ctx
,
298 OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable
,
300 return TEST_int_eq(fixture
->expected
, res
);
303 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE
*fixture
)
306 ossl_cmp_exchange_error(fixture
->cmp_ctx
,
307 OSSL_CMP_PKISTATUS_rejection
,
308 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion
,
309 "foo_status", 999, "foo_details");
311 return TEST_int_eq(fixture
->expected
, res
);
314 static int test_exchange_certConf(void)
316 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
317 fixture
->expected
= 0; /* client should not send certConf immediately */
318 if (!ossl_cmp_ctx_set0_newCert(fixture
->cmp_ctx
, X509_dup(client_cert
))) {
322 EXECUTE_TEST(execute_exchange_certConf_test
, tear_down
);
326 static int test_exchange_error(void)
328 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE
, set_up
);
329 fixture
->expected
= 1; /* client may send error any time */
330 EXECUTE_TEST(execute_exchange_error_test
, tear_down
);
334 void cleanup_tests(void)
336 X509_free(server_cert
);
337 EVP_PKEY_free(server_key
);
338 X509_free(client_cert
);
339 EVP_PKEY_free(client_key
);
343 int setup_tests(void)
345 if (!test_skip_common_options()) {
346 TEST_error("Error parsing test options\n");
350 if (!TEST_ptr(server_key_f
= test_get_argument(0))
351 || !TEST_ptr(server_cert_f
= test_get_argument(1))
352 || !TEST_ptr(client_key_f
= test_get_argument(2))
353 || !TEST_ptr(client_cert_f
= test_get_argument(3))
354 || !TEST_ptr(pkcs10_f
= test_get_argument(4))) {
355 TEST_error("usage: cmp_client_test server.key server.crt client.key client.crt client.csr\n");
359 if (!TEST_ptr(server_key
= load_pem_key(server_key_f
))
360 || !TEST_ptr(server_cert
= load_pem_cert(server_cert_f
))
361 || !TEST_ptr(client_key
= load_pem_key(client_key_f
))
362 || !TEST_ptr(client_cert
= load_pem_cert(client_cert_f
))
363 || !TEST_int_eq(1, RAND_bytes(ref
, sizeof(ref
)))) {
368 ADD_TEST(test_exec_RR_ses
);
369 ADD_TEST(test_exec_RR_ses_receive_error
);
370 ADD_TEST(test_exec_CR_ses
);
371 ADD_TEST(test_exec_CR_ses_implicit_confirm
);
372 ADD_TEST(test_exec_IR_ses
);
373 ADD_TEST(test_exec_IR_ses_poll
);
374 ADD_TEST(test_exec_IR_ses_poll_timeout
);
375 ADD_TEST(test_exec_KUR_ses
);
376 ADD_TEST(test_exec_P10CR_ses
);
377 ADD_TEST(test_try_certreq_poll
);
378 ADD_TEST(test_try_certreq_poll_abort
);
379 ADD_TEST(test_exec_GENM_ses
);
380 ADD_TEST(test_exchange_certConf
);
381 ADD_TEST(test_exchange_error
);
385 #else /* !defined (NDEBUG) */
387 int setup_tests(void)
389 TEST_note("CMP session tests are disabled in this build (NDEBUG).");