cmp_vfy_test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "helpers/cmp_testlib.h"
  12. #include "../crypto/crmf/crmf_local.h" /* for manipulating POPO signature */
  13. static const char *server_f;
  14. static const char *client_f;
  15. static const char *endentity1_f;
  16. static const char *endentity2_f;
  17. static const char *root_f;
  18. static const char *intermediate_f;
  19. static const char *ir_protected_f;
  20. static const char *ir_unprotected_f;
  21. static const char *ir_rmprotection_f;
  22. static const char *ip_waiting_f;
  23. static const char *instacert_f;
  24. static const char *instaca_f;
  25. static const char *ir_protected_0_extracerts;
  26. static const char *ir_protected_2_extracerts;
  27. typedef struct test_fixture {
  28. const char *test_case_name;
  29. int expected;
  30. OSSL_CMP_CTX *cmp_ctx;
  31. OSSL_CMP_MSG *msg;
  32. X509 *cert;
  33. ossl_cmp_allow_unprotected_cb_t allow_unprotected_cb;
  34. int additional_arg;
  35. } CMP_VFY_TEST_FIXTURE;
  36. static OSSL_LIB_CTX *libctx = NULL;
  37. static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
  38. static void tear_down(CMP_VFY_TEST_FIXTURE *fixture)
  39. {
  40. OSSL_CMP_MSG_free(fixture->msg);
  41. OSSL_CMP_CTX_free(fixture->cmp_ctx);
  42. OPENSSL_free(fixture);
  43. }
  44. static time_t test_time_valid = 0, test_time_after_expiration = 0;
  45. static CMP_VFY_TEST_FIXTURE *set_up(const char *const test_case_name)
  46. {
  47. X509_STORE *ts = X509_STORE_new();
  48. CMP_VFY_TEST_FIXTURE *fixture;
  49. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  50. return NULL;
  51. fixture->test_case_name = test_case_name;
  52. if (ts == NULL
  53. || !TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))
  54. || !OSSL_CMP_CTX_set0_trustedStore(fixture->cmp_ctx, ts)
  55. || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)) {
  56. tear_down(fixture);
  57. X509_STORE_free(ts);
  58. return NULL;
  59. }
  60. X509_VERIFY_PARAM_set_time(X509_STORE_get0_param(ts), test_time_valid);
  61. X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb);
  62. return fixture;
  63. }
  64. static X509 *srvcert = NULL;
  65. static X509 *clcert = NULL;
  66. /* chain */
  67. static X509 *endentity1 = NULL, *endentity2 = NULL,
  68. *intermediate = NULL, *root = NULL;
  69. /* INSTA chain */
  70. static X509 *insta_cert = NULL, *instaca_cert = NULL;
  71. static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
  72. static OSSL_CMP_MSG *ir_unprotected, *ir_rmprotection;
  73. static int flip_bit(ASN1_BIT_STRING *bitstr)
  74. {
  75. int bit_num = 7;
  76. int bit = ASN1_BIT_STRING_get_bit(bitstr, bit_num);
  77. return ASN1_BIT_STRING_set_bit(bitstr, bit_num, !bit);
  78. }
  79. static int execute_verify_popo_test(CMP_VFY_TEST_FIXTURE *fixture)
  80. {
  81. if ((fixture->msg = load_pkimsg(ir_protected_f)) == NULL)
  82. return 0;
  83. if (fixture->expected == 0) {
  84. const OSSL_CRMF_MSGS *reqs = fixture->msg->body->value.ir;
  85. const OSSL_CRMF_MSG *req = sk_OSSL_CRMF_MSG_value(reqs, 0);
  86. if (req == NULL || !flip_bit(req->popo->value.signature->signature))
  87. return 0;
  88. }
  89. return TEST_int_eq(fixture->expected,
  90. ossl_cmp_verify_popo(fixture->cmp_ctx, fixture->msg,
  91. fixture->additional_arg));
  92. }
  93. static int test_verify_popo(void)
  94. {
  95. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  96. fixture->expected = 1;
  97. EXECUTE_TEST(execute_verify_popo_test, tear_down);
  98. return result;
  99. }
  100. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  101. static int test_verify_popo_bad(void)
  102. {
  103. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  104. fixture->expected = 0;
  105. EXECUTE_TEST(execute_verify_popo_test, tear_down);
  106. return result;
  107. }
  108. #endif
  109. static int execute_validate_msg_test(CMP_VFY_TEST_FIXTURE *fixture)
  110. {
  111. return TEST_int_eq(fixture->expected,
  112. ossl_cmp_msg_check_update(fixture->cmp_ctx, fixture->msg,
  113. NULL, 0));
  114. }
  115. static int execute_validate_cert_path_test(CMP_VFY_TEST_FIXTURE *fixture)
  116. {
  117. X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore(fixture->cmp_ctx);
  118. int res = TEST_int_eq(fixture->expected,
  119. OSSL_CMP_validate_cert_path(fixture->cmp_ctx,
  120. ts, fixture->cert));
  121. OSSL_CMP_CTX_print_errors(fixture->cmp_ctx);
  122. return res;
  123. }
  124. static int test_validate_msg_mac_alg_protection(void)
  125. {
  126. /* secret value belonging to cmp-test/CMP_IP_waitingStatus_PBM.der */
  127. const unsigned char sec_1[] = {
  128. '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3',
  129. 'Q', '-', 'u', 'd', 'N', 'R'
  130. };
  131. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  132. fixture->expected = 1;
  133. if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_1,
  134. sizeof(sec_1)))
  135. || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f))) {
  136. tear_down(fixture);
  137. fixture = NULL;
  138. }
  139. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  140. return result;
  141. }
  142. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  143. static int test_validate_msg_mac_alg_protection_bad(void)
  144. {
  145. const unsigned char sec_bad[] = {
  146. '9', 'p', 'p', '8', '-', 'b', '3', '5', 'i', '-', 'X', 'd', '3',
  147. 'Q', '-', 'u', 'd', 'N', 'r'
  148. };
  149. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  150. fixture->expected = 0;
  151. if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx, sec_bad,
  152. sizeof(sec_bad)))
  153. || !TEST_ptr(fixture->msg = load_pkimsg(ip_waiting_f))) {
  154. tear_down(fixture);
  155. fixture = NULL;
  156. }
  157. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  158. return result;
  159. }
  160. #endif
  161. static int add_trusted(OSSL_CMP_CTX *ctx, X509 *cert)
  162. {
  163. return X509_STORE_add_cert(OSSL_CMP_CTX_get0_trustedStore(ctx), cert);
  164. }
  165. static int add_untrusted(OSSL_CMP_CTX *ctx, X509 *cert)
  166. {
  167. return X509_add_cert(OSSL_CMP_CTX_get0_untrusted(ctx), cert,
  168. X509_ADD_FLAG_UP_REF);
  169. }
  170. static int test_validate_msg_signature_partial_chain(int expired)
  171. {
  172. X509_STORE *ts;
  173. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  174. ts = OSSL_CMP_CTX_get0_trustedStore(fixture->cmp_ctx);
  175. fixture->expected = !expired;
  176. if (ts == NULL
  177. || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
  178. || !add_trusted(fixture->cmp_ctx, srvcert)) {
  179. tear_down(fixture);
  180. fixture = NULL;
  181. } else {
  182. X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
  183. X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_PARTIAL_CHAIN);
  184. if (expired)
  185. X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration);
  186. }
  187. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  188. return result;
  189. }
  190. static int test_validate_msg_signature_trusted_ok(void)
  191. {
  192. return test_validate_msg_signature_partial_chain(0);
  193. }
  194. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  195. static int test_validate_msg_signature_trusted_expired(void)
  196. {
  197. return test_validate_msg_signature_partial_chain(1);
  198. }
  199. #endif
  200. static int test_validate_msg_signature_srvcert_wrong(void)
  201. {
  202. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  203. fixture->expected = 0;
  204. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
  205. || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, clcert))) {
  206. tear_down(fixture);
  207. fixture = NULL;
  208. }
  209. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  210. return result;
  211. }
  212. static int test_validate_msg_signature_srvcert(int bad_sig)
  213. {
  214. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  215. fixture->expected = !bad_sig;
  216. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
  217. || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, srvcert))
  218. || (bad_sig && !flip_bit(fixture->msg->protection))) {
  219. tear_down(fixture);
  220. fixture = NULL;
  221. }
  222. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  223. return result;
  224. }
  225. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  226. static int test_validate_msg_signature_bad(void)
  227. {
  228. return test_validate_msg_signature_srvcert(1);
  229. }
  230. #endif
  231. static int test_validate_msg_signature_sender_cert_srvcert(void)
  232. {
  233. return test_validate_msg_signature_srvcert(0);
  234. }
  235. static int test_validate_msg_signature_sender_cert_untrusted(void)
  236. {
  237. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  238. fixture->expected = 1;
  239. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts))
  240. || !add_trusted(fixture->cmp_ctx, instaca_cert)
  241. || !add_untrusted(fixture->cmp_ctx, insta_cert)) {
  242. tear_down(fixture);
  243. fixture = NULL;
  244. }
  245. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  246. return result;
  247. }
  248. static int test_validate_msg_signature_sender_cert_trusted(void)
  249. {
  250. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  251. fixture->expected = 1;
  252. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts))
  253. || !add_trusted(fixture->cmp_ctx, instaca_cert)
  254. || !add_trusted(fixture->cmp_ctx, insta_cert)) {
  255. tear_down(fixture);
  256. fixture = NULL;
  257. }
  258. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  259. return result;
  260. }
  261. static int test_validate_msg_signature_sender_cert_extracert(void)
  262. {
  263. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  264. fixture->expected = 1;
  265. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_2_extracerts))
  266. || !add_trusted(fixture->cmp_ctx, instaca_cert)) {
  267. tear_down(fixture);
  268. fixture = NULL;
  269. }
  270. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  271. return result;
  272. }
  273. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  274. static int test_validate_msg_signature_sender_cert_absent(void)
  275. {
  276. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  277. fixture->expected = 0;
  278. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_0_extracerts))) {
  279. tear_down(fixture);
  280. fixture = NULL;
  281. }
  282. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  283. return result;
  284. }
  285. #endif
  286. static int test_validate_with_sender(const X509_NAME *name, int expected)
  287. {
  288. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  289. fixture->expected = expected;
  290. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f))
  291. || !TEST_true(OSSL_CMP_CTX_set1_expected_sender(fixture->cmp_ctx, name))
  292. || !TEST_true(OSSL_CMP_CTX_set1_srvCert(fixture->cmp_ctx, srvcert))) {
  293. tear_down(fixture);
  294. fixture = NULL;
  295. }
  296. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  297. return result;
  298. }
  299. static int test_validate_msg_signature_expected_sender(void)
  300. {
  301. return test_validate_with_sender(X509_get_subject_name(srvcert), 1);
  302. }
  303. static int test_validate_msg_signature_unexpected_sender(void)
  304. {
  305. return test_validate_with_sender(X509_get_subject_name(root), 0);
  306. }
  307. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  308. static int test_validate_msg_unprotected_request(void)
  309. {
  310. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  311. fixture->expected = 0;
  312. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f))) {
  313. tear_down(fixture);
  314. fixture = NULL;
  315. }
  316. EXECUTE_TEST(execute_validate_msg_test, tear_down);
  317. return result;
  318. }
  319. #endif
  320. static void setup_path(CMP_VFY_TEST_FIXTURE **fixture, X509 *wrong, int expired)
  321. {
  322. (*fixture)->cert = endentity2;
  323. (*fixture)->expected = wrong == NULL && !expired;
  324. if (expired) {
  325. X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore((*fixture)->cmp_ctx);
  326. X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
  327. X509_VERIFY_PARAM_set_time(vpm, test_time_after_expiration);
  328. }
  329. if (!add_trusted((*fixture)->cmp_ctx, wrong == NULL ? root : wrong)
  330. || !add_untrusted((*fixture)->cmp_ctx, endentity1)
  331. || !add_untrusted((*fixture)->cmp_ctx, intermediate)) {
  332. tear_down((*fixture));
  333. (*fixture) = NULL;
  334. }
  335. }
  336. static int test_validate_cert_path_ok(void)
  337. {
  338. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  339. setup_path(&fixture, NULL, 0);
  340. EXECUTE_TEST(execute_validate_cert_path_test, tear_down);
  341. return result;
  342. }
  343. static int test_validate_cert_path_wrong_anchor(void)
  344. {
  345. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  346. setup_path(&fixture, srvcert /* wrong/non-root cert */, 0);
  347. EXECUTE_TEST(execute_validate_cert_path_test, tear_down);
  348. return result;
  349. }
  350. static int test_validate_cert_path_expired(void)
  351. {
  352. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  353. setup_path(&fixture, NULL, 1);
  354. EXECUTE_TEST(execute_validate_cert_path_test, tear_down);
  355. return result;
  356. }
  357. static int execute_msg_check_test(CMP_VFY_TEST_FIXTURE *fixture)
  358. {
  359. const OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(fixture->msg);
  360. const ASN1_OCTET_STRING *tid = OSSL_CMP_HDR_get0_transactionID(hdr);
  361. if (!TEST_int_eq(fixture->expected,
  362. ossl_cmp_msg_check_update(fixture->cmp_ctx,
  363. fixture->msg,
  364. fixture->allow_unprotected_cb,
  365. fixture->additional_arg)))
  366. return 0;
  367. if (fixture->expected == 0) /* error expected aready during above check */
  368. return 1;
  369. return
  370. TEST_int_eq(0,
  371. ASN1_OCTET_STRING_cmp(ossl_cmp_hdr_get0_senderNonce(hdr),
  372. fixture->cmp_ctx->recipNonce))
  373. && TEST_int_eq(0,
  374. ASN1_OCTET_STRING_cmp(tid,
  375. fixture->cmp_ctx->transactionID));
  376. }
  377. static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg,
  378. int invalid_protection, int allow)
  379. {
  380. return allow;
  381. }
  382. static void setup_check_update(CMP_VFY_TEST_FIXTURE **fixture, int expected,
  383. ossl_cmp_allow_unprotected_cb_t cb, int arg,
  384. const unsigned char *trid_data,
  385. const unsigned char *nonce_data)
  386. {
  387. OSSL_CMP_CTX *ctx = (*fixture)->cmp_ctx;
  388. int nonce_len = OSSL_CMP_SENDERNONCE_LENGTH;
  389. (*fixture)->expected = expected;
  390. (*fixture)->allow_unprotected_cb = cb;
  391. (*fixture)->additional_arg = arg;
  392. (*fixture)->msg = OSSL_CMP_MSG_dup(ir_rmprotection);
  393. if ((*fixture)->msg == NULL
  394. || (nonce_data != NULL
  395. && !ossl_cmp_asn1_octet_string_set1_bytes(&ctx->senderNonce,
  396. nonce_data, nonce_len))) {
  397. tear_down((*fixture));
  398. (*fixture) = NULL;
  399. } else if (trid_data != NULL) {
  400. ASN1_OCTET_STRING *trid = ASN1_OCTET_STRING_new();
  401. if (trid == NULL
  402. || !ASN1_OCTET_STRING_set(trid, trid_data,
  403. OSSL_CMP_TRANSACTIONID_LENGTH)
  404. || !OSSL_CMP_CTX_set1_transactionID(ctx, trid)) {
  405. tear_down((*fixture));
  406. (*fixture) = NULL;
  407. }
  408. ASN1_OCTET_STRING_free(trid);
  409. }
  410. }
  411. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  412. static int test_msg_check_no_protection_no_cb(void)
  413. {
  414. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  415. setup_check_update(&fixture, 0, NULL, 0, NULL, NULL);
  416. EXECUTE_TEST(execute_msg_check_test, tear_down);
  417. return result;
  418. }
  419. static int test_msg_check_no_protection_restrictive_cb(void)
  420. {
  421. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  422. setup_check_update(&fixture, 0, allow_unprotected, 0, NULL, NULL);
  423. EXECUTE_TEST(execute_msg_check_test, tear_down);
  424. return result;
  425. }
  426. #endif
  427. static int test_msg_check_no_protection_permissive_cb(void)
  428. {
  429. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  430. setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, NULL);
  431. EXECUTE_TEST(execute_msg_check_test, tear_down);
  432. return result;
  433. }
  434. static int test_msg_check_transaction_id(void)
  435. {
  436. /* Transaction id belonging to CMP_IR_rmprotection.der */
  437. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
  438. 0x39, 0xB6, 0x90, 0x28, 0xC4, 0xBC, 0x7A, 0xF6,
  439. 0xBE, 0xC6, 0x4A, 0x88, 0x97, 0xA6, 0x95, 0x0B
  440. };
  441. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  442. setup_check_update(&fixture, 1, allow_unprotected, 1, trans_id, NULL);
  443. EXECUTE_TEST(execute_msg_check_test, tear_down);
  444. return result;
  445. }
  446. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  447. static int test_msg_check_transaction_id_bad(void)
  448. {
  449. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  450. setup_check_update(&fixture, 0, allow_unprotected, 1, rand_data, NULL);
  451. EXECUTE_TEST(execute_msg_check_test, tear_down);
  452. return result;
  453. }
  454. #endif
  455. static int test_msg_check_recipient_nonce(void)
  456. {
  457. /* Recipient nonce belonging to CMP_IP_ir_rmprotection.der */
  458. const unsigned char rec_nonce[OSSL_CMP_SENDERNONCE_LENGTH] = {
  459. 0x48, 0xF1, 0x71, 0x1F, 0xE5, 0xAF, 0x1C, 0x8B,
  460. 0x21, 0x97, 0x5C, 0x84, 0x74, 0x49, 0xBA, 0x32
  461. };
  462. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  463. setup_check_update(&fixture, 1, allow_unprotected, 1, NULL, rec_nonce);
  464. EXECUTE_TEST(execute_msg_check_test, tear_down);
  465. return result;
  466. }
  467. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  468. static int test_msg_check_recipient_nonce_bad(void)
  469. {
  470. SETUP_TEST_FIXTURE(CMP_VFY_TEST_FIXTURE, set_up);
  471. setup_check_update(&fixture, 0, allow_unprotected, 1, NULL, rand_data);
  472. EXECUTE_TEST(execute_msg_check_test, tear_down);
  473. return result;
  474. }
  475. #endif
  476. void cleanup_tests(void)
  477. {
  478. X509_free(srvcert);
  479. X509_free(clcert);
  480. X509_free(endentity1);
  481. X509_free(endentity2);
  482. X509_free(intermediate);
  483. X509_free(root);
  484. X509_free(insta_cert);
  485. X509_free(instaca_cert);
  486. OSSL_CMP_MSG_free(ir_unprotected);
  487. OSSL_CMP_MSG_free(ir_rmprotection);
  488. OSSL_LIB_CTX_free(libctx);
  489. return;
  490. }
  491. #define USAGE "server.crt client.crt " \
  492. "EndEntity1.crt EndEntity2.crt " \
  493. "Root_CA.crt Intermediate_CA.crt " \
  494. "CMP_IR_protected.der CMP_IR_unprotected.der " \
  495. "IP_waitingStatus_PBM.der IR_rmprotection.der " \
  496. "insta.cert.pem insta_ca.cert.pem " \
  497. "IR_protected_0_extraCerts.der " \
  498. "IR_protected_2_extraCerts.der module_name [module_conf_file]\n"
  499. OPT_TEST_DECLARE_USAGE(USAGE)
  500. int setup_tests(void)
  501. {
  502. /* Set test time stamps */
  503. struct tm ts = { 0 };
  504. ts.tm_year = 2018 - 1900; /* 2018 */
  505. ts.tm_mon = 1; /* February */
  506. ts.tm_mday = 18; /* 18th */
  507. test_time_valid = mktime(&ts); /* February 18th 2018 */
  508. ts.tm_year += 10; /* February 18th 2028 */
  509. test_time_after_expiration = mktime(&ts);
  510. if (!test_skip_common_options()) {
  511. TEST_error("Error parsing test options\n");
  512. return 0;
  513. }
  514. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  515. if (!TEST_ptr(server_f = test_get_argument(0))
  516. || !TEST_ptr(client_f = test_get_argument(1))
  517. || !TEST_ptr(endentity1_f = test_get_argument(2))
  518. || !TEST_ptr(endentity2_f = test_get_argument(3))
  519. || !TEST_ptr(root_f = test_get_argument(4))
  520. || !TEST_ptr(intermediate_f = test_get_argument(5))
  521. || !TEST_ptr(ir_protected_f = test_get_argument(6))
  522. || !TEST_ptr(ir_unprotected_f = test_get_argument(7))
  523. || !TEST_ptr(ip_waiting_f = test_get_argument(8))
  524. || !TEST_ptr(ir_rmprotection_f = test_get_argument(9))
  525. || !TEST_ptr(instacert_f = test_get_argument(10))
  526. || !TEST_ptr(instaca_f = test_get_argument(11))
  527. || !TEST_ptr(ir_protected_0_extracerts = test_get_argument(12))
  528. || !TEST_ptr(ir_protected_2_extracerts = test_get_argument(13))) {
  529. TEST_error("usage: cmp_vfy_test %s", USAGE);
  530. return 0;
  531. }
  532. if (!test_get_libctx(&libctx, &default_null_provider, &provider, 14, USAGE))
  533. return 0;
  534. /* Load certificates for cert chain */
  535. if (!TEST_ptr(endentity1 = load_pem_cert(endentity1_f, libctx))
  536. || !TEST_ptr(endentity2 = load_pem_cert(endentity2_f, libctx))
  537. || !TEST_ptr(root = load_pem_cert(root_f, NULL))
  538. || !TEST_ptr(intermediate = load_pem_cert(intermediate_f, libctx)))
  539. goto err;
  540. if (!TEST_ptr(insta_cert = load_pem_cert(instacert_f, libctx))
  541. || !TEST_ptr(instaca_cert = load_pem_cert(instaca_f, libctx)))
  542. goto err;
  543. /* Load certificates for message validation */
  544. if (!TEST_ptr(srvcert = load_pem_cert(server_f, libctx))
  545. || !TEST_ptr(clcert = load_pem_cert(client_f, libctx)))
  546. goto err;
  547. if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
  548. goto err;
  549. if (!TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f))
  550. || !TEST_ptr(ir_rmprotection = load_pkimsg(ir_rmprotection_f)))
  551. goto err;
  552. /* Message validation tests */
  553. ADD_TEST(test_verify_popo);
  554. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  555. ADD_TEST(test_verify_popo_bad);
  556. #endif
  557. ADD_TEST(test_validate_msg_signature_trusted_ok);
  558. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  559. ADD_TEST(test_validate_msg_signature_trusted_expired);
  560. #endif
  561. ADD_TEST(test_validate_msg_signature_srvcert_wrong);
  562. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  563. ADD_TEST(test_validate_msg_signature_bad);
  564. #endif
  565. ADD_TEST(test_validate_msg_signature_sender_cert_srvcert);
  566. ADD_TEST(test_validate_msg_signature_sender_cert_untrusted);
  567. ADD_TEST(test_validate_msg_signature_sender_cert_trusted);
  568. ADD_TEST(test_validate_msg_signature_sender_cert_extracert);
  569. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  570. ADD_TEST(test_validate_msg_signature_sender_cert_absent);
  571. #endif
  572. ADD_TEST(test_validate_msg_signature_expected_sender);
  573. ADD_TEST(test_validate_msg_signature_unexpected_sender);
  574. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  575. ADD_TEST(test_validate_msg_unprotected_request);
  576. #endif
  577. ADD_TEST(test_validate_msg_mac_alg_protection);
  578. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  579. ADD_TEST(test_validate_msg_mac_alg_protection_bad);
  580. #endif
  581. /* Cert path validation tests */
  582. ADD_TEST(test_validate_cert_path_ok);
  583. ADD_TEST(test_validate_cert_path_expired);
  584. ADD_TEST(test_validate_cert_path_wrong_anchor);
  585. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  586. ADD_TEST(test_msg_check_no_protection_no_cb);
  587. ADD_TEST(test_msg_check_no_protection_restrictive_cb);
  588. #endif
  589. ADD_TEST(test_msg_check_no_protection_permissive_cb);
  590. ADD_TEST(test_msg_check_transaction_id);
  591. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  592. ADD_TEST(test_msg_check_transaction_id_bad);
  593. #endif
  594. ADD_TEST(test_msg_check_recipient_nonce);
  595. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  596. ADD_TEST(test_msg_check_recipient_nonce_bad);
  597. #endif
  598. return 1;
  599. err:
  600. cleanup_tests();
  601. return 0;
  602. }