]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkcs12.c
Make the '-in' option in apps/passwd.c less mandatory
[thirdparty/openssl.git] / apps / pkcs12.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
9a48b07e 3 * project.
ee0508d4
DSH
4 */
5/* ====================================================================
f3dea9a5 6 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
ee0508d4
DSH
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
ee0508d4
DSH
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
6e04afb8 59#include <openssl/opensslconf.h>
96bea000
MC
60#if defined(OPENSSL_NO_DES)
61NON_EMPTY_TRANSLATION_UNIT
62#else
6e04afb8 63
0f113f3e
MC
64# include <stdio.h>
65# include <stdlib.h>
66# include <string.h>
67# include "apps.h"
68# include <openssl/crypto.h>
69# include <openssl/err.h>
70# include <openssl/pem.h>
71# include <openssl/pkcs12.h>
ee0508d4 72
0f113f3e
MC
73# define NOKEYS 0x1
74# define NOCERTS 0x2
75# define INFO 0x4
76# define CLCERTS 0x8
77# define CACERTS 0x10
78
e29c73c9
VD
79static int get_cert_chain(X509 *cert, X509_STORE *store,
80 STACK_OF(X509) **chain);
0f113f3e 81int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
7e1b7485 82 int options, char *pempass, const EVP_CIPHER *enc);
0f113f3e 83int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
7e1b7485
RS
84 char *pass, int passlen, int options, char *pempass,
85 const EVP_CIPHER *enc);
0f113f3e 86int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
7e1b7485
RS
87 int passlen, int options, char *pempass,
88 const EVP_CIPHER *enc);
0f113f3e
MC
89int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
90 const char *name);
ee0508d4 91void hex_prin(BIO *out, unsigned char *buf, int len);
ecf3a1fb 92static int alg_print(X509_ALGOR *alg);
84c15db5 93int cert_load(BIO *in, STACK_OF(X509) *sk);
7e1b7485
RS
94static int set_pbe(int *ppbe, const char *str);
95
96typedef enum OPTION_choice {
97 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
98 OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS,
99 OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER,
100 OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
101 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
102 OPT_RAND, OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME,
103 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
2b6bcb70 104 OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE
7e1b7485
RS
105} OPTION_CHOICE;
106
107OPTIONS pkcs12_options[] = {
108 {"help", OPT_HELP, '-', "Display this summary"},
109 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
110 {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"},
111 {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"},
112 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
113 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
114 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
115 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"},
116 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
117 {"chain", OPT_CHAIN, '-', "Add certificate chain"},
118 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
119 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
120# ifndef OPENSSL_NO_RC2
121 {"descert", OPT_DESCERT, '-',
122 "Encrypt output with 3DES (default RC2-40)"},
123 {"certpbe", OPT_CERTPBE, 's',
124 "Certificate PBE algorithm (default RC2-40)"},
125# else
126 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
127 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
128# endif
129 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
130 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
131 {"maciter", OPT_MACITER, '-', "Use MAC iteration"},
132 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
133 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
134 {"LMK", OPT_LMK, '-',
135 "Add local machine keyset attribute to private key"},
136 {"nodes", OPT_NODES, '-', "Don't encrypt private keys"},
137 {"macalg", OPT_MACALG, 's',
138 "Digest algorithm used in MAC (default SHA1)"},
139 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
140 {"rand", OPT_RAND, 's',
141 "Load the file(s) into the random number generator"},
142 {"inkey", OPT_INKEY, '<', "Private key if not infile"},
143 {"certfile", OPT_CERTFILE, '<', "Load certs from file"},
144 {"name", OPT_NAME, 's', "Use name as friendly name"},
145 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
146 {"caname", OPT_CANAME, 's',
147 "Use name as CA friendly name (can be repeated)"},
148 {"in", OPT_IN, '<', "Input filename"},
149 {"out", OPT_OUT, '>', "Output filename"},
150 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
151 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
152 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
153 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
154 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
2b6bcb70
MC
155 {"no-CAfile", OPT_NOCAFILE, '-',
156 "Do not load the default certificates file"},
157 {"no-CApath", OPT_NOCAPATH, '-',
158 "Do not load certificates from the default certificates directory"},
9c3bcfa0 159 {"", OPT_CIPHER, '-', "Any supported cipher"},
7e1b7485
RS
160# ifndef OPENSSL_NO_ENGINE
161 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
162# endif
7e1b7485
RS
163 {NULL}
164};
667ac4ec 165
7e1b7485 166int pkcs12_main(int argc, char **argv)
ee0508d4 167{
7e1b7485
RS
168 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
169 char *name = NULL, *csp_name = NULL;
65d3941f 170 char pass[2048], macpass[2048];
7e1b7485
RS
171 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
172 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
173# ifndef OPENSSL_NO_RC2
ee0508d4 174 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
7e1b7485
RS
175# else
176 int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
177# endif
525f51f6 178 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
3b061a00 179 int ret = 1, macver = 1, noprompt = 0, add_lmk = 0, private = 0;
7e1b7485
RS
180 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
181 char *passin = NULL, *passout = NULL, *inrand = NULL, *macalg = NULL;
182 char *cpass = NULL, *mpass = NULL, *CApath = NULL, *CAfile = NULL;
333b070e 183 char *prog;
2b6bcb70 184 int noCApath = 0, noCAfile = 0;
7e1b7485
RS
185 ENGINE *e = NULL;
186 BIO *in = NULL, *out = NULL;
187 PKCS12 *p12 = NULL;
c869da88 188 STACK_OF(OPENSSL_STRING) *canames = NULL;
7e1b7485
RS
189 const EVP_CIPHER *enc = EVP_des_ede3_cbc();
190 OPTION_CHOICE o;
191
192 prog = opt_init(argc, argv, pkcs12_options);
193 while ((o = opt_next()) != OPT_EOF) {
194 switch (o) {
195 case OPT_EOF:
196 case OPT_ERR:
197 opthelp:
198 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
199 goto end;
200 case OPT_HELP:
201 opt_help(pkcs12_options);
202 ret = 0;
203 goto end;
204 case OPT_NOKEYS:
205 options |= NOKEYS;
206 break;
207 case OPT_KEYEX:
208 keytype = KEY_EX;
209 break;
210 case OPT_KEYSIG:
211 keytype = KEY_SIG;
212 break;
213 case OPT_NOCERTS:
214 options |= NOCERTS;
215 break;
216 case OPT_CLCERTS:
217 options |= CLCERTS;
218 break;
219 case OPT_CACERTS:
220 options |= CACERTS;
221 break;
222 case OPT_NOOUT:
223 options |= (NOKEYS | NOCERTS);
224 break;
225 case OPT_INFO:
226 options |= INFO;
227 break;
228 case OPT_CHAIN:
229 chain = 1;
230 break;
231 case OPT_TWOPASS:
232 twopass = 1;
233 break;
234 case OPT_NOMACVER:
235 macver = 0;
236 break;
237 case OPT_DESCERT:
238 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
239 break;
240 case OPT_EXPORT:
241 export_cert = 1;
242 break;
243 case OPT_CIPHER:
244 if (!opt_cipher(opt_unknown(), &enc))
245 goto opthelp;
246 break;
247 case OPT_NOITER:
248 iter = 1;
249 break;
250 case OPT_MACITER:
251 maciter = PKCS12_DEFAULT_ITER;
252 break;
253 case OPT_NOMACITER:
254 maciter = 1;
255 break;
256 case OPT_NOMAC:
257 maciter = -1;
258 break;
259 case OPT_MACALG:
260 macalg = opt_arg();
261 break;
262 case OPT_NODES:
263 enc = NULL;
264 break;
265 case OPT_CERTPBE:
266 if (!set_pbe(&cert_pbe, opt_arg()))
267 goto opthelp;
268 break;
269 case OPT_KEYPBE:
270 if (!set_pbe(&key_pbe, opt_arg()))
271 goto opthelp;
272 break;
273 case OPT_RAND:
274 inrand = opt_arg();
275 break;
276 case OPT_INKEY:
277 keyname = opt_arg();
278 break;
279 case OPT_CERTFILE:
280 certfile = opt_arg();
281 break;
282 case OPT_NAME:
283 name = opt_arg();
284 break;
285 case OPT_LMK:
286 add_lmk = 1;
287 break;
288 case OPT_CSP:
289 csp_name = opt_arg();
290 break;
291 case OPT_CANAME:
292 if (canames == NULL
293 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
294 goto end;
295 sk_OPENSSL_STRING_push(canames, opt_arg());
296 break;
297 case OPT_IN:
298 infile = opt_arg();
299 break;
300 case OPT_OUT:
301 outfile = opt_arg();
302 break;
303 case OPT_PASSIN:
304 passinarg = opt_arg();
305 break;
306 case OPT_PASSOUT:
307 passoutarg = opt_arg();
308 break;
309 case OPT_PASSWORD:
310 passarg = opt_arg();
311 break;
312 case OPT_CAPATH:
313 CApath = opt_arg();
314 break;
315 case OPT_CAFILE:
316 CAfile = opt_arg();
317 break;
2b6bcb70
MC
318 case OPT_NOCAPATH:
319 noCApath = 1;
320 break;
321 case OPT_NOCAFILE:
322 noCAfile = 1;
323 break;
7e1b7485 324 case OPT_ENGINE:
333b070e 325 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
326 break;
327 }
ee0508d4 328 }
7e1b7485 329 argc = opt_num_rest();
03358517
KR
330 if (argc != 0)
331 goto opthelp;
332
3b061a00 333 private = 1;
ee0508d4 334
0f113f3e
MC
335 if (passarg) {
336 if (export_cert)
7e1b7485 337 passoutarg = passarg;
0f113f3e 338 else
7e1b7485 339 passinarg = passarg;
a3fe382e
DSH
340 }
341
7e1b7485 342 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
343 BIO_printf(bio_err, "Error getting passwords\n");
344 goto end;
a3fe382e
DSH
345 }
346
0f113f3e
MC
347 if (!cpass) {
348 if (export_cert)
349 cpass = passout;
350 else
351 cpass = passin;
f07fb9b2
DSH
352 }
353
0f113f3e
MC
354 if (cpass) {
355 mpass = cpass;
356 noprompt = 1;
f07fb9b2 357 } else {
0f113f3e
MC
358 cpass = pass;
359 mpass = macpass;
e40b7abe
DSH
360 }
361
0f113f3e 362 if (export_cert || inrand) {
7e1b7485 363 app_RAND_load_file(NULL, (inrand != NULL));
d13e4eb0 364 if (inrand != NULL)
0f113f3e
MC
365 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
366 app_RAND_load_files(inrand));
d13e4eb0 367 }
a873356c 368
ee0508d4 369 if (twopass) {
0f113f3e
MC
370 if (EVP_read_pw_string
371 (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
372 BIO_printf(bio_err, "Can't read Password\n");
373 goto end;
374 }
ee0508d4
DSH
375 }
376
2d681b77 377 if (export_cert) {
0f113f3e
MC
378 EVP_PKEY *key = NULL;
379 X509 *ucert = NULL, *x = NULL;
380 STACK_OF(X509) *certs = NULL;
381 const EVP_MD *macmd = NULL;
382 unsigned char *catmp = NULL;
383 int i;
384
385 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
386 BIO_printf(bio_err, "Nothing to do!\n");
387 goto export_end;
388 }
389
390 if (options & NOCERTS)
391 chain = 0;
392
0f113f3e 393 if (!(options & NOKEYS)) {
7e1b7485 394 key = load_key(keyname ? keyname : infile,
0f113f3e
MC
395 FORMAT_PEM, 1, passin, e, "private key");
396 if (!key)
397 goto export_end;
398 }
0f113f3e
MC
399
400 /* Load in all certs in input file */
401 if (!(options & NOCERTS)) {
a773b52a 402 if (!load_certs(infile, &certs, FORMAT_PEM, NULL,
0996dc54 403 "certificates"))
0f113f3e
MC
404 goto export_end;
405
406 if (key) {
407 /* Look for matching private key */
408 for (i = 0; i < sk_X509_num(certs); i++) {
409 x = sk_X509_value(certs, i);
410 if (X509_check_private_key(x, key)) {
411 ucert = x;
412 /* Zero keyid and alias */
413 X509_keyid_set1(ucert, NULL, 0);
414 X509_alias_set1(ucert, NULL, 0);
415 /* Remove from list */
416 (void)sk_X509_delete(certs, i);
417 break;
418 }
419 }
420 if (!ucert) {
421 BIO_printf(bio_err,
422 "No certificate matches private key\n");
423 goto export_end;
424 }
425 }
426
427 }
0f113f3e
MC
428
429 /* Add any more certificates asked for */
430 if (certfile) {
a773b52a 431 if (!load_certs(certfile, &certs, FORMAT_PEM, NULL,
0996dc54 432 "certificates from certfile"))
0f113f3e 433 goto export_end;
0f113f3e 434 }
0f113f3e
MC
435
436 /* If chaining get chain from user cert */
437 if (chain) {
438 int vret;
439 STACK_OF(X509) *chain2;
7e1b7485 440 X509_STORE *store;
2b6bcb70
MC
441 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath))
442 == NULL)
0f113f3e 443 goto export_end;
0f113f3e
MC
444
445 vret = get_cert_chain(ucert, store, &chain2);
446 X509_STORE_free(store);
447
e29c73c9 448 if (vret == X509_V_OK) {
0f113f3e
MC
449 /* Exclude verified certificate */
450 for (i = 1; i < sk_X509_num(chain2); i++)
451 sk_X509_push(certs, sk_X509_value(chain2, i));
452 /* Free first certificate */
453 X509_free(sk_X509_value(chain2, 0));
454 sk_X509_free(chain2);
455 } else {
e29c73c9 456 if (vret != X509_V_ERR_UNSPECIFIED)
0f113f3e
MC
457 BIO_printf(bio_err, "Error %s getting chain.\n",
458 X509_verify_cert_error_string(vret));
459 else
460 ERR_print_errors(bio_err);
461 goto export_end;
462 }
463 }
464
465 /* Add any CA names */
466
467 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
468 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
469 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
470 }
471
472 if (csp_name && key)
473 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
474 MBSTRING_ASC, (unsigned char *)csp_name,
475 -1);
476
477 if (add_lmk && key)
478 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
479
0f113f3e
MC
480 if (!noprompt &&
481 EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
482 1)) {
483 BIO_printf(bio_err, "Can't read Password\n");
484 goto export_end;
485 }
486 if (!twopass)
7644a9ae 487 OPENSSL_strlcpy(macpass, pass, sizeof macpass);
0f113f3e 488
0f113f3e
MC
489 p12 = PKCS12_create(cpass, name, key, ucert, certs,
490 key_pbe, cert_pbe, iter, -1, keytype);
491
492 if (!p12) {
493 ERR_print_errors(bio_err);
494 goto export_end;
495 }
496
497 if (macalg) {
7e1b7485
RS
498 if (!opt_md(macalg, &macmd))
499 goto opthelp;
0f113f3e
MC
500 }
501
502 if (maciter != -1)
503 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
504
3b061a00 505 assert(private);
bdd58d98
RL
506
507 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
508 if (out == NULL)
509 goto end;
510
0f113f3e
MC
511 i2d_PKCS12_bio(out, p12);
512
513 ret = 0;
514
515 export_end:
0f113f3e 516
c5ba2d99 517 EVP_PKEY_free(key);
222561fe
RS
518 sk_X509_pop_free(certs, X509_free);
519 X509_free(ucert);
0f113f3e 520
0f113f3e 521 goto end;
ee0508d4 522
ee86c3f5 523 }
ee0508d4 524
bdd58d98
RL
525 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
526 if (in == NULL)
527 goto end;
528 out = bio_open_owner(outfile, FORMAT_PEM, private);
529 if (out == NULL)
530 goto end;
531
75ebbd9a 532 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
0f113f3e
MC
533 ERR_print_errors(bio_err);
534 goto end;
ee0508d4 535 }
7e1b7485 536
0f113f3e
MC
537 if (!noprompt
538 && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
539 0)) {
540 BIO_printf(bio_err, "Can't read Password\n");
541 goto end;
ee0508d4 542 }
0f113f3e
MC
543
544 if (!twopass)
7644a9ae 545 OPENSSL_strlcpy(macpass, pass, sizeof macpass);
0f113f3e 546
776cfa9b
DSH
547 if ((options & INFO) && PKCS12_mac_present(p12)) {
548 ASN1_INTEGER *tmaciter;
c9018bdf 549
776cfa9b 550 PKCS12_get0_mac(NULL, NULL, NULL, &tmaciter, p12);
0f113f3e 551 BIO_printf(bio_err, "MAC Iteration %ld\n",
c9018bdf 552 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
776cfa9b 553 }
0f113f3e 554 if (macver) {
0f113f3e
MC
555 /* If we enter empty password try no password first */
556 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
557 /* If mac and crypto pass the same set it to NULL too */
558 if (!twopass)
559 cpass = NULL;
560 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
561 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
562 ERR_print_errors(bio_err);
563 goto end;
564 }
ee0508d4 565 }
7e1b7485 566
3b061a00 567 assert(private);
7e1b7485 568 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
0f113f3e
MC
569 BIO_printf(bio_err, "Error outputting keys and certificates\n");
570 ERR_print_errors(bio_err);
571 goto end;
ee0508d4 572 }
ee0508d4 573 ret = 0;
88364bc2 574 end:
e0e920b1 575 PKCS12_free(p12);
0f113f3e 576 if (export_cert || inrand)
7e1b7485 577 app_RAND_write_file(NULL);
a873356c 578 BIO_free(in);
645749ef 579 BIO_free_all(out);
25aaa98a 580 sk_OPENSSL_STRING_free(canames);
b548a1f1
RS
581 OPENSSL_free(passin);
582 OPENSSL_free(passout);
7e1b7485 583 return (ret);
ee0508d4
DSH
584}
585
0f113f3e 586int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
7e1b7485
RS
587 int passlen, int options, char *pempass,
588 const EVP_CIPHER *enc)
ee0508d4 589{
0f113f3e
MC
590 STACK_OF(PKCS7) *asafes = NULL;
591 STACK_OF(PKCS12_SAFEBAG) *bags;
592 int i, bagnid;
593 int ret = 0;
594 PKCS7 *p7;
595
75ebbd9a 596 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
0f113f3e
MC
597 return 0;
598 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
599 p7 = sk_PKCS7_value(asafes, i);
600 bagnid = OBJ_obj2nid(p7->type);
601 if (bagnid == NID_pkcs7_data) {
602 bags = PKCS12_unpack_p7data(p7);
603 if (options & INFO)
604 BIO_printf(bio_err, "PKCS7 Data\n");
605 } else if (bagnid == NID_pkcs7_encrypted) {
606 if (options & INFO) {
607 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
ecf3a1fb 608 alg_print(p7->d.encrypted->enc_data->algorithm);
0f113f3e
MC
609 }
610 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
611 } else
612 continue;
613 if (!bags)
614 goto err;
615 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
7e1b7485 616 options, pempass, enc)) {
0f113f3e
MC
617 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
618 goto err;
619 }
620 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
621 bags = NULL;
622 }
623 ret = 1;
624
625 err:
e0e920b1 626 sk_PKCS7_pop_free(asafes, PKCS7_free);
0f113f3e 627 return ret;
ee0508d4
DSH
628}
629
0f113f3e 630int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
7e1b7485
RS
631 char *pass, int passlen, int options, char *pempass,
632 const EVP_CIPHER *enc)
ee0508d4 633{
0f113f3e
MC
634 int i;
635 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
636 if (!dump_certs_pkeys_bag(out,
637 sk_PKCS12_SAFEBAG_value(bags, i),
7e1b7485 638 pass, passlen, options, pempass, enc))
0f113f3e
MC
639 return 0;
640 }
641 return 1;
ee0508d4
DSH
642}
643
0f113f3e 644int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
7e1b7485
RS
645 int passlen, int options, char *pempass,
646 const EVP_CIPHER *enc)
ee0508d4 647{
0f113f3e
MC
648 EVP_PKEY *pkey;
649 PKCS8_PRIV_KEY_INFO *p8;
650 X509 *x509;
776cfa9b
DSH
651 STACK_OF(X509_ATTRIBUTE) *attrs;
652
653 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
0f113f3e 654
762ee38d 655 switch (PKCS12_SAFEBAG_get_nid(bag)) {
0f113f3e
MC
656 case NID_keyBag:
657 if (options & INFO)
658 BIO_printf(bio_err, "Key bag\n");
659 if (options & NOKEYS)
660 return 1;
776cfa9b
DSH
661 print_attribs(out, attrs, "Bag Attributes");
662 p8 = PKCS12_SAFEBAG_get0_p8inf(bag);
75ebbd9a 663 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL)
0f113f3e 664 return 0;
54dbf423 665 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
0f113f3e
MC
666 PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
667 EVP_PKEY_free(pkey);
668 break;
669
670 case NID_pkcs8ShroudedKeyBag:
671 if (options & INFO) {
776cfa9b 672 X509_SIG *tp8;
a6eb1ce6 673 X509_ALGOR *tp8alg;
c9018bdf 674
0f113f3e 675 BIO_printf(bio_err, "Shrouded Keybag: ");
776cfa9b 676 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
a6eb1ce6
DSH
677 X509_SIG_get0(&tp8alg, NULL, tp8);
678 alg_print(tp8alg);
0f113f3e
MC
679 }
680 if (options & NOKEYS)
681 return 1;
776cfa9b 682 print_attribs(out, attrs, "Bag Attributes");
75ebbd9a 683 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
0f113f3e 684 return 0;
75ebbd9a 685 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
0f113f3e
MC
686 PKCS8_PRIV_KEY_INFO_free(p8);
687 return 0;
688 }
54dbf423 689 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
0f113f3e
MC
690 PKCS8_PRIV_KEY_INFO_free(p8);
691 PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
692 EVP_PKEY_free(pkey);
693 break;
694
695 case NID_certBag:
696 if (options & INFO)
697 BIO_printf(bio_err, "Certificate bag\n");
698 if (options & NOCERTS)
699 return 1;
762ee38d 700 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
0f113f3e
MC
701 if (options & CACERTS)
702 return 1;
703 } else if (options & CLCERTS)
704 return 1;
776cfa9b 705 print_attribs(out, attrs, "Bag Attributes");
762ee38d 706 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
0f113f3e 707 return 1;
762ee38d 708 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
0f113f3e
MC
709 return 0;
710 dump_cert_text(out, x509);
711 PEM_write_bio_X509(out, x509);
712 X509_free(x509);
713 break;
714
715 case NID_safeContentsBag:
716 if (options & INFO)
717 BIO_printf(bio_err, "Safe Contents bag\n");
776cfa9b
DSH
718 print_attribs(out, attrs, "Bag Attributes");
719 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
720 pass, passlen, options, pempass, enc);
0f113f3e
MC
721
722 default:
723 BIO_printf(bio_err, "Warning unsupported bag type: ");
776cfa9b 724 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
0f113f3e
MC
725 BIO_printf(bio_err, "\n");
726 return 1;
0f113f3e
MC
727 }
728 return 1;
ee0508d4
DSH
729}
730
731/* Given a single certificate return a verified chain or NULL if error */
732
e29c73c9
VD
733static int get_cert_chain(X509 *cert, X509_STORE *store,
734 STACK_OF(X509) **chain)
ee0508d4 735{
0f113f3e 736 X509_STORE_CTX store_ctx;
e29c73c9 737 STACK_OF(X509) *chn = NULL;
0f113f3e
MC
738 int i = 0;
739
e29c73c9
VD
740 if (!X509_STORE_CTX_init(&store_ctx, store, cert, NULL)) {
741 *chain = NULL;
742 return X509_V_ERR_UNSPECIFIED;
743 }
744
745 if (X509_verify_cert(&store_ctx) > 0)
0f113f3e 746 chn = X509_STORE_CTX_get1_chain(&store_ctx);
e29c73c9
VD
747 else if ((i = X509_STORE_CTX_get_error(&store_ctx)) == 0)
748 i = X509_V_ERR_UNSPECIFIED;
749
0f113f3e
MC
750 X509_STORE_CTX_cleanup(&store_ctx);
751 *chain = chn;
0f113f3e
MC
752 return i;
753}
754
ecf3a1fb 755static int alg_print(X509_ALGOR *alg)
ee0508d4 756{
0f113f3e 757 PBEPARAM *pbe;
ecf3a1fb
RS
758 const unsigned char *p = alg->parameter->value.sequence->data;
759
0f113f3e
MC
760 pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
761 if (!pbe)
762 return 1;
763 BIO_printf(bio_err, "%s, Iteration %ld\n",
764 OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
765 ASN1_INTEGER_get(pbe->iter));
766 PBEPARAM_free(pbe);
767 return 1;
ee0508d4
DSH
768}
769
770/* Load all certificates from a given file */
771
84c15db5 772int cert_load(BIO *in, STACK_OF(X509) *sk)
ee0508d4 773{
0f113f3e
MC
774 int ret;
775 X509 *cert;
776 ret = 0;
0f113f3e 777 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
0f113f3e
MC
778 ret = 1;
779 sk_X509_push(sk, cert);
0f113f3e 780 }
0f113f3e
MC
781 if (ret)
782 ERR_clear_error();
783 return ret;
ee0508d4
DSH
784}
785
786/* Generalised attribute print: handle PKCS#8 and bag attributes */
787
0f113f3e
MC
788int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
789 const char *name)
ee0508d4 790{
0f113f3e
MC
791 X509_ATTRIBUTE *attr;
792 ASN1_TYPE *av;
793 char *value;
794 int i, attr_nid;
795 if (!attrlst) {
796 BIO_printf(out, "%s: <No Attributes>\n", name);
797 return 1;
798 }
799 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
800 BIO_printf(out, "%s: <Empty Attributes>\n", name);
801 return 1;
802 }
803 BIO_printf(out, "%s\n", name);
804 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
9b0a4531 805 ASN1_OBJECT *attr_obj;
0f113f3e 806 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
9b0a4531
DSH
807 attr_obj = X509_ATTRIBUTE_get0_object(attr);
808 attr_nid = OBJ_obj2nid(attr_obj);
0f113f3e
MC
809 BIO_printf(out, " ");
810 if (attr_nid == NID_undef) {
9b0a4531 811 i2a_ASN1_OBJECT(out, attr_obj);
0f113f3e
MC
812 BIO_printf(out, ": ");
813 } else
814 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
815
9b0a4531
DSH
816 if (X509_ATTRIBUTE_count(attr)) {
817 av = X509_ATTRIBUTE_get0_type(attr, 0);
0f113f3e
MC
818 switch (av->type) {
819 case V_ASN1_BMPSTRING:
820 value = OPENSSL_uni2asc(av->value.bmpstring->data,
821 av->value.bmpstring->length);
822 BIO_printf(out, "%s\n", value);
823 OPENSSL_free(value);
824 break;
825
826 case V_ASN1_OCTET_STRING:
827 hex_prin(out, av->value.octet_string->data,
828 av->value.octet_string->length);
829 BIO_printf(out, "\n");
830 break;
831
832 case V_ASN1_BIT_STRING:
833 hex_prin(out, av->value.bit_string->data,
834 av->value.bit_string->length);
835 BIO_printf(out, "\n");
836 break;
837
838 default:
839 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
840 break;
841 }
842 } else
843 BIO_printf(out, "<No Values>\n");
844 }
845 return 1;
ee0508d4
DSH
846}
847
6b691a5c 848void hex_prin(BIO *out, unsigned char *buf, int len)
ee0508d4 849{
0f113f3e
MC
850 int i;
851 for (i = 0; i < len; i++)
852 BIO_printf(out, "%02X ", buf[i]);
ee0508d4 853}
a8515441 854
7e1b7485 855static int set_pbe(int *ppbe, const char *str)
0f113f3e
MC
856{
857 if (!str)
858 return 0;
86885c28 859 if (strcmp(str, "NONE") == 0) {
0f113f3e
MC
860 *ppbe = -1;
861 return 1;
862 }
863 *ppbe = OBJ_txt2nid(str);
864 if (*ppbe == NID_undef) {
865 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
866 return 0;
867 }
868 return 1;
869}
870
a8515441 871#endif