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