cmp_ctx_test.c 30 KB

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