]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/cmp_ctx_test.c
test cleanup: move helper .c and .h files to test/helpers/
[thirdparty/openssl.git] / test / cmp_ctx_test.c
CommitLineData
7960dbec 1/*
33388b44 2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
7960dbec
DDO
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
20f8bc72 12#include "helpers/cmp_testlib.h"
7960dbec
DDO
13
14#include <openssl/x509_vfy.h>
15
16typedef struct test_fixture {
17 const char *test_case_name;
18 OSSL_CMP_CTX *ctx;
19} OSSL_CMP_CTX_TEST_FIXTURE;
20
21static void tear_down(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
22{
23 if (fixture != NULL)
24 OSSL_CMP_CTX_free(fixture->ctx);
25 OPENSSL_free(fixture);
26}
27
28static OSSL_CMP_CTX_TEST_FIXTURE *set_up(const char *const test_case_name)
29{
30 OSSL_CMP_CTX_TEST_FIXTURE *fixture;
31
4dde554c
DDO
32 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
33 return NULL;
1a7cd250 34 if (!TEST_ptr(fixture->ctx = OSSL_CMP_CTX_new(NULL, NULL))) {
7960dbec
DDO
35 tear_down(fixture);
36 return NULL;
37 }
38 fixture->test_case_name = test_case_name;
39 return fixture;
40}
41
3dbc5156
DDO
42static STACK_OF(X509) *sk_X509_new_1(void)
43{
7960dbec
DDO
44 STACK_OF(X509) *sk = sk_X509_new_null();
45 X509 *x = X509_new();
46
47 if (x == NULL || !sk_X509_push(sk, x)) {
48 sk_X509_free(sk);
49 X509_free(x);
50 sk = NULL;
51 }
52 return sk;
53}
54
3dbc5156
DDO
55static void sk_X509_pop_X509_free(STACK_OF(X509) *sk)
56{
7960dbec
DDO
57 sk_X509_pop_free(sk, X509_free);
58}
59
60static int execute_CTX_reinit_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
61{
62 OSSL_CMP_CTX *ctx = fixture->ctx;
63 ASN1_OCTET_STRING *bytes = NULL;
64 STACK_OF(X509) *certs = NULL;
65 int res = 0;
66
67 /* set non-default values in all relevant fields */
68 ctx->status = 1;
69 ctx->failInfoCode = 1;
70 if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
71 || !ossl_cmp_ctx_set0_newCert(ctx, X509_new())
72 || !TEST_ptr(certs = sk_X509_new_1())
39082af2 73 || !ossl_cmp_ctx_set1_newChain(ctx, certs)
7960dbec
DDO
74 || !ossl_cmp_ctx_set1_caPubs(ctx, certs)
75 || !ossl_cmp_ctx_set1_extraCertsIn(ctx, certs)
76 || !ossl_cmp_ctx_set0_validatedSrvCert(ctx, X509_new())
77 || !TEST_ptr(bytes = ASN1_OCTET_STRING_new())
78 || !OSSL_CMP_CTX_set1_transactionID(ctx, bytes)
79 || !OSSL_CMP_CTX_set1_senderNonce(ctx, bytes)
80 || !ossl_cmp_ctx_set1_recipNonce(ctx, bytes))
7960dbec
DDO
81 goto err;
82
83 if (!TEST_true(OSSL_CMP_CTX_reinit(ctx)))
84 goto err;
85
86 /* check whether values have been reset to default in all relevant fields */
87 if (!TEST_true(ctx->status == -1
88 && ctx->failInfoCode == -1
89 && ctx->statusString == NULL
90 && ctx->newCert == NULL
39082af2 91 && ctx->newChain == NULL
7960dbec
DDO
92 && ctx->caPubs == NULL
93 && ctx->extraCertsIn == NULL
94 && ctx->validatedSrvCert == NULL
95 && ctx->transactionID == NULL
96 && ctx->senderNonce == NULL
97 && ctx->recipNonce == NULL))
98 goto err;
99
100 /* this does not check that all remaining fields are untouched */
101 res = 1;
102
103 err:
104 sk_X509_pop_X509_free(certs);
105 ASN1_OCTET_STRING_free(bytes);
106 return res;
107}
108
109static int test_CTX_reinit(void)
110{
111 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up);
112 EXECUTE_TEST(execute_CTX_reinit_test, tear_down);
113 return result;
114}
115
8e1a1582 116#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
85870311 117
7960dbec
DDO
118static int msg_total_size = 0;
119static int msg_total_size_log_cb(const char *func, const char *file, int line,
120 OSSL_CMP_severity level, const char *msg)
121{
122 msg_total_size += strlen(msg);
0acaa795 123 TEST_note("total=%d len=%zu msg='%s'\n", msg_total_size, strlen(msg), msg);
7960dbec
DDO
124 return 1;
125}
126
85870311 127# define STR64 "This is a 64 bytes looooooooooooooooooooooooooooooooong string.\n"
7960dbec 128/* max string length ISO C90 compilers are required to support is 509. */
85870311 129# define STR509 STR64 STR64 STR64 STR64 STR64 STR64 STR64 \
7960dbec
DDO
130 "This is a 61 bytes loooooooooooooooooooooooooooooong string.\n"
131static const char *const max_str_literal = STR509;
85870311 132# define STR_SEP "<SEP>"
7960dbec
DDO
133
134static int execute_CTX_print_errors_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
135{
136 OSSL_CMP_CTX *ctx = fixture->ctx;
137 int base_err_msg_size, expected_size;
138 int res = 1;
139
140 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx, NULL)))
141 res = 0;
142 if (!TEST_true(ctx->log_cb == NULL))
143 res = 0;
144
85870311 145# ifndef OPENSSL_NO_STDIO
9311d0c4 146 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
7960dbec 147 OSSL_CMP_CTX_print_errors(ctx); /* should print above error to STDERR */
85870311 148# endif
7960dbec
DDO
149
150 /* this should work regardless of OPENSSL_NO_STDIO and OPENSSL_NO_TRACE: */
151 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx, msg_total_size_log_cb)))
152 res = 0;
153 if (!TEST_true(ctx->log_cb == msg_total_size_log_cb)) {
154 res = 0;
155 } else {
9311d0c4 156 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
7960dbec 157 base_err_msg_size = strlen("INVALID_ARGS");
9311d0c4 158 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
7960dbec
DDO
159 base_err_msg_size += strlen("NULL_ARGUMENT");
160 expected_size = base_err_msg_size;
161 ossl_cmp_add_error_data("data1"); /* should prepend separator " : " */
162 expected_size += strlen(" : " "data1");
163 ossl_cmp_add_error_data("data2"); /* should prepend separator " : " */
164 expected_size += strlen(" : " "data2");
165 ossl_cmp_add_error_line("new line"); /* should prepend separator "\n" */
166 expected_size += strlen("\n" "new line");
167 OSSL_CMP_CTX_print_errors(ctx);
168 if (!TEST_int_eq(msg_total_size, expected_size))
169 res = 0;
170
9311d0c4 171 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
7960dbec
DDO
172 base_err_msg_size = strlen("INVALID_ARGS") + strlen(" : ");
173 expected_size = base_err_msg_size;
174 while (expected_size < 4096) { /* force split */
31b28ad9 175 ERR_add_error_txt(STR_SEP, max_str_literal);
7960dbec
DDO
176 expected_size += strlen(STR_SEP) + strlen(max_str_literal);
177 }
178 expected_size += base_err_msg_size - 2 * strlen(STR_SEP);
179 msg_total_size = 0;
180 OSSL_CMP_CTX_print_errors(ctx);
181 if (!TEST_int_eq(msg_total_size, expected_size))
182 res = 0;
183 }
184
185 return res;
186}
187
188static int test_CTX_print_errors(void)
189{
190 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up);
191 EXECUTE_TEST(execute_CTX_print_errors_test, tear_down);
192 return result;
193}
85870311 194#endif
7960dbec 195
235595c4
DDO
196static
197int execute_CTX_reqExtensions_have_SAN_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
7960dbec
DDO
198{
199 OSSL_CMP_CTX *ctx = fixture->ctx;
200 const int len = 16;
ebf30069 201 unsigned char str[16 /* = len */];
7960dbec
DDO
202 ASN1_OCTET_STRING *data = NULL;
203 X509_EXTENSION *ext = NULL;
204 X509_EXTENSIONS *exts = NULL;
205 int res = 0;
206
207 if (!TEST_false(OSSL_CMP_CTX_reqExtensions_have_SAN(ctx)))
208 return 0;
209
210 if (!TEST_int_eq(1, RAND_bytes(str, len))
211 || !TEST_ptr(data = ASN1_OCTET_STRING_new())
212 || !TEST_true(ASN1_OCTET_STRING_set(data, str, len)))
213 goto err;
214 ext = X509_EXTENSION_create_by_NID(NULL, NID_subject_alt_name, 0, data);
215 if (!TEST_ptr(ext)
216 || !TEST_ptr(exts = sk_X509_EXTENSION_new_null())
217 || !TEST_true(sk_X509_EXTENSION_push(exts, ext))
218 || !TEST_true(OSSL_CMP_CTX_set0_reqExtensions(ctx, exts))) {
219 X509_EXTENSION_free(ext);
220 sk_X509_EXTENSION_free(exts);
221 goto err;
222 }
223 if (TEST_int_eq(OSSL_CMP_CTX_reqExtensions_have_SAN(ctx), 1)) {
224 ext = sk_X509_EXTENSION_pop(exts);
225 res = TEST_false(OSSL_CMP_CTX_reqExtensions_have_SAN(ctx));
226 X509_EXTENSION_free(ext);
227 }
228 err:
229 ASN1_OCTET_STRING_free(data);
230 return res;
231}
232
233static int test_CTX_reqExtensions_have_SAN(void)
234{
235 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up);
236 EXECUTE_TEST(execute_CTX_reqExtensions_have_SAN_test, tear_down);
237 return result;
238}
239
7960dbec
DDO
240static int test_log_line;
241static int test_log_cb_res = 0;
242static int test_log_cb(const char *func, const char *file, int line,
243 OSSL_CMP_severity level, const char *msg)
244{
245 test_log_cb_res =
235595c4 246#ifndef PEDANTIC
ebf30069
DDO
247 (TEST_str_eq(func, "execute_cmp_ctx_log_cb_test")
248 || TEST_str_eq(func, "(unknown function)")) &&
235595c4 249#endif
ebf30069
DDO
250 (TEST_str_eq(file, OPENSSL_FILE)
251 || TEST_str_eq(file, "(no file)"))
252 && (TEST_int_eq(line, test_log_line) || TEST_int_eq(line, 0))
253 && (TEST_int_eq(level, OSSL_CMP_LOG_INFO) || TEST_int_eq(level, -1))
254 && TEST_str_eq(msg, "ok");
7960dbec
DDO
255 return 1;
256}
7960dbec
DDO
257
258static int execute_cmp_ctx_log_cb_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
259{
260 int res = 1;
7960dbec
DDO
261 OSSL_CMP_CTX *ctx = fixture->ctx;
262
263 OSSL_TRACE(ALL, "this general trace message is not shown by default\n");
264
265 OSSL_CMP_log_open();
266 OSSL_CMP_log_open(); /* multiple calls should be harmless */
267
268 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx, NULL))) {
269 res = 0;
270 } else {
ebf30069
DDO
271 ossl_cmp_err(ctx, "this should be printed as CMP error message");
272 ossl_cmp_warn(ctx, "this should be printed as CMP warning message");
273 ossl_cmp_debug(ctx, "this should not be printed");
7960dbec 274 TEST_true(OSSL_CMP_CTX_set_log_verbosity(ctx, OSSL_CMP_LOG_DEBUG));
ebf30069 275 ossl_cmp_debug(ctx, "this should be printed as CMP debug message");
7960dbec
DDO
276 TEST_true(OSSL_CMP_CTX_set_log_verbosity(ctx, OSSL_CMP_LOG_INFO));
277 }
278 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx, test_log_cb))) {
279 res = 0;
280 } else {
281 test_log_line = OPENSSL_LINE + 1;
ebf30069 282 ossl_cmp_log2(INFO, ctx, "%s%c", "o", 'k');
7960dbec
DDO
283 if (!TEST_int_eq(test_log_cb_res, 1))
284 res = 0;
285 OSSL_CMP_CTX_set_log_verbosity(ctx, OSSL_CMP_LOG_ERR);
286 test_log_cb_res = -1; /* callback should not be called at all */
287 test_log_line = OPENSSL_LINE + 1;
ebf30069 288 ossl_cmp_log2(INFO, ctx, "%s%c", "o", 'k');
7960dbec
DDO
289 if (!TEST_int_eq(test_log_cb_res, -1))
290 res = 0;
291 }
292 OSSL_CMP_log_close();
293 OSSL_CMP_log_close(); /* multiple calls should be harmless */
7960dbec
DDO
294 return res;
295}
296
297static int test_cmp_ctx_log_cb(void)
298{
299 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up);
300 EXECUTE_TEST(execute_cmp_ctx_log_cb_test, tear_down);
301 return result;
302}
303
29f178bd 304static BIO *test_http_cb(BIO *bio, void *arg, int use_ssl, int detail)
7960dbec
DDO
305{
306 return NULL;
307}
308
29f178bd
DDO
309static OSSL_CMP_MSG *test_transfer_cb(OSSL_CMP_CTX *ctx,
310 const OSSL_CMP_MSG *req)
7960dbec 311{
29f178bd 312 return NULL;
7960dbec
DDO
313}
314
315static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
316 const char **txt)
317{
318 return 0;
319}
320
321typedef OSSL_CMP_CTX CMP_CTX; /* prevents rewriting type name by below macro */
322#define OSSL_CMP_CTX 1 /* name prefix for exported setter functions */
323#define ossl_cmp_ctx 0 /* name prefix for internal setter functions */
324#define set 0
325#define set0 0
326#define set1 1
327#define get 0
328#define get0 0
329#define get1 1
330
331#define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
332 DEFAULT, NEW, FREE) \
235595c4
DDO
333static int \
334execute_CTX_##SETN##_##GETN##_##FIELD(OSSL_CMP_CTX_TEST_FIXTURE *fixture) \
7960dbec
DDO
335{ \
336 CMP_CTX *ctx = fixture->ctx; \
337 int (*set_fn)(CMP_CTX *ctx, TYPE) = \
338 (int (*)(CMP_CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
235595c4 339 /* need type cast in above assignment as TYPE arg sometimes is const */ \
7960dbec
DDO
340 TYPE (*get_fn)(const CMP_CTX *ctx) = OSSL_CMP_CTX_##GETN##_##FIELD; \
341 TYPE val1_to_free = NEW; \
342 TYPE val1 = val1_to_free; \
343 TYPE val1_read = 0; /* 0 works for any type */ \
344 TYPE val2_to_free = NEW; \
345 TYPE val2 = val2_to_free; \
346 TYPE val2_read = 0; \
347 TYPE val3_read = 0; \
348 int res = 1; \
349 \
350 if (!TEST_int_eq(ERR_peek_error(), 0)) \
351 res = 0; \
352 if (PREFIX == 1) { /* exported setter functions must test ctx == NULL */ \
353 if ((*set_fn)(NULL, val1) || ERR_peek_error() == 0) { \
354 TEST_error("setter did not return error on ctx == NULL"); \
355 res = 0; \
356 } \
357 } \
358 ERR_clear_error(); \
359 \
360 if ((*get_fn)(NULL) != ERR || ERR_peek_error() == 0) { \
361 TEST_error("getter did not return error on ctx == NULL"); \
362 res = 0; \
363 } \
364 ERR_clear_error(); \
365 \
366 val1_read = (*get_fn)(ctx); \
367 if (!DEFAULT(val1_read)) { \
368 TEST_error("did not get default value"); \
369 res = 0; \
370 } \
371 if (!(*set_fn)(ctx, val1)) { \
372 TEST_error("setting first value failed"); \
373 res = 0; \
374 } \
375 if (SETN == 0) \
376 val1_to_free = 0; /* 0 works for any type */ \
377 \
378 if (GETN == 1) \
379 FREE(val1_read); \
380 val1_read = (*get_fn)(ctx); \
381 if (SETN == 0) { \
382 if (val1_read != val1) { \
383 TEST_error("set/get first value did not match"); \
384 res = 0; \
385 } \
386 } else { \
387 if (DUP && val1_read == val1) { \
388 TEST_error("first set did not dup the value"); \
389 res = 0; \
390 } \
391 if (DEFAULT(val1_read)) { \
392 TEST_error("first set had no effect"); \
393 res = 0; \
394 } \
395 } \
396 \
397 if (!(*set_fn)(ctx, val2)) { \
398 TEST_error("setting second value failed"); \
399 res = 0; \
400 } \
401 if (SETN == 0) \
402 val2_to_free = 0; \
403 \
404 val2_read = (*get_fn)(ctx); \
405 if (DEFAULT(val2_read)) { \
406 TEST_error("second set reset the value"); \
407 res = 0; \
408 } \
409 if (SETN == 0 && GETN == 0) { \
410 if (val2_read != val2) { \
411 TEST_error("set/get second value did not match"); \
412 res = 0; \
413 } \
414 } else { \
415 if (DUP && val2_read == val2) { \
416 TEST_error("second set did not dup the value"); \
417 res = 0; \
418 } \
419 if (val2 == val1) { \
420 TEST_error("second value is same as first value"); \
421 res = 0; \
422 } \
423 if (GETN == 1 && val2_read == val1_read) { \
424 /* \
425 * Note that if GETN == 0 then possibly val2_read == val1_read \
426 * because set1 may allocate the new copy at the same location. \
427 */ \
428 TEST_error("second get returned same as first get"); \
429 res = 0; \
430 } \
431 } \
432 \
433 val3_read = (*get_fn)(ctx); \
434 if (DEFAULT(val3_read)) { \
435 TEST_error("third set reset the value"); \
436 res = 0; \
437 } \
438 if (GETN == 0) { \
439 if (val3_read != val2_read) { \
440 TEST_error("third get gave different value"); \
441 res = 0; \
442 } \
235595c4 443 } else { \
7960dbec
DDO
444 if (DUP && val3_read == val2_read) { \
445 TEST_error("third get did not create a new dup"); \
446 res = 0; \
447 } \
448 } \
449 /* this does not check that all remaining fields are untouched */ \
450 \
451 if (!TEST_int_eq(ERR_peek_error(), 0)) \
452 res = 0; \
453 \
454 FREE(val1_to_free); \
455 FREE(val2_to_free); \
456 if (GETN == 1) { \
457 FREE(val1_read); \
458 FREE(val2_read); \
459 FREE(val3_read); \
460 } \
461 return TEST_true(res); \
462} \
463\
464static int test_CTX_##SETN##_##GETN##_##FIELD(void) \
465{ \
466 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up); \
467 EXECUTE_TEST(execute_CTX_##SETN##_##GETN##_##FIELD, tear_down); \
468 return result; \
469}
470
3dbc5156
DDO
471static char *char_new(void)
472{
7960dbec
DDO
473 return OPENSSL_strdup("test");
474}
475
3dbc5156
DDO
476static void char_free(char *val)
477{
7960dbec
DDO
478 OPENSSL_free(val);
479}
480
481#define EMPTY_SK_X509(x) ((x) == NULL || sk_X509_num(x) == 0)
482
3dbc5156
DDO
483static X509_STORE *X509_STORE_new_1(void)
484{
7960dbec
DDO
485 X509_STORE *store = X509_STORE_new();
486
487 if (store != NULL)
488 X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store), 1);
489 return store;
490}
491
235595c4
DDO
492#define DEFAULT_STORE(x) \
493 ((x) == NULL || X509_VERIFY_PARAM_get_flags(X509_STORE_get0_param(x)) == 0)
7960dbec
DDO
494
495#define IS_NEG(x) ((x) < 0)
496#define IS_0(x) ((x) == 0) /* for any type */
7960dbec
DDO
497#define DROP(x) (void)(x) /* dummy free() for non-pointer and function types */
498
3327c8d6
SL
499#define RET_IF_NULL_ARG(ctx, ret) \
500 if (ctx == NULL) { \
9311d0c4 501 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
3327c8d6
SL
502 return ret; \
503 }
7960dbec
DDO
504
505#define DEFINE_SET_GET_TEST(OSSL_CMP, CTX, N, M, DUP, FIELD, TYPE) \
506 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, DUP, FIELD, \
235595c4 507 TYPE *, NULL, IS_0, TYPE##_new(), TYPE##_free)
7960dbec
DDO
508
509#define DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FIELD, ELEM_TYPE, \
510 DEFAULT, NEW, FREE) \
511 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, 1, FIELD, \
3dbc5156 512 STACK_OF(ELEM_TYPE)*, NULL, DEFAULT, NEW, FREE)
7960dbec
DDO
513#define DEFINE_SET_GET_SK_TEST(OSSL_CMP, CTX, N, M, FIELD, T) \
514 DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FIELD, T, \
3dbc5156 515 IS_0, sk_##T##_new_null(), sk_##T##_free)
7960dbec
DDO
516#define DEFINE_SET_GET_SK_X509_TEST(OSSL_CMP, CTX, N, M, FNAME) \
517 DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FNAME, X509, \
3dbc5156
DDO
518 EMPTY_SK_X509, \
519 sk_X509_new_1(), sk_X509_pop_X509_free)
7960dbec
DDO
520
521#define DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, N, M, DUP, FIELD, TYPE, \
522 DEFAULT) \
523 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, DUP, FIELD, \
235595c4 524 TYPE *, NULL, DEFAULT, TYPE##_new(), TYPE##_free)
7960dbec
DDO
525#define DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, N, DUP, FIELD, TYPE, DEFAULT) \
526 static TYPE *OSSL_CMP_CTX_get0_##FIELD(const CMP_CTX *ctx) \
527 { \
3327c8d6
SL
528 RET_IF_NULL_ARG(ctx, NULL); \
529 return (TYPE *)ctx->FIELD; \
7960dbec
DDO
530 } \
531 DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, N, 0, DUP, FIELD, TYPE, DEFAULT)
532#define DEFINE_SET_TEST(OSSL_CMP, CTX, N, DUP, FIELD, TYPE) \
533 DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, N, DUP, FIELD, TYPE, IS_0)
534
535#define DEFINE_SET_SK_TEST(OSSL_CMP, CTX, N, FIELD, TYPE) \
536 static STACK_OF(TYPE) *OSSL_CMP_CTX_get0_##FIELD(const CMP_CTX *ctx) \
537 { \
3327c8d6
SL
538 RET_IF_NULL_ARG(ctx, NULL); \
539 return ctx->FIELD; \
7960dbec
DDO
540 } \
541 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get0, 1, FIELD, \
3dbc5156
DDO
542 STACK_OF(TYPE)*, NULL, IS_0, \
543 sk_##TYPE##_new_null(), sk_##TYPE##_free)
7960dbec 544
7e765f46 545typedef OSSL_HTTP_bio_cb_t OSSL_CMP_http_cb_t;
7960dbec 546#define DEFINE_SET_CB_TEST(FIELD) \
7e765f46 547 static OSSL_CMP_##FIELD##_t OSSL_CMP_CTX_get_##FIELD(const CMP_CTX *ctx) \
7960dbec 548 { \
3327c8d6
SL
549 RET_IF_NULL_ARG(ctx, NULL); \
550 return ctx->FIELD; \
7960dbec
DDO
551 } \
552 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, FIELD, \
7e765f46 553 OSSL_CMP_##FIELD##_t, NULL, IS_0, \
3dbc5156 554 test_##FIELD, DROP)
7960dbec 555#define DEFINE_SET_GET_P_VOID_TEST(FIELD) \
235595c4 556 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, FIELD, void *, \
3dbc5156 557 NULL, IS_0, ((void *)1), DROP)
7960dbec
DDO
558
559#define DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, DEFAULT) \
560 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set, get, 0, FIELD, int, -1, \
3dbc5156 561 DEFAULT, 1, DROP)
7960dbec
DDO
562#define DEFINE_SET_GET_INT_TEST(OSSL_CMP, CTX, FIELD) \
563 DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, IS_NEG)
4b1fe471 564#define DEFINE_SET_INT_TEST(FIELD) \
7960dbec
DDO
565 static int OSSL_CMP_CTX_get_##FIELD(const CMP_CTX *ctx) \
566 { \
3327c8d6
SL
567 RET_IF_NULL_ARG(ctx, -1); \
568 return ctx->FIELD; \
7960dbec 569 } \
4b1fe471 570 DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, IS_0)
7960dbec
DDO
571
572#define DEFINE_SET_GET_ARG_FN(SETN, GETN, FIELD, ARG, T) \
573 static int OSSL_CMP_CTX_##SETN##_##FIELD##_##ARG(CMP_CTX *ctx, T val) \
574 { \
575 return OSSL_CMP_CTX_##SETN##_##FIELD(ctx, ARG, val); \
576 } \
577 \
578 static T OSSL_CMP_CTX_##GETN##_##FIELD##_##ARG(const CMP_CTX *ctx) \
579 { \
580 return OSSL_CMP_CTX_##GETN##_##FIELD(ctx, ARG); \
581 }
582
583#define DEFINE_SET_GET1_STR_FN(SETN, FIELD) \
584 static int OSSL_CMP_CTX_##SETN##_##FIELD##_str(CMP_CTX *ctx, char *val)\
585 { \
586 return OSSL_CMP_CTX_##SETN##_##FIELD(ctx, (unsigned char *)val, \
235595c4 587 strlen(val)); \
7960dbec
DDO
588 } \
589 \
590 static char *OSSL_CMP_CTX_get1_##FIELD##_str(const CMP_CTX *ctx) \
591 { \
3327c8d6 592 const ASN1_OCTET_STRING *bytes = NULL; \
7960dbec 593 \
3327c8d6
SL
594 RET_IF_NULL_ARG(ctx, NULL); \
595 bytes = ctx->FIELD; \
7960dbec
DDO
596 return bytes == NULL ? NULL : \
597 OPENSSL_strndup((char *)bytes->data, bytes->length); \
598 }
599
600#define push 0
601#define push0 0
602#define push1 1
603#define DEFINE_PUSH_BASE_TEST(PUSHN, DUP, FIELD, ELEM, TYPE, T, \
3dbc5156
DDO
604 DEFAULT, NEW, FREE) \
605static TYPE sk_top_##FIELD(const CMP_CTX *ctx) \
606{ \
7960dbec
DDO
607 return sk_##T##_value(ctx->FIELD, sk_##T##_num(ctx->FIELD) - 1); \
608} \
609\
610static int execute_CTX_##PUSHN##_##ELEM(OSSL_CMP_CTX_TEST_FIXTURE *fixture) \
611{ \
612 CMP_CTX *ctx = fixture->ctx; \
613 int (*push_fn)(CMP_CTX *ctx, TYPE) = \
614 (int (*)(CMP_CTX *ctx, TYPE))OSSL_CMP_CTX_##PUSHN##_##ELEM; \
3dbc5156
DDO
615 /* \
616 * need type cast in above assignment because TYPE arg sometimes is const \
617 */ \
7960dbec
DDO
618 int n_elem = sk_##T##_num(ctx->FIELD); \
619 STACK_OF(TYPE) field_read; \
620 TYPE val1_to_free = NEW; \
621 TYPE val1 = val1_to_free; \
622 TYPE val1_read = 0; /* 0 works for any type */ \
623 TYPE val2_to_free = NEW; \
624 TYPE val2 = val2_to_free; \
625 TYPE val2_read = 0; \
626 int res = 1; \
627 \
628 if (!TEST_int_eq(ERR_peek_error(), 0)) \
629 res = 0; \
630 if ((*push_fn)(NULL, val1) || ERR_peek_error() == 0) { \
631 TEST_error("pusher did not return error on ctx == NULL"); \
632 res = 0; \
633 } \
634 ERR_clear_error(); \
635 \
636 if (n_elem < 0) /* can happen for NULL stack */ \
637 n_elem = 0; \
638 field_read = ctx->FIELD; \
639 if (!DEFAULT(field_read)) { \
640 TEST_error("did not get default value for stack field"); \
641 res = 0; \
642 } \
643 if (!(*push_fn)(ctx, val1)) { \
644 TEST_error("pushing first value failed"); \
645 res = 0; \
646 } \
647 if (PUSHN == 0) \
648 val1_to_free = 0; /* 0 works for any type */ \
649 \
650 if (sk_##T##_num(ctx->FIELD) != ++n_elem) { \
651 TEST_error("pushing first value did not increment number"); \
652 res = 0; \
653 } \
654 val1_read = sk_top_##FIELD(ctx); \
655 if (PUSHN == 0) { \
656 if (val1_read != val1) { \
657 TEST_error("push/sk_top first value did not match"); \
658 res = 0; \
659 } \
660 } else { \
661 if (DUP && val1_read == val1) { \
662 TEST_error("first push did not dup the value"); \
663 res = 0; \
664 } \
665 } \
666 \
667 if (!(*push_fn)(ctx, val2)) { \
668 TEST_error("pushting second value failed"); \
669 res = 0; \
670 } \
671 if (PUSHN == 0) \
672 val2_to_free = 0; \
673 \
674 if (sk_##T##_num(ctx->FIELD) != ++n_elem) { \
675 TEST_error("pushing second value did not increment number"); \
676 res = 0; \
677 } \
678 val2_read = sk_top_##FIELD(ctx); \
679 if (PUSHN == 0) { \
680 if (val2_read != val2) { \
681 TEST_error("push/sk_top second value did not match"); \
682 res = 0; \
683 } \
684 } else { \
685 if (DUP && val2_read == val2) { \
686 TEST_error("second push did not dup the value"); \
687 res = 0; \
688 } \
689 if (val2 == val1) { \
690 TEST_error("second value is same as first value"); \
691 res = 0; \
692 } \
693 } \
235595c4 694 /* this does not check if all remaining fields and elems are untouched */ \
7960dbec
DDO
695 \
696 if (!TEST_int_eq(ERR_peek_error(), 0)) \
697 res = 0; \
698 \
699 FREE(val1_to_free); \
700 FREE(val2_to_free); \
701 return TEST_true(res); \
702} \
703\
704static int test_CTX_##PUSHN##_##ELEM(void) \
705{ \
706 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up); \
707 EXECUTE_TEST(execute_CTX_##PUSHN##_##ELEM, tear_down); \
708 return result; \
709} \
710
711#define DEFINE_PUSH_TEST(N, DUP, FIELD, ELEM, TYPE) \
235595c4 712 DEFINE_PUSH_BASE_TEST(push##N, DUP, FIELD, ELEM, TYPE *, TYPE, \
3dbc5156 713 IS_0, TYPE##_new(), TYPE##_free)
7960dbec
DDO
714
715void cleanup_tests(void)
716{
717 return;
718}
719
720DEFINE_SET_GET_ARG_FN(set, get, option, 16, int)
3dbc5156 721/* option == OSSL_CMP_OPT_IGNORE_KEYUSAGE */
7960dbec 722DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, option_16, int, -1, IS_0, \
3dbc5156 723 1 /* true */, DROP)
7960dbec 724
7960dbec 725DEFINE_SET_CB_TEST(log_cb)
7960dbec
DDO
726
727DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, serverPath, char, IS_0)
4b1fe471
DDO
728DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, server, char)
729DEFINE_SET_INT_TEST(serverPort)
afe554c2
DDO
730DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, proxy, char)
731DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, no_proxy, char)
7960dbec
DDO
732DEFINE_SET_CB_TEST(http_cb)
733DEFINE_SET_GET_P_VOID_TEST(http_cb_arg)
734DEFINE_SET_CB_TEST(transfer_cb)
735DEFINE_SET_GET_P_VOID_TEST(transfer_cb_arg)
736
737DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, srvCert, X509)
738DEFINE_SET_TEST(ossl_cmp, ctx, 0, 0, validatedSrvCert, X509)
739DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, expected_sender, X509_NAME)
740DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set0, get0, 0, trustedStore,
235595c4 741 X509_STORE *, NULL,
3dbc5156 742 DEFAULT_STORE, X509_STORE_new_1(), X509_STORE_free)
0b86eefd 743DEFINE_SET_GET_SK_X509_TEST(OSSL_CMP, CTX, 1, 0, untrusted)
7960dbec 744
63f1883d 745DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, cert, X509)
7960dbec
DDO
746DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, pkey, EVP_PKEY)
747
748DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, recipient, X509_NAME)
749DEFINE_PUSH_TEST(0, 0, geninfo_ITAVs, geninfo_ITAV, OSSL_CMP_ITAV)
750DEFINE_SET_SK_TEST(OSSL_CMP, CTX, 1, extraCertsOut, X509)
235595c4 751DEFINE_SET_GET_ARG_FN(set0, get0, newPkey, 1, EVP_PKEY *) /* priv == 1 */
7960dbec 752DEFINE_SET_GET_TEST(OSSL_CMP, CTX, 0, 0, 0, newPkey_1, EVP_PKEY)
235595c4 753DEFINE_SET_GET_ARG_FN(set0, get0, newPkey, 0, EVP_PKEY *) /* priv == 0 */
7960dbec
DDO
754DEFINE_SET_GET_TEST(OSSL_CMP, CTX, 0, 0, 0, newPkey_0, EVP_PKEY)
755DEFINE_SET_GET1_STR_FN(set1, referenceValue)
3dbc5156
DDO
756DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, 1, referenceValue_str, char,
757 IS_0)
7960dbec 758DEFINE_SET_GET1_STR_FN(set1, secretValue)
3dbc5156 759DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, 1, secretValue_str, char, IS_0)
7960dbec
DDO
760DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, issuer, X509_NAME)
761DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, subjectName, X509_NAME)
762#ifdef ISSUE_9504_RESOLVED
763DEFINE_PUSH_TEST(1, 1, subjectAltNames, subjectAltName, GENERAL_NAME)
764#endif
765DEFINE_SET_SK_TEST(OSSL_CMP, CTX, 0, reqExtensions, X509_EXTENSION)
766DEFINE_PUSH_TEST(0, 0, policies, policy, POLICYINFO)
767DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, oldCert, X509)
768#ifdef ISSUE_9504_RESOLVED
769DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, p10CSR, X509_REQ)
770#endif
771DEFINE_PUSH_TEST(0, 0, genm_ITAVs, genm_ITAV, OSSL_CMP_ITAV)
772DEFINE_SET_CB_TEST(certConf_cb)
773DEFINE_SET_GET_P_VOID_TEST(certConf_cb_arg)
774
775DEFINE_SET_GET_INT_TEST(ossl_cmp, ctx, status)
776DEFINE_SET_GET_SK_TEST(ossl_cmp, ctx, 0, 0, statusString, ASN1_UTF8STRING)
777DEFINE_SET_GET_INT_TEST(ossl_cmp, ctx, failInfoCode)
778DEFINE_SET_GET_TEST(ossl_cmp, ctx, 0, 0, 0, newCert, X509)
39082af2 779DEFINE_SET_GET_SK_X509_TEST(ossl_cmp, ctx, 1, 1, newChain)
7960dbec
DDO
780DEFINE_SET_GET_SK_X509_TEST(ossl_cmp, ctx, 1, 1, caPubs)
781DEFINE_SET_GET_SK_X509_TEST(ossl_cmp, ctx, 1, 1, extraCertsIn)
782
3dbc5156
DDO
783DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, transactionID, ASN1_OCTET_STRING,
784 IS_0)
7960dbec
DDO
785DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, senderNonce, ASN1_OCTET_STRING)
786DEFINE_SET_TEST(ossl_cmp, ctx, 1, 1, recipNonce, ASN1_OCTET_STRING)
787
788int setup_tests(void)
789{
790 /* OSSL_CMP_CTX_new() is tested by set_up() */
791 /* OSSL_CMP_CTX_free() is tested by tear_down() */
792 ADD_TEST(test_CTX_reinit);
793
3dbc5156 794 /* various CMP options: */
7960dbec 795 ADD_TEST(test_CTX_set_get_option_16);
3dbc5156 796 /* CMP-specific callback for logging and outputting the error queue: */
7960dbec 797 ADD_TEST(test_CTX_set_get_log_cb);
7960dbec
DDO
798 /*
799 * also tests OSSL_CMP_log_open(), OSSL_CMP_CTX_set_log_verbosity(),
ebf30069
DDO
800 * ossl_cmp_err(), ossl_cmp_warn(), * ossl_cmp_debug(),
801 * ossl_cmp_log2(), ossl_cmp_log_parse_metadata(), and OSSL_CMP_log_close()
7960dbec
DDO
802 * with OSSL_CMP_severity OSSL_CMP_LOG_ERR/WARNING/DEBUG/INFO:
803 */
804 ADD_TEST(test_cmp_ctx_log_cb);
8e1a1582 805#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
3dbc5156
DDO
806 /*
807 * also tests OSSL_CMP_CTX_set_log_cb(), OSSL_CMP_print_errors_cb(),
31b28ad9 808 * and the macros ossl_cmp_add_error_data and ossl_cmp_add_error_line:
3dbc5156 809 */
7960dbec 810 ADD_TEST(test_CTX_print_errors);
85870311 811#endif
3dbc5156 812 /* message transfer: */
7960dbec 813 ADD_TEST(test_CTX_set1_get0_serverPath);
4b1fe471 814 ADD_TEST(test_CTX_set1_get0_server);
7960dbec 815 ADD_TEST(test_CTX_set_get_serverPort);
afe554c2
DDO
816 ADD_TEST(test_CTX_set1_get0_proxy);
817 ADD_TEST(test_CTX_set1_get0_no_proxy);
7960dbec
DDO
818 ADD_TEST(test_CTX_set_get_http_cb);
819 ADD_TEST(test_CTX_set_get_http_cb_arg);
820 ADD_TEST(test_CTX_set_get_transfer_cb);
821 ADD_TEST(test_CTX_set_get_transfer_cb_arg);
3dbc5156 822 /* server authentication: */
7960dbec
DDO
823 ADD_TEST(test_CTX_set1_get0_srvCert);
824 ADD_TEST(test_CTX_set0_get0_validatedSrvCert);
825 ADD_TEST(test_CTX_set1_get0_expected_sender);
826 ADD_TEST(test_CTX_set0_get0_trustedStore);
0b86eefd 827 ADD_TEST(test_CTX_set1_get0_untrusted);
3dbc5156 828 /* client authentication: */
63f1883d 829 ADD_TEST(test_CTX_set1_get0_cert);
7960dbec
DDO
830 ADD_TEST(test_CTX_set1_get0_pkey);
831 /* the following two also test ossl_cmp_asn1_octet_string_set1_bytes(): */
832 ADD_TEST(test_CTX_set1_get1_referenceValue_str);
833 ADD_TEST(test_CTX_set1_get1_secretValue_str);
3dbc5156 834 /* CMP message header and extra certificates: */
7960dbec
DDO
835 ADD_TEST(test_CTX_set1_get0_recipient);
836 ADD_TEST(test_CTX_push0_geninfo_ITAV);
837 ADD_TEST(test_CTX_set1_get0_extraCertsOut);
3dbc5156 838 /* certificate template: */
7960dbec
DDO
839 ADD_TEST(test_CTX_set0_get0_newPkey_1);
840 ADD_TEST(test_CTX_set0_get0_newPkey_0);
841 ADD_TEST(test_CTX_set1_get0_issuer);
842 ADD_TEST(test_CTX_set1_get0_subjectName);
843#ifdef ISSUE_9504_RESOLVED
3dbc5156
DDO
844 /*
845 * test currently fails, see https://github.com/openssl/openssl/issues/9504
846 */
7960dbec
DDO
847 ADD_TEST(test_CTX_push1_subjectAltName);
848#endif
849 ADD_TEST(test_CTX_set0_get0_reqExtensions);
850 ADD_TEST(test_CTX_reqExtensions_have_SAN);
851 ADD_TEST(test_CTX_push0_policy);
852 ADD_TEST(test_CTX_set1_get0_oldCert);
853#ifdef ISSUE_9504_RESOLVED
3dbc5156
DDO
854 /*
855 * test currently fails, see https://github.com/openssl/openssl/issues/9504
856 */
7960dbec
DDO
857 ADD_TEST(test_CTX_set1_get0_p10CSR);
858#endif
3dbc5156 859 /* misc body contents: */
7960dbec 860 ADD_TEST(test_CTX_push0_genm_ITAV);
3dbc5156 861 /* certificate confirmation: */
7960dbec
DDO
862 ADD_TEST(test_CTX_set_get_certConf_cb);
863 ADD_TEST(test_CTX_set_get_certConf_cb_arg);
3dbc5156 864 /* result fetching: */
7960dbec
DDO
865 ADD_TEST(test_CTX_set_get_status);
866 ADD_TEST(test_CTX_set0_get0_statusString);
867 ADD_TEST(test_CTX_set_get_failInfoCode);
868 ADD_TEST(test_CTX_set0_get0_newCert);
39082af2 869 ADD_TEST(test_CTX_set1_get1_newChain);
7960dbec
DDO
870 ADD_TEST(test_CTX_set1_get1_caPubs);
871 ADD_TEST(test_CTX_set1_get1_extraCertsIn);
3dbc5156 872 /* exported for testing and debugging purposes: */
7960dbec
DDO
873 /* the following three also test ossl_cmp_asn1_octet_string_set1(): */
874 ADD_TEST(test_CTX_set1_get0_transactionID);
875 ADD_TEST(test_CTX_set1_get0_senderNonce);
876 ADD_TEST(test_CTX_set1_get0_recipNonce);
3dbc5156 877 /* ossl_cmp_build_cert_chain() is tested in cmp_protect.c */
7960dbec
DDO
878 return 1;
879}