pkcs12.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * Copyright 1999-2024 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/asn1.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/pkcs12.h>
  20. #include <openssl/provider.h>
  21. #include <openssl/kdf.h>
  22. #include <openssl/rand.h>
  23. #define NOKEYS 0x1
  24. #define NOCERTS 0x2
  25. #define INFO 0x4
  26. #define CLCERTS 0x8
  27. #define CACERTS 0x10
  28. #define PASSWD_BUF_SIZE 2048
  29. #define WARN_EXPORT(opt) \
  30. BIO_printf(bio_err, "Warning: -%s option ignored with -export\n", opt);
  31. #define WARN_NO_EXPORT(opt) \
  32. BIO_printf(bio_err, "Warning: -%s option ignored without -export\n", opt);
  33. static int get_cert_chain(X509 *cert, X509_STORE *store,
  34. STACK_OF(X509) *untrusted_certs,
  35. STACK_OF(X509) **chain);
  36. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
  37. const char *pass, int passlen, int options,
  38. char *pempass, const EVP_CIPHER *enc);
  39. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  40. const char *pass, int passlen, int options,
  41. char *pempass, const EVP_CIPHER *enc);
  42. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
  43. const char *pass, int passlen,
  44. int options, char *pempass, const EVP_CIPHER *enc);
  45. void print_attribute(BIO *out, const ASN1_TYPE *av);
  46. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  47. const char *name);
  48. void hex_prin(BIO *out, unsigned char *buf, int len);
  49. static int alg_print(const X509_ALGOR *alg);
  50. int cert_load(BIO *in, STACK_OF(X509) *sk);
  51. static int set_pbe(int *ppbe, const char *str);
  52. static int jdk_trust(PKCS12_SAFEBAG *bag, void *cbarg);
  53. typedef enum OPTION_choice {
  54. OPT_COMMON,
  55. OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
  56. OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
  57. #ifndef OPENSSL_NO_DES
  58. OPT_DESCERT,
  59. #endif
  60. OPT_EXPORT, OPT_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER, OPT_MACSALTLEN,
  61. OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_NOENC, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
  62. OPT_INKEY, OPT_CERTFILE, OPT_UNTRUSTED, OPT_PASSCERTS,
  63. OPT_NAME, OPT_CSP, OPT_CANAME,
  64. OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
  65. OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
  66. OPT_R_ENUM, OPT_PROV_ENUM, OPT_JDKTRUST,
  67. #ifndef OPENSSL_NO_DES
  68. OPT_LEGACY_ALG
  69. #endif
  70. } OPTION_CHOICE;
  71. const OPTIONS pkcs12_options[] = {
  72. OPT_SECTION("General"),
  73. {"help", OPT_HELP, '-', "Display this summary"},
  74. {"in", OPT_IN, '<', "Input file"},
  75. {"out", OPT_OUT, '>', "Output file"},
  76. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  77. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  78. {"password", OPT_PASSWORD, 's', "Set PKCS#12 import/export password source"},
  79. {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
  80. {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
  81. {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
  82. {"noout", OPT_NOOUT, '-', "Don't output anything, just verify PKCS#12 input"},
  83. #ifndef OPENSSL_NO_DES
  84. {"legacy", OPT_LEGACY_ALG, '-',
  85. # ifdef OPENSSL_NO_RC2
  86. "Use legacy encryption algorithm 3DES_CBC for keys and certs"
  87. # else
  88. "Use legacy encryption: 3DES_CBC for keys, RC2_CBC for certs"
  89. # endif
  90. },
  91. #endif
  92. #ifndef OPENSSL_NO_ENGINE
  93. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  94. #endif
  95. OPT_PROV_OPTIONS,
  96. OPT_R_OPTIONS,
  97. OPT_SECTION("PKCS#12 import (parsing PKCS#12)"),
  98. {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
  99. {"nomacver", OPT_NOMACVER, '-', "Don't verify integrity MAC"},
  100. {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
  101. {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
  102. {"", OPT_CIPHER, '-', "Any supported cipher for output encryption"},
  103. {"noenc", OPT_NOENC, '-', "Don't encrypt private keys"},
  104. {"nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated"},
  105. OPT_SECTION("PKCS#12 output (export)"),
  106. {"export", OPT_EXPORT, '-', "Create PKCS12 file"},
  107. {"inkey", OPT_INKEY, 's', "Private key, else read from -in input file"},
  108. {"certfile", OPT_CERTFILE, '<', "Extra certificates for PKCS12 output"},
  109. {"passcerts", OPT_PASSCERTS, 's', "Certificate file pass phrase source"},
  110. {"chain", OPT_CHAIN, '-', "Build and add certificate chain for EE cert,"},
  111. {OPT_MORE_STR, 0, 0,
  112. "which is the 1st cert from -in matching the private key (if given)"},
  113. {"untrusted", OPT_UNTRUSTED, '<', "Untrusted certificates for chain building"},
  114. {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
  115. {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
  116. {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
  117. {"no-CAfile", OPT_NOCAFILE, '-',
  118. "Do not load the default certificates file"},
  119. {"no-CApath", OPT_NOCAPATH, '-',
  120. "Do not load certificates from the default certificates directory"},
  121. {"no-CAstore", OPT_NOCASTORE, '-',
  122. "Do not load certificates from the default certificates store"},
  123. {"name", OPT_NAME, 's', "Use name as friendly name"},
  124. {"caname", OPT_CANAME, 's',
  125. "Use name as CA friendly name (can be repeated)"},
  126. {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
  127. {"LMK", OPT_LMK, '-',
  128. "Add local machine keyset attribute to private key"},
  129. {"keyex", OPT_KEYEX, '-', "Set key type to MS key exchange"},
  130. {"keysig", OPT_KEYSIG, '-', "Set key type to MS key signature"},
  131. {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default AES-256 CBC)"},
  132. {"certpbe", OPT_CERTPBE, 's',
  133. "Certificate PBE algorithm (default PBES2 with PBKDF2 and AES-256 CBC)"},
  134. #ifndef OPENSSL_NO_DES
  135. {"descert", OPT_DESCERT, '-',
  136. "Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)"},
  137. #endif
  138. {"macalg", OPT_MACALG, 's',
  139. "Digest algorithm to use in MAC (default SHA256)"},
  140. {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption and MAC"},
  141. {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
  142. {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration)"},
  143. {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
  144. {"macsaltlen", OPT_MACSALTLEN, 'p', "Specify the salt len for MAC"},
  145. {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
  146. {"jdktrust", OPT_JDKTRUST, 's', "Mark certificate in PKCS#12 store as trusted for JDK compatibility"},
  147. {NULL}
  148. };
  149. int pkcs12_main(int argc, char **argv)
  150. {
  151. char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
  152. char *untrusted = NULL, *ciphername = NULL, *enc_name = NULL;
  153. char *passcertsarg = NULL, *passcerts = NULL;
  154. char *name = NULL, *csp_name = NULL;
  155. char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
  156. int export_pkcs12 = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
  157. char *jdktrust = NULL;
  158. #ifndef OPENSSL_NO_DES
  159. int use_legacy = 0;
  160. #endif
  161. /* use library defaults for the iter, maciter, cert, and key PBE */
  162. int iter = 0, maciter = 0;
  163. int macsaltlen = PKCS12_SALT_LEN;
  164. int cert_pbe = NID_undef;
  165. int key_pbe = NID_undef;
  166. int ret = 1, macver = 1, add_lmk = 0, private = 0;
  167. int noprompt = 0;
  168. char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
  169. char *passin = NULL, *passout = NULL, *macalg = NULL;
  170. char *cpass = NULL, *mpass = NULL, *badpass = NULL;
  171. const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
  172. int noCApath = 0, noCAfile = 0, noCAstore = 0;
  173. ENGINE *e = NULL;
  174. BIO *in = NULL, *out = NULL;
  175. PKCS12 *p12 = NULL;
  176. STACK_OF(OPENSSL_STRING) *canames = NULL;
  177. EVP_CIPHER *default_enc = (EVP_CIPHER *)EVP_aes_256_cbc();
  178. EVP_CIPHER *enc = (EVP_CIPHER *)default_enc;
  179. OPTION_CHOICE o;
  180. opt_set_unknown_name("cipher");
  181. prog = opt_init(argc, argv, pkcs12_options);
  182. while ((o = opt_next()) != OPT_EOF) {
  183. switch (o) {
  184. case OPT_EOF:
  185. case OPT_ERR:
  186. opthelp:
  187. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  188. goto end;
  189. case OPT_HELP:
  190. opt_help(pkcs12_options);
  191. ret = 0;
  192. goto end;
  193. case OPT_NOKEYS:
  194. options |= NOKEYS;
  195. break;
  196. case OPT_KEYEX:
  197. keytype = KEY_EX;
  198. break;
  199. case OPT_KEYSIG:
  200. keytype = KEY_SIG;
  201. break;
  202. case OPT_NOCERTS:
  203. options |= NOCERTS;
  204. break;
  205. case OPT_CLCERTS:
  206. options |= CLCERTS;
  207. break;
  208. case OPT_CACERTS:
  209. options |= CACERTS;
  210. break;
  211. case OPT_NOOUT:
  212. options |= (NOKEYS | NOCERTS);
  213. break;
  214. case OPT_JDKTRUST:
  215. jdktrust = opt_arg();
  216. /* Adding jdk trust implies nokeys */
  217. options |= NOKEYS;
  218. break;
  219. case OPT_INFO:
  220. options |= INFO;
  221. break;
  222. case OPT_CHAIN:
  223. chain = 1;
  224. break;
  225. case OPT_TWOPASS:
  226. twopass = 1;
  227. break;
  228. case OPT_NOMACVER:
  229. macver = 0;
  230. break;
  231. #ifndef OPENSSL_NO_DES
  232. case OPT_DESCERT:
  233. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  234. break;
  235. #endif
  236. case OPT_EXPORT:
  237. export_pkcs12 = 1;
  238. break;
  239. case OPT_NODES:
  240. case OPT_NOENC:
  241. /*
  242. * |enc_name| stores the name of the option used so it
  243. * can be printed if an error message is output.
  244. */
  245. enc_name = opt_flag() + 1;
  246. enc = NULL;
  247. ciphername = NULL;
  248. break;
  249. case OPT_CIPHER:
  250. enc_name = ciphername = opt_unknown();
  251. break;
  252. case OPT_ITER:
  253. maciter = iter = opt_int_arg();
  254. break;
  255. case OPT_NOITER:
  256. iter = 1;
  257. break;
  258. case OPT_MACITER:
  259. /* no-op */
  260. break;
  261. case OPT_NOMACITER:
  262. maciter = 1;
  263. break;
  264. case OPT_MACSALTLEN:
  265. macsaltlen = opt_int_arg();
  266. break;
  267. case OPT_NOMAC:
  268. cert_pbe = -1;
  269. maciter = -1;
  270. break;
  271. case OPT_MACALG:
  272. macalg = opt_arg();
  273. break;
  274. case OPT_CERTPBE:
  275. if (!set_pbe(&cert_pbe, opt_arg()))
  276. goto opthelp;
  277. break;
  278. case OPT_KEYPBE:
  279. if (!set_pbe(&key_pbe, opt_arg()))
  280. goto opthelp;
  281. break;
  282. case OPT_R_CASES:
  283. if (!opt_rand(o))
  284. goto end;
  285. break;
  286. case OPT_INKEY:
  287. keyname = opt_arg();
  288. break;
  289. case OPT_CERTFILE:
  290. certfile = opt_arg();
  291. break;
  292. case OPT_UNTRUSTED:
  293. untrusted = opt_arg();
  294. break;
  295. case OPT_PASSCERTS:
  296. passcertsarg = opt_arg();
  297. break;
  298. case OPT_NAME:
  299. name = opt_arg();
  300. break;
  301. case OPT_LMK:
  302. add_lmk = 1;
  303. break;
  304. case OPT_CSP:
  305. csp_name = opt_arg();
  306. break;
  307. case OPT_CANAME:
  308. if (canames == NULL
  309. && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
  310. goto end;
  311. sk_OPENSSL_STRING_push(canames, opt_arg());
  312. break;
  313. case OPT_IN:
  314. infile = opt_arg();
  315. break;
  316. case OPT_OUT:
  317. outfile = opt_arg();
  318. break;
  319. case OPT_PASSIN:
  320. passinarg = opt_arg();
  321. break;
  322. case OPT_PASSOUT:
  323. passoutarg = opt_arg();
  324. break;
  325. case OPT_PASSWORD:
  326. passarg = opt_arg();
  327. break;
  328. case OPT_CAPATH:
  329. CApath = opt_arg();
  330. break;
  331. case OPT_CASTORE:
  332. CAstore = opt_arg();
  333. break;
  334. case OPT_CAFILE:
  335. CAfile = opt_arg();
  336. break;
  337. case OPT_NOCAPATH:
  338. noCApath = 1;
  339. break;
  340. case OPT_NOCASTORE:
  341. noCAstore = 1;
  342. break;
  343. case OPT_NOCAFILE:
  344. noCAfile = 1;
  345. break;
  346. case OPT_ENGINE:
  347. e = setup_engine(opt_arg(), 0);
  348. break;
  349. #ifndef OPENSSL_NO_DES
  350. case OPT_LEGACY_ALG:
  351. use_legacy = 1;
  352. break;
  353. #endif
  354. case OPT_PROV_CASES:
  355. if (!opt_provider(o))
  356. goto end;
  357. break;
  358. }
  359. }
  360. /* No extra arguments. */
  361. if (!opt_check_rest_arg(NULL))
  362. goto opthelp;
  363. if (!app_RAND_load())
  364. goto end;
  365. if (!opt_cipher_any(ciphername, &enc))
  366. goto opthelp;
  367. if (export_pkcs12) {
  368. if ((options & INFO) != 0)
  369. WARN_EXPORT("info");
  370. if (macver == 0)
  371. WARN_EXPORT("nomacver");
  372. if ((options & CLCERTS) != 0)
  373. WARN_EXPORT("clcerts");
  374. if ((options & CACERTS) != 0)
  375. WARN_EXPORT("cacerts");
  376. if (enc != default_enc)
  377. BIO_printf(bio_err,
  378. "Warning: output encryption option -%s ignored with -export\n", enc_name);
  379. } else {
  380. if (keyname != NULL)
  381. WARN_NO_EXPORT("inkey");
  382. if (certfile != NULL)
  383. WARN_NO_EXPORT("certfile");
  384. if (passcertsarg != NULL)
  385. WARN_NO_EXPORT("passcerts");
  386. if (chain)
  387. WARN_NO_EXPORT("chain");
  388. if (untrusted != NULL)
  389. WARN_NO_EXPORT("untrusted");
  390. if (CAfile != NULL)
  391. WARN_NO_EXPORT("CAfile");
  392. if (CApath != NULL)
  393. WARN_NO_EXPORT("CApath");
  394. if (CAstore != NULL)
  395. WARN_NO_EXPORT("CAstore");
  396. if (noCAfile)
  397. WARN_NO_EXPORT("no-CAfile");
  398. if (noCApath)
  399. WARN_NO_EXPORT("no-CApath");
  400. if (noCAstore)
  401. WARN_NO_EXPORT("no-CAstore");
  402. if (name != NULL)
  403. WARN_NO_EXPORT("name");
  404. if (canames != NULL)
  405. WARN_NO_EXPORT("caname");
  406. if (csp_name != NULL)
  407. WARN_NO_EXPORT("CSP");
  408. if (add_lmk)
  409. WARN_NO_EXPORT("LMK");
  410. if (keytype == KEY_EX)
  411. WARN_NO_EXPORT("keyex");
  412. if (keytype == KEY_SIG)
  413. WARN_NO_EXPORT("keysig");
  414. if (key_pbe != NID_undef)
  415. WARN_NO_EXPORT("keypbe");
  416. if (cert_pbe != NID_undef && cert_pbe != -1)
  417. WARN_NO_EXPORT("certpbe and -descert");
  418. if (macalg != NULL)
  419. WARN_NO_EXPORT("macalg");
  420. if (iter != 0)
  421. WARN_NO_EXPORT("iter and -noiter");
  422. if (maciter == 1)
  423. WARN_NO_EXPORT("nomaciter");
  424. if (cert_pbe == -1 && maciter == -1)
  425. WARN_NO_EXPORT("nomac");
  426. if (macsaltlen != PKCS12_SALT_LEN)
  427. WARN_NO_EXPORT("macsaltlen");
  428. }
  429. #ifndef OPENSSL_NO_DES
  430. if (use_legacy) {
  431. /* load the legacy provider if not loaded already*/
  432. if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
  433. if (!app_provider_load(app_get0_libctx(), "legacy"))
  434. goto end;
  435. /* load the default provider explicitly */
  436. if (!app_provider_load(app_get0_libctx(), "default"))
  437. goto end;
  438. }
  439. if (cert_pbe == NID_undef) {
  440. /* Adapt default algorithm */
  441. # ifndef OPENSSL_NO_RC2
  442. cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
  443. # else
  444. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  445. # endif
  446. }
  447. if (key_pbe == NID_undef)
  448. key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  449. if (enc == default_enc)
  450. enc = (EVP_CIPHER *)EVP_des_ede3_cbc();
  451. if (macalg == NULL)
  452. macalg = "sha1";
  453. }
  454. #endif
  455. private = 1;
  456. if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
  457. BIO_printf(bio_err, "Error getting certificate file password\n");
  458. goto end;
  459. }
  460. if (passarg != NULL) {
  461. if (export_pkcs12)
  462. passoutarg = passarg;
  463. else
  464. passinarg = passarg;
  465. }
  466. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  467. BIO_printf(bio_err, "Error getting passwords\n");
  468. goto end;
  469. }
  470. if (cpass == NULL) {
  471. if (export_pkcs12)
  472. cpass = passout;
  473. else
  474. cpass = passin;
  475. }
  476. if (cpass != NULL) {
  477. mpass = cpass;
  478. noprompt = 1;
  479. if (twopass) {
  480. if (export_pkcs12)
  481. BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
  482. else
  483. BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
  484. goto end;
  485. }
  486. } else {
  487. cpass = pass;
  488. mpass = macpass;
  489. }
  490. if (twopass) {
  491. /* To avoid bit rot */
  492. if (1) {
  493. #ifndef OPENSSL_NO_UI_CONSOLE
  494. if (EVP_read_pw_string(
  495. macpass, sizeof(macpass), "Enter MAC Password:", export_pkcs12)) {
  496. BIO_printf(bio_err, "Can't read Password\n");
  497. goto end;
  498. }
  499. } else {
  500. #endif
  501. BIO_printf(bio_err, "Unsupported option -twopass\n");
  502. goto end;
  503. }
  504. }
  505. if (export_pkcs12) {
  506. EVP_PKEY *key = NULL;
  507. X509 *ee_cert = NULL, *x = NULL;
  508. STACK_OF(X509) *certs = NULL;
  509. STACK_OF(X509) *untrusted_certs = NULL;
  510. EVP_MD *macmd = NULL;
  511. unsigned char *catmp = NULL;
  512. int i;
  513. ASN1_OBJECT *obj = NULL;
  514. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  515. BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
  516. goto export_end;
  517. }
  518. if ((options & NOCERTS) != 0) {
  519. chain = 0;
  520. BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
  521. }
  522. if (!(options & NOKEYS)) {
  523. key = load_key(keyname ? keyname : infile,
  524. FORMAT_PEM, 1, passin, e,
  525. keyname ?
  526. "private key from -inkey file" :
  527. "private key from -in file");
  528. if (key == NULL)
  529. goto export_end;
  530. }
  531. /* Load all certs in input file */
  532. if (!(options & NOCERTS)) {
  533. if (!load_certs(infile, 1, &certs, passin,
  534. "certificates from -in file"))
  535. goto export_end;
  536. if (sk_X509_num(certs) < 1) {
  537. BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
  538. goto export_end;
  539. }
  540. if (key != NULL) {
  541. /* Look for matching private key */
  542. for (i = 0; i < sk_X509_num(certs); i++) {
  543. x = sk_X509_value(certs, i);
  544. if (cert_matches_key(x, key)) {
  545. ee_cert = x;
  546. /* Zero keyid and alias */
  547. X509_keyid_set1(ee_cert, NULL, 0);
  548. X509_alias_set1(ee_cert, NULL, 0);
  549. /* Remove from list */
  550. (void)sk_X509_delete(certs, i);
  551. break;
  552. }
  553. }
  554. if (ee_cert == NULL) {
  555. BIO_printf(bio_err,
  556. "No cert in -in file '%s' matches private key\n",
  557. infile);
  558. goto export_end;
  559. }
  560. }
  561. }
  562. /* Load any untrusted certificates for chain building */
  563. if (untrusted != NULL) {
  564. if (!load_certs(untrusted, 0, &untrusted_certs, passcerts,
  565. "untrusted certificates"))
  566. goto export_end;
  567. }
  568. /* If chaining get chain from end entity cert */
  569. if (chain) {
  570. int vret;
  571. STACK_OF(X509) *chain2;
  572. X509_STORE *store;
  573. X509 *ee_cert_tmp = ee_cert;
  574. /* Assume the first cert if we haven't got anything else */
  575. if (ee_cert_tmp == NULL && certs != NULL)
  576. ee_cert_tmp = sk_X509_value(certs, 0);
  577. if (ee_cert_tmp == NULL) {
  578. BIO_printf(bio_err,
  579. "No end entity certificate to check with -chain\n");
  580. goto export_end;
  581. }
  582. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  583. CAstore, noCAstore))
  584. == NULL)
  585. goto export_end;
  586. vret = get_cert_chain(ee_cert_tmp, store, untrusted_certs, &chain2);
  587. X509_STORE_free(store);
  588. if (vret == X509_V_OK) {
  589. int add_certs;
  590. /* Remove from chain2 the first (end entity) certificate */
  591. X509_free(sk_X509_shift(chain2));
  592. /* Add the remaining certs (except for duplicates) */
  593. add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
  594. | X509_ADD_FLAG_NO_DUP);
  595. OSSL_STACK_OF_X509_free(chain2);
  596. if (!add_certs)
  597. goto export_end;
  598. } else {
  599. if (vret != X509_V_ERR_UNSPECIFIED)
  600. BIO_printf(bio_err, "Error getting chain: %s\n",
  601. X509_verify_cert_error_string(vret));
  602. goto export_end;
  603. }
  604. }
  605. /* Add any extra certificates asked for */
  606. if (certfile != NULL) {
  607. if (!load_certs(certfile, 0, &certs, passcerts,
  608. "extra certificates from -certfile"))
  609. goto export_end;
  610. }
  611. /* Add any CA names */
  612. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  613. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  614. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  615. }
  616. if (csp_name != NULL && key != NULL)
  617. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  618. MBSTRING_ASC, (unsigned char *)csp_name,
  619. -1);
  620. if (add_lmk && key != NULL)
  621. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  622. if (!noprompt && !(enc == NULL && maciter == -1)) {
  623. /* To avoid bit rot */
  624. if (1) {
  625. #ifndef OPENSSL_NO_UI_CONSOLE
  626. if (EVP_read_pw_string(pass, sizeof(pass),
  627. "Enter Export Password:", 1)) {
  628. BIO_printf(bio_err, "Can't read Password\n");
  629. goto export_end;
  630. }
  631. } else {
  632. #endif
  633. BIO_printf(bio_err, "Password required\n");
  634. goto export_end;
  635. }
  636. }
  637. if (!twopass)
  638. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  639. if (jdktrust != NULL) {
  640. obj = OBJ_txt2obj(jdktrust, 0);
  641. }
  642. p12 = PKCS12_create_ex2(cpass, name, key, ee_cert, certs,
  643. key_pbe, cert_pbe, iter, -1, keytype,
  644. app_get0_libctx(), app_get0_propq(),
  645. jdk_trust, (void*)obj);
  646. if (p12 == NULL) {
  647. BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
  648. outfile);
  649. goto export_end;
  650. }
  651. if (macalg != NULL) {
  652. if (!opt_md(macalg, &macmd))
  653. goto opthelp;
  654. }
  655. if (maciter != -1) {
  656. if (!PKCS12_set_mac(p12, mpass, -1, NULL, macsaltlen, maciter, macmd)) {
  657. BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
  658. BIO_printf(bio_err, "Use -nomac if MAC not required and PKCS12KDF support not available.\n");
  659. goto export_end;
  660. }
  661. }
  662. assert(private);
  663. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  664. if (out == NULL)
  665. goto end;
  666. i2d_PKCS12_bio(out, p12);
  667. ret = 0;
  668. export_end:
  669. EVP_PKEY_free(key);
  670. EVP_MD_free(macmd);
  671. OSSL_STACK_OF_X509_free(certs);
  672. OSSL_STACK_OF_X509_free(untrusted_certs);
  673. X509_free(ee_cert);
  674. ASN1_OBJECT_free(obj);
  675. ERR_print_errors(bio_err);
  676. goto end;
  677. }
  678. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  679. if (in == NULL)
  680. goto end;
  681. out = bio_open_owner(outfile, FORMAT_PEM, private);
  682. if (out == NULL)
  683. goto end;
  684. p12 = PKCS12_init_ex(NID_pkcs7_data, app_get0_libctx(), app_get0_propq());
  685. if (p12 == NULL) {
  686. ERR_print_errors(bio_err);
  687. goto end;
  688. }
  689. if ((p12 = d2i_PKCS12_bio(in, &p12)) == NULL) {
  690. ERR_print_errors(bio_err);
  691. goto end;
  692. }
  693. if (!noprompt) {
  694. if (1) {
  695. #ifndef OPENSSL_NO_UI_CONSOLE
  696. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
  697. 0)) {
  698. BIO_printf(bio_err, "Can't read Password\n");
  699. goto end;
  700. }
  701. } else {
  702. #endif
  703. BIO_printf(bio_err, "Password required\n");
  704. goto end;
  705. }
  706. }
  707. if (!twopass)
  708. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  709. if ((options & INFO) && PKCS12_mac_present(p12)) {
  710. const ASN1_INTEGER *tmaciter;
  711. const X509_ALGOR *macalgid;
  712. const ASN1_OBJECT *macobj;
  713. const ASN1_OCTET_STRING *tmac;
  714. const ASN1_OCTET_STRING *tsalt;
  715. PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
  716. /* current hash algorithms do not use parameters so extract just name,
  717. in future alg_print() may be needed */
  718. X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
  719. BIO_puts(bio_err, "MAC: ");
  720. i2a_ASN1_OBJECT(bio_err, macobj);
  721. BIO_printf(bio_err, ", Iteration %ld\n",
  722. tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
  723. BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
  724. tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
  725. tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
  726. }
  727. if (macver) {
  728. EVP_KDF *pkcs12kdf;
  729. pkcs12kdf = EVP_KDF_fetch(app_get0_libctx(), "PKCS12KDF",
  730. app_get0_propq());
  731. if (pkcs12kdf == NULL) {
  732. BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
  733. BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
  734. goto end;
  735. }
  736. EVP_KDF_free(pkcs12kdf);
  737. /* If we enter empty password try no password first */
  738. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  739. /* If mac and crypto pass the same set it to NULL too */
  740. if (!twopass)
  741. cpass = NULL;
  742. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  743. /*
  744. * May be UTF8 from previous version of OpenSSL:
  745. * convert to a UTF8 form which will translate
  746. * to the same Unicode password.
  747. */
  748. unsigned char *utmp;
  749. int utmplen;
  750. unsigned long err = ERR_peek_error();
  751. if (ERR_GET_LIB(err) == ERR_LIB_PKCS12
  752. && ERR_GET_REASON(err) == PKCS12_R_MAC_ABSENT) {
  753. BIO_printf(bio_err, "Warning: MAC is absent!\n");
  754. goto dump;
  755. }
  756. utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
  757. if (utmp == NULL)
  758. goto end;
  759. badpass = OPENSSL_uni2utf8(utmp, utmplen);
  760. OPENSSL_free(utmp);
  761. if (!PKCS12_verify_mac(p12, badpass, -1)) {
  762. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  763. ERR_print_errors(bio_err);
  764. goto end;
  765. } else {
  766. BIO_printf(bio_err, "Warning: using broken algorithm\n");
  767. if (!twopass)
  768. cpass = badpass;
  769. }
  770. }
  771. }
  772. dump:
  773. assert(private);
  774. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  775. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  776. ERR_print_errors(bio_err);
  777. goto end;
  778. }
  779. ret = 0;
  780. end:
  781. PKCS12_free(p12);
  782. release_engine(e);
  783. BIO_free(in);
  784. BIO_free_all(out);
  785. sk_OPENSSL_STRING_free(canames);
  786. OPENSSL_free(badpass);
  787. OPENSSL_free(passcerts);
  788. OPENSSL_free(passin);
  789. OPENSSL_free(passout);
  790. return ret;
  791. }
  792. static int jdk_trust(PKCS12_SAFEBAG *bag, void *cbarg)
  793. {
  794. STACK_OF(X509_ATTRIBUTE) *attrs = NULL;
  795. X509_ATTRIBUTE *attr = NULL;
  796. /* Nothing to do */
  797. if (cbarg == NULL)
  798. return 1;
  799. /* Get the current attrs */
  800. attrs = (STACK_OF(X509_ATTRIBUTE)*)PKCS12_SAFEBAG_get0_attrs(bag);
  801. /* Create a new attr for the JDK Trusted Usage and add it */
  802. attr = X509_ATTRIBUTE_create(NID_oracle_jdk_trustedkeyusage, V_ASN1_OBJECT, (ASN1_OBJECT*)cbarg);
  803. /* Add the new attr, if attrs is NULL, it'll be initialised */
  804. X509at_add1_attr(&attrs, attr);
  805. /* Set the bag attrs */
  806. PKCS12_SAFEBAG_set0_attrs(bag, attrs);
  807. X509_ATTRIBUTE_free(attr);
  808. return 1;
  809. }
  810. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  811. int passlen, int options, char *pempass,
  812. const EVP_CIPHER *enc)
  813. {
  814. STACK_OF(PKCS7) *asafes = NULL;
  815. int i, bagnid;
  816. int ret = 0;
  817. PKCS7 *p7;
  818. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  819. return 0;
  820. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  821. STACK_OF(PKCS12_SAFEBAG) *bags;
  822. p7 = sk_PKCS7_value(asafes, i);
  823. bagnid = OBJ_obj2nid(p7->type);
  824. if (bagnid == NID_pkcs7_data) {
  825. bags = PKCS12_unpack_p7data(p7);
  826. if (options & INFO)
  827. BIO_printf(bio_err, "PKCS7 Data\n");
  828. } else if (bagnid == NID_pkcs7_encrypted) {
  829. if (options & INFO) {
  830. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  831. if (p7->d.encrypted == NULL) {
  832. BIO_printf(bio_err, "<no data>\n");
  833. } else {
  834. alg_print(p7->d.encrypted->enc_data->algorithm);
  835. }
  836. }
  837. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  838. } else {
  839. continue;
  840. }
  841. if (bags == NULL)
  842. goto err;
  843. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  844. options, pempass, enc)) {
  845. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  846. goto err;
  847. }
  848. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  849. }
  850. ret = 1;
  851. err:
  852. sk_PKCS7_pop_free(asafes, PKCS7_free);
  853. return ret;
  854. }
  855. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  856. const char *pass, int passlen, int options,
  857. char *pempass, const EVP_CIPHER *enc)
  858. {
  859. int i;
  860. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  861. if (!dump_certs_pkeys_bag(out,
  862. sk_PKCS12_SAFEBAG_value(bags, i),
  863. pass, passlen, options, pempass, enc))
  864. return 0;
  865. }
  866. return 1;
  867. }
  868. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  869. const char *pass, int passlen, int options,
  870. char *pempass, const EVP_CIPHER *enc)
  871. {
  872. EVP_PKEY *pkey;
  873. PKCS8_PRIV_KEY_INFO *p8;
  874. const PKCS8_PRIV_KEY_INFO *p8c;
  875. X509 *x509;
  876. const STACK_OF(X509_ATTRIBUTE) *attrs;
  877. int ret = 0;
  878. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  879. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  880. case NID_keyBag:
  881. if (options & INFO)
  882. BIO_printf(bio_err, "Key bag\n");
  883. if (options & NOKEYS)
  884. return 1;
  885. print_attribs(out, attrs, "Bag Attributes");
  886. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  887. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  888. return 0;
  889. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  890. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  891. EVP_PKEY_free(pkey);
  892. break;
  893. case NID_pkcs8ShroudedKeyBag:
  894. if (options & INFO) {
  895. const X509_SIG *tp8;
  896. const X509_ALGOR *tp8alg;
  897. BIO_printf(bio_err, "Shrouded Keybag: ");
  898. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  899. X509_SIG_get0(tp8, &tp8alg, NULL);
  900. alg_print(tp8alg);
  901. }
  902. if (options & NOKEYS)
  903. return 1;
  904. print_attribs(out, attrs, "Bag Attributes");
  905. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  906. return 0;
  907. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  908. PKCS8_PRIV_KEY_INFO_free(p8);
  909. return 0;
  910. }
  911. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  912. PKCS8_PRIV_KEY_INFO_free(p8);
  913. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  914. EVP_PKEY_free(pkey);
  915. break;
  916. case NID_certBag:
  917. if (options & INFO)
  918. BIO_printf(bio_err, "Certificate bag\n");
  919. if (options & NOCERTS)
  920. return 1;
  921. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  922. if (options & CACERTS)
  923. return 1;
  924. } else if (options & CLCERTS)
  925. return 1;
  926. print_attribs(out, attrs, "Bag Attributes");
  927. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  928. return 1;
  929. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  930. return 0;
  931. dump_cert_text(out, x509);
  932. ret = PEM_write_bio_X509(out, x509);
  933. X509_free(x509);
  934. break;
  935. case NID_secretBag:
  936. if (options & INFO)
  937. BIO_printf(bio_err, "Secret bag\n");
  938. print_attribs(out, attrs, "Bag Attributes");
  939. BIO_printf(bio_err, "Bag Type: ");
  940. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
  941. BIO_printf(bio_err, "\nBag Value: ");
  942. print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
  943. return 1;
  944. case NID_safeContentsBag:
  945. if (options & INFO)
  946. BIO_printf(bio_err, "Safe Contents bag\n");
  947. print_attribs(out, attrs, "Bag Attributes");
  948. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  949. pass, passlen, options, pempass, enc);
  950. default:
  951. BIO_printf(bio_err, "Warning unsupported bag type: ");
  952. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  953. BIO_printf(bio_err, "\n");
  954. return 1;
  955. }
  956. return ret;
  957. }
  958. /* Given a single certificate return a verified chain or NULL if error */
  959. static int get_cert_chain(X509 *cert, X509_STORE *store,
  960. STACK_OF(X509) *untrusted_certs,
  961. STACK_OF(X509) **chain)
  962. {
  963. X509_STORE_CTX *store_ctx = NULL;
  964. STACK_OF(X509) *chn = NULL;
  965. int i = 0;
  966. store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
  967. if (store_ctx == NULL) {
  968. i = X509_V_ERR_UNSPECIFIED;
  969. goto end;
  970. }
  971. if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
  972. i = X509_V_ERR_UNSPECIFIED;
  973. goto end;
  974. }
  975. if (X509_verify_cert(store_ctx) > 0)
  976. chn = X509_STORE_CTX_get1_chain(store_ctx);
  977. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  978. i = X509_V_ERR_UNSPECIFIED;
  979. end:
  980. X509_STORE_CTX_free(store_ctx);
  981. *chain = chn;
  982. return i;
  983. }
  984. static int alg_print(const X509_ALGOR *alg)
  985. {
  986. int pbenid, aparamtype;
  987. const ASN1_OBJECT *aoid;
  988. const void *aparam;
  989. PBEPARAM *pbe = NULL;
  990. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  991. pbenid = OBJ_obj2nid(aoid);
  992. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  993. /*
  994. * If PBE algorithm is PBES2 decode algorithm parameters
  995. * for additional details.
  996. */
  997. if (pbenid == NID_pbes2) {
  998. PBE2PARAM *pbe2 = NULL;
  999. int encnid;
  1000. if (aparamtype == V_ASN1_SEQUENCE)
  1001. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  1002. if (pbe2 == NULL) {
  1003. BIO_puts(bio_err, ", <unsupported parameters>");
  1004. goto done;
  1005. }
  1006. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  1007. pbenid = OBJ_obj2nid(aoid);
  1008. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  1009. encnid = OBJ_obj2nid(aoid);
  1010. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  1011. OBJ_nid2sn(encnid));
  1012. /* If KDF is PBKDF2 decode parameters */
  1013. if (pbenid == NID_id_pbkdf2) {
  1014. PBKDF2PARAM *kdf = NULL;
  1015. int prfnid;
  1016. if (aparamtype == V_ASN1_SEQUENCE)
  1017. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  1018. if (kdf == NULL) {
  1019. BIO_puts(bio_err, ", <unsupported parameters>");
  1020. goto done;
  1021. }
  1022. if (kdf->prf == NULL) {
  1023. prfnid = NID_hmacWithSHA1;
  1024. } else {
  1025. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  1026. prfnid = OBJ_obj2nid(aoid);
  1027. }
  1028. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  1029. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  1030. PBKDF2PARAM_free(kdf);
  1031. #ifndef OPENSSL_NO_SCRYPT
  1032. } else if (pbenid == NID_id_scrypt) {
  1033. SCRYPT_PARAMS *kdf = NULL;
  1034. if (aparamtype == V_ASN1_SEQUENCE)
  1035. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  1036. if (kdf == NULL) {
  1037. BIO_puts(bio_err, ", <unsupported parameters>");
  1038. goto done;
  1039. }
  1040. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  1041. "Block size(r): %ld, Parallelism(p): %ld",
  1042. ASN1_STRING_length(kdf->salt),
  1043. ASN1_INTEGER_get(kdf->costParameter),
  1044. ASN1_INTEGER_get(kdf->blockSize),
  1045. ASN1_INTEGER_get(kdf->parallelizationParameter));
  1046. SCRYPT_PARAMS_free(kdf);
  1047. #endif
  1048. }
  1049. PBE2PARAM_free(pbe2);
  1050. } else {
  1051. if (aparamtype == V_ASN1_SEQUENCE)
  1052. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  1053. if (pbe == NULL) {
  1054. BIO_puts(bio_err, ", <unsupported parameters>");
  1055. goto done;
  1056. }
  1057. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  1058. PBEPARAM_free(pbe);
  1059. }
  1060. done:
  1061. BIO_puts(bio_err, "\n");
  1062. return 1;
  1063. }
  1064. /* Load all certificates from a given file */
  1065. int cert_load(BIO *in, STACK_OF(X509) *sk)
  1066. {
  1067. int ret = 0;
  1068. X509 *cert;
  1069. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  1070. ret = 1;
  1071. if (!sk_X509_push(sk, cert))
  1072. return 0;
  1073. }
  1074. if (ret)
  1075. ERR_clear_error();
  1076. return ret;
  1077. }
  1078. /* Generalised x509 attribute value print */
  1079. void print_attribute(BIO *out, const ASN1_TYPE *av)
  1080. {
  1081. char *value;
  1082. const char *ln;
  1083. char objbuf[80];
  1084. switch (av->type) {
  1085. case V_ASN1_BMPSTRING:
  1086. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  1087. av->value.bmpstring->length);
  1088. BIO_printf(out, "%s\n", value);
  1089. OPENSSL_free(value);
  1090. break;
  1091. case V_ASN1_UTF8STRING:
  1092. BIO_printf(out, "%.*s\n", av->value.utf8string->length,
  1093. av->value.utf8string->data);
  1094. break;
  1095. case V_ASN1_OCTET_STRING:
  1096. hex_prin(out, av->value.octet_string->data,
  1097. av->value.octet_string->length);
  1098. BIO_printf(out, "\n");
  1099. break;
  1100. case V_ASN1_BIT_STRING:
  1101. hex_prin(out, av->value.bit_string->data,
  1102. av->value.bit_string->length);
  1103. BIO_printf(out, "\n");
  1104. break;
  1105. case V_ASN1_OBJECT:
  1106. ln = OBJ_nid2ln(OBJ_obj2nid(av->value.object));
  1107. if (!ln)
  1108. ln = "";
  1109. OBJ_obj2txt(objbuf, sizeof(objbuf), av->value.object, 1);
  1110. BIO_printf(out, "%s (%s)", ln, objbuf);
  1111. BIO_printf(out, "\n");
  1112. break;
  1113. default:
  1114. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  1115. break;
  1116. }
  1117. }
  1118. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  1119. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  1120. const char *name)
  1121. {
  1122. X509_ATTRIBUTE *attr;
  1123. ASN1_TYPE *av;
  1124. int i, j, attr_nid;
  1125. if (!attrlst) {
  1126. BIO_printf(out, "%s: <No Attributes>\n", name);
  1127. return 1;
  1128. }
  1129. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  1130. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  1131. return 1;
  1132. }
  1133. BIO_printf(out, "%s\n", name);
  1134. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  1135. ASN1_OBJECT *attr_obj;
  1136. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  1137. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  1138. attr_nid = OBJ_obj2nid(attr_obj);
  1139. BIO_printf(out, " ");
  1140. if (attr_nid == NID_undef) {
  1141. i2a_ASN1_OBJECT(out, attr_obj);
  1142. BIO_printf(out, ": ");
  1143. } else {
  1144. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  1145. }
  1146. if (X509_ATTRIBUTE_count(attr)) {
  1147. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
  1148. {
  1149. av = X509_ATTRIBUTE_get0_type(attr, j);
  1150. print_attribute(out, av);
  1151. }
  1152. } else {
  1153. BIO_printf(out, "<No Values>\n");
  1154. }
  1155. }
  1156. return 1;
  1157. }
  1158. void hex_prin(BIO *out, unsigned char *buf, int len)
  1159. {
  1160. int i;
  1161. for (i = 0; i < len; i++)
  1162. BIO_printf(out, "%02X ", buf[i]);
  1163. }
  1164. static int set_pbe(int *ppbe, const char *str)
  1165. {
  1166. if (!str)
  1167. return 0;
  1168. if (strcmp(str, "NONE") == 0) {
  1169. *ppbe = -1;
  1170. return 1;
  1171. }
  1172. *ppbe = OBJ_txt2nid(str);
  1173. if (*ppbe == NID_undef) {
  1174. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  1175. return 0;
  1176. }
  1177. return 1;
  1178. }