Ensure libctx/propq is propagated when handling X509_REQ
[openssl.git] / test / cmp_client_test.c
1 /*
2  * Copyright 2007-2021 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 #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 OSSL_LIB_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) == 1);
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     return 1;
105 }
106
107 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
108 {
109     X509 *res = OSSL_CMP_exec_certreq(fixture->cmp_ctx,
110                                       fixture->req_type, NULL);
111
112     if (fixture->expected == 0)
113         return TEST_ptr_null(res);
114
115     if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
116         return 0;
117     if (fixture->caPubs != NULL) {
118         STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
119         int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
120
121         sk_X509_pop_free(caPubs, X509_free);
122         return ret;
123     }
124     return 1;
125 }
126
127 static int test_exec_RR_ses(void)
128 {
129     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
130     fixture->expected = 1;
131     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
132     return result;
133 }
134
135 static int test_exec_RR_ses_receive_error(void)
136 {
137     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
138     ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
139                                      OSSL_CMP_PKISTATUS_rejection,
140                                      OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
141                                      "test string");
142     ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
143     fixture->expected = 0;
144     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
145     return result;
146 }
147
148 static int test_exec_IR_ses(void)
149 {
150     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
151     fixture->req_type = OSSL_CMP_IR;
152     fixture->expected = 1;
153     fixture->caPubs = sk_X509_new_null();
154     sk_X509_push(fixture->caPubs, server_cert);
155     sk_X509_push(fixture->caPubs, server_cert);
156     ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
157     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
158     return result;
159 }
160
161 static const int checkAfter = 1;
162 static int test_exec_IR_ses_poll(void)
163 {
164     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
165     fixture->req_type = OSSL_CMP_IR;
166     fixture->expected = 1;
167     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 2);
168     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
169     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
170     return result;
171 }
172
173 static int test_exec_IR_ses_poll_timeout(void)
174 {
175     const int pollCount = 3;
176     const int tout = pollCount * checkAfter;
177
178     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
179     fixture->req_type = OSSL_CMP_IR;
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);
185     return result;
186 }
187
188
189 static int test_exec_CR_ses(void)
190 {
191     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
192     fixture->req_type = OSSL_CMP_CR;
193     fixture->expected = 1;
194     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
195     return result;
196 }
197
198 static int test_exec_CR_ses_implicit_confirm(void)
199 {
200     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
201     fixture->req_type = OSSL_CMP_CR;
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);
207     return result;
208 }
209
210 static int test_exec_KUR_ses(void)
211 {
212     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
213     fixture->req_type = OSSL_CMP_KUR;
214     fixture->expected = 1;
215     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
216     return result;
217 }
218
219 static int test_exec_P10CR_ses(void)
220 {
221     X509_REQ *req = NULL;
222
223     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
224     fixture->req_type = OSSL_CMP_P10CR;
225     fixture->expected = 1;
226     if (!TEST_ptr(req = load_csr_der(pkcs10_f, libctx))
227             || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, req))) {
228         tear_down(fixture);
229         fixture = NULL;
230     }
231     X509_REQ_free(req);
232     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
233     return result;
234 }
235
236 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
237 {
238     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
239     int check_after;
240     const int CHECK_AFTER = 5;
241     const int TYPE = OSSL_CMP_KUR;
242
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, NULL, &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, NULL, &check_after))
249         && check_after == CHECK_AFTER
250         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
251         && TEST_int_eq(fixture->expected,
252                        OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
253         && TEST_int_eq(0,
254                        X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
255 }
256
257 static int test_try_certreq_poll(void)
258 {
259     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
260     fixture->expected = 1;
261     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
262     return result;
263 }
264
265 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
266 {
267     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
268     int check_after;
269     const int CHECK_AFTER = INT_MAX;
270     const int TYPE = OSSL_CMP_CR;
271
272     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
273     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
274     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
275         && check_after == CHECK_AFTER
276         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
277         && TEST_int_eq(fixture->expected,
278                        OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
279         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
280 }
281
282 static int test_try_certreq_poll_abort(void)
283 {
284     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
285     fixture->expected = 1;
286     EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
287     return result;
288 }
289
290 static int test_exec_GENM_ses(void)
291 {
292     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
293     EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
294     return result;
295 }
296
297 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
298 {
299     int res =
300         ossl_cmp_exchange_certConf(fixture->cmp_ctx,
301                                    OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
302                                    "abcdefg");
303     return TEST_int_eq(fixture->expected, res);
304 }
305
306 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
307 {
308     int res =
309         ossl_cmp_exchange_error(fixture->cmp_ctx,
310                                 OSSL_CMP_PKISTATUS_rejection,
311                                 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
312                                 "foo_status", 999, "foo_details");
313
314     return TEST_int_eq(fixture->expected, res);
315 }
316
317 static int test_exchange_certConf(void)
318 {
319     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
320     fixture->expected = 0; /* client should not send certConf immediately */
321     if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
322         tear_down(fixture);
323         fixture = NULL;
324     }
325     EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
326     return result;
327 }
328
329 static int test_exchange_error(void)
330 {
331     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
332     fixture->expected = 1; /* client may send error any time */
333     EXECUTE_TEST(execute_exchange_error_test, tear_down);
334     return result;
335 }
336
337 void cleanup_tests(void)
338 {
339     X509_free(server_cert);
340     EVP_PKEY_free(server_key);
341     X509_free(client_cert);
342     EVP_PKEY_free(client_key);
343     OSSL_LIB_CTX_free(libctx);
344     return;
345 }
346
347 # define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
348 OPT_TEST_DECLARE_USAGE(USAGE)
349
350 int setup_tests(void)
351 {
352     if (!test_skip_common_options()) {
353         TEST_error("Error parsing test options\n");
354         return 0;
355     }
356
357     if (!TEST_ptr(server_key_f = test_get_argument(0))
358             || !TEST_ptr(server_cert_f = test_get_argument(1))
359             || !TEST_ptr(client_key_f = test_get_argument(2))
360             || !TEST_ptr(client_cert_f = test_get_argument(3))
361             || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
362         TEST_error("usage: cmp_client_test %s", USAGE);
363         return 0;
364     }
365
366     if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
367         return 0;
368
369     if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
370             || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
371             || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
372             || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
373             || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
374         cleanup_tests();
375         return 0;
376     }
377
378     ADD_TEST(test_exec_RR_ses);
379     ADD_TEST(test_exec_RR_ses_receive_error);
380     ADD_TEST(test_exec_CR_ses);
381     ADD_TEST(test_exec_CR_ses_implicit_confirm);
382     ADD_TEST(test_exec_IR_ses);
383     ADD_TEST(test_exec_IR_ses_poll);
384     ADD_TEST(test_exec_IR_ses_poll_timeout);
385     ADD_TEST(test_exec_KUR_ses);
386     ADD_TEST(test_exec_P10CR_ses);
387     ADD_TEST(test_try_certreq_poll);
388     ADD_TEST(test_try_certreq_poll_abort);
389     ADD_TEST(test_exec_GENM_ses);
390     ADD_TEST(test_exchange_certConf);
391     ADD_TEST(test_exchange_error);
392     return 1;
393 }
394
395 #else /* !defined (NDEBUG) */
396
397 int setup_tests(void)
398 {
399     TEST_note("CMP session tests are disabled in this build (NDEBUG).");
400     return 1;
401 }
402
403 #endif