pkcs12.c 32 KB

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