pkcs12.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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) && PKCS12_mac_present(p12)) {
  497. ASN1_INTEGER *tmaciter;
  498. PKCS12_get0_mac(NULL, NULL, NULL, &tmaciter, p12);
  499. BIO_printf(bio_err, "MAC Iteration %ld\n",
  500. tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
  501. }
  502. if (macver) {
  503. /* If we enter empty password try no password first */
  504. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  505. /* If mac and crypto pass the same set it to NULL too */
  506. if (!twopass)
  507. cpass = NULL;
  508. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  509. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  510. ERR_print_errors(bio_err);
  511. goto end;
  512. }
  513. }
  514. assert(private);
  515. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  516. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  517. ERR_print_errors(bio_err);
  518. goto end;
  519. }
  520. ret = 0;
  521. end:
  522. PKCS12_free(p12);
  523. if (export_cert || inrand)
  524. app_RAND_write_file(NULL);
  525. BIO_free(in);
  526. BIO_free_all(out);
  527. sk_OPENSSL_STRING_free(canames);
  528. OPENSSL_free(passin);
  529. OPENSSL_free(passout);
  530. return (ret);
  531. }
  532. int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
  533. int passlen, int options, char *pempass,
  534. const EVP_CIPHER *enc)
  535. {
  536. STACK_OF(PKCS7) *asafes = NULL;
  537. STACK_OF(PKCS12_SAFEBAG) *bags;
  538. int i, bagnid;
  539. int ret = 0;
  540. PKCS7 *p7;
  541. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  542. return 0;
  543. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  544. p7 = sk_PKCS7_value(asafes, i);
  545. bagnid = OBJ_obj2nid(p7->type);
  546. if (bagnid == NID_pkcs7_data) {
  547. bags = PKCS12_unpack_p7data(p7);
  548. if (options & INFO)
  549. BIO_printf(bio_err, "PKCS7 Data\n");
  550. } else if (bagnid == NID_pkcs7_encrypted) {
  551. if (options & INFO) {
  552. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  553. alg_print(p7->d.encrypted->enc_data->algorithm);
  554. }
  555. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  556. } else
  557. continue;
  558. if (!bags)
  559. goto err;
  560. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  561. options, pempass, enc)) {
  562. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  563. goto err;
  564. }
  565. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  566. bags = NULL;
  567. }
  568. ret = 1;
  569. err:
  570. sk_PKCS7_pop_free(asafes, PKCS7_free);
  571. return ret;
  572. }
  573. int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
  574. char *pass, int passlen, int options, char *pempass,
  575. const EVP_CIPHER *enc)
  576. {
  577. int i;
  578. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  579. if (!dump_certs_pkeys_bag(out,
  580. sk_PKCS12_SAFEBAG_value(bags, i),
  581. pass, passlen, options, pempass, enc))
  582. return 0;
  583. }
  584. return 1;
  585. }
  586. int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
  587. int passlen, int options, char *pempass,
  588. const EVP_CIPHER *enc)
  589. {
  590. EVP_PKEY *pkey;
  591. PKCS8_PRIV_KEY_INFO *p8;
  592. X509 *x509;
  593. STACK_OF(X509_ATTRIBUTE) *attrs;
  594. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  595. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  596. case NID_keyBag:
  597. if (options & INFO)
  598. BIO_printf(bio_err, "Key bag\n");
  599. if (options & NOKEYS)
  600. return 1;
  601. print_attribs(out, attrs, "Bag Attributes");
  602. p8 = PKCS12_SAFEBAG_get0_p8inf(bag);
  603. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL)
  604. return 0;
  605. print_attribs(out, p8->attributes, "Key Attributes");
  606. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  607. EVP_PKEY_free(pkey);
  608. break;
  609. case NID_pkcs8ShroudedKeyBag:
  610. if (options & INFO) {
  611. X509_SIG *tp8;
  612. BIO_printf(bio_err, "Shrouded Keybag: ");
  613. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  614. alg_print(tp8->algor);
  615. }
  616. if (options & NOKEYS)
  617. return 1;
  618. print_attribs(out, attrs, "Bag Attributes");
  619. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  620. return 0;
  621. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  622. PKCS8_PRIV_KEY_INFO_free(p8);
  623. return 0;
  624. }
  625. print_attribs(out, p8->attributes, "Key Attributes");
  626. PKCS8_PRIV_KEY_INFO_free(p8);
  627. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  628. EVP_PKEY_free(pkey);
  629. break;
  630. case NID_certBag:
  631. if (options & INFO)
  632. BIO_printf(bio_err, "Certificate bag\n");
  633. if (options & NOCERTS)
  634. return 1;
  635. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  636. if (options & CACERTS)
  637. return 1;
  638. } else if (options & CLCERTS)
  639. return 1;
  640. print_attribs(out, attrs, "Bag Attributes");
  641. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  642. return 1;
  643. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  644. return 0;
  645. dump_cert_text(out, x509);
  646. PEM_write_bio_X509(out, x509);
  647. X509_free(x509);
  648. break;
  649. case NID_safeContentsBag:
  650. if (options & INFO)
  651. BIO_printf(bio_err, "Safe Contents bag\n");
  652. print_attribs(out, attrs, "Bag Attributes");
  653. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  654. pass, passlen, options, pempass, enc);
  655. default:
  656. BIO_printf(bio_err, "Warning unsupported bag type: ");
  657. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  658. BIO_printf(bio_err, "\n");
  659. return 1;
  660. }
  661. return 1;
  662. }
  663. /* Given a single certificate return a verified chain or NULL if error */
  664. static int get_cert_chain(X509 *cert, X509_STORE *store,
  665. STACK_OF(X509) **chain)
  666. {
  667. X509_STORE_CTX store_ctx;
  668. STACK_OF(X509) *chn = NULL;
  669. int i = 0;
  670. if (!X509_STORE_CTX_init(&store_ctx, store, cert, NULL)) {
  671. *chain = NULL;
  672. return X509_V_ERR_UNSPECIFIED;
  673. }
  674. if (X509_verify_cert(&store_ctx) > 0)
  675. chn = X509_STORE_CTX_get1_chain(&store_ctx);
  676. else if ((i = X509_STORE_CTX_get_error(&store_ctx)) == 0)
  677. i = X509_V_ERR_UNSPECIFIED;
  678. X509_STORE_CTX_cleanup(&store_ctx);
  679. *chain = chn;
  680. return i;
  681. }
  682. static int alg_print(X509_ALGOR *alg)
  683. {
  684. PBEPARAM *pbe;
  685. const unsigned char *p = alg->parameter->value.sequence->data;
  686. pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
  687. if (!pbe)
  688. return 1;
  689. BIO_printf(bio_err, "%s, Iteration %ld\n",
  690. OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
  691. ASN1_INTEGER_get(pbe->iter));
  692. PBEPARAM_free(pbe);
  693. return 1;
  694. }
  695. /* Load all certificates from a given file */
  696. int cert_load(BIO *in, STACK_OF(X509) *sk)
  697. {
  698. int ret;
  699. X509 *cert;
  700. ret = 0;
  701. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  702. ret = 1;
  703. sk_X509_push(sk, cert);
  704. }
  705. if (ret)
  706. ERR_clear_error();
  707. return ret;
  708. }
  709. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  710. int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
  711. const char *name)
  712. {
  713. X509_ATTRIBUTE *attr;
  714. ASN1_TYPE *av;
  715. char *value;
  716. int i, attr_nid;
  717. if (!attrlst) {
  718. BIO_printf(out, "%s: <No Attributes>\n", name);
  719. return 1;
  720. }
  721. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  722. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  723. return 1;
  724. }
  725. BIO_printf(out, "%s\n", name);
  726. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  727. ASN1_OBJECT *attr_obj;
  728. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  729. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  730. attr_nid = OBJ_obj2nid(attr_obj);
  731. BIO_printf(out, " ");
  732. if (attr_nid == NID_undef) {
  733. i2a_ASN1_OBJECT(out, attr_obj);
  734. BIO_printf(out, ": ");
  735. } else
  736. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  737. if (X509_ATTRIBUTE_count(attr)) {
  738. av = X509_ATTRIBUTE_get0_type(attr, 0);
  739. switch (av->type) {
  740. case V_ASN1_BMPSTRING:
  741. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  742. av->value.bmpstring->length);
  743. BIO_printf(out, "%s\n", value);
  744. OPENSSL_free(value);
  745. break;
  746. case V_ASN1_OCTET_STRING:
  747. hex_prin(out, av->value.octet_string->data,
  748. av->value.octet_string->length);
  749. BIO_printf(out, "\n");
  750. break;
  751. case V_ASN1_BIT_STRING:
  752. hex_prin(out, av->value.bit_string->data,
  753. av->value.bit_string->length);
  754. BIO_printf(out, "\n");
  755. break;
  756. default:
  757. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  758. break;
  759. }
  760. } else
  761. BIO_printf(out, "<No Values>\n");
  762. }
  763. return 1;
  764. }
  765. void hex_prin(BIO *out, unsigned char *buf, int len)
  766. {
  767. int i;
  768. for (i = 0; i < len; i++)
  769. BIO_printf(out, "%02X ", buf[i]);
  770. }
  771. static int set_pbe(int *ppbe, const char *str)
  772. {
  773. if (!str)
  774. return 0;
  775. if (strcmp(str, "NONE") == 0) {
  776. *ppbe = -1;
  777. return 1;
  778. }
  779. *ppbe = OBJ_txt2nid(str);
  780. if (*ppbe == NID_undef) {
  781. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  782. return 0;
  783. }
  784. return 1;
  785. }
  786. #endif