cmp_client_test.c: add tests for errors reported by server on subsequent requests...
[openssl.git] / test / cmp_client_test.c
1 /*
2  * Copyright 2007-2022 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 "helpers/cmp_testlib.h"
13
14 #include "cmp_mock_srv.h"
15
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;
21
22 typedef struct test_fixture {
23     const char *test_case_name;
24     OSSL_CMP_CTX *cmp_ctx;
25     OSSL_CMP_SRV_CTX *srv_ctx;
26     int req_type;
27     int expected;
28     STACK_OF(X509) *caPubs;
29 } CMP_SES_TEST_FIXTURE;
30
31 static OSSL_LIB_CTX *libctx = NULL;
32 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
33
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];
39
40 /*
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
44  */
45
46 static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
47 {
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);
52 }
53
54 static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
55 {
56     CMP_SES_TEST_FIXTURE *fixture;
57     OSSL_CMP_CTX *srv_cmp_ctx = NULL;
58     OSSL_CMP_CTX *ctx = NULL; /* for client */
59
60     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
61         return NULL;
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)
67             || (srv_cmp_ctx =
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))
71         goto err;
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             || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
81             || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
82         goto err;
83     fixture->req_type = -1;
84     return fixture;
85
86  err:
87     tear_down(fixture);
88     return NULL;
89 }
90
91 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
92 {
93     return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
94                        OSSL_CMP_PKISTATUS_unspecified)
95         && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
96                        fixt->expected == OSSL_CMP_PKISTATUS_accepted)
97         && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
98 }
99
100 static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
101 {
102     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
103     ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
104     OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
105     STACK_OF(OSSL_CMP_ITAV) *itavs;
106
107     OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
108     itavs = OSSL_CMP_exec_GENM_ses(ctx);
109
110     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
111     return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
112         && fixture->expected == OSSL_CMP_PKISTATUS_accepted ?
113         TEST_ptr(itavs) : TEST_ptr_null(itavs);
114 }
115
116 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
117 {
118     return execute_exec_GENM_ses_test_single(fixture)
119         && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
120         && execute_exec_GENM_ses_test_single(fixture);
121 }
122
123 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
124 {
125     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
126     X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
127     int status = OSSL_CMP_CTX_get_status(ctx);
128
129     OSSL_CMP_CTX_print_errors(ctx);
130     if (!TEST_int_eq(status, fixture->expected)
131         && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
132              && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
133         return 0;
134     if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
135         return TEST_ptr_null(res);
136
137     if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
138         return 0;
139     if (fixture->caPubs != NULL) {
140         STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
141         int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
142
143         OSSL_STACK_OF_X509_free(caPubs);
144         return ret;
145     }
146     return 1;
147 }
148
149 static int test_exec_RR_ses(int request_error)
150 {
151     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
152     if (request_error)
153         OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
154     fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
155         : OSSL_CMP_PKISTATUS_accepted;
156     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
157     return result;
158 }
159
160 static int test_exec_RR_ses_ok(void)
161 {
162     return test_exec_RR_ses(0);
163 }
164
165 static int test_exec_RR_ses_request_error(void)
166 {
167     return test_exec_RR_ses(1);
168 }
169
170 static int test_exec_RR_ses_receive_error(void)
171 {
172     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
173     ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
174                                      OSSL_CMP_PKISTATUS_rejection,
175                                      OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
176                                      "test string");
177     ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR);
178     fixture->expected = OSSL_CMP_PKISTATUS_rejection;
179     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
180     return result;
181 }
182
183 static int test_exec_IR_ses(void)
184 {
185     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
186     fixture->req_type = OSSL_CMP_IR;
187     fixture->expected = OSSL_CMP_PKISTATUS_accepted;
188     fixture->caPubs = sk_X509_new_null();
189     sk_X509_push(fixture->caPubs, server_cert);
190     sk_X509_push(fixture->caPubs, server_cert);
191     ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
192     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
193     return result;
194 }
195
196 static int test_exec_IR_ses_poll(int check_after, int poll_count,
197                                  int total_timeout, int expect)
198 {
199     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
200     fixture->req_type = OSSL_CMP_IR;
201     fixture->expected = expect;
202     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
203     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
204     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
205                             OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
206     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
207     return result;
208 }
209
210 static int checkAfter = 1;
211 static int test_exec_IR_ses_poll_ok(void)
212 {
213     return test_exec_IR_ses_poll(checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted);
214 }
215
216 static int test_exec_IR_ses_poll_no_timeout(void)
217 {
218     return test_exec_IR_ses_poll(checkAfter, 1 /* pollCount */, checkAfter + 1,
219                                  OSSL_CMP_PKISTATUS_accepted);
220 }
221
222 static int test_exec_IR_ses_poll_total_timeout(void)
223 {
224     return test_exec_IR_ses_poll(checkAfter + 1, 2 /* pollCount */, checkAfter,
225                                  OSSL_CMP_PKISTATUS_waiting);
226 }
227
228 static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
229 {
230     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
231     fixture->req_type = OSSL_CMP_CR;
232     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
233                             OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
234     OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
235     ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
236                                     reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
237     fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
238         : OSSL_CMP_PKISTATUS_accepted;
239     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
240     return result;
241 }
242
243 static int test_exec_CR_ses_explicit_confirm(void)
244 {
245     return test_exec_CR_ses(0, 0, 0)
246         && test_exec_CR_ses(0, 0, 1 /* reject */);
247 }
248
249 static int test_exec_CR_ses_implicit_confirm(void)
250 {
251     return test_exec_CR_ses(1, 0, 0)
252         && test_exec_CR_ses(1, 1 /* granted */, 0);
253 }
254
255 static int test_exec_KUR_ses(int transfer_error)
256 {
257     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
258     fixture->req_type = OSSL_CMP_KUR;
259     if (transfer_error)
260         OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
261     fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans
262         : OSSL_CMP_PKISTATUS_accepted;
263     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
264     return result;
265 }
266
267 static int test_exec_KUR_ses_ok(void)
268 {
269     return test_exec_KUR_ses(0);
270 }
271
272 static int test_exec_KUR_ses_transfer_error(void)
273 {
274     return test_exec_KUR_ses(1);
275 }
276
277 static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
278                             const char **txt)
279 {
280     int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
281
282     if (*reject) {
283         *txt = "not to my taste";
284         fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
285     }
286     return fail_info;
287 }
288
289 static int test_exec_P10CR_ses(int reject)
290 {
291     OSSL_CMP_CTX *ctx;
292     X509_REQ *csr = NULL;
293
294     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
295     fixture->req_type = OSSL_CMP_P10CR;
296     fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
297         : OSSL_CMP_PKISTATUS_accepted;
298     ctx = fixture->cmp_ctx;
299     if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
300         || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
301         || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
302         || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
303         tear_down(fixture);
304         fixture = NULL;
305     }
306     X509_REQ_free(csr);
307     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
308     return result;
309 }
310
311 static int test_exec_P10CR_ses_ok(void)
312 {
313     return test_exec_P10CR_ses(0);
314 }
315
316 static int test_exec_P10CR_ses_reject(void)
317 {
318     return test_exec_P10CR_ses(1);
319 }
320
321 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
322 {
323     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
324     int check_after;
325     const int CHECK_AFTER = 5;
326     const int TYPE = OSSL_CMP_KUR;
327
328     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
329     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
330     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
331         && check_after == CHECK_AFTER
332         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
333         && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
334         && check_after == CHECK_AFTER
335         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
336         && TEST_int_eq(fixture->expected,
337                        OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
338         && TEST_int_eq(0,
339                        X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
340 }
341
342 static int test_try_certreq_poll(void)
343 {
344     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
345     fixture->expected = 1;
346     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
347     return result;
348 }
349
350 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
351 {
352     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
353     int check_after;
354     const int CHECK_AFTER = 99;
355     const int TYPE = OSSL_CMP_CR;
356
357     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
358     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
359     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
360         && check_after == CHECK_AFTER
361         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
362         && TEST_int_eq(fixture->expected,
363                        OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
364         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
365 }
366
367 static int test_try_certreq_poll_abort(void)
368 {
369     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
370     fixture->expected = 1;
371     EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
372     return result;
373 }
374
375 static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
376 {
377     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
378     if (transfer_error)
379         OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
380     /*
381      * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
382      * here because this will correct total_timeout to be >= 0
383      */
384     fixture->cmp_ctx->total_timeout = total_timeout;
385     fixture->expected = expect;
386     EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
387     return result;
388 }
389
390 static int test_exec_GENM_ses_ok(void)
391 {
392     return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
393 }
394
395 static int test_exec_GENM_ses_transfer_error(void)
396 {
397     return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
398 }
399
400 static int test_exec_GENM_ses_total_timeout(void)
401 {
402     return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
403 }
404
405 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
406 {
407     int res =
408         ossl_cmp_exchange_certConf(fixture->cmp_ctx,
409                                    OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
410                                    "abcdefg");
411
412     return TEST_int_eq(fixture->expected, res);
413 }
414
415 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
416 {
417     int res =
418         ossl_cmp_exchange_error(fixture->cmp_ctx,
419                                 OSSL_CMP_PKISTATUS_rejection,
420                                 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
421                                 "foo_status", 999, "foo_details");
422
423     return TEST_int_eq(fixture->expected, res);
424 }
425
426 static int test_exchange_certConf(void)
427 {
428     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
429     fixture->expected = 0; /* client should not send certConf immediately */
430     if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
431         tear_down(fixture);
432         fixture = NULL;
433     }
434     EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
435     return result;
436 }
437
438 static int test_exchange_error(void)
439 {
440     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
441     fixture->expected = 1; /* client may send error any time */
442     EXECUTE_TEST(execute_exchange_error_test, tear_down);
443     return result;
444 }
445
446 void cleanup_tests(void)
447 {
448     X509_free(server_cert);
449     EVP_PKEY_free(server_key);
450     X509_free(client_cert);
451     EVP_PKEY_free(client_key);
452     OSSL_LIB_CTX_free(libctx);
453     return;
454 }
455
456 #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
457 OPT_TEST_DECLARE_USAGE(USAGE)
458
459 int setup_tests(void)
460 {
461     if (!test_skip_common_options()) {
462         TEST_error("Error parsing test options\n");
463         return 0;
464     }
465
466     if (!TEST_ptr(server_key_f = test_get_argument(0))
467             || !TEST_ptr(server_cert_f = test_get_argument(1))
468             || !TEST_ptr(client_key_f = test_get_argument(2))
469             || !TEST_ptr(client_cert_f = test_get_argument(3))
470             || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
471         TEST_error("usage: cmp_client_test %s", USAGE);
472         return 0;
473     }
474
475     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
476         return 0;
477
478     if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
479             || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
480             || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
481             || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
482             || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
483         cleanup_tests();
484         return 0;
485     }
486
487     ADD_TEST(test_exec_RR_ses_ok);
488     ADD_TEST(test_exec_RR_ses_request_error);
489     ADD_TEST(test_exec_RR_ses_receive_error);
490     ADD_TEST(test_exec_CR_ses_explicit_confirm);
491     ADD_TEST(test_exec_CR_ses_implicit_confirm);
492     ADD_TEST(test_exec_IR_ses);
493     ADD_TEST(test_exec_IR_ses_poll_ok);
494     ADD_TEST(test_exec_IR_ses_poll_no_timeout);
495     ADD_TEST(test_exec_IR_ses_poll_total_timeout);
496     ADD_TEST(test_exec_KUR_ses_ok);
497     ADD_TEST(test_exec_KUR_ses_transfer_error);
498     ADD_TEST(test_exec_P10CR_ses_ok);
499     ADD_TEST(test_exec_P10CR_ses_reject);
500     ADD_TEST(test_try_certreq_poll);
501     ADD_TEST(test_try_certreq_poll_abort);
502     ADD_TEST(test_exec_GENM_ses_ok);
503     ADD_TEST(test_exec_GENM_ses_transfer_error);
504     ADD_TEST(test_exec_GENM_ses_total_timeout);
505     ADD_TEST(test_exchange_certConf);
506     ADD_TEST(test_exchange_error);
507     return 1;
508 }