pkcs12_helper.c 17 KB

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