pkcs12.c 40 KB

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