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