pkcs12.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. /* No extra arguments. */
  327. argc = opt_num_rest();
  328. if (argc != 0)
  329. goto opthelp;
  330. if (export_pkcs12) {
  331. if ((options & INFO) != 0)
  332. WARN_EXPORT("info");
  333. if (macver == 0)
  334. WARN_EXPORT("nomacver");
  335. if ((options & CLCERTS) != 0)
  336. WARN_EXPORT("clcerts");
  337. if ((options & CACERTS) != 0)
  338. WARN_EXPORT("cacerts");
  339. if (enc != default_enc)
  340. BIO_printf(bio_err,
  341. "Warning: output encryption option -%s ignored with -export\n", enc_flag);
  342. } else {
  343. if (keyname != NULL)
  344. WARN_NO_EXPORT("inkey");
  345. if (certfile != NULL)
  346. WARN_NO_EXPORT("certfile");
  347. if (passcertsarg != NULL)
  348. WARN_NO_EXPORT("passcerts");
  349. if (chain)
  350. WARN_NO_EXPORT("chain");
  351. if (untrusted != NULL)
  352. WARN_NO_EXPORT("untrusted");
  353. if (CAfile != NULL)
  354. WARN_NO_EXPORT("CAfile");
  355. if (CApath != NULL)
  356. WARN_NO_EXPORT("CApath");
  357. if (CAstore != NULL)
  358. WARN_NO_EXPORT("CAstore");
  359. if (noCAfile)
  360. WARN_NO_EXPORT("no-CAfile");
  361. if (noCApath)
  362. WARN_NO_EXPORT("no-CApath");
  363. if (noCAstore)
  364. WARN_NO_EXPORT("no-CAstore");
  365. if (name != NULL)
  366. WARN_NO_EXPORT("name");
  367. if (canames != NULL)
  368. WARN_NO_EXPORT("caname");
  369. if (csp_name != NULL)
  370. WARN_NO_EXPORT("CSP");
  371. if (add_lmk)
  372. WARN_NO_EXPORT("LMK");
  373. if (keytype == KEY_EX)
  374. WARN_NO_EXPORT("keyex");
  375. if (keytype == KEY_SIG)
  376. WARN_NO_EXPORT("keysig");
  377. if (key_pbe != PKCS12_DEFAULT_PBE)
  378. WARN_NO_EXPORT("keypbe");
  379. if (cert_pbe != PKCS12_DEFAULT_PBE && cert_pbe != -1)
  380. WARN_NO_EXPORT("certpbe and -descert");
  381. if (macalg != NULL)
  382. WARN_NO_EXPORT("macalg");
  383. if (iter != PKCS12_DEFAULT_ITER)
  384. WARN_NO_EXPORT("iter and -noiter");
  385. if (maciter == 1)
  386. WARN_NO_EXPORT("nomaciter");
  387. if (cert_pbe == -1 && maciter == -1)
  388. WARN_NO_EXPORT("nomac");
  389. }
  390. if (use_legacy) {
  391. /* load the legacy provider if not loaded already*/
  392. if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
  393. if (!app_provider_load(app_get0_libctx(), "legacy"))
  394. goto end;
  395. /* load the default provider explicitly */
  396. if (!app_provider_load(app_get0_libctx(), "default"))
  397. goto end;
  398. }
  399. if (cert_pbe == PKCS12_DEFAULT_PBE) {
  400. /* Adapt default algorithm */
  401. #ifndef OPENSSL_NO_RC2
  402. cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
  403. #else
  404. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  405. #endif
  406. }
  407. if (key_pbe == PKCS12_DEFAULT_PBE)
  408. key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  409. if (enc == default_enc)
  410. enc = EVP_des_ede3_cbc();
  411. }
  412. private = 1;
  413. if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
  414. BIO_printf(bio_err, "Error getting certificate file password\n");
  415. goto end;
  416. }
  417. if (passarg != NULL) {
  418. if (export_pkcs12)
  419. passoutarg = passarg;
  420. else
  421. passinarg = passarg;
  422. }
  423. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  424. BIO_printf(bio_err, "Error getting passwords\n");
  425. goto end;
  426. }
  427. if (cpass == NULL) {
  428. if (export_pkcs12)
  429. cpass = passout;
  430. else
  431. cpass = passin;
  432. }
  433. if (cpass != NULL) {
  434. mpass = cpass;
  435. noprompt = 1;
  436. if (twopass) {
  437. if (export_pkcs12)
  438. BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
  439. else
  440. BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
  441. goto end;
  442. }
  443. } else {
  444. cpass = pass;
  445. mpass = macpass;
  446. }
  447. if (twopass) {
  448. /* To avoid bit rot */
  449. if (1) {
  450. #ifndef OPENSSL_NO_UI_CONSOLE
  451. if (EVP_read_pw_string(
  452. macpass, sizeof(macpass), "Enter MAC Password:", export_pkcs12)) {
  453. BIO_printf(bio_err, "Can't read Password\n");
  454. goto end;
  455. }
  456. } else {
  457. #endif
  458. BIO_printf(bio_err, "Unsupported option -twopass\n");
  459. goto end;
  460. }
  461. }
  462. if (export_pkcs12) {
  463. EVP_PKEY *key = NULL;
  464. X509 *ee_cert = NULL, *x = NULL;
  465. STACK_OF(X509) *certs = NULL;
  466. STACK_OF(X509) *untrusted_certs = NULL;
  467. const EVP_MD *macmd = NULL;
  468. unsigned char *catmp = NULL;
  469. int i;
  470. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  471. BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
  472. goto export_end;
  473. }
  474. if ((options & NOCERTS) != 0) {
  475. chain = 0;
  476. BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
  477. }
  478. if (!(options & NOKEYS)) {
  479. key = load_key(keyname ? keyname : infile,
  480. FORMAT_PEM, 1, passin, e,
  481. keyname ?
  482. "private key from -inkey file" :
  483. "private key from -in file");
  484. if (key == NULL)
  485. goto export_end;
  486. }
  487. /* Load all certs in input file */
  488. if (!(options & NOCERTS)) {
  489. if (!load_certs(infile, &certs, passin,
  490. "certificates from -in file"))
  491. goto export_end;
  492. if (sk_X509_num(certs) < 1) {
  493. BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
  494. goto export_end;
  495. }
  496. if (key != NULL) {
  497. /* Look for matching private key */
  498. for (i = 0; i < sk_X509_num(certs); i++) {
  499. x = sk_X509_value(certs, i);
  500. if (X509_check_private_key(x, key)) {
  501. ee_cert = x;
  502. /* Zero keyid and alias */
  503. X509_keyid_set1(ee_cert, NULL, 0);
  504. X509_alias_set1(ee_cert, NULL, 0);
  505. /* Remove from list */
  506. (void)sk_X509_delete(certs, i);
  507. break;
  508. }
  509. }
  510. if (ee_cert == NULL) {
  511. BIO_printf(bio_err,
  512. "No cert in -in file '%s' matches private key\n",
  513. infile);
  514. goto export_end;
  515. }
  516. } else {
  517. ee_cert = X509_dup(sk_X509_value(certs, 0)); /* take 1st cert */
  518. }
  519. }
  520. /* Load any untrusted certificates for chain building */
  521. if (untrusted != NULL) {
  522. if (!load_certs(untrusted, &untrusted_certs, passcerts,
  523. "untrusted certificates"))
  524. goto export_end;
  525. }
  526. /* If chaining get chain from end entity cert */
  527. if (chain) {
  528. int vret;
  529. STACK_OF(X509) *chain2;
  530. X509_STORE *store;
  531. if (ee_cert == NULL) {
  532. BIO_printf(bio_err,
  533. "No end entity certificate to check with -chain\n");
  534. goto export_end;
  535. }
  536. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  537. CAstore, noCAstore))
  538. == NULL)
  539. goto export_end;
  540. vret = get_cert_chain(ee_cert, store, untrusted_certs, &chain2);
  541. X509_STORE_free(store);
  542. if (vret == X509_V_OK) {
  543. int add_certs;
  544. /* Remove from chain2 the first (end entity) certificate */
  545. X509_free(sk_X509_shift(chain2));
  546. /* Add the remaining certs (except for duplicates) */
  547. add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
  548. | X509_ADD_FLAG_NO_DUP);
  549. sk_X509_pop_free(chain2, X509_free);
  550. if (!add_certs)
  551. goto export_end;
  552. } else {
  553. if (vret != X509_V_ERR_UNSPECIFIED)
  554. BIO_printf(bio_err, "Error getting chain: %s\n",
  555. X509_verify_cert_error_string(vret));
  556. goto export_end;
  557. }
  558. }
  559. /* Add any extra certificates asked for */
  560. if (certfile != NULL) {
  561. if (!load_certs(certfile, &certs, passcerts,
  562. "extra certificates from -certfile"))
  563. goto export_end;
  564. }
  565. /* Add any CA names */
  566. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  567. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  568. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  569. }
  570. if (csp_name != NULL && key != NULL)
  571. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  572. MBSTRING_ASC, (unsigned char *)csp_name,
  573. -1);
  574. if (add_lmk && key != NULL)
  575. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  576. if (!noprompt && !(enc == NULL && maciter == -1)) {
  577. /* To avoid bit rot */
  578. if (1) {
  579. #ifndef OPENSSL_NO_UI_CONSOLE
  580. if (EVP_read_pw_string(pass, sizeof(pass),
  581. "Enter Export Password:", 1)) {
  582. BIO_printf(bio_err, "Can't read Password\n");
  583. goto export_end;
  584. }
  585. } else {
  586. #endif
  587. BIO_printf(bio_err, "Password required\n");
  588. goto export_end;
  589. }
  590. }
  591. if (!twopass)
  592. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  593. p12 = PKCS12_create(cpass, name, key, ee_cert, certs,
  594. key_pbe, cert_pbe, iter, -1, keytype);
  595. if (p12 == NULL) {
  596. BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
  597. outfile);
  598. goto export_end;
  599. }
  600. if (macalg != NULL) {
  601. if (!opt_md(macalg, &macmd))
  602. goto opthelp;
  603. }
  604. if (maciter != -1)
  605. PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
  606. assert(private);
  607. out = bio_open_owner(outfile, FORMAT_PKCS12, private);
  608. if (out == NULL)
  609. goto end;
  610. i2d_PKCS12_bio(out, p12);
  611. ret = 0;
  612. export_end:
  613. EVP_PKEY_free(key);
  614. sk_X509_pop_free(certs, X509_free);
  615. sk_X509_pop_free(untrusted_certs, X509_free);
  616. X509_free(ee_cert);
  617. ERR_print_errors(bio_err);
  618. goto end;
  619. }
  620. in = bio_open_default(infile, 'r', FORMAT_PKCS12);
  621. if (in == NULL)
  622. goto end;
  623. out = bio_open_owner(outfile, FORMAT_PEM, private);
  624. if (out == NULL)
  625. goto end;
  626. if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
  627. ERR_print_errors(bio_err);
  628. goto end;
  629. }
  630. if (!noprompt) {
  631. if (1) {
  632. #ifndef OPENSSL_NO_UI_CONSOLE
  633. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
  634. 0)) {
  635. BIO_printf(bio_err, "Can't read Password\n");
  636. goto end;
  637. }
  638. } else {
  639. #endif
  640. BIO_printf(bio_err, "Password required\n");
  641. goto end;
  642. }
  643. }
  644. if (!twopass)
  645. OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
  646. if ((options & INFO) && PKCS12_mac_present(p12)) {
  647. const ASN1_INTEGER *tmaciter;
  648. const X509_ALGOR *macalgid;
  649. const ASN1_OBJECT *macobj;
  650. const ASN1_OCTET_STRING *tmac;
  651. const ASN1_OCTET_STRING *tsalt;
  652. PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
  653. /* current hash algorithms do not use parameters so extract just name,
  654. in future alg_print() may be needed */
  655. X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
  656. BIO_puts(bio_err, "MAC: ");
  657. i2a_ASN1_OBJECT(bio_err, macobj);
  658. BIO_printf(bio_err, ", Iteration %ld\n",
  659. tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
  660. BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
  661. tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
  662. tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
  663. }
  664. if (macver) {
  665. /* If we enter empty password try no password first */
  666. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  667. /* If mac and crypto pass the same set it to NULL too */
  668. if (!twopass)
  669. cpass = NULL;
  670. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  671. /*
  672. * May be UTF8 from previous version of OpenSSL:
  673. * convert to a UTF8 form which will translate
  674. * to the same Unicode password.
  675. */
  676. unsigned char *utmp;
  677. int utmplen;
  678. utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
  679. if (utmp == NULL)
  680. goto end;
  681. badpass = OPENSSL_uni2utf8(utmp, utmplen);
  682. OPENSSL_free(utmp);
  683. if (!PKCS12_verify_mac(p12, badpass, -1)) {
  684. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  685. ERR_print_errors(bio_err);
  686. goto end;
  687. } else {
  688. BIO_printf(bio_err, "Warning: using broken algorithm\n");
  689. if (!twopass)
  690. cpass = badpass;
  691. }
  692. }
  693. }
  694. assert(private);
  695. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
  696. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  697. ERR_print_errors(bio_err);
  698. goto end;
  699. }
  700. ret = 0;
  701. end:
  702. PKCS12_free(p12);
  703. release_engine(e);
  704. BIO_free(in);
  705. BIO_free_all(out);
  706. sk_OPENSSL_STRING_free(canames);
  707. OPENSSL_free(badpass);
  708. OPENSSL_free(passcerts);
  709. OPENSSL_free(passin);
  710. OPENSSL_free(passout);
  711. return ret;
  712. }
  713. int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
  714. int passlen, int options, char *pempass,
  715. const EVP_CIPHER *enc)
  716. {
  717. STACK_OF(PKCS7) *asafes = NULL;
  718. STACK_OF(PKCS12_SAFEBAG) *bags;
  719. int i, bagnid;
  720. int ret = 0;
  721. PKCS7 *p7;
  722. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  723. return 0;
  724. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  725. p7 = sk_PKCS7_value(asafes, i);
  726. bagnid = OBJ_obj2nid(p7->type);
  727. if (bagnid == NID_pkcs7_data) {
  728. bags = PKCS12_unpack_p7data(p7);
  729. if (options & INFO)
  730. BIO_printf(bio_err, "PKCS7 Data\n");
  731. } else if (bagnid == NID_pkcs7_encrypted) {
  732. if (options & INFO) {
  733. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  734. alg_print(p7->d.encrypted->enc_data->algorithm);
  735. }
  736. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  737. } else {
  738. continue;
  739. }
  740. if (!bags)
  741. goto err;
  742. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  743. options, pempass, enc)) {
  744. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  745. goto err;
  746. }
  747. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  748. bags = NULL;
  749. }
  750. ret = 1;
  751. err:
  752. sk_PKCS7_pop_free(asafes, PKCS7_free);
  753. return ret;
  754. }
  755. int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
  756. const char *pass, int passlen, int options,
  757. char *pempass, const EVP_CIPHER *enc)
  758. {
  759. int i;
  760. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  761. if (!dump_certs_pkeys_bag(out,
  762. sk_PKCS12_SAFEBAG_value(bags, i),
  763. pass, passlen, options, pempass, enc))
  764. return 0;
  765. }
  766. return 1;
  767. }
  768. int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
  769. const char *pass, int passlen, int options,
  770. char *pempass, const EVP_CIPHER *enc)
  771. {
  772. EVP_PKEY *pkey;
  773. PKCS8_PRIV_KEY_INFO *p8;
  774. const PKCS8_PRIV_KEY_INFO *p8c;
  775. X509 *x509;
  776. const STACK_OF(X509_ATTRIBUTE) *attrs;
  777. int ret = 0;
  778. attrs = PKCS12_SAFEBAG_get0_attrs(bag);
  779. switch (PKCS12_SAFEBAG_get_nid(bag)) {
  780. case NID_keyBag:
  781. if (options & INFO)
  782. BIO_printf(bio_err, "Key bag\n");
  783. if (options & NOKEYS)
  784. return 1;
  785. print_attribs(out, attrs, "Bag Attributes");
  786. p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
  787. if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
  788. return 0;
  789. print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
  790. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  791. EVP_PKEY_free(pkey);
  792. break;
  793. case NID_pkcs8ShroudedKeyBag:
  794. if (options & INFO) {
  795. const X509_SIG *tp8;
  796. const X509_ALGOR *tp8alg;
  797. BIO_printf(bio_err, "Shrouded Keybag: ");
  798. tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
  799. X509_SIG_get0(tp8, &tp8alg, NULL);
  800. alg_print(tp8alg);
  801. }
  802. if (options & NOKEYS)
  803. return 1;
  804. print_attribs(out, attrs, "Bag Attributes");
  805. if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
  806. return 0;
  807. if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
  808. PKCS8_PRIV_KEY_INFO_free(p8);
  809. return 0;
  810. }
  811. print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
  812. PKCS8_PRIV_KEY_INFO_free(p8);
  813. ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  814. EVP_PKEY_free(pkey);
  815. break;
  816. case NID_certBag:
  817. if (options & INFO)
  818. BIO_printf(bio_err, "Certificate bag\n");
  819. if (options & NOCERTS)
  820. return 1;
  821. if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
  822. if (options & CACERTS)
  823. return 1;
  824. } else if (options & CLCERTS)
  825. return 1;
  826. print_attribs(out, attrs, "Bag Attributes");
  827. if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
  828. return 1;
  829. if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
  830. return 0;
  831. dump_cert_text(out, x509);
  832. ret = PEM_write_bio_X509(out, x509);
  833. X509_free(x509);
  834. break;
  835. case NID_secretBag:
  836. if (options & INFO)
  837. BIO_printf(bio_err, "Secret bag\n");
  838. print_attribs(out, attrs, "Bag Attributes");
  839. BIO_printf(bio_err, "Bag Type: ");
  840. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
  841. BIO_printf(bio_err, "\nBag Value: ");
  842. print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
  843. return 1;
  844. case NID_safeContentsBag:
  845. if (options & INFO)
  846. BIO_printf(bio_err, "Safe Contents bag\n");
  847. print_attribs(out, attrs, "Bag Attributes");
  848. return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
  849. pass, passlen, options, pempass, enc);
  850. default:
  851. BIO_printf(bio_err, "Warning unsupported bag type: ");
  852. i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
  853. BIO_printf(bio_err, "\n");
  854. return 1;
  855. }
  856. return ret;
  857. }
  858. /* Given a single certificate return a verified chain or NULL if error */
  859. static int get_cert_chain(X509 *cert, X509_STORE *store,
  860. STACK_OF(X509) *untrusted_certs,
  861. STACK_OF(X509) **chain)
  862. {
  863. X509_STORE_CTX *store_ctx = NULL;
  864. STACK_OF(X509) *chn = NULL;
  865. int i = 0;
  866. store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
  867. if (store_ctx == NULL) {
  868. i = X509_V_ERR_UNSPECIFIED;
  869. goto end;
  870. }
  871. if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
  872. i = X509_V_ERR_UNSPECIFIED;
  873. goto end;
  874. }
  875. if (X509_verify_cert(store_ctx) > 0)
  876. chn = X509_STORE_CTX_get1_chain(store_ctx);
  877. else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
  878. i = X509_V_ERR_UNSPECIFIED;
  879. end:
  880. X509_STORE_CTX_free(store_ctx);
  881. *chain = chn;
  882. return i;
  883. }
  884. static int alg_print(const X509_ALGOR *alg)
  885. {
  886. int pbenid, aparamtype;
  887. const ASN1_OBJECT *aoid;
  888. const void *aparam;
  889. PBEPARAM *pbe = NULL;
  890. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
  891. pbenid = OBJ_obj2nid(aoid);
  892. BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
  893. /*
  894. * If PBE algorithm is PBES2 decode algorithm parameters
  895. * for additional details.
  896. */
  897. if (pbenid == NID_pbes2) {
  898. PBE2PARAM *pbe2 = NULL;
  899. int encnid;
  900. if (aparamtype == V_ASN1_SEQUENCE)
  901. pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
  902. if (pbe2 == NULL) {
  903. BIO_puts(bio_err, ", <unsupported parameters>");
  904. goto done;
  905. }
  906. X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
  907. pbenid = OBJ_obj2nid(aoid);
  908. X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
  909. encnid = OBJ_obj2nid(aoid);
  910. BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
  911. OBJ_nid2sn(encnid));
  912. /* If KDF is PBKDF2 decode parameters */
  913. if (pbenid == NID_id_pbkdf2) {
  914. PBKDF2PARAM *kdf = NULL;
  915. int prfnid;
  916. if (aparamtype == V_ASN1_SEQUENCE)
  917. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
  918. if (kdf == NULL) {
  919. BIO_puts(bio_err, ", <unsupported parameters>");
  920. goto done;
  921. }
  922. if (kdf->prf == NULL) {
  923. prfnid = NID_hmacWithSHA1;
  924. } else {
  925. X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
  926. prfnid = OBJ_obj2nid(aoid);
  927. }
  928. BIO_printf(bio_err, ", Iteration %ld, PRF %s",
  929. ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
  930. PBKDF2PARAM_free(kdf);
  931. #ifndef OPENSSL_NO_SCRYPT
  932. } else if (pbenid == NID_id_scrypt) {
  933. SCRYPT_PARAMS *kdf = NULL;
  934. if (aparamtype == V_ASN1_SEQUENCE)
  935. kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
  936. if (kdf == NULL) {
  937. BIO_puts(bio_err, ", <unsupported parameters>");
  938. goto done;
  939. }
  940. BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
  941. "Block size(r): %ld, Parallelism(p): %ld",
  942. ASN1_STRING_length(kdf->salt),
  943. ASN1_INTEGER_get(kdf->costParameter),
  944. ASN1_INTEGER_get(kdf->blockSize),
  945. ASN1_INTEGER_get(kdf->parallelizationParameter));
  946. SCRYPT_PARAMS_free(kdf);
  947. #endif
  948. }
  949. PBE2PARAM_free(pbe2);
  950. } else {
  951. if (aparamtype == V_ASN1_SEQUENCE)
  952. pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
  953. if (pbe == NULL) {
  954. BIO_puts(bio_err, ", <unsupported parameters>");
  955. goto done;
  956. }
  957. BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
  958. PBEPARAM_free(pbe);
  959. }
  960. done:
  961. BIO_puts(bio_err, "\n");
  962. return 1;
  963. }
  964. /* Load all certificates from a given file */
  965. int cert_load(BIO *in, STACK_OF(X509) *sk)
  966. {
  967. int ret = 0;
  968. X509 *cert;
  969. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  970. ret = 1;
  971. if (!sk_X509_push(sk, cert))
  972. return 0;
  973. }
  974. if (ret)
  975. ERR_clear_error();
  976. return ret;
  977. }
  978. /* Generalised x509 attribute value print */
  979. void print_attribute(BIO *out, const ASN1_TYPE *av)
  980. {
  981. char *value;
  982. switch (av->type) {
  983. case V_ASN1_BMPSTRING:
  984. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  985. av->value.bmpstring->length);
  986. BIO_printf(out, "%s\n", value);
  987. OPENSSL_free(value);
  988. break;
  989. case V_ASN1_UTF8STRING:
  990. BIO_printf(out, "%s\n", av->value.utf8string->data);
  991. break;
  992. case V_ASN1_OCTET_STRING:
  993. hex_prin(out, av->value.octet_string->data,
  994. av->value.octet_string->length);
  995. BIO_printf(out, "\n");
  996. break;
  997. case V_ASN1_BIT_STRING:
  998. hex_prin(out, av->value.bit_string->data,
  999. av->value.bit_string->length);
  1000. BIO_printf(out, "\n");
  1001. break;
  1002. default:
  1003. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  1004. break;
  1005. }
  1006. }
  1007. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  1008. int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
  1009. const char *name)
  1010. {
  1011. X509_ATTRIBUTE *attr;
  1012. ASN1_TYPE *av;
  1013. int i, j, attr_nid;
  1014. if (!attrlst) {
  1015. BIO_printf(out, "%s: <No Attributes>\n", name);
  1016. return 1;
  1017. }
  1018. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  1019. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  1020. return 1;
  1021. }
  1022. BIO_printf(out, "%s\n", name);
  1023. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  1024. ASN1_OBJECT *attr_obj;
  1025. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  1026. attr_obj = X509_ATTRIBUTE_get0_object(attr);
  1027. attr_nid = OBJ_obj2nid(attr_obj);
  1028. BIO_printf(out, " ");
  1029. if (attr_nid == NID_undef) {
  1030. i2a_ASN1_OBJECT(out, attr_obj);
  1031. BIO_printf(out, ": ");
  1032. } else {
  1033. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  1034. }
  1035. if (X509_ATTRIBUTE_count(attr)) {
  1036. for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
  1037. {
  1038. av = X509_ATTRIBUTE_get0_type(attr, j);
  1039. print_attribute(out, av);
  1040. }
  1041. } else {
  1042. BIO_printf(out, "<No Values>\n");
  1043. }
  1044. }
  1045. return 1;
  1046. }
  1047. void hex_prin(BIO *out, unsigned char *buf, int len)
  1048. {
  1049. int i;
  1050. for (i = 0; i < len; i++)
  1051. BIO_printf(out, "%02X ", buf[i]);
  1052. }
  1053. static int set_pbe(int *ppbe, const char *str)
  1054. {
  1055. if (!str)
  1056. return 0;
  1057. if (strcmp(str, "NONE") == 0) {
  1058. *ppbe = -1;
  1059. return 1;
  1060. }
  1061. *ppbe = OBJ_txt2nid(str);
  1062. if (*ppbe == NID_undef) {
  1063. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  1064. return 0;
  1065. }
  1066. return 1;
  1067. }