pkcs12.c 27 KB

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