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