1 /* $OpenBSD: pkcs12.c,v 1.14 2019/07/26 12:35:59 inoguchi Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/opensslconf.h>
61 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
69 #include <openssl/crypto.h>
70 #include <openssl/err.h>
71 #include <openssl/pem.h>
72 #include <openssl/pkcs12.h>
80 int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain);
81 int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
82 int options, char *pempass);
83 int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags, char *pass,
84 int passlen, int options, char *pempass);
85 int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
86 int passlen, int options, char *pempass);
87 int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
89 void hex_prin(BIO *out, unsigned char *buf, int len);
90 int alg_print(BIO *x, X509_ALGOR *alg);
91 int cert_load(BIO *in, STACK_OF(X509) *sk);
92 static int set_pbe(BIO *err, int *ppbe, const char *str);
97 STACK_OF(OPENSSL_STRING) *canames;
103 const EVP_CIPHER *enc;
124 pkcs12_opt_canames(char *arg)
126 if (pkcs12_config.canames == NULL &&
127 (pkcs12_config.canames = sk_OPENSSL_STRING_new_null()) == NULL)
130 if (!sk_OPENSSL_STRING_push(pkcs12_config.canames, arg))
137 pkcs12_opt_cert_pbe(char *arg)
139 return (!set_pbe(bio_err, &pkcs12_config.cert_pbe, arg));
143 pkcs12_opt_key_pbe(char *arg)
145 return (!set_pbe(bio_err, &pkcs12_config.key_pbe, arg));
149 pkcs12_opt_passarg(char *arg)
151 pkcs12_config.passarg = arg;
152 pkcs12_config.noprompt = 1;
156 static const EVP_CIPHER *get_cipher_by_name(char *name)
158 if (name == NULL || strcmp(name, "") == 0)
160 #ifndef OPENSSL_NO_AES
161 else if (strcmp(name, "aes128") == 0)
162 return EVP_aes_128_cbc();
163 else if (strcmp(name, "aes192") == 0)
164 return EVP_aes_192_cbc();
165 else if (strcmp(name, "aes256") == 0)
166 return EVP_aes_256_cbc();
168 #ifndef OPENSSL_NO_CAMELLIA
169 else if (strcmp(name, "camellia128") == 0)
170 return EVP_camellia_128_cbc();
171 else if (strcmp(name, "camellia192") == 0)
172 return EVP_camellia_192_cbc();
173 else if (strcmp(name, "camellia256") == 0)
174 return EVP_camellia_256_cbc();
176 #ifndef OPENSSL_NO_DES
177 else if (strcmp(name, "des") == 0)
178 return EVP_des_cbc();
179 else if (strcmp(name, "des3") == 0)
180 return EVP_des_ede3_cbc();
182 #ifndef OPENSSL_NO_IDEA
183 else if (strcmp(name, "idea") == 0)
184 return EVP_idea_cbc();
191 pkcs12_opt_enc(int argc, char **argv, int *argsused)
193 char *name = argv[0];
198 if (strcmp(name, "nodes") == 0)
199 pkcs12_config.enc = NULL;
200 else if ((pkcs12_config.enc = get_cipher_by_name(name)) == NULL)
207 static const struct option pkcs12_options[] = {
208 #ifndef OPENSSL_NO_AES
211 .desc = "Encrypt PEM output with CBC AES",
212 .type = OPTION_ARGV_FUNC,
213 .opt.argvfunc = pkcs12_opt_enc,
217 .desc = "Encrypt PEM output with CBC AES",
218 .type = OPTION_ARGV_FUNC,
219 .opt.argvfunc = pkcs12_opt_enc,
223 .desc = "Encrypt PEM output with CBC AES",
224 .type = OPTION_ARGV_FUNC,
225 .opt.argvfunc = pkcs12_opt_enc,
228 #ifndef OPENSSL_NO_CAMELLIA
230 .name = "camellia128",
231 .desc = "Encrypt PEM output with CBC Camellia",
232 .type = OPTION_ARGV_FUNC,
233 .opt.argvfunc = pkcs12_opt_enc,
236 .name = "camellia192",
237 .desc = "Encrypt PEM output with CBC Camellia",
238 .type = OPTION_ARGV_FUNC,
239 .opt.argvfunc = pkcs12_opt_enc,
242 .name = "camellia256",
243 .desc = "Encrypt PEM output with CBC Camellia",
244 .type = OPTION_ARGV_FUNC,
245 .opt.argvfunc = pkcs12_opt_enc,
250 .desc = "Encrypt private keys with DES",
251 .type = OPTION_ARGV_FUNC,
252 .opt.argvfunc = pkcs12_opt_enc,
256 .desc = "Encrypt private keys with triple DES (default)",
257 .type = OPTION_ARGV_FUNC,
258 .opt.argvfunc = pkcs12_opt_enc,
260 #ifndef OPENSSL_NO_IDEA
263 .desc = "Encrypt private keys with IDEA",
264 .type = OPTION_ARGV_FUNC,
265 .opt.argvfunc = pkcs12_opt_enc,
270 .desc = "Only output CA certificates",
271 .type = OPTION_VALUE_OR,
272 .opt.value = &pkcs12_config.options,
278 .desc = "PEM format file of CA certificates",
280 .opt.arg = &pkcs12_config.CAfile,
285 .desc = "Use name as CA friendly name (can be used more than once)",
286 .type = OPTION_ARG_FUNC,
287 .opt.argfunc = pkcs12_opt_canames,
291 .argname = "directory",
292 .desc = "PEM format directory of CA certificates",
294 .opt.arg = &pkcs12_config.CApath,
299 .desc = "Add all certs in file",
301 .opt.arg = &pkcs12_config.certfile,
306 .desc = "Specify certificate PBE algorithm (default RC2-40)",
307 .type = OPTION_ARG_FUNC,
308 .opt.argfunc = pkcs12_opt_cert_pbe,
312 .desc = "Add certificate chain",
314 .opt.flag = &pkcs12_config.chain,
318 .desc = "Only output client certificates",
319 .type = OPTION_VALUE_OR,
320 .opt.value = &pkcs12_config.options,
326 .desc = "Microsoft CSP name",
328 .opt.arg = &pkcs12_config.csp_name,
332 .desc = "Encrypt PKCS#12 certificates with triple DES (default RC2-40)",
333 .type = OPTION_VALUE,
334 .opt.value = &pkcs12_config.cert_pbe,
335 .value = NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
339 .desc = "Output PKCS#12 file",
341 .opt.flag = &pkcs12_config.export_cert,
346 .desc = "Input filename",
348 .opt.arg = &pkcs12_config.infile,
352 .desc = "Give info about PKCS#12 structure",
353 .type = OPTION_VALUE_OR,
354 .opt.value = &pkcs12_config.options,
360 .desc = "Private key if not infile",
362 .opt.arg = &pkcs12_config.keyname,
366 .desc = "Set MS key exchange type",
367 .type = OPTION_VALUE,
368 .opt.value = &pkcs12_config.keytype,
374 .desc = "Specify private key PBE algorithm (default 3DES)",
375 .type = OPTION_ARG_FUNC,
376 .opt.argfunc = pkcs12_opt_key_pbe,
380 .desc = "Set MS key signature type",
381 .type = OPTION_VALUE,
382 .opt.value = &pkcs12_config.keytype,
387 .desc = "Add local machine keyset attribute to private key",
389 .opt.flag = &pkcs12_config.add_lmk,
394 .desc = "Digest algorithm used in MAC (default SHA1)",
396 .opt.arg = &pkcs12_config.macalg,
400 .desc = "Use MAC iteration",
401 .type = OPTION_VALUE,
402 .opt.value = &pkcs12_config.maciter,
403 .value = PKCS12_DEFAULT_ITER,
408 .desc = "Use name as friendly name",
410 .opt.arg = &pkcs12_config.name,
414 .desc = "Don't output certificates",
415 .type = OPTION_VALUE_OR,
416 .opt.value = &pkcs12_config.options,
421 .desc = "Don't encrypt private keys",
422 .type = OPTION_ARGV_FUNC,
423 .opt.argvfunc = pkcs12_opt_enc,
427 .desc = "Don't use encryption iteration",
428 .type = OPTION_VALUE,
429 .opt.value = &pkcs12_config.iter,
434 .desc = "Don't output private keys",
435 .type = OPTION_VALUE_OR,
436 .opt.value = &pkcs12_config.options,
441 .desc = "Don't generate MAC",
442 .type = OPTION_VALUE,
443 .opt.value = &pkcs12_config.maciter,
448 .desc = "Don't use MAC iteration",
449 .type = OPTION_VALUE,
450 .opt.value = &pkcs12_config.maciter,
455 .desc = "Don't verify MAC",
456 .type = OPTION_VALUE,
457 .opt.value = &pkcs12_config.macver,
462 .desc = "Don't output anything, just verify",
463 .type = OPTION_VALUE_OR,
464 .opt.value = &pkcs12_config.options,
465 .value = (NOKEYS | NOCERTS),
470 .desc = "Output filename",
472 .opt.arg = &pkcs12_config.outfile,
477 .desc = "Input file passphrase source",
479 .opt.arg = &pkcs12_config.passargin,
484 .desc = "Output file passphrase source",
486 .opt.arg = &pkcs12_config.passargout,
491 .desc = "Set import/export password source",
492 .type = OPTION_ARG_FUNC,
493 .opt.argfunc = pkcs12_opt_passarg,
497 .desc = "Separate MAC, encryption passwords",
499 .opt.flag = &pkcs12_config.twopass,
507 fprintf(stderr, "usage: pkcs12 [-aes128 | -aes192 | -aes256 |");
508 fprintf(stderr, " -camellia128 |\n");
509 fprintf(stderr, " -camellia192 | -camellia256 | -des | -des3 |");
510 fprintf(stderr, " -idea]\n");
511 fprintf(stderr, " [-cacerts] [-CAfile file] [-caname name]\n");
512 fprintf(stderr, " [-CApath directory] [-certfile file]");
513 fprintf(stderr, " [-certpbe alg]\n");
514 fprintf(stderr, " [-chain] [-clcerts] [-CSP name] [-descert]");
515 fprintf(stderr, " [-export]\n");
516 fprintf(stderr, " [-in file] [-info] [-inkey file] [-keyex]");
517 fprintf(stderr, " [-keypbe alg]\n");
518 fprintf(stderr, " [-keysig] [-LMK] [-macalg alg] [-maciter]");
519 fprintf(stderr, " [-name name]\n");
520 fprintf(stderr, " [-nocerts] [-nodes] [-noiter] [-nokeys]");
521 fprintf(stderr, " [-nomac]\n");
522 fprintf(stderr, " [-nomaciter] [-nomacver] [-noout] [-out file]\n");
523 fprintf(stderr, " [-passin arg] [-passout arg] [-password arg]");
524 fprintf(stderr, " [-twopass]\n\n");
525 options_usage(pkcs12_options);
526 fprintf(stderr, "\n");
530 pkcs12_main(int argc, char **argv)
532 BIO *in = NULL, *out = NULL;
534 char pass[50], macpass[50];
536 char *cpass = NULL, *mpass = NULL;
537 char *passin = NULL, *passout = NULL;
539 if (single_execution) {
540 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
546 memset(&pkcs12_config, 0, sizeof(pkcs12_config));
547 pkcs12_config.cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
548 pkcs12_config.enc = EVP_des_ede3_cbc();
549 pkcs12_config.iter = PKCS12_DEFAULT_ITER;
550 pkcs12_config.key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
551 pkcs12_config.maciter = PKCS12_DEFAULT_ITER;
552 pkcs12_config.macver = 1;
554 if (options_parse(argc, argv, pkcs12_options, NULL, NULL) != 0) {
559 if (pkcs12_config.passarg) {
560 if (pkcs12_config.export_cert)
561 pkcs12_config.passargout = pkcs12_config.passarg;
563 pkcs12_config.passargin = pkcs12_config.passarg;
565 if (!app_passwd(bio_err, pkcs12_config.passargin,
566 pkcs12_config.passargout, &passin, &passout)) {
567 BIO_printf(bio_err, "Error getting passwords\n");
571 if (pkcs12_config.export_cert)
578 pkcs12_config.noprompt = 1;
584 if (!pkcs12_config.infile)
585 in = BIO_new_fp(stdin, BIO_NOCLOSE);
587 in = BIO_new_file(pkcs12_config.infile, "rb");
589 BIO_printf(bio_err, "Error opening input file %s\n",
590 pkcs12_config.infile ? pkcs12_config.infile : "<stdin>");
591 perror(pkcs12_config.infile);
595 if (!pkcs12_config.outfile) {
596 out = BIO_new_fp(stdout, BIO_NOCLOSE);
598 out = BIO_new_file(pkcs12_config.outfile, "wb");
600 BIO_printf(bio_err, "Error opening output file %s\n",
601 pkcs12_config.outfile ? pkcs12_config.outfile : "<stdout>");
602 perror(pkcs12_config.outfile);
605 if (pkcs12_config.twopass) {
606 if (EVP_read_pw_string(macpass, sizeof macpass,
607 "Enter MAC Password:", pkcs12_config.export_cert)) {
608 BIO_printf(bio_err, "Can't read Password\n");
612 if (pkcs12_config.export_cert) {
613 EVP_PKEY *key = NULL;
614 X509 *ucert = NULL, *x = NULL;
615 STACK_OF(X509) *certs = NULL;
616 const EVP_MD *macmd = NULL;
617 unsigned char *catmp = NULL;
620 if ((pkcs12_config.options & (NOCERTS | NOKEYS)) ==
621 (NOCERTS | NOKEYS)) {
622 BIO_printf(bio_err, "Nothing to do!\n");
625 if (pkcs12_config.options & NOCERTS)
626 pkcs12_config.chain = 0;
628 if (!(pkcs12_config.options & NOKEYS)) {
629 key = load_key(bio_err, pkcs12_config.keyname ?
630 pkcs12_config.keyname : pkcs12_config.infile,
631 FORMAT_PEM, 1, passin, "private key");
636 /* Load in all certs in input file */
637 if (!(pkcs12_config.options & NOCERTS)) {
638 certs = load_certs(bio_err, pkcs12_config.infile,
639 FORMAT_PEM, NULL, "certificates");
644 /* Look for matching private key */
645 for (i = 0; i < sk_X509_num(certs); i++) {
646 x = sk_X509_value(certs, i);
647 if (X509_check_private_key(x, key)) {
649 /* Zero keyid and alias */
650 X509_keyid_set1(ucert, NULL, 0);
651 X509_alias_set1(ucert, NULL, 0);
652 /* Remove from list */
653 (void) sk_X509_delete(certs, i);
659 "No certificate matches private key\n");
665 /* Add any more certificates asked for */
666 if (pkcs12_config.certfile) {
667 STACK_OF(X509) *morecerts = NULL;
668 if (!(morecerts = load_certs(bio_err,
669 pkcs12_config.certfile, FORMAT_PEM, NULL,
670 "certificates from certfile")))
672 while (sk_X509_num(morecerts) > 0)
673 sk_X509_push(certs, sk_X509_shift(morecerts));
674 sk_X509_free(morecerts);
678 /* If chaining get chain from user cert */
679 if (pkcs12_config.chain) {
681 STACK_OF(X509) *chain2;
682 X509_STORE *store = X509_STORE_new();
685 "Memory allocation error\n");
688 if (!X509_STORE_load_locations(store,
689 pkcs12_config.CAfile, pkcs12_config.CApath))
690 X509_STORE_set_default_paths(store);
692 vret = get_cert_chain(ucert, store, &chain2);
693 X509_STORE_free(store);
696 /* Exclude verified certificate */
697 for (i = 1; i < sk_X509_num(chain2); i++)
698 sk_X509_push(certs, sk_X509_value(
700 /* Free first certificate */
701 X509_free(sk_X509_value(chain2, 0));
702 sk_X509_free(chain2);
706 "Error %s getting chain.\n",
707 X509_verify_cert_error_string(
710 ERR_print_errors(bio_err);
714 /* Add any CA names */
716 for (i = 0; i < sk_OPENSSL_STRING_num(pkcs12_config.canames);
718 catmp = (unsigned char *) sk_OPENSSL_STRING_value(
719 pkcs12_config.canames, i);
720 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
723 if (pkcs12_config.csp_name && key)
724 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
726 (unsigned char *) pkcs12_config.csp_name, -1);
728 if (pkcs12_config.add_lmk && key)
729 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL,
732 if (!pkcs12_config.noprompt &&
733 EVP_read_pw_string(pass, sizeof pass,
734 "Enter Export Password:", 1)) {
735 BIO_printf(bio_err, "Can't read Password\n");
738 if (!pkcs12_config.twopass)
739 strlcpy(macpass, pass, sizeof macpass);
742 p12 = PKCS12_create(cpass, pkcs12_config.name, key, ucert,
743 certs, pkcs12_config.key_pbe, pkcs12_config.cert_pbe,
744 pkcs12_config.iter, -1, pkcs12_config.keytype);
747 ERR_print_errors(bio_err);
750 if (pkcs12_config.macalg) {
751 macmd = EVP_get_digestbyname(pkcs12_config.macalg);
754 "Unknown digest algorithm %s\n",
755 pkcs12_config.macalg);
758 if (pkcs12_config.maciter != -1)
759 PKCS12_set_mac(p12, mpass, -1, NULL, 0,
760 pkcs12_config.maciter, macmd);
762 i2d_PKCS12_bio(out, p12);
768 sk_X509_pop_free(certs, X509_free);
774 if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
775 ERR_print_errors(bio_err);
778 if (!pkcs12_config.noprompt && EVP_read_pw_string(pass, sizeof pass,
779 "Enter Import Password:", 0)) {
780 BIO_printf(bio_err, "Can't read Password\n");
784 if (!pkcs12_config.twopass)
785 strlcpy(macpass, pass, sizeof macpass);
787 if ((pkcs12_config.options & INFO) && p12->mac)
788 BIO_printf(bio_err, "MAC Iteration %ld\n",
789 p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
790 if (pkcs12_config.macver) {
791 /* If we enter empty password try no password first */
792 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
793 /* If mac and crypto pass the same set it to NULL too */
794 if (!pkcs12_config.twopass)
796 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
798 "Mac verify error: invalid password?\n");
799 ERR_print_errors(bio_err);
802 BIO_printf(bio_err, "MAC verified OK\n");
804 if (!dump_certs_keys_p12(out, p12, cpass, -1, pkcs12_config.options,
806 BIO_printf(bio_err, "Error outputting keys and certificates\n");
807 ERR_print_errors(bio_err);
815 sk_OPENSSL_STRING_free(pkcs12_config.canames);
823 dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
824 int passlen, int options, char *pempass)
826 STACK_OF(PKCS7) *asafes = NULL;
827 STACK_OF(PKCS12_SAFEBAG) *bags;
832 if (!(asafes = PKCS12_unpack_authsafes(p12)))
834 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
835 p7 = sk_PKCS7_value(asafes, i);
836 bagnid = OBJ_obj2nid(p7->type);
837 if (bagnid == NID_pkcs7_data) {
838 bags = PKCS12_unpack_p7data(p7);
840 BIO_printf(bio_err, "PKCS7 Data\n");
841 } else if (bagnid == NID_pkcs7_encrypted) {
842 if (options & INFO) {
843 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
845 p7->d.encrypted->enc_data->algorithm);
847 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
852 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
854 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
857 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
863 sk_PKCS7_pop_free(asafes, PKCS7_free);
868 dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
869 char *pass, int passlen, int options, char *pempass)
872 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
873 if (!dump_certs_pkeys_bag(out,
874 sk_PKCS12_SAFEBAG_value(bags, i),
883 dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
884 int passlen, int options, char *pempass)
887 PKCS8_PRIV_KEY_INFO *p8;
890 switch (OBJ_obj2nid(bag->type)) {
893 BIO_printf(bio_err, "Key bag\n");
894 if (options & NOKEYS)
896 print_attribs(out, bag->attrib, "Bag Attributes");
897 p8 = bag->value.keybag;
898 if (!(pkey = EVP_PKCS82PKEY(p8)))
900 print_attribs(out, p8->attributes, "Key Attributes");
901 PEM_write_bio_PrivateKey(out, pkey, pkcs12_config.enc, NULL, 0,
906 case NID_pkcs8ShroudedKeyBag:
907 if (options & INFO) {
908 BIO_printf(bio_err, "Shrouded Keybag: ");
909 alg_print(bio_err, bag->value.shkeybag->algor);
911 if (options & NOKEYS)
913 print_attribs(out, bag->attrib, "Bag Attributes");
914 if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
916 if (!(pkey = EVP_PKCS82PKEY(p8))) {
917 PKCS8_PRIV_KEY_INFO_free(p8);
920 print_attribs(out, p8->attributes, "Key Attributes");
921 PKCS8_PRIV_KEY_INFO_free(p8);
922 PEM_write_bio_PrivateKey(out, pkey, pkcs12_config.enc, NULL, 0,
929 BIO_printf(bio_err, "Certificate bag\n");
930 if (options & NOCERTS)
932 if (PKCS12_get_attr(bag, NID_localKeyID)) {
933 if (options & CACERTS)
935 } else if (options & CLCERTS)
937 print_attribs(out, bag->attrib, "Bag Attributes");
938 if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
940 if (!(x509 = PKCS12_certbag2x509(bag)))
942 dump_cert_text(out, x509);
943 PEM_write_bio_X509(out, x509);
947 case NID_safeContentsBag:
949 BIO_printf(bio_err, "Safe Contents bag\n");
950 print_attribs(out, bag->attrib, "Bag Attributes");
951 return dump_certs_pkeys_bags(out, bag->value.safes, pass,
952 passlen, options, pempass);
955 BIO_printf(bio_err, "Warning unsupported bag type: ");
956 i2a_ASN1_OBJECT(bio_err, bag->type);
957 BIO_printf(bio_err, "\n");
964 /* Given a single certificate return a verified chain or NULL if error */
966 /* Hope this is OK .... */
969 get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain)
971 X509_STORE_CTX store_ctx;
976 * FIXME: Should really check the return status of
977 * X509_STORE_CTX_init for an error, but how that fits into the
978 * return value of this function is less obvious.
980 X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
981 if (X509_verify_cert(&store_ctx) <= 0) {
982 i = X509_STORE_CTX_get_error(&store_ctx);
985 * avoid returning 0 if X509_verify_cert() did not
986 * set an appropriate error value in the context
992 chn = X509_STORE_CTX_get1_chain(&store_ctx);
994 X509_STORE_CTX_cleanup(&store_ctx);
1001 alg_print(BIO *x, X509_ALGOR *alg)
1004 const unsigned char *p;
1005 p = alg->parameter->value.sequence->data;
1006 pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
1009 BIO_printf(bio_err, "%s, Iteration %ld\n",
1010 OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
1011 ASN1_INTEGER_get(pbe->iter));
1016 /* Load all certificates from a given file */
1019 cert_load(BIO *in, STACK_OF(X509) *sk)
1024 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1026 sk_X509_push(sk, cert);
1033 /* Generalised attribute print: handle PKCS#8 and bag attributes */
1036 print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst, const char *name)
1038 X509_ATTRIBUTE *attr;
1043 BIO_printf(out, "%s: <No Attributes>\n", name);
1046 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1047 BIO_printf(out, "%s: <Empty Attributes>\n", name);
1050 BIO_printf(out, "%s\n", name);
1051 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1052 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1053 attr_nid = OBJ_obj2nid(attr->object);
1054 BIO_printf(out, " ");
1055 if (attr_nid == NID_undef) {
1056 i2a_ASN1_OBJECT(out, attr->object);
1057 BIO_printf(out, ": ");
1059 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1061 if (sk_ASN1_TYPE_num(attr->value.set)) {
1062 av = sk_ASN1_TYPE_value(attr->value.set, 0);
1064 case V_ASN1_BMPSTRING:
1065 value = OPENSSL_uni2asc(
1066 av->value.bmpstring->data,
1067 av->value.bmpstring->length);
1068 BIO_printf(out, "%s\n", value);
1072 case V_ASN1_OCTET_STRING:
1073 hex_prin(out, av->value.octet_string->data,
1074 av->value.octet_string->length);
1075 BIO_printf(out, "\n");
1078 case V_ASN1_BIT_STRING:
1079 hex_prin(out, av->value.bit_string->data,
1080 av->value.bit_string->length);
1081 BIO_printf(out, "\n");
1085 BIO_printf(out, "<Unsupported tag %d>\n",
1090 BIO_printf(out, "<No Values>\n");
1096 hex_prin(BIO *out, unsigned char *buf, int len)
1099 for (i = 0; i < len; i++)
1100 BIO_printf(out, "%02X ", buf[i]);
1104 set_pbe(BIO *err, int *ppbe, const char *str)
1108 if (!strcmp(str, "NONE")) {
1112 *ppbe = OBJ_txt2nid(str);
1113 if (*ppbe == NID_undef) {
1114 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);