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