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