Update some nits around the FIPS module
[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 expected;
29     X509 *(*exec_cert_ses_cb) (OSSL_CMP_CTX *);
30     STACK_OF(X509) *caPubs;
31 } CMP_SES_TEST_FIXTURE;
32
33 static EVP_PKEY *server_key = NULL;
34 static X509 *server_cert = NULL;
35 static EVP_PKEY *client_key = NULL;
36 static X509 *client_cert = NULL;
37 static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
38
39 /*
40  * For these unit tests, the client abandons message protection, and for
41  * error messages the mock server does so as well.
42  * Message protection and verification is tested in cmp_lib_test.c
43  */
44
45 static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
46 {
47     OSSL_CMP_CTX_free(fixture->cmp_ctx);
48     ossl_cmp_mock_srv_free(fixture->srv_ctx);
49     sk_X509_free(fixture->caPubs);
50     OPENSSL_free(fixture);
51 }
52
53 static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
54 {
55     CMP_SES_TEST_FIXTURE *fixture;
56     OSSL_CMP_CTX *srv_cmp_ctx = NULL;
57     OSSL_CMP_CTX *ctx = NULL; /* for client */
58
59     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
60         return NULL;
61     fixture->test_case_name = test_case_name;
62     if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new())
63             || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
64             || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
65             || (srv_cmp_ctx =
66                 OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
67             || !OSSL_CMP_CTX_set1_clCert(srv_cmp_ctx, server_cert)
68             || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
69         goto err;
70     if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new())
71             || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
72             || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform)
73             || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
74             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
75             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1)
76             || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
77             || !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
78             || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
79             || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
80         goto err;
81     fixture->exec_cert_ses_cb = NULL;
82     return fixture;
83
84  err:
85     tear_down(fixture);
86     return NULL;
87 }
88
89 static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixture)
90 {
91     return TEST_int_eq(fixture->expected,
92                        OSSL_CMP_exec_RR_ses(fixture->cmp_ctx) == client_cert);
93 }
94
95 static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
96 {
97     STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
98     if (!TEST_ptr(itavs = OSSL_CMP_exec_GENM_ses(fixture->cmp_ctx)))
99         return 0;
100     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
101     /* TODO: check if the returned value is the expected one (same as sent) */
102     return 1;
103 }
104
105 static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
106 {
107     X509 *res;
108
109     if (fixture->expected == 0)
110         return TEST_ptr_null(fixture->exec_cert_ses_cb(fixture->cmp_ctx));
111
112     if (!TEST_ptr(res = fixture->exec_cert_ses_cb(fixture->cmp_ctx))
113             || !TEST_int_eq(X509_cmp(res, client_cert), 0))
114         return 0;
115     /* TODO: check that cerfConf has been exchanged unless implicitConfirm */
116     if (fixture->caPubs != NULL) {
117         STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
118         int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
119
120         sk_X509_pop_free(caPubs, X509_free);
121         return ret;
122     }
123     return 1;
124 }
125
126 static int test_exec_RR_ses(void)
127 {
128     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
129     fixture->expected = 1;
130     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
131     return result;
132 }
133
134 static int test_exec_RR_ses_receive_error(void)
135 {
136     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
137     ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
138                                      OSSL_CMP_PKISTATUS_rejection,
139                                      OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
140                                      "test string");
141     ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
142     fixture->expected = 0;
143     EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
144     return result;
145 }
146
147 static int test_exec_IR_ses(void)
148 {
149     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
150     fixture->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
151     fixture->expected = 1;
152     fixture->caPubs = sk_X509_new_null();
153     sk_X509_push(fixture->caPubs, server_cert);
154     sk_X509_push(fixture->caPubs, server_cert);
155     ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
156     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
157     return result;
158 }
159
160 static const int checkAfter = 1;
161 static int test_exec_IR_ses_poll(void)
162 {
163     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
164     fixture->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
165     fixture->expected = 1;
166     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 2);
167     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
168     EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
169     /* TODO: check that 2 rounds are done or session takes 2..3 seconds */
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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_CR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_CR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_KUR_ses;
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->exec_cert_ses_cb = OSSL_CMP_exec_P10CR_ses;
225     fixture->expected = 1;
226     if (!TEST_ptr(req = load_csr(pkcs10_f))
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, &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, &check_after))
249         && check_after == CHECK_AFTER
250         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
251         && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, TYPE, NULL))
252         && TEST_int_eq(0, X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
253 }
254
255 static int test_try_certreq_poll(void)
256 {
257     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
258     fixture->expected = 1;
259     EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
260     return result;
261 }
262
263 static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
264 {
265     OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
266     int check_after;
267     const int CHECK_AFTER = INT_MAX;
268     const int TYPE = OSSL_CMP_CR;
269
270     ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
271     ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
272     return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, &check_after))
273         && check_after == CHECK_AFTER
274         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
275         && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, -1, NULL))
276         && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
277 }
278
279 static int test_try_certreq_poll_abort(void)
280 {
281     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
282     fixture->expected = 1;
283     EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
284     return result;
285 }
286
287 static int test_exec_GENM_ses(void)
288 {
289     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
290     EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
291     return result;
292 }
293
294 static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
295 {
296     int res =
297         ossl_cmp_exchange_certConf(fixture->cmp_ctx,
298                                    OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
299                                    "abcdefg");
300     return TEST_int_eq(fixture->expected, res);
301 }
302
303 static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
304 {
305     int res =
306         ossl_cmp_exchange_error(fixture->cmp_ctx,
307                                 OSSL_CMP_PKISTATUS_rejection,
308                                 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
309                                 "foo_status", 999, "foo_details");
310
311     return TEST_int_eq(fixture->expected, res);
312 }
313
314 static int test_exchange_certConf(void)
315 {
316     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
317     fixture->expected = 0; /* client should not send certConf immediately */
318     if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
319         tear_down(fixture);
320         fixture = NULL;
321     }
322     EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
323     return result;
324 }
325
326 static int test_exchange_error(void)
327 {
328     SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
329     fixture->expected = 1; /* client may send error any time */
330     EXECUTE_TEST(execute_exchange_error_test, tear_down);
331     return result;
332 }
333
334 void cleanup_tests(void)
335 {
336     X509_free(server_cert);
337     EVP_PKEY_free(server_key);
338     X509_free(client_cert);
339     EVP_PKEY_free(client_key);
340     return;
341 }
342
343 int setup_tests(void)
344 {
345     if (!test_skip_common_options()) {
346         TEST_error("Error parsing test options\n");
347         return 0;
348     }
349
350     if (!TEST_ptr(server_key_f = test_get_argument(0))
351             || !TEST_ptr(server_cert_f = test_get_argument(1))
352             || !TEST_ptr(client_key_f = test_get_argument(2))
353             || !TEST_ptr(client_cert_f = test_get_argument(3))
354             || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
355         TEST_error("usage: cmp_client_test server.key server.crt client.key client.crt client.csr\n");
356         return 0;
357     }
358
359     if (!TEST_ptr(server_key = load_pem_key(server_key_f))
360             || !TEST_ptr(server_cert = load_pem_cert(server_cert_f))
361             || !TEST_ptr(client_key = load_pem_key(client_key_f))
362             || !TEST_ptr(client_cert = load_pem_cert(client_cert_f))
363             || !TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))) {
364         cleanup_tests();
365         return 0;
366     }
367
368     ADD_TEST(test_exec_RR_ses);
369     ADD_TEST(test_exec_RR_ses_receive_error);
370     ADD_TEST(test_exec_CR_ses);
371     ADD_TEST(test_exec_CR_ses_implicit_confirm);
372     ADD_TEST(test_exec_IR_ses);
373     ADD_TEST(test_exec_IR_ses_poll);
374     ADD_TEST(test_exec_IR_ses_poll_timeout);
375     ADD_TEST(test_exec_KUR_ses);
376     ADD_TEST(test_exec_P10CR_ses);
377     ADD_TEST(test_try_certreq_poll);
378     ADD_TEST(test_try_certreq_poll_abort);
379     ADD_TEST(test_exec_GENM_ses);
380     ADD_TEST(test_exchange_certConf);
381     ADD_TEST(test_exchange_error);
382     return 1;
383 }
384
385 #else /* !defined (NDEBUG) */
386
387 int setup_tests(void)
388 {
389     TEST_note("CMP session tests are disabled in this build (NDEBUG).");
390     return 1;
391 }
392
393 #endif