pkcs12.c 42 KB

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