]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/cmp_client_test.c
Streamline the CMP request session API, adding the generalized OSSL_CMP_exec_certreq()
[thirdparty/openssl.git] / test / cmp_client_test.c
CommitLineData
7e765f46 1/*
33388b44 2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
7e765f46
DDO
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
5 *
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
10 */
11
12#include "cmp_testlib.h"
13
14#include "apps/cmp_mock_srv.h"
15
16#ifndef NDEBUG /* tests need mock server, which is available only if !NDEBUG */
17
852c2ed2
RS
18DEFINE_STACK_OF(X509)
19DEFINE_STACK_OF(OSSL_CMP_ITAV)
20
7e765f46
DDO
21static const char *server_key_f;
22static const char *server_cert_f;
23static const char *client_key_f;
24static const char *client_cert_f;
25static const char *pkcs10_f;
26
27typedef struct test_fixture {
28 const char *test_case_name;
29 OSSL_CMP_CTX *cmp_ctx;
30 OSSL_CMP_SRV_CTX *srv_ctx;
299e0f1e 31 int req_type;
7e765f46 32 int expected;
7e765f46
DDO
33 STACK_OF(X509) *caPubs;
34} CMP_SES_TEST_FIXTURE;
35
36static EVP_PKEY *server_key = NULL;
37static X509 *server_cert = NULL;
38static EVP_PKEY *client_key = NULL;
39static X509 *client_cert = NULL;
40static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
41
42/*
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
46 */
47
48static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
49{
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);
54}
55
56static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
57{
58 CMP_SES_TEST_FIXTURE *fixture;
59 OSSL_CMP_CTX *srv_cmp_ctx = NULL;
60 OSSL_CMP_CTX *ctx = NULL; /* for client */
61
62 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
63 return NULL;
64 fixture->test_case_name = test_case_name;
65 if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new())
66 || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
67 || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
68 || (srv_cmp_ctx =
69 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
63f1883d 70 || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
7e765f46
DDO
71 || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
72 goto err;
73 if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new())
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)))
83 goto err;
299e0f1e 84 fixture->req_type = -1;
7e765f46
DDO
85 return fixture;
86
87 err:
88 tear_down(fixture);
89 return NULL;
90}
91
92static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixture)
93{
94 return TEST_int_eq(fixture->expected,
95 OSSL_CMP_exec_RR_ses(fixture->cmp_ctx) == client_cert);
96}
97
98static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
99{
100 STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
101 if (!TEST_ptr(itavs = OSSL_CMP_exec_GENM_ses(fixture->cmp_ctx)))
102 return 0;
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) */
105 return 1;
106}
107
108static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
109{
299e0f1e
DDO
110 X509 *res = OSSL_CMP_exec_certreq(fixture->cmp_ctx,
111 fixture->req_type, NULL);
7e765f46
DDO
112
113 if (fixture->expected == 0)
299e0f1e 114 return TEST_ptr_null(res);
7e765f46 115
299e0f1e 116 if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
7e765f46
DDO
117 return 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);
122
123 sk_X509_pop_free(caPubs, X509_free);
124 return ret;
125 }
126 return 1;
127}
128
129static int test_exec_RR_ses(void)
130{
131 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
132 fixture->expected = 1;
133 EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
134 return result;
135}
136
137static int test_exec_RR_ses_receive_error(void)
138{
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,
143 "test string");
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);
147 return result;
148}
149
150static int test_exec_IR_ses(void)
151{
152 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 153 fixture->req_type = OSSL_CMP_IR;
7e765f46
DDO
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);
160 return result;
161}
162
163static const int checkAfter = 1;
164static int test_exec_IR_ses_poll(void)
165{
166 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 167 fixture->req_type = OSSL_CMP_IR;
7e765f46
DDO
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 */
173 return result;
174}
175
176static int test_exec_IR_ses_poll_timeout(void)
177{
178 const int pollCount = 3;
179 const int tout = pollCount * checkAfter;
180
181 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 182 fixture->req_type = OSSL_CMP_IR;
7e765f46
DDO
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);
188 return result;
189}
190
191
192static int test_exec_CR_ses(void)
193{
194 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 195 fixture->req_type = OSSL_CMP_CR;
7e765f46
DDO
196 fixture->expected = 1;
197 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
198 return result;
199}
200
201static int test_exec_CR_ses_implicit_confirm(void)
202{
203 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 204 fixture->req_type = OSSL_CMP_CR;
7e765f46
DDO
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);
210 return result;
211}
212
213static int test_exec_KUR_ses(void)
214{
215 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 216 fixture->req_type = OSSL_CMP_KUR;
7e765f46
DDO
217 fixture->expected = 1;
218 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
219 return result;
220}
221
222static int test_exec_P10CR_ses(void)
223{
224 X509_REQ *req = NULL;
225
226 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
299e0f1e 227 fixture->req_type = OSSL_CMP_P10CR;
7e765f46
DDO
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))) {
231 tear_down(fixture);
232 fixture = NULL;
233 }
234 X509_REQ_free(req);
235 EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
236 return result;
237}
238
239static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
240{
241 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
242 int check_after;
243 const int CHECK_AFTER = 5;
244 const int TYPE = OSSL_CMP_KUR;
245
246 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
247 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
299e0f1e 248 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
7e765f46
DDO
249 && check_after == CHECK_AFTER
250 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
299e0f1e 251 && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
7e765f46
DDO
252 && check_after == CHECK_AFTER
253 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
299e0f1e
DDO
254 && TEST_int_eq(fixture->expected,
255 OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
430efff1
DDO
256 && TEST_int_eq(0,
257 X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
7e765f46
DDO
258}
259
260static int test_try_certreq_poll(void)
261{
262 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
263 fixture->expected = 1;
264 EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
265 return result;
266}
267
268static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
269{
270 OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
271 int check_after;
272 const int CHECK_AFTER = INT_MAX;
273 const int TYPE = OSSL_CMP_CR;
274
275 ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
276 ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
299e0f1e 277 return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
7e765f46
DDO
278 && check_after == CHECK_AFTER
279 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
299e0f1e
DDO
280 && TEST_int_eq(fixture->expected,
281 OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
7e765f46
DDO
282 && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
283}
284
285static int test_try_certreq_poll_abort(void)
286{
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);
290 return result;
291}
292
293static int test_exec_GENM_ses(void)
294{
295 SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
296 EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
297 return result;
298}
299
300static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
301{
302 int res =
303 ossl_cmp_exchange_certConf(fixture->cmp_ctx,
304 OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
305 "abcdefg");
306 return TEST_int_eq(fixture->expected, res);
307}
308
309static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
310{
311 int res =
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");
316
317 return TEST_int_eq(fixture->expected, res);
318}
319
320static int test_exchange_certConf(void)
321{
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))) {
325 tear_down(fixture);
326 fixture = NULL;
327 }
328 EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
329 return result;
330}
331
332static int test_exchange_error(void)
333{
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);
337 return result;
338}
339
340void cleanup_tests(void)
341{
342 X509_free(server_cert);
343 EVP_PKEY_free(server_key);
344 X509_free(client_cert);
345 EVP_PKEY_free(client_key);
346 return;
347}
348
349int setup_tests(void)
350{
351 if (!test_skip_common_options()) {
352 TEST_error("Error parsing test options\n");
353 return 0;
354 }
355
356 if (!TEST_ptr(server_key_f = test_get_argument(0))
357 || !TEST_ptr(server_cert_f = test_get_argument(1))
358 || !TEST_ptr(client_key_f = test_get_argument(2))
359 || !TEST_ptr(client_cert_f = test_get_argument(3))
360 || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
361 TEST_error("usage: cmp_client_test server.key server.crt client.key client.crt client.csr\n");
362 return 0;
363 }
364
365 if (!TEST_ptr(server_key = load_pem_key(server_key_f))
366 || !TEST_ptr(server_cert = load_pem_cert(server_cert_f))
367 || !TEST_ptr(client_key = load_pem_key(client_key_f))
368 || !TEST_ptr(client_cert = load_pem_cert(client_cert_f))
369 || !TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))) {
370 cleanup_tests();
371 return 0;
372 }
373
374 ADD_TEST(test_exec_RR_ses);
375 ADD_TEST(test_exec_RR_ses_receive_error);
376 ADD_TEST(test_exec_CR_ses);
377 ADD_TEST(test_exec_CR_ses_implicit_confirm);
378 ADD_TEST(test_exec_IR_ses);
379 ADD_TEST(test_exec_IR_ses_poll);
380 ADD_TEST(test_exec_IR_ses_poll_timeout);
381 ADD_TEST(test_exec_KUR_ses);
382 ADD_TEST(test_exec_P10CR_ses);
383 ADD_TEST(test_try_certreq_poll);
384 ADD_TEST(test_try_certreq_poll_abort);
385 ADD_TEST(test_exec_GENM_ses);
386 ADD_TEST(test_exchange_certConf);
387 ADD_TEST(test_exchange_error);
388 return 1;
389}
390
391#else /* !defined (NDEBUG) */
392
393int setup_tests(void)
394{
395 TEST_note("CMP session tests are disabled in this build (NDEBUG).");
396 return 1;
397}
398
399#endif