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>
60 #if defined(OPENSSL_NO_DES)
61 NON_EMPTY_TRANSLATION_UNIT
68 # include <openssl/crypto.h>
69 # include <openssl/err.h>
70 # include <openssl/pem.h>
71 # include <openssl/pkcs12.h>
79 static int get_cert_chain(X509
*cert
, X509_STORE
*store
,
80 STACK_OF(X509
) **chain
);
81 int dump_certs_keys_p12(BIO
*out
, PKCS12
*p12
, char *pass
, int passlen
,
82 int options
, char *pempass
, const EVP_CIPHER
*enc
);
83 int dump_certs_pkeys_bags(BIO
*out
, STACK_OF(PKCS12_SAFEBAG
) *bags
,
84 char *pass
, int passlen
, int options
, char *pempass
,
85 const EVP_CIPHER
*enc
);
86 int dump_certs_pkeys_bag(BIO
*out
, PKCS12_SAFEBAG
*bags
, char *pass
,
87 int passlen
, int options
, char *pempass
,
88 const EVP_CIPHER
*enc
);
89 int print_attribs(BIO
*out
, STACK_OF(X509_ATTRIBUTE
) *attrlst
,
91 void hex_prin(BIO
*out
, unsigned char *buf
, int len
);
92 static int alg_print(X509_ALGOR
*alg
);
93 int cert_load(BIO
*in
, STACK_OF(X509
) *sk
);
94 static int set_pbe(int *ppbe
, const char *str
);
96 typedef enum OPTION_choice
{
97 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
98 OPT_CIPHER
, OPT_NOKEYS
, OPT_KEYEX
, OPT_KEYSIG
, OPT_NOCERTS
, OPT_CLCERTS
,
99 OPT_CACERTS
, OPT_NOOUT
, OPT_INFO
, OPT_CHAIN
, OPT_TWOPASS
, OPT_NOMACVER
,
100 OPT_DESCERT
, OPT_EXPORT
, OPT_NOITER
, OPT_MACITER
, OPT_NOMACITER
,
101 OPT_NOMAC
, OPT_LMK
, OPT_NODES
, OPT_MACALG
, OPT_CERTPBE
, OPT_KEYPBE
,
102 OPT_RAND
, OPT_INKEY
, OPT_CERTFILE
, OPT_NAME
, OPT_CSP
, OPT_CANAME
,
103 OPT_IN
, OPT_OUT
, OPT_PASSIN
, OPT_PASSOUT
, OPT_PASSWORD
, OPT_CAPATH
,
104 OPT_CAFILE
, OPT_NOCAPATH
, OPT_NOCAFILE
, OPT_ENGINE
107 OPTIONS pkcs12_options
[] = {
108 {"help", OPT_HELP
, '-', "Display this summary"},
109 {"nokeys", OPT_NOKEYS
, '-', "Don't output private keys"},
110 {"keyex", OPT_KEYEX
, '-', "Set MS key exchange type"},
111 {"keysig", OPT_KEYSIG
, '-', "Set MS key signature type"},
112 {"nocerts", OPT_NOCERTS
, '-', "Don't output certificates"},
113 {"clcerts", OPT_CLCERTS
, '-', "Only output client certificates"},
114 {"cacerts", OPT_CACERTS
, '-', "Only output CA certificates"},
115 {"noout", OPT_NOOUT
, '-', "Don't output anything, just verify"},
116 {"info", OPT_INFO
, '-', "Print info about PKCS#12 structure"},
117 {"chain", OPT_CHAIN
, '-', "Add certificate chain"},
118 {"twopass", OPT_TWOPASS
, '-', "Separate MAC, encryption passwords"},
119 {"nomacver", OPT_NOMACVER
, '-', "Don't verify MAC"},
120 # ifndef OPENSSL_NO_RC2
121 {"descert", OPT_DESCERT
, '-',
122 "Encrypt output with 3DES (default RC2-40)"},
123 {"certpbe", OPT_CERTPBE
, 's',
124 "Certificate PBE algorithm (default RC2-40)"},
126 {"descert", OPT_DESCERT
, '-', "Encrypt output with 3DES (the default)"},
127 {"certpbe", OPT_CERTPBE
, 's', "Certificate PBE algorithm (default 3DES)"},
129 {"export", OPT_EXPORT
, '-', "Output PKCS12 file"},
130 {"noiter", OPT_NOITER
, '-', "Don't use encryption iteration"},
131 {"maciter", OPT_MACITER
, '-', "Use MAC iteration"},
132 {"nomaciter", OPT_NOMACITER
, '-', "Don't use MAC iteration"},
133 {"nomac", OPT_NOMAC
, '-', "Don't generate MAC"},
134 {"LMK", OPT_LMK
, '-',
135 "Add local machine keyset attribute to private key"},
136 {"nodes", OPT_NODES
, '-', "Don't encrypt private keys"},
137 {"macalg", OPT_MACALG
, 's',
138 "Digest algorithm used in MAC (default SHA1)"},
139 {"keypbe", OPT_KEYPBE
, 's', "Private key PBE algorithm (default 3DES)"},
140 {"rand", OPT_RAND
, 's',
141 "Load the file(s) into the random number generator"},
142 {"inkey", OPT_INKEY
, '<', "Private key if not infile"},
143 {"certfile", OPT_CERTFILE
, '<', "Load certs from file"},
144 {"name", OPT_NAME
, 's', "Use name as friendly name"},
145 {"CSP", OPT_CSP
, 's', "Microsoft CSP name"},
146 {"caname", OPT_CANAME
, 's',
147 "Use name as CA friendly name (can be repeated)"},
148 {"in", OPT_IN
, '<', "Input filename"},
149 {"out", OPT_OUT
, '>', "Output filename"},
150 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
151 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
152 {"password", OPT_PASSWORD
, 's', "Set import/export password source"},
153 {"CApath", OPT_CAPATH
, '/', "PEM-format directory of CA's"},
154 {"CAfile", OPT_CAFILE
, '<', "PEM-format file of CA's"},
155 {"no-CAfile", OPT_NOCAFILE
, '-',
156 "Do not load the default certificates file"},
157 {"no-CApath", OPT_NOCAPATH
, '-',
158 "Do not load certificates from the default certificates directory"},
159 {"", OPT_CIPHER
, '-', "Any supported cipher"},
160 # ifndef OPENSSL_NO_ENGINE
161 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
166 int pkcs12_main(int argc
, char **argv
)
168 char *infile
= NULL
, *outfile
= NULL
, *keyname
= NULL
, *certfile
= NULL
;
169 char *name
= NULL
, *csp_name
= NULL
;
170 char pass
[2048], macpass
[2048];
171 int export_cert
= 0, options
= 0, chain
= 0, twopass
= 0, keytype
= 0;
172 int iter
= PKCS12_DEFAULT_ITER
, maciter
= PKCS12_DEFAULT_ITER
;
173 # ifndef OPENSSL_NO_RC2
174 int cert_pbe
= NID_pbe_WithSHA1And40BitRC2_CBC
;
176 int cert_pbe
= NID_pbe_WithSHA1And3_Key_TripleDES_CBC
;
178 int key_pbe
= NID_pbe_WithSHA1And3_Key_TripleDES_CBC
;
179 int ret
= 1, macver
= 1, add_lmk
= 0, private = 0;
181 char *passinarg
= NULL
, *passoutarg
= NULL
, *passarg
= NULL
;
182 char *passin
= NULL
, *passout
= NULL
, *inrand
= NULL
, *macalg
= NULL
;
183 char *cpass
= NULL
, *mpass
= NULL
, *CApath
= NULL
, *CAfile
= NULL
;
185 int noCApath
= 0, noCAfile
= 0;
187 BIO
*in
= NULL
, *out
= NULL
;
189 STACK_OF(OPENSSL_STRING
) *canames
= NULL
;
190 const EVP_CIPHER
*enc
= EVP_des_ede3_cbc();
193 prog
= opt_init(argc
, argv
, pkcs12_options
);
194 while ((o
= opt_next()) != OPT_EOF
) {
199 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
202 opt_help(pkcs12_options
);
224 options
|= (NOKEYS
| NOCERTS
);
239 cert_pbe
= NID_pbe_WithSHA1And3_Key_TripleDES_CBC
;
245 if (!opt_cipher(opt_unknown(), &enc
))
252 maciter
= PKCS12_DEFAULT_ITER
;
267 if (!set_pbe(&cert_pbe
, opt_arg()))
271 if (!set_pbe(&key_pbe
, opt_arg()))
281 certfile
= opt_arg();
290 csp_name
= opt_arg();
294 && (canames
= sk_OPENSSL_STRING_new_null()) == NULL
)
296 sk_OPENSSL_STRING_push(canames
, opt_arg());
305 passinarg
= opt_arg();
308 passoutarg
= opt_arg();
326 e
= setup_engine(opt_arg(), 0);
330 argc
= opt_num_rest();
338 passoutarg
= passarg
;
343 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
344 BIO_printf(bio_err
, "Error getting passwords\n");
363 if (export_cert
|| inrand
) {
364 app_RAND_load_file(NULL
, (inrand
!= NULL
));
366 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
367 app_RAND_load_files(inrand
));
372 #ifndef OPENSSL_NO_UI
373 if (EVP_read_pw_string
374 (macpass
, sizeof macpass
, "Enter MAC Password:", export_cert
)) {
375 BIO_printf(bio_err
, "Can't read Password\n");
380 BIO_printf(bio_err
, "Unsupported option -twopass\n");
386 EVP_PKEY
*key
= NULL
;
387 X509
*ucert
= NULL
, *x
= NULL
;
388 STACK_OF(X509
) *certs
= NULL
;
389 const EVP_MD
*macmd
= NULL
;
390 unsigned char *catmp
= NULL
;
393 if ((options
& (NOCERTS
| NOKEYS
)) == (NOCERTS
| NOKEYS
)) {
394 BIO_printf(bio_err
, "Nothing to do!\n");
398 if (options
& NOCERTS
)
401 if (!(options
& NOKEYS
)) {
402 key
= load_key(keyname
? keyname
: infile
,
403 FORMAT_PEM
, 1, passin
, e
, "private key");
408 /* Load in all certs in input file */
409 if (!(options
& NOCERTS
)) {
410 if (!load_certs(infile
, &certs
, FORMAT_PEM
, NULL
,
415 /* Look for matching private key */
416 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
417 x
= sk_X509_value(certs
, i
);
418 if (X509_check_private_key(x
, key
)) {
420 /* Zero keyid and alias */
421 X509_keyid_set1(ucert
, NULL
, 0);
422 X509_alias_set1(ucert
, NULL
, 0);
423 /* Remove from list */
424 (void)sk_X509_delete(certs
, i
);
430 "No certificate matches private key\n");
437 /* Add any more certificates asked for */
439 if (!load_certs(certfile
, &certs
, FORMAT_PEM
, NULL
,
440 "certificates from certfile"))
444 /* If chaining get chain from user cert */
447 STACK_OF(X509
) *chain2
;
449 if ((store
= setup_verify(CAfile
, CApath
, noCAfile
, noCApath
))
453 vret
= get_cert_chain(ucert
, store
, &chain2
);
454 X509_STORE_free(store
);
456 if (vret
== X509_V_OK
) {
457 /* Exclude verified certificate */
458 for (i
= 1; i
< sk_X509_num(chain2
); i
++)
459 sk_X509_push(certs
, sk_X509_value(chain2
, i
));
460 /* Free first certificate */
461 X509_free(sk_X509_value(chain2
, 0));
462 sk_X509_free(chain2
);
464 if (vret
!= X509_V_ERR_UNSPECIFIED
)
465 BIO_printf(bio_err
, "Error %s getting chain.\n",
466 X509_verify_cert_error_string(vret
));
468 ERR_print_errors(bio_err
);
473 /* Add any CA names */
475 for (i
= 0; i
< sk_OPENSSL_STRING_num(canames
); i
++) {
476 catmp
= (unsigned char *)sk_OPENSSL_STRING_value(canames
, i
);
477 X509_alias_set1(sk_X509_value(certs
, i
), catmp
, -1);
481 EVP_PKEY_add1_attr_by_NID(key
, NID_ms_csp_name
,
482 MBSTRING_ASC
, (unsigned char *)csp_name
,
486 EVP_PKEY_add1_attr_by_NID(key
, NID_LocalKeySet
, 0, NULL
, -1);
490 #ifndef OPENSSL_NO_UI
491 if (EVP_read_pw_string(pass
, sizeof pass
, "Enter Export Password:",
493 BIO_printf(bio_err
, "Can't read Password\n");
498 BIO_printf(bio_err
, "Password required\n");
504 OPENSSL_strlcpy(macpass
, pass
, sizeof macpass
);
506 p12
= PKCS12_create(cpass
, name
, key
, ucert
, certs
,
507 key_pbe
, cert_pbe
, iter
, -1, keytype
);
510 ERR_print_errors(bio_err
);
515 if (!opt_md(macalg
, &macmd
))
520 PKCS12_set_mac(p12
, mpass
, -1, NULL
, 0, maciter
, macmd
);
524 out
= bio_open_owner(outfile
, FORMAT_PKCS12
, private);
528 i2d_PKCS12_bio(out
, p12
);
535 sk_X509_pop_free(certs
, X509_free
);
542 in
= bio_open_default(infile
, 'r', FORMAT_PKCS12
);
545 out
= bio_open_owner(outfile
, FORMAT_PEM
, private);
549 if ((p12
= d2i_PKCS12_bio(in
, NULL
)) == NULL
) {
550 ERR_print_errors(bio_err
);
556 #ifndef OPENSSL_NO_UI
557 if (EVP_read_pw_string(pass
, sizeof pass
, "Enter Import Password:",
559 BIO_printf(bio_err
, "Can't read Password\n");
564 BIO_printf(bio_err
, "Password required\n");
570 OPENSSL_strlcpy(macpass
, pass
, sizeof macpass
);
572 if ((options
& INFO
) && PKCS12_mac_present(p12
)) {
573 ASN1_INTEGER
*tmaciter
;
575 PKCS12_get0_mac(NULL
, NULL
, NULL
, &tmaciter
, p12
);
576 BIO_printf(bio_err
, "MAC Iteration %ld\n",
577 tmaciter
!= NULL
? ASN1_INTEGER_get(tmaciter
) : 1L);
580 /* If we enter empty password try no password first */
581 if (!mpass
[0] && PKCS12_verify_mac(p12
, NULL
, 0)) {
582 /* If mac and crypto pass the same set it to NULL too */
585 } else if (!PKCS12_verify_mac(p12
, mpass
, -1)) {
586 BIO_printf(bio_err
, "Mac verify error: invalid password?\n");
587 ERR_print_errors(bio_err
);
593 if (!dump_certs_keys_p12(out
, p12
, cpass
, -1, options
, passout
, enc
)) {
594 BIO_printf(bio_err
, "Error outputting keys and certificates\n");
595 ERR_print_errors(bio_err
);
601 if (export_cert
|| inrand
)
602 app_RAND_write_file(NULL
);
605 sk_OPENSSL_STRING_free(canames
);
606 OPENSSL_free(passin
);
607 OPENSSL_free(passout
);
611 int dump_certs_keys_p12(BIO
*out
, PKCS12
*p12
, char *pass
,
612 int passlen
, int options
, char *pempass
,
613 const EVP_CIPHER
*enc
)
615 STACK_OF(PKCS7
) *asafes
= NULL
;
616 STACK_OF(PKCS12_SAFEBAG
) *bags
;
621 if ((asafes
= PKCS12_unpack_authsafes(p12
)) == NULL
)
623 for (i
= 0; i
< sk_PKCS7_num(asafes
); i
++) {
624 p7
= sk_PKCS7_value(asafes
, i
);
625 bagnid
= OBJ_obj2nid(p7
->type
);
626 if (bagnid
== NID_pkcs7_data
) {
627 bags
= PKCS12_unpack_p7data(p7
);
629 BIO_printf(bio_err
, "PKCS7 Data\n");
630 } else if (bagnid
== NID_pkcs7_encrypted
) {
631 if (options
& INFO
) {
632 BIO_printf(bio_err
, "PKCS7 Encrypted data: ");
633 alg_print(p7
->d
.encrypted
->enc_data
->algorithm
);
635 bags
= PKCS12_unpack_p7encdata(p7
, pass
, passlen
);
640 if (!dump_certs_pkeys_bags(out
, bags
, pass
, passlen
,
641 options
, pempass
, enc
)) {
642 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
645 sk_PKCS12_SAFEBAG_pop_free(bags
, PKCS12_SAFEBAG_free
);
651 sk_PKCS7_pop_free(asafes
, PKCS7_free
);
655 int dump_certs_pkeys_bags(BIO
*out
, STACK_OF(PKCS12_SAFEBAG
) *bags
,
656 char *pass
, int passlen
, int options
, char *pempass
,
657 const EVP_CIPHER
*enc
)
660 for (i
= 0; i
< sk_PKCS12_SAFEBAG_num(bags
); i
++) {
661 if (!dump_certs_pkeys_bag(out
,
662 sk_PKCS12_SAFEBAG_value(bags
, i
),
663 pass
, passlen
, options
, pempass
, enc
))
669 int dump_certs_pkeys_bag(BIO
*out
, PKCS12_SAFEBAG
*bag
, char *pass
,
670 int passlen
, int options
, char *pempass
,
671 const EVP_CIPHER
*enc
)
674 PKCS8_PRIV_KEY_INFO
*p8
;
676 STACK_OF(X509_ATTRIBUTE
) *attrs
;
679 attrs
= PKCS12_SAFEBAG_get0_attrs(bag
);
681 switch (PKCS12_SAFEBAG_get_nid(bag
)) {
684 BIO_printf(bio_err
, "Key bag\n");
685 if (options
& NOKEYS
)
687 print_attribs(out
, attrs
, "Bag Attributes");
688 p8
= PKCS12_SAFEBAG_get0_p8inf(bag
);
689 if ((pkey
= EVP_PKCS82PKEY(p8
)) == NULL
)
691 print_attribs(out
, PKCS8_pkey_get0_attrs(p8
), "Key Attributes");
692 ret
= PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, pempass
);
696 case NID_pkcs8ShroudedKeyBag
:
697 if (options
& INFO
) {
701 BIO_printf(bio_err
, "Shrouded Keybag: ");
702 tp8
= PKCS12_SAFEBAG_get0_pkcs8(bag
);
703 X509_SIG_get0(&tp8alg
, NULL
, tp8
);
706 if (options
& NOKEYS
)
708 print_attribs(out
, attrs
, "Bag Attributes");
709 if ((p8
= PKCS12_decrypt_skey(bag
, pass
, passlen
)) == NULL
)
711 if ((pkey
= EVP_PKCS82PKEY(p8
)) == NULL
) {
712 PKCS8_PRIV_KEY_INFO_free(p8
);
715 print_attribs(out
, PKCS8_pkey_get0_attrs(p8
), "Key Attributes");
716 PKCS8_PRIV_KEY_INFO_free(p8
);
717 ret
= PEM_write_bio_PrivateKey(out
, pkey
, enc
, NULL
, 0, NULL
, pempass
);
723 BIO_printf(bio_err
, "Certificate bag\n");
724 if (options
& NOCERTS
)
726 if (PKCS12_SAFEBAG_get0_attr(bag
, NID_localKeyID
)) {
727 if (options
& CACERTS
)
729 } else if (options
& CLCERTS
)
731 print_attribs(out
, attrs
, "Bag Attributes");
732 if (PKCS12_SAFEBAG_get_bag_nid(bag
) != NID_x509Certificate
)
734 if ((x509
= PKCS12_SAFEBAG_get1_cert(bag
)) == NULL
)
736 dump_cert_text(out
, x509
);
737 ret
= PEM_write_bio_X509(out
, x509
);
741 case NID_safeContentsBag
:
743 BIO_printf(bio_err
, "Safe Contents bag\n");
744 print_attribs(out
, attrs
, "Bag Attributes");
745 return dump_certs_pkeys_bags(out
, PKCS12_SAFEBAG_get0_safes(bag
),
746 pass
, passlen
, options
, pempass
, enc
);
749 BIO_printf(bio_err
, "Warning unsupported bag type: ");
750 i2a_ASN1_OBJECT(bio_err
, PKCS12_SAFEBAG_get0_type(bag
));
751 BIO_printf(bio_err
, "\n");
757 /* Given a single certificate return a verified chain or NULL if error */
759 static int get_cert_chain(X509
*cert
, X509_STORE
*store
,
760 STACK_OF(X509
) **chain
)
762 X509_STORE_CTX
*store_ctx
= NULL
;
763 STACK_OF(X509
) *chn
= NULL
;
766 store_ctx
= X509_STORE_CTX_new();
767 if (store_ctx
== NULL
) {
768 i
= X509_V_ERR_UNSPECIFIED
;
771 if (!X509_STORE_CTX_init(store_ctx
, store
, cert
, NULL
)) {
772 i
= X509_V_ERR_UNSPECIFIED
;
777 if (X509_verify_cert(store_ctx
) > 0)
778 chn
= X509_STORE_CTX_get1_chain(store_ctx
);
779 else if ((i
= X509_STORE_CTX_get_error(store_ctx
)) == 0)
780 i
= X509_V_ERR_UNSPECIFIED
;
783 X509_STORE_CTX_free(store_ctx
);
788 static int alg_print(X509_ALGOR
*alg
)
791 const unsigned char *p
= alg
->parameter
->value
.sequence
->data
;
793 pbe
= d2i_PBEPARAM(NULL
, &p
, alg
->parameter
->value
.sequence
->length
);
796 BIO_printf(bio_err
, "%s, Iteration %ld\n",
797 OBJ_nid2ln(OBJ_obj2nid(alg
->algorithm
)),
798 ASN1_INTEGER_get(pbe
->iter
));
803 /* Load all certificates from a given file */
805 int cert_load(BIO
*in
, STACK_OF(X509
) *sk
)
810 while ((cert
= PEM_read_bio_X509(in
, NULL
, NULL
, NULL
))) {
812 sk_X509_push(sk
, cert
);
819 /* Generalised attribute print: handle PKCS#8 and bag attributes */
821 int print_attribs(BIO
*out
, STACK_OF(X509_ATTRIBUTE
) *attrlst
,
824 X509_ATTRIBUTE
*attr
;
829 BIO_printf(out
, "%s: <No Attributes>\n", name
);
832 if (!sk_X509_ATTRIBUTE_num(attrlst
)) {
833 BIO_printf(out
, "%s: <Empty Attributes>\n", name
);
836 BIO_printf(out
, "%s\n", name
);
837 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(attrlst
); i
++) {
838 ASN1_OBJECT
*attr_obj
;
839 attr
= sk_X509_ATTRIBUTE_value(attrlst
, i
);
840 attr_obj
= X509_ATTRIBUTE_get0_object(attr
);
841 attr_nid
= OBJ_obj2nid(attr_obj
);
842 BIO_printf(out
, " ");
843 if (attr_nid
== NID_undef
) {
844 i2a_ASN1_OBJECT(out
, attr_obj
);
845 BIO_printf(out
, ": ");
847 BIO_printf(out
, "%s: ", OBJ_nid2ln(attr_nid
));
849 if (X509_ATTRIBUTE_count(attr
)) {
850 av
= X509_ATTRIBUTE_get0_type(attr
, 0);
852 case V_ASN1_BMPSTRING
:
853 value
= OPENSSL_uni2asc(av
->value
.bmpstring
->data
,
854 av
->value
.bmpstring
->length
);
855 BIO_printf(out
, "%s\n", value
);
859 case V_ASN1_OCTET_STRING
:
860 hex_prin(out
, av
->value
.octet_string
->data
,
861 av
->value
.octet_string
->length
);
862 BIO_printf(out
, "\n");
865 case V_ASN1_BIT_STRING
:
866 hex_prin(out
, av
->value
.bit_string
->data
,
867 av
->value
.bit_string
->length
);
868 BIO_printf(out
, "\n");
872 BIO_printf(out
, "<Unsupported tag %d>\n", av
->type
);
876 BIO_printf(out
, "<No Values>\n");
881 void hex_prin(BIO
*out
, unsigned char *buf
, int len
)
884 for (i
= 0; i
< len
; i
++)
885 BIO_printf(out
, "%02X ", buf
[i
]);
888 static int set_pbe(int *ppbe
, const char *str
)
892 if (strcmp(str
, "NONE") == 0) {
896 *ppbe
= OBJ_txt2nid(str
);
897 if (*ppbe
== NID_undef
) {
898 BIO_printf(bio_err
, "Unknown PBE algorithm %s\n", str
);