pkcs12.c 38 KB

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