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