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