3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/opensslconf.h>
61 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
67 # include <openssl/crypto.h>
68 # include <openssl/err.h>
69 # include <openssl/pem.h>
70 # include <openssl/pkcs12.h>
72 # ifdef OPENSSL_SYS_NETWARE
73 /* Rename these functions to avoid name clashes on NetWare OS */
74 # define uni2asc OPENSSL_uni2asc
75 # define asc2uni OPENSSL_asc2uni
78 # define PROG pkcs12_main
80 const EVP_CIPHER
*enc
;
88 int get_cert_chain(X509
*cert
, X509_STORE
*store
, STACK_OF(X509
) **chain
);
89 int dump_certs_keys_p12(BIO
*out
, PKCS12
*p12
, char *pass
, int passlen
,
90 int options
, char *pempass
);
91 int dump_certs_pkeys_bags(BIO
*out
, STACK_OF(PKCS12_SAFEBAG
) *bags
,
92 char *pass
, int passlen
, int options
,
94 int dump_certs_pkeys_bag(BIO
*out
, PKCS12_SAFEBAG
*bags
, char *pass
,
95 int passlen
, int options
, char *pempass
);
96 int print_attribs(BIO
*out
, STACK_OF(X509_ATTRIBUTE
) *attrlst
,
98 void hex_prin(BIO
*out
, unsigned char *buf
, int len
);
99 int alg_print(BIO
*x
, X509_ALGOR
*alg
);
100 int cert_load(BIO
*in
, STACK_OF(X509
) *sk
);
102 int MAIN(int, char **);
104 int MAIN(int argc
, char **argv
)
107 char *infile
= NULL
, *outfile
= NULL
, *keyname
= NULL
;
108 char *certfile
= NULL
;
109 BIO
*in
= NULL
, *out
= NULL
;
112 char *csp_name
= NULL
;
115 char pass
[50], macpass
[50];
120 int iter
= PKCS12_DEFAULT_ITER
;
121 int maciter
= PKCS12_DEFAULT_ITER
;
125 int key_pbe
= NID_pbe_WithSHA1And3_Key_TripleDES_CBC
;
129 STACK
*canames
= NULL
;
130 char *cpass
= NULL
, *mpass
= NULL
;
131 char *passargin
= NULL
, *passargout
= NULL
, *passarg
= NULL
;
132 char *passin
= NULL
, *passout
= NULL
;
134 char *CApath
= NULL
, *CAfile
= NULL
;
135 # ifndef OPENSSL_NO_ENGINE
143 cert_pbe
= NID_pbe_WithSHA1And3_Key_TripleDES_CBC
;
146 cert_pbe
= NID_pbe_WithSHA1And40BitRC2_CBC
;
148 enc
= EVP_des_ede3_cbc();
150 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
152 if (!load_config(bio_err
, NULL
))
158 if (*args
[0] == '-') {
159 if (!strcmp(*args
, "-nokeys"))
161 else if (!strcmp(*args
, "-keyex"))
163 else if (!strcmp(*args
, "-keysig"))
165 else if (!strcmp(*args
, "-nocerts"))
167 else if (!strcmp(*args
, "-clcerts"))
169 else if (!strcmp(*args
, "-cacerts"))
171 else if (!strcmp(*args
, "-noout"))
172 options
|= (NOKEYS
| NOCERTS
);
173 else if (!strcmp(*args
, "-info"))
175 else if (!strcmp(*args
, "-chain"))
177 else if (!strcmp(*args
, "-twopass"))
179 else if (!strcmp(*args
, "-nomacver"))
181 else if (!strcmp(*args
, "-descert"))
182 cert_pbe
= NID_pbe_WithSHA1And3_Key_TripleDES_CBC
;
183 else if (!strcmp(*args
, "-export"))
185 else if (!strcmp(*args
, "-des"))
187 else if (!strcmp(*args
, "-des3"))
188 enc
= EVP_des_ede3_cbc();
189 # ifndef OPENSSL_NO_IDEA
190 else if (!strcmp(*args
, "-idea"))
191 enc
= EVP_idea_cbc();
193 # ifndef OPENSSL_NO_SEED
194 else if (!strcmp(*args
, "-seed"))
195 enc
= EVP_seed_cbc();
197 # ifndef OPENSSL_NO_AES
198 else if (!strcmp(*args
, "-aes128"))
199 enc
= EVP_aes_128_cbc();
200 else if (!strcmp(*args
, "-aes192"))
201 enc
= EVP_aes_192_cbc();
202 else if (!strcmp(*args
, "-aes256"))
203 enc
= EVP_aes_256_cbc();
205 # ifndef OPENSSL_NO_CAMELLIA
206 else if (!strcmp(*args
, "-camellia128"))
207 enc
= EVP_camellia_128_cbc();
208 else if (!strcmp(*args
, "-camellia192"))
209 enc
= EVP_camellia_192_cbc();
210 else if (!strcmp(*args
, "-camellia256"))
211 enc
= EVP_camellia_256_cbc();
213 else if (!strcmp(*args
, "-noiter"))
215 else if (!strcmp(*args
, "-maciter"))
216 maciter
= PKCS12_DEFAULT_ITER
;
217 else if (!strcmp(*args
, "-nomaciter"))
219 else if (!strcmp(*args
, "-nomac"))
221 else if (!strcmp(*args
, "-nodes"))
223 else if (!strcmp(*args
, "-certpbe")) {
226 if (!strcmp(*args
, "NONE"))
229 cert_pbe
= OBJ_txt2nid(*args
);
230 if (cert_pbe
== NID_undef
) {
232 "Unknown PBE algorithm %s\n", *args
);
237 } else if (!strcmp(*args
, "-keypbe")) {
240 if (!strcmp(*args
, "NONE"))
243 key_pbe
= OBJ_txt2nid(*args
);
244 if (key_pbe
== NID_undef
) {
246 "Unknown PBE algorithm %s\n", *args
);
251 } else if (!strcmp(*args
, "-rand")) {
257 } else if (!strcmp(*args
, "-inkey")) {
263 } else if (!strcmp(*args
, "-certfile")) {
269 } else if (!strcmp(*args
, "-name")) {
275 } else if (!strcmp(*args
, "-LMK"))
277 else if (!strcmp(*args
, "-CSP")) {
283 } else if (!strcmp(*args
, "-caname")) {
287 canames
= sk_new_null();
288 sk_push(canames
, *args
);
291 } else if (!strcmp(*args
, "-in")) {
297 } else if (!strcmp(*args
, "-out")) {
303 } else if (!strcmp(*args
, "-passin")) {
309 } else if (!strcmp(*args
, "-passout")) {
315 } else if (!strcmp(*args
, "-password")) {
322 } else if (!strcmp(*args
, "-CApath")) {
328 } else if (!strcmp(*args
, "-CAfile")) {
334 # ifndef OPENSSL_NO_ENGINE
335 } else if (!strcmp(*args
, "-engine")) {
351 BIO_printf(bio_err
, "Usage: pkcs12 [options]\n");
352 BIO_printf(bio_err
, "where options are\n");
353 BIO_printf(bio_err
, "-export output PKCS12 file\n");
354 BIO_printf(bio_err
, "-chain add certificate chain\n");
355 BIO_printf(bio_err
, "-inkey file private key if not infile\n");
356 BIO_printf(bio_err
, "-certfile f add all certs in f\n");
357 BIO_printf(bio_err
, "-CApath arg - PEM format directory of CA's\n");
358 BIO_printf(bio_err
, "-CAfile arg - PEM format file of CA's\n");
359 BIO_printf(bio_err
, "-name \"name\" use name as friendly name\n");
361 "-caname \"nm\" use nm as CA friendly name (can be used more than once).\n");
362 BIO_printf(bio_err
, "-in infile input filename\n");
363 BIO_printf(bio_err
, "-out outfile output filename\n");
365 "-noout don't output anything, just verify.\n");
366 BIO_printf(bio_err
, "-nomacver don't verify MAC.\n");
367 BIO_printf(bio_err
, "-nocerts don't output certificates.\n");
369 "-clcerts only output client certificates.\n");
370 BIO_printf(bio_err
, "-cacerts only output CA certificates.\n");
371 BIO_printf(bio_err
, "-nokeys don't output private keys.\n");
373 "-info give info about PKCS#12 structure.\n");
374 BIO_printf(bio_err
, "-des encrypt private keys with DES\n");
376 "-des3 encrypt private keys with triple DES (default)\n");
377 # ifndef OPENSSL_NO_IDEA
378 BIO_printf(bio_err
, "-idea encrypt private keys with idea\n");
380 # ifndef OPENSSL_NO_SEED
381 BIO_printf(bio_err
, "-seed encrypt private keys with seed\n");
383 # ifndef OPENSSL_NO_AES
384 BIO_printf(bio_err
, "-aes128, -aes192, -aes256\n");
386 " encrypt PEM output with cbc aes\n");
388 # ifndef OPENSSL_NO_CAMELLIA
389 BIO_printf(bio_err
, "-camellia128, -camellia192, -camellia256\n");
391 " encrypt PEM output with cbc camellia\n");
393 BIO_printf(bio_err
, "-nodes don't encrypt private keys\n");
394 BIO_printf(bio_err
, "-noiter don't use encryption iteration\n");
395 BIO_printf(bio_err
, "-maciter use MAC iteration\n");
397 "-twopass separate MAC, encryption passwords\n");
399 "-descert encrypt PKCS#12 certificates with triple DES (default RC2-40)\n");
401 "-certpbe alg specify certificate PBE algorithm (default RC2-40)\n");
403 "-keypbe alg specify private key PBE algorithm (default 3DES)\n");
404 BIO_printf(bio_err
, "-keyex set MS key exchange type\n");
405 BIO_printf(bio_err
, "-keysig set MS key signature type\n");
407 "-password p set import/export password source\n");
408 BIO_printf(bio_err
, "-passin p input file pass phrase source\n");
409 BIO_printf(bio_err
, "-passout p output file pass phrase source\n");
410 # ifndef OPENSSL_NO_ENGINE
412 "-engine e use engine e, possibly a hardware device.\n");
414 BIO_printf(bio_err
, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
,
415 LIST_SEPARATOR_CHAR
);
417 " load the file (or the files in the directory) into\n");
418 BIO_printf(bio_err
, " the random number generator\n");
419 BIO_printf(bio_err
, "-CSP name Microsoft CSP name\n");
421 "-LMK Add local machine keyset attribute to private key\n");
424 # ifndef OPENSSL_NO_ENGINE
425 e
= setup_engine(bio_err
, engine
, 0);
430 passargout
= passarg
;
435 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
436 BIO_printf(bio_err
, "Error getting passwords\n");
455 if (export_cert
|| inrand
) {
456 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
458 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
459 app_RAND_load_files(inrand
));
461 ERR_load_crypto_strings();
463 # ifdef CRYPTO_MDEBUG
464 CRYPTO_push_info("read files");
468 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
470 in
= BIO_new_file(infile
, "rb");
472 BIO_printf(bio_err
, "Error opening input file %s\n",
473 infile
? infile
: "<stdin>");
477 # ifdef CRYPTO_MDEBUG
479 CRYPTO_push_info("write files");
483 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
484 # ifdef OPENSSL_SYS_VMS
486 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
487 out
= BIO_push(tmpbio
, out
);
491 out
= BIO_new_file(outfile
, "wb");
493 BIO_printf(bio_err
, "Error opening output file %s\n",
494 outfile
? outfile
: "<stdout>");
499 # ifdef CRYPTO_MDEBUG
500 CRYPTO_push_info("read MAC password");
502 if (EVP_read_pw_string
503 (macpass
, sizeof macpass
, "Enter MAC Password:", export_cert
)) {
504 BIO_printf(bio_err
, "Can't read Password\n");
507 # ifdef CRYPTO_MDEBUG
513 EVP_PKEY
*key
= NULL
;
514 X509
*ucert
= NULL
, *x
= NULL
;
515 STACK_OF(X509
) *certs
= NULL
;
516 unsigned char *catmp
= NULL
;
519 if ((options
& (NOCERTS
| NOKEYS
)) == (NOCERTS
| NOKEYS
)) {
520 BIO_printf(bio_err
, "Nothing to do!\n");
524 if (options
& NOCERTS
)
527 # ifdef CRYPTO_MDEBUG
528 CRYPTO_push_info("process -export_cert");
529 CRYPTO_push_info("reading private key");
531 if (!(options
& NOKEYS
)) {
532 key
= load_key(bio_err
, keyname
? keyname
: infile
,
533 FORMAT_PEM
, 1, passin
, e
, "private key");
537 # ifdef CRYPTO_MDEBUG
539 CRYPTO_push_info("reading certs from input");
542 /* Load in all certs in input file */
543 if (!(options
& NOCERTS
)) {
544 certs
= load_certs(bio_err
, infile
, FORMAT_PEM
, NULL
, e
,
550 /* Look for matching private key */
551 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
552 x
= sk_X509_value(certs
, i
);
553 if (X509_check_private_key(x
, key
)) {
555 /* Zero keyid and alias */
556 X509_keyid_set1(ucert
, NULL
, 0);
557 X509_alias_set1(ucert
, NULL
, 0);
558 /* Remove from list */
559 (void)sk_X509_delete(certs
, i
);
565 "No certificate matches private key\n");
571 # ifdef CRYPTO_MDEBUG
573 CRYPTO_push_info("reading certs from input 2");
576 /* Add any more certificates asked for */
578 STACK_OF(X509
) *morecerts
= NULL
;
579 if (!(morecerts
= load_certs(bio_err
, certfile
, FORMAT_PEM
,
581 "certificates from certfile")))
583 while (sk_X509_num(morecerts
) > 0)
584 sk_X509_push(certs
, sk_X509_shift(morecerts
));
585 sk_X509_free(morecerts
);
587 # ifdef CRYPTO_MDEBUG
589 CRYPTO_push_info("reading certs from certfile");
592 # ifdef CRYPTO_MDEBUG
594 CRYPTO_push_info("building chain");
597 /* If chaining get chain from user cert */
600 STACK_OF(X509
) *chain2
;
601 X509_STORE
*store
= X509_STORE_new();
603 BIO_printf(bio_err
, "Memory allocation error\n");
606 if (!X509_STORE_load_locations(store
, CAfile
, CApath
))
607 X509_STORE_set_default_paths(store
);
609 vret
= get_cert_chain(ucert
, store
, &chain2
);
610 X509_STORE_free(store
);
613 /* Exclude verified certificate */
614 for (i
= 1; i
< sk_X509_num(chain2
); i
++)
615 sk_X509_push(certs
, sk_X509_value(chain2
, i
));
616 /* Free first certificate */
617 X509_free(sk_X509_value(chain2
, 0));
618 sk_X509_free(chain2
);
621 BIO_printf(bio_err
, "Error %s getting chain.\n",
622 X509_verify_cert_error_string(vret
));
624 ERR_print_errors(bio_err
);
629 /* Add any CA names */
631 for (i
= 0; i
< sk_num(canames
); i
++) {
632 catmp
= (unsigned char *)sk_value(canames
, i
);
633 X509_alias_set1(sk_X509_value(certs
, i
), catmp
, -1);
637 EVP_PKEY_add1_attr_by_NID(key
, NID_ms_csp_name
,
638 MBSTRING_ASC
, (unsigned char *)csp_name
,
642 EVP_PKEY_add1_attr_by_NID(key
, NID_LocalKeySet
, 0, NULL
, -1);
644 # ifdef CRYPTO_MDEBUG
646 CRYPTO_push_info("reading password");
650 EVP_read_pw_string(pass
, sizeof pass
, "Enter Export Password:",
652 BIO_printf(bio_err
, "Can't read Password\n");
656 BUF_strlcpy(macpass
, pass
, sizeof macpass
);
658 # ifdef CRYPTO_MDEBUG
660 CRYPTO_push_info("creating PKCS#12 structure");
663 p12
= PKCS12_create(cpass
, name
, key
, ucert
, certs
,
664 key_pbe
, cert_pbe
, iter
, -1, keytype
);
667 ERR_print_errors(bio_err
);
672 PKCS12_set_mac(p12
, mpass
, -1, NULL
, 0, maciter
, NULL
);
674 # ifdef CRYPTO_MDEBUG
676 CRYPTO_push_info("writing pkcs12");
679 i2d_PKCS12_bio(out
, p12
);
684 # ifdef CRYPTO_MDEBUG
687 CRYPTO_push_info("process -export_cert: freeing");
693 sk_X509_pop_free(certs
, X509_free
);
697 # ifdef CRYPTO_MDEBUG
704 if (!(p12
= d2i_PKCS12_bio(in
, NULL
))) {
705 ERR_print_errors(bio_err
);
708 # ifdef CRYPTO_MDEBUG
709 CRYPTO_push_info("read import password");
712 && EVP_read_pw_string(pass
, sizeof pass
, "Enter Import Password:",
714 BIO_printf(bio_err
, "Can't read Password\n");
717 # ifdef CRYPTO_MDEBUG
722 BUF_strlcpy(macpass
, pass
, sizeof macpass
);
724 if ((options
& INFO
) && p12
->mac
)
725 BIO_printf(bio_err
, "MAC Iteration %ld\n",
726 p12
->mac
->iter
? ASN1_INTEGER_get(p12
->mac
->iter
) : 1);
728 # ifdef CRYPTO_MDEBUG
729 CRYPTO_push_info("verify MAC");
731 /* If we enter empty password try no password first */
732 if (!mpass
[0] && PKCS12_verify_mac(p12
, NULL
, 0)) {
733 /* If mac and crypto pass the same set it to NULL too */
736 } else if (!PKCS12_verify_mac(p12
, mpass
, -1)) {
737 BIO_printf(bio_err
, "Mac verify error: invalid password?\n");
738 ERR_print_errors(bio_err
);
741 BIO_printf(bio_err
, "MAC verified OK\n");
742 # ifdef CRYPTO_MDEBUG
746 # ifdef CRYPTO_MDEBUG
747 CRYPTO_push_info("output keys and certificates");
749 if (!dump_certs_keys_p12(out
, p12
, cpass
, -1, options
, passout
)) {
750 BIO_printf(bio_err
, "Error outputting keys and certificates\n");
751 ERR_print_errors(bio_err
);
754 # ifdef CRYPTO_MDEBUG
761 if (export_cert
|| inrand
)
762 app_RAND_write_file(NULL
, bio_err
);
763 # ifdef CRYPTO_MDEBUG
764 CRYPTO_remove_all_info();
771 OPENSSL_free(passin
);
773 OPENSSL_free(passout
);
778 int dump_certs_keys_p12(BIO
*out
, PKCS12
*p12
, char *pass
,
779 int passlen
, int options
, char *pempass
)
781 STACK_OF(PKCS7
) *asafes
= NULL
;
782 STACK_OF(PKCS12_SAFEBAG
) *bags
;
787 if (!(asafes
= PKCS12_unpack_authsafes(p12
)))
789 for (i
= 0; i
< sk_PKCS7_num(asafes
); i
++) {
790 p7
= sk_PKCS7_value(asafes
, i
);
791 bagnid
= OBJ_obj2nid(p7
->type
);
792 if (bagnid
== NID_pkcs7_data
) {
793 bags
= PKCS12_unpack_p7data(p7
);
795 BIO_printf(bio_err
, "PKCS7 Data\n");
796 } else if (bagnid
== NID_pkcs7_encrypted
) {
797 if (options
& INFO
) {
798 BIO_printf(bio_err
, "PKCS7 Encrypted data: ");
799 alg_print(bio_err
, p7
->d
.encrypted
->enc_data
->algorithm
);
801 bags
= PKCS12_unpack_p7encdata(p7
, pass
, passlen
);
806 if (!dump_certs_pkeys_bags(out
, bags
, pass
, passlen
,
808 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
811 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
819 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
823 int dump_certs_pkeys_bags(BIO
*out
, STACK_OF(PKCS12_SAFEBAG
) *bags
,
824 char *pass
, int passlen
, int options
, char *pempass
)
827 for (i
= 0; i
< sk_PKCS12_SAFEBAG_num(bags
); i
++) {
828 if (!dump_certs_pkeys_bag(out
,
829 sk_PKCS12_SAFEBAG_value(bags
, i
),
830 pass
, passlen
, options
, pempass
))
836 int dump_certs_pkeys_bag(BIO
*out
, PKCS12_SAFEBAG
*bag
, char *pass
,
837 int passlen
, int options
, char *pempass
)
840 PKCS8_PRIV_KEY_INFO
*p8
;
843 switch (M_PKCS12_bag_type(bag
)) {
846 BIO_printf(bio_err
, "Key bag\n");
847 if (options
& NOKEYS
)
849 print_attribs(out
, bag
->attrib
, "Bag Attributes");
850 p8
= bag
->value
.keybag
;
851 if (!(pkey
= EVP_PKCS82PKEY(p8
)))
853 print_attribs(out
, p8
->attributes
, "Key Attributes");
854 PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, pempass
);
858 case NID_pkcs8ShroudedKeyBag
:
859 if (options
& INFO
) {
860 BIO_printf(bio_err
, "Shrouded Keybag: ");
861 alg_print(bio_err
, bag
->value
.shkeybag
->algor
);
863 if (options
& NOKEYS
)
865 print_attribs(out
, bag
->attrib
, "Bag Attributes");
866 if (!(p8
= PKCS12_decrypt_skey(bag
, pass
, passlen
)))
868 if (!(pkey
= EVP_PKCS82PKEY(p8
))) {
869 PKCS8_PRIV_KEY_INFO_free(p8
);
872 print_attribs(out
, p8
->attributes
, "Key Attributes");
873 PKCS8_PRIV_KEY_INFO_free(p8
);
874 PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, pempass
);
880 BIO_printf(bio_err
, "Certificate bag\n");
881 if (options
& NOCERTS
)
883 if (PKCS12_get_attr(bag
, NID_localKeyID
)) {
884 if (options
& CACERTS
)
886 } else if (options
& CLCERTS
)
888 print_attribs(out
, bag
->attrib
, "Bag Attributes");
889 if (M_PKCS12_cert_bag_type(bag
) != NID_x509Certificate
)
891 if (!(x509
= PKCS12_certbag2x509(bag
)))
893 dump_cert_text(out
, x509
);
894 PEM_write_bio_X509(out
, x509
);
898 case NID_safeContentsBag
:
900 BIO_printf(bio_err
, "Safe Contents bag\n");
901 print_attribs(out
, bag
->attrib
, "Bag Attributes");
902 return dump_certs_pkeys_bags(out
, bag
->value
.safes
, pass
,
903 passlen
, options
, pempass
);
906 BIO_printf(bio_err
, "Warning unsupported bag type: ");
907 i2a_ASN1_OBJECT(bio_err
, bag
->type
);
908 BIO_printf(bio_err
, "\n");
915 /* Given a single certificate return a verified chain or NULL if error */
917 /* Hope this is OK .... */
919 int get_cert_chain(X509
*cert
, X509_STORE
*store
, STACK_OF(X509
) **chain
)
921 X509_STORE_CTX store_ctx
;
926 * FIXME: Should really check the return status of X509_STORE_CTX_init
927 * for an error, but how that fits into the return value of this function
930 X509_STORE_CTX_init(&store_ctx
, store
, cert
, NULL
);
931 if (X509_verify_cert(&store_ctx
) <= 0) {
932 i
= X509_STORE_CTX_get_error(&store_ctx
);
935 * avoid returning 0 if X509_verify_cert() did not set an
936 * appropriate error value in the context
942 chn
= X509_STORE_CTX_get1_chain(&store_ctx
);
944 X509_STORE_CTX_cleanup(&store_ctx
);
950 int alg_print(BIO
*x
, X509_ALGOR
*alg
)
953 const unsigned char *p
;
954 p
= alg
->parameter
->value
.sequence
->data
;
955 pbe
= d2i_PBEPARAM(NULL
, &p
, alg
->parameter
->value
.sequence
->length
);
958 BIO_printf(bio_err
, "%s, Iteration %ld\n",
959 OBJ_nid2ln(OBJ_obj2nid(alg
->algorithm
)),
960 ASN1_INTEGER_get(pbe
->iter
));
965 /* Load all certificates from a given file */
967 int cert_load(BIO
*in
, STACK_OF(X509
) *sk
)
972 # ifdef CRYPTO_MDEBUG
973 CRYPTO_push_info("cert_load(): reading one cert");
975 while ((cert
= PEM_read_bio_X509(in
, NULL
, NULL
, NULL
))) {
976 # ifdef CRYPTO_MDEBUG
980 sk_X509_push(sk
, cert
);
981 # ifdef CRYPTO_MDEBUG
982 CRYPTO_push_info("cert_load(): reading one cert");
985 # ifdef CRYPTO_MDEBUG
993 /* Generalised attribute print: handle PKCS#8 and bag attributes */
995 int print_attribs(BIO
*out
, STACK_OF(X509_ATTRIBUTE
) *attrlst
,
998 X509_ATTRIBUTE
*attr
;
1003 BIO_printf(out
, "%s: <No Attributes>\n", name
);
1006 if (!sk_X509_ATTRIBUTE_num(attrlst
)) {
1007 BIO_printf(out
, "%s: <Empty Attributes>\n", name
);
1010 BIO_printf(out
, "%s\n", name
);
1011 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(attrlst
); i
++) {
1012 attr
= sk_X509_ATTRIBUTE_value(attrlst
, i
);
1013 attr_nid
= OBJ_obj2nid(attr
->object
);
1014 BIO_printf(out
, " ");
1015 if (attr_nid
== NID_undef
) {
1016 i2a_ASN1_OBJECT(out
, attr
->object
);
1017 BIO_printf(out
, ": ");
1019 BIO_printf(out
, "%s: ", OBJ_nid2ln(attr_nid
));
1021 if (sk_ASN1_TYPE_num(attr
->value
.set
)) {
1022 av
= sk_ASN1_TYPE_value(attr
->value
.set
, 0);
1024 case V_ASN1_BMPSTRING
:
1025 value
= uni2asc(av
->value
.bmpstring
->data
,
1026 av
->value
.bmpstring
->length
);
1027 BIO_printf(out
, "%s\n", value
);
1028 OPENSSL_free(value
);
1031 case V_ASN1_OCTET_STRING
:
1032 hex_prin(out
, av
->value
.octet_string
->data
,
1033 av
->value
.octet_string
->length
);
1034 BIO_printf(out
, "\n");
1037 case V_ASN1_BIT_STRING
:
1038 hex_prin(out
, av
->value
.bit_string
->data
,
1039 av
->value
.bit_string
->length
);
1040 BIO_printf(out
, "\n");
1044 BIO_printf(out
, "<Unsupported tag %d>\n", av
->type
);
1048 BIO_printf(out
, "<No Values>\n");
1053 void hex_prin(BIO
*out
, unsigned char *buf
, int len
)
1056 for (i
= 0; i
< len
; i
++)
1057 BIO_printf(out
, "%02X ", buf
[i
]);