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