pkcs12.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #if defined(OPENSSL_NO_DES)
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include <stdio.h>
  14. # include <stdlib.h>
  15. # include <string.h>
  16. # include "apps.h"
  17. # include "progs.h"
  18. # include <openssl/crypto.h>
  19. # include <openssl/err.h>
  20. # include <openssl/pem.h>
  21. # include <openssl/pkcs12.h>
  22. # define NOKEYS 0x1
  23. # define NOCERTS 0x2
  24. # define INFO 0x4
  25. # define CLCERTS 0x8
  26. # define CACERTS 0x10
  27. #define PASSWD_BUF_SIZE 2048
  28. static int get_cert_chain(X509 *cert, X509_STORE *store,
  29. STACK_OF(X509) **chain);
  30. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
  31. const char *pass, int passlen, int options,
  32. char *pempass, const EVP_CIPHER *enc);
  33. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  34. const char *pass, int passlen, int options,
  35. char *pempass, const EVP_CIPHER *enc);
  36. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
  37. const char *pass, int passlen,
  38. int options, char *pempass, const EVP_CIPHER *enc);
  39. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  40. const char *name);
  41. void hex_prin(BIO *out, unsigned char *buf, int len);
  42. static int alg_print(const X509_ALGOR *alg);
  43. int cert_load(BIO *in, STACK_OF(X509) *sk);
  44. static int set_pbe(int *ppbe, const char *str);
  45. typedef enum OPTION_choice {
  46. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  47. OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
  48. OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
  49. OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
  50. OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
  51. OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
  52. OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
  53. OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE,
  54. OPT_R_ENUM
  55. } OPTION_CHOICE;
  56. const OPTIONS pkcs12_options[] = {
  57. {"help", OPT_HELP, '-', "Display this summary"},
  58. {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
  59. {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
  60. {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
  61. {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
  62. {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
  63. {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
  64. {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
  65. {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
  66. {"chain", OPT_CHAIN, '-', "Add certificate chain"},
  67. {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
  68. {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
  69. # ifndef OPENSSL_NO_RC2
  70. {"descert", OPT_DESCERT, '-',
  71. "Encrypt output with 3DES (default RC2-40)"},
  72. {"certpbe", OPT_CERTPBE, 's',
  73. "Certificate PBE algorithm (default RC2-40)"},
  74. # else
  75. {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
  76. {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
  77. # endif
  78. {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
  79. {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
  80. {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
  81. {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
  82. {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
  83. {"LMK", OPT_LMK, '-',
  84. "Add local machine keyset attribute to private key"},
  85. {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
  86. {"macalg", OPT_MACALG, 's',
  87. "Digest algorithm used in MAC (default SHA1)"},
  88. {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
  89. OPT_R_OPTIONS,
  90. {"inkey", OPT_INKEY, 's', "Private key if not infile"},
  91. {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
  92. {"name", OPT_NAME, 's', "Use name as friendly name"},
  93. {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
  94. {"caname", OPT_CANAME, 's',
  95. "Use name as CA friendly name (can be repeated)"},
  96. {"in", OPT_IN, '<', "Input filename"},
  97. {"out", OPT_OUT, '>', "Output filename"},
  98. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  99. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  100. {"password", OPT_PASSWORD, 's', "Set import/export password source"},
  101. {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
  102. {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
  103. {"no-CAfile", OPT_NOCAFILE, '-',
  104. "Do not load the default certificates file"},
  105. {"no-CApath", OPT_NOCAPATH, '-',
  106. "Do not load certificates from the default certificates directory"},
  107. {"", OPT_CIPHER, '-', "Any supported cipher"},
  108. # ifndef OPENSSL_NO_ENGINE
  109. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  110. # endif
  111. {NULL}
  112. };
  113. int pkcs12_main(int argc, char **argv)
  114. {
  115. char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
  116. char *name = NULL, *csp_name = NULL;
  117. char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
  118. int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
  119. int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
  120. # ifndef OPENSSL_NO_RC2
  121. int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
  122. # else
  123. int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  124. # endif
  125. int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  126. int ret = 1, macver = 1, add_lmk = 0, private = 0;
  127. int noprompt = 0;
  128. char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
  129. char *passin = NULL, *passout = NULL, *macalg = NULL;
  130. char *cpass = NULL, *mpass = NULL, *badpass = NULL;
  131. const char *CApath = NULL, *CAfile = NULL, *prog;
  132. int noCApath = 0, noCAfile = 0;
  133. ENGINE *e = NULL;
  134. BIO *in = NULL, *out = NULL;
  135. PKCS12 *p12 = NULL;
  136. STACK_OF(OPENSSL_STRING) *canames = NULL;
  137. const EVP_CIPHER *enc = EVP_des_ede3_cbc();
  138. OPTION_CHOICE o;
  139. prog = opt_init(argc, argv, pkcs12_options);
  140. while ((o = opt_next()) != OPT_EOF) {
  141. switch (o) {
  142. case OPT_EOF:
  143. case OPT_ERR:
  144. opthelp:
  145. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  146. goto end;
  147. case OPT_HELP:
  148. opt_help(pkcs12_options);
  149. ret = 0;
  150. goto end;
  151. case OPT_NOKEYS:
  152. options |= NOKEYS;
  153. break;
  154. case OPT_KEYEX:
  155. keytype = KEY_EX;
  156. break;
  157. case OPT_KEYSIG:
  158. keytype = KEY_SIG;
  159. break;
  160. case OPT_NOCERTS:
  161. options |= NOCERTS;
  162. break;
  163. case OPT_CLCERTS:
  164. options |= CLCERTS;
  165. break;
  166. case OPT_CACERTS:
  167. options |= CACERTS;
  168. break;
  169. case OPT_NOOUT:
  170. options |= (NOKEYS | NOCERTS);
  171. break;
  172. case OPT_INFO:
  173. options |= INFO;
  174. break;
  175. case OPT_CHAIN:
  176. chain = 1;
  177. break;
  178. case OPT_TWOPASS:
  179. twopass = 1;
  180. break;
  181. case OPT_NOMACVER:
  182. macver = 0;
  183. break;
  184. case OPT_DESCERT:
  185. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  186. break;
  187. case OPT_EXPORT:
  188. export_cert = 1;
  189. break;
  190. case OPT_CIPHER:
  191. if (!opt_cipher(opt_unknown(), &enc))
  192. goto opthelp;
  193. break;
  194. case OPT_NOITER:
  195. iter = 1;
  196. break;
  197. case OPT_MACITER:
  198. maciter = PKCS12_DEFAULT_ITER;
  199. break;
  200. case OPT_NOMACITER:
  201. maciter = 1;
  202. break;
  203. case OPT_NOMAC:
  204. maciter = -1;
  205. break;
  206. case OPT_MACALG:
  207. macalg = opt_arg();
  208. break;
  209. case OPT_NODES:
  210. enc = NULL;
  211. break;
  212. case OPT_CERTPBE:
  213. if (!set_pbe(&cert_pbe, opt_arg()))
  214. goto opthelp;
  215. break;
  216. case OPT_KEYPBE:
  217. if (!set_pbe(&key_pbe, opt_arg()))
  218. goto opthelp;
  219. break;
  220. case OPT_R_CASES:
  221. if (!opt_rand(o))
  222. goto end;
  223. break;
  224. case OPT_INKEY:
  225. keyname = opt_arg();
  226. break;
  227. case OPT_CERTFILE:
  228. certfile = opt_arg();
  229. break;
  230. case OPT_NAME:
  231. name = opt_arg();
  232. break;
  233. case OPT_LMK:
  234. add_lmk = 1;
  235. break;
  236. case OPT_CSP:
  237. csp_name = opt_arg();
  238. break;
  239. case OPT_CANAME:
  240. if (canames == NULL
  241. && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
  242. goto end;
  243. sk_OPENSSL_STRING_push(canames, opt_arg());
  244. break;
  245. case OPT_IN:
  246. infile = opt_arg();
  247. break;
  248. case OPT_OUT:
  249. outfile = opt_arg();
  250. break;
  251. case OPT_PASSIN:
  252. passinarg = opt_arg();
  253. break;
  254. case OPT_PASSOUT:
  255. passoutarg = opt_arg();
  256. break;
  257. case OPT_PASSWORD:
  258. passarg = opt_arg();
  259. break;
  260. case OPT_CAPATH:
  261. CApath = opt_arg();
  262. break;
  263. case OPT_CAFILE:
  264. CAfile = opt_arg();
  265. break;
  266. case OPT_NOCAPATH:
  267. noCApath = 1;
  268. break;
  269. case OPT_NOCAFILE:
  270. noCAfile = 1;
  271. break;
  272. case OPT_ENGINE:
  273. e = setup_engine(opt_arg(), 0);
  274. break;
  275. }
  276. }
  277. argc = opt_num_rest();
  278. if (argc != 0)
  279. goto opthelp;
  280. private = 1;
  281. if (passarg != NULL) {
  282. if (export_cert)
  283. passoutarg = passarg;
  284. else
  285. passinarg = passarg;
  286. }
  287. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  288. BIO_printf(bio_err, "Error getting passwords\n");
  289. goto end;
  290. }
  291. if (cpass == NULL) {
  292. if (export_cert)
  293. cpass = passout;
  294. else
  295. cpass = passin;
  296. }
  297. if (cpass != NULL) {
  298. mpass = cpass;
  299. noprompt = 1;
  300. } else {
  301. cpass = pass;
  302. mpass = macpass;
  303. }
  304. if (twopass) {
  305. /* To avoid bit rot */
  306. if (1) {
  307. #ifndef OPENSSL_NO_UI_CONSOLE
  308. if (EVP_read_pw_string(
  309. macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
  310. BIO_printf(bio_err, "Can't read Password\n");
  311. goto end;
  312. }
  313. } else {
  314. #endif
  315. BIO_printf(bio_err, "Unsupported option -twopass\n");
  316. goto end;
  317. }
  318. }
  319. if (export_cert) {
  320. EVP_PKEY *key = NULL;
  321. X509 *ucert = NULL, *x = NULL;
  322. STACK_OF(X509) *certs = NULL;
  323. const EVP_MD *macmd = NULL;
  324. unsigned char *catmp = NULL;
  325. int i;
  326. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  327. BIO_printf(bio_err, "Nothing to do!\n");
  328. goto export_end;
  329. }
  330. if (options & NOCERTS)
  331. chain = 0;
  332. if (!(options & NOKEYS)) {
  333. key = load_key(keyname ? keyname : infile,
  334. FORMAT_PEM, 1, passin, e, "private key");
  335. if (key == NULL)
  336. goto export_end;
  337. }
  338. /* Load in all certs in input file */
  339. if (!(options & NOCERTS)) {
  340. if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
  341. "certificates"))
  342. goto export_end;
  343. if (key != NULL) {
  344. /* Look for matching private key */
  345. for (i = 0; i < sk_X509_num(certs); i++) {
  346. x = sk_X509_value(certs, i);
  347. if (X509_check_private_key(x, key)) {
  348. ucert = x;
  349. /* Zero keyid and alias */
  350. X509_keyid_set1(ucert, NULL, 0);
  351. X509_alias_set1(ucert, NULL, 0);
  352. /* Remove from list */
  353. (void)sk_X509_delete(certs, i);
  354. break;
  355. }
  356. }
  357. if (ucert == NULL) {
  358. BIO_printf(bio_err,
  359. "No certificate matches private key\n");
  360. goto export_end;
  361. }
  362. }
  363. }
  364. /* Add any more certificates asked for */
  365. if (certfile != NULL) {
  366. if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
  367. "certificates from certfile"))
  368. goto export_end;
  369. }
  370. /* If chaining get chain from user cert */
  371. if (chain) {
  372. int vret;
  373. STACK_OF(X509) *chain2;
  374. X509_STORE *store;
  375. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
  376. == NULL)
  377. goto export_end;
  378. vret = get_cert_chain(ucert, store, &chain2);
  379. X509_STORE_free(store);
  380. if (vret == X509_V_OK) {
  381. /* Exclude verified certificate */
  382. for (i = 1; i < sk_X509_num(chain2); i++)
  383. sk_X509_push(certs, sk_X509_value(chain2, i));
  384. /* Free first certificate */
  385. X509_free(sk_X509_value(chain2, 0));
  386. sk_X509_free(chain2);
  387. } else {
  388. if (vret != X509_V_ERR_UNSPECIFIED)
  389. BIO_printf(bio_err, "Error %s getting chain.\n",
  390. X509_verify_cert_error_string(vret));
  391. else
  392. ERR_print_errors(bio_err);
  393. goto export_end;
  394. }
  395. }
  396. /* Add any CA names */
  397. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  398. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  399. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  400. }
  401. if (csp_name != NULL && key != NULL)
  402. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  403. MBSTRING_ASC, (unsigned char *)csp_name,
  404. -1);
  405. if (add_lmk && key != NULL)
  406. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  407. if (!noprompt) {
  408. /* To avoid bit rot */
  409. if (1) {
  410. #ifndef OPENSSL_NO_UI_CONSOLE
  411. if (EVP_read_pw_string(pass, sizeof(pass),
  412. "Enter Export Password:", 1)) {
  413. BIO_printf(bio_err, "Can't read Password\n");
  414. goto export_end;
  415. }
  416. } else {
  417. #endif
  418. BIO_printf(bio_err, "Password required\n");
  419. goto export_end;
  420. }
  421. }
  422. if (!twopass)
  423. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  424. p12 = PKCS12_create(cpass, name, key, ucert, certs,
  425. key_pbe, cert_pbe, iter, -1, keytype);
  426. if (!p12) {
  427. ERR_print_errors(bio_err);
  428. goto export_end;
  429. }
  430. if (macalg) {
  431. if (!opt_md(macalg, &macmd))
  432. goto opthelp;
  433. }
  434. if (maciter != -1)
  435. PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
  436. assert(private);
  437. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  438. if (out == NULL)
  439. goto end;
  440. i2d_PKCS12_bio(out, p12);
  441. ret = 0;
  442. export_end:
  443. EVP_PKEY_free(key);
  444. sk_X509_pop_free(certs, X509_free);
  445. X509_free(ucert);
  446. goto end;
  447. }
  448. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  449. if (in == NULL)
  450. goto end;
  451. out = bio_open_owner(outfile, FORMAT_PEM, private);
  452. if (out == NULL)
  453. goto end;
  454. if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
  455. ERR_print_errors(bio_err);
  456. goto end;
  457. }
  458. if (!noprompt) {
  459. if (1) {
  460. #ifndef OPENSSL_NO_UI_CONSOLE
  461. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
  462. 0)) {
  463. BIO_printf(bio_err, "Can't read Password\n");
  464. goto end;
  465. }
  466. } else {
  467. #endif
  468. BIO_printf(bio_err, "Password required\n");
  469. goto end;
  470. }
  471. }
  472. if (!twopass)
  473. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  474. if ((options & INFO) && PKCS12_mac_present(p12)) {
  475. const ASN1_INTEGER *tmaciter;
  476. const X509_ALGOR *macalgid;
  477. const ASN1_OBJECT *macobj;
  478. const ASN1_OCTET_STRING *tmac;
  479. const ASN1_OCTET_STRING *tsalt;
  480. PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
  481. /* current hash algorithms do not use parameters so extract just name,
  482. in future alg_print() may be needed */
  483. X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
  484. BIO_puts(bio_err, "MAC: ");
  485. i2a_ASN1_OBJECT(bio_err, macobj);
  486. BIO_printf(bio_err, ", Iteration %ld\n",
  487. tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
  488. BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
  489. tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
  490. tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
  491. }
  492. if (macver) {
  493. /* If we enter empty password try no password first */
  494. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  495. /* If mac and crypto pass the same set it to NULL too */
  496. if (!twopass)
  497. cpass = NULL;
  498. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  499. /*
  500. * May be UTF8 from previous version of OpenSSL:
  501. * convert to a UTF8 form which will translate
  502. * to the same Unicode password.
  503. */
  504. unsigned char *utmp;
  505. int utmplen;
  506. utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
  507. if (utmp == NULL)
  508. goto end;
  509. badpass = OPENSSL_uni2utf8(utmp, utmplen);
  510. OPENSSL_free(utmp);
  511. if (!PKCS12_verify_mac(p12, badpass, -1)) {
  512. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  513. ERR_print_errors(bio_err);
  514. goto end;
  515. } else {
  516. BIO_printf(bio_err, "Warning: using broken algorithm\n");
  517. if (!twopass)
  518. cpass = badpass;
  519. }
  520. }
  521. }
  522. assert(private);
  523. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  524. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  525. ERR_print_errors(bio_err);
  526. goto end;
  527. }
  528. ret = 0;
  529. end:
  530. PKCS12_free(p12);
  531. release_engine(e);
  532. BIO_free(in);
  533. BIO_free_all(out);
  534. sk_OPENSSL_STRING_free(canames);
  535. OPENSSL_free(badpass);
  536. OPENSSL_free(passin);
  537. OPENSSL_free(passout);
  538. return ret;
  539. }
  540. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  541. int passlen, int options, char *pempass,
  542. const EVP_CIPHER *enc)
  543. {
  544. STACK_OF(PKCS7) *asafes = NULL;
  545. STACK_OF(PKCS12_SAFEBAG) *bags;
  546. int i, bagnid;
  547. int ret = 0;
  548. PKCS7 *p7;
  549. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  550. return 0;
  551. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  552. p7 = sk_PKCS7_value(asafes, i);
  553. bagnid = OBJ_obj2nid(p7->type);
  554. if (bagnid == NID_pkcs7_data) {
  555. bags = PKCS12_unpack_p7data(p7);
  556. if (options & INFO)
  557. BIO_printf(bio_err, "PKCS7 Data\n");
  558. } else if (bagnid == NID_pkcs7_encrypted) {
  559. if (options & INFO) {
  560. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  561. alg_print(p7->d.encrypted->enc_data->algorithm);
  562. }
  563. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  564. } else {
  565. continue;
  566. }
  567. if (!bags)
  568. goto err;
  569. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  570. options, pempass, enc)) {
  571. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  572. goto err;
  573. }
  574. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  575. bags = NULL;
  576. }
  577. ret = 1;
  578. err:
  579. sk_PKCS7_pop_free(asafes, PKCS7_free);
  580. return ret;
  581. }
  582. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  583. const char *pass, int passlen, int options,
  584. char *pempass, const EVP_CIPHER *enc)
  585. {
  586. int i;
  587. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  588. if (!dump_certs_pkeys_bag(out,
  589. sk_PKCS12_SAFEBAG_value(bags, i),
  590. pass, passlen, options, pempass, enc))
  591. return 0;
  592. }
  593. return 1;
  594. }
  595. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  596. const char *pass, int passlen, int options,
  597. char *pempass, const EVP_CIPHER *enc)
  598. {
  599. EVP_PKEY *pkey;
  600. PKCS8_PRIV_KEY_INFO *p8;
  601. const PKCS8_PRIV_KEY_INFO *p8c;
  602. X509 *x509;
  603. const STACK_OF(X509_ATTRIBUTE) *attrs;
  604. int ret = 0;
  605. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  606. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  607. case NID_keyBag:
  608. if (options & INFO)
  609. BIO_printf(bio_err, "Key bag\n");
  610. if (options & NOKEYS)
  611. return 1;
  612. print_attribs(out, attrs, "Bag Attributes");
  613. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  614. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  615. return 0;
  616. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  617. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  618. EVP_PKEY_free(pkey);
  619. break;
  620. case NID_pkcs8ShroudedKeyBag:
  621. if (options & INFO) {
  622. const X509_SIG *tp8;
  623. const X509_ALGOR *tp8alg;
  624. BIO_printf(bio_err, "Shrouded Keybag: ");
  625. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  626. X509_SIG_get0(tp8, &tp8alg, NULL);
  627. alg_print(tp8alg);
  628. }
  629. if (options & NOKEYS)
  630. return 1;
  631. print_attribs(out, attrs, "Bag Attributes");
  632. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  633. return 0;
  634. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  635. PKCS8_PRIV_KEY_INFO_free(p8);
  636. return 0;
  637. }
  638. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  639. PKCS8_PRIV_KEY_INFO_free(p8);
  640. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  641. EVP_PKEY_free(pkey);
  642. break;
  643. case NID_certBag:
  644. if (options & INFO)
  645. BIO_printf(bio_err, "Certificate bag\n");
  646. if (options & NOCERTS)
  647. return 1;
  648. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  649. if (options & CACERTS)
  650. return 1;
  651. } else if (options & CLCERTS)
  652. return 1;
  653. print_attribs(out, attrs, "Bag Attributes");
  654. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  655. return 1;
  656. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  657. return 0;
  658. dump_cert_text(out, x509);
  659. ret = PEM_write_bio_X509(out, x509);
  660. X509_free(x509);
  661. break;
  662. case NID_safeContentsBag:
  663. if (options & INFO)
  664. BIO_printf(bio_err, "Safe Contents bag\n");
  665. print_attribs(out, attrs, "Bag Attributes");
  666. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  667. pass, passlen, options, pempass, enc);
  668. default:
  669. BIO_printf(bio_err, "Warning unsupported bag type: ");
  670. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  671. BIO_printf(bio_err, "\n");
  672. return 1;
  673. }
  674. return ret;
  675. }
  676. /* Given a single certificate return a verified chain or NULL if error */
  677. static int get_cert_chain(X509 *cert, X509_STORE *store,
  678. STACK_OF(X509) **chain)
  679. {
  680. X509_STORE_CTX *store_ctx = NULL;
  681. STACK_OF(X509) *chn = NULL;
  682. int i = 0;
  683. store_ctx = X509_STORE_CTX_new();
  684. if (store_ctx == NULL) {
  685. i = X509_V_ERR_UNSPECIFIED;
  686. goto end;
  687. }
  688. if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
  689. i = X509_V_ERR_UNSPECIFIED;
  690. goto end;
  691. }
  692. if (X509_verify_cert(store_ctx) > 0)
  693. chn = X509_STORE_CTX_get1_chain(store_ctx);
  694. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  695. i = X509_V_ERR_UNSPECIFIED;
  696. end:
  697. X509_STORE_CTX_free(store_ctx);
  698. *chain = chn;
  699. return i;
  700. }
  701. static int alg_print(const X509_ALGOR *alg)
  702. {
  703. int pbenid, aparamtype;
  704. const ASN1_OBJECT *aoid;
  705. const void *aparam;
  706. PBEPARAM *pbe = NULL;
  707. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  708. pbenid = OBJ_obj2nid(aoid);
  709. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  710. /*
  711. * If PBE algorithm is PBES2 decode algorithm parameters
  712. * for additional details.
  713. */
  714. if (pbenid == NID_pbes2) {
  715. PBE2PARAM *pbe2 = NULL;
  716. int encnid;
  717. if (aparamtype == V_ASN1_SEQUENCE)
  718. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  719. if (pbe2 == NULL) {
  720. BIO_puts(bio_err, ", <unsupported parameters>");
  721. goto done;
  722. }
  723. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  724. pbenid = OBJ_obj2nid(aoid);
  725. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  726. encnid = OBJ_obj2nid(aoid);
  727. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  728. OBJ_nid2sn(encnid));
  729. /* If KDF is PBKDF2 decode parameters */
  730. if (pbenid == NID_id_pbkdf2) {
  731. PBKDF2PARAM *kdf = NULL;
  732. int prfnid;
  733. if (aparamtype == V_ASN1_SEQUENCE)
  734. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  735. if (kdf == NULL) {
  736. BIO_puts(bio_err, ", <unsupported parameters>");
  737. goto done;
  738. }
  739. if (kdf->prf == NULL) {
  740. prfnid = NID_hmacWithSHA1;
  741. } else {
  742. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  743. prfnid = OBJ_obj2nid(aoid);
  744. }
  745. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  746. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  747. PBKDF2PARAM_free(kdf);
  748. #ifndef OPENSSL_NO_SCRYPT
  749. } else if (pbenid == NID_id_scrypt) {
  750. SCRYPT_PARAMS *kdf = NULL;
  751. if (aparamtype == V_ASN1_SEQUENCE)
  752. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  753. if (kdf == NULL) {
  754. BIO_puts(bio_err, ", <unsupported parameters>");
  755. goto done;
  756. }
  757. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  758. "Block size(r): %ld, Paralelizm(p): %ld",
  759. ASN1_STRING_length(kdf->salt),
  760. ASN1_INTEGER_get(kdf->costParameter),
  761. ASN1_INTEGER_get(kdf->blockSize),
  762. ASN1_INTEGER_get(kdf->parallelizationParameter));
  763. SCRYPT_PARAMS_free(kdf);
  764. #endif
  765. }
  766. PBE2PARAM_free(pbe2);
  767. } else {
  768. if (aparamtype == V_ASN1_SEQUENCE)
  769. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  770. if (pbe == NULL) {
  771. BIO_puts(bio_err, ", <unsupported parameters>");
  772. goto done;
  773. }
  774. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  775. PBEPARAM_free(pbe);
  776. }
  777. done:
  778. BIO_puts(bio_err, "\n");
  779. return 1;
  780. }
  781. /* Load all certificates from a given file */
  782. int cert_load(BIO *in, STACK_OF(X509) *sk)
  783. {
  784. int ret;
  785. X509 *cert;
  786. ret = 0;
  787. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  788. ret = 1;
  789. sk_X509_push(sk, cert);
  790. }
  791. if (ret)
  792. ERR_clear_error();
  793. return ret;
  794. }
  795. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  796. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  797. const char *name)
  798. {
  799. X509_ATTRIBUTE *attr;
  800. ASN1_TYPE *av;
  801. char *value;
  802. int i, attr_nid;
  803. if (!attrlst) {
  804. BIO_printf(out, "%s: <No Attributes>\n", name);
  805. return 1;
  806. }
  807. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  808. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  809. return 1;
  810. }
  811. BIO_printf(out, "%s\n", name);
  812. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  813. ASN1_OBJECT *attr_obj;
  814. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  815. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  816. attr_nid = OBJ_obj2nid(attr_obj);
  817. BIO_printf(out, " ");
  818. if (attr_nid == NID_undef) {
  819. i2a_ASN1_OBJECT(out, attr_obj);
  820. BIO_printf(out, ": ");
  821. } else {
  822. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  823. }
  824. if (X509_ATTRIBUTE_count(attr)) {
  825. av = X509_ATTRIBUTE_get0_type(attr, 0);
  826. switch (av->type) {
  827. case V_ASN1_BMPSTRING:
  828. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  829. av->value.bmpstring->length);
  830. BIO_printf(out, "%s\n", value);
  831. OPENSSL_free(value);
  832. break;
  833. case V_ASN1_OCTET_STRING:
  834. hex_prin(out, av->value.octet_string->data,
  835. av->value.octet_string->length);
  836. BIO_printf(out, "\n");
  837. break;
  838. case V_ASN1_BIT_STRING:
  839. hex_prin(out, av->value.bit_string->data,
  840. av->value.bit_string->length);
  841. BIO_printf(out, "\n");
  842. break;
  843. default:
  844. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  845. break;
  846. }
  847. } else {
  848. BIO_printf(out, "<No Values>\n");
  849. }
  850. }
  851. return 1;
  852. }
  853. void hex_prin(BIO *out, unsigned char *buf, int len)
  854. {
  855. int i;
  856. for (i = 0; i < len; i++)
  857. BIO_printf(out, "%02X ", buf[i]);
  858. }
  859. static int set_pbe(int *ppbe, const char *str)
  860. {
  861. if (!str)
  862. return 0;
  863. if (strcmp(str, "NONE") == 0) {
  864. *ppbe = -1;
  865. return 1;
  866. }
  867. *ppbe = OBJ_txt2nid(str);
  868. if (*ppbe == NID_undef) {
  869. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  870. return 0;
  871. }
  872. return 1;
  873. }
  874. #endif