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, '-', "use legacy algorithms"},
68 #ifndef OPENSSL_NO_ENGINE
69 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
72 OPT_SECTION("CA input for export with the -chain option"),
73 {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"},
74 {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"},
75 {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
76 {"no-CAfile", OPT_NOCAFILE, '-',
77 "Do not load the default certificates file"},
78 {"no-CApath", OPT_NOCAPATH, '-',
79 "Do not load certificates from the default certificates directory"},
80 {"no-CAstore", OPT_NOCASTORE, '-',
81 "Do not load certificates from the default certificates store"},
84 {"in", OPT_IN, '<', "Input file for PKCS12 parsing or certs and possibly key"},
85 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
86 {"inkey", OPT_INKEY, 's', "Private key, else read from -in input file"},
87 {"certfile", OPT_CERTFILE, '<', "Extra certificates for PKCS12 output"},
88 {"untrusted", OPT_UNTRUSTED, '<', "Untrusted certificates for chain building"},
89 {"passcerts", OPT_PASSCERTS, 's', "Certificate file pass phrase source"},
90 {"name", OPT_NAME, 's', "Use name as friendly name"},
91 {"CSP", OPT_CSP, 's', "Microsoft CSP name"},
92 {"caname", OPT_CANAME, 's',
93 "Use name as CA friendly name (can be repeated)"},
95 OPT_SECTION("Output"),
96 {"export", OPT_EXPORT, '-', "Output PKCS12 file"},
98 "Add local machine keyset attribute to private key"},
99 {"macalg", OPT_MACALG, 's',
100 "Digest algorithm to use in MAC (default SHA1)"},
101 {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"},
102 {"out", OPT_OUT, '>', "Output filename"},
103 {"passout", OPT_PASSOUT, 's', "Output pass phrase source"},
104 {"password", OPT_PASSWORD, 's', "Set import/export password source"},
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"},
109 {"chain", OPT_CHAIN, '-', "Build and add certificate chain for EE cert,"},
111 "which is the 1st cert from -in matching the private key (if given)"},
112 {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"},
113 {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"},
114 {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"},
115 {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"},
116 {"keyex", OPT_KEYEX, '-', "Set key type to MS key exchange"},
117 {"keysig", OPT_KEYSIG, '-', "Set key type to MS key signature"},
119 OPT_SECTION("PKCS12 output encryption and MAC"),
120 #ifndef OPENSSL_NO_RC2
121 {"descert", OPT_DESCERT, '-',
122 "Encrypt output with 3DES (default PBES2 with PBKDF2 and AES-256 CBC)"},
123 {"certpbe", OPT_CERTPBE, 's',
124 "Certificate PBE algorithm (default PBES2 with PBKDF2 and AES-256 CBC)"},
126 {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"},
127 {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"},
129 {"iter", OPT_ITER, 'p', "Specify the iteration count for encryption key and MAC"},
130 {"noiter", OPT_NOITER, '-', "Don't use encryption key iteration"},
131 {"maciter", OPT_MACITER, '-', "Unused, kept for backwards compatibility"},
132 {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"},
133 {"nomac", OPT_NOMAC, '-', "Don't generate MAC"},
134 {"noenc", OPT_NOENC, '-', "Don't encrypt private keys"},
135 {"nodes", OPT_NODES, '-', "Don't encrypt private keys; deprecated"},
136 {"", OPT_CIPHER, '-', "Any supported cipher"},
143 int pkcs12_main(int argc, char **argv)
145 char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL;
146 char *untrusted = NULL;
147 char *passcertsarg = NULL, *passcerts = NULL;
148 char *name = NULL, *csp_name = NULL;
149 char pass[PASSWD_BUF_SIZE] = "", macpass[PASSWD_BUF_SIZE] = "";
150 int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0, use_legacy = 0;
151 int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER;
152 int cert_pbe = NID_aes_256_cbc;
153 int key_pbe = NID_aes_256_cbc;
154 int ret = 1, macver = 1, add_lmk = 0, private = 0;
156 char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL;
157 char *passin = NULL, *passout = NULL, *macalg = NULL;
158 char *cpass = NULL, *mpass = NULL, *badpass = NULL;
159 const char *CApath = NULL, *CAfile = NULL, *CAstore = NULL, *prog;
160 int noCApath = 0, noCAfile = 0, noCAstore = 0;
162 BIO *in = NULL, *out = NULL;
164 STACK_OF(OPENSSL_STRING) *canames = NULL;
165 const EVP_CIPHER *enc = EVP_aes_256_cbc();
168 prog = opt_init(argc, argv, pkcs12_options);
169 while ((o = opt_next()) != OPT_EOF) {
174 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
177 opt_help(pkcs12_options);
199 options |= (NOKEYS | NOCERTS);
214 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
220 if (!opt_cipher(opt_unknown(), &enc))
224 if (!opt_int(opt_arg(), &iter))
248 if (!set_pbe(&cert_pbe, opt_arg()))
252 if (!set_pbe(&key_pbe, opt_arg()))
263 certfile = opt_arg();
266 untrusted = opt_arg();
269 passcertsarg = opt_arg();
278 csp_name = opt_arg();
282 && (canames = sk_OPENSSL_STRING_new_null()) == NULL)
284 sk_OPENSSL_STRING_push(canames, opt_arg());
293 passinarg = opt_arg();
296 passoutarg = opt_arg();
320 e = setup_engine(opt_arg(), 0);
326 if (!opt_provider(o))
331 argc = opt_num_rest();
335 BIO_printf(bio_err, "Warning: -chain option ignored without -export\n");
336 if (certfile != NULL)
337 BIO_printf(bio_err, "Warning: -certfile option ignored without -export\n");
338 if (untrusted != NULL)
339 BIO_printf(bio_err, "Warning: -untrusted option ignored without -export\n");
340 if (passcertsarg != NULL)
342 "Warning: -passcerts option ignored without -export\n");
343 if (CApath != NULL || noCApath)
344 BIO_printf(bio_err, "Warning: -[no-]CApath option ignored without -export\n");
345 if (CAfile != NULL || noCAfile)
346 BIO_printf(bio_err, "Warning: -[no-]CAfile option ignored without -export\n");
347 if (CAstore != NULL || noCAstore)
348 BIO_printf(bio_err, "Warning: -[no-]CAstore option ignored without -export\n");
350 BIO_printf(bio_err, "Warning: -LMK option ignored without -export\n");
352 BIO_printf(bio_err, "Warning: -name option ignored without -export\n");
353 if (csp_name != NULL)
354 BIO_printf(bio_err, "Warning: -CSP option ignored without -export\n");
356 BIO_printf(bio_err, "Warning: -caname option ignored without -export\n");
358 BIO_printf(bio_err, "Warning: -inkey option ignored without -export\n");
360 BIO_printf(bio_err, "Warning: -keyex and -keysig options ignored without -export\n");
362 BIO_printf(bio_err, "Warning: -macalg option ignored without -export\n");
365 /* load the legacy provider if not loaded already*/
366 if (!OSSL_PROVIDER_available(app_get0_libctx(), "legacy")) {
367 if (!app_provider_load(app_get0_libctx(), "legacy"))
369 /* load the default provider explicitly */
370 if (!app_provider_load(app_get0_libctx(), "default"))
373 if (cert_pbe != NID_pbe_WithSHA1And3_Key_TripleDES_CBC) {
374 /* Restore default algorithms */
375 #ifndef OPENSSL_NO_RC2
376 cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
378 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
382 key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
383 enc = EVP_des_ede3_cbc();
391 if (!app_passwd(passcertsarg, NULL, &passcerts, NULL)) {
392 BIO_printf(bio_err, "Error getting certificate file password\n");
396 if (passarg != NULL) {
398 passoutarg = passarg;
403 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
404 BIO_printf(bio_err, "Error getting passwords\n");
420 BIO_printf(bio_err, "Option -twopass cannot be used with -passout or -password\n");
422 BIO_printf(bio_err, "Option -twopass cannot be used with -passin or -password\n");
431 /* To avoid bit rot */
433 #ifndef OPENSSL_NO_UI_CONSOLE
434 if (EVP_read_pw_string(
435 macpass, sizeof(macpass), "Enter MAC Password:", export_cert)) {
436 BIO_printf(bio_err, "Can't read Password\n");
441 BIO_printf(bio_err, "Unsupported option -twopass\n");
447 EVP_PKEY *key = NULL;
448 X509 *ee_cert = NULL, *x = NULL;
449 STACK_OF(X509) *certs = NULL;
450 STACK_OF(X509) *untrusted_certs = NULL;
451 const EVP_MD *macmd = NULL;
452 unsigned char *catmp = NULL;
455 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
456 BIO_printf(bio_err, "Nothing to export due to -nocerts and -nokeys or -noout!\n");
460 if ((options & NOCERTS) != 0) {
462 BIO_printf(bio_err, "Warning: -chain option ignored with -nocerts\n");
465 if (!(options & NOKEYS)) {
466 key = load_key(keyname ? keyname : infile,
467 FORMAT_PEM, 1, passin, e,
469 "private key from -inkey file" :
470 "private key from -in file");
475 /* Load all certs in input file */
476 if (!(options & NOCERTS)) {
477 if (!load_certs(infile, &certs, passin,
478 "certificates from -in file"))
480 if (sk_X509_num(certs) < 1) {
481 BIO_printf(bio_err, "No certificate in -in file %s\n", infile);
486 /* Look for matching private key */
487 for (i = 0; i < sk_X509_num(certs); i++) {
488 x = sk_X509_value(certs, i);
489 if (X509_check_private_key(x, key)) {
491 /* Zero keyid and alias */
492 X509_keyid_set1(ee_cert, NULL, 0);
493 X509_alias_set1(ee_cert, NULL, 0);
494 /* Remove from list */
495 (void)sk_X509_delete(certs, i);
499 if (ee_cert == NULL) {
501 "No cert in -in file '%s' matches private key\n",
506 ee_cert = X509_dup(sk_X509_value(certs, 0)); /* take 1st cert */
510 /* Load any untrusted certificates for chain building */
511 if (untrusted != NULL) {
512 if (!load_certs(untrusted, &untrusted_certs, passcerts,
513 "untrusted certificates"))
517 /* If chaining get chain from end entity cert */
520 STACK_OF(X509) *chain2;
523 if (ee_cert == NULL) {
525 "No end entity certificate to check with -chain\n");
529 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
534 vret = get_cert_chain(ee_cert, store, untrusted_certs, &chain2);
535 X509_STORE_free(store);
537 if (vret == X509_V_OK) {
539 /* Remove from chain2 the first (end entity) certificate */
540 X509_free(sk_X509_shift(chain2));
541 /* Add the remaining certs (except for duplicates) */
542 add_certs = X509_add_certs(certs, chain2, X509_ADD_FLAG_UP_REF
543 | X509_ADD_FLAG_NO_DUP);
544 sk_X509_pop_free(chain2, X509_free);
548 if (vret != X509_V_ERR_UNSPECIFIED)
549 BIO_printf(bio_err, "Error getting chain: %s\n",
550 X509_verify_cert_error_string(vret));
555 /* Add any extra certificates asked for */
556 if (certfile != NULL) {
557 if (!load_certs(certfile, &certs, passcerts,
558 "extra certificates from -certfile"))
562 /* Add any CA names */
563 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
564 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
565 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
568 if (csp_name != NULL && key != NULL)
569 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
570 MBSTRING_ASC, (unsigned char *)csp_name,
573 if (add_lmk && key != NULL)
574 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
577 /* To avoid bit rot */
579 #ifndef OPENSSL_NO_UI_CONSOLE
580 if (EVP_read_pw_string(pass, sizeof(pass),
581 "Enter Export Password:", 1)) {
582 BIO_printf(bio_err, "Can't read Password\n");
587 BIO_printf(bio_err, "Password required\n");
593 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
595 p12 = PKCS12_create(cpass, name, key, ee_cert, certs,
596 key_pbe, cert_pbe, iter, -1, keytype);
599 ERR_print_errors(bio_err);
603 if (macalg != NULL) {
604 if (!opt_md(macalg, &macmd))
609 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
613 out = bio_open_owner(outfile, FORMAT_PKCS12, private);
617 i2d_PKCS12_bio(out, p12);
624 sk_X509_pop_free(certs, X509_free);
625 sk_X509_pop_free(untrusted_certs, X509_free);
632 in = bio_open_default(infile, 'r', FORMAT_PKCS12);
635 out = bio_open_owner(outfile, FORMAT_PEM, private);
639 if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) {
640 ERR_print_errors(bio_err);
646 #ifndef OPENSSL_NO_UI_CONSOLE
647 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Import Password:",
649 BIO_printf(bio_err, "Can't read Password\n");
654 BIO_printf(bio_err, "Password required\n");
660 OPENSSL_strlcpy(macpass, pass, sizeof(macpass));
662 if ((options & INFO) && PKCS12_mac_present(p12)) {
663 const ASN1_INTEGER *tmaciter;
664 const X509_ALGOR *macalgid;
665 const ASN1_OBJECT *macobj;
666 const ASN1_OCTET_STRING *tmac;
667 const ASN1_OCTET_STRING *tsalt;
669 PKCS12_get0_mac(&tmac, &macalgid, &tsalt, &tmaciter, p12);
670 /* current hash algorithms do not use parameters so extract just name,
671 in future alg_print() may be needed */
672 X509_ALGOR_get0(&macobj, NULL, NULL, macalgid);
673 BIO_puts(bio_err, "MAC: ");
674 i2a_ASN1_OBJECT(bio_err, macobj);
675 BIO_printf(bio_err, ", Iteration %ld\n",
676 tmaciter != NULL ? ASN1_INTEGER_get(tmaciter) : 1L);
677 BIO_printf(bio_err, "MAC length: %ld, salt length: %ld\n",
678 tmac != NULL ? ASN1_STRING_length(tmac) : 0L,
679 tsalt != NULL ? ASN1_STRING_length(tsalt) : 0L);
682 /* If we enter empty password try no password first */
683 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
684 /* If mac and crypto pass the same set it to NULL too */
687 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
689 * May be UTF8 from previous version of OpenSSL:
690 * convert to a UTF8 form which will translate
691 * to the same Unicode password.
695 utmp = OPENSSL_asc2uni(mpass, -1, NULL, &utmplen);
698 badpass = OPENSSL_uni2utf8(utmp, utmplen);
700 if (!PKCS12_verify_mac(p12, badpass, -1)) {
701 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
702 ERR_print_errors(bio_err);
705 BIO_printf(bio_err, "Warning: using broken algorithm\n");
713 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) {
714 BIO_printf(bio_err, "Error outputting keys and certificates\n");
715 ERR_print_errors(bio_err);
724 sk_OPENSSL_STRING_free(canames);
725 OPENSSL_free(badpass);
726 OPENSSL_free(passcerts);
727 OPENSSL_free(passin);
728 OPENSSL_free(passout);
732 int dump_certs_keys_p12(BIO *out, const PKCS12 *p12, const char *pass,
733 int passlen, int options, char *pempass,
734 const EVP_CIPHER *enc)
736 STACK_OF(PKCS7) *asafes = NULL;
737 STACK_OF(PKCS12_SAFEBAG) *bags;
742 if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
744 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
745 p7 = sk_PKCS7_value(asafes, i);
746 bagnid = OBJ_obj2nid(p7->type);
747 if (bagnid == NID_pkcs7_data) {
748 bags = PKCS12_unpack_p7data(p7);
750 BIO_printf(bio_err, "PKCS7 Data\n");
751 } else if (bagnid == NID_pkcs7_encrypted) {
752 if (options & INFO) {
753 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
754 alg_print(p7->d.encrypted->enc_data->algorithm);
756 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
762 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
763 options, pempass, enc)) {
764 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
767 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
773 sk_PKCS7_pop_free(asafes, PKCS7_free);
777 int dump_certs_pkeys_bags(BIO *out, const STACK_OF(PKCS12_SAFEBAG) *bags,
778 const char *pass, int passlen, int options,
779 char *pempass, const EVP_CIPHER *enc)
782 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
783 if (!dump_certs_pkeys_bag(out,
784 sk_PKCS12_SAFEBAG_value(bags, i),
785 pass, passlen, options, pempass, enc))
791 int dump_certs_pkeys_bag(BIO *out, const PKCS12_SAFEBAG *bag,
792 const char *pass, int passlen, int options,
793 char *pempass, const EVP_CIPHER *enc)
796 PKCS8_PRIV_KEY_INFO *p8;
797 const PKCS8_PRIV_KEY_INFO *p8c;
799 const STACK_OF(X509_ATTRIBUTE) *attrs;
802 attrs = PKCS12_SAFEBAG_get0_attrs(bag);
804 switch (PKCS12_SAFEBAG_get_nid(bag)) {
807 BIO_printf(bio_err, "Key bag\n");
808 if (options & NOKEYS)
810 print_attribs(out, attrs, "Bag Attributes");
811 p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
812 if ((pkey = EVP_PKCS82PKEY(p8c)) == NULL)
814 print_attribs(out, PKCS8_pkey_get0_attrs(p8c), "Key Attributes");
815 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
819 case NID_pkcs8ShroudedKeyBag:
820 if (options & INFO) {
822 const X509_ALGOR *tp8alg;
824 BIO_printf(bio_err, "Shrouded Keybag: ");
825 tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
826 X509_SIG_get0(tp8, &tp8alg, NULL);
829 if (options & NOKEYS)
831 print_attribs(out, attrs, "Bag Attributes");
832 if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL)
834 if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) {
835 PKCS8_PRIV_KEY_INFO_free(p8);
838 print_attribs(out, PKCS8_pkey_get0_attrs(p8), "Key Attributes");
839 PKCS8_PRIV_KEY_INFO_free(p8);
840 ret = PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
846 BIO_printf(bio_err, "Certificate bag\n");
847 if (options & NOCERTS)
849 if (PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID)) {
850 if (options & CACERTS)
852 } else if (options & CLCERTS)
854 print_attribs(out, attrs, "Bag Attributes");
855 if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate)
857 if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL)
859 dump_cert_text(out, x509);
860 ret = PEM_write_bio_X509(out, x509);
866 BIO_printf(bio_err, "Secret bag\n");
867 print_attribs(out, attrs, "Bag Attributes");
868 BIO_printf(bio_err, "Bag Type: ");
869 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_bag_type(bag));
870 BIO_printf(bio_err, "\nBag Value: ");
871 print_attribute(out, PKCS12_SAFEBAG_get0_bag_obj(bag));
874 case NID_safeContentsBag:
876 BIO_printf(bio_err, "Safe Contents bag\n");
877 print_attribs(out, attrs, "Bag Attributes");
878 return dump_certs_pkeys_bags(out, PKCS12_SAFEBAG_get0_safes(bag),
879 pass, passlen, options, pempass, enc);
882 BIO_printf(bio_err, "Warning unsupported bag type: ");
883 i2a_ASN1_OBJECT(bio_err, PKCS12_SAFEBAG_get0_type(bag));
884 BIO_printf(bio_err, "\n");
890 /* Given a single certificate return a verified chain or NULL if error */
892 static int get_cert_chain(X509 *cert, X509_STORE *store,
893 STACK_OF(X509) *untrusted_certs,
894 STACK_OF(X509) **chain)
896 X509_STORE_CTX *store_ctx = NULL;
897 STACK_OF(X509) *chn = NULL;
900 store_ctx = X509_STORE_CTX_new_ex(app_get0_libctx(), app_get0_propq());
901 if (store_ctx == NULL) {
902 i = X509_V_ERR_UNSPECIFIED;
905 if (!X509_STORE_CTX_init(store_ctx, store, cert, untrusted_certs)) {
906 i = X509_V_ERR_UNSPECIFIED;
911 if (X509_verify_cert(store_ctx) > 0)
912 chn = X509_STORE_CTX_get1_chain(store_ctx);
913 else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)
914 i = X509_V_ERR_UNSPECIFIED;
917 X509_STORE_CTX_free(store_ctx);
922 static int alg_print(const X509_ALGOR *alg)
924 int pbenid, aparamtype;
925 const ASN1_OBJECT *aoid;
927 PBEPARAM *pbe = NULL;
929 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg);
931 pbenid = OBJ_obj2nid(aoid);
933 BIO_printf(bio_err, "%s", OBJ_nid2ln(pbenid));
936 * If PBE algorithm is PBES2 decode algorithm parameters
937 * for additional details.
939 if (pbenid == NID_pbes2) {
940 PBE2PARAM *pbe2 = NULL;
942 if (aparamtype == V_ASN1_SEQUENCE)
943 pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM));
945 BIO_puts(bio_err, ", <unsupported parameters>");
948 X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc);
949 pbenid = OBJ_obj2nid(aoid);
950 X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption);
951 encnid = OBJ_obj2nid(aoid);
952 BIO_printf(bio_err, ", %s, %s", OBJ_nid2ln(pbenid),
954 /* If KDF is PBKDF2 decode parameters */
955 if (pbenid == NID_id_pbkdf2) {
956 PBKDF2PARAM *kdf = NULL;
958 if (aparamtype == V_ASN1_SEQUENCE)
959 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM));
961 BIO_puts(bio_err, ", <unsupported parameters>");
965 if (kdf->prf == NULL) {
966 prfnid = NID_hmacWithSHA1;
968 X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf);
969 prfnid = OBJ_obj2nid(aoid);
971 BIO_printf(bio_err, ", Iteration %ld, PRF %s",
972 ASN1_INTEGER_get(kdf->iter), OBJ_nid2sn(prfnid));
973 PBKDF2PARAM_free(kdf);
974 #ifndef OPENSSL_NO_SCRYPT
975 } else if (pbenid == NID_id_scrypt) {
976 SCRYPT_PARAMS *kdf = NULL;
978 if (aparamtype == V_ASN1_SEQUENCE)
979 kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(SCRYPT_PARAMS));
981 BIO_puts(bio_err, ", <unsupported parameters>");
984 BIO_printf(bio_err, ", Salt length: %d, Cost(N): %ld, "
985 "Block size(r): %ld, Parallelism(p): %ld",
986 ASN1_STRING_length(kdf->salt),
987 ASN1_INTEGER_get(kdf->costParameter),
988 ASN1_INTEGER_get(kdf->blockSize),
989 ASN1_INTEGER_get(kdf->parallelizationParameter));
990 SCRYPT_PARAMS_free(kdf);
993 PBE2PARAM_free(pbe2);
995 if (aparamtype == V_ASN1_SEQUENCE)
996 pbe = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBEPARAM));
998 BIO_puts(bio_err, ", <unsupported parameters>");
1001 BIO_printf(bio_err, ", Iteration %ld", ASN1_INTEGER_get(pbe->iter));
1005 BIO_puts(bio_err, "\n");
1009 /* Load all certificates from a given file */
1011 int cert_load(BIO *in, STACK_OF(X509) *sk)
1016 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1018 if (!sk_X509_push(sk, cert))
1026 /* Generalised x509 attribute value print */
1028 void print_attribute(BIO *out, const ASN1_TYPE *av)
1033 case V_ASN1_BMPSTRING:
1034 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1035 av->value.bmpstring->length);
1036 BIO_printf(out, "%s\n", value);
1037 OPENSSL_free(value);
1040 case V_ASN1_UTF8STRING:
1041 BIO_printf(out, "%s\n", av->value.utf8string->data);
1044 case V_ASN1_OCTET_STRING:
1045 hex_prin(out, av->value.octet_string->data,
1046 av->value.octet_string->length);
1047 BIO_printf(out, "\n");
1050 case V_ASN1_BIT_STRING:
1051 hex_prin(out, av->value.bit_string->data,
1052 av->value.bit_string->length);
1053 BIO_printf(out, "\n");
1057 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1062 /* Generalised attribute print: handle PKCS#8 and bag attributes */
1064 int print_attribs(BIO *out, const STACK_OF(X509_ATTRIBUTE) *attrlst,
1067 X509_ATTRIBUTE *attr;
1071 BIO_printf(out, "%s: <No Attributes>\n", name);
1074 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1075 BIO_printf(out, "%s: <Empty Attributes>\n", name);
1078 BIO_printf(out, "%s\n", name);
1079 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1080 ASN1_OBJECT *attr_obj;
1081 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1082 attr_obj = X509_ATTRIBUTE_get0_object(attr);
1083 attr_nid = OBJ_obj2nid(attr_obj);
1084 BIO_printf(out, " ");
1085 if (attr_nid == NID_undef) {
1086 i2a_ASN1_OBJECT(out, attr_obj);
1087 BIO_printf(out, ": ");
1089 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1092 if (X509_ATTRIBUTE_count(attr)) {
1093 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
1095 av = X509_ATTRIBUTE_get0_type(attr, j);
1096 print_attribute(out, av);
1099 BIO_printf(out, "<No Values>\n");
1105 void hex_prin(BIO *out, unsigned char *buf, int len)
1108 for (i = 0; i < len; i++)
1109 BIO_printf(out, "%02X ", buf[i]);
1112 static int set_pbe(int *ppbe, const char *str)
1116 if (strcmp(str, "NONE") == 0) {
1120 *ppbe = OBJ_txt2nid(str);
1121 if (*ppbe == NID_undef) {
1122 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);