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