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