In OpenSSL builds, declare STACK for datatypes ...
[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(X509)
19 DEFINE_STACK_OF(OSSL_CMP_ITAV)
20
21 static const char *server_key_f;
22 static const char *server_cert_f;
23 static const char *client_key_f;
24 static const char *client_cert_f;
25 static const char *pkcs10_f;
26
27 typedef struct test_fixture {
28     const char *test_case_name;
29     OSSL_CMP_CTX *cmp_ctx;
30     OSSL_CMP_SRV_CTX *srv_ctx;
31     int expected;
32     X509 *(*exec_cert_ses_cb) (OSSL_CMP_CTX *);
33     STACK_OF(X509) *caPubs;
34 } CMP_SES_TEST_FIXTURE;
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())
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_clCert(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())
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->exec_cert_ses_cb = NULL;
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;
111
112     if (fixture->expected == 0)
113         return TEST_ptr_null(fixture->exec_cert_ses_cb(fixture->cmp_ctx));
114
115     if (!TEST_ptr(res = fixture->exec_cert_ses_cb(fixture->cmp_ctx))
116             || !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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_CR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_CR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_KUR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_P10CR_ses;
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, &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, &check_after))
252         && check_after == CHECK_AFTER
253         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
254         && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, TYPE, NULL))
255         && TEST_int_eq(0, X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
256 }
257
258 static int test_try_certreq_poll(void)
259 {
260     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
261     fixture->expected = 1;
262     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
263     return result;
264 }
265
266 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
267 {
268     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
269     int check_after;
270     const int CHECK_AFTER = INT_MAX;
271     const int TYPE = OSSL_CMP_CR;
272
273     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
274     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
275     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, &check_after))
276         && check_after == CHECK_AFTER
277         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
278         && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, -1, 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     return;
344 }
345
346 int setup_tests(void)
347 {
348     if (!test_skip_common_options()) {
349         TEST_error("Error parsing test options\n");
350         return 0;
351     }
352
353     if (!TEST_ptr(server_key_f = test_get_argument(0))
354             || !TEST_ptr(server_cert_f = test_get_argument(1))
355             || !TEST_ptr(client_key_f = test_get_argument(2))
356             || !TEST_ptr(client_cert_f = test_get_argument(3))
357             || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
358         TEST_error("usage: cmp_client_test server.key server.crt client.key client.crt client.csr\n");
359         return 0;
360     }
361
362     if (!TEST_ptr(server_key = load_pem_key(server_key_f))
363             || !TEST_ptr(server_cert = load_pem_cert(server_cert_f))
364             || !TEST_ptr(client_key = load_pem_key(client_key_f))
365             || !TEST_ptr(client_cert = load_pem_cert(client_cert_f))
366             || !TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))) {
367         cleanup_tests();
368         return 0;
369     }
370
371     ADD_TEST(test_exec_RR_ses);
372     ADD_TEST(test_exec_RR_ses_receive_error);
373     ADD_TEST(test_exec_CR_ses);
374     ADD_TEST(test_exec_CR_ses_implicit_confirm);
375     ADD_TEST(test_exec_IR_ses);
376     ADD_TEST(test_exec_IR_ses_poll);
377     ADD_TEST(test_exec_IR_ses_poll_timeout);
378     ADD_TEST(test_exec_KUR_ses);
379     ADD_TEST(test_exec_P10CR_ses);
380     ADD_TEST(test_try_certreq_poll);
381     ADD_TEST(test_try_certreq_poll_abort);
382     ADD_TEST(test_exec_GENM_ses);
383     ADD_TEST(test_exchange_certConf);
384     ADD_TEST(test_exchange_error);
385     return 1;
386 }
387
388 #else /* !defined (NDEBUG) */
389
390 int setup_tests(void)
391 {
392     TEST_note("CMP session tests are disabled in this build (NDEBUG).");
393     return 1;
394 }
395
396 #endif