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