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