1
0

cmp_vfy_test.c 24 KB

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