pkcs12.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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_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(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. } else {
  546. ee_cert = X509_dup(sk_X509_value(certs, 0)); /* take 1st cert */
  547. }
  548. }
  549. /* Load any untrusted certificates for chain building */
  550. if (untrusted != NULL) {
  551. if (!load_certs(untrusted, 0, &untrusted_certs, passcerts,
  552. "untrusted certificates"))
  553. goto export_end;
  554. }
  555. /* If chaining get chain from end entity cert */
  556. if (chain) {
  557. int vret;
  558. STACK_OF(X509) *chain2;
  559. X509_STORE *store;
  560. if (ee_cert == NULL) {
  561. BIO_printf(bio_err,
  562. "No end entity certificate to check with -chain\n");
  563. goto export_end;
  564. }
  565. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  566. CAstore, noCAstore))
  567. == NULL)
  568. goto export_end;
  569. vret = get_cert_chain(ee_cert, store, untrusted_certs, &chain2);
  570. X509_STORE_free(store);
  571. if (vret == X509_V_OK) {
  572. int add_certs;
  573. /* Remove from chain2 the first (end entity) certificate */
  574. X509_free(sk_X509_shift(chain2));
  575. /* Add the remaining certs (except for duplicates) */
  576. add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
  577. | X509_ADD_FLAG_NO_DUP);
  578. sk_X509_pop_free(chain2, X509_free);
  579. if (!add_certs)
  580. goto export_end;
  581. } else {
  582. if (vret != X509_V_ERR_UNSPECIFIED)
  583. BIO_printf(bio_err, "Error getting chain: %s\n",
  584. X509_verify_cert_error_string(vret));
  585. goto export_end;
  586. }
  587. }
  588. /* Add any extra certificates asked for */
  589. if (certfile != NULL) {
  590. if (!load_certs(certfile, 0, &certs, passcerts,
  591. "extra certificates from -certfile"))
  592. goto export_end;
  593. }
  594. /* Add any CA names */
  595. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  596. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  597. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  598. }
  599. if (csp_name != NULL && key != NULL)
  600. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  601. MBSTRING_ASC, (unsigned char *)csp_name,
  602. -1);
  603. if (add_lmk && key != NULL)
  604. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  605. if (!noprompt && !(enc == NULL && maciter == -1)) {
  606. /* To avoid bit rot */
  607. if (1) {
  608. #ifndef OPENSSL_NO_UI_CONSOLE
  609. if (EVP_read_pw_string(pass, sizeof(pass),
  610. "Enter Export Password:", 1)) {
  611. BIO_printf(bio_err, "Can't read Password\n");
  612. goto export_end;
  613. }
  614. } else {
  615. #endif
  616. BIO_printf(bio_err, "Password required\n");
  617. goto export_end;
  618. }
  619. }
  620. if (!twopass)
  621. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  622. p12 = PKCS12_create_ex(cpass, name, key, ee_cert, certs,
  623. key_pbe, cert_pbe, iter, -1, keytype,
  624. app_get0_libctx(), app_get0_propq());
  625. if (p12 == NULL) {
  626. BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
  627. outfile);
  628. goto export_end;
  629. }
  630. if (macalg != NULL) {
  631. if (!opt_md(macalg, &macmd))
  632. goto opthelp;
  633. }
  634. if (maciter != -1)
  635. if (!PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd)) {
  636. BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
  637. BIO_printf(bio_err, "Use -nomac if MAC not required and PKCS12KDF support not available.\n");
  638. goto export_end;
  639. }
  640. assert(private);
  641. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  642. if (out == NULL)
  643. goto end;
  644. i2d_PKCS12_bio(out, p12);
  645. ret = 0;
  646. export_end:
  647. EVP_PKEY_free(key);
  648. EVP_MD_free(macmd);
  649. sk_X509_pop_free(certs, X509_free);
  650. sk_X509_pop_free(untrusted_certs, X509_free);
  651. X509_free(ee_cert);
  652. ERR_print_errors(bio_err);
  653. goto end;
  654. }
  655. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  656. if (in == NULL)
  657. goto end;
  658. out = bio_open_owner(outfile, FORMAT_PEM, private);
  659. if (out == NULL)
  660. goto end;
  661. p12 = PKCS12_init_ex(NID_pkcs7_data, app_get0_libctx(), app_get0_propq());
  662. if (p12 == NULL) {
  663. ERR_print_errors(bio_err);
  664. goto end;
  665. }
  666. if ((p12 = d2i_PKCS12_bio(in, &p12)) == NULL) {
  667. ERR_print_errors(bio_err);
  668. goto end;
  669. }
  670. if (!noprompt) {
  671. if (1) {
  672. #ifndef OPENSSL_NO_UI_CONSOLE
  673. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
  674. 0)) {
  675. BIO_printf(bio_err, "Can't read Password\n");
  676. goto end;
  677. }
  678. } else {
  679. #endif
  680. BIO_printf(bio_err, "Password required\n");
  681. goto end;
  682. }
  683. }
  684. if (!twopass)
  685. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  686. if ((options & INFO) && PKCS12_mac_present(p12)) {
  687. const ASN1_INTEGER *tmaciter;
  688. const X509_ALGOR *macalgid;
  689. const ASN1_OBJECT *macobj;
  690. const ASN1_OCTET_STRING *tmac;
  691. const ASN1_OCTET_STRING *tsalt;
  692. PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
  693. /* current hash algorithms do not use parameters so extract just name,
  694. in future alg_print() may be needed */
  695. X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
  696. BIO_puts(bio_err, "MAC: ");
  697. i2a_ASN1_OBJECT(bio_err, macobj);
  698. BIO_printf(bio_err, ", Iteration %ld\n",
  699. tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
  700. BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
  701. tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
  702. tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
  703. }
  704. if (macver) {
  705. EVP_KDF *pkcs12kdf;
  706. pkcs12kdf = EVP_KDF_fetch(app_get0_libctx(), "PKCS12KDF",
  707. app_get0_propq());
  708. if (pkcs12kdf == NULL) {
  709. BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
  710. BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
  711. goto end;
  712. }
  713. EVP_KDF_free(pkcs12kdf);
  714. /* If we enter empty password try no password first */
  715. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  716. /* If mac and crypto pass the same set it to NULL too */
  717. if (!twopass)
  718. cpass = NULL;
  719. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  720. /*
  721. * May be UTF8 from previous version of OpenSSL:
  722. * convert to a UTF8 form which will translate
  723. * to the same Unicode password.
  724. */
  725. unsigned char *utmp;
  726. int utmplen;
  727. unsigned long err = ERR_peek_error();
  728. if (ERR_GET_LIB(err) == ERR_LIB_PKCS12
  729. && ERR_GET_REASON(err) == PKCS12_R_MAC_ABSENT) {
  730. BIO_printf(bio_err, "Warning: MAC is absent!\n");
  731. goto dump;
  732. }
  733. utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
  734. if (utmp == NULL)
  735. goto end;
  736. badpass = OPENSSL_uni2utf8(utmp, utmplen);
  737. OPENSSL_free(utmp);
  738. if (!PKCS12_verify_mac(p12, badpass, -1)) {
  739. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  740. ERR_print_errors(bio_err);
  741. goto end;
  742. } else {
  743. BIO_printf(bio_err, "Warning: using broken algorithm\n");
  744. if (!twopass)
  745. cpass = badpass;
  746. }
  747. }
  748. }
  749. dump:
  750. assert(private);
  751. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  752. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  753. ERR_print_errors(bio_err);
  754. goto end;
  755. }
  756. ret = 0;
  757. end:
  758. PKCS12_free(p12);
  759. release_engine(e);
  760. BIO_free(in);
  761. BIO_free_all(out);
  762. sk_OPENSSL_STRING_free(canames);
  763. OPENSSL_free(badpass);
  764. OPENSSL_free(passcerts);
  765. OPENSSL_free(passin);
  766. OPENSSL_free(passout);
  767. return ret;
  768. }
  769. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  770. int passlen, int options, char *pempass,
  771. const EVP_CIPHER *enc)
  772. {
  773. STACK_OF(PKCS7) *asafes = NULL;
  774. STACK_OF(PKCS12_SAFEBAG) *bags;
  775. int i, bagnid;
  776. int ret = 0;
  777. PKCS7 *p7;
  778. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  779. return 0;
  780. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  781. p7 = sk_PKCS7_value(asafes, i);
  782. bagnid = OBJ_obj2nid(p7->type);
  783. if (bagnid == NID_pkcs7_data) {
  784. bags = PKCS12_unpack_p7data(p7);
  785. if (options & INFO)
  786. BIO_printf(bio_err, "PKCS7 Data\n");
  787. } else if (bagnid == NID_pkcs7_encrypted) {
  788. if (options & INFO) {
  789. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  790. alg_print(p7->d.encrypted->enc_data->algorithm);
  791. }
  792. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  793. } else {
  794. continue;
  795. }
  796. if (!bags)
  797. goto err;
  798. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  799. options, pempass, enc)) {
  800. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  801. goto err;
  802. }
  803. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  804. bags = NULL;
  805. }
  806. ret = 1;
  807. err:
  808. sk_PKCS7_pop_free(asafes, PKCS7_free);
  809. return ret;
  810. }
  811. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  812. const char *pass, int passlen, int options,
  813. char *pempass, const EVP_CIPHER *enc)
  814. {
  815. int i;
  816. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  817. if (!dump_certs_pkeys_bag(out,
  818. sk_PKCS12_SAFEBAG_value(bags, i),
  819. pass, passlen, options, pempass, enc))
  820. return 0;
  821. }
  822. return 1;
  823. }
  824. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  825. const char *pass, int passlen, int options,
  826. char *pempass, const EVP_CIPHER *enc)
  827. {
  828. EVP_PKEY *pkey;
  829. PKCS8_PRIV_KEY_INFO *p8;
  830. const PKCS8_PRIV_KEY_INFO *p8c;
  831. X509 *x509;
  832. const STACK_OF(X509_ATTRIBUTE) *attrs;
  833. int ret = 0;
  834. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  835. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  836. case NID_keyBag:
  837. if (options & INFO)
  838. BIO_printf(bio_err, "Key bag\n");
  839. if (options & NOKEYS)
  840. return 1;
  841. print_attribs(out, attrs, "Bag Attributes");
  842. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  843. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  844. return 0;
  845. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  846. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  847. EVP_PKEY_free(pkey);
  848. break;
  849. case NID_pkcs8ShroudedKeyBag:
  850. if (options & INFO) {
  851. const X509_SIG *tp8;
  852. const X509_ALGOR *tp8alg;
  853. BIO_printf(bio_err, "Shrouded Keybag: ");
  854. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  855. X509_SIG_get0(tp8, &tp8alg, NULL);
  856. alg_print(tp8alg);
  857. }
  858. if (options & NOKEYS)
  859. return 1;
  860. print_attribs(out, attrs, "Bag Attributes");
  861. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  862. return 0;
  863. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  864. PKCS8_PRIV_KEY_INFO_free(p8);
  865. return 0;
  866. }
  867. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  868. PKCS8_PRIV_KEY_INFO_free(p8);
  869. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  870. EVP_PKEY_free(pkey);
  871. break;
  872. case NID_certBag:
  873. if (options & INFO)
  874. BIO_printf(bio_err, "Certificate bag\n");
  875. if (options & NOCERTS)
  876. return 1;
  877. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  878. if (options & CACERTS)
  879. return 1;
  880. } else if (options & CLCERTS)
  881. return 1;
  882. print_attribs(out, attrs, "Bag Attributes");
  883. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  884. return 1;
  885. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  886. return 0;
  887. dump_cert_text(out, x509);
  888. ret = PEM_write_bio_X509(out, x509);
  889. X509_free(x509);
  890. break;
  891. case NID_secretBag:
  892. if (options & INFO)
  893. BIO_printf(bio_err, "Secret bag\n");
  894. print_attribs(out, attrs, "Bag Attributes");
  895. BIO_printf(bio_err, "Bag Type: ");
  896. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
  897. BIO_printf(bio_err, "\nBag Value: ");
  898. print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
  899. return 1;
  900. case NID_safeContentsBag:
  901. if (options & INFO)
  902. BIO_printf(bio_err, "Safe Contents bag\n");
  903. print_attribs(out, attrs, "Bag Attributes");
  904. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  905. pass, passlen, options, pempass, enc);
  906. default:
  907. BIO_printf(bio_err, "Warning unsupported bag type: ");
  908. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  909. BIO_printf(bio_err, "\n");
  910. return 1;
  911. }
  912. return ret;
  913. }
  914. /* Given a single certificate return a verified chain or NULL if error */
  915. static int get_cert_chain(X509 *cert, X509_STORE *store,
  916. STACK_OF(X509) *untrusted_certs,
  917. STACK_OF(X509) **chain)
  918. {
  919. X509_STORE_CTX *store_ctx = NULL;
  920. STACK_OF(X509) *chn = NULL;
  921. int i = 0;
  922. store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
  923. if (store_ctx == NULL) {
  924. i = X509_V_ERR_UNSPECIFIED;
  925. goto end;
  926. }
  927. if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
  928. i = X509_V_ERR_UNSPECIFIED;
  929. goto end;
  930. }
  931. if (X509_verify_cert(store_ctx) > 0)
  932. chn = X509_STORE_CTX_get1_chain(store_ctx);
  933. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  934. i = X509_V_ERR_UNSPECIFIED;
  935. end:
  936. X509_STORE_CTX_free(store_ctx);
  937. *chain = chn;
  938. return i;
  939. }
  940. static int alg_print(const X509_ALGOR *alg)
  941. {
  942. int pbenid, aparamtype;
  943. const ASN1_OBJECT *aoid;
  944. const void *aparam;
  945. PBEPARAM *pbe = NULL;
  946. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  947. pbenid = OBJ_obj2nid(aoid);
  948. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  949. /*
  950. * If PBE algorithm is PBES2 decode algorithm parameters
  951. * for additional details.
  952. */
  953. if (pbenid == NID_pbes2) {
  954. PBE2PARAM *pbe2 = NULL;
  955. int encnid;
  956. if (aparamtype == V_ASN1_SEQUENCE)
  957. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  958. if (pbe2 == NULL) {
  959. BIO_puts(bio_err, ", <unsupported parameters>");
  960. goto done;
  961. }
  962. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  963. pbenid = OBJ_obj2nid(aoid);
  964. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  965. encnid = OBJ_obj2nid(aoid);
  966. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  967. OBJ_nid2sn(encnid));
  968. /* If KDF is PBKDF2 decode parameters */
  969. if (pbenid == NID_id_pbkdf2) {
  970. PBKDF2PARAM *kdf = NULL;
  971. int prfnid;
  972. if (aparamtype == V_ASN1_SEQUENCE)
  973. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  974. if (kdf == NULL) {
  975. BIO_puts(bio_err, ", <unsupported parameters>");
  976. goto done;
  977. }
  978. if (kdf->prf == NULL) {
  979. prfnid = NID_hmacWithSHA1;
  980. } else {
  981. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  982. prfnid = OBJ_obj2nid(aoid);
  983. }
  984. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  985. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  986. PBKDF2PARAM_free(kdf);
  987. #ifndef OPENSSL_NO_SCRYPT
  988. } else if (pbenid == NID_id_scrypt) {
  989. SCRYPT_PARAMS *kdf = NULL;
  990. if (aparamtype == V_ASN1_SEQUENCE)
  991. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  992. if (kdf == NULL) {
  993. BIO_puts(bio_err, ", <unsupported parameters>");
  994. goto done;
  995. }
  996. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  997. "Block size(r): %ld, Parallelism(p): %ld",
  998. ASN1_STRING_length(kdf->salt),
  999. ASN1_INTEGER_get(kdf->costParameter),
  1000. ASN1_INTEGER_get(kdf->blockSize),
  1001. ASN1_INTEGER_get(kdf->parallelizationParameter));
  1002. SCRYPT_PARAMS_free(kdf);
  1003. #endif
  1004. }
  1005. PBE2PARAM_free(pbe2);
  1006. } else {
  1007. if (aparamtype == V_ASN1_SEQUENCE)
  1008. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  1009. if (pbe == NULL) {
  1010. BIO_puts(bio_err, ", <unsupported parameters>");
  1011. goto done;
  1012. }
  1013. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  1014. PBEPARAM_free(pbe);
  1015. }
  1016. done:
  1017. BIO_puts(bio_err, "\n");
  1018. return 1;
  1019. }
  1020. /* Load all certificates from a given file */
  1021. int cert_load(BIO *in, STACK_OF(X509) *sk)
  1022. {
  1023. int ret = 0;
  1024. X509 *cert;
  1025. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  1026. ret = 1;
  1027. if (!sk_X509_push(sk, cert))
  1028. return 0;
  1029. }
  1030. if (ret)
  1031. ERR_clear_error();
  1032. return ret;
  1033. }
  1034. /* Generalised x509 attribute value print */
  1035. void print_attribute(BIO *out, const ASN1_TYPE *av)
  1036. {
  1037. char *value;
  1038. switch (av->type) {
  1039. case V_ASN1_BMPSTRING:
  1040. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  1041. av->value.bmpstring->length);
  1042. BIO_printf(out, "%s\n", value);
  1043. OPENSSL_free(value);
  1044. break;
  1045. case V_ASN1_UTF8STRING:
  1046. BIO_printf(out, "%s\n", 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. }