2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19 #include <openssl/pem.h>
20 #include <openssl/pkcs12.h>
21 #include <openssl/provider.h>
29 #define PASSWD_BUF_SIZE 2048
31 static int get_cert_chain(X509 *cert, X509_STORE *store,
32 STACK_OF(X509) *untrusted_certs,
33 STACK_OF(X509) **chain);
34 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12,
35 const char *pass, int passlen, int options,
36 char *pempass, const EVP_CIPHER *enc);
37 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
38 const char *pass, int passlen, int options,
39 char *pempass, const EVP_CIPHER *enc);
40 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bags,
41 const char *pass, int passlen,
42 int options, char *pempass, const EVP_CIPHER *enc);
43 void print_attribute(BIO *out, const ASN1_TYPE *av);
44 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
46 void hex_prin(BIO *out, unsigned char *buf, int len);
47 static int alg_print(const X509_ALGOR *alg);
48 int cert_load(BIO *in, STACK_OF(X509) *sk);
49 static int set_pbe(int *ppbe, const char *str);
51 typedef 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_ITER, OPT_NOITER, OPT_MACITER, OPT_NOMACITER,
56 OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_NOENC, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE,
57 OPT_INKEY, OPT_CERTFILE, OPT_UNTRUSTED, OPT_PASSCERTS,
58 OPT_NAME, OPT_CSP, OPT_CANAME,
59 OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH,
60 OPT_CAFILE, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_ENGINE,
61 OPT_R_ENUM, OPT_PROV_ENUM, OPT_LEGACY_ALG
64 const OPTIONS pkcs12_options[] = {
65 OPT_SECTION("General"),
66 {"help", OPT_HELP, '-', "Display this summary"},
67 {"legacy", OPT_LEGACY_ALG, '-',
69 "Use legacy encryption algorithm 3DES_CBC for keys and certs"
71 "Use legacy encryption: 3DES_CBC for keys, RC2_CBC for certs"
74 #ifndef OPENSSL_NO_ENGINE
75 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
77 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
78 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
81 {"in", OPT_IN, '<', "Input file for PKCS12 parsing or certs and possibly key"},
82 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
83 {"inkey", OPT_INKEY, 's', "Private key, else read from -in input file"},
84 {"certfile", OPT_CERTFILE, '<', "Extra certificates for PKCS12 output"},
85 {"untrusted", OPT_UNTRUSTED, '<', "Untrusted certificates for chain building"},
86 {"passcerts", OPT_PASSCERTS, 's', "Certificate file pass phrase source"},
87 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
89 OPT_SECTION("CA input for export with the -chain option"),
90 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
91 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
92 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
93 {"no-CAfile", OPT_NOCAFILE, '-',
94 "Do not load the default certificates file"},
95 {"no-CApath", OPT_NOCAPATH, '-',
96 "Do not load certificates from the default certificates directory"},
97 {"no-CAstore", OPT_NOCASTORE, '-',
98 "Do not load certificates from the default certificates store"},
100 OPT_SECTION("Output"),
101 {"out", OPT_OUT, '>', "Output filename"},
102 {"passout", OPT_PASSOUT, 's', "Output pass phrase source"},
103 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
104 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
105 {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"},
106 {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"},
107 {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"},
108 {"noout", OPT_NOOUT, '-', "Don't output anything, just verify PKCS#12 input"},
110 OPT_SECTION("PKCS12 output"),
111 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
112 {"chain", OPT_CHAIN, '-', "Build and add certificate chain for EE cert,"},
114 "which is the 1st cert from -in matching the privte key (if given)"},
115 {"name", OPT_NAME, 's', "Use name as friendly name"},
116 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
117 {"caname", OPT_CANAME, 's',
118 "Use name as CA friendly name (can be repeated)"},
119 {"LMK", OPT_LMK, '-',
120 "Add local machine keyset attribute to private key"},
121 {"keyex", OPT_KEYEX, '-', "Set key type to MS key exchange"},
122 {"keysig", OPT_KEYSIG, '-', "Set key type to MS key signature"},
124 OPT_SECTION("PKCS12 output encryption and MAC"),
125 {"descert", OPT_DESCERT, '-',
126 "Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)"},
127 {"certpbe", OPT_CERTPBE, 's',
128 "Certificate PBE algorithm (default PBES2 with PBKDF2 and AES-256 CBC)"},
129 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default AES-256 CBC)"},
130 {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption and MAC"},
131 {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"},
132 {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
133 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
134 {"macalg", OPT_MACALG, 's',
135 "Digest algorithm to use in MAC (default SHA1)"},
136 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
137 {"noenc", OPT_NOENC, '-', "Don't encrypt private keys"},
138 {"nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated"},
139 {"", OPT_CIPHER, '-', "Any supported cipher"},
146 #define PKCS12_DEFAULT_PBE NID_aes_256_cbc
148 int pkcs12_main(int argc, char **argv)
150 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
151 char *untrusted = NULL;
152 char *passcertsarg = NULL, *passcerts = NULL;
153 char *name = NULL, *csp_name = NULL;
154 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
155 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0, use_legacy = 0;
156 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
157 int cert_pbe = PKCS12_DEFAULT_PBE;
158 int key_pbe = PKCS12_DEFAULT_PBE;
159 int ret = 1, macver = 1, add_lmk = 0, private = 0;
161 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
162 char *passin = NULL, *passout = NULL, *macalg = NULL;
163 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
164 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
165 int noCApath = 0, noCAfile = 0, noCAstore = 0;
167 BIO *in = NULL, *out = NULL;
169 STACK_OF(OPENSSL_STRING) *canames = NULL;
170 const EVP_CIPHER *const default_enc = EVP_aes_256_cbc();
171 const EVP_CIPHER *enc = default_enc;
174 prog = opt_init(argc, argv, pkcs12_options);
175 while ((o = opt_next()) != OPT_EOF) {
180 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
183 opt_help(pkcs12_options);
205 options |= (NOKEYS | NOCERTS);
220 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
226 if (!opt_cipher(opt_unknown(), &enc))
230 if (!opt_int(opt_arg(), &iter))
255 if (!set_pbe(&cert_pbe, opt_arg()))
259 if (!set_pbe(&key_pbe, opt_arg()))
270 certfile = opt_arg();
273 untrusted = opt_arg();
276 passcertsarg = opt_arg();
285 csp_name = opt_arg();
289 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
291 sk_OPENSSL_STRING_push(canames, opt_arg());
300 passinarg = opt_arg();
303 passoutarg = opt_arg();
327 e = setup_engine(opt_arg(), 0);
333 if (!opt_provider(o))
338 argc = opt_num_rest();
342 BIO_printf(bio_err, "Warning: -chain option ignored without -export\n");
343 if (certfile != NULL)
344 BIO_printf(bio_err, "Warning: -certfile option ignored without -export\n");
345 if (untrusted != NULL)
346 BIO_printf(bio_err, "Warning: -untrusted option ignored without -export\n");
347 if (passcertsarg != NULL)
349 "Warning: -passcerts option ignored without -export\n");
350 if (CApath != NULL || noCApath)
351 BIO_printf(bio_err, "Warning: -[no-]CApath option ignored without -export\n");
352 if (CAfile != NULL || noCAfile)
353 BIO_printf(bio_err, "Warning: -[no-]CAfile option ignored without -export\n");
354 if (CAstore != NULL || noCAstore)
355 BIO_printf(bio_err, "Warning: -[no-]CAstore option ignored without -export\n");
357 BIO_printf(bio_err, "Warning: -LMK option ignored without -export\n");
359 BIO_printf(bio_err, "Warning: -name option ignored without -export\n");
360 if (csp_name != NULL)
361 BIO_printf(bio_err, "Warning: -CSP option ignored without -export\n");
363 BIO_printf(bio_err, "Warning: -caname option ignored without -export\n");
365 BIO_printf(bio_err, "Warning: -inkey option ignored without -export\n");
367 BIO_printf(bio_err, "Warning: -keyex and -keysig options ignored without -export\n");
369 BIO_printf(bio_err, "Warning: -macalg option ignored without -export\n");
372 /* load the legacy provider if not loaded already*/
373 if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
374 if (!app_provider_load(app_get0_libctx(), "legacy"))
376 /* load the default provider explicitly */
377 if (!app_provider_load(app_get0_libctx(), "default"))
380 if (cert_pbe == PKCS12_DEFAULT_PBE) {
381 /* Adapt default algorithm */
382 #ifndef OPENSSL_NO_RC2
383 cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
385 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
389 if (key_pbe == PKCS12_DEFAULT_PBE)
390 key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
391 if (enc == default_enc)
392 enc = EVP_des_ede3_cbc();
400 if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
401 BIO_printf(bio_err, "Error getting certificate file password\n");
405 if (passarg != NULL) {
407 passoutarg = passarg;
412 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
413 BIO_printf(bio_err, "Error getting passwords\n");
429 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
431 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
440 /* To avoid bit rot */
442 #ifndef OPENSSL_NO_UI_CONSOLE
443 if (EVP_read_pw_string(
444 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
445 BIO_printf(bio_err, "Can't read Password\n");
450 BIO_printf(bio_err, "Unsupported option -twopass\n");
456 EVP_PKEY *key = NULL;
457 X509 *ee_cert = NULL, *x = NULL;
458 STACK_OF(X509) *certs = NULL;
459 STACK_OF(X509) *untrusted_certs = NULL;
460 const EVP_MD *macmd = NULL;
461 unsigned char *catmp = NULL;
464 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
465 BIO_printf(bio_err, "Nothing to export due to -nocerts and -nokeys or -noout!\n");
469 if ((options & NOCERTS) != 0) {
471 BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
474 if (!(options & NOKEYS)) {
475 key = load_key(keyname ? keyname : infile,
476 FORMAT_PEM, 1, passin, e,
478 "private key from -inkey file" :
479 "private key from -in file");
484 /* Load all certs in input file */
485 if (!(options & NOCERTS)) {
486 if (!load_certs(infile, &certs, passin,
487 "certificates from -in file"))
489 if (sk_X509_num(certs) < 1) {
490 BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
495 /* Look for matching private key */
496 for (i = 0; i < sk_X509_num(certs); i++) {
497 x = sk_X509_value(certs, i);
498 if (X509_check_private_key(x, key)) {
500 /* Zero keyid and alias */
501 X509_keyid_set1(ee_cert, NULL, 0);
502 X509_alias_set1(ee_cert, NULL, 0);
503 /* Remove from list */
504 (void)sk_X509_delete(certs, i);
508 if (ee_cert == NULL) {
510 "No cert in -in file '%s' matches private key\n",
515 ee_cert = X509_dup(sk_X509_value(certs, 0)); /* take 1st cert */
519 /* Load any untrusted certificates for chain building */
520 if (untrusted != NULL) {
521 if (!load_certs(untrusted, &untrusted_certs, passcerts,
522 "untrusted certificates"))
526 /* If chaining get chain from end entity cert */
529 STACK_OF(X509) *chain2;
532 if (ee_cert == NULL) {
534 "No end entity certificate to check with -chain\n");
538 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
543 vret = get_cert_chain(ee_cert, store, untrusted_certs, &chain2);
544 X509_STORE_free(store);
546 if (vret == X509_V_OK) {
548 /* Remove from chain2 the first (end entity) certificate */
549 X509_free(sk_X509_shift(chain2));
550 /* Add the remaining certs (except for duplicates) */
551 add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
552 | X509_ADD_FLAG_NO_DUP);
553 sk_X509_pop_free(chain2, X509_free);
557 if (vret != X509_V_ERR_UNSPECIFIED)
558 BIO_printf(bio_err, "Error getting chain: %s\n",
559 X509_verify_cert_error_string(vret));
564 /* Add any extra certificates asked for */
565 if (certfile != NULL) {
566 if (!load_certs(certfile, &certs, passcerts,
567 "extra certificates from -certfile"))
571 /* Add any CA names */
572 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
573 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
574 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
577 if (csp_name != NULL && key != NULL)
578 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
579 MBSTRING_ASC, (unsigned char *)csp_name,
582 if (add_lmk && key != NULL)
583 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
585 if (!noprompt && !(enc == NULL && maciter == -1)) {
586 /* To avoid bit rot */
588 #ifndef OPENSSL_NO_UI_CONSOLE
589 if (EVP_read_pw_string(pass, sizeof(pass),
590 "Enter Export Password:", 1)) {
591 BIO_printf(bio_err, "Can't read Password\n");
596 BIO_printf(bio_err, "Password required\n");
602 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
604 p12 = PKCS12_create(cpass, name, key, ee_cert, certs,
605 key_pbe, cert_pbe, iter, -1, keytype);
608 BIO_printf(bio_err, "Error creating PKCS12 structure for %s\n",
613 if (macalg != NULL) {
614 if (!opt_md(macalg, &macmd))
619 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
623 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
627 i2d_PKCS12_bio(out, p12);
634 sk_X509_pop_free(certs, X509_free);
635 sk_X509_pop_free(untrusted_certs, X509_free);
638 ERR_print_errors(bio_err);
643 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
646 out = bio_open_owner(outfile, FORMAT_PEM, private);
650 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
651 ERR_print_errors(bio_err);
657 #ifndef OPENSSL_NO_UI_CONSOLE
658 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
660 BIO_printf(bio_err, "Can't read Password\n");
665 BIO_printf(bio_err, "Password required\n");
671 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
673 if ((options & INFO) && PKCS12_mac_present(p12)) {
674 const ASN1_INTEGER *tmaciter;
675 const X509_ALGOR *macalgid;
676 const ASN1_OBJECT *macobj;
677 const ASN1_OCTET_STRING *tmac;
678 const ASN1_OCTET_STRING *tsalt;
680 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
681 /* current hash algorithms do not use parameters so extract just name,
682 in future alg_print() may be needed */
683 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
684 BIO_puts(bio_err, "MAC: ");
685 i2a_ASN1_OBJECT(bio_err, macobj);
686 BIO_printf(bio_err, ", Iteration %ld\n",
687 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
688 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
689 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
690 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
693 /* If we enter empty password try no password first */
694 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
695 /* If mac and crypto pass the same set it to NULL too */
698 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
700 * May be UTF8 from previous version of OpenSSL:
701 * convert to a UTF8 form which will translate
702 * to the same Unicode password.
706 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
709 badpass = OPENSSL_uni2utf8(utmp, utmplen);
711 if (!PKCS12_verify_mac(p12, badpass, -1)) {
712 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
713 ERR_print_errors(bio_err);
716 BIO_printf(bio_err, "Warning: using broken algorithm\n");
724 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
725 BIO_printf(bio_err, "Error outputting keys and certificates\n");
726 ERR_print_errors(bio_err);
735 sk_OPENSSL_STRING_free(canames);
736 OPENSSL_free(badpass);
737 OPENSSL_free(passcerts);
738 OPENSSL_free(passin);
739 OPENSSL_free(passout);
743 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
744 int passlen, int options, char *pempass,
745 const EVP_CIPHER *enc)
747 STACK_OF(PKCS7) *asafes = NULL;
748 STACK_OF(PKCS12_SAFEBAG) *bags;
753 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
755 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
756 p7 = sk_PKCS7_value(asafes, i);
757 bagnid = OBJ_obj2nid(p7->type);
758 if (bagnid == NID_pkcs7_data) {
759 bags = PKCS12_unpack_p7data(p7);
761 BIO_printf(bio_err, "PKCS7 Data\n");
762 } else if (bagnid == NID_pkcs7_encrypted) {
763 if (options & INFO) {
764 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
765 alg_print(p7->d.encrypted->enc_data->algorithm);
767 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
773 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
774 options, pempass, enc)) {
775 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
778 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
784 sk_PKCS7_pop_free(asafes, PKCS7_free);
788 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
789 const char *pass, int passlen, int options,
790 char *pempass, const EVP_CIPHER *enc)
793 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
794 if (!dump_certs_pkeys_bag(out,
795 sk_PKCS12_SAFEBAG_value(bags, i),
796 pass, passlen, options, pempass, enc))
802 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
803 const char *pass, int passlen, int options,
804 char *pempass, const EVP_CIPHER *enc)
807 PKCS8_PRIV_KEY_INFO *p8;
808 const PKCS8_PRIV_KEY_INFO *p8c;
810 const STACK_OF(X509_ATTRIBUTE) *attrs;
813 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
815 switch (PKCS12_SAFEBAG_get_nid(bag)) {
818 BIO_printf(bio_err, "Key bag\n");
819 if (options & NOKEYS)
821 print_attribs(out, attrs, "Bag Attributes");
822 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
823 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
825 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
826 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
830 case NID_pkcs8ShroudedKeyBag:
831 if (options & INFO) {
833 const X509_ALGOR *tp8alg;
835 BIO_printf(bio_err, "Shrouded Keybag: ");
836 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
837 X509_SIG_get0(tp8, &tp8alg, NULL);
840 if (options & NOKEYS)
842 print_attribs(out, attrs, "Bag Attributes");
843 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
845 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
846 PKCS8_PRIV_KEY_INFO_free(p8);
849 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
850 PKCS8_PRIV_KEY_INFO_free(p8);
851 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
857 BIO_printf(bio_err, "Certificate bag\n");
858 if (options & NOCERTS)
860 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
861 if (options & CACERTS)
863 } else if (options & CLCERTS)
865 print_attribs(out, attrs, "Bag Attributes");
866 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
868 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
870 dump_cert_text(out, x509);
871 ret = PEM_write_bio_X509(out, x509);
877 BIO_printf(bio_err, "Secret bag\n");
878 print_attribs(out, attrs, "Bag Attributes");
879 BIO_printf(bio_err, "Bag Type: ");
880 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
881 BIO_printf(bio_err, "\nBag Value: ");
882 print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
885 case NID_safeContentsBag:
887 BIO_printf(bio_err, "Safe Contents bag\n");
888 print_attribs(out, attrs, "Bag Attributes");
889 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
890 pass, passlen, options, pempass, enc);
893 BIO_printf(bio_err, "Warning unsupported bag type: ");
894 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
895 BIO_printf(bio_err, "\n");
901 /* Given a single certificate return a verified chain or NULL if error */
903 static int get_cert_chain(X509 *cert, X509_STORE *store,
904 STACK_OF(X509) *untrusted_certs,
905 STACK_OF(X509) **chain)
907 X509_STORE_CTX *store_ctx = NULL;
908 STACK_OF(X509) *chn = NULL;
911 store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
912 if (store_ctx == NULL) {
913 i = X509_V_ERR_UNSPECIFIED;
916 if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
917 i = X509_V_ERR_UNSPECIFIED;
922 if (X509_verify_cert(store_ctx) > 0)
923 chn = X509_STORE_CTX_get1_chain(store_ctx);
924 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
925 i = X509_V_ERR_UNSPECIFIED;
928 X509_STORE_CTX_free(store_ctx);
933 static int alg_print(const X509_ALGOR *alg)
935 int pbenid, aparamtype;
936 const ASN1_OBJECT *aoid;
938 PBEPARAM *pbe = NULL;
940 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
942 pbenid = OBJ_obj2nid(aoid);
944 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
947 * If PBE algorithm is PBES2 decode algorithm parameters
948 * for additional details.
950 if (pbenid == NID_pbes2) {
951 PBE2PARAM *pbe2 = NULL;
953 if (aparamtype == V_ASN1_SEQUENCE)
954 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
956 BIO_puts(bio_err, ", <unsupported parameters>");
959 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
960 pbenid = OBJ_obj2nid(aoid);
961 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
962 encnid = OBJ_obj2nid(aoid);
963 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
965 /* If KDF is PBKDF2 decode parameters */
966 if (pbenid == NID_id_pbkdf2) {
967 PBKDF2PARAM *kdf = NULL;
969 if (aparamtype == V_ASN1_SEQUENCE)
970 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
972 BIO_puts(bio_err, ", <unsupported parameters>");
976 if (kdf->prf == NULL) {
977 prfnid = NID_hmacWithSHA1;
979 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
980 prfnid = OBJ_obj2nid(aoid);
982 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
983 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
984 PBKDF2PARAM_free(kdf);
985 #ifndef OPENSSL_NO_SCRYPT
986 } else if (pbenid == NID_id_scrypt) {
987 SCRYPT_PARAMS *kdf = NULL;
989 if (aparamtype == V_ASN1_SEQUENCE)
990 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
992 BIO_puts(bio_err, ", <unsupported parameters>");
995 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
996 "Block size(r): %ld, Parallelism(p): %ld",
997 ASN1_STRING_length(kdf->salt),
998 ASN1_INTEGER_get(kdf->costParameter),
999 ASN1_INTEGER_get(kdf->blockSize),
1000 ASN1_INTEGER_get(kdf->parallelizationParameter));
1001 SCRYPT_PARAMS_free(kdf);
1004 PBE2PARAM_free(pbe2);
1006 if (aparamtype == V_ASN1_SEQUENCE)
1007 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
1009 BIO_puts(bio_err, ", <unsupported parameters>");
1012 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
1016 BIO_puts(bio_err, "\n");
1020 /* Load all certificates from a given file */
1022 int cert_load(BIO *in, STACK_OF(X509) *sk)
1027 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1029 if (!sk_X509_push(sk, cert))
1037 /* Generalised x509 attribute value print */
1039 void print_attribute(BIO *out, const ASN1_TYPE *av)
1044 case V_ASN1_BMPSTRING:
1045 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1046 av->value.bmpstring->length);
1047 BIO_printf(out, "%s\n", value);
1048 OPENSSL_free(value);
1051 case V_ASN1_UTF8STRING:
1052 BIO_printf(out, "%s\n", av->value.utf8string->data);
1055 case V_ASN1_OCTET_STRING:
1056 hex_prin(out, av->value.octet_string->data,
1057 av->value.octet_string->length);
1058 BIO_printf(out, "\n");
1061 case V_ASN1_BIT_STRING:
1062 hex_prin(out, av->value.bit_string->data,
1063 av->value.bit_string->length);
1064 BIO_printf(out, "\n");
1068 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1073 /* Generalised attribute print: handle PKCS#8 and bag attributes */
1075 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
1078 X509_ATTRIBUTE *attr;
1082 BIO_printf(out, "%s: <No Attributes>\n", name);
1085 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1086 BIO_printf(out, "%s: <Empty Attributes>\n", name);
1089 BIO_printf(out, "%s\n", name);
1090 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1091 ASN1_OBJECT *attr_obj;
1092 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1093 attr_obj = X509_ATTRIBUTE_get0_object(attr);
1094 attr_nid = OBJ_obj2nid(attr_obj);
1095 BIO_printf(out, " ");
1096 if (attr_nid == NID_undef) {
1097 i2a_ASN1_OBJECT(out, attr_obj);
1098 BIO_printf(out, ": ");
1100 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1103 if (X509_ATTRIBUTE_count(attr)) {
1104 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
1106 av = X509_ATTRIBUTE_get0_type(attr, j);
1107 print_attribute(out, av);
1110 BIO_printf(out, "<No Values>\n");
1116 void hex_prin(BIO *out, unsigned char *buf, int len)
1119 for (i = 0; i < len; i++)
1120 BIO_printf(out, "%02X ", buf[i]);
1123 static int set_pbe(int *ppbe, const char *str)
1127 if (strcmp(str, "NONE") == 0) {
1131 *ppbe = OBJ_txt2nid(str);
1132 if (*ppbe == NID_undef) {
1133 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);