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