pkcs12.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*
  2. * Copyright 1999-2021 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 SHA1)"},
  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_name = 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. opt_set_unknown_name("cipher");
  174. prog = opt_init(argc, argv, pkcs12_options);
  175. while ((o = opt_next()) != OPT_EOF) {
  176. switch (o) {
  177. case OPT_EOF:
  178. case OPT_ERR:
  179. opthelp:
  180. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  181. goto end;
  182. case OPT_HELP:
  183. opt_help(pkcs12_options);
  184. ret = 0;
  185. goto end;
  186. case OPT_NOKEYS:
  187. options |= NOKEYS;
  188. break;
  189. case OPT_KEYEX:
  190. keytype = KEY_EX;
  191. break;
  192. case OPT_KEYSIG:
  193. keytype = KEY_SIG;
  194. break;
  195. case OPT_NOCERTS:
  196. options |= NOCERTS;
  197. break;
  198. case OPT_CLCERTS:
  199. options |= CLCERTS;
  200. break;
  201. case OPT_CACERTS:
  202. options |= CACERTS;
  203. break;
  204. case OPT_NOOUT:
  205. options |= (NOKEYS | NOCERTS);
  206. break;
  207. case OPT_INFO:
  208. options |= INFO;
  209. break;
  210. case OPT_CHAIN:
  211. chain = 1;
  212. break;
  213. case OPT_TWOPASS:
  214. twopass = 1;
  215. break;
  216. case OPT_NOMACVER:
  217. macver = 0;
  218. break;
  219. #ifndef OPENSSL_NO_DES
  220. case OPT_DESCERT:
  221. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  222. break;
  223. #endif
  224. case OPT_EXPORT:
  225. export_pkcs12 = 1;
  226. break;
  227. case OPT_NODES:
  228. case OPT_NOENC:
  229. /*
  230. * |enc_name| stores the name of the option used so it
  231. * can be printed if an error message is output.
  232. */
  233. enc_name = opt_flag() + 1;
  234. enc = NULL;
  235. ciphername = NULL;
  236. break;
  237. case OPT_CIPHER:
  238. enc_name = ciphername = 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. if (!opt_check_rest_arg(NULL))
  347. goto opthelp;
  348. if (!app_RAND_load())
  349. goto end;
  350. if (!opt_cipher_any(ciphername, &enc))
  351. goto opthelp;
  352. if (export_pkcs12) {
  353. if ((options & INFO) != 0)
  354. WARN_EXPORT("info");
  355. if (macver == 0)
  356. WARN_EXPORT("nomacver");
  357. if ((options & CLCERTS) != 0)
  358. WARN_EXPORT("clcerts");
  359. if ((options & CACERTS) != 0)
  360. WARN_EXPORT("cacerts");
  361. if (enc != default_enc)
  362. BIO_printf(bio_err,
  363. "Warning: output encryption option -%s ignored with -export\n", enc_name);
  364. } else {
  365. if (keyname != NULL)
  366. WARN_NO_EXPORT("inkey");
  367. if (certfile != NULL)
  368. WARN_NO_EXPORT("certfile");
  369. if (passcertsarg != NULL)
  370. WARN_NO_EXPORT("passcerts");
  371. if (chain)
  372. WARN_NO_EXPORT("chain");
  373. if (untrusted != NULL)
  374. WARN_NO_EXPORT("untrusted");
  375. if (CAfile != NULL)
  376. WARN_NO_EXPORT("CAfile");
  377. if (CApath != NULL)
  378. WARN_NO_EXPORT("CApath");
  379. if (CAstore != NULL)
  380. WARN_NO_EXPORT("CAstore");
  381. if (noCAfile)
  382. WARN_NO_EXPORT("no-CAfile");
  383. if (noCApath)
  384. WARN_NO_EXPORT("no-CApath");
  385. if (noCAstore)
  386. WARN_NO_EXPORT("no-CAstore");
  387. if (name != NULL)
  388. WARN_NO_EXPORT("name");
  389. if (canames != NULL)
  390. WARN_NO_EXPORT("caname");
  391. if (csp_name != NULL)
  392. WARN_NO_EXPORT("CSP");
  393. if (add_lmk)
  394. WARN_NO_EXPORT("LMK");
  395. if (keytype == KEY_EX)
  396. WARN_NO_EXPORT("keyex");
  397. if (keytype == KEY_SIG)
  398. WARN_NO_EXPORT("keysig");
  399. if (key_pbe != NID_undef)
  400. WARN_NO_EXPORT("keypbe");
  401. if (cert_pbe != NID_undef && cert_pbe != -1)
  402. WARN_NO_EXPORT("certpbe and -descert");
  403. if (macalg != NULL)
  404. WARN_NO_EXPORT("macalg");
  405. if (iter != 0)
  406. WARN_NO_EXPORT("iter and -noiter");
  407. if (maciter == 1)
  408. WARN_NO_EXPORT("nomaciter");
  409. if (cert_pbe == -1 && maciter == -1)
  410. WARN_NO_EXPORT("nomac");
  411. }
  412. #ifndef OPENSSL_NO_DES
  413. if (use_legacy) {
  414. /* load the legacy provider if not loaded already*/
  415. if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
  416. if (!app_provider_load(app_get0_libctx(), "legacy"))
  417. goto end;
  418. /* load the default provider explicitly */
  419. if (!app_provider_load(app_get0_libctx(), "default"))
  420. goto end;
  421. }
  422. if (cert_pbe == NID_undef) {
  423. /* Adapt default algorithm */
  424. # ifndef OPENSSL_NO_RC2
  425. cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
  426. # else
  427. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  428. # endif
  429. }
  430. if (key_pbe == NID_undef)
  431. key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  432. if (enc == default_enc)
  433. enc = (EVP_CIPHER *)EVP_des_ede3_cbc();
  434. if (macalg == NULL)
  435. macalg = "sha1";
  436. }
  437. #endif
  438. private = 1;
  439. if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
  440. BIO_printf(bio_err, "Error getting certificate file password\n");
  441. goto end;
  442. }
  443. if (passarg != NULL) {
  444. if (export_pkcs12)
  445. passoutarg = passarg;
  446. else
  447. passinarg = passarg;
  448. }
  449. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  450. BIO_printf(bio_err, "Error getting passwords\n");
  451. goto end;
  452. }
  453. if (cpass == NULL) {
  454. if (export_pkcs12)
  455. cpass = passout;
  456. else
  457. cpass = passin;
  458. }
  459. if (cpass != NULL) {
  460. mpass = cpass;
  461. noprompt = 1;
  462. if (twopass) {
  463. if (export_pkcs12)
  464. BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
  465. else
  466. BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
  467. goto end;
  468. }
  469. } else {
  470. cpass = pass;
  471. mpass = macpass;
  472. }
  473. if (twopass) {
  474. /* To avoid bit rot */
  475. if (1) {
  476. #ifndef OPENSSL_NO_UI_CONSOLE
  477. if (EVP_read_pw_string(
  478. macpass, sizeof(macpass), "Enter MAC Password:", export_pkcs12)) {
  479. BIO_printf(bio_err, "Can't read Password\n");
  480. goto end;
  481. }
  482. } else {
  483. #endif
  484. BIO_printf(bio_err, "Unsupported option -twopass\n");
  485. goto end;
  486. }
  487. }
  488. if (export_pkcs12) {
  489. EVP_PKEY *key = NULL;
  490. X509 *ee_cert = NULL, *x = NULL;
  491. STACK_OF(X509) *certs = NULL;
  492. STACK_OF(X509) *untrusted_certs = NULL;
  493. EVP_MD *macmd = NULL;
  494. unsigned char *catmp = NULL;
  495. int i;
  496. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  497. BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
  498. goto export_end;
  499. }
  500. if ((options & NOCERTS) != 0) {
  501. chain = 0;
  502. BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
  503. }
  504. if (!(options & NOKEYS)) {
  505. key = load_key(keyname ? keyname : infile,
  506. FORMAT_PEM, 1, passin, e,
  507. keyname ?
  508. "private key from -inkey file" :
  509. "private key from -in file");
  510. if (key == NULL)
  511. goto export_end;
  512. }
  513. /* Load all certs in input file */
  514. if (!(options & NOCERTS)) {
  515. if (!load_certs(infile, 1, &certs, passin,
  516. "certificates from -in file"))
  517. goto export_end;
  518. if (sk_X509_num(certs) < 1) {
  519. BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
  520. goto export_end;
  521. }
  522. if (key != NULL) {
  523. /* Look for matching private key */
  524. for (i = 0; i < sk_X509_num(certs); i++) {
  525. x = sk_X509_value(certs, i);
  526. if (cert_matches_key(x, key)) {
  527. ee_cert = x;
  528. /* Zero keyid and alias */
  529. X509_keyid_set1(ee_cert, NULL, 0);
  530. X509_alias_set1(ee_cert, NULL, 0);
  531. /* Remove from list */
  532. (void)sk_X509_delete(certs, i);
  533. break;
  534. }
  535. }
  536. if (ee_cert == NULL) {
  537. BIO_printf(bio_err,
  538. "No cert in -in file '%s' matches private key\n",
  539. infile);
  540. goto export_end;
  541. }
  542. }
  543. }
  544. /* Load any untrusted certificates for chain building */
  545. if (untrusted != NULL) {
  546. if (!load_certs(untrusted, 0, &untrusted_certs, passcerts,
  547. "untrusted certificates"))
  548. goto export_end;
  549. }
  550. /* If chaining get chain from end entity cert */
  551. if (chain) {
  552. int vret;
  553. STACK_OF(X509) *chain2;
  554. X509_STORE *store;
  555. X509 *ee_cert_tmp = ee_cert;
  556. /* Assume the first cert if we haven't got anything else */
  557. if (ee_cert_tmp == NULL && certs != NULL)
  558. ee_cert_tmp = sk_X509_value(certs, 0);
  559. if (ee_cert_tmp == NULL) {
  560. BIO_printf(bio_err,
  561. "No end entity certificate to check with -chain\n");
  562. goto export_end;
  563. }
  564. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  565. CAstore, noCAstore))
  566. == NULL)
  567. goto export_end;
  568. vret = get_cert_chain(ee_cert_tmp, store, untrusted_certs, &chain2);
  569. X509_STORE_free(store);
  570. if (vret == X509_V_OK) {
  571. int add_certs;
  572. /* Remove from chain2 the first (end entity) certificate */
  573. X509_free(sk_X509_shift(chain2));
  574. /* Add the remaining certs (except for duplicates) */
  575. add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
  576. | X509_ADD_FLAG_NO_DUP);
  577. OSSL_STACK_OF_X509_free(chain2);
  578. if (!add_certs)
  579. goto export_end;
  580. } else {
  581. if (vret != X509_V_ERR_UNSPECIFIED)
  582. BIO_printf(bio_err, "Error getting chain: %s\n",
  583. X509_verify_cert_error_string(vret));
  584. goto export_end;
  585. }
  586. }
  587. /* Add any extra certificates asked for */
  588. if (certfile != NULL) {
  589. if (!load_certs(certfile, 0, &certs, passcerts,
  590. "extra certificates from -certfile"))
  591. goto export_end;
  592. }
  593. /* Add any CA names */
  594. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  595. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  596. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  597. }
  598. if (csp_name != NULL && key != NULL)
  599. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  600. MBSTRING_ASC, (unsigned char *)csp_name,
  601. -1);
  602. if (add_lmk && key != NULL)
  603. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  604. if (!noprompt && !(enc == NULL && maciter == -1)) {
  605. /* To avoid bit rot */
  606. if (1) {
  607. #ifndef OPENSSL_NO_UI_CONSOLE
  608. if (EVP_read_pw_string(pass, sizeof(pass),
  609. "Enter Export Password:", 1)) {
  610. BIO_printf(bio_err, "Can't read Password\n");
  611. goto export_end;
  612. }
  613. } else {
  614. #endif
  615. BIO_printf(bio_err, "Password required\n");
  616. goto export_end;
  617. }
  618. }
  619. if (!twopass)
  620. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  621. p12 = PKCS12_create_ex(cpass, name, key, ee_cert, certs,
  622. key_pbe, cert_pbe, iter, -1, keytype,
  623. app_get0_libctx(), app_get0_propq());
  624. if (p12 == NULL) {
  625. BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
  626. outfile);
  627. goto export_end;
  628. }
  629. if (macalg != NULL) {
  630. if (!opt_md(macalg, &macmd))
  631. goto opthelp;
  632. }
  633. if (maciter != -1)
  634. if (!PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd)) {
  635. BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
  636. BIO_printf(bio_err, "Use -nomac if MAC not required and PKCS12KDF support not available.\n");
  637. goto export_end;
  638. }
  639. assert(private);
  640. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  641. if (out == NULL)
  642. goto end;
  643. i2d_PKCS12_bio(out, p12);
  644. ret = 0;
  645. export_end:
  646. EVP_PKEY_free(key);
  647. EVP_MD_free(macmd);
  648. OSSL_STACK_OF_X509_free(certs);
  649. OSSL_STACK_OF_X509_free(untrusted_certs);
  650. X509_free(ee_cert);
  651. ERR_print_errors(bio_err);
  652. goto end;
  653. }
  654. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  655. if (in == NULL)
  656. goto end;
  657. out = bio_open_owner(outfile, FORMAT_PEM, private);
  658. if (out == 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. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  751. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  752. ERR_print_errors(bio_err);
  753. goto end;
  754. }
  755. ret = 0;
  756. end:
  757. PKCS12_free(p12);
  758. release_engine(e);
  759. BIO_free(in);
  760. BIO_free_all(out);
  761. sk_OPENSSL_STRING_free(canames);
  762. OPENSSL_free(badpass);
  763. OPENSSL_free(passcerts);
  764. OPENSSL_free(passin);
  765. OPENSSL_free(passout);
  766. return ret;
  767. }
  768. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  769. int passlen, int options, char *pempass,
  770. const EVP_CIPHER *enc)
  771. {
  772. STACK_OF(PKCS7) *asafes = NULL;
  773. STACK_OF(PKCS12_SAFEBAG) *bags;
  774. int i, bagnid;
  775. int ret = 0;
  776. PKCS7 *p7;
  777. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  778. return 0;
  779. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  780. p7 = sk_PKCS7_value(asafes, i);
  781. bagnid = OBJ_obj2nid(p7->type);
  782. if (bagnid == NID_pkcs7_data) {
  783. bags = PKCS12_unpack_p7data(p7);
  784. if (options & INFO)
  785. BIO_printf(bio_err, "PKCS7 Data\n");
  786. } else if (bagnid == NID_pkcs7_encrypted) {
  787. if (options & INFO) {
  788. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  789. alg_print(p7->d.encrypted->enc_data->algorithm);
  790. }
  791. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  792. } else {
  793. continue;
  794. }
  795. if (!bags)
  796. goto err;
  797. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  798. options, pempass, enc)) {
  799. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  800. goto err;
  801. }
  802. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  803. bags = NULL;
  804. }
  805. ret = 1;
  806. err:
  807. sk_PKCS7_pop_free(asafes, PKCS7_free);
  808. return ret;
  809. }
  810. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  811. const char *pass, int passlen, int options,
  812. char *pempass, const EVP_CIPHER *enc)
  813. {
  814. int i;
  815. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  816. if (!dump_certs_pkeys_bag(out,
  817. sk_PKCS12_SAFEBAG_value(bags, i),
  818. pass, passlen, options, pempass, enc))
  819. return 0;
  820. }
  821. return 1;
  822. }
  823. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  824. const char *pass, int passlen, int options,
  825. char *pempass, const EVP_CIPHER *enc)
  826. {
  827. EVP_PKEY *pkey;
  828. PKCS8_PRIV_KEY_INFO *p8;
  829. const PKCS8_PRIV_KEY_INFO *p8c;
  830. X509 *x509;
  831. const STACK_OF(X509_ATTRIBUTE) *attrs;
  832. int ret = 0;
  833. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  834. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  835. case NID_keyBag:
  836. if (options & INFO)
  837. BIO_printf(bio_err, "Key bag\n");
  838. if (options & NOKEYS)
  839. return 1;
  840. print_attribs(out, attrs, "Bag Attributes");
  841. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  842. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  843. return 0;
  844. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  845. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  846. EVP_PKEY_free(pkey);
  847. break;
  848. case NID_pkcs8ShroudedKeyBag:
  849. if (options & INFO) {
  850. const X509_SIG *tp8;
  851. const X509_ALGOR *tp8alg;
  852. BIO_printf(bio_err, "Shrouded Keybag: ");
  853. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  854. X509_SIG_get0(tp8, &tp8alg, NULL);
  855. alg_print(tp8alg);
  856. }
  857. if (options & NOKEYS)
  858. return 1;
  859. print_attribs(out, attrs, "Bag Attributes");
  860. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  861. return 0;
  862. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  863. PKCS8_PRIV_KEY_INFO_free(p8);
  864. return 0;
  865. }
  866. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  867. PKCS8_PRIV_KEY_INFO_free(p8);
  868. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  869. EVP_PKEY_free(pkey);
  870. break;
  871. case NID_certBag:
  872. if (options & INFO)
  873. BIO_printf(bio_err, "Certificate bag\n");
  874. if (options & NOCERTS)
  875. return 1;
  876. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  877. if (options & CACERTS)
  878. return 1;
  879. } else if (options & CLCERTS)
  880. return 1;
  881. print_attribs(out, attrs, "Bag Attributes");
  882. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  883. return 1;
  884. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  885. return 0;
  886. dump_cert_text(out, x509);
  887. ret = PEM_write_bio_X509(out, x509);
  888. X509_free(x509);
  889. break;
  890. case NID_secretBag:
  891. if (options & INFO)
  892. BIO_printf(bio_err, "Secret bag\n");
  893. print_attribs(out, attrs, "Bag Attributes");
  894. BIO_printf(bio_err, "Bag Type: ");
  895. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
  896. BIO_printf(bio_err, "\nBag Value: ");
  897. print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
  898. return 1;
  899. case NID_safeContentsBag:
  900. if (options & INFO)
  901. BIO_printf(bio_err, "Safe Contents bag\n");
  902. print_attribs(out, attrs, "Bag Attributes");
  903. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  904. pass, passlen, options, pempass, enc);
  905. default:
  906. BIO_printf(bio_err, "Warning unsupported bag type: ");
  907. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  908. BIO_printf(bio_err, "\n");
  909. return 1;
  910. }
  911. return ret;
  912. }
  913. /* Given a single certificate return a verified chain or NULL if error */
  914. static int get_cert_chain(X509 *cert, X509_STORE *store,
  915. STACK_OF(X509) *untrusted_certs,
  916. STACK_OF(X509) **chain)
  917. {
  918. X509_STORE_CTX *store_ctx = NULL;
  919. STACK_OF(X509) *chn = NULL;
  920. int i = 0;
  921. store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
  922. if (store_ctx == NULL) {
  923. i = X509_V_ERR_UNSPECIFIED;
  924. goto end;
  925. }
  926. if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
  927. i = X509_V_ERR_UNSPECIFIED;
  928. goto end;
  929. }
  930. if (X509_verify_cert(store_ctx) > 0)
  931. chn = X509_STORE_CTX_get1_chain(store_ctx);
  932. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  933. i = X509_V_ERR_UNSPECIFIED;
  934. end:
  935. X509_STORE_CTX_free(store_ctx);
  936. *chain = chn;
  937. return i;
  938. }
  939. static int alg_print(const X509_ALGOR *alg)
  940. {
  941. int pbenid, aparamtype;
  942. const ASN1_OBJECT *aoid;
  943. const void *aparam;
  944. PBEPARAM *pbe = NULL;
  945. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  946. pbenid = OBJ_obj2nid(aoid);
  947. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  948. /*
  949. * If PBE algorithm is PBES2 decode algorithm parameters
  950. * for additional details.
  951. */
  952. if (pbenid == NID_pbes2) {
  953. PBE2PARAM *pbe2 = NULL;
  954. int encnid;
  955. if (aparamtype == V_ASN1_SEQUENCE)
  956. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  957. if (pbe2 == NULL) {
  958. BIO_puts(bio_err, ", <unsupported parameters>");
  959. goto done;
  960. }
  961. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  962. pbenid = OBJ_obj2nid(aoid);
  963. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  964. encnid = OBJ_obj2nid(aoid);
  965. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  966. OBJ_nid2sn(encnid));
  967. /* If KDF is PBKDF2 decode parameters */
  968. if (pbenid == NID_id_pbkdf2) {
  969. PBKDF2PARAM *kdf = NULL;
  970. int prfnid;
  971. if (aparamtype == V_ASN1_SEQUENCE)
  972. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  973. if (kdf == NULL) {
  974. BIO_puts(bio_err, ", <unsupported parameters>");
  975. goto done;
  976. }
  977. if (kdf->prf == NULL) {
  978. prfnid = NID_hmacWithSHA1;
  979. } else {
  980. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  981. prfnid = OBJ_obj2nid(aoid);
  982. }
  983. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  984. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  985. PBKDF2PARAM_free(kdf);
  986. #ifndef OPENSSL_NO_SCRYPT
  987. } else if (pbenid == NID_id_scrypt) {
  988. SCRYPT_PARAMS *kdf = NULL;
  989. if (aparamtype == V_ASN1_SEQUENCE)
  990. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  991. if (kdf == NULL) {
  992. BIO_puts(bio_err, ", <unsupported parameters>");
  993. goto done;
  994. }
  995. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  996. "Block size(r): %ld, Parallelism(p): %ld",
  997. ASN1_STRING_length(kdf->salt),
  998. ASN1_INTEGER_get(kdf->costParameter),
  999. ASN1_INTEGER_get(kdf->blockSize),
  1000. ASN1_INTEGER_get(kdf->parallelizationParameter));
  1001. SCRYPT_PARAMS_free(kdf);
  1002. #endif
  1003. }
  1004. PBE2PARAM_free(pbe2);
  1005. } else {
  1006. if (aparamtype == V_ASN1_SEQUENCE)
  1007. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  1008. if (pbe == NULL) {
  1009. BIO_puts(bio_err, ", <unsupported parameters>");
  1010. goto done;
  1011. }
  1012. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  1013. PBEPARAM_free(pbe);
  1014. }
  1015. done:
  1016. BIO_puts(bio_err, "\n");
  1017. return 1;
  1018. }
  1019. /* Load all certificates from a given file */
  1020. int cert_load(BIO *in, STACK_OF(X509) *sk)
  1021. {
  1022. int ret = 0;
  1023. X509 *cert;
  1024. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  1025. ret = 1;
  1026. if (!sk_X509_push(sk, cert))
  1027. return 0;
  1028. }
  1029. if (ret)
  1030. ERR_clear_error();
  1031. return ret;
  1032. }
  1033. /* Generalised x509 attribute value print */
  1034. void print_attribute(BIO *out, const ASN1_TYPE *av)
  1035. {
  1036. char *value;
  1037. switch (av->type) {
  1038. case V_ASN1_BMPSTRING:
  1039. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  1040. av->value.bmpstring->length);
  1041. BIO_printf(out, "%s\n", value);
  1042. OPENSSL_free(value);
  1043. break;
  1044. case V_ASN1_UTF8STRING:
  1045. BIO_printf(out, "%.*s\n", av->value.utf8string->length,
  1046. av->value.utf8string->data);
  1047. break;
  1048. case V_ASN1_OCTET_STRING:
  1049. hex_prin(out, av->value.octet_string->data,
  1050. av->value.octet_string->length);
  1051. BIO_printf(out, "\n");
  1052. break;
  1053. case V_ASN1_BIT_STRING:
  1054. hex_prin(out, av->value.bit_string->data,
  1055. av->value.bit_string->length);
  1056. BIO_printf(out, "\n");
  1057. break;
  1058. default:
  1059. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  1060. break;
  1061. }
  1062. }
  1063. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  1064. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  1065. const char *name)
  1066. {
  1067. X509_ATTRIBUTE *attr;
  1068. ASN1_TYPE *av;
  1069. int i, j, attr_nid;
  1070. if (!attrlst) {
  1071. BIO_printf(out, "%s: <No Attributes>\n", name);
  1072. return 1;
  1073. }
  1074. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  1075. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  1076. return 1;
  1077. }
  1078. BIO_printf(out, "%s\n", name);
  1079. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  1080. ASN1_OBJECT *attr_obj;
  1081. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  1082. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  1083. attr_nid = OBJ_obj2nid(attr_obj);
  1084. BIO_printf(out, " ");
  1085. if (attr_nid == NID_undef) {
  1086. i2a_ASN1_OBJECT(out, attr_obj);
  1087. BIO_printf(out, ": ");
  1088. } else {
  1089. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  1090. }
  1091. if (X509_ATTRIBUTE_count(attr)) {
  1092. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
  1093. {
  1094. av = X509_ATTRIBUTE_get0_type(attr, j);
  1095. print_attribute(out, av);
  1096. }
  1097. } else {
  1098. BIO_printf(out, "<No Values>\n");
  1099. }
  1100. }
  1101. return 1;
  1102. }
  1103. void hex_prin(BIO *out, unsigned char *buf, int len)
  1104. {
  1105. int i;
  1106. for (i = 0; i < len; i++)
  1107. BIO_printf(out, "%02X ", buf[i]);
  1108. }
  1109. static int set_pbe(int *ppbe, const char *str)
  1110. {
  1111. if (!str)
  1112. return 0;
  1113. if (strcmp(str, "NONE") == 0) {
  1114. *ppbe = -1;
  1115. return 1;
  1116. }
  1117. *ppbe = OBJ_txt2nid(str);
  1118. if (*ppbe == NID_undef) {
  1119. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  1120. return 0;
  1121. }
  1122. return 1;
  1123. }