pkcs12.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 *attr)
  276. {
  277. int ret = 0;
  278. int attr_nid;
  279. const PKCS12_ATTR *p_attr = attr;
  280. STACK_OF(X509_ATTRIBUTE)* attrs = NULL;
  281. X509_ATTRIBUTE *x509_attr = NULL;
  282. if (attr == NULL)
  283. return 1;
  284. while (p_attr->oid != NULL) {
  285. TEST_info("Adding attribute %s = %s", p_attr->oid, p_attr->value);
  286. attr_nid = OBJ_txt2nid(p_attr->oid);
  287. if (attr_nid == NID_friendlyName) {
  288. if (!TEST_true(PKCS12_add_friendlyname(bag, p_attr->value, -1)))
  289. goto err;
  290. } else if (attr_nid == NID_localKeyID) {
  291. if (!TEST_true(PKCS12_add_localkeyid(bag, (unsigned char *)p_attr->value,
  292. strlen(p_attr->value))))
  293. goto err;
  294. } else if (attr_nid == NID_oracle_jdk_trustedkeyusage) {
  295. attrs = (STACK_OF(X509_ATTRIBUTE)*)PKCS12_SAFEBAG_get0_attrs(bag);
  296. x509_attr = X509_ATTRIBUTE_create(attr_nid, V_ASN1_OBJECT, OBJ_txt2obj(p_attr->value, 0));
  297. X509at_add1_attr(&attrs, x509_attr);
  298. PKCS12_SAFEBAG_set0_attrs(bag, attrs);
  299. X509_ATTRIBUTE_free(x509_attr);
  300. } else {
  301. /* Custom attribute values limited to ASCII in these tests */
  302. if (!TEST_true(PKCS12_add1_attr_by_txt(bag, p_attr->oid, MBSTRING_ASC,
  303. (unsigned char *)p_attr->value,
  304. strlen(p_attr->value))))
  305. goto err;
  306. }
  307. p_attr++;
  308. }
  309. ret = 1;
  310. err:
  311. return ret;
  312. }
  313. void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  314. const PKCS12_ATTR *attrs)
  315. {
  316. PKCS12_SAFEBAG *bag = NULL;
  317. X509 *cert = NULL;
  318. char *name;
  319. if (!pb->success)
  320. return;
  321. cert = load_cert_asn1(bytes, len);
  322. if (!TEST_ptr(cert)) {
  323. pb->success = 0;
  324. return;
  325. }
  326. name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
  327. TEST_info("Adding certificate <%s>", name);
  328. OPENSSL_free(name);
  329. bag = PKCS12_add_cert(&pb->bags, cert);
  330. if (!TEST_ptr(bag)) {
  331. pb->success = 0;
  332. goto err;
  333. }
  334. if (!TEST_true(add_attributes(bag, attrs))) {
  335. pb->success = 0;
  336. goto err;
  337. }
  338. err:
  339. X509_free(cert);
  340. }
  341. void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  342. const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
  343. {
  344. PKCS12_SAFEBAG *bag = NULL;
  345. EVP_PKEY *pkey = NULL;
  346. if (!pb->success)
  347. return;
  348. TEST_info("Adding key");
  349. pkey = load_pkey_asn1(bytes, len);
  350. if (!TEST_ptr(pkey)) {
  351. pb->success = 0;
  352. return;
  353. }
  354. if (legacy)
  355. bag = PKCS12_add_key(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass);
  356. else
  357. bag = PKCS12_add_key_ex(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass,
  358. test_ctx, test_propq);
  359. if (!TEST_ptr(bag)) {
  360. pb->success = 0;
  361. goto err;
  362. }
  363. if (!add_attributes(bag, attrs))
  364. pb->success = 0;
  365. err:
  366. EVP_PKEY_free(pkey);
  367. }
  368. void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
  369. const PKCS12_ATTR *attrs)
  370. {
  371. PKCS12_SAFEBAG *bag = NULL;
  372. if (!pb->success)
  373. return;
  374. TEST_info("Adding secret <%s>", secret);
  375. bag = PKCS12_add_secret(&pb->bags, secret_nid, (const unsigned char *)secret, strlen(secret));
  376. if (!TEST_ptr(bag)) {
  377. pb->success = 0;
  378. return;
  379. }
  380. if (!add_attributes(bag, attrs))
  381. pb->success = 0;
  382. }
  383. /* -------------------------------------------------------------------------
  384. * PKCS12 structure checking
  385. */
  386. static int check_asn1_string(const ASN1_TYPE *av, const char *txt)
  387. {
  388. int ret = 0;
  389. char *value = NULL;
  390. if (!TEST_ptr(av))
  391. goto err;
  392. switch (av->type) {
  393. case V_ASN1_BMPSTRING:
  394. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  395. av->value.bmpstring->length);
  396. if (!TEST_str_eq(txt, (char *)value))
  397. goto err;
  398. break;
  399. case V_ASN1_UTF8STRING:
  400. if (!TEST_mem_eq(txt, strlen(txt), (char *)av->value.utf8string->data,
  401. av->value.utf8string->length))
  402. goto err;
  403. break;
  404. case V_ASN1_OCTET_STRING:
  405. if (!TEST_mem_eq(txt, strlen(txt),
  406. (char *)av->value.octet_string->data,
  407. av->value.octet_string->length))
  408. goto err;
  409. break;
  410. default:
  411. /* Tests do not support other attribute types currently */
  412. goto err;
  413. }
  414. ret = 1;
  415. err:
  416. OPENSSL_free(value);
  417. return ret;
  418. }
  419. static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs)
  420. {
  421. int ret = 0;
  422. X509_ATTRIBUTE *attr;
  423. ASN1_TYPE *av;
  424. int i, j;
  425. char attr_txt[100];
  426. for (i = 0; i < sk_X509_ATTRIBUTE_num(bag_attrs); i++) {
  427. const PKCS12_ATTR *p_attr = attrs;
  428. ASN1_OBJECT *attr_obj;
  429. attr = sk_X509_ATTRIBUTE_value(bag_attrs, i);
  430. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  431. OBJ_obj2txt(attr_txt, 100, attr_obj, 0);
  432. while (p_attr->oid != NULL) {
  433. /* Find a matching attribute type */
  434. if (strcmp(p_attr->oid, attr_txt) == 0) {
  435. if (!TEST_int_eq(X509_ATTRIBUTE_count(attr), 1))
  436. goto err;
  437. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
  438. {
  439. av = X509_ATTRIBUTE_get0_type(attr, j);
  440. if (!TEST_true(check_asn1_string(av, p_attr->value)))
  441. goto err;
  442. }
  443. break;
  444. }
  445. p_attr++;
  446. }
  447. }
  448. ret = 1;
  449. err:
  450. return ret;
  451. }
  452. void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  453. const PKCS12_ATTR *attrs)
  454. {
  455. X509 *x509 = NULL;
  456. X509 *ref_x509 = NULL;
  457. const PKCS12_SAFEBAG *bag;
  458. if (!pb->success)
  459. return;
  460. bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
  461. if (!TEST_ptr(bag)) {
  462. pb->success = 0;
  463. return;
  464. }
  465. if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
  466. || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_certBag)
  467. || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), NID_x509Certificate)) {
  468. pb->success = 0;
  469. return;
  470. }
  471. x509 = PKCS12_SAFEBAG_get1_cert(bag);
  472. if (!TEST_ptr(x509)) {
  473. pb->success = 0;
  474. goto err;
  475. }
  476. ref_x509 = load_cert_asn1(bytes, len);
  477. if (!TEST_false(X509_cmp(x509, ref_x509)))
  478. pb->success = 0;
  479. err:
  480. X509_free(x509);
  481. X509_free(ref_x509);
  482. }
  483. void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
  484. const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
  485. {
  486. EVP_PKEY *pkey = NULL;
  487. EVP_PKEY *ref_pkey = NULL;
  488. PKCS8_PRIV_KEY_INFO *p8;
  489. const PKCS8_PRIV_KEY_INFO *p8c;
  490. const PKCS12_SAFEBAG *bag;
  491. if (!pb->success)
  492. return;
  493. bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
  494. if (!TEST_ptr(bag)) {
  495. pb->success = 0;
  496. return;
  497. }
  498. if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)) {
  499. pb->success = 0;
  500. return;
  501. }
  502. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  503. case NID_keyBag:
  504. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  505. if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8c))) {
  506. pb->success = 0;
  507. goto err;
  508. }
  509. break;
  510. case NID_pkcs8ShroudedKeyBag:
  511. if (legacy)
  512. p8 = PKCS12_decrypt_skey(bag, enc->pass, strlen(enc->pass));
  513. else
  514. p8 = PKCS12_decrypt_skey_ex(bag, enc->pass, strlen(enc->pass), test_ctx, test_propq);
  515. if (!TEST_ptr(p8)) {
  516. pb->success = 0;
  517. goto err;
  518. }
  519. if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8))) {
  520. PKCS8_PRIV_KEY_INFO_free(p8);
  521. pb->success = 0;
  522. goto err;
  523. }
  524. PKCS8_PRIV_KEY_INFO_free(p8);
  525. break;
  526. default:
  527. pb->success = 0;
  528. goto err;
  529. }
  530. /* PKEY compare returns 1 for match */
  531. ref_pkey = load_pkey_asn1(bytes, len);
  532. if (!TEST_true(EVP_PKEY_eq(pkey, ref_pkey)))
  533. pb->success = 0;
  534. err:
  535. EVP_PKEY_free(pkey);
  536. EVP_PKEY_free(ref_pkey);
  537. }
  538. void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs)
  539. {
  540. const PKCS12_SAFEBAG *bag;
  541. if (!pb->success)
  542. return;
  543. bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
  544. if (!TEST_ptr(bag)) {
  545. pb->success = 0;
  546. return;
  547. }
  548. if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
  549. || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_secretBag)
  550. || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), secret_nid)
  551. || !TEST_true(check_asn1_string(PKCS12_SAFEBAG_get0_bag_obj(bag), secret)))
  552. pb->success = 0;
  553. }
  554. void start_check_pkcs12(PKCS12_BUILDER *pb)
  555. {
  556. PKCS12 *p12;
  557. if (!pb->success)
  558. return;
  559. p12 = from_bio_p12(pb->p12bio, NULL);
  560. if (!TEST_ptr(p12)) {
  561. pb->success = 0;
  562. return;
  563. }
  564. pb->safes = PKCS12_unpack_authsafes(p12);
  565. if (!TEST_ptr(pb->safes))
  566. pb->success = 0;
  567. pb->safe_idx = 0;
  568. PKCS12_free(p12);
  569. }
  570. void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
  571. {
  572. PKCS12 *p12;
  573. if (!pb->success)
  574. return;
  575. p12 = from_bio_p12(pb->p12bio, mac);
  576. if (!TEST_ptr(p12)) {
  577. pb->success = 0;
  578. return;
  579. }
  580. pb->safes = PKCS12_unpack_authsafes(p12);
  581. if (!TEST_ptr(pb->safes))
  582. pb->success = 0;
  583. pb->safe_idx = 0;
  584. PKCS12_free(p12);
  585. }
  586. void start_check_pkcs12_file(PKCS12_BUILDER *pb)
  587. {
  588. PKCS12 *p12;
  589. if (!pb->success)
  590. return;
  591. p12 = read_p12(pb->filename, NULL);
  592. if (!TEST_ptr(p12)) {
  593. pb->success = 0;
  594. return;
  595. }
  596. pb->safes = PKCS12_unpack_authsafes(p12);
  597. if (!TEST_ptr(pb->safes))
  598. pb->success = 0;
  599. pb->safe_idx = 0;
  600. PKCS12_free(p12);
  601. }
  602. void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
  603. {
  604. PKCS12 *p12;
  605. if (!pb->success)
  606. return;
  607. p12 = read_p12(pb->filename, mac);
  608. if (!TEST_ptr(p12)) {
  609. pb->success = 0;
  610. return;
  611. }
  612. pb->safes = PKCS12_unpack_authsafes(p12);
  613. if (!TEST_ptr(pb->safes))
  614. pb->success = 0;
  615. pb->safe_idx = 0;
  616. PKCS12_free(p12);
  617. }
  618. void end_check_pkcs12(PKCS12_BUILDER *pb)
  619. {
  620. if (!pb->success)
  621. return;
  622. sk_PKCS7_pop_free(pb->safes, PKCS7_free);
  623. }
  624. void start_check_contentinfo(PKCS12_BUILDER *pb)
  625. {
  626. if (!pb->success)
  627. return;
  628. pb->bag_idx = 0;
  629. pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, NULL);
  630. if (!TEST_ptr(pb->bags)) {
  631. pb->success = 0;
  632. return;
  633. }
  634. TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
  635. }
  636. void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
  637. {
  638. if (!pb->success)
  639. return;
  640. pb->bag_idx = 0;
  641. pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, enc);
  642. if (!TEST_ptr(pb->bags)) {
  643. pb->success = 0;
  644. return;
  645. }
  646. TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
  647. }
  648. void end_check_contentinfo(PKCS12_BUILDER *pb)
  649. {
  650. if (!pb->success)
  651. return;
  652. if (!TEST_int_eq(sk_PKCS12_SAFEBAG_num(pb->bags), pb->bag_idx))
  653. pb->success = 0;
  654. sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
  655. pb->bags = NULL;
  656. }