pkcs12.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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(NULL, "PKCS12KDF", NULL);
  707. if (pkcs12kdf == NULL) {
  708. BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
  709. BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
  710. goto end;
  711. }
  712. EVP_KDF_free(pkcs12kdf);
  713. /* If we enter empty password try no password first */
  714. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  715. /* If mac and crypto pass the same set it to NULL too */
  716. if (!twopass)
  717. cpass = NULL;
  718. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  719. /*
  720. * May be UTF8 from previous version of OpenSSL:
  721. * convert to a UTF8 form which will translate
  722. * to the same Unicode password.
  723. */
  724. unsigned char *utmp;
  725. int utmplen;
  726. unsigned long err = ERR_peek_error();
  727. if (ERR_GET_LIB(err) == ERR_LIB_PKCS12
  728. && ERR_GET_REASON(err) == PKCS12_R_MAC_ABSENT) {
  729. BIO_printf(bio_err, "Warning: MAC is absent!\n");
  730. goto dump;
  731. }
  732. utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
  733. if (utmp == NULL)
  734. goto end;
  735. badpass = OPENSSL_uni2utf8(utmp, utmplen);
  736. OPENSSL_free(utmp);
  737. if (!PKCS12_verify_mac(p12, badpass, -1)) {
  738. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  739. ERR_print_errors(bio_err);
  740. goto end;
  741. } else {
  742. BIO_printf(bio_err, "Warning: using broken algorithm\n");
  743. if (!twopass)
  744. cpass = badpass;
  745. }
  746. }
  747. }
  748. dump:
  749. assert(private);
  750. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  751. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  752. ERR_print_errors(bio_err);
  753. goto end;
  754. }
  755. ret = 0;
  756. end:
  757. PKCS12_free(p12);
  758. release_engine(e);
  759. BIO_free(in);
  760. BIO_free_all(out);
  761. sk_OPENSSL_STRING_free(canames);
  762. OPENSSL_free(badpass);
  763. OPENSSL_free(passcerts);
  764. OPENSSL_free(passin);
  765. OPENSSL_free(passout);
  766. return ret;
  767. }
  768. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  769. int passlen, int options, char *pempass,
  770. const EVP_CIPHER *enc)
  771. {
  772. STACK_OF(PKCS7) *asafes = NULL;
  773. STACK_OF(PKCS12_SAFEBAG) *bags;
  774. int i, bagnid;
  775. int ret = 0;
  776. PKCS7 *p7;
  777. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  778. return 0;
  779. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  780. p7 = sk_PKCS7_value(asafes, i);
  781. bagnid = OBJ_obj2nid(p7->type);
  782. if (bagnid == NID_pkcs7_data) {
  783. bags = PKCS12_unpack_p7data(p7);
  784. if (options & INFO)
  785. BIO_printf(bio_err, "PKCS7 Data\n");
  786. } else if (bagnid == NID_pkcs7_encrypted) {
  787. if (options & INFO) {
  788. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  789. alg_print(p7->d.encrypted->enc_data->algorithm);
  790. }
  791. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  792. } else {
  793. continue;
  794. }
  795. if (!bags)
  796. goto err;
  797. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  798. options, pempass, enc)) {
  799. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  800. goto err;
  801. }
  802. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  803. bags = NULL;
  804. }
  805. ret = 1;
  806. err:
  807. sk_PKCS7_pop_free(asafes, PKCS7_free);
  808. return ret;
  809. }
  810. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  811. const char *pass, int passlen, int options,
  812. char *pempass, const EVP_CIPHER *enc)
  813. {
  814. int i;
  815. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  816. if (!dump_certs_pkeys_bag(out,
  817. sk_PKCS12_SAFEBAG_value(bags, i),
  818. pass, passlen, options, pempass, enc))
  819. return 0;
  820. }
  821. return 1;
  822. }
  823. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  824. const char *pass, int passlen, int options,
  825. char *pempass, const EVP_CIPHER *enc)
  826. {
  827. EVP_PKEY *pkey;
  828. PKCS8_PRIV_KEY_INFO *p8;
  829. const PKCS8_PRIV_KEY_INFO *p8c;
  830. X509 *x509;
  831. const STACK_OF(X509_ATTRIBUTE) *attrs;
  832. int ret = 0;
  833. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  834. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  835. case NID_keyBag:
  836. if (options & INFO)
  837. BIO_printf(bio_err, "Key bag\n");
  838. if (options & NOKEYS)
  839. return 1;
  840. print_attribs(out, attrs, "Bag Attributes");
  841. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  842. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  843. return 0;
  844. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  845. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  846. EVP_PKEY_free(pkey);
  847. break;
  848. case NID_pkcs8ShroudedKeyBag:
  849. if (options & INFO) {
  850. const X509_SIG *tp8;
  851. const X509_ALGOR *tp8alg;
  852. BIO_printf(bio_err, "Shrouded Keybag: ");
  853. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  854. X509_SIG_get0(tp8, &tp8alg, NULL);
  855. alg_print(tp8alg);
  856. }
  857. if (options & NOKEYS)
  858. return 1;
  859. print_attribs(out, attrs, "Bag Attributes");
  860. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  861. return 0;
  862. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  863. PKCS8_PRIV_KEY_INFO_free(p8);
  864. return 0;
  865. }
  866. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  867. PKCS8_PRIV_KEY_INFO_free(p8);
  868. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  869. EVP_PKEY_free(pkey);
  870. break;
  871. case NID_certBag:
  872. if (options & INFO)
  873. BIO_printf(bio_err, "Certificate bag\n");
  874. if (options & NOCERTS)
  875. return 1;
  876. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  877. if (options & CACERTS)
  878. return 1;
  879. } else if (options & CLCERTS)
  880. return 1;
  881. print_attribs(out, attrs, "Bag Attributes");
  882. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  883. return 1;
  884. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  885. return 0;
  886. dump_cert_text(out, x509);
  887. ret = PEM_write_bio_X509(out, x509);
  888. X509_free(x509);
  889. break;
  890. case NID_secretBag:
  891. if (options & INFO)
  892. BIO_printf(bio_err, "Secret bag\n");
  893. print_attribs(out, attrs, "Bag Attributes");
  894. BIO_printf(bio_err, "Bag Type: ");
  895. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
  896. BIO_printf(bio_err, "\nBag Value: ");
  897. print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
  898. return 1;
  899. case NID_safeContentsBag:
  900. if (options & INFO)
  901. BIO_printf(bio_err, "Safe Contents bag\n");
  902. print_attribs(out, attrs, "Bag Attributes");
  903. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  904. pass, passlen, options, pempass, enc);
  905. default:
  906. BIO_printf(bio_err, "Warning unsupported bag type: ");
  907. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  908. BIO_printf(bio_err, "\n");
  909. return 1;
  910. }
  911. return ret;
  912. }
  913. /* Given a single certificate return a verified chain or NULL if error */
  914. static int get_cert_chain(X509 *cert, X509_STORE *store,
  915. STACK_OF(X509) *untrusted_certs,
  916. STACK_OF(X509) **chain)
  917. {
  918. X509_STORE_CTX *store_ctx = NULL;
  919. STACK_OF(X509) *chn = NULL;
  920. int i = 0;
  921. store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
  922. if (store_ctx == NULL) {
  923. i = X509_V_ERR_UNSPECIFIED;
  924. goto end;
  925. }
  926. if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
  927. i = X509_V_ERR_UNSPECIFIED;
  928. goto end;
  929. }
  930. if (X509_verify_cert(store_ctx) > 0)
  931. chn = X509_STORE_CTX_get1_chain(store_ctx);
  932. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  933. i = X509_V_ERR_UNSPECIFIED;
  934. end:
  935. X509_STORE_CTX_free(store_ctx);
  936. *chain = chn;
  937. return i;
  938. }
  939. static int alg_print(const X509_ALGOR *alg)
  940. {
  941. int pbenid, aparamtype;
  942. const ASN1_OBJECT *aoid;
  943. const void *aparam;
  944. PBEPARAM *pbe = NULL;
  945. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  946. pbenid = OBJ_obj2nid(aoid);
  947. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  948. /*
  949. * If PBE algorithm is PBES2 decode algorithm parameters
  950. * for additional details.
  951. */
  952. if (pbenid == NID_pbes2) {
  953. PBE2PARAM *pbe2 = NULL;
  954. int encnid;
  955. if (aparamtype == V_ASN1_SEQUENCE)
  956. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  957. if (pbe2 == NULL) {
  958. BIO_puts(bio_err, ", <unsupported parameters>");
  959. goto done;
  960. }
  961. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  962. pbenid = OBJ_obj2nid(aoid);
  963. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  964. encnid = OBJ_obj2nid(aoid);
  965. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  966. OBJ_nid2sn(encnid));
  967. /* If KDF is PBKDF2 decode parameters */
  968. if (pbenid == NID_id_pbkdf2) {
  969. PBKDF2PARAM *kdf = NULL;
  970. int prfnid;
  971. if (aparamtype == V_ASN1_SEQUENCE)
  972. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  973. if (kdf == NULL) {
  974. BIO_puts(bio_err, ", <unsupported parameters>");
  975. goto done;
  976. }
  977. if (kdf->prf == NULL) {
  978. prfnid = NID_hmacWithSHA1;
  979. } else {
  980. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  981. prfnid = OBJ_obj2nid(aoid);
  982. }
  983. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  984. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  985. PBKDF2PARAM_free(kdf);
  986. #ifndef OPENSSL_NO_SCRYPT
  987. } else if (pbenid == NID_id_scrypt) {
  988. SCRYPT_PARAMS *kdf = NULL;
  989. if (aparamtype == V_ASN1_SEQUENCE)
  990. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  991. if (kdf == NULL) {
  992. BIO_puts(bio_err, ", <unsupported parameters>");
  993. goto done;
  994. }
  995. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  996. "Block size(r): %ld, Parallelism(p): %ld",
  997. ASN1_STRING_length(kdf->salt),
  998. ASN1_INTEGER_get(kdf->costParameter),
  999. ASN1_INTEGER_get(kdf->blockSize),
  1000. ASN1_INTEGER_get(kdf->parallelizationParameter));
  1001. SCRYPT_PARAMS_free(kdf);
  1002. #endif
  1003. }
  1004. PBE2PARAM_free(pbe2);
  1005. } else {
  1006. if (aparamtype == V_ASN1_SEQUENCE)
  1007. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  1008. if (pbe == NULL) {
  1009. BIO_puts(bio_err, ", <unsupported parameters>");
  1010. goto done;
  1011. }
  1012. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  1013. PBEPARAM_free(pbe);
  1014. }
  1015. done:
  1016. BIO_puts(bio_err, "\n");
  1017. return 1;
  1018. }
  1019. /* Load all certificates from a given file */
  1020. int cert_load(BIO *in, STACK_OF(X509) *sk)
  1021. {
  1022. int ret = 0;
  1023. X509 *cert;
  1024. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  1025. ret = 1;
  1026. if (!sk_X509_push(sk, cert))
  1027. return 0;
  1028. }
  1029. if (ret)
  1030. ERR_clear_error();
  1031. return ret;
  1032. }
  1033. /* Generalised x509 attribute value print */
  1034. void print_attribute(BIO *out, const ASN1_TYPE *av)
  1035. {
  1036. char *value;
  1037. switch (av->type) {
  1038. case V_ASN1_BMPSTRING:
  1039. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  1040. av->value.bmpstring->length);
  1041. BIO_printf(out, "%s\n", value);
  1042. OPENSSL_free(value);
  1043. break;
  1044. case V_ASN1_UTF8STRING:
  1045. BIO_printf(out, "%s\n", av->value.utf8string->data);
  1046. break;
  1047. case V_ASN1_OCTET_STRING:
  1048. hex_prin(out, av->value.octet_string->data,
  1049. av->value.octet_string->length);
  1050. BIO_printf(out, "\n");
  1051. break;
  1052. case V_ASN1_BIT_STRING:
  1053. hex_prin(out, av->value.bit_string->data,
  1054. av->value.bit_string->length);
  1055. BIO_printf(out, "\n");
  1056. break;
  1057. default:
  1058. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  1059. break;
  1060. }
  1061. }
  1062. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  1063. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  1064. const char *name)
  1065. {
  1066. X509_ATTRIBUTE *attr;
  1067. ASN1_TYPE *av;
  1068. int i, j, attr_nid;
  1069. if (!attrlst) {
  1070. BIO_printf(out, "%s: <No Attributes>\n", name);
  1071. return 1;
  1072. }
  1073. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  1074. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  1075. return 1;
  1076. }
  1077. BIO_printf(out, "%s\n", name);
  1078. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  1079. ASN1_OBJECT *attr_obj;
  1080. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  1081. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  1082. attr_nid = OBJ_obj2nid(attr_obj);
  1083. BIO_printf(out, " ");
  1084. if (attr_nid == NID_undef) {
  1085. i2a_ASN1_OBJECT(out, attr_obj);
  1086. BIO_printf(out, ": ");
  1087. } else {
  1088. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  1089. }
  1090. if (X509_ATTRIBUTE_count(attr)) {
  1091. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
  1092. {
  1093. av = X509_ATTRIBUTE_get0_type(attr, j);
  1094. print_attribute(out, av);
  1095. }
  1096. } else {
  1097. BIO_printf(out, "<No Values>\n");
  1098. }
  1099. }
  1100. return 1;
  1101. }
  1102. void hex_prin(BIO *out, unsigned char *buf, int len)
  1103. {
  1104. int i;
  1105. for (i = 0; i < len; i++)
  1106. BIO_printf(out, "%02X ", buf[i]);
  1107. }
  1108. static int set_pbe(int *ppbe, const char *str)
  1109. {
  1110. if (!str)
  1111. return 0;
  1112. if (strcmp(str, "NONE") == 0) {
  1113. *ppbe = -1;
  1114. return 1;
  1115. }
  1116. *ppbe = OBJ_txt2nid(str);
  1117. if (*ppbe == NID_undef) {
  1118. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  1119. return 0;
  1120. }
  1121. return 1;
  1122. }