]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/cmp_client_test.c
Fix safestack issues in conf.h
[thirdparty/openssl.git] / test / cmp_client_test.c
1 /*
2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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;
23
24 typedef struct test_fixture {
25 const char *test_case_name;
26 OSSL_CMP_CTX *cmp_ctx;
27 OSSL_CMP_SRV_CTX *srv_ctx;
28 int req_type;
29 int expected;
30 STACK_OF(X509) *caPubs;
31 } CMP_SES_TEST_FIXTURE;
32
33 static OPENSSL_CTX *libctx = NULL;
34 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
35
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];
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
48 static 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
56 static 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(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)
68 || (srv_cmp_ctx =
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))
72 goto err;
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)))
83 goto err;
84 fixture->req_type = -1;
85 return fixture;
86
87 err:
88 tear_down(fixture);
89 return NULL;
90 }
91
92 static 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
98 static 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
108 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
109 {
110 X509 *res = OSSL_CMP_exec_certreq(fixture->cmp_ctx,
111 fixture->req_type, NULL);
112
113 if (fixture->expected == 0)
114 return TEST_ptr_null(res);
115
116 if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
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
129 static 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
137 static 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
150 static int test_exec_IR_ses(void)
151 {
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);
160 return result;
161 }
162
163 static const int checkAfter = 1;
164 static int test_exec_IR_ses_poll(void)
165 {
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 */
173 return result;
174 }
175
176 static 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);
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);
188 return result;
189 }
190
191
192 static int test_exec_CR_ses(void)
193 {
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);
198 return result;
199 }
200
201 static int test_exec_CR_ses_implicit_confirm(void)
202 {
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);
210 return result;
211 }
212
213 static int test_exec_KUR_ses(void)
214 {
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);
219 return result;
220 }
221
222 static int test_exec_P10CR_ses(void)
223 {
224 X509_REQ *req = NULL;
225
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))) {
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
239 static 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);
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))
256 && TEST_int_eq(0,
257 X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
258 }
259
260 static 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
268 static 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);
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);
283 }
284
285 static 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
293 static 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
300 static 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
309 static 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
320 static 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
332 static 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
340 void 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 OPENSSL_CTX_free(libctx);
347 return;
348 }
349
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)
352
353 int setup_tests(void)
354 {
355 if (!test_skip_common_options()) {
356 TEST_error("Error parsing test options\n");
357 return 0;
358 }
359
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);
366 return 0;
367 }
368
369 if (!test_get_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
370 return 0;
371
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)))) {
377 cleanup_tests();
378 return 0;
379 }
380
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);
395 return 1;
396 }
397
398 #else /* !defined (NDEBUG) */
399
400 int setup_tests(void)
401 {
402 TEST_note("CMP session tests are disabled in this build (NDEBUG).");
403 return 1;
404 }
405
406 #endif