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