Fix safestack issues in x509.h
[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 DEFINE_STACK_OF(OSSL_CMP_ITAV)
19
20 static const char *server_key_f;
21 static const char *server_cert_f;
22 static const char *client_key_f;
23 static const char *client_cert_f;
24 static const char *pkcs10_f;
25
26 typedef struct test_fixture {
27     const char *test_case_name;
28     OSSL_CMP_CTX *cmp_ctx;
29     OSSL_CMP_SRV_CTX *srv_ctx;
30     int req_type;
31     int expected;
32     STACK_OF(X509) *caPubs;
33 } CMP_SES_TEST_FIXTURE;
34
35 static OPENSSL_CTX *libctx = NULL;
36 static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
37
38 static EVP_PKEY *server_key = NULL;
39 static X509 *server_cert = NULL;
40 static EVP_PKEY *client_key = NULL;
41 static X509 *client_cert = NULL;
42 static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
43
44 /*
45  * For these unit tests, the client abandons message protection, and for
46  * error messages the mock server does so as well.
47  * Message protection and verification is tested in cmp_lib_test.c
48  */
49
50 static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
51 {
52     OSSL_CMP_CTX_free(fixture->cmp_ctx);
53     ossl_cmp_mock_srv_free(fixture->srv_ctx);
54     sk_X509_free(fixture->caPubs);
55     OPENSSL_free(fixture);
56 }
57
58 static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
59 {
60     CMP_SES_TEST_FIXTURE *fixture;
61     OSSL_CMP_CTX *srv_cmp_ctx = NULL;
62     OSSL_CMP_CTX *ctx = NULL; /* for client */
63
64     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
65         return NULL;
66     fixture->test_case_name = test_case_name;
67     if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
68             || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
69             || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
70             || (srv_cmp_ctx =
71                 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
72             || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
73             || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
74         goto err;
75     if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL))
76             || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
77             || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform)
78             || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
79             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
80             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1)
81             || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
82             || !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
83             || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
84             || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
85         goto err;
86     fixture->req_type = -1;
87     return fixture;
88
89  err:
90     tear_down(fixture);
91     return NULL;
92 }
93
94 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixture)
95 {
96     return TEST_int_eq(fixture->expected,
97                        OSSL_CMP_exec_RR_ses(fixture->cmp_ctx) == client_cert);
98 }
99
100 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
101 {
102     STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
103     if (!TEST_ptr(itavs = OSSL_CMP_exec_GENM_ses(fixture->cmp_ctx)))
104         return 0;
105     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
106     /* TODO: check if the returned value is the expected one (same as sent) */
107     return 1;
108 }
109
110 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
111 {
112     X509 *res = OSSL_CMP_exec_certreq(fixture->cmp_ctx,
113                                       fixture->req_type, NULL);
114
115     if (fixture->expected == 0)
116         return TEST_ptr_null(res);
117
118     if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
119         return 0;
120     /* TODO: check that cerfConf has been exchanged unless implicitConfirm */
121     if (fixture->caPubs != NULL) {
122         STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
123         int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
124
125         sk_X509_pop_free(caPubs, X509_free);
126         return ret;
127     }
128     return 1;
129 }
130
131 static int test_exec_RR_ses(void)
132 {
133     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
134     fixture->expected = 1;
135     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
136     return result;
137 }
138
139 static int test_exec_RR_ses_receive_error(void)
140 {
141     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
142     ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
143                                      OSSL_CMP_PKISTATUS_rejection,
144                                      OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
145                                      "test string");
146     ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
147     fixture->expected = 0;
148     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
149     return result;
150 }
151
152 static int test_exec_IR_ses(void)
153 {
154     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
155     fixture->req_type = OSSL_CMP_IR;
156     fixture->expected = 1;
157     fixture->caPubs = sk_X509_new_null();
158     sk_X509_push(fixture->caPubs, server_cert);
159     sk_X509_push(fixture->caPubs, server_cert);
160     ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
161     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
162     return result;
163 }
164
165 static const int checkAfter = 1;
166 static int test_exec_IR_ses_poll(void)
167 {
168     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
169     fixture->req_type = OSSL_CMP_IR;
170     fixture->expected = 1;
171     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 2);
172     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
173     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
174     /* TODO: check that 2 rounds are done or session takes 2..3 seconds */
175     return result;
176 }
177
178 static int test_exec_IR_ses_poll_timeout(void)
179 {
180     const int pollCount = 3;
181     const int tout = pollCount * checkAfter;
182
183     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
184     fixture->req_type = OSSL_CMP_IR;
185     fixture->expected = 0;
186     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, pollCount + 1);
187     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
188     OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT, tout);
189     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
190     return result;
191 }
192
193
194 static int test_exec_CR_ses(void)
195 {
196     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
197     fixture->req_type = OSSL_CMP_CR;
198     fixture->expected = 1;
199     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
200     return result;
201 }
202
203 static int test_exec_CR_ses_implicit_confirm(void)
204 {
205     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
206     fixture->req_type = OSSL_CMP_CR;
207     fixture->expected = 1;
208     OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
209                             OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
210     OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, 1);
211     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
212     return result;
213 }
214
215 static int test_exec_KUR_ses(void)
216 {
217     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
218     fixture->req_type = OSSL_CMP_KUR;
219     fixture->expected = 1;
220     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
221     return result;
222 }
223
224 static int test_exec_P10CR_ses(void)
225 {
226     X509_REQ *req = NULL;
227
228     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
229     fixture->req_type = OSSL_CMP_P10CR;
230     fixture->expected = 1;
231     if (!TEST_ptr(req = load_csr(pkcs10_f))
232             || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, req))) {
233         tear_down(fixture);
234         fixture = NULL;
235     }
236     X509_REQ_free(req);
237     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
238     return result;
239 }
240
241 static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
242 {
243     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
244     int check_after;
245     const int CHECK_AFTER = 5;
246     const int TYPE = OSSL_CMP_KUR;
247
248     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
249     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
250     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
251         && check_after == CHECK_AFTER
252         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
253         && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
254         && check_after == CHECK_AFTER
255         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
256         && TEST_int_eq(fixture->expected,
257                        OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
258         && TEST_int_eq(0,
259                        X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
260 }
261
262 static int test_try_certreq_poll(void)
263 {
264     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
265     fixture->expected = 1;
266     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
267     return result;
268 }
269
270 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
271 {
272     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
273     int check_after;
274     const int CHECK_AFTER = INT_MAX;
275     const int TYPE = OSSL_CMP_CR;
276
277     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
278     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
279     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
280         && check_after == CHECK_AFTER
281         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
282         && TEST_int_eq(fixture->expected,
283                        OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
284         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
285 }
286
287 static int test_try_certreq_poll_abort(void)
288 {
289     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
290     fixture->expected = 1;
291     EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
292     return result;
293 }
294
295 static int test_exec_GENM_ses(void)
296 {
297     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
298     EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
299     return result;
300 }
301
302 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
303 {
304     int res =
305         ossl_cmp_exchange_certConf(fixture->cmp_ctx,
306                                    OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
307                                    "abcdefg");
308     return TEST_int_eq(fixture->expected, res);
309 }
310
311 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
312 {
313     int res =
314         ossl_cmp_exchange_error(fixture->cmp_ctx,
315                                 OSSL_CMP_PKISTATUS_rejection,
316                                 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
317                                 "foo_status", 999, "foo_details");
318
319     return TEST_int_eq(fixture->expected, res);
320 }
321
322 static int test_exchange_certConf(void)
323 {
324     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
325     fixture->expected = 0; /* client should not send certConf immediately */
326     if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
327         tear_down(fixture);
328         fixture = NULL;
329     }
330     EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
331     return result;
332 }
333
334 static int test_exchange_error(void)
335 {
336     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
337     fixture->expected = 1; /* client may send error any time */
338     EXECUTE_TEST(execute_exchange_error_test, tear_down);
339     return result;
340 }
341
342 void cleanup_tests(void)
343 {
344     X509_free(server_cert);
345     EVP_PKEY_free(server_key);
346     X509_free(client_cert);
347     EVP_PKEY_free(client_key);
348     OPENSSL_CTX_free(libctx);
349     return;
350 }
351
352 # define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
353 OPT_TEST_DECLARE_USAGE(USAGE)
354
355 int setup_tests(void)
356 {
357     if (!test_skip_common_options()) {
358         TEST_error("Error parsing test options\n");
359         return 0;
360     }
361
362     if (!TEST_ptr(server_key_f = test_get_argument(0))
363             || !TEST_ptr(server_cert_f = test_get_argument(1))
364             || !TEST_ptr(client_key_f = test_get_argument(2))
365             || !TEST_ptr(client_cert_f = test_get_argument(3))
366             || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
367         TEST_error("usage: cmp_client_test %s", USAGE);
368         return 0;
369     }
370
371     if (!test_get_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
372         return 0;
373
374     if (!TEST_ptr(server_key = load_pem_key(server_key_f, libctx))
375             || !TEST_ptr(server_cert = load_pem_cert(server_cert_f, libctx))
376             || !TEST_ptr(client_key = load_pem_key(client_key_f, libctx))
377             || !TEST_ptr(client_cert = load_pem_cert(client_cert_f, libctx))
378             || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref)))) {
379         cleanup_tests();
380         return 0;
381     }
382
383     ADD_TEST(test_exec_RR_ses);
384     ADD_TEST(test_exec_RR_ses_receive_error);
385     ADD_TEST(test_exec_CR_ses);
386     ADD_TEST(test_exec_CR_ses_implicit_confirm);
387     ADD_TEST(test_exec_IR_ses);
388     ADD_TEST(test_exec_IR_ses_poll);
389     ADD_TEST(test_exec_IR_ses_poll_timeout);
390     ADD_TEST(test_exec_KUR_ses);
391     ADD_TEST(test_exec_P10CR_ses);
392     ADD_TEST(test_try_certreq_poll);
393     ADD_TEST(test_try_certreq_poll_abort);
394     ADD_TEST(test_exec_GENM_ses);
395     ADD_TEST(test_exchange_certConf);
396     ADD_TEST(test_exchange_error);
397     return 1;
398 }
399
400 #else /* !defined (NDEBUG) */
401
402 int setup_tests(void)
403 {
404     TEST_note("CMP session tests are disabled in this build (NDEBUG).");
405     return 1;
406 }
407
408 #endif