pkcs12.c 38 KB

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