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