2 * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include "helpers/cmp_testlib.h"
14 #include <openssl/x509_vfy.h>
16 typedef struct test_fixture
{
17 const char *test_case_name
;
19 } OSSL_CMP_CTX_TEST_FIXTURE
;
21 static void tear_down(OSSL_CMP_CTX_TEST_FIXTURE
*fixture
)
24 OSSL_CMP_CTX_free(fixture
->ctx
);
25 OPENSSL_free(fixture
);
28 static OSSL_CMP_CTX_TEST_FIXTURE
*set_up(const char *const test_case_name
)
30 OSSL_CMP_CTX_TEST_FIXTURE
*fixture
;
32 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
))))
34 if (!TEST_ptr(fixture
->ctx
= OSSL_CMP_CTX_new(NULL
, NULL
))) {
38 fixture
->test_case_name
= test_case_name
;
42 static STACK_OF(X509
) *sk_X509_new_1(void)
44 STACK_OF(X509
) *sk
= sk_X509_new_null();
47 if (x
== NULL
|| !sk_X509_push(sk
, x
)) {
55 static void sk_X509_pop_X509_free(STACK_OF(X509
) *sk
)
57 OSSL_STACK_OF_X509_free(sk
);
60 static int execute_CTX_reinit_test(OSSL_CMP_CTX_TEST_FIXTURE
*fixture
)
62 OSSL_CMP_CTX
*ctx
= fixture
->ctx
;
63 ASN1_OCTET_STRING
*bytes
= NULL
;
64 STACK_OF(X509
) *certs
= NULL
;
65 X509
*cert
= X509_new();
68 /* set non-default values in all relevant fields */
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
))
84 if (!TEST_true(OSSL_CMP_CTX_reinit(ctx
)))
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
))
101 /* this does not check that all remaining fields are untouched */
106 sk_X509_pop_X509_free(certs
);
107 ASN1_OCTET_STRING_free(bytes
);
111 static int test_CTX_libctx_propq(void)
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
)
118 && TEST_ptr_eq(libctx
, OSSL_CMP_CTX_get0_libctx(cmpctx
))
119 && TEST_str_eq(propq
, OSSL_CMP_CTX_get0_propq(cmpctx
));
121 OSSL_CMP_CTX_free(cmpctx
);
122 OSSL_LIB_CTX_free(libctx
);
126 static int test_CTX_reinit(void)
128 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE
, set_up
);
129 EXECUTE_TEST(execute_CTX_reinit_test
, tear_down
);
133 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
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
)
139 msg_total_size
+= strlen(msg
);
140 TEST_note("total=%d len=%zu msg='%s'\n", msg_total_size
, strlen(msg
), msg
);
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>"
151 static int execute_CTX_print_errors_test(OSSL_CMP_CTX_TEST_FIXTURE
*fixture
)
153 OSSL_CMP_CTX
*ctx
= fixture
->ctx
;
154 int base_err_msg_size
, expected_size
;
157 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx
, NULL
)))
159 if (!TEST_true(ctx
->log_cb
== NULL
))
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 */
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
)))
170 if (!TEST_true(ctx
->log_cb
== msg_total_size_log_cb
)) {
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
))
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
);
195 expected_size
+= base_err_msg_size
- 2 * strlen(STR_SEP
);
197 OSSL_CMP_CTX_print_errors(ctx
);
198 if (!TEST_int_eq(msg_total_size
, expected_size
))
205 static int test_CTX_print_errors(void)
207 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE
, set_up
);
208 EXECUTE_TEST(execute_CTX_print_errors_test
, tear_down
);
214 int execute_CTX_reqExtensions_have_SAN_test(OSSL_CMP_CTX_TEST_FIXTURE
*fixture
)
216 OSSL_CMP_CTX
*ctx
= fixture
->ctx
;
218 unsigned char str
[16 /* = len */];
219 ASN1_OCTET_STRING
*data
= NULL
;
220 X509_EXTENSION
*ext
= NULL
;
221 X509_EXTENSIONS
*exts
= NULL
;
224 if (!TEST_false(OSSL_CMP_CTX_reqExtensions_have_SAN(ctx
)))
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
)))
231 ext
= X509_EXTENSION_create_by_NID(NULL
, NID_subject_alt_name
, 0, data
);
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
);
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
);
246 ASN1_OCTET_STRING_free(data
);
250 static int test_CTX_reqExtensions_have_SAN(void)
252 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE
, set_up
);
253 EXECUTE_TEST(execute_CTX_reqExtensions_have_SAN_test
, tear_down
);
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
)
264 (TEST_str_eq(func
, "execute_cmp_ctx_log_cb_test")
265 || TEST_str_eq(func
, "(unknown function)")) &&
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");
275 static int execute_cmp_ctx_log_cb_test(OSSL_CMP_CTX_TEST_FIXTURE
*fixture
)
278 OSSL_CMP_CTX
*ctx
= fixture
->ctx
;
280 OSSL_TRACE(ALL
, "this general trace message is not shown by default\n");
283 OSSL_CMP_log_open(); /* multiple calls should be harmless */
285 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx
, NULL
))) {
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
));
295 if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx
, test_log_cb
))) {
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))
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))
309 OSSL_CMP_log_close();
310 OSSL_CMP_log_close(); /* multiple calls should be harmless */
314 static int test_cmp_ctx_log_cb(void)
316 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE
, set_up
);
317 EXECUTE_TEST(execute_cmp_ctx_log_cb_test
, tear_down
);
321 #ifndef OPENSSL_NO_HTTP
322 static BIO
*test_http_cb(BIO
*bio
, void *arg
, int use_ssl
, int detail
)
328 static OSSL_CMP_MSG
*test_transfer_cb(OSSL_CMP_CTX
*ctx
,
329 const OSSL_CMP_MSG
*req
)
334 static int test_certConf_cb(OSSL_CMP_CTX
*ctx
, X509
*cert
, int fail_info
,
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 */
350 #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
351 DEFAULT, NEW, FREE) \
353 execute_CTX_##SETN##_##GETN##_##FIELD(OSSL_CMP_CTX_TEST_FIXTURE *fixture) \
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; \
369 if (!TEST_int_eq(ERR_peek_error(), 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"); \
379 if ((*get_fn)(NULL) != ERR || ERR_peek_error() == 0) { \
380 TEST_error("getter did not return error on ctx == NULL"); \
385 val1_read = (*get_fn)(ctx); \
386 if (!DEFAULT(val1_read)) { \
387 TEST_error("did not get default value"); \
390 if (!(*set_fn)(ctx, val1)) { \
391 TEST_error("setting first value failed"); \
395 val1_to_free = 0; /* 0 works for any type */ \
399 val1_read = (*get_fn)(ctx); \
401 if (val1_read != val1) { \
402 TEST_error("set/get first value did not match"); \
406 if (DUP && val1_read == val1) { \
407 TEST_error("first set did not dup the value"); \
410 if (DEFAULT(val1_read)) { \
411 TEST_error("first set had no effect"); \
416 if (!(*set_fn)(ctx, val2)) { \
417 TEST_error("setting second value failed"); \
423 val2_read = (*get_fn)(ctx); \
424 if (DEFAULT(val2_read)) { \
425 TEST_error("second set reset the value"); \
428 if (SETN == 0 && GETN == 0) { \
429 if (val2_read != val2) { \
430 TEST_error("set/get second value did not match"); \
434 if (DUP && val2_read == val2) { \
435 TEST_error("second set did not dup the value"); \
438 if (val2 == val1) { \
439 TEST_error("second value is same as first value"); \
442 if (GETN == 1 && val2_read == val1_read) { \
444 * Note that if GETN == 0 then possibly val2_read == val1_read \
445 * because set1 may allocate the new copy at the same location. \
447 TEST_error("second get returned same as first get"); \
452 val3_read = (*get_fn)(ctx); \
453 if (DEFAULT(val3_read)) { \
454 TEST_error("third set reset the value"); \
458 if (val3_read != val2_read) { \
459 TEST_error("third get gave different value"); \
463 if (DUP && val3_read == val2_read) { \
464 TEST_error("third get did not create a new dup"); \
468 /* this does not check that all remaining fields are untouched */ \
470 if (!TEST_int_eq(ERR_peek_error(), 0)) \
473 FREE(val1_to_free); \
474 FREE(val2_to_free); \
480 return TEST_true(res); \
483 static int test_CTX_##SETN##_##GETN##_##FIELD(void) \
485 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up); \
486 EXECUTE_TEST(execute_CTX_##SETN##_##GETN##_##FIELD, tear_down); \
490 static char *char_new(void)
492 return OPENSSL_strdup("test");
495 static void char_free(char *val
)
500 #define EMPTY_SK_X509(x) ((x) == NULL || sk_X509_num(x) == 0)
502 static X509_STORE
*X509_STORE_new_1(void)
504 X509_STORE
*store
= X509_STORE_new();
507 X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store
), 1);
511 #define DEFAULT_STORE(x) \
512 ((x) == NULL || X509_VERIFY_PARAM_get_flags(X509_STORE_get0_param(x)) == 0)
514 #define IS_NEG(x) ((x) < 0)
515 #define IS_0(x) ((x) == 0) /* for any type */
516 #define DROP(x) (void)(x) /* dummy free() for non-pointer and function types */
518 #define RET_IF_NULL_ARG(ctx, ret) \
520 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
524 /* cannot use PREFIX instead of OSSL_CMP and CTX due to #define OSSL_CMP_CTX */
525 #define DEFINE_SET_GET_TEST(OSSL_CMP, CTX, N, M, DUP, FIELD, TYPE) \
526 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, DUP, FIELD, \
527 TYPE *, NULL, IS_0, TYPE##_new(), TYPE##_free)
529 #define DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FIELD, ELEM_TYPE, \
530 DEFAULT, NEW, FREE) \
531 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, 1, FIELD, \
532 STACK_OF(ELEM_TYPE)*, NULL, DEFAULT, NEW, FREE)
533 #define DEFINE_SET_GET_SK_TEST(OSSL_CMP, CTX, N, M, FIELD, T) \
534 DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FIELD, T, \
535 IS_0, sk_##T##_new_null(), sk_##T##_free)
536 #define DEFINE_SET_GET_SK_X509_TEST(OSSL_CMP, CTX, N, M, FNAME) \
537 DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FNAME, X509, \
539 sk_X509_new_1(), sk_X509_pop_X509_free)
541 #define DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, N, M, DUP, FIELD, TYPE, \
543 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, DUP, FIELD, \
544 TYPE *, NULL, DEFAULT, TYPE##_new(), TYPE##_free)
545 #define DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, N, DUP, FIELD, TYPE, DEFAULT) \
546 static TYPE *OSSL_CMP_CTX_get0_##FIELD(const CMP_CTX *ctx) \
548 RET_IF_NULL_ARG(ctx, NULL); \
549 return (TYPE *)ctx->FIELD; \
551 DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, N, 0, DUP, FIELD, TYPE, DEFAULT)
552 #define DEFINE_SET_TEST(OSSL_CMP, CTX, N, DUP, FIELD, TYPE) \
553 DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, N, DUP, FIELD, TYPE, IS_0)
555 #define DEFINE_SET_SK_TEST(OSSL_CMP, CTX, N, FIELD, TYPE) \
556 static STACK_OF(TYPE) *OSSL_CMP_CTX_get0_##FIELD(const CMP_CTX *ctx) \
558 RET_IF_NULL_ARG(ctx, NULL); \
561 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get0, 1, FIELD, \
562 STACK_OF(TYPE)*, NULL, IS_0, \
563 sk_##TYPE##_new_null(), sk_##TYPE##_free)
565 #ifndef OPENSSL_NO_HTTP
566 typedef OSSL_HTTP_bio_cb_t OSSL_CMP_http_cb_t
;
568 #define DEFINE_SET_CB_TEST(FIELD) \
569 static OSSL_CMP_##FIELD##_t OSSL_CMP_CTX_get_##FIELD(const CMP_CTX *ctx) \
571 RET_IF_NULL_ARG(ctx, NULL); \
574 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, FIELD, \
575 OSSL_CMP_##FIELD##_t, NULL, IS_0, \
577 #define DEFINE_SET_GET_P_VOID_TEST(FIELD) \
578 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, FIELD, void *, \
579 NULL, IS_0, ((void *)1), DROP)
581 #define DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, DEFAULT) \
582 DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set, get, 0, FIELD, int, -1, \
584 #define DEFINE_SET_GET_INT_TEST(OSSL_CMP, CTX, FIELD) \
585 DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, IS_NEG)
586 #define DEFINE_SET_INT_TEST(FIELD) \
587 static int OSSL_CMP_CTX_get_##FIELD(const CMP_CTX *ctx) \
589 RET_IF_NULL_ARG(ctx, -1); \
592 DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, IS_0)
594 #define DEFINE_SET_GET_ARG_FN(SETN, GETN, FIELD, ARG, T) \
595 static int OSSL_CMP_CTX_##SETN##_##FIELD##_##ARG(CMP_CTX *ctx, T val) \
597 return OSSL_CMP_CTX_##SETN##_##FIELD(ctx, ARG, val); \
600 static T OSSL_CMP_CTX_##GETN##_##FIELD##_##ARG(const CMP_CTX *ctx) \
602 return OSSL_CMP_CTX_##GETN##_##FIELD(ctx, ARG); \
605 #define DEFINE_SET_GET1_STR_FN(SETN, FIELD) \
606 static int OSSL_CMP_CTX_##SETN##_##FIELD##_str(CMP_CTX *ctx, char *val)\
608 return OSSL_CMP_CTX_##SETN##_##FIELD(ctx, (unsigned char *)val, \
612 static char *OSSL_CMP_CTX_get1_##FIELD##_str(const CMP_CTX *ctx) \
614 const ASN1_OCTET_STRING *bytes = NULL; \
616 RET_IF_NULL_ARG(ctx, NULL); \
617 bytes = ctx->FIELD; \
618 return bytes == NULL ? NULL : \
619 OPENSSL_strndup((char *)bytes->data, bytes->length); \
625 #define DEFINE_PUSH_BASE_TEST(PUSHN, DUP, FIELD, ELEM, TYPE, T, \
626 DEFAULT, NEW, FREE) \
627 static TYPE sk_top_##FIELD(const CMP_CTX *ctx) \
629 return sk_##T##_value(ctx->FIELD, sk_##T##_num(ctx->FIELD) - 1); \
632 static int execute_CTX_##PUSHN##_##ELEM(OSSL_CMP_CTX_TEST_FIXTURE *fixture) \
634 CMP_CTX *ctx = fixture->ctx; \
635 int (*push_fn)(CMP_CTX *ctx, TYPE) = \
636 (int (*)(CMP_CTX *ctx, TYPE))OSSL_CMP_CTX_##PUSHN##_##ELEM; \
638 * need type cast in above assignment because TYPE arg sometimes is const \
640 int n_elem = sk_##T##_num(ctx->FIELD); \
641 STACK_OF(TYPE) field_read; \
642 TYPE val1_to_free = NEW; \
643 TYPE val1 = val1_to_free; \
644 TYPE val1_read = 0; /* 0 works for any type */ \
645 TYPE val2_to_free = NEW; \
646 TYPE val2 = val2_to_free; \
647 TYPE val2_read = 0; \
650 if (!TEST_int_eq(ERR_peek_error(), 0)) \
652 if ((*push_fn)(NULL, val1) || ERR_peek_error() == 0) { \
653 TEST_error("pusher did not return error on ctx == NULL"); \
658 if (n_elem < 0) /* can happen for NULL stack */ \
660 field_read = ctx->FIELD; \
661 if (!DEFAULT(field_read)) { \
662 TEST_error("did not get default value for stack field"); \
665 if (!(*push_fn)(ctx, val1)) { \
666 TEST_error("pushing first value failed"); \
670 val1_to_free = 0; /* 0 works for any type */ \
672 if (sk_##T##_num(ctx->FIELD) != ++n_elem) { \
673 TEST_error("pushing first value did not increment number"); \
676 val1_read = sk_top_##FIELD(ctx); \
678 if (val1_read != val1) { \
679 TEST_error("push/sk_top first value did not match"); \
683 if (DUP && val1_read == val1) { \
684 TEST_error("first push did not dup the value"); \
689 if (!(*push_fn)(ctx, val2)) { \
690 TEST_error("pushing second value failed"); \
696 if (sk_##T##_num(ctx->FIELD) != ++n_elem) { \
697 TEST_error("pushing second value did not increment number"); \
700 val2_read = sk_top_##FIELD(ctx); \
702 if (val2_read != val2) { \
703 TEST_error("push/sk_top second value did not match"); \
707 if (DUP && val2_read == val2) { \
708 TEST_error("second push did not dup the value"); \
711 if (val2 == val1) { \
712 TEST_error("second value is same as first value"); \
716 /* this does not check if all remaining fields and elems are untouched */ \
718 if (!TEST_int_eq(ERR_peek_error(), 0)) \
721 FREE(val1_to_free); \
722 FREE(val2_to_free); \
723 return TEST_true(res); \
726 static int test_CTX_##PUSHN##_##ELEM(void) \
728 SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up); \
729 EXECUTE_TEST(execute_CTX_##PUSHN##_##ELEM, tear_down); \
733 #define DEFINE_PUSH_TEST(N, DUP, FIELD, ELEM, TYPE) \
734 DEFINE_PUSH_BASE_TEST(push##N, DUP, FIELD, ELEM, TYPE *, TYPE, \
735 IS_0, TYPE##_new(), TYPE##_free)
737 void cleanup_tests(void)
742 DEFINE_SET_GET_ARG_FN(set
, get
, option
, 35, int) /* OPT_IGNORE_KEYUSAGE */
743 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX
, set
, get
, 0, option_35
, int, -1, IS_0
, \
746 DEFINE_SET_CB_TEST(log_cb
)
748 DEFINE_SET_TEST_DEFAULT(OSSL_CMP
, CTX
, 1, 1, serverPath
, char, IS_0
)
749 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, server
, char)
750 DEFINE_SET_INT_TEST(serverPort
)
751 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, proxy
, char)
752 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, no_proxy
, char)
753 #ifndef OPENSSL_NO_HTTP
754 DEFINE_SET_CB_TEST(http_cb
)
755 DEFINE_SET_GET_P_VOID_TEST(http_cb_arg
)
757 DEFINE_SET_CB_TEST(transfer_cb
)
758 DEFINE_SET_GET_P_VOID_TEST(transfer_cb_arg
)
760 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 0, srvCert
, X509
)
761 DEFINE_SET_GET_TEST(ossl_cmp
, ctx
, 1, 0, 0, validatedSrvCert
, X509
)
762 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, expected_sender
, X509_NAME
)
763 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX
, set0
, get0
, 0, trusted
,
765 DEFAULT_STORE
, X509_STORE_new_1(), X509_STORE_free
)
766 DEFINE_SET_GET_SK_X509_TEST(OSSL_CMP
, CTX
, 1, 0, untrusted
)
768 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 0, cert
, X509
)
769 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 0, pkey
, EVP_PKEY
)
771 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, recipient
, X509_NAME
)
772 DEFINE_PUSH_TEST(0, 0, geninfo_ITAVs
, geninfo_ITAV
, OSSL_CMP_ITAV
)
773 DEFINE_SET_SK_TEST(OSSL_CMP
, CTX
, 1, extraCertsOut
, X509
)
774 DEFINE_SET_GET_ARG_FN(set0
, get0
, newPkey
, 1, EVP_PKEY
*) /* priv == 1 */
775 DEFINE_SET_GET_TEST(OSSL_CMP
, CTX
, 0, 0, 0, newPkey_1
, EVP_PKEY
)
776 DEFINE_SET_GET_ARG_FN(set0
, get0
, newPkey
, 0, EVP_PKEY
*) /* priv == 0 */
777 DEFINE_SET_GET_TEST(OSSL_CMP
, CTX
, 0, 0, 0, newPkey_0
, EVP_PKEY
)
778 DEFINE_SET_GET1_STR_FN(set1
, referenceValue
)
779 DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP
, CTX
, 1, 1, 1, referenceValue_str
, char,
781 DEFINE_SET_GET1_STR_FN(set1
, secretValue
)
782 DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP
, CTX
, 1, 1, 1, secretValue_str
, char, IS_0
)
783 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, issuer
, X509_NAME
)
784 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, subjectName
, X509_NAME
)
785 #ifdef ISSUE_9504_RESOLVED
786 DEFINE_PUSH_TEST(1, 1, subjectAltNames
, subjectAltName
, GENERAL_NAME
)
788 DEFINE_SET_SK_TEST(OSSL_CMP
, CTX
, 0, reqExtensions
, X509_EXTENSION
)
789 DEFINE_PUSH_TEST(0, 0, policies
, policy
, POLICYINFO
)
790 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 0, oldCert
, X509
)
791 #ifdef ISSUE_9504_RESOLVED
792 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, p10CSR
, X509_REQ
)
794 DEFINE_PUSH_TEST(0, 0, genm_ITAVs
, genm_ITAV
, OSSL_CMP_ITAV
)
795 DEFINE_SET_CB_TEST(certConf_cb
)
796 DEFINE_SET_GET_P_VOID_TEST(certConf_cb_arg
)
798 DEFINE_SET_GET_INT_TEST(ossl_cmp
, ctx
, status
)
799 DEFINE_SET_GET_SK_TEST(ossl_cmp
, ctx
, 0, 0, statusString
, ASN1_UTF8STRING
)
800 DEFINE_SET_GET_INT_TEST(ossl_cmp
, ctx
, failInfoCode
)
801 DEFINE_SET_GET_TEST(ossl_cmp
, ctx
, 0, 0, 0, newCert
, X509
)
802 DEFINE_SET_GET_SK_X509_TEST(ossl_cmp
, ctx
, 1, 1, newChain
)
803 DEFINE_SET_GET_SK_X509_TEST(ossl_cmp
, ctx
, 1, 1, caPubs
)
804 DEFINE_SET_GET_SK_X509_TEST(ossl_cmp
, ctx
, 1, 1, extraCertsIn
)
806 DEFINE_SET_TEST_DEFAULT(OSSL_CMP
, CTX
, 1, 1, transactionID
, ASN1_OCTET_STRING
,
808 DEFINE_SET_TEST(OSSL_CMP
, CTX
, 1, 1, senderNonce
, ASN1_OCTET_STRING
)
809 DEFINE_SET_TEST(ossl_cmp
, ctx
, 1, 1, recipNonce
, ASN1_OCTET_STRING
)
811 int setup_tests(void)
813 if (!test_skip_common_options()) {
814 TEST_error("Error parsing test options\n");
818 /* also tests OSSL_CMP_CTX_new() and OSSL_CMP_CTX_free(): */
819 ADD_TEST(test_CTX_libctx_propq
);
820 ADD_TEST(test_CTX_reinit
);
822 /* various CMP options: */
823 ADD_TEST(test_CTX_set_get_option_35
);
824 /* CMP-specific callback for logging and outputting the error queue: */
825 ADD_TEST(test_CTX_set_get_log_cb
);
827 * also tests OSSL_CMP_log_open(), OSSL_CMP_CTX_set_log_verbosity(),
828 * ossl_cmp_err(), ossl_cmp_warn(), * ossl_cmp_debug(),
829 * ossl_cmp_log2(), ossl_cmp_log_parse_metadata(), and OSSL_CMP_log_close()
830 * with OSSL_CMP_severity OSSL_CMP_LOG_ERR/WARNING/DEBUG/INFO:
832 ADD_TEST(test_cmp_ctx_log_cb
);
833 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
835 * also tests OSSL_CMP_CTX_set_log_cb(), OSSL_CMP_print_errors_cb(),
836 * and the macros ossl_cmp_add_error_data and ossl_cmp_add_error_line:
838 ADD_TEST(test_CTX_print_errors
);
840 /* message transfer: */
841 ADD_TEST(test_CTX_set1_get0_serverPath
);
842 ADD_TEST(test_CTX_set1_get0_server
);
843 ADD_TEST(test_CTX_set_get_serverPort
);
844 ADD_TEST(test_CTX_set1_get0_proxy
);
845 ADD_TEST(test_CTX_set1_get0_no_proxy
);
846 #ifndef OPENSSL_NO_HTTP
847 ADD_TEST(test_CTX_set_get_http_cb
);
848 ADD_TEST(test_CTX_set_get_http_cb_arg
);
850 ADD_TEST(test_CTX_set_get_transfer_cb
);
851 ADD_TEST(test_CTX_set_get_transfer_cb_arg
);
852 /* server authentication: */
853 ADD_TEST(test_CTX_set1_get0_srvCert
);
854 ADD_TEST(test_CTX_set1_get0_validatedSrvCert
);
855 ADD_TEST(test_CTX_set1_get0_expected_sender
);
856 ADD_TEST(test_CTX_set0_get0_trusted
);
857 ADD_TEST(test_CTX_set1_get0_untrusted
);
858 /* client authentication: */
859 ADD_TEST(test_CTX_set1_get0_cert
);
860 ADD_TEST(test_CTX_set1_get0_pkey
);
861 /* the following two also test ossl_cmp_asn1_octet_string_set1_bytes(): */
862 ADD_TEST(test_CTX_set1_get1_referenceValue_str
);
863 ADD_TEST(test_CTX_set1_get1_secretValue_str
);
864 /* CMP message header and extra certificates: */
865 ADD_TEST(test_CTX_set1_get0_recipient
);
866 ADD_TEST(test_CTX_push0_geninfo_ITAV
);
867 ADD_TEST(test_CTX_set1_get0_extraCertsOut
);
868 /* certificate template: */
869 ADD_TEST(test_CTX_set0_get0_newPkey_1
);
870 ADD_TEST(test_CTX_set0_get0_newPkey_0
);
871 ADD_TEST(test_CTX_set1_get0_issuer
);
872 ADD_TEST(test_CTX_set1_get0_subjectName
);
873 #ifdef ISSUE_9504_RESOLVED
875 * test currently fails, see https://github.com/openssl/openssl/issues/9504
877 ADD_TEST(test_CTX_push1_subjectAltName
);
879 ADD_TEST(test_CTX_set0_get0_reqExtensions
);
880 ADD_TEST(test_CTX_reqExtensions_have_SAN
);
881 ADD_TEST(test_CTX_push0_policy
);
882 ADD_TEST(test_CTX_set1_get0_oldCert
);
883 #ifdef ISSUE_9504_RESOLVED
885 * test currently fails, see https://github.com/openssl/openssl/issues/9504
887 ADD_TEST(test_CTX_set1_get0_p10CSR
);
889 /* misc body contents: */
890 ADD_TEST(test_CTX_push0_genm_ITAV
);
891 /* certificate confirmation: */
892 ADD_TEST(test_CTX_set_get_certConf_cb
);
893 ADD_TEST(test_CTX_set_get_certConf_cb_arg
);
894 /* result fetching: */
895 ADD_TEST(test_CTX_set_get_status
);
896 ADD_TEST(test_CTX_set0_get0_statusString
);
897 ADD_TEST(test_CTX_set_get_failInfoCode
);
898 ADD_TEST(test_CTX_set0_get0_newCert
);
899 ADD_TEST(test_CTX_set1_get1_newChain
);
900 ADD_TEST(test_CTX_set1_get1_caPubs
);
901 ADD_TEST(test_CTX_set1_get1_extraCertsIn
);
902 /* exported for testing and debugging purposes: */
903 /* the following three also test ossl_cmp_asn1_octet_string_set1(): */
904 ADD_TEST(test_CTX_set1_get0_transactionID
);
905 ADD_TEST(test_CTX_set1_get0_senderNonce
);
906 ADD_TEST(test_CTX_set1_get0_recipNonce
);