2
0

cmp_ctx_test.c 30 KB

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