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