cmp_vfy_test.c 22 KB

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