1 /*
2 * Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10 #include <openssl/opensslconf.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "apps.h"
16 #include "progs.h"
17 #include <openssl/asn1.h>
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/pem.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/provider.h>
23 #include <openssl/kdf.h>
24 #include <openssl/rand.h>
25
26 #define NOKEYS 0x1
27 #define NOCERTS 0x2
28 #define INFO 0x4
29 #define CLCERTS 0x8
30 #define CACERTS 0x10
31
32 #define PASSWD_BUF_SIZE 2048
33
34 #define WARN_EXPORT(opt) \
35 BIO_printf(bio_err, "Warning: -%s option ignored with -export\n", opt);
36 #define WARN_NO_EXPORT(opt) \
37 BIO_printf(bio_err, "Warning: -%s option ignored without -export\n", opt);
38
39 static int get_cert_chain(X509 *cert, X509_STORE *store,
40 STACK_OF(X509) *untrusted_certs,
41 STACK_OF(X509) **chain);
42 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
43 const char *pass, int passlen, int options,
44 char *pempass, const EVP_CIPHER *enc);
45 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
46 const char *pass, int passlen, int options,
47 char *pempass, const EVP_CIPHER *enc);
48 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
49 const char *pass, int passlen,
50 int options, char *pempass, const EVP_CIPHER *enc);
51 void print_attribute(BIO *out, const ASN1_TYPE *av);
52 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
53 const char *name);
54 void hex_prin(BIO *out, unsigned char *buf, int len);
55 static int alg_print(const X509_ALGOR *alg);
56 int cert_load(BIO *in, STACK_OF(X509) *sk);
57 static int set_pbe(int *ppbe, const char *str);
58 static int jdk_trust(PKCS12_SAFEBAG *bag, void *cbarg);
59
60 typedef enum OPTION_choice {
61 OPT_COMMON,
62 OPT_CIPHER,
63 OPT_NOKEYS,
64 OPT_KEYEX,
65 OPT_KEYSIG,
66 OPT_NOCERTS,
67 OPT_CLCERTS,
68 OPT_CACERTS,
69 OPT_NOOUT,
70 OPT_INFO,
71 OPT_CHAIN,
72 OPT_TWOPASS,
73 OPT_NOMACVER,
74 #ifndef OPENSSL_NO_DES
75 OPT_DESCERT,
76 #endif
77 OPT_EXPORT,
78 OPT_ITER,
79 OPT_NOITER,
80 OPT_MACITER,
81 OPT_NOMACITER,
82 OPT_MACSALTLEN,
83 OPT_NOMAC,
84 OPT_LMK,
85 OPT_NODES,
86 OPT_NOENC,
87 OPT_MACALG,
88 OPT_CERTPBE,
89 OPT_KEYPBE,
90 OPT_INKEY,
91 OPT_CERTFILE,
92 OPT_UNTRUSTED,
93 OPT_PASSCERTS,
94 OPT_NAME,
95 OPT_CSP,
96 OPT_CANAME,
97 OPT_IN,
98 OPT_OUT,
99 OPT_PASSIN,
100 OPT_PASSOUT,
101 OPT_PASSWORD,
102 OPT_CAPATH,
103 OPT_CAFILE,
104 OPT_CASTORE,
105 OPT_NOCAPATH,
106 OPT_NOCAFILE,
107 OPT_NOCASTORE,
108 OPT_ENGINE,
109 OPT_R_ENUM,
110 OPT_PROV_ENUM,
111 OPT_JDKTRUST,
112 OPT_PBMAC1_PBKDF2,
113 OPT_PBMAC1_PBKDF2_MD,
114 #ifndef OPENSSL_NO_DES
115 OPT_LEGACY_ALG
116 #endif
117 } OPTION_CHOICE;
118
119 const OPTIONS pkcs12_options[] = {
120 OPT_SECTION("General"),
121 { "help", OPT_HELP, '-', "Display this summary" },
122 { "in", OPT_IN, '<', "Input file" },
123 { "out", OPT_OUT, '>', "Output file" },
124 { "passin", OPT_PASSIN, 's', "Input file pass phrase source" },
125 { "passout", OPT_PASSOUT, 's', "Output file pass phrase source" },
126 { "password", OPT_PASSWORD, 's', "Set PKCS#12 import/export password source" },
127 { "twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords" },
128 { "nokeys", OPT_NOKEYS, '-', "Don't output private keys" },
129 { "nocerts", OPT_NOCERTS, '-', "Don't output certificates" },
130 { "noout", OPT_NOOUT, '-', "Don't output anything, just verify PKCS#12 input" },
131 #ifndef OPENSSL_NO_DES
132 { "legacy", OPT_LEGACY_ALG, '-',
133 #ifdef OPENSSL_NO_RC2
134 "Use legacy encryption algorithm 3DES_CBC for keys and certs"
135 #else
136 "Use legacy encryption: 3DES_CBC for keys, RC2_CBC for certs"
137 #endif
138 },
139 #endif
140 #ifndef OPENSSL_NO_ENGINE
141 { "engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device" },
142 #endif
143 OPT_PROV_OPTIONS,
144 OPT_R_OPTIONS,
145
146 OPT_SECTION("PKCS#12 import (parsing PKCS#12)"),
147 { "info", OPT_INFO, '-', "Print info about PKCS#12 structure" },
148 { "nomacver", OPT_NOMACVER, '-', "Don't verify integrity MAC" },
149 { "clcerts", OPT_CLCERTS, '-', "Only output client certificates" },
150 { "cacerts", OPT_CACERTS, '-', "Only output CA certificates" },
151 { "", OPT_CIPHER, '-', "Any supported cipher for output encryption" },
152 { "noenc", OPT_NOENC, '-', "Don't encrypt private keys" },
153 { "nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated" },
154
155 OPT_SECTION("PKCS#12 output (export)"),
156 { "export", OPT_EXPORT, '-', "Create PKCS12 file" },
157 { "inkey", OPT_INKEY, 's', "Private key, else read from -in input file" },
158 { "certfile", OPT_CERTFILE, '<', "Extra certificates for PKCS12 output" },
159 { "passcerts", OPT_PASSCERTS, 's', "Certificate file pass phrase source" },
160 { "chain", OPT_CHAIN, '-', "Build and add certificate chain for EE cert," },
161 { OPT_MORE_STR, 0, 0,
162 "which is the 1st cert from -in matching the private key (if given)" },
163 { "untrusted", OPT_UNTRUSTED, '<', "Untrusted certificates for chain building" },
164 { "CAfile", OPT_CAFILE, '<', "PEM-format file of CA's" },
165 { "CApath", OPT_CAPATH, '/', "PEM-format directory of CA's" },
166 { "CAstore", OPT_CASTORE, ':', "URI to store of CA's" },
167 { "no-CAfile", OPT_NOCAFILE, '-',
168 "Do not load the default certificates file" },
169 { "no-CApath", OPT_NOCAPATH, '-',
170 "Do not load certificates from the default certificates directory" },
171 { "no-CAstore", OPT_NOCASTORE, '-',
172 "Do not load certificates from the default certificates store" },
173 { "name", OPT_NAME, 's', "Use name as friendly name" },
174 { "caname", OPT_CANAME, 's',
175 "Use name as CA friendly name (can be repeated)" },
176 { "CSP", OPT_CSP, 's', "Microsoft CSP name" },
177 { "LMK", OPT_LMK, '-',
178 "Add local machine keyset attribute to private key" },
179 { "keyex", OPT_KEYEX, '-', "Set key type to MS key exchange" },
180 { "keysig", OPT_KEYSIG, '-', "Set key type to MS key signature" },
181 { "keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default AES-256 CBC)" },
182 { "certpbe", OPT_CERTPBE, 's',
183 "Certificate PBE algorithm (default PBES2 with PBKDF2 and AES-256 CBC)" },
184 #ifndef OPENSSL_NO_DES
185 { "descert", OPT_DESCERT, '-',
186 "Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)" },
187 #endif
188 { "macalg", OPT_MACALG, 's',
189 "Digest algorithm to use in MAC (default SHA256)" },
190 { "pbmac1_pbkdf2", OPT_PBMAC1_PBKDF2, '-', "Use PBMAC1 with PBKDF2 instead of MAC" },
191 { "pbmac1_pbkdf2_md", OPT_PBMAC1_PBKDF2_MD, 's', "Digest to use for PBMAC1 KDF (default SHA256)" },
192 { "iter", OPT_ITER, 'p', "Specify the iteration count for encryption and MAC" },
193 { "noiter", OPT_NOITER, '-', "Don't use encryption iteration" },
194 { "nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration)" },
195 { "maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility" },
196 { "macsaltlen", OPT_MACSALTLEN, 'p', "Specify the salt len for MAC" },
197 { "nomac", OPT_NOMAC, '-', "Don't generate MAC" },
198 { "jdktrust", OPT_JDKTRUST, 's', "Mark certificate in PKCS#12 store as trusted for JDK compatibility" },
199 { NULL }
200 };
201
pkcs12_main(int argc,char ** argv)202 int pkcs12_main(int argc, char **argv)
203 {
204 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
205 char *untrusted = NULL, *ciphername = NULL, *enc_name = NULL;
206 char *passcertsarg = NULL, *passcerts = NULL;
207 char *name = NULL, *csp_name = NULL;
208 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
209 int export_pkcs12 = 0, options = 0, chain = 0, twopass = 0, keytype = 0;
210 char *jdktrust = NULL;
211 #ifndef OPENSSL_NO_DES
212 int use_legacy = 0;
213 #endif
214 /* use library defaults for the iter, maciter, cert, and key PBE */
215 int iter = 0, maciter = 0, pbmac1_pbkdf2 = 0;
216 int macsaltlen = PKCS12_SALT_LEN;
217 int cert_pbe = NID_undef;
218 int key_pbe = NID_undef;
219 int ret = 1, macver = 1, add_lmk = 0, private = 0;
220 int noprompt = 0;
221 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
222 char *passin = NULL, *passout = NULL, *macalg = NULL, *pbmac1_pbkdf2_md = NULL;
223 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
224 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
225 int noCApath = 0, noCAfile = 0, noCAstore = 0;
226 ENGINE *e = NULL;
227 BIO *in = NULL, *out = NULL;
228 PKCS12 *p12 = NULL;
229 STACK_OF(OPENSSL_STRING) *canames = NULL;
230 EVP_CIPHER *default_enc = (EVP_CIPHER *)EVP_aes_256_cbc();
231 EVP_CIPHER *enc = (EVP_CIPHER *)default_enc;
232 OPTION_CHOICE o;
233
234 opt_set_unknown_name("cipher");
235 prog = opt_init(argc, argv, pkcs12_options);
236 while ((o = opt_next()) != OPT_EOF) {
237 switch (o) {
238 case OPT_EOF:
239 case OPT_ERR:
240 opthelp:
241 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
242 goto end;
243 case OPT_HELP:
244 opt_help(pkcs12_options);
245 ret = 0;
246 goto end;
247 case OPT_NOKEYS:
248 options |= NOKEYS;
249 break;
250 case OPT_KEYEX:
251 keytype = KEY_EX;
252 break;
253 case OPT_KEYSIG:
254 keytype = KEY_SIG;
255 break;
256 case OPT_NOCERTS:
257 options |= NOCERTS;
258 break;
259 case OPT_CLCERTS:
260 options |= CLCERTS;
261 break;
262 case OPT_CACERTS:
263 options |= CACERTS;
264 break;
265 case OPT_NOOUT:
266 options |= (NOKEYS | NOCERTS);
267 break;
268 case OPT_JDKTRUST:
269 jdktrust = opt_arg();
270 /* Adding jdk trust implies nokeys */
271 options |= NOKEYS;
272 break;
273 case OPT_INFO:
274 options |= INFO;
275 break;
276 case OPT_CHAIN:
277 chain = 1;
278 break;
279 case OPT_TWOPASS:
280 twopass = 1;
281 break;
282 case OPT_NOMACVER:
283 macver = 0;
284 break;
285 #ifndef OPENSSL_NO_DES
286 case OPT_DESCERT:
287 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
288 break;
289 #endif
290 case OPT_EXPORT:
291 export_pkcs12 = 1;
292 break;
293 case OPT_NODES:
294 case OPT_NOENC:
295 /*
296 * |enc_name| stores the name of the option used so it
297 * can be printed if an error message is output.
298 */
299 enc_name = opt_flag() + 1;
300 enc = NULL;
301 ciphername = NULL;
302 break;
303 case OPT_CIPHER:
304 enc_name = ciphername = opt_unknown();
305 break;
306 case OPT_ITER:
307 maciter = iter = opt_int_arg();
308 break;
309 case OPT_NOITER:
310 iter = 1;
311 break;
312 case OPT_MACITER:
313 /* no-op */
314 break;
315 case OPT_NOMACITER:
316 maciter = 1;
317 break;
318 case OPT_MACSALTLEN:
319 macsaltlen = opt_int_arg();
320 break;
321 case OPT_NOMAC:
322 cert_pbe = -1;
323 maciter = -1;
324 break;
325 case OPT_MACALG:
326 macalg = opt_arg();
327 break;
328 case OPT_PBMAC1_PBKDF2:
329 pbmac1_pbkdf2 = 1;
330 break;
331 case OPT_PBMAC1_PBKDF2_MD:
332 pbmac1_pbkdf2_md = opt_arg();
333 break;
334 case OPT_CERTPBE:
335 if (!set_pbe(&cert_pbe, opt_arg()))
336 goto opthelp;
337 break;
338 case OPT_KEYPBE:
339 if (!set_pbe(&key_pbe, opt_arg()))
340 goto opthelp;
341 break;
342 case OPT_R_CASES:
343 if (!opt_rand(o))
344 goto end;
345 break;
346 case OPT_INKEY:
347 keyname = opt_arg();
348 break;
349 case OPT_CERTFILE:
350 certfile = opt_arg();
351 break;
352 case OPT_UNTRUSTED:
353 untrusted = opt_arg();
354 break;
355 case OPT_PASSCERTS:
356 passcertsarg = opt_arg();
357 break;
358 case OPT_NAME:
359 name = opt_arg();
360 break;
361 case OPT_LMK:
362 add_lmk = 1;
363 break;
364 case OPT_CSP:
365 csp_name = opt_arg();
366 break;
367 case OPT_CANAME:
368 if (canames == NULL
369 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
370 goto end;
371 if (sk_OPENSSL_STRING_push(canames, opt_arg()) <= 0)
372 goto end;
373 break;
374 case OPT_IN:
375 infile = opt_arg();
376 break;
377 case OPT_OUT:
378 outfile = opt_arg();
379 break;
380 case OPT_PASSIN:
381 passinarg = opt_arg();
382 break;
383 case OPT_PASSOUT:
384 passoutarg = opt_arg();
385 break;
386 case OPT_PASSWORD:
387 passarg = opt_arg();
388 break;
389 case OPT_CAPATH:
390 CApath = opt_arg();
391 break;
392 case OPT_CASTORE:
393 CAstore = opt_arg();
394 break;
395 case OPT_CAFILE:
396 CAfile = opt_arg();
397 break;
398 case OPT_NOCAPATH:
399 noCApath = 1;
400 break;
401 case OPT_NOCASTORE:
402 noCAstore = 1;
403 break;
404 case OPT_NOCAFILE:
405 noCAfile = 1;
406 break;
407 case OPT_ENGINE:
408 e = setup_engine(opt_arg(), 0);
409 break;
410 #ifndef OPENSSL_NO_DES
411 case OPT_LEGACY_ALG:
412 use_legacy = 1;
413 break;
414 #endif
415 case OPT_PROV_CASES:
416 if (!opt_provider(o))
417 goto end;
418 break;
419 }
420 }
421
422 /* No extra arguments. */
423 if (!opt_check_rest_arg(NULL))
424 goto opthelp;
425
426 if (!app_RAND_load())
427 goto end;
428
429 if (!opt_cipher_any(ciphername, &enc))
430 goto opthelp;
431 if (export_pkcs12) {
432 if ((options & INFO) != 0)
433 WARN_EXPORT("info");
434 if (macver == 0)
435 WARN_EXPORT("nomacver");
436 if ((options & CLCERTS) != 0)
437 WARN_EXPORT("clcerts");
438 if ((options & CACERTS) != 0)
439 WARN_EXPORT("cacerts");
440 if (enc != default_enc)
441 BIO_printf(bio_err,
442 "Warning: output encryption option -%s ignored with -export\n", enc_name);
443 } else {
444 if (keyname != NULL)
445 WARN_NO_EXPORT("inkey");
446 if (certfile != NULL)
447 WARN_NO_EXPORT("certfile");
448 if (passcertsarg != NULL)
449 WARN_NO_EXPORT("passcerts");
450 if (chain)
451 WARN_NO_EXPORT("chain");
452 if (untrusted != NULL)
453 WARN_NO_EXPORT("untrusted");
454 if (CAfile != NULL)
455 WARN_NO_EXPORT("CAfile");
456 if (CApath != NULL)
457 WARN_NO_EXPORT("CApath");
458 if (CAstore != NULL)
459 WARN_NO_EXPORT("CAstore");
460 if (noCAfile)
461 WARN_NO_EXPORT("no-CAfile");
462 if (noCApath)
463 WARN_NO_EXPORT("no-CApath");
464 if (noCAstore)
465 WARN_NO_EXPORT("no-CAstore");
466 if (name != NULL)
467 WARN_NO_EXPORT("name");
468 if (canames != NULL)
469 WARN_NO_EXPORT("caname");
470 if (csp_name != NULL)
471 WARN_NO_EXPORT("CSP");
472 if (add_lmk)
473 WARN_NO_EXPORT("LMK");
474 if (keytype == KEY_EX)
475 WARN_NO_EXPORT("keyex");
476 if (keytype == KEY_SIG)
477 WARN_NO_EXPORT("keysig");
478 if (key_pbe != NID_undef)
479 WARN_NO_EXPORT("keypbe");
480 if (cert_pbe != NID_undef && cert_pbe != -1)
481 WARN_NO_EXPORT("certpbe and -descert");
482 if (macalg != NULL)
483 WARN_NO_EXPORT("macalg");
484 if (iter != 0)
485 WARN_NO_EXPORT("iter and -noiter");
486 if (maciter == 1)
487 WARN_NO_EXPORT("nomaciter");
488 if (cert_pbe == -1 && maciter == -1)
489 WARN_NO_EXPORT("nomac");
490 if (macsaltlen != PKCS12_SALT_LEN)
491 WARN_NO_EXPORT("macsaltlen");
492 }
493 #ifndef OPENSSL_NO_DES
494 if (use_legacy) {
495 /* load the legacy provider if not loaded already*/
496 if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
497 if (!app_provider_load(app_get0_libctx(), "legacy"))
498 goto end;
499 /* load the default provider explicitly */
500 if (!app_provider_load(app_get0_libctx(), "default"))
501 goto end;
502 }
503 if (cert_pbe == NID_undef) {
504 /* Adapt default algorithm */
505 #ifndef OPENSSL_NO_RC2
506 cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
507 #else
508 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
509 #endif
510 }
511
512 if (key_pbe == NID_undef)
513 key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
514 if (enc == default_enc)
515 enc = (EVP_CIPHER *)EVP_des_ede3_cbc();
516 if (macalg == NULL)
517 macalg = "sha1";
518 }
519 #endif
520
521 private = 1;
522
523 if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
524 BIO_printf(bio_err, "Error getting certificate file password\n");
525 goto end;
526 }
527
528 if (passarg != NULL) {
529 if (export_pkcs12)
530 passoutarg = passarg;
531 else
532 passinarg = passarg;
533 }
534
535 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
536 BIO_printf(bio_err, "Error getting passwords\n");
537 goto end;
538 }
539
540 if (cpass == NULL) {
541 if (export_pkcs12)
542 cpass = passout;
543 else
544 cpass = passin;
545 }
546
547 if (cpass != NULL) {
548 mpass = cpass;
549 noprompt = 1;
550 if (twopass) {
551 if (export_pkcs12)
552 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
553 else
554 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
555 goto end;
556 }
557 } else {
558 cpass = pass;
559 mpass = macpass;
560 }
561
562 if (twopass) {
563 /* To avoid bit rot */
564 if (1) {
565 #ifndef OPENSSL_NO_UI_CONSOLE
566 if (EVP_read_pw_string(
567 macpass, sizeof(macpass), "Enter MAC Password:", export_pkcs12)) {
568 BIO_printf(bio_err, "Can't read Password\n");
569 goto end;
570 }
571 } else {
572 #endif
573 BIO_printf(bio_err, "Unsupported option -twopass\n");
574 goto end;
575 }
576 }
577
578 if (export_pkcs12) {
579 EVP_PKEY *key = NULL;
580 X509 *ee_cert = NULL, *x = NULL;
581 STACK_OF(X509) *certs = NULL;
582 STACK_OF(X509) *untrusted_certs = NULL;
583 EVP_MD *macmd = NULL;
584 unsigned char *catmp = NULL;
585 int i;
586 ASN1_OBJECT *obj = NULL;
587
588 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
589 BIO_printf(bio_err, "Nothing to export due to -noout or -nocerts and -nokeys\n");
590 goto export_end;
591 }
592
593 if ((options & NOCERTS) != 0) {
594 chain = 0;
595 BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
596 }
597
598 if (!(options & NOKEYS)) {
599 key = load_key(keyname ? keyname : infile,
600 FORMAT_PEM, 1, passin, e,
601 keyname ? "private key from -inkey file" : "private key from -in file");
602 if (key == NULL)
603 goto export_end;
604 }
605
606 /* Load all certs in input file */
607 if (!(options & NOCERTS)) {
608 if (!load_certs(infile, 1, &certs, passin,
609 "certificates from -in file"))
610 goto export_end;
611 if (sk_X509_num(certs) < 1) {
612 BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
613 goto export_end;
614 }
615
616 if (key != NULL) {
617 /* Look for matching private key */
618 for (i = 0; i < sk_X509_num(certs); i++) {
619 x = sk_X509_value(certs, i);
620 if (cert_matches_key(x, key)) {
621 ee_cert = x;
622 /* Zero keyid and alias */
623 X509_keyid_set1(ee_cert, NULL, 0);
624 X509_alias_set1(ee_cert, NULL, 0);
625 /* Remove from list */
626 (void)sk_X509_delete(certs, i);
627 break;
628 }
629 }
630 if (ee_cert == NULL) {
631 BIO_printf(bio_err,
632 "No cert in -in file '%s' matches private key\n",
633 infile);
634 goto export_end;
635 }
636 }
637 }
638
639 /* Load any untrusted certificates for chain building */
640 if (untrusted != NULL) {
641 if (!load_certs(untrusted, 0, &untrusted_certs, passcerts,
642 "untrusted certificates"))
643 goto export_end;
644 }
645
646 /* If chaining get chain from end entity cert */
647 if (chain) {
648 int vret;
649 STACK_OF(X509) *chain2;
650 X509_STORE *store;
651 X509 *ee_cert_tmp = ee_cert;
652
653 /* Assume the first cert if we haven't got anything else */
654 if (ee_cert_tmp == NULL && certs != NULL)
655 ee_cert_tmp = sk_X509_value(certs, 0);
656
657 if (ee_cert_tmp == NULL) {
658 BIO_printf(bio_err,
659 "No end entity certificate to check with -chain\n");
660 goto export_end;
661 }
662
663 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
664 CAstore, noCAstore))
665 == NULL)
666 goto export_end;
667
668 vret = get_cert_chain(ee_cert_tmp, store, untrusted_certs, &chain2);
669 X509_STORE_free(store);
670
671 if (vret == X509_V_OK) {
672 int add_certs;
673 /* Remove from chain2 the first (end entity) certificate */
674 X509_free(sk_X509_shift(chain2));
675 /* Add the remaining certs (except for duplicates) */
676 add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP);
677 OSSL_STACK_OF_X509_free(chain2);
678 if (!add_certs)
679 goto export_end;
680 } else {
681 if (vret != X509_V_ERR_UNSPECIFIED)
682 BIO_printf(bio_err, "Error getting chain: %s\n",
683 X509_verify_cert_error_string(vret));
684 goto export_end;
685 }
686 }
687
688 /* Add any extra certificates asked for */
689 if (certfile != NULL) {
690 if (!load_certs(certfile, 0, &certs, passcerts,
691 "extra certificates from -certfile"))
692 goto export_end;
693 }
694
695 /* Add any CA names */
696 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
697 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
698 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
699 }
700
701 if (csp_name != NULL && key != NULL)
702 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
703 MBSTRING_ASC, (unsigned char *)csp_name,
704 -1);
705
706 if (add_lmk && key != NULL)
707 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
708
709 if (!noprompt && !(enc == NULL && maciter == -1)) {
710 /* To avoid bit rot */
711 if (1) {
712 #ifndef OPENSSL_NO_UI_CONSOLE
713 if (EVP_read_pw_string(pass, sizeof(pass),
714 "Enter Export Password:", 1)) {
715 BIO_printf(bio_err, "Can't read Password\n");
716 goto export_end;
717 }
718 } else {
719 #endif
720 BIO_printf(bio_err, "Password required\n");
721 goto export_end;
722 }
723 }
724
725 if (!twopass)
726 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
727
728 if (jdktrust != NULL) {
729 obj = OBJ_txt2obj(jdktrust, 0);
730 }
731
732 p12 = PKCS12_create_ex2(cpass, name, key, ee_cert, certs,
733 key_pbe, cert_pbe, iter, -1, keytype,
734 app_get0_libctx(), app_get0_propq(),
735 jdk_trust, (void *)obj);
736
737 if (p12 == NULL) {
738 BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
739 outfile);
740 goto export_end;
741 }
742
743 if (macalg != NULL) {
744 if (!opt_md(macalg, &macmd))
745 goto opthelp;
746 }
747
748 if (maciter != -1) {
749 if (pbmac1_pbkdf2 == 1) {
750 if (!PKCS12_set_pbmac1_pbkdf2(p12, mpass, -1, NULL,
751 macsaltlen, maciter,
752 macmd, pbmac1_pbkdf2_md)) {
753 BIO_printf(bio_err, "Error creating PBMAC1\n");
754 goto export_end;
755 }
756 } else {
757 if (!PKCS12_set_mac(p12, mpass, -1, NULL, macsaltlen, maciter, macmd)) {
758 BIO_printf(bio_err, "Error creating PKCS12 MAC; no PKCS12KDF support?\n");
759 BIO_printf(bio_err,
760 "Use -nomac or -pbmac1_pbkdf2 if PKCS12KDF support not available\n");
761 goto export_end;
762 }
763 }
764 }
765 assert(private);
766
767 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
768 if (out == NULL)
769 goto end;
770
771 i2d_PKCS12_bio(out, p12);
772
773 ret = 0;
774
775 export_end:
776
777 EVP_PKEY_free(key);
778 EVP_MD_free(macmd);
779 OSSL_STACK_OF_X509_free(certs);
780 OSSL_STACK_OF_X509_free(untrusted_certs);
781 X509_free(ee_cert);
782 ASN1_OBJECT_free(obj);
783 ERR_print_errors(bio_err);
784 goto end;
785 }
786
787 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
788 if (in == NULL)
789 goto end;
790
791 p12 = PKCS12_init_ex(NID_pkcs7_data, app_get0_libctx(), app_get0_propq());
792 if (p12 == NULL) {
793 ERR_print_errors(bio_err);
794 goto end;
795 }
796 if ((p12 = d2i_PKCS12_bio(in, &p12)) == NULL) {
797 ERR_print_errors(bio_err);
798 goto end;
799 }
800
801 if (!noprompt) {
802 if (1) {
803 #ifndef OPENSSL_NO_UI_CONSOLE
804 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
805 0)) {
806 BIO_printf(bio_err, "Can't read Password\n");
807 goto end;
808 }
809 } else {
810 #endif
811 BIO_printf(bio_err, "Password required\n");
812 goto end;
813 }
814 }
815
816 if (!twopass)
817 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
818
819 if ((options & INFO) && PKCS12_mac_present(p12)) {
820 const ASN1_INTEGER *tmaciter;
821 const X509_ALGOR *macalgid;
822 const ASN1_OBJECT *macobj;
823 const ASN1_OCTET_STRING *tmac;
824 const ASN1_OCTET_STRING *tsalt;
825
826 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
827 /* current hash algorithms do not use parameters so extract just name,
828 in future alg_print() may be needed */
829 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
830 BIO_puts(bio_err, "MAC: ");
831 i2a_ASN1_OBJECT(bio_err, macobj);
832 if (OBJ_obj2nid(macobj) == NID_pbmac1) {
833 PBKDF2PARAM *pbkdf2_param = PBMAC1_get1_pbkdf2_param(macalgid);
834
835 if (pbkdf2_param == NULL) {
836 BIO_printf(bio_err, ", Unsupported KDF or params for PBMAC1\n");
837 } else {
838 const ASN1_OBJECT *prfobj;
839 int prfnid;
840
841 BIO_printf(bio_err, " using PBKDF2, Iteration %ld\n",
842 ASN1_INTEGER_get(pbkdf2_param->iter));
843 BIO_printf(bio_err, "Key length: %ld, Salt length: %d\n",
844 ASN1_INTEGER_get(pbkdf2_param->keylength),
845 ASN1_STRING_length(pbkdf2_param->salt->value.octet_string));
846 if (pbkdf2_param->prf == NULL) {
847 prfnid = NID_hmacWithSHA1;
848 } else {
849 X509_ALGOR_get0(&prfobj, NULL, NULL, pbkdf2_param->prf);
850 prfnid = OBJ_obj2nid(prfobj);
851 }
852 BIO_printf(bio_err, "PBKDF2 PRF: %s\n", OBJ_nid2sn(prfnid));
853 }
854 PBKDF2PARAM_free(pbkdf2_param);
855 } else {
856 BIO_printf(bio_err, ", Iteration %ld\n",
857 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
858 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
859 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
860 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
861 }
862 }
863
864 if (macver) {
865 const X509_ALGOR *macalgid;
866 const ASN1_OBJECT *macobj;
867
868 PKCS12_get0_mac(NULL, &macalgid, NULL, NULL, p12);
869
870 if (macalgid == NULL) {
871 BIO_printf(bio_err, "Warning: MAC is absent!\n");
872 goto dump;
873 }
874
875 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
876
877 if (OBJ_obj2nid(macobj) != NID_pbmac1) {
878 EVP_KDF *pkcs12kdf;
879
880 pkcs12kdf = EVP_KDF_fetch(app_get0_libctx(), "PKCS12KDF",
881 app_get0_propq());
882 if (pkcs12kdf == NULL) {
883 BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
884 BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
885 goto end;
886 }
887 EVP_KDF_free(pkcs12kdf);
888 }
889
890 /* If we enter empty password try no password first */
891 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
892 /* If mac and crypto pass the same set it to NULL too */
893 if (!twopass)
894 cpass = NULL;
895 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
896 /*
897 * May be UTF8 from previous version of OpenSSL:
898 * convert to a UTF8 form which will translate
899 * to the same Unicode password.
900 */
901 unsigned char *utmp;
902 int utmplen;
903 unsigned long err = ERR_peek_error();
904
905 if (ERR_GET_LIB(err) == ERR_LIB_PKCS12
906 && ERR_GET_REASON(err) == PKCS12_R_MAC_ABSENT) {
907 BIO_printf(bio_err, "Warning: MAC is absent!\n");
908 goto dump;
909 }
910
911 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
912 if (utmp == NULL)
913 goto end;
914 badpass = OPENSSL_uni2utf8(utmp, utmplen);
915 if (badpass == NULL) {
916 BIO_printf(bio_err, "Verbatim password did not match, and fallback conversion to UTF-8 failed\n"
917 "The password entered or the input encoding may be wrong\n");
918 OPENSSL_free(utmp);
919 goto end;
920 }
921 OPENSSL_free(utmp);
922 if (!PKCS12_verify_mac(p12, badpass, -1)) {
923 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
924 ERR_print_errors(bio_err);
925 goto end;
926 } else {
927 BIO_printf(bio_err, "Warning: using broken algorithm\n");
928 if (!twopass)
929 cpass = badpass;
930 }
931 }
932 }
933
934 dump:
935 assert(private);
936
937 out = bio_open_owner(outfile, FORMAT_PEM, private);
938 if (out == NULL)
939 goto end;
940
941 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
942 BIO_printf(bio_err, "Error outputting keys and certificates\n");
943 ERR_print_errors(bio_err);
944 goto end;
945 }
946 ret = 0;
947 end:
948 PKCS12_free(p12);
949 release_engine(e);
950 BIO_free(in);
951 BIO_free_all(out);
952 sk_OPENSSL_STRING_free(canames);
953 OPENSSL_free(badpass);
954 OPENSSL_free(passcerts);
955 OPENSSL_free(passin);
956 OPENSSL_free(passout);
957 return ret;
958 }
959
jdk_trust(PKCS12_SAFEBAG * bag,void * cbarg)960 static int jdk_trust(PKCS12_SAFEBAG *bag, void *cbarg)
961 {
962 STACK_OF(X509_ATTRIBUTE) *attrs = NULL;
963 X509_ATTRIBUTE *attr = NULL;
964
965 /* Nothing to do */
966 if (cbarg == NULL)
967 return 1;
968
969 /* Get the current attrs */
970 attrs = (STACK_OF(X509_ATTRIBUTE) *)PKCS12_SAFEBAG_get0_attrs(bag);
971
972 /* Create a new attr for the JDK Trusted Usage and add it */
973 attr = X509_ATTRIBUTE_create(NID_oracle_jdk_trustedkeyusage, V_ASN1_OBJECT, (ASN1_OBJECT *)cbarg);
974
975 /* Add the new attr, if attrs is NULL, it'll be initialised */
976 X509at_add1_attr(&attrs, attr);
977
978 /* Set the bag attrs */
979 PKCS12_SAFEBAG_set0_attrs(bag, attrs);
980
981 X509_ATTRIBUTE_free(attr);
982 return 1;
983 }
984
dump_certs_keys_p12(BIO * out,const PKCS12 * p12,const char * pass,int passlen,int options,char * pempass,const EVP_CIPHER * enc)985 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
986 int passlen, int options, char *pempass,
987 const EVP_CIPHER *enc)
988 {
989 STACK_OF(PKCS7) *asafes = NULL;
990 int i, bagnid;
991 int ret = 0;
992 PKCS7 *p7;
993
994 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
995 return 0;
996 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
997 STACK_OF(PKCS12_SAFEBAG) *bags;
998
999 p7 = sk_PKCS7_value(asafes, i);
1000 bagnid = OBJ_obj2nid(p7->type);
1001 if (bagnid == NID_pkcs7_data) {
1002 bags = PKCS12_unpack_p7data(p7);
1003 if (options & INFO)
1004 BIO_printf(bio_err, "PKCS7 Data\n");
1005 } else if (bagnid == NID_pkcs7_encrypted) {
1006 if (options & INFO) {
1007 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
1008 if (p7->d.encrypted == NULL) {
1009 BIO_printf(bio_err, "<no data>\n");
1010 } else {
1011 alg_print(p7->d.encrypted->enc_data->algorithm);
1012 }
1013 }
1014 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
1015 } else {
1016 continue;
1017 }
1018 if (bags == NULL)
1019 goto err;
1020 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
1021 options, pempass, enc)) {
1022 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
1023 goto err;
1024 }
1025 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
1026 }
1027 ret = 1;
1028
1029 err:
1030 sk_PKCS7_pop_free(asafes, PKCS7_free);
1031 return ret;
1032 }
1033
dump_certs_pkeys_bags(BIO * out,const STACK_OF (PKCS12_SAFEBAG)* bags,const char * pass,int passlen,int options,char * pempass,const EVP_CIPHER * enc)1034 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
1035 const char *pass, int passlen, int options,
1036 char *pempass, const EVP_CIPHER *enc)
1037 {
1038 int i;
1039 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
1040 if (!dump_certs_pkeys_bag(out,
1041 sk_PKCS12_SAFEBAG_value(bags, i),
1042 pass, passlen, options, pempass, enc))
1043 return 0;
1044 }
1045 return 1;
1046 }
1047
dump_certs_pkeys_bag(BIO * out,const PKCS12_SAFEBAG * bag,const char * pass,int passlen,int options,char * pempass,const EVP_CIPHER * enc)1048 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
1049 const char *pass, int passlen, int options,
1050 char *pempass, const EVP_CIPHER *enc)
1051 {
1052 EVP_PKEY *pkey;
1053 PKCS8_PRIV_KEY_INFO *p8;
1054 const PKCS8_PRIV_KEY_INFO *p8c;
1055 X509 *x509;
1056 const STACK_OF(X509_ATTRIBUTE) *attrs;
1057 int ret = 0;
1058
1059 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
1060
1061 switch (PKCS12_SAFEBAG_get_nid(bag)) {
1062 case NID_keyBag:
1063 if (options & INFO)
1064 BIO_printf(bio_err, "Key bag\n");
1065 if (options & NOKEYS)
1066 return 1;
1067 print_attribs(out, attrs, "Bag Attributes");
1068 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
1069 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
1070 return 0;
1071 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
1072 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
1073 EVP_PKEY_free(pkey);
1074 break;
1075
1076 case NID_pkcs8ShroudedKeyBag:
1077 if (options & INFO) {
1078 const X509_SIG *tp8;
1079 const X509_ALGOR *tp8alg;
1080
1081 BIO_printf(bio_err, "Shrouded Keybag: ");
1082 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
1083 X509_SIG_get0(tp8, &tp8alg, NULL);
1084 alg_print(tp8alg);
1085 }
1086 if (options & NOKEYS)
1087 return 1;
1088 print_attribs(out, attrs, "Bag Attributes");
1089 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
1090 return 0;
1091 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
1092 PKCS8_PRIV_KEY_INFO_free(p8);
1093 return 0;
1094 }
1095 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
1096 PKCS8_PRIV_KEY_INFO_free(p8);
1097 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
1098 EVP_PKEY_free(pkey);
1099 break;
1100
1101 case NID_certBag:
1102 if (options & INFO)
1103 BIO_printf(bio_err, "Certificate bag\n");
1104 if (options & NOCERTS)
1105 return 1;
1106 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
1107 if (options & CACERTS)
1108 return 1;
1109 } else if (options & CLCERTS)
1110 return 1;
1111 print_attribs(out, attrs, "Bag Attributes");
1112 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
1113 return 1;
1114 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
1115 return 0;
1116 dump_cert_text(out, x509);
1117 ret = PEM_write_bio_X509(out, x509);
1118 X509_free(x509);
1119 break;
1120
1121 case NID_secretBag:
1122 if (options & INFO)
1123 BIO_printf(bio_err, "Secret bag\n");
1124 print_attribs(out, attrs, "Bag Attributes");
1125 BIO_printf(bio_err, "Bag Type: ");
1126 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
1127 BIO_printf(bio_err, "\nBag Value: ");
1128 print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
1129 return 1;
1130
1131 case NID_safeContentsBag:
1132 if (options & INFO)
1133 BIO_printf(bio_err, "Safe Contents bag\n");
1134 print_attribs(out, attrs, "Bag Attributes");
1135 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
1136 pass, passlen, options, pempass, enc);
1137
1138 default:
1139 BIO_printf(bio_err, "Warning unsupported bag type: ");
1140 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
1141 BIO_printf(bio_err, "\n");
1142 return 1;
1143 }
1144 return ret;
1145 }
1146
1147 /* Given a single certificate return a verified chain or NULL if error */
1148
get_cert_chain(X509 * cert,X509_STORE * store,STACK_OF (X509)* untrusted_certs,STACK_OF (X509)** chain)1149 static int get_cert_chain(X509 *cert, X509_STORE *store,
1150 STACK_OF(X509) *untrusted_certs,
1151 STACK_OF(X509) **chain)
1152 {
1153 X509_STORE_CTX *store_ctx = NULL;
1154 STACK_OF(X509) *chn = NULL;
1155 int i = 0;
1156
1157 store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
1158 if (store_ctx == NULL) {
1159 i = X509_V_ERR_UNSPECIFIED;
1160 goto end;
1161 }
1162 if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
1163 i = X509_V_ERR_UNSPECIFIED;
1164 goto end;
1165 }
1166
1167 if (X509_verify_cert(store_ctx) > 0)
1168 chn = X509_STORE_CTX_get1_chain(store_ctx);
1169 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
1170 i = X509_V_ERR_UNSPECIFIED;
1171
1172 end:
1173 X509_STORE_CTX_free(store_ctx);
1174 *chain = chn;
1175 return i;
1176 }
1177
alg_print(const X509_ALGOR * alg)1178 static int alg_print(const X509_ALGOR *alg)
1179 {
1180 int pbenid, aparamtype;
1181 const ASN1_OBJECT *aoid;
1182 const void *aparam;
1183 PBEPARAM *pbe = NULL;
1184
1185 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
1186
1187 pbenid = OBJ_obj2nid(aoid);
1188
1189 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
1190
1191 /*
1192 * If PBE algorithm is PBES2 decode algorithm parameters
1193 * for additional details.
1194 */
1195 if (pbenid == NID_pbes2) {
1196 PBE2PARAM *pbe2 = NULL;
1197 int encnid;
1198 if (aparamtype == V_ASN1_SEQUENCE)
1199 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
1200 if (pbe2 == NULL) {
1201 BIO_puts(bio_err, ", <unsupported parameters>");
1202 goto done;
1203 }
1204 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
1205 pbenid = OBJ_obj2nid(aoid);
1206 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
1207 encnid = OBJ_obj2nid(aoid);
1208 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
1209 OBJ_nid2sn(encnid));
1210 /* If KDF is PBKDF2 decode parameters */
1211 if (pbenid == NID_id_pbkdf2) {
1212 PBKDF2PARAM *kdf = NULL;
1213 int prfnid;
1214 if (aparamtype == V_ASN1_SEQUENCE)
1215 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
1216 if (kdf == NULL) {
1217 BIO_puts(bio_err, ", <unsupported parameters>");
1218 goto done;
1219 }
1220
1221 if (kdf->prf == NULL) {
1222 prfnid = NID_hmacWithSHA1;
1223 } else {
1224 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
1225 prfnid = OBJ_obj2nid(aoid);
1226 }
1227 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
1228 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
1229 PBKDF2PARAM_free(kdf);
1230 #ifndef OPENSSL_NO_SCRYPT
1231 } else if (pbenid == NID_id_scrypt) {
1232 SCRYPT_PARAMS *kdf = NULL;
1233
1234 if (aparamtype == V_ASN1_SEQUENCE)
1235 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
1236 if (kdf == NULL) {
1237 BIO_puts(bio_err, ", <unsupported parameters>");
1238 goto done;
1239 }
1240 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
1241 "Block size(r): %ld, Parallelism(p): %ld",
1242 ASN1_STRING_length(kdf->salt),
1243 ASN1_INTEGER_get(kdf->costParameter),
1244 ASN1_INTEGER_get(kdf->blockSize),
1245 ASN1_INTEGER_get(kdf->parallelizationParameter));
1246 SCRYPT_PARAMS_free(kdf);
1247 #endif
1248 }
1249 PBE2PARAM_free(pbe2);
1250 } else {
1251 if (aparamtype == V_ASN1_SEQUENCE)
1252 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
1253 if (pbe == NULL) {
1254 BIO_puts(bio_err, ", <unsupported parameters>");
1255 goto done;
1256 }
1257 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
1258 PBEPARAM_free(pbe);
1259 }
1260 done:
1261 BIO_puts(bio_err, "\n");
1262 return 1;
1263 }
1264
1265 /* Load all certificates from a given file */
1266
cert_load(BIO * in,STACK_OF (X509)* sk)1267 int cert_load(BIO *in, STACK_OF(X509) *sk)
1268 {
1269 int ret = 0;
1270 X509 *cert;
1271
1272 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1273 ret = 1;
1274 if (!sk_X509_push(sk, cert))
1275 return 0;
1276 }
1277 if (ret)
1278 ERR_clear_error();
1279 return ret;
1280 }
1281
1282 /* Generalised x509 attribute value print */
1283
print_attribute(BIO * out,const ASN1_TYPE * av)1284 void print_attribute(BIO *out, const ASN1_TYPE *av)
1285 {
1286 char *value;
1287 const char *ln;
1288 char objbuf[80];
1289
1290 switch (av->type) {
1291 case V_ASN1_BMPSTRING:
1292 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1293 av->value.bmpstring->length);
1294 BIO_printf(out, "%s\n", value);
1295 OPENSSL_free(value);
1296 break;
1297
1298 case V_ASN1_UTF8STRING:
1299 BIO_printf(out, "%.*s\n", av->value.utf8string->length,
1300 av->value.utf8string->data);
1301 break;
1302
1303 case V_ASN1_OCTET_STRING:
1304 hex_prin(out, av->value.octet_string->data,
1305 av->value.octet_string->length);
1306 BIO_printf(out, "\n");
1307 break;
1308
1309 case V_ASN1_BIT_STRING:
1310 hex_prin(out, av->value.bit_string->data,
1311 av->value.bit_string->length);
1312 BIO_printf(out, "\n");
1313 break;
1314
1315 case V_ASN1_OBJECT:
1316 ln = OBJ_nid2ln(OBJ_obj2nid(av->value.object));
1317 if (!ln)
1318 ln = "";
1319 OBJ_obj2txt(objbuf, sizeof(objbuf), av->value.object, 1);
1320 BIO_printf(out, "%s (%s)", ln, objbuf);
1321 BIO_printf(out, "\n");
1322 break;
1323
1324 default:
1325 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1326 break;
1327 }
1328 }
1329
1330 /* Generalised attribute print: handle PKCS#8 and bag attributes */
1331
print_attribs(BIO * out,const STACK_OF (X509_ATTRIBUTE)* attrlst,const char * name)1332 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
1333 const char *name)
1334 {
1335 X509_ATTRIBUTE *attr;
1336 ASN1_TYPE *av;
1337 int i, j, attr_nid;
1338 if (!attrlst) {
1339 BIO_printf(out, "%s: <No Attributes>\n", name);
1340 return 1;
1341 }
1342 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1343 BIO_printf(out, "%s: <Empty Attributes>\n", name);
1344 return 1;
1345 }
1346 BIO_printf(out, "%s\n", name);
1347 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1348 ASN1_OBJECT *attr_obj;
1349 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1350 attr_obj = X509_ATTRIBUTE_get0_object(attr);
1351 attr_nid = OBJ_obj2nid(attr_obj);
1352 BIO_printf(out, " ");
1353 if (attr_nid == NID_undef) {
1354 i2a_ASN1_OBJECT(out, attr_obj);
1355 BIO_printf(out, ": ");
1356 } else {
1357 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1358 }
1359
1360 if (X509_ATTRIBUTE_count(attr)) {
1361 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++) {
1362 av = X509_ATTRIBUTE_get0_type(attr, j);
1363 print_attribute(out, av);
1364 }
1365 } else {
1366 BIO_printf(out, "<No Values>\n");
1367 }
1368 }
1369 return 1;
1370 }
1371
hex_prin(BIO * out,unsigned char * buf,int len)1372 void hex_prin(BIO *out, unsigned char *buf, int len)
1373 {
1374 int i;
1375 for (i = 0; i < len; i++)
1376 BIO_printf(out, "%02X ", buf[i]);
1377 }
1378
set_pbe(int * ppbe,const char * str)1379 static int set_pbe(int *ppbe, const char *str)
1380 {
1381 if (!str)
1382 return 0;
1383 if (strcmp(str, "NONE") == 0) {
1384 *ppbe = -1;
1385 return 1;
1386 }
1387 *ppbe = OBJ_txt2nid(str);
1388 if (*ppbe == NID_undef) {
1389 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
1390 return 0;
1391 }
1392 return 1;
1393 }
1394