pkcs12.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <openssl/opensslconf.h>
  59. #if !defined(OPENSSL_NO_DES)
  60. # include <stdio.h>
  61. # include <stdlib.h>
  62. # include <string.h>
  63. # include "apps.h"
  64. # include <openssl/crypto.h>
  65. # include <openssl/err.h>
  66. # include <openssl/pem.h>
  67. # include <openssl/pkcs12.h>
  68. # define NOKEYS 0x1
  69. # define NOCERTS 0x2
  70. # define INFO 0x4
  71. # define CLCERTS 0x8
  72. # define CACERTS 0x10
  73. static int get_cert_chain(X509 *cert, X509_STORE *store,
  74. STACK_OF(X509) **chain);
  75. int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
  76. int options, char *pempass, const EVP_CIPHER *enc);
  77. int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
  78. char *pass, int passlen, int options, char *pempass,
  79. const EVP_CIPHER *enc);
  80. int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
  81. int passlen, int options, char *pempass,
  82. const EVP_CIPHER *enc);
  83. int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
  84. const char *name);
  85. void hex_prin(BIO *out, unsigned char *buf, int len);
  86. static int alg_print(X509_ALGOR *alg);
  87. int cert_load(BIO *in, STACK_OF(X509) *sk);
  88. static int set_pbe(int *ppbe, const char *str);
  89. typedef enum OPTION_choice {
  90. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  91. OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
  92. OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
  93. OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
  94. OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
  95. OPT_RAND, OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
  96. OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
  97. OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE
  98. } OPTION_CHOICE;
  99. OPTIONS pkcs12_options[] = {
  100. {"help", OPT_HELP, '-', "Display this summary"},
  101. {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
  102. {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
  103. {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
  104. {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
  105. {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
  106. {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
  107. {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
  108. {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
  109. {"chain", OPT_CHAIN, '-', "Add certificate chain"},
  110. {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
  111. {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
  112. # ifndef OPENSSL_NO_RC2
  113. {"descert", OPT_DESCERT, '-',
  114. "Encrypt output with 3DES (default RC2-40)"},
  115. {"certpbe", OPT_CERTPBE, 's',
  116. "Certificate PBE algorithm (default RC2-40)"},
  117. # else
  118. {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
  119. {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
  120. # endif
  121. {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
  122. {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
  123. {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
  124. {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
  125. {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
  126. {"LMK", OPT_LMK, '-',
  127. "Add local machine keyset attribute to private key"},
  128. {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
  129. {"macalg", OPT_MACALG, 's',
  130. "Digest algorithm used in MAC (default SHA1)"},
  131. {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
  132. {"rand", OPT_RAND, 's',
  133. "Load the file(s) into the random number generator"},
  134. {"inkey", OPT_INKEY, '<', "Private key if not infile"},
  135. {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
  136. {"name", OPT_NAME, 's', "Use name as friendly name"},
  137. {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
  138. {"caname", OPT_CANAME, 's',
  139. "Use name as CA friendly name (can be repeated)"},
  140. {"in", OPT_IN, '<', "Input filename"},
  141. {"out", OPT_OUT, '>', "Output filename"},
  142. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  143. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  144. {"password", OPT_PASSWORD, 's', "Set import/export password source"},
  145. {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
  146. {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
  147. {"no-CAfile", OPT_NOCAFILE, '-',
  148. "Do not load the default certificates file"},
  149. {"no-CApath", OPT_NOCAPATH, '-',
  150. "Do not load certificates from the default certificates directory"},
  151. {"", OPT_CIPHER, '-', "Any supported cipher"},
  152. # ifndef OPENSSL_NO_ENGINE
  153. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  154. # endif
  155. {NULL}
  156. };
  157. int pkcs12_main(int argc, char **argv)
  158. {
  159. char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
  160. char *name = NULL, *csp_name = NULL;
  161. char pass[2048], macpass[2048];
  162. int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
  163. int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
  164. # ifndef OPENSSL_NO_RC2
  165. int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
  166. # else
  167. int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  168. # endif
  169. int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  170. int ret = 1, macver = 1, noprompt = 0, add_lmk = 0, private = 0;
  171. char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
  172. char *passin = NULL, *passout = NULL, *inrand = NULL, *macalg = NULL;
  173. char *cpass = NULL, *mpass = NULL, *CApath = NULL, *CAfile = NULL;
  174. char *prog;
  175. int noCApath = 0, noCAfile = 0;
  176. ENGINE *e = NULL;
  177. BIO *in = NULL, *out = NULL;
  178. PKCS12 *p12 = NULL;
  179. STACK_OF(OPENSSL_STRING) *canames = NULL;
  180. const EVP_CIPHER *enc = EVP_des_ede3_cbc();
  181. OPTION_CHOICE o;
  182. prog = opt_init(argc, argv, pkcs12_options);
  183. while ((o = opt_next()) != OPT_EOF) {
  184. switch (o) {
  185. case OPT_EOF:
  186. case OPT_ERR:
  187. opthelp:
  188. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  189. goto end;
  190. case OPT_HELP:
  191. opt_help(pkcs12_options);
  192. ret = 0;
  193. goto end;
  194. case OPT_NOKEYS:
  195. options |= NOKEYS;
  196. break;
  197. case OPT_KEYEX:
  198. keytype = KEY_EX;
  199. break;
  200. case OPT_KEYSIG:
  201. keytype = KEY_SIG;
  202. break;
  203. case OPT_NOCERTS:
  204. options |= NOCERTS;
  205. break;
  206. case OPT_CLCERTS:
  207. options |= CLCERTS;
  208. break;
  209. case OPT_CACERTS:
  210. options |= CACERTS;
  211. break;
  212. case OPT_NOOUT:
  213. options |= (NOKEYS | NOCERTS);
  214. break;
  215. case OPT_INFO:
  216. options |= INFO;
  217. break;
  218. case OPT_CHAIN:
  219. chain = 1;
  220. break;
  221. case OPT_TWOPASS:
  222. twopass = 1;
  223. break;
  224. case OPT_NOMACVER:
  225. macver = 0;
  226. break;
  227. case OPT_DESCERT:
  228. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  229. break;
  230. case OPT_EXPORT:
  231. export_cert = 1;
  232. break;
  233. case OPT_CIPHER:
  234. if (!opt_cipher(opt_unknown(), &enc))
  235. goto opthelp;
  236. break;
  237. case OPT_NOITER:
  238. iter = 1;
  239. break;
  240. case OPT_MACITER:
  241. maciter = PKCS12_DEFAULT_ITER;
  242. break;
  243. case OPT_NOMACITER:
  244. maciter = 1;
  245. break;
  246. case OPT_NOMAC:
  247. maciter = -1;
  248. break;
  249. case OPT_MACALG:
  250. macalg = opt_arg();
  251. break;
  252. case OPT_NODES:
  253. enc = NULL;
  254. break;
  255. case OPT_CERTPBE:
  256. if (!set_pbe(&cert_pbe, opt_arg()))
  257. goto opthelp;
  258. break;
  259. case OPT_KEYPBE:
  260. if (!set_pbe(&key_pbe, opt_arg()))
  261. goto opthelp;
  262. break;
  263. case OPT_RAND:
  264. inrand = opt_arg();
  265. break;
  266. case OPT_INKEY:
  267. keyname = opt_arg();
  268. break;
  269. case OPT_CERTFILE:
  270. certfile = opt_arg();
  271. break;
  272. case OPT_NAME:
  273. name = opt_arg();
  274. break;
  275. case OPT_LMK:
  276. add_lmk = 1;
  277. break;
  278. case OPT_CSP:
  279. csp_name = opt_arg();
  280. break;
  281. case OPT_CANAME:
  282. if (canames == NULL
  283. && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
  284. goto end;
  285. sk_OPENSSL_STRING_push(canames, opt_arg());
  286. break;
  287. case OPT_IN:
  288. infile = opt_arg();
  289. break;
  290. case OPT_OUT:
  291. outfile = opt_arg();
  292. break;
  293. case OPT_PASSIN:
  294. passinarg = opt_arg();
  295. break;
  296. case OPT_PASSOUT:
  297. passoutarg = opt_arg();
  298. break;
  299. case OPT_PASSWORD:
  300. passarg = opt_arg();
  301. break;
  302. case OPT_CAPATH:
  303. CApath = opt_arg();
  304. break;
  305. case OPT_CAFILE:
  306. CAfile = opt_arg();
  307. break;
  308. case OPT_NOCAPATH:
  309. noCApath = 1;
  310. break;
  311. case OPT_NOCAFILE:
  312. noCAfile = 1;
  313. break;
  314. case OPT_ENGINE:
  315. e = setup_engine(opt_arg(), 0);
  316. break;
  317. }
  318. }
  319. argc = opt_num_rest();
  320. argv = opt_rest();
  321. private = 1;
  322. if (passarg) {
  323. if (export_cert)
  324. passoutarg = passarg;
  325. else
  326. passinarg = passarg;
  327. }
  328. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  329. BIO_printf(bio_err, "Error getting passwords\n");
  330. goto end;
  331. }
  332. if (!cpass) {
  333. if (export_cert)
  334. cpass = passout;
  335. else
  336. cpass = passin;
  337. }
  338. if (cpass) {
  339. mpass = cpass;
  340. noprompt = 1;
  341. } else {
  342. cpass = pass;
  343. mpass = macpass;
  344. }
  345. if (export_cert || inrand) {
  346. app_RAND_load_file(NULL, (inrand != NULL));
  347. if (inrand != NULL)
  348. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  349. app_RAND_load_files(inrand));
  350. }
  351. if (twopass) {
  352. if (EVP_read_pw_string
  353. (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
  354. BIO_printf(bio_err, "Can't read Password\n");
  355. goto end;
  356. }
  357. }
  358. if (export_cert) {
  359. EVP_PKEY *key = NULL;
  360. X509 *ucert = NULL, *x = NULL;
  361. STACK_OF(X509) *certs = NULL;
  362. const EVP_MD *macmd = NULL;
  363. unsigned char *catmp = NULL;
  364. int i;
  365. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  366. BIO_printf(bio_err, "Nothing to do!\n");
  367. goto export_end;
  368. }
  369. if (options & NOCERTS)
  370. chain = 0;
  371. if (!(options & NOKEYS)) {
  372. key = load_key(keyname ? keyname : infile,
  373. FORMAT_PEM, 1, passin, e, "private key");
  374. if (!key)
  375. goto export_end;
  376. }
  377. /* Load in all certs in input file */
  378. if (!(options & NOCERTS)) {
  379. if (!load_certs(infile, &certs, FORMAT_PEM, NULL, e,
  380. "certificates"))
  381. goto export_end;
  382. if (key) {
  383. /* Look for matching private key */
  384. for (i = 0; i < sk_X509_num(certs); i++) {
  385. x = sk_X509_value(certs, i);
  386. if (X509_check_private_key(x, key)) {
  387. ucert = x;
  388. /* Zero keyid and alias */
  389. X509_keyid_set1(ucert, NULL, 0);
  390. X509_alias_set1(ucert, NULL, 0);
  391. /* Remove from list */
  392. (void)sk_X509_delete(certs, i);
  393. break;
  394. }
  395. }
  396. if (!ucert) {
  397. BIO_printf(bio_err,
  398. "No certificate matches private key\n");
  399. goto export_end;
  400. }
  401. }
  402. }
  403. /* Add any more certificates asked for */
  404. if (certfile) {
  405. if (!load_certs(certfile, &certs, FORMAT_PEM, NULL, e,
  406. "certificates from certfile"))
  407. goto export_end;
  408. }
  409. /* If chaining get chain from user cert */
  410. if (chain) {
  411. int vret;
  412. STACK_OF(X509) *chain2;
  413. X509_STORE *store;
  414. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
  415. == NULL)
  416. goto export_end;
  417. vret = get_cert_chain(ucert, store, &chain2);
  418. X509_STORE_free(store);
  419. if (vret == X509_V_OK) {
  420. /* Exclude verified certificate */
  421. for (i = 1; i < sk_X509_num(chain2); i++)
  422. sk_X509_push(certs, sk_X509_value(chain2, i));
  423. /* Free first certificate */
  424. X509_free(sk_X509_value(chain2, 0));
  425. sk_X509_free(chain2);
  426. } else {
  427. if (vret != X509_V_ERR_UNSPECIFIED)
  428. BIO_printf(bio_err, "Error %s getting chain.\n",
  429. X509_verify_cert_error_string(vret));
  430. else
  431. ERR_print_errors(bio_err);
  432. goto export_end;
  433. }
  434. }
  435. /* Add any CA names */
  436. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  437. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  438. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  439. }
  440. if (csp_name && key)
  441. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  442. MBSTRING_ASC, (unsigned char *)csp_name,
  443. -1);
  444. if (add_lmk && key)
  445. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  446. if (!noprompt &&
  447. EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
  448. 1)) {
  449. BIO_printf(bio_err, "Can't read Password\n");
  450. goto export_end;
  451. }
  452. if (!twopass)
  453. OPENSSL_strlcpy(macpass, pass, sizeof macpass);
  454. p12 = PKCS12_create(cpass, name, key, ucert, certs,
  455. key_pbe, cert_pbe, iter, -1, keytype);
  456. if (!p12) {
  457. ERR_print_errors(bio_err);
  458. goto export_end;
  459. }
  460. if (macalg) {
  461. if (!opt_md(macalg, &macmd))
  462. goto opthelp;
  463. }
  464. if (maciter != -1)
  465. PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
  466. assert(private);
  467. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  468. if (out == NULL)
  469. goto end;
  470. i2d_PKCS12_bio(out, p12);
  471. ret = 0;
  472. export_end:
  473. EVP_PKEY_free(key);
  474. sk_X509_pop_free(certs, X509_free);
  475. X509_free(ucert);
  476. goto end;
  477. }
  478. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  479. if (in == NULL)
  480. goto end;
  481. out = bio_open_owner(outfile, FORMAT_PEM, private);
  482. if (out == NULL)
  483. goto end;
  484. if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
  485. ERR_print_errors(bio_err);
  486. goto end;
  487. }
  488. if (!noprompt
  489. && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
  490. 0)) {
  491. BIO_printf(bio_err, "Can't read Password\n");
  492. goto end;
  493. }
  494. if (!twopass)
  495. OPENSSL_strlcpy(macpass, pass, sizeof macpass);
  496. if ((options & INFO) && p12->mac)
  497. BIO_printf(bio_err, "MAC Iteration %ld\n",
  498. p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
  499. if (macver) {
  500. /* If we enter empty password try no password first */
  501. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  502. /* If mac and crypto pass the same set it to NULL too */
  503. if (!twopass)
  504. cpass = NULL;
  505. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  506. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  507. ERR_print_errors(bio_err);
  508. goto end;
  509. }
  510. }
  511. assert(private);
  512. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  513. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  514. ERR_print_errors(bio_err);
  515. goto end;
  516. }
  517. ret = 0;
  518. end:
  519. PKCS12_free(p12);
  520. if (export_cert || inrand)
  521. app_RAND_write_file(NULL);
  522. BIO_free(in);
  523. BIO_free_all(out);
  524. sk_OPENSSL_STRING_free(canames);
  525. OPENSSL_free(passin);
  526. OPENSSL_free(passout);
  527. return (ret);
  528. }
  529. int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
  530. int passlen, int options, char *pempass,
  531. const EVP_CIPHER *enc)
  532. {
  533. STACK_OF(PKCS7) *asafes = NULL;
  534. STACK_OF(PKCS12_SAFEBAG) *bags;
  535. int i, bagnid;
  536. int ret = 0;
  537. PKCS7 *p7;
  538. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  539. return 0;
  540. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  541. p7 = sk_PKCS7_value(asafes, i);
  542. bagnid = OBJ_obj2nid(p7->type);
  543. if (bagnid == NID_pkcs7_data) {
  544. bags = PKCS12_unpack_p7data(p7);
  545. if (options & INFO)
  546. BIO_printf(bio_err, "PKCS7 Data\n");
  547. } else if (bagnid == NID_pkcs7_encrypted) {
  548. if (options & INFO) {
  549. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  550. alg_print(p7->d.encrypted->enc_data->algorithm);
  551. }
  552. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  553. } else
  554. continue;
  555. if (!bags)
  556. goto err;
  557. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  558. options, pempass, enc)) {
  559. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  560. goto err;
  561. }
  562. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  563. bags = NULL;
  564. }
  565. ret = 1;
  566. err:
  567. sk_PKCS7_pop_free(asafes, PKCS7_free);
  568. return ret;
  569. }
  570. int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
  571. char *pass, int passlen, int options, char *pempass,
  572. const EVP_CIPHER *enc)
  573. {
  574. int i;
  575. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  576. if (!dump_certs_pkeys_bag(out,
  577. sk_PKCS12_SAFEBAG_value(bags, i),
  578. pass, passlen, options, pempass, enc))
  579. return 0;
  580. }
  581. return 1;
  582. }
  583. int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
  584. int passlen, int options, char *pempass,
  585. const EVP_CIPHER *enc)
  586. {
  587. EVP_PKEY *pkey;
  588. PKCS8_PRIV_KEY_INFO *p8;
  589. X509 *x509;
  590. switch (M_PKCS12_bag_type(bag)) {
  591. case NID_keyBag:
  592. if (options & INFO)
  593. BIO_printf(bio_err, "Key bag\n");
  594. if (options & NOKEYS)
  595. return 1;
  596. print_attribs(out, bag->attrib, "Bag Attributes");
  597. p8 = bag->value.keybag;
  598. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL)
  599. return 0;
  600. print_attribs(out, p8->attributes, "Key Attributes");
  601. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  602. EVP_PKEY_free(pkey);
  603. break;
  604. case NID_pkcs8ShroudedKeyBag:
  605. if (options & INFO) {
  606. BIO_printf(bio_err, "Shrouded Keybag: ");
  607. alg_print(bag->value.shkeybag->algor);
  608. }
  609. if (options & NOKEYS)
  610. return 1;
  611. print_attribs(out, bag->attrib, "Bag Attributes");
  612. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  613. return 0;
  614. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  615. PKCS8_PRIV_KEY_INFO_free(p8);
  616. return 0;
  617. }
  618. print_attribs(out, p8->attributes, "Key Attributes");
  619. PKCS8_PRIV_KEY_INFO_free(p8);
  620. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  621. EVP_PKEY_free(pkey);
  622. break;
  623. case NID_certBag:
  624. if (options & INFO)
  625. BIO_printf(bio_err, "Certificate bag\n");
  626. if (options & NOCERTS)
  627. return 1;
  628. if (PKCS12_get_attr(bag, NID_localKeyID)) {
  629. if (options & CACERTS)
  630. return 1;
  631. } else if (options & CLCERTS)
  632. return 1;
  633. print_attribs(out, bag->attrib, "Bag Attributes");
  634. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  635. return 1;
  636. if ((x509 = PKCS12_certbag2x509(bag)) == NULL)
  637. return 0;
  638. dump_cert_text(out, x509);
  639. PEM_write_bio_X509(out, x509);
  640. X509_free(x509);
  641. break;
  642. case NID_safeContentsBag:
  643. if (options & INFO)
  644. BIO_printf(bio_err, "Safe Contents bag\n");
  645. print_attribs(out, bag->attrib, "Bag Attributes");
  646. return dump_certs_pkeys_bags(out, bag->value.safes, pass,
  647. passlen, options, pempass, enc);
  648. default:
  649. BIO_printf(bio_err, "Warning unsupported bag type: ");
  650. i2a_ASN1_OBJECT(bio_err, bag->type);
  651. BIO_printf(bio_err, "\n");
  652. return 1;
  653. }
  654. return 1;
  655. }
  656. /* Given a single certificate return a verified chain or NULL if error */
  657. static int get_cert_chain(X509 *cert, X509_STORE *store,
  658. STACK_OF(X509) **chain)
  659. {
  660. X509_STORE_CTX store_ctx;
  661. STACK_OF(X509) *chn = NULL;
  662. int i = 0;
  663. if (!X509_STORE_CTX_init(&store_ctx, store, cert, NULL)) {
  664. *chain = NULL;
  665. return X509_V_ERR_UNSPECIFIED;
  666. }
  667. if (X509_verify_cert(&store_ctx) > 0)
  668. chn = X509_STORE_CTX_get1_chain(&store_ctx);
  669. else if ((i = X509_STORE_CTX_get_error(&store_ctx)) == 0)
  670. i = X509_V_ERR_UNSPECIFIED;
  671. X509_STORE_CTX_cleanup(&store_ctx);
  672. *chain = chn;
  673. return i;
  674. }
  675. static int alg_print(X509_ALGOR *alg)
  676. {
  677. PBEPARAM *pbe;
  678. const unsigned char *p = alg->parameter->value.sequence->data;
  679. pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
  680. if (!pbe)
  681. return 1;
  682. BIO_printf(bio_err, "%s, Iteration %ld\n",
  683. OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
  684. ASN1_INTEGER_get(pbe->iter));
  685. PBEPARAM_free(pbe);
  686. return 1;
  687. }
  688. /* Load all certificates from a given file */
  689. int cert_load(BIO *in, STACK_OF(X509) *sk)
  690. {
  691. int ret;
  692. X509 *cert;
  693. ret = 0;
  694. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  695. ret = 1;
  696. sk_X509_push(sk, cert);
  697. }
  698. if (ret)
  699. ERR_clear_error();
  700. return ret;
  701. }
  702. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  703. int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
  704. const char *name)
  705. {
  706. X509_ATTRIBUTE *attr;
  707. ASN1_TYPE *av;
  708. char *value;
  709. int i, attr_nid;
  710. if (!attrlst) {
  711. BIO_printf(out, "%s: <No Attributes>\n", name);
  712. return 1;
  713. }
  714. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  715. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  716. return 1;
  717. }
  718. BIO_printf(out, "%s\n", name);
  719. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  720. ASN1_OBJECT *attr_obj;
  721. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  722. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  723. attr_nid = OBJ_obj2nid(attr_obj);
  724. BIO_printf(out, " ");
  725. if (attr_nid == NID_undef) {
  726. i2a_ASN1_OBJECT(out, attr_obj);
  727. BIO_printf(out, ": ");
  728. } else
  729. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  730. if (X509_ATTRIBUTE_count(attr)) {
  731. av = X509_ATTRIBUTE_get0_type(attr, 0);
  732. switch (av->type) {
  733. case V_ASN1_BMPSTRING:
  734. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  735. av->value.bmpstring->length);
  736. BIO_printf(out, "%s\n", value);
  737. OPENSSL_free(value);
  738. break;
  739. case V_ASN1_OCTET_STRING:
  740. hex_prin(out, av->value.octet_string->data,
  741. av->value.octet_string->length);
  742. BIO_printf(out, "\n");
  743. break;
  744. case V_ASN1_BIT_STRING:
  745. hex_prin(out, av->value.bit_string->data,
  746. av->value.bit_string->length);
  747. BIO_printf(out, "\n");
  748. break;
  749. default:
  750. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  751. break;
  752. }
  753. } else
  754. BIO_printf(out, "<No Values>\n");
  755. }
  756. return 1;
  757. }
  758. void hex_prin(BIO *out, unsigned char *buf, int len)
  759. {
  760. int i;
  761. for (i = 0; i < len; i++)
  762. BIO_printf(out, "%02X ", buf[i]);
  763. }
  764. static int set_pbe(int *ppbe, const char *str)
  765. {
  766. if (!str)
  767. return 0;
  768. if (strcmp(str, "NONE") == 0) {
  769. *ppbe = -1;
  770. return 1;
  771. }
  772. *ppbe = OBJ_txt2nid(str);
  773. if (*ppbe == NID_undef) {
  774. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  775. return 0;
  776. }
  777. return 1;
  778. }
  779. #endif