3c8d75f4e0f1f945dfb9277913c3eb24422c3669
[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 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
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          || strcmp(func, "(unknown function)") == 0) &&
245 # endif
246         (strcmp(file, OPENSSL_FILE) == 0 || strcmp(file, "(no file)") == 0)
247         && (line == test_log_line || line == 0)
248         && (level == OSSL_CMP_LOG_INFO || level == -1)
249         && strcmp(msg, "ok\n") == 0;
250     return 1;
251 }
252 #endif
253
254 static int execute_cmp_ctx_log_cb_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
255 {
256     int res = 1;
257 #if !defined OPENSSL_NO_TRACE && !defined OPENSSL_NO_STDIO
258     OSSL_CMP_CTX *ctx = fixture->ctx;
259
260     OSSL_TRACE(ALL, "this general trace message is not shown by default\n");
261
262     OSSL_CMP_log_open();
263     OSSL_CMP_log_open(); /* multiple calls should be harmless */
264
265     if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx, NULL))) {
266         res = 0;
267     } else {
268         OSSL_CMP_err("this should be printed as CMP error message");
269         OSSL_CMP_warn("this should be printed as CMP warning message");
270         OSSL_CMP_debug("this should not be printed");
271         TEST_true(OSSL_CMP_CTX_set_log_verbosity(ctx, OSSL_CMP_LOG_DEBUG));
272         OSSL_CMP_debug("this should be printed as CMP debug message");
273         TEST_true(OSSL_CMP_CTX_set_log_verbosity(ctx, OSSL_CMP_LOG_INFO));
274     }
275     if (!TEST_true(OSSL_CMP_CTX_set_log_cb(ctx, test_log_cb))) {
276         res = 0;
277     } else {
278         test_log_line = OPENSSL_LINE + 1;
279         OSSL_CMP_log2(INFO, "%s%c", "o", 'k');
280         if (!TEST_int_eq(test_log_cb_res, 1))
281             res = 0;
282         OSSL_CMP_CTX_set_log_verbosity(ctx, OSSL_CMP_LOG_ERR);
283         test_log_cb_res = -1; /* callback should not be called at all */
284         test_log_line = OPENSSL_LINE + 1;
285         OSSL_CMP_log2(INFO, "%s%c", "o", 'k');
286         if (!TEST_int_eq(test_log_cb_res, -1))
287             res = 0;
288     }
289     OSSL_CMP_log_close();
290     OSSL_CMP_log_close(); /* multiple calls should be harmless */
291 #endif
292     return res;
293 }
294
295 static int test_cmp_ctx_log_cb(void)
296 {
297     SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up);
298     EXECUTE_TEST(execute_cmp_ctx_log_cb_test, tear_down);
299     return result;
300 }
301
302 static BIO *test_http_cb(OSSL_CMP_CTX *ctx, BIO *hbio, unsigned long detail)
303 {
304     return NULL;
305 }
306
307 static int test_transfer_cb(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
308                             OSSL_CMP_MSG **res)
309 {
310     return 0;
311 }
312
313 static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
314                             const char **txt)
315 {
316     return 0;
317 }
318
319 typedef OSSL_CMP_CTX CMP_CTX; /* prevents rewriting type name by below macro */
320 #define OSSL_CMP_CTX 1 /* name prefix for exported setter functions */
321 #define ossl_cmp_ctx 0 /* name prefix for internal setter functions */
322 #define set 0
323 #define set0 0
324 #define set1 1
325 #define get 0
326 #define get0 0
327 #define get1 1
328
329 #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
330                                  DEFAULT, NEW, FREE) \
331 static int execute_CTX_##SETN##_##GETN##_##FIELD( \
332     OSSL_CMP_CTX_TEST_FIXTURE *fixture) \
333 { \
334     CMP_CTX *ctx = fixture->ctx; \
335     int (*set_fn)(CMP_CTX *ctx, TYPE) = \
336         (int (*)(CMP_CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
337  /* need type cast in above assignment because TYPE arg sometimes is const */ \
338     TYPE (*get_fn)(const CMP_CTX *ctx) = OSSL_CMP_CTX_##GETN##_##FIELD; \
339     TYPE val1_to_free = NEW; \
340     TYPE val1 = val1_to_free; \
341     TYPE val1_read = 0; /* 0 works for any type */ \
342     TYPE val2_to_free = NEW; \
343     TYPE val2 = val2_to_free; \
344     TYPE val2_read = 0; \
345     TYPE val3_read = 0; \
346     int res = 1; \
347     \
348     if (!TEST_int_eq(ERR_peek_error(), 0)) \
349         res = 0; \
350     if (PREFIX == 1) { /* exported setter functions must test ctx == NULL */ \
351         if ((*set_fn)(NULL, val1) || ERR_peek_error() == 0) { \
352             TEST_error("setter did not return error on ctx == NULL"); \
353             res = 0; \
354         } \
355     } \
356     ERR_clear_error(); \
357     \
358     if ((*get_fn)(NULL) != ERR || ERR_peek_error() == 0) { \
359         TEST_error("getter did not return error on ctx == NULL"); \
360         res = 0; \
361     } \
362     ERR_clear_error(); \
363     \
364     val1_read = (*get_fn)(ctx); \
365     if (!DEFAULT(val1_read)) { \
366         TEST_error("did not get default value"); \
367         res = 0; \
368     } \
369     if (!(*set_fn)(ctx, val1)) { \
370         TEST_error("setting first value failed"); \
371         res = 0; \
372     } \
373     if (SETN == 0) \
374         val1_to_free = 0; /* 0 works for any type */ \
375     \
376     if (GETN == 1) \
377         FREE(val1_read); \
378     val1_read = (*get_fn)(ctx); \
379     if (SETN == 0) { \
380         if (val1_read != val1) { \
381             TEST_error("set/get first value did not match"); \
382             res = 0; \
383         } \
384     } else { \
385         if (DUP && val1_read == val1) { \
386             TEST_error("first set did not dup the value"); \
387             res = 0; \
388         } \
389         if (DEFAULT(val1_read)) { \
390             TEST_error("first set had no effect"); \
391             res = 0; \
392         } \
393     } \
394     \
395     if (!(*set_fn)(ctx, val2)) { \
396         TEST_error("setting second value failed"); \
397         res = 0; \
398     } \
399     if (SETN == 0) \
400         val2_to_free = 0; \
401     \
402     val2_read = (*get_fn)(ctx); \
403     if (DEFAULT(val2_read)) { \
404         TEST_error("second set reset the value"); \
405         res = 0; \
406     } \
407     if (SETN == 0 && GETN == 0) { \
408         if (val2_read != val2) { \
409             TEST_error("set/get second value did not match"); \
410             res = 0; \
411         } \
412     } else { \
413         if (DUP && val2_read == val2) { \
414             TEST_error("second set did not dup the value"); \
415             res = 0; \
416         } \
417         if (val2 == val1) { \
418             TEST_error("second value is same as first value"); \
419             res = 0; \
420         } \
421         if (GETN == 1 && val2_read == val1_read) { \
422             /* \
423              * Note that if GETN == 0 then possibly val2_read == val1_read \
424              * because set1 may allocate the new copy at the same location. \
425              */ \
426             TEST_error("second get returned same as first get"); \
427             res = 0; \
428         } \
429     } \
430     \
431     val3_read = (*get_fn)(ctx); \
432     if (DEFAULT(val3_read)) { \
433         TEST_error("third set reset the value"); \
434         res = 0; \
435     } \
436     if (GETN == 0) { \
437         if (val3_read != val2_read) { \
438             TEST_error("third get gave different value"); \
439             res = 0; \
440         } \
441     } else  { \
442         if (DUP && val3_read == val2_read) { \
443             TEST_error("third get did not create a new dup"); \
444             res = 0; \
445         } \
446     } \
447     /* this does not check that all remaining fields are untouched */ \
448     \
449     if (!TEST_int_eq(ERR_peek_error(), 0)) \
450         res = 0; \
451     \
452     FREE(val1_to_free); \
453     FREE(val2_to_free); \
454     if (GETN == 1) { \
455         FREE(val1_read); \
456         FREE(val2_read); \
457         FREE(val3_read); \
458     } \
459     return TEST_true(res); \
460 } \
461 \
462 static int test_CTX_##SETN##_##GETN##_##FIELD(void) \
463 { \
464     SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up); \
465     EXECUTE_TEST(execute_CTX_##SETN##_##GETN##_##FIELD, tear_down); \
466     return result; \
467 }
468
469 static char *char_new(void) {
470     return OPENSSL_strdup("test");
471 }
472
473 static void char_free(char *val) {
474     OPENSSL_free(val);
475 }
476
477 #define EMPTY_SK_X509(x) ((x) == NULL || sk_X509_num(x) == 0)
478
479 static X509_STORE *X509_STORE_new_1(void) {
480     X509_STORE *store = X509_STORE_new();
481
482     if (store != NULL)
483         X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store), 1);
484     return store;
485 }
486
487 #define DEFAULT_STORE(x) ((x) == NULL \
488     || X509_VERIFY_PARAM_get_flags(X509_STORE_get0_param(x)) == 0)
489
490 #define IS_NEG(x) ((x) < 0)
491 #define IS_0(x) ((x) == 0) /* for any type */
492 #define IS_DEFAULT_PORT(x) ((x) == OSSL_CMP_DEFAULT_PORT)
493 #define DROP(x) (void)(x) /* dummy free() for non-pointer and function types */
494
495 #define ERR(x) (CMPerr(0, CMP_R_NULL_ARGUMENT), x)
496
497 #define DEFINE_SET_GET_TEST(OSSL_CMP, CTX, N, M, DUP, FIELD, TYPE) \
498     DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, DUP, FIELD, \
499                               TYPE*, NULL, IS_0, TYPE##_new(), TYPE##_free)
500
501 #define DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FIELD, ELEM_TYPE, \
502                                        DEFAULT, NEW, FREE) \
503     DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, 1, FIELD, \
504                               STACK_OF(ELEM_TYPE)*, NULL, DEFAULT, NEW, FREE)
505 #define DEFINE_SET_GET_SK_TEST(OSSL_CMP, CTX, N, M, FIELD, T) \
506     DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FIELD, T, \
507                                     IS_0, sk_##T##_new_null(), sk_##T##_free)
508 #define DEFINE_SET_GET_SK_X509_TEST(OSSL_CMP, CTX, N, M, FNAME) \
509     DEFINE_SET_GET_SK_TEST_DEFAULT(OSSL_CMP, CTX, N, M, FNAME, X509, \
510                                     EMPTY_SK_X509, \
511                                     sk_X509_new_1(), sk_X509_pop_X509_free)
512
513 #define DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, N, M, DUP, FIELD, TYPE, \
514                                     DEFAULT) \
515     DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get##M, DUP, FIELD, \
516                               TYPE*, NULL, DEFAULT, TYPE##_new(), TYPE##_free)
517 #define DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, N, DUP, FIELD, TYPE, DEFAULT) \
518     static TYPE *OSSL_CMP_CTX_get0_##FIELD(const CMP_CTX *ctx) \
519     { \
520         return ctx == NULL ? ERR(NULL) : ctx->FIELD; \
521     } \
522     DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, N, 0, DUP, FIELD, TYPE, DEFAULT)
523 #define DEFINE_SET_TEST(OSSL_CMP, CTX, N, DUP, FIELD, TYPE) \
524     DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, N, DUP, FIELD, TYPE, IS_0)
525
526 #define DEFINE_SET_SK_TEST(OSSL_CMP, CTX, N, FIELD, TYPE) \
527     static STACK_OF(TYPE) *OSSL_CMP_CTX_get0_##FIELD(const CMP_CTX *ctx) \
528     { \
529         return ctx == NULL ? ERR(NULL) : ctx->FIELD; \
530     } \
531     DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set##N, get0, 1, FIELD, \
532                               STACK_OF(TYPE)*, NULL, IS_0, \
533                               sk_##TYPE##_new_null(), sk_##TYPE##_free)
534
535 #define DEFINE_SET_CB_TEST(FIELD) \
536     static OSSL_cmp_##FIELD##_t \
537         OSSL_CMP_CTX_get_##FIELD(const CMP_CTX *ctx) \
538     { \
539         if (ctx == NULL) \
540             CMPerr(0, CMP_R_NULL_ARGUMENT); \
541         return ctx == NULL ? NULL /* cannot use ERR(NULL) here */ : ctx->FIELD;\
542     } \
543     DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, FIELD, \
544                               OSSL_cmp_##FIELD##_t, NULL, IS_0, \
545                               test_##FIELD, DROP)
546 #define DEFINE_SET_GET_P_VOID_TEST(FIELD) \
547     DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, FIELD, void*, \
548                               NULL, IS_0, ((void *)1), DROP)
549
550 #define DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, DEFAULT) \
551     DEFINE_SET_GET_BASE_TEST(OSSL_CMP##_##CTX, set, get, 0, FIELD, int, -1, \
552                               DEFAULT, 1, DROP)
553 #define DEFINE_SET_GET_INT_TEST(OSSL_CMP, CTX, FIELD) \
554     DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, IS_NEG)
555 #define DEFINE_SET_PORT_TEST(FIELD) \
556     static int OSSL_CMP_CTX_get_##FIELD(const CMP_CTX *ctx) \
557     { \
558         return ctx == NULL ? ERR(-1) : ctx->FIELD; \
559     } \
560     DEFINE_SET_GET_INT_TEST_DEFAULT(OSSL_CMP, CTX, FIELD, IS_DEFAULT_PORT)
561
562 #define DEFINE_SET_GET_ARG_FN(SETN, GETN, FIELD, ARG, T) \
563     static int OSSL_CMP_CTX_##SETN##_##FIELD##_##ARG(CMP_CTX *ctx, T val) \
564     { \
565         return OSSL_CMP_CTX_##SETN##_##FIELD(ctx, ARG, val); \
566     } \
567     \
568     static T OSSL_CMP_CTX_##GETN##_##FIELD##_##ARG(const CMP_CTX *ctx) \
569     { \
570         return OSSL_CMP_CTX_##GETN##_##FIELD(ctx, ARG); \
571     }
572
573 #define DEFINE_SET_GET1_STR_FN(SETN, FIELD) \
574     static int OSSL_CMP_CTX_##SETN##_##FIELD##_str(CMP_CTX *ctx, char *val)\
575     { \
576         return OSSL_CMP_CTX_##SETN##_##FIELD(ctx, (unsigned char *)val, \
577                                             strlen(val)); \
578     } \
579     \
580     static char *OSSL_CMP_CTX_get1_##FIELD##_str(const CMP_CTX *ctx) \
581     { \
582         const ASN1_OCTET_STRING *bytes = ctx == NULL ? ERR(NULL) : ctx->FIELD; \
583         \
584         return bytes == NULL ? NULL : \
585             OPENSSL_strndup((char *)bytes->data, bytes->length); \
586     }
587
588 #define push 0
589 #define push0 0
590 #define push1 1
591 #define DEFINE_PUSH_BASE_TEST(PUSHN, DUP, FIELD, ELEM, TYPE, T, \
592                                DEFAULT, NEW, FREE) \
593 static TYPE sk_top_##FIELD(const CMP_CTX *ctx) { \
594     return sk_##T##_value(ctx->FIELD, sk_##T##_num(ctx->FIELD) - 1); \
595 } \
596 \
597 static int execute_CTX_##PUSHN##_##ELEM(OSSL_CMP_CTX_TEST_FIXTURE *fixture) \
598 { \
599     CMP_CTX *ctx = fixture->ctx; \
600     int (*push_fn)(CMP_CTX *ctx, TYPE) = \
601         (int (*)(CMP_CTX *ctx, TYPE))OSSL_CMP_CTX_##PUSHN##_##ELEM; \
602  /* need type cast in above assignment because TYPE arg sometimes is const */ \
603     int n_elem = sk_##T##_num(ctx->FIELD); \
604     STACK_OF(TYPE) field_read; \
605     TYPE val1_to_free = NEW; \
606     TYPE val1 = val1_to_free; \
607     TYPE val1_read = 0; /* 0 works for any type */ \
608     TYPE val2_to_free = NEW; \
609     TYPE val2 = val2_to_free; \
610     TYPE val2_read = 0; \
611     int res = 1; \
612     \
613     if (!TEST_int_eq(ERR_peek_error(), 0)) \
614         res = 0; \
615     if ((*push_fn)(NULL, val1) || ERR_peek_error() == 0) { \
616         TEST_error("pusher did not return error on ctx == NULL"); \
617         res = 0; \
618     } \
619     ERR_clear_error(); \
620     \
621     if (n_elem < 0) /* can happen for NULL stack */ \
622         n_elem = 0; \
623     field_read = ctx->FIELD; \
624     if (!DEFAULT(field_read)) { \
625         TEST_error("did not get default value for stack field"); \
626         res = 0; \
627     } \
628     if (!(*push_fn)(ctx, val1)) { \
629         TEST_error("pushing first value failed"); \
630         res = 0; \
631     } \
632     if (PUSHN == 0) \
633         val1_to_free = 0; /* 0 works for any type */ \
634     \
635     if (sk_##T##_num(ctx->FIELD) != ++n_elem) { \
636         TEST_error("pushing first value did not increment number"); \
637         res = 0; \
638     } \
639     val1_read = sk_top_##FIELD(ctx); \
640     if (PUSHN == 0) { \
641         if (val1_read != val1) { \
642             TEST_error("push/sk_top first value did not match"); \
643             res = 0; \
644         } \
645     } else { \
646         if (DUP && val1_read == val1) { \
647             TEST_error("first push did not dup the value"); \
648             res = 0; \
649         } \
650     } \
651     \
652     if (!(*push_fn)(ctx, val2)) { \
653         TEST_error("pushting second value failed"); \
654         res = 0; \
655     } \
656     if (PUSHN == 0) \
657         val2_to_free = 0; \
658     \
659     if (sk_##T##_num(ctx->FIELD) != ++n_elem) { \
660         TEST_error("pushing second value did not increment number"); \
661         res = 0; \
662     } \
663     val2_read = sk_top_##FIELD(ctx); \
664     if (PUSHN == 0) { \
665         if (val2_read != val2) { \
666             TEST_error("push/sk_top second value did not match"); \
667             res = 0; \
668         } \
669     } else { \
670         if (DUP && val2_read == val2) { \
671             TEST_error("second push did not dup the value"); \
672             res = 0; \
673         } \
674         if (val2 == val1) { \
675             TEST_error("second value is same as first value"); \
676             res = 0; \
677         } \
678     } \
679     /* this does not check that all remaining fields and elems are untouched */\
680     \
681     if (!TEST_int_eq(ERR_peek_error(), 0)) \
682         res = 0; \
683     \
684     FREE(val1_to_free); \
685     FREE(val2_to_free); \
686     return TEST_true(res); \
687 } \
688 \
689 static int test_CTX_##PUSHN##_##ELEM(void) \
690 { \
691     SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up); \
692     EXECUTE_TEST(execute_CTX_##PUSHN##_##ELEM, tear_down); \
693     return result; \
694 } \
695
696 #define DEFINE_PUSH_TEST(N, DUP, FIELD, ELEM, TYPE) \
697     DEFINE_PUSH_BASE_TEST(push##N, DUP, FIELD, ELEM, TYPE*, TYPE, \
698                            IS_0, TYPE##_new(), TYPE##_free)
699
700 void cleanup_tests(void)
701 {
702     return;
703 }
704
705 DEFINE_SET_GET_ARG_FN(set, get, option, 16, int)
706                                     /* option == OSSL_CMP_OPT_IGNORE_KEYUSAGE */
707 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set, get, 0, option_16, int, -1, IS_0, \
708                           1 /* true */, DROP)
709
710 #ifndef OPENSSL_NO_TRACE
711 DEFINE_SET_CB_TEST(log_cb)
712 #endif
713
714 DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, serverPath, char, IS_0)
715 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, serverName, char)
716 DEFINE_SET_PORT_TEST(serverPort)
717 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, proxyName, char)
718 DEFINE_SET_PORT_TEST(proxyPort)
719 DEFINE_SET_CB_TEST(http_cb)
720 DEFINE_SET_GET_P_VOID_TEST(http_cb_arg)
721 DEFINE_SET_CB_TEST(transfer_cb)
722 DEFINE_SET_GET_P_VOID_TEST(transfer_cb_arg)
723
724 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, srvCert, X509)
725 DEFINE_SET_TEST(ossl_cmp, ctx, 0, 0, validatedSrvCert, X509)
726 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, expected_sender, X509_NAME)
727 DEFINE_SET_GET_BASE_TEST(OSSL_CMP_CTX, set0, get0, 0, trustedStore,
728                           X509_STORE*, NULL,
729                           DEFAULT_STORE, X509_STORE_new_1(), X509_STORE_free)
730 DEFINE_SET_GET_SK_X509_TEST(OSSL_CMP, CTX, 1, 0, untrusted_certs)
731
732 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, clCert, X509)
733 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, pkey, EVP_PKEY)
734
735 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, recipient, X509_NAME)
736 DEFINE_PUSH_TEST(0, 0, geninfo_ITAVs, geninfo_ITAV, OSSL_CMP_ITAV)
737 DEFINE_SET_SK_TEST(OSSL_CMP, CTX, 1, extraCertsOut, X509)
738 DEFINE_SET_GET_ARG_FN(set0, get0, newPkey, 1, EVP_PKEY*) /* priv == 1 */
739 DEFINE_SET_GET_TEST(OSSL_CMP, CTX, 0, 0, 0, newPkey_1, EVP_PKEY)
740 DEFINE_SET_GET_ARG_FN(set0, get0, newPkey, 0, EVP_PKEY*) /* priv == 0 */
741 DEFINE_SET_GET_TEST(OSSL_CMP, CTX, 0, 0, 0, newPkey_0, EVP_PKEY)
742 DEFINE_SET_GET1_STR_FN(set1, referenceValue)
743 DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, 1, referenceValue_str,
744                              char, IS_0)
745 DEFINE_SET_GET1_STR_FN(set1, secretValue)
746 DEFINE_SET_GET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, 1, secretValue_str,
747                              char, IS_0)
748 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, issuer, X509_NAME)
749 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, subjectName, X509_NAME)
750 #ifdef ISSUE_9504_RESOLVED
751 DEFINE_PUSH_TEST(1, 1, subjectAltNames, subjectAltName, GENERAL_NAME)
752 #endif
753 DEFINE_SET_SK_TEST(OSSL_CMP, CTX, 0, reqExtensions, X509_EXTENSION)
754 DEFINE_PUSH_TEST(0, 0, policies, policy, POLICYINFO)
755 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 0, oldCert, X509)
756 #ifdef ISSUE_9504_RESOLVED
757 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, p10CSR, X509_REQ)
758 #endif
759 DEFINE_PUSH_TEST(0, 0, genm_ITAVs, genm_ITAV, OSSL_CMP_ITAV)
760 DEFINE_SET_CB_TEST(certConf_cb)
761 DEFINE_SET_GET_P_VOID_TEST(certConf_cb_arg)
762
763 DEFINE_SET_GET_INT_TEST(ossl_cmp, ctx, status)
764 DEFINE_SET_GET_SK_TEST(ossl_cmp, ctx, 0, 0, statusString, ASN1_UTF8STRING)
765 DEFINE_SET_GET_INT_TEST(ossl_cmp, ctx, failInfoCode)
766 DEFINE_SET_GET_TEST(ossl_cmp, ctx, 0, 0, 0, newCert, X509)
767 DEFINE_SET_GET_SK_X509_TEST(ossl_cmp, ctx, 1, 1, caPubs)
768 DEFINE_SET_GET_SK_X509_TEST(ossl_cmp, ctx, 1, 1, extraCertsIn)
769
770 DEFINE_SET_TEST_DEFAULT(OSSL_CMP, CTX, 1, 1, transactionID,
771                          ASN1_OCTET_STRING, IS_0)
772 DEFINE_SET_TEST(OSSL_CMP, CTX, 1, 1, senderNonce, ASN1_OCTET_STRING)
773 DEFINE_SET_TEST(ossl_cmp, ctx, 1, 1, recipNonce, ASN1_OCTET_STRING)
774
775 int setup_tests(void)
776 {
777     /* OSSL_CMP_CTX_new() is tested by set_up() */
778     /* OSSL_CMP_CTX_free() is tested by tear_down() */
779     ADD_TEST(test_CTX_reinit);
780
781 /* various CMP options: */
782     ADD_TEST(test_CTX_set_get_option_16);
783 /* CMP-specific callback for logging and outputting the error queue: */
784 #ifndef OPENSSL_NO_TRACE
785     ADD_TEST(test_CTX_set_get_log_cb);
786 #endif
787     /*
788      * also tests OSSL_CMP_log_open(), OSSL_CMP_CTX_set_log_verbosity(),
789      * OSSL_CMP_err(), OSSL_CMP_warn(), * OSSL_CMP_debug(),
790      * OSSL_CMP_log2(), ossl_cmp_log_parse_metadata(), and OSSL_CMP_log_close()
791      * with OSSL_CMP_severity OSSL_CMP_LOG_ERR/WARNING/DEBUG/INFO:
792      */
793     ADD_TEST(test_cmp_ctx_log_cb);
794 #if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
795     /* also tests OSSL_CMP_CTX_set_log_cb(), OSSL_CMP_print_errors_cb(),
796        ossl_cmp_add_error_txt(), and the macros
797        ossl_cmp_add_error_data and ossl_cmp_add_error_line:
798     */
799     ADD_TEST(test_CTX_print_errors);
800 #endif
801 /* message transfer: */
802     ADD_TEST(test_CTX_set1_get0_serverPath);
803     ADD_TEST(test_CTX_set1_get0_serverName);
804     ADD_TEST(test_CTX_set_get_serverPort);
805     ADD_TEST(test_CTX_set1_get0_proxyName);
806     ADD_TEST(test_CTX_set_get_proxyPort);
807     ADD_TEST(test_CTX_set_get_http_cb);
808     ADD_TEST(test_CTX_set_get_http_cb_arg);
809     ADD_TEST(test_CTX_set_get_transfer_cb);
810     ADD_TEST(test_CTX_set_get_transfer_cb_arg);
811 /* server authentication: */
812     ADD_TEST(test_CTX_set1_get0_srvCert);
813     ADD_TEST(test_CTX_set0_get0_validatedSrvCert);
814     ADD_TEST(test_CTX_set1_get0_expected_sender);
815     ADD_TEST(test_CTX_set0_get0_trustedStore);
816     ADD_TEST(test_CTX_set1_get0_untrusted_certs);
817 /* client authentication: */
818     ADD_TEST(test_CTX_set1_get0_clCert);
819     ADD_TEST(test_CTX_set1_get0_pkey);
820     /* the following two also test ossl_cmp_asn1_octet_string_set1_bytes(): */
821     ADD_TEST(test_CTX_set1_get1_referenceValue_str);
822     ADD_TEST(test_CTX_set1_get1_secretValue_str);
823 /* CMP message header and extra certificates: */
824     ADD_TEST(test_CTX_set1_get0_recipient);
825     ADD_TEST(test_CTX_push0_geninfo_ITAV);
826     ADD_TEST(test_CTX_set1_get0_extraCertsOut);
827 /* certificate template: */
828     ADD_TEST(test_CTX_set0_get0_newPkey_1);
829     ADD_TEST(test_CTX_set0_get0_newPkey_0);
830     ADD_TEST(test_CTX_set1_get0_issuer);
831     ADD_TEST(test_CTX_set1_get0_subjectName);
832 #ifdef ISSUE_9504_RESOLVED
833 /* test currently fails, see https://github.com/openssl/openssl/issues/9504 */
834     ADD_TEST(test_CTX_push1_subjectAltName);
835 #endif
836     ADD_TEST(test_CTX_set0_get0_reqExtensions);
837     ADD_TEST(test_CTX_reqExtensions_have_SAN);
838     ADD_TEST(test_CTX_push0_policy);
839     ADD_TEST(test_CTX_set1_get0_oldCert);
840 #ifdef ISSUE_9504_RESOLVED
841 /* test currently fails, see https://github.com/openssl/openssl/issues/9504 */
842     ADD_TEST(test_CTX_set1_get0_p10CSR);
843 #endif
844 /* misc body contents: */
845     ADD_TEST(test_CTX_push0_genm_ITAV);
846 /* certificate confirmation: */
847     ADD_TEST(test_CTX_set_get_certConf_cb);
848     ADD_TEST(test_CTX_set_get_certConf_cb_arg);
849 /* result fetching: */
850     ADD_TEST(test_CTX_set_get_status);
851     ADD_TEST(test_CTX_set0_get0_statusString);
852     ADD_TEST(test_CTX_set_get_failInfoCode);
853     ADD_TEST(test_CTX_set0_get0_newCert);
854     ADD_TEST(test_CTX_set1_get1_caPubs);
855     ADD_TEST(test_CTX_set1_get1_extraCertsIn);
856 /* exported for testing and debugging purposes: */
857     /* the following three also test ossl_cmp_asn1_octet_string_set1(): */
858     ADD_TEST(test_CTX_set1_get0_transactionID);
859     ADD_TEST(test_CTX_set1_get0_senderNonce);
860     ADD_TEST(test_CTX_set1_get0_recipNonce);
861
862     /* TODO ossl_cmp_build_cert_chain() will be tested with cmp_protect.c*/
863
864     return 1;
865 }