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