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