pkcs12.c 37 KB

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