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