pkcs12.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. certs = load_certs(infile, FORMAT_PEM, NULL, e,
  380. "certificates");
  381. if (!certs)
  382. goto export_end;
  383. if (key) {
  384. /* Look for matching private key */
  385. for (i = 0; i < sk_X509_num(certs); i++) {
  386. x = sk_X509_value(certs, i);
  387. if (X509_check_private_key(x, key)) {
  388. ucert = x;
  389. /* Zero keyid and alias */
  390. X509_keyid_set1(ucert, NULL, 0);
  391. X509_alias_set1(ucert, NULL, 0);
  392. /* Remove from list */
  393. (void)sk_X509_delete(certs, i);
  394. break;
  395. }
  396. }
  397. if (!ucert) {
  398. BIO_printf(bio_err,
  399. "No certificate matches private key\n");
  400. goto export_end;
  401. }
  402. }
  403. }
  404. /* Add any more certificates asked for */
  405. if (certfile) {
  406. STACK_OF(X509) *morecerts = NULL;
  407. if ((morecerts = load_certs(certfile, FORMAT_PEM, NULL, e,
  408. "certificates from certfile")) == NULL)
  409. goto export_end;
  410. while (sk_X509_num(morecerts) > 0)
  411. sk_X509_push(certs, sk_X509_shift(morecerts));
  412. sk_X509_free(morecerts);
  413. }
  414. /* If chaining get chain from user cert */
  415. if (chain) {
  416. int vret;
  417. STACK_OF(X509) *chain2;
  418. X509_STORE *store;
  419. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
  420. == NULL)
  421. goto export_end;
  422. vret = get_cert_chain(ucert, store, &chain2);
  423. X509_STORE_free(store);
  424. if (vret == X509_V_OK) {
  425. /* Exclude verified certificate */
  426. for (i = 1; i < sk_X509_num(chain2); i++)
  427. sk_X509_push(certs, sk_X509_value(chain2, i));
  428. /* Free first certificate */
  429. X509_free(sk_X509_value(chain2, 0));
  430. sk_X509_free(chain2);
  431. } else {
  432. if (vret != X509_V_ERR_UNSPECIFIED)
  433. BIO_printf(bio_err, "Error %s getting chain.\n",
  434. X509_verify_cert_error_string(vret));
  435. else
  436. ERR_print_errors(bio_err);
  437. goto export_end;
  438. }
  439. }
  440. /* Add any CA names */
  441. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  442. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  443. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  444. }
  445. if (csp_name && key)
  446. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  447. MBSTRING_ASC, (unsigned char *)csp_name,
  448. -1);
  449. if (add_lmk && key)
  450. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  451. if (!noprompt &&
  452. EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
  453. 1)) {
  454. BIO_printf(bio_err, "Can't read Password\n");
  455. goto export_end;
  456. }
  457. if (!twopass)
  458. OPENSSL_strlcpy(macpass, pass, sizeof macpass);
  459. p12 = PKCS12_create(cpass, name, key, ucert, certs,
  460. key_pbe, cert_pbe, iter, -1, keytype);
  461. if (!p12) {
  462. ERR_print_errors(bio_err);
  463. goto export_end;
  464. }
  465. if (macalg) {
  466. if (!opt_md(macalg, &macmd))
  467. goto opthelp;
  468. }
  469. if (maciter != -1)
  470. PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
  471. assert(private);
  472. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  473. if (out == NULL)
  474. goto end;
  475. i2d_PKCS12_bio(out, p12);
  476. ret = 0;
  477. export_end:
  478. EVP_PKEY_free(key);
  479. sk_X509_pop_free(certs, X509_free);
  480. X509_free(ucert);
  481. goto end;
  482. }
  483. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  484. if (in == NULL)
  485. goto end;
  486. out = bio_open_owner(outfile, FORMAT_PEM, private);
  487. if (out == NULL)
  488. goto end;
  489. if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
  490. ERR_print_errors(bio_err);
  491. goto end;
  492. }
  493. if (!noprompt
  494. && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
  495. 0)) {
  496. BIO_printf(bio_err, "Can't read Password\n");
  497. goto end;
  498. }
  499. if (!twopass)
  500. OPENSSL_strlcpy(macpass, pass, sizeof macpass);
  501. if ((options & INFO) && p12->mac)
  502. BIO_printf(bio_err, "MAC Iteration %ld\n",
  503. p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
  504. if (macver) {
  505. /* If we enter empty password try no password first */
  506. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  507. /* If mac and crypto pass the same set it to NULL too */
  508. if (!twopass)
  509. cpass = NULL;
  510. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  511. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  512. ERR_print_errors(bio_err);
  513. goto end;
  514. }
  515. }
  516. assert(private);
  517. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  518. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  519. ERR_print_errors(bio_err);
  520. goto end;
  521. }
  522. ret = 0;
  523. end:
  524. PKCS12_free(p12);
  525. if (export_cert || inrand)
  526. app_RAND_write_file(NULL);
  527. BIO_free(in);
  528. BIO_free_all(out);
  529. sk_OPENSSL_STRING_free(canames);
  530. OPENSSL_free(passin);
  531. OPENSSL_free(passout);
  532. return (ret);
  533. }
  534. int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
  535. int passlen, int options, char *pempass,
  536. const EVP_CIPHER *enc)
  537. {
  538. STACK_OF(PKCS7) *asafes = NULL;
  539. STACK_OF(PKCS12_SAFEBAG) *bags;
  540. int i, bagnid;
  541. int ret = 0;
  542. PKCS7 *p7;
  543. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  544. return 0;
  545. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  546. p7 = sk_PKCS7_value(asafes, i);
  547. bagnid = OBJ_obj2nid(p7->type);
  548. if (bagnid == NID_pkcs7_data) {
  549. bags = PKCS12_unpack_p7data(p7);
  550. if (options & INFO)
  551. BIO_printf(bio_err, "PKCS7 Data\n");
  552. } else if (bagnid == NID_pkcs7_encrypted) {
  553. if (options & INFO) {
  554. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  555. alg_print(p7->d.encrypted->enc_data->algorithm);
  556. }
  557. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  558. } else
  559. continue;
  560. if (!bags)
  561. goto err;
  562. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  563. options, pempass, enc)) {
  564. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  565. goto err;
  566. }
  567. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  568. bags = NULL;
  569. }
  570. ret = 1;
  571. err:
  572. sk_PKCS7_pop_free(asafes, PKCS7_free);
  573. return ret;
  574. }
  575. int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
  576. char *pass, int passlen, int options, char *pempass,
  577. const EVP_CIPHER *enc)
  578. {
  579. int i;
  580. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  581. if (!dump_certs_pkeys_bag(out,
  582. sk_PKCS12_SAFEBAG_value(bags, i),
  583. pass, passlen, options, pempass, enc))
  584. return 0;
  585. }
  586. return 1;
  587. }
  588. int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
  589. int passlen, int options, char *pempass,
  590. const EVP_CIPHER *enc)
  591. {
  592. EVP_PKEY *pkey;
  593. PKCS8_PRIV_KEY_INFO *p8;
  594. X509 *x509;
  595. switch (M_PKCS12_bag_type(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, bag->attrib, "Bag Attributes");
  602. p8 = bag->value.keybag;
  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. BIO_printf(bio_err, "Shrouded Keybag: ");
  612. alg_print(bag->value.shkeybag->algor);
  613. }
  614. if (options & NOKEYS)
  615. return 1;
  616. print_attribs(out, bag->attrib, "Bag Attributes");
  617. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  618. return 0;
  619. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  620. PKCS8_PRIV_KEY_INFO_free(p8);
  621. return 0;
  622. }
  623. print_attribs(out, p8->attributes, "Key Attributes");
  624. PKCS8_PRIV_KEY_INFO_free(p8);
  625. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  626. EVP_PKEY_free(pkey);
  627. break;
  628. case NID_certBag:
  629. if (options & INFO)
  630. BIO_printf(bio_err, "Certificate bag\n");
  631. if (options & NOCERTS)
  632. return 1;
  633. if (PKCS12_get_attr(bag, NID_localKeyID)) {
  634. if (options & CACERTS)
  635. return 1;
  636. } else if (options & CLCERTS)
  637. return 1;
  638. print_attribs(out, bag->attrib, "Bag Attributes");
  639. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  640. return 1;
  641. if ((x509 = PKCS12_certbag2x509(bag)) == NULL)
  642. return 0;
  643. dump_cert_text(out, x509);
  644. PEM_write_bio_X509(out, x509);
  645. X509_free(x509);
  646. break;
  647. case NID_safeContentsBag:
  648. if (options & INFO)
  649. BIO_printf(bio_err, "Safe Contents bag\n");
  650. print_attribs(out, bag->attrib, "Bag Attributes");
  651. return dump_certs_pkeys_bags(out, bag->value.safes, pass,
  652. passlen, options, pempass, enc);
  653. default:
  654. BIO_printf(bio_err, "Warning unsupported bag type: ");
  655. i2a_ASN1_OBJECT(bio_err, bag->type);
  656. BIO_printf(bio_err, "\n");
  657. return 1;
  658. }
  659. return 1;
  660. }
  661. /* Given a single certificate return a verified chain or NULL if error */
  662. static int get_cert_chain(X509 *cert, X509_STORE *store,
  663. STACK_OF(X509) **chain)
  664. {
  665. X509_STORE_CTX store_ctx;
  666. STACK_OF(X509) *chn = NULL;
  667. int i = 0;
  668. if (!X509_STORE_CTX_init(&store_ctx, store, cert, NULL)) {
  669. *chain = NULL;
  670. return X509_V_ERR_UNSPECIFIED;
  671. }
  672. if (X509_verify_cert(&store_ctx) > 0)
  673. chn = X509_STORE_CTX_get1_chain(&store_ctx);
  674. else if ((i = X509_STORE_CTX_get_error(&store_ctx)) == 0)
  675. i = X509_V_ERR_UNSPECIFIED;
  676. X509_STORE_CTX_cleanup(&store_ctx);
  677. *chain = chn;
  678. return i;
  679. }
  680. static int alg_print(X509_ALGOR *alg)
  681. {
  682. PBEPARAM *pbe;
  683. const unsigned char *p = alg->parameter->value.sequence->data;
  684. pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
  685. if (!pbe)
  686. return 1;
  687. BIO_printf(bio_err, "%s, Iteration %ld\n",
  688. OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
  689. ASN1_INTEGER_get(pbe->iter));
  690. PBEPARAM_free(pbe);
  691. return 1;
  692. }
  693. /* Load all certificates from a given file */
  694. int cert_load(BIO *in, STACK_OF(X509) *sk)
  695. {
  696. int ret;
  697. X509 *cert;
  698. ret = 0;
  699. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  700. ret = 1;
  701. sk_X509_push(sk, cert);
  702. }
  703. if (ret)
  704. ERR_clear_error();
  705. return ret;
  706. }
  707. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  708. int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
  709. const char *name)
  710. {
  711. X509_ATTRIBUTE *attr;
  712. ASN1_TYPE *av;
  713. char *value;
  714. int i, attr_nid;
  715. if (!attrlst) {
  716. BIO_printf(out, "%s: <No Attributes>\n", name);
  717. return 1;
  718. }
  719. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  720. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  721. return 1;
  722. }
  723. BIO_printf(out, "%s\n", name);
  724. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  725. ASN1_OBJECT *attr_obj;
  726. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  727. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  728. attr_nid = OBJ_obj2nid(attr_obj);
  729. BIO_printf(out, " ");
  730. if (attr_nid == NID_undef) {
  731. i2a_ASN1_OBJECT(out, attr_obj);
  732. BIO_printf(out, ": ");
  733. } else
  734. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  735. if (X509_ATTRIBUTE_count(attr)) {
  736. av = X509_ATTRIBUTE_get0_type(attr, 0);
  737. switch (av->type) {
  738. case V_ASN1_BMPSTRING:
  739. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  740. av->value.bmpstring->length);
  741. BIO_printf(out, "%s\n", value);
  742. OPENSSL_free(value);
  743. break;
  744. case V_ASN1_OCTET_STRING:
  745. hex_prin(out, av->value.octet_string->data,
  746. av->value.octet_string->length);
  747. BIO_printf(out, "\n");
  748. break;
  749. case V_ASN1_BIT_STRING:
  750. hex_prin(out, av->value.bit_string->data,
  751. av->value.bit_string->length);
  752. BIO_printf(out, "\n");
  753. break;
  754. default:
  755. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  756. break;
  757. }
  758. } else
  759. BIO_printf(out, "<No Values>\n");
  760. }
  761. return 1;
  762. }
  763. void hex_prin(BIO *out, unsigned char *buf, int len)
  764. {
  765. int i;
  766. for (i = 0; i < len; i++)
  767. BIO_printf(out, "%02X ", buf[i]);
  768. }
  769. static int set_pbe(int *ppbe, const char *str)
  770. {
  771. if (!str)
  772. return 0;
  773. if (strcmp(str, "NONE") == 0) {
  774. *ppbe = -1;
  775. return 1;
  776. }
  777. *ppbe = OBJ_txt2nid(str);
  778. if (*ppbe == NID_undef) {
  779. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  780. return 0;
  781. }
  782. return 1;
  783. }
  784. #endif