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