2
0

pkcs12.c 43 KB

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