pkcs12.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. if (twopass) {
  301. if (export_cert)
  302. BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
  303. else
  304. BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
  305. goto end;
  306. }
  307. } else {
  308. cpass = pass;
  309. mpass = macpass;
  310. }
  311. if (twopass) {
  312. /* To avoid bit rot */
  313. if (1) {
  314. #ifndef OPENSSL_NO_UI_CONSOLE
  315. if (EVP_read_pw_string(
  316. macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
  317. BIO_printf(bio_err, "Can't read Password\n");
  318. goto end;
  319. }
  320. } else {
  321. #endif
  322. BIO_printf(bio_err, "Unsupported option -twopass\n");
  323. goto end;
  324. }
  325. }
  326. if (export_cert) {
  327. EVP_PKEY *key = NULL;
  328. X509 *ucert = NULL, *x = NULL;
  329. STACK_OF(X509) *certs = NULL;
  330. const EVP_MD *macmd = NULL;
  331. unsigned char *catmp = NULL;
  332. int i;
  333. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  334. BIO_printf(bio_err, "Nothing to do!\n");
  335. goto export_end;
  336. }
  337. if (options & NOCERTS)
  338. chain = 0;
  339. if (!(options & NOKEYS)) {
  340. key = load_key(keyname ? keyname : infile,
  341. FORMAT_PEM, 1, passin, e, "private key");
  342. if (key == NULL)
  343. goto export_end;
  344. }
  345. /* Load in all certs in input file */
  346. if (!(options & NOCERTS)) {
  347. if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
  348. "certificates"))
  349. goto export_end;
  350. if (key != NULL) {
  351. /* Look for matching private key */
  352. for (i = 0; i < sk_X509_num(certs); i++) {
  353. x = sk_X509_value(certs, i);
  354. if (X509_check_private_key(x, key)) {
  355. ucert = x;
  356. /* Zero keyid and alias */
  357. X509_keyid_set1(ucert, NULL, 0);
  358. X509_alias_set1(ucert, NULL, 0);
  359. /* Remove from list */
  360. (void)sk_X509_delete(certs, i);
  361. break;
  362. }
  363. }
  364. if (ucert == NULL) {
  365. BIO_printf(bio_err,
  366. "No certificate matches private key\n");
  367. goto export_end;
  368. }
  369. }
  370. }
  371. /* Add any more certificates asked for */
  372. if (certfile != NULL) {
  373. if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
  374. "certificates from certfile"))
  375. goto export_end;
  376. }
  377. /* If chaining get chain from user cert */
  378. if (chain) {
  379. int vret;
  380. STACK_OF(X509) *chain2;
  381. X509_STORE *store;
  382. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
  383. == NULL)
  384. goto export_end;
  385. vret = get_cert_chain(ucert, store, &chain2);
  386. X509_STORE_free(store);
  387. if (vret == X509_V_OK) {
  388. /* Exclude verified certificate */
  389. for (i = 1; i < sk_X509_num(chain2); i++)
  390. sk_X509_push(certs, sk_X509_value(chain2, i));
  391. /* Free first certificate */
  392. X509_free(sk_X509_value(chain2, 0));
  393. sk_X509_free(chain2);
  394. } else {
  395. if (vret != X509_V_ERR_UNSPECIFIED)
  396. BIO_printf(bio_err, "Error %s getting chain.\n",
  397. X509_verify_cert_error_string(vret));
  398. else
  399. ERR_print_errors(bio_err);
  400. goto export_end;
  401. }
  402. }
  403. /* Add any CA names */
  404. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  405. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  406. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  407. }
  408. if (csp_name != NULL && key != NULL)
  409. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  410. MBSTRING_ASC, (unsigned char *)csp_name,
  411. -1);
  412. if (add_lmk && key != NULL)
  413. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  414. if (!noprompt) {
  415. /* To avoid bit rot */
  416. if (1) {
  417. #ifndef OPENSSL_NO_UI_CONSOLE
  418. if (EVP_read_pw_string(pass, sizeof(pass),
  419. "Enter Export Password:", 1)) {
  420. BIO_printf(bio_err, "Can't read Password\n");
  421. goto export_end;
  422. }
  423. } else {
  424. #endif
  425. BIO_printf(bio_err, "Password required\n");
  426. goto export_end;
  427. }
  428. }
  429. if (!twopass)
  430. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  431. p12 = PKCS12_create(cpass, name, key, ucert, certs,
  432. key_pbe, cert_pbe, iter, -1, keytype);
  433. if (!p12) {
  434. ERR_print_errors(bio_err);
  435. goto export_end;
  436. }
  437. if (macalg) {
  438. if (!opt_md(macalg, &macmd))
  439. goto opthelp;
  440. }
  441. if (maciter != -1)
  442. PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
  443. assert(private);
  444. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  445. if (out == NULL)
  446. goto end;
  447. i2d_PKCS12_bio(out, p12);
  448. ret = 0;
  449. export_end:
  450. EVP_PKEY_free(key);
  451. sk_X509_pop_free(certs, X509_free);
  452. X509_free(ucert);
  453. goto end;
  454. }
  455. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  456. if (in == NULL)
  457. goto end;
  458. out = bio_open_owner(outfile, FORMAT_PEM, private);
  459. if (out == NULL)
  460. goto end;
  461. if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
  462. ERR_print_errors(bio_err);
  463. goto end;
  464. }
  465. if (!noprompt) {
  466. if (1) {
  467. #ifndef OPENSSL_NO_UI_CONSOLE
  468. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
  469. 0)) {
  470. BIO_printf(bio_err, "Can't read Password\n");
  471. goto end;
  472. }
  473. } else {
  474. #endif
  475. BIO_printf(bio_err, "Password required\n");
  476. goto end;
  477. }
  478. }
  479. if (!twopass)
  480. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  481. if ((options & INFO) && PKCS12_mac_present(p12)) {
  482. const ASN1_INTEGER *tmaciter;
  483. const X509_ALGOR *macalgid;
  484. const ASN1_OBJECT *macobj;
  485. const ASN1_OCTET_STRING *tmac;
  486. const ASN1_OCTET_STRING *tsalt;
  487. PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
  488. /* current hash algorithms do not use parameters so extract just name,
  489. in future alg_print() may be needed */
  490. X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
  491. BIO_puts(bio_err, "MAC: ");
  492. i2a_ASN1_OBJECT(bio_err, macobj);
  493. BIO_printf(bio_err, ", Iteration %ld\n",
  494. tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
  495. BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
  496. tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
  497. tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
  498. }
  499. if (macver) {
  500. /* If we enter empty password try no password first */
  501. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  502. /* If mac and crypto pass the same set it to NULL too */
  503. if (!twopass)
  504. cpass = NULL;
  505. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  506. /*
  507. * May be UTF8 from previous version of OpenSSL:
  508. * convert to a UTF8 form which will translate
  509. * to the same Unicode password.
  510. */
  511. unsigned char *utmp;
  512. int utmplen;
  513. utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
  514. if (utmp == NULL)
  515. goto end;
  516. badpass = OPENSSL_uni2utf8(utmp, utmplen);
  517. OPENSSL_free(utmp);
  518. if (!PKCS12_verify_mac(p12, badpass, -1)) {
  519. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  520. ERR_print_errors(bio_err);
  521. goto end;
  522. } else {
  523. BIO_printf(bio_err, "Warning: using broken algorithm\n");
  524. if (!twopass)
  525. cpass = badpass;
  526. }
  527. }
  528. }
  529. assert(private);
  530. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  531. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  532. ERR_print_errors(bio_err);
  533. goto end;
  534. }
  535. ret = 0;
  536. end:
  537. PKCS12_free(p12);
  538. release_engine(e);
  539. BIO_free(in);
  540. BIO_free_all(out);
  541. sk_OPENSSL_STRING_free(canames);
  542. OPENSSL_free(badpass);
  543. OPENSSL_free(passin);
  544. OPENSSL_free(passout);
  545. return ret;
  546. }
  547. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  548. int passlen, int options, char *pempass,
  549. const EVP_CIPHER *enc)
  550. {
  551. STACK_OF(PKCS7) *asafes = NULL;
  552. STACK_OF(PKCS12_SAFEBAG) *bags;
  553. int i, bagnid;
  554. int ret = 0;
  555. PKCS7 *p7;
  556. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  557. return 0;
  558. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  559. p7 = sk_PKCS7_value(asafes, i);
  560. bagnid = OBJ_obj2nid(p7->type);
  561. if (bagnid == NID_pkcs7_data) {
  562. bags = PKCS12_unpack_p7data(p7);
  563. if (options & INFO)
  564. BIO_printf(bio_err, "PKCS7 Data\n");
  565. } else if (bagnid == NID_pkcs7_encrypted) {
  566. if (options & INFO) {
  567. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  568. alg_print(p7->d.encrypted->enc_data->algorithm);
  569. }
  570. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  571. } else {
  572. continue;
  573. }
  574. if (!bags)
  575. goto err;
  576. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  577. options, pempass, enc)) {
  578. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  579. goto err;
  580. }
  581. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  582. bags = NULL;
  583. }
  584. ret = 1;
  585. err:
  586. sk_PKCS7_pop_free(asafes, PKCS7_free);
  587. return ret;
  588. }
  589. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  590. const char *pass, int passlen, int options,
  591. char *pempass, const EVP_CIPHER *enc)
  592. {
  593. int i;
  594. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  595. if (!dump_certs_pkeys_bag(out,
  596. sk_PKCS12_SAFEBAG_value(bags, i),
  597. pass, passlen, options, pempass, enc))
  598. return 0;
  599. }
  600. return 1;
  601. }
  602. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  603. const char *pass, int passlen, int options,
  604. char *pempass, const EVP_CIPHER *enc)
  605. {
  606. EVP_PKEY *pkey;
  607. PKCS8_PRIV_KEY_INFO *p8;
  608. const PKCS8_PRIV_KEY_INFO *p8c;
  609. X509 *x509;
  610. const STACK_OF(X509_ATTRIBUTE) *attrs;
  611. int ret = 0;
  612. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  613. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  614. case NID_keyBag:
  615. if (options & INFO)
  616. BIO_printf(bio_err, "Key bag\n");
  617. if (options & NOKEYS)
  618. return 1;
  619. print_attribs(out, attrs, "Bag Attributes");
  620. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  621. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  622. return 0;
  623. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  624. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  625. EVP_PKEY_free(pkey);
  626. break;
  627. case NID_pkcs8ShroudedKeyBag:
  628. if (options & INFO) {
  629. const X509_SIG *tp8;
  630. const X509_ALGOR *tp8alg;
  631. BIO_printf(bio_err, "Shrouded Keybag: ");
  632. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  633. X509_SIG_get0(tp8, &tp8alg, NULL);
  634. alg_print(tp8alg);
  635. }
  636. if (options & NOKEYS)
  637. return 1;
  638. print_attribs(out, attrs, "Bag Attributes");
  639. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  640. return 0;
  641. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  642. PKCS8_PRIV_KEY_INFO_free(p8);
  643. return 0;
  644. }
  645. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  646. PKCS8_PRIV_KEY_INFO_free(p8);
  647. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  648. EVP_PKEY_free(pkey);
  649. break;
  650. case NID_certBag:
  651. if (options & INFO)
  652. BIO_printf(bio_err, "Certificate bag\n");
  653. if (options & NOCERTS)
  654. return 1;
  655. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  656. if (options & CACERTS)
  657. return 1;
  658. } else if (options & CLCERTS)
  659. return 1;
  660. print_attribs(out, attrs, "Bag Attributes");
  661. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  662. return 1;
  663. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  664. return 0;
  665. dump_cert_text(out, x509);
  666. ret = PEM_write_bio_X509(out, x509);
  667. X509_free(x509);
  668. break;
  669. case NID_safeContentsBag:
  670. if (options & INFO)
  671. BIO_printf(bio_err, "Safe Contents bag\n");
  672. print_attribs(out, attrs, "Bag Attributes");
  673. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  674. pass, passlen, options, pempass, enc);
  675. default:
  676. BIO_printf(bio_err, "Warning unsupported bag type: ");
  677. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  678. BIO_printf(bio_err, "\n");
  679. return 1;
  680. }
  681. return ret;
  682. }
  683. /* Given a single certificate return a verified chain or NULL if error */
  684. static int get_cert_chain(X509 *cert, X509_STORE *store,
  685. STACK_OF(X509) **chain)
  686. {
  687. X509_STORE_CTX *store_ctx = NULL;
  688. STACK_OF(X509) *chn = NULL;
  689. int i = 0;
  690. store_ctx = X509_STORE_CTX_new();
  691. if (store_ctx == NULL) {
  692. i = X509_V_ERR_UNSPECIFIED;
  693. goto end;
  694. }
  695. if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {
  696. i = X509_V_ERR_UNSPECIFIED;
  697. goto end;
  698. }
  699. if (X509_verify_cert(store_ctx) > 0)
  700. chn = X509_STORE_CTX_get1_chain(store_ctx);
  701. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  702. i = X509_V_ERR_UNSPECIFIED;
  703. end:
  704. X509_STORE_CTX_free(store_ctx);
  705. *chain = chn;
  706. return i;
  707. }
  708. static int alg_print(const X509_ALGOR *alg)
  709. {
  710. int pbenid, aparamtype;
  711. const ASN1_OBJECT *aoid;
  712. const void *aparam;
  713. PBEPARAM *pbe = NULL;
  714. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  715. pbenid = OBJ_obj2nid(aoid);
  716. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  717. /*
  718. * If PBE algorithm is PBES2 decode algorithm parameters
  719. * for additional details.
  720. */
  721. if (pbenid == NID_pbes2) {
  722. PBE2PARAM *pbe2 = NULL;
  723. int encnid;
  724. if (aparamtype == V_ASN1_SEQUENCE)
  725. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  726. if (pbe2 == NULL) {
  727. BIO_puts(bio_err, ", <unsupported parameters>");
  728. goto done;
  729. }
  730. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  731. pbenid = OBJ_obj2nid(aoid);
  732. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  733. encnid = OBJ_obj2nid(aoid);
  734. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  735. OBJ_nid2sn(encnid));
  736. /* If KDF is PBKDF2 decode parameters */
  737. if (pbenid == NID_id_pbkdf2) {
  738. PBKDF2PARAM *kdf = NULL;
  739. int prfnid;
  740. if (aparamtype == V_ASN1_SEQUENCE)
  741. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  742. if (kdf == NULL) {
  743. BIO_puts(bio_err, ", <unsupported parameters>");
  744. goto done;
  745. }
  746. if (kdf->prf == NULL) {
  747. prfnid = NID_hmacWithSHA1;
  748. } else {
  749. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  750. prfnid = OBJ_obj2nid(aoid);
  751. }
  752. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  753. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  754. PBKDF2PARAM_free(kdf);
  755. #ifndef OPENSSL_NO_SCRYPT
  756. } else if (pbenid == NID_id_scrypt) {
  757. SCRYPT_PARAMS *kdf = NULL;
  758. if (aparamtype == V_ASN1_SEQUENCE)
  759. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  760. if (kdf == NULL) {
  761. BIO_puts(bio_err, ", <unsupported parameters>");
  762. goto done;
  763. }
  764. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  765. "Block size(r): %ld, Paralelizm(p): %ld",
  766. ASN1_STRING_length(kdf->salt),
  767. ASN1_INTEGER_get(kdf->costParameter),
  768. ASN1_INTEGER_get(kdf->blockSize),
  769. ASN1_INTEGER_get(kdf->parallelizationParameter));
  770. SCRYPT_PARAMS_free(kdf);
  771. #endif
  772. }
  773. PBE2PARAM_free(pbe2);
  774. } else {
  775. if (aparamtype == V_ASN1_SEQUENCE)
  776. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  777. if (pbe == NULL) {
  778. BIO_puts(bio_err, ", <unsupported parameters>");
  779. goto done;
  780. }
  781. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  782. PBEPARAM_free(pbe);
  783. }
  784. done:
  785. BIO_puts(bio_err, "\n");
  786. return 1;
  787. }
  788. /* Load all certificates from a given file */
  789. int cert_load(BIO *in, STACK_OF(X509) *sk)
  790. {
  791. int ret;
  792. X509 *cert;
  793. ret = 0;
  794. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  795. ret = 1;
  796. sk_X509_push(sk, cert);
  797. }
  798. if (ret)
  799. ERR_clear_error();
  800. return ret;
  801. }
  802. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  803. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  804. const char *name)
  805. {
  806. X509_ATTRIBUTE *attr;
  807. ASN1_TYPE *av;
  808. char *value;
  809. int i, attr_nid;
  810. if (!attrlst) {
  811. BIO_printf(out, "%s: <No Attributes>\n", name);
  812. return 1;
  813. }
  814. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  815. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  816. return 1;
  817. }
  818. BIO_printf(out, "%s\n", name);
  819. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  820. ASN1_OBJECT *attr_obj;
  821. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  822. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  823. attr_nid = OBJ_obj2nid(attr_obj);
  824. BIO_printf(out, " ");
  825. if (attr_nid == NID_undef) {
  826. i2a_ASN1_OBJECT(out, attr_obj);
  827. BIO_printf(out, ": ");
  828. } else {
  829. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  830. }
  831. if (X509_ATTRIBUTE_count(attr)) {
  832. av = X509_ATTRIBUTE_get0_type(attr, 0);
  833. switch (av->type) {
  834. case V_ASN1_BMPSTRING:
  835. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  836. av->value.bmpstring->length);
  837. BIO_printf(out, "%s\n", value);
  838. OPENSSL_free(value);
  839. break;
  840. case V_ASN1_OCTET_STRING:
  841. hex_prin(out, av->value.octet_string->data,
  842. av->value.octet_string->length);
  843. BIO_printf(out, "\n");
  844. break;
  845. case V_ASN1_BIT_STRING:
  846. hex_prin(out, av->value.bit_string->data,
  847. av->value.bit_string->length);
  848. BIO_printf(out, "\n");
  849. break;
  850. default:
  851. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  852. break;
  853. }
  854. } else {
  855. BIO_printf(out, "<No Values>\n");
  856. }
  857. }
  858. return 1;
  859. }
  860. void hex_prin(BIO *out, unsigned char *buf, int len)
  861. {
  862. int i;
  863. for (i = 0; i < len; i++)
  864. BIO_printf(out, "%02X ", buf[i]);
  865. }
  866. static int set_pbe(int *ppbe, const char *str)
  867. {
  868. if (!str)
  869. return 0;
  870. if (strcmp(str, "NONE") == 0) {
  871. *ppbe = -1;
  872. return 1;
  873. }
  874. *ppbe = OBJ_txt2nid(str);
  875. if (*ppbe == NID_undef) {
  876. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  877. return 0;
  878. }
  879. return 1;
  880. }
  881. #endif