pkcs12.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "internal/nelem.h"
  13. #include <openssl/pkcs12.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/pem.h>
  17. #include "../testutil.h"
  18. #include "pkcs12.h" /* from the same directory */
  19. /* Set this to > 0 write test data to file */
  20. static int write_files = 0;
  21. static int legacy = 0;
  22. static OSSL_LIB_CTX *test_ctx = NULL;
  23. static const char *test_propq = NULL;
  24. /* -------------------------------------------------------------------------
  25. * Local function declarations
  26. */
  27. static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs);
  28. static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
  29. static int write_p12(PKCS12 *p12, const char *outfile);
  30. static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac);
  31. static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac);
  32. static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac);
  33. static int check_asn1_string(const ASN1_TYPE *av, const char *txt);
  34. static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs);
  35. /* --------------------------------------------------------------------------
  36. * Global settings
  37. */
  38. void PKCS12_helper_set_write_files(int enable)
  39. {
  40. write_files = enable;
  41. }
  42. void PKCS12_helper_set_legacy(int enable)
  43. {
  44. legacy = enable;
  45. }
  46. void PKCS12_helper_set_libctx(OSSL_LIB_CTX *libctx)
  47. {
  48. test_ctx = libctx;
  49. }
  50. void PKCS12_helper_set_propq(const char *propq)
  51. {
  52. test_propq = propq;
  53. }
  54. /* --------------------------------------------------------------------------
  55. * Test data load functions
  56. */
  57. static X509 *load_cert_asn1(const unsigned char *bytes, int len)
  58. {
  59. X509 *cert = NULL;
  60. cert = d2i_X509(NULL, &bytes, len);
  61. if (!TEST_ptr(cert))
  62. goto err;
  63. err:
  64. return cert;
  65. }
  66. static EVP_PKEY *load_pkey_asn1(const unsigned char *bytes, int len)
  67. {
  68. EVP_PKEY *pkey = NULL;
  69. pkey = d2i_AutoPrivateKey(NULL, &bytes, len);
  70. if (!TEST_ptr(pkey))
  71. goto err;
  72. err:
  73. return pkey;
  74. }
  75. /* -------------------------------------------------------------------------
  76. * PKCS12 builder
  77. */
  78. PKCS12_BUILDER *new_pkcs12_builder(const char *filename)
  79. {
  80. PKCS12_BUILDER *pb = OPENSSL_malloc(sizeof(PKCS12_BUILDER));
  81. if (!TEST_ptr(pb))
  82. return NULL;
  83. pb->filename = filename;
  84. pb->success = 1;
  85. return pb;
  86. }
  87. int end_pkcs12_builder(PKCS12_BUILDER *pb)
  88. {
  89. int result = pb->success;
  90. OPENSSL_free(pb);
  91. return result;
  92. }
  93. void start_pkcs12(PKCS12_BUILDER *pb)
  94. {
  95. pb->safes = NULL;
  96. }
  97. void end_pkcs12(PKCS12_BUILDER *pb)
  98. {
  99. if (!pb->success)
  100. return;
  101. generate_p12(pb, NULL);
  102. }
  103. void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
  104. {
  105. if (!pb->success)
  106. return;
  107. generate_p12(pb, mac);
  108. }
  109. /* Generate the PKCS12 encoding and write to memory bio */
  110. static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
  111. {
  112. PKCS12 *p12;
  113. EVP_MD *md = NULL;
  114. if (!pb->success)
  115. return;
  116. pb->p12bio = BIO_new(BIO_s_mem());
  117. if (!TEST_ptr(pb->p12bio)) {
  118. pb->success = 0;
  119. return;
  120. }
  121. if (legacy)
  122. p12 = PKCS12_add_safes(pb->safes, 0);
  123. else
  124. p12 = PKCS12_add_safes_ex(pb->safes, 0, test_ctx, test_propq);
  125. if (!TEST_ptr(p12)) {
  126. pb->success = 0;
  127. goto err;
  128. }
  129. sk_PKCS7_pop_free(pb->safes, PKCS7_free);
  130. if (mac != NULL) {
  131. if (legacy)
  132. md = (EVP_MD *)EVP_get_digestbynid(mac->nid);
  133. else
  134. md = EVP_MD_fetch(test_ctx, OBJ_nid2sn(mac->nid), test_propq);
  135. if (!TEST_true(PKCS12_set_mac(p12, mac->pass, strlen(mac->pass),
  136. NULL, 0, mac->iter, md))) {
  137. pb->success = 0;
  138. goto err;
  139. }
  140. }
  141. i2d_PKCS12_bio(pb->p12bio, p12);
  142. /* Can write to file here for debug */
  143. if (write_files)
  144. write_p12(p12, pb->filename);
  145. err:
  146. if (!legacy && md != NULL)
  147. EVP_MD_free(md);
  148. PKCS12_free(p12);
  149. }
  150. static int write_p12(PKCS12 *p12, const char *outfile)
  151. {
  152. int ret = 0;
  153. BIO *out = BIO_new_file(outfile, "w");
  154. if (out == NULL)
  155. goto err;
  156. if (!TEST_int_eq(i2d_PKCS12_bio(out, p12), 1))
  157. goto err;
  158. ret = 1;
  159. err:
  160. BIO_free(out);
  161. return ret;
  162. }
  163. static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac)
  164. {
  165. PKCS12 *p12 = NULL;
  166. /* Supply a p12 with library context/propq to the d2i decoder*/
  167. if (!legacy) {
  168. p12 = PKCS12_init_ex(NID_pkcs7_data, test_ctx, test_propq);
  169. if (!TEST_ptr(p12))
  170. goto err;
  171. }
  172. p12 = d2i_PKCS12_bio(bio, &p12);
  173. BIO_free(bio);
  174. if (!TEST_ptr(p12))
  175. goto err;
  176. if (mac == NULL) {
  177. if (!TEST_false(PKCS12_mac_present(p12)))
  178. goto err;
  179. } else {
  180. if (!check_p12_mac(p12, mac))
  181. goto err;
  182. }
  183. return p12;
  184. err:
  185. PKCS12_free(p12);
  186. return NULL;
  187. }
  188. /* For use with existing files */
  189. static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac)
  190. {
  191. PKCS12 *p12 = NULL;
  192. BIO *in = BIO_new_file(infile, "r");
  193. if (in == NULL)
  194. goto err;
  195. p12 = d2i_PKCS12_bio(in, NULL);
  196. BIO_free(in);
  197. if (!TEST_ptr(p12))
  198. goto err;
  199. if (mac == NULL) {
  200. if (!TEST_false(PKCS12_mac_present(p12)))
  201. goto err;
  202. } else {
  203. if (!check_p12_mac(p12, mac))
  204. goto err;
  205. }
  206. return p12;
  207. err:
  208. PKCS12_free(p12);
  209. return NULL;
  210. }
  211. static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac)
  212. {
  213. return TEST_true(PKCS12_mac_present(p12))
  214. && TEST_true(PKCS12_verify_mac(p12, mac->pass, strlen(mac->pass)));
  215. }
  216. /* -------------------------------------------------------------------------
  217. * PKCS7 content info builder
  218. */
  219. void start_contentinfo(PKCS12_BUILDER *pb)
  220. {
  221. pb->bags = NULL;
  222. }
  223. void end_contentinfo(PKCS12_BUILDER *pb)
  224. {
  225. if (pb->success && pb->bags != NULL) {
  226. if (!TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, -1, 0, NULL)))
  227. pb->success = 0;
  228. }
  229. sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
  230. pb->bags = NULL;
  231. }
  232. void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
  233. {
  234. if (pb->success && pb->bags != NULL) {
  235. if (legacy) {
  236. if (!TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, enc->nid,
  237. enc->iter, enc->pass)))
  238. pb->success = 0;
  239. } else {
  240. if (!TEST_true(PKCS12_add_safe_ex(&pb->safes, pb->bags, enc->nid,
  241. enc->iter, enc->pass, test_ctx,
  242. test_propq)))
  243. pb->success = 0;
  244. }
  245. }
  246. sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
  247. pb->bags = NULL;
  248. }
  249. static STACK_OF(PKCS12_SAFEBAG) *decode_contentinfo(STACK_OF(PKCS7) *safes, int idx, const PKCS12_ENC *enc)
  250. {
  251. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  252. int bagnid;
  253. PKCS7 *p7 = sk_PKCS7_value(safes, idx);
  254. if (!TEST_ptr(p7))
  255. goto err;
  256. bagnid = OBJ_obj2nid(p7->type);
  257. if (enc) {
  258. if (!TEST_int_eq(bagnid, NID_pkcs7_encrypted))
  259. goto err;
  260. bags = PKCS12_unpack_p7encdata(p7, enc->pass, strlen(enc->pass));
  261. } else {
  262. if (!TEST_int_eq(bagnid, NID_pkcs7_data))
  263. goto err;
  264. bags = PKCS12_unpack_p7data(p7);
  265. }
  266. if (!TEST_ptr(bags))
  267. goto err;
  268. return bags;
  269. err:
  270. return NULL;
  271. }
  272. /* -------------------------------------------------------------------------
  273. * PKCS12 safeBag/attribute builder
  274. */
  275. static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs)
  276. {
  277. int ret = 0;
  278. int attr_nid;
  279. const PKCS12_ATTR *p_attr = attrs;
  280. if (attrs == NULL)
  281. return 1;
  282. while (p_attr->oid != NULL) {
  283. TEST_info("Adding attribute %s = %s", p_attr->oid, p_attr->value);
  284. attr_nid = OBJ_txt2nid(p_attr->oid);
  285. if (attr_nid == NID_friendlyName) {
  286. if (!TEST_true(PKCS12_add_friendlyname(bag, p_attr->value, -1)))
  287. goto err;
  288. } else if (attr_nid == NID_localKeyID) {
  289. if (!TEST_true(PKCS12_add_localkeyid(bag, (unsigned char *)p_attr->value,
  290. strlen(p_attr->value))))
  291. goto err;
  292. } else {
  293. /* Custom attribute values limited to ASCII in these tests */
  294. if (!TEST_true(PKCS12_add1_attr_by_txt(bag, p_attr->oid, MBSTRING_ASC,
  295. (unsigned char *)p_attr->value,
  296. strlen(p_attr->value))))
  297. goto err;
  298. }
  299. p_attr++;
  300. }
  301. ret = 1;
  302. err:
  303. return ret;
  304. }
  305. void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  306. const PKCS12_ATTR *attrs)
  307. {
  308. PKCS12_SAFEBAG *bag = NULL;
  309. X509 *cert = NULL;
  310. char *name;
  311. if (!pb->success)
  312. return;
  313. cert = load_cert_asn1(bytes, len);
  314. if (!TEST_ptr(cert)) {
  315. pb->success = 0;
  316. return;
  317. }
  318. name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
  319. TEST_info("Adding certificate <%s>", name);
  320. OPENSSL_free(name);
  321. bag = PKCS12_add_cert(&pb->bags, cert);
  322. if (!TEST_ptr(bag)) {
  323. pb->success = 0;
  324. goto err;
  325. }
  326. if (!TEST_true(add_attributes(bag, attrs))) {
  327. pb->success = 0;
  328. goto err;
  329. }
  330. err:
  331. X509_free(cert);
  332. }
  333. void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  334. const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
  335. {
  336. PKCS12_SAFEBAG *bag = NULL;
  337. EVP_PKEY *pkey = NULL;
  338. if (!pb->success)
  339. return;
  340. TEST_info("Adding key");
  341. pkey = load_pkey_asn1(bytes, len);
  342. if (!TEST_ptr(pkey)) {
  343. pb->success = 0;
  344. return;
  345. }
  346. if (legacy)
  347. bag = PKCS12_add_key(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass);
  348. else
  349. bag = PKCS12_add_key_ex(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass,
  350. test_ctx, test_propq);
  351. if (!TEST_ptr(bag)) {
  352. pb->success = 0;
  353. goto err;
  354. }
  355. if (!add_attributes(bag, attrs))
  356. pb->success = 0;
  357. err:
  358. EVP_PKEY_free(pkey);
  359. }
  360. void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
  361. const PKCS12_ATTR *attrs)
  362. {
  363. PKCS12_SAFEBAG *bag = NULL;
  364. if (!pb->success)
  365. return;
  366. TEST_info("Adding secret <%s>", secret);
  367. bag = PKCS12_add_secret(&pb->bags, secret_nid, (const unsigned char *)secret, strlen(secret));
  368. if (!TEST_ptr(bag)) {
  369. pb->success = 0;
  370. return;
  371. }
  372. if (!add_attributes(bag, attrs))
  373. pb->success = 0;
  374. }
  375. /* -------------------------------------------------------------------------
  376. * PKCS12 structure checking
  377. */
  378. static int check_asn1_string(const ASN1_TYPE *av, const char *txt)
  379. {
  380. int ret = 0;
  381. char *value = NULL;
  382. if (!TEST_ptr(av))
  383. goto err;
  384. switch (av->type) {
  385. case V_ASN1_BMPSTRING:
  386. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  387. av->value.bmpstring->length);
  388. if (!TEST_str_eq(txt, (char *)value))
  389. goto err;
  390. break;
  391. case V_ASN1_UTF8STRING:
  392. if (!TEST_str_eq(txt, (char *)av->value.utf8string->data))
  393. goto err;
  394. break;
  395. case V_ASN1_OCTET_STRING:
  396. if (!TEST_str_eq(txt, (char *)av->value.octet_string->data))
  397. goto err;
  398. break;
  399. default:
  400. /* Tests do not support other attribute types currently */
  401. goto err;
  402. }
  403. ret = 1;
  404. err:
  405. OPENSSL_free(value);
  406. return ret;
  407. }
  408. static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs)
  409. {
  410. int ret = 0;
  411. X509_ATTRIBUTE *attr;
  412. ASN1_TYPE *av;
  413. int i, j;
  414. char attr_txt[100];
  415. for (i = 0; i < sk_X509_ATTRIBUTE_num(bag_attrs); i++) {
  416. const PKCS12_ATTR *p_attr = attrs;
  417. ASN1_OBJECT *attr_obj;
  418. attr = sk_X509_ATTRIBUTE_value(bag_attrs, i);
  419. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  420. OBJ_obj2txt(attr_txt, 100, attr_obj, 0);
  421. while(p_attr->oid != NULL) {
  422. /* Find a matching attribute type */
  423. if (strcmp(p_attr->oid, attr_txt) == 0) {
  424. if (!TEST_int_eq(X509_ATTRIBUTE_count(attr), 1))
  425. goto err;
  426. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
  427. {
  428. av = X509_ATTRIBUTE_get0_type(attr, j);
  429. if (!TEST_true(check_asn1_string(av, p_attr->value)))
  430. goto err;
  431. }
  432. break;
  433. }
  434. p_attr++;
  435. }
  436. }
  437. ret = 1;
  438. err:
  439. return ret;
  440. }
  441. void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  442. const PKCS12_ATTR *attrs)
  443. {
  444. X509 *x509 = NULL;
  445. X509 *ref_x509 = NULL;
  446. const PKCS12_SAFEBAG *bag;
  447. if (!pb->success)
  448. return;
  449. bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
  450. if (!TEST_ptr(bag)) {
  451. pb->success = 0;
  452. return;
  453. }
  454. if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
  455. || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_certBag)
  456. || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), NID_x509Certificate)) {
  457. pb->success = 0;
  458. return;
  459. }
  460. x509 = PKCS12_SAFEBAG_get1_cert(bag);
  461. if (!TEST_ptr(x509)) {
  462. pb->success = 0;
  463. goto err;
  464. }
  465. ref_x509 = load_cert_asn1(bytes, len);
  466. if (!TEST_false(X509_cmp(x509, ref_x509)))
  467. pb->success = 0;
  468. err:
  469. X509_free(x509);
  470. X509_free(ref_x509);
  471. }
  472. void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  473. const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
  474. {
  475. EVP_PKEY *pkey = NULL;
  476. EVP_PKEY *ref_pkey = NULL;
  477. PKCS8_PRIV_KEY_INFO *p8;
  478. const PKCS8_PRIV_KEY_INFO *p8c;
  479. const PKCS12_SAFEBAG *bag;
  480. if (!pb->success)
  481. return;
  482. bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
  483. if (!TEST_ptr(bag)) {
  484. pb->success = 0;
  485. return;
  486. }
  487. if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)) {
  488. pb->success = 0;
  489. return;
  490. }
  491. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  492. case NID_keyBag:
  493. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  494. if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8c))) {
  495. pb->success = 0;
  496. goto err;
  497. }
  498. break;
  499. case NID_pkcs8ShroudedKeyBag:
  500. if (legacy)
  501. p8 = PKCS12_decrypt_skey(bag, enc->pass, strlen(enc->pass));
  502. else
  503. p8 = PKCS12_decrypt_skey_ex(bag, enc->pass, strlen(enc->pass), test_ctx, test_propq);
  504. if (!TEST_ptr(p8)) {
  505. pb->success = 0;
  506. goto err;
  507. }
  508. if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8))) {
  509. PKCS8_PRIV_KEY_INFO_free(p8);
  510. pb->success = 0;
  511. goto err;
  512. }
  513. PKCS8_PRIV_KEY_INFO_free(p8);
  514. break;
  515. default:
  516. pb->success = 0;
  517. goto err;
  518. }
  519. /* PKEY compare returns 1 for match */
  520. ref_pkey = load_pkey_asn1(bytes, len);
  521. if (!TEST_true(EVP_PKEY_eq(pkey, ref_pkey)))
  522. pb->success = 0;
  523. err:
  524. EVP_PKEY_free(pkey);
  525. EVP_PKEY_free(ref_pkey);
  526. }
  527. void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs)
  528. {
  529. const PKCS12_SAFEBAG *bag;
  530. if (!pb->success)
  531. return;
  532. bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
  533. if (!TEST_ptr(bag)) {
  534. pb->success = 0;
  535. return;
  536. }
  537. if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
  538. || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_secretBag)
  539. || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), secret_nid)
  540. || !TEST_true(check_asn1_string(PKCS12_SAFEBAG_get0_bag_obj(bag), secret)))
  541. pb->success = 0;
  542. }
  543. void start_check_pkcs12(PKCS12_BUILDER *pb)
  544. {
  545. PKCS12 *p12;
  546. if (!pb->success)
  547. return;
  548. p12 = from_bio_p12(pb->p12bio, NULL);
  549. if (!TEST_ptr(p12)) {
  550. pb->success = 0;
  551. return;
  552. }
  553. pb->safes = PKCS12_unpack_authsafes(p12);
  554. if (!TEST_ptr(pb->safes))
  555. pb->success = 0;
  556. pb->safe_idx = 0;
  557. PKCS12_free(p12);
  558. }
  559. void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
  560. {
  561. PKCS12 *p12;
  562. if (!pb->success)
  563. return;
  564. p12 = from_bio_p12(pb->p12bio, mac);
  565. if (!TEST_ptr(p12)) {
  566. pb->success = 0;
  567. return;
  568. }
  569. pb->safes = PKCS12_unpack_authsafes(p12);
  570. if (!TEST_ptr(pb->safes))
  571. pb->success = 0;
  572. pb->safe_idx = 0;
  573. PKCS12_free(p12);
  574. }
  575. void start_check_pkcs12_file(PKCS12_BUILDER *pb)
  576. {
  577. PKCS12 *p12;
  578. if (!pb->success)
  579. return;
  580. p12 = read_p12(pb->filename, NULL);
  581. if (!TEST_ptr(p12)) {
  582. pb->success = 0;
  583. return;
  584. }
  585. pb->safes = PKCS12_unpack_authsafes(p12);
  586. if (!TEST_ptr(pb->safes))
  587. pb->success = 0;
  588. pb->safe_idx = 0;
  589. PKCS12_free(p12);
  590. }
  591. void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
  592. {
  593. PKCS12 *p12;
  594. if (!pb->success)
  595. return;
  596. p12 = read_p12(pb->filename, mac);
  597. if (!TEST_ptr(p12)) {
  598. pb->success = 0;
  599. return;
  600. }
  601. pb->safes = PKCS12_unpack_authsafes(p12);
  602. if (!TEST_ptr(pb->safes))
  603. pb->success = 0;
  604. pb->safe_idx = 0;
  605. PKCS12_free(p12);
  606. }
  607. void end_check_pkcs12(PKCS12_BUILDER *pb)
  608. {
  609. if (!pb->success)
  610. return;
  611. sk_PKCS7_pop_free(pb->safes, PKCS7_free);
  612. }
  613. void start_check_contentinfo(PKCS12_BUILDER *pb)
  614. {
  615. if (!pb->success)
  616. return;
  617. pb->bag_idx = 0;
  618. pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, NULL);
  619. if (!TEST_ptr(pb->bags)) {
  620. pb->success = 0;
  621. return;
  622. }
  623. TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
  624. }
  625. void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
  626. {
  627. if (!pb->success)
  628. return;
  629. pb->bag_idx = 0;
  630. pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, enc);
  631. if (!TEST_ptr(pb->bags)) {
  632. pb->success = 0;
  633. return;
  634. }
  635. TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
  636. }
  637. void end_check_contentinfo(PKCS12_BUILDER *pb)
  638. {
  639. if (!pb->success)
  640. return;
  641. if (!TEST_int_eq(sk_PKCS12_SAFEBAG_num(pb->bags), pb->bag_idx))
  642. pb->success = 0;
  643. sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
  644. pb->bags = NULL;
  645. }