cmp_ctx_test.c 30 KB

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