cmp_ctx_test.c 31 KB

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