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