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