cmp_ctx_test.c 30 KB

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