pkcs12.c 31 KB

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