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 # define PROG pkcs12_main
74 const EVP_CIPHER *enc;
82 static int get_cert_chain(X509 *cert, X509_STORE *store,
83 STACK_OF(X509) **chain);
84 int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
85 int options, char *pempass);
86 int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
87 char *pass, int passlen, int options,
89 int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
90 int passlen, int options, char *pempass);
91 int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
93 void hex_prin(BIO *out, unsigned char *buf, int len);
94 int alg_print(BIO *x, X509_ALGOR *alg);
95 int cert_load(BIO *in, STACK_OF(X509) *sk);
96 static int set_pbe(BIO *err, int *ppbe, const char *str);
98 int MAIN(int, char **);
100 int MAIN(int argc, char **argv)
103 char *infile = NULL, *outfile = NULL, *keyname = NULL;
104 char *certfile = NULL;
105 BIO *in = NULL, *out = NULL;
108 char *csp_name = NULL;
111 char pass[50], macpass[50];
116 int iter = PKCS12_DEFAULT_ITER;
117 int maciter = PKCS12_DEFAULT_ITER;
121 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
125 STACK_OF(OPENSSL_STRING) *canames = NULL;
126 char *cpass = NULL, *mpass = NULL;
127 char *passargin = NULL, *passargout = NULL, *passarg = NULL;
128 char *passin = NULL, *passout = NULL;
131 char *CApath = NULL, *CAfile = NULL;
132 # ifndef OPENSSL_NO_ENGINE
138 enc = EVP_des_ede3_cbc();
140 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
142 if (!load_config(bio_err, NULL))
147 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
150 cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
155 if (*args[0] == '-') {
156 if (!strcmp(*args, "-nokeys"))
158 else if (!strcmp(*args, "-keyex"))
160 else if (!strcmp(*args, "-keysig"))
162 else if (!strcmp(*args, "-nocerts"))
164 else if (!strcmp(*args, "-clcerts"))
166 else if (!strcmp(*args, "-cacerts"))
168 else if (!strcmp(*args, "-noout"))
169 options |= (NOKEYS | NOCERTS);
170 else if (!strcmp(*args, "-info"))
172 else if (!strcmp(*args, "-chain"))
174 else if (!strcmp(*args, "-twopass"))
176 else if (!strcmp(*args, "-nomacver"))
178 else if (!strcmp(*args, "-descert"))
179 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
180 else if (!strcmp(*args, "-export"))
182 else if (!strcmp(*args, "-des"))
184 else if (!strcmp(*args, "-des3"))
185 enc = EVP_des_ede3_cbc();
186 # ifndef OPENSSL_NO_IDEA
187 else if (!strcmp(*args, "-idea"))
188 enc = EVP_idea_cbc();
190 # ifndef OPENSSL_NO_SEED
191 else if (!strcmp(*args, "-seed"))
192 enc = EVP_seed_cbc();
194 # ifndef OPENSSL_NO_AES
195 else if (!strcmp(*args, "-aes128"))
196 enc = EVP_aes_128_cbc();
197 else if (!strcmp(*args, "-aes192"))
198 enc = EVP_aes_192_cbc();
199 else if (!strcmp(*args, "-aes256"))
200 enc = EVP_aes_256_cbc();
202 # ifndef OPENSSL_NO_CAMELLIA
203 else if (!strcmp(*args, "-camellia128"))
204 enc = EVP_camellia_128_cbc();
205 else if (!strcmp(*args, "-camellia192"))
206 enc = EVP_camellia_192_cbc();
207 else if (!strcmp(*args, "-camellia256"))
208 enc = EVP_camellia_256_cbc();
210 else if (!strcmp(*args, "-noiter"))
212 else if (!strcmp(*args, "-maciter"))
213 maciter = PKCS12_DEFAULT_ITER;
214 else if (!strcmp(*args, "-nomaciter"))
216 else if (!strcmp(*args, "-nomac"))
218 else if (!strcmp(*args, "-macalg"))
224 else if (!strcmp(*args, "-nodes"))
226 else if (!strcmp(*args, "-certpbe")) {
227 if (!set_pbe(bio_err, &cert_pbe, *++args))
229 } else if (!strcmp(*args, "-keypbe")) {
230 if (!set_pbe(bio_err, &key_pbe, *++args))
232 } else if (!strcmp(*args, "-rand")) {
238 } else if (!strcmp(*args, "-inkey")) {
244 } else if (!strcmp(*args, "-certfile")) {
250 } else if (!strcmp(*args, "-name")) {
256 } else if (!strcmp(*args, "-LMK"))
258 else if (!strcmp(*args, "-CSP")) {
264 } else if (!strcmp(*args, "-caname")) {
268 canames = sk_OPENSSL_STRING_new_null();
269 sk_OPENSSL_STRING_push(canames, *args);
272 } else if (!strcmp(*args, "-in")) {
278 } else if (!strcmp(*args, "-out")) {
284 } else if (!strcmp(*args, "-passin")) {
290 } else if (!strcmp(*args, "-passout")) {
296 } else if (!strcmp(*args, "-password")) {
303 } else if (!strcmp(*args, "-CApath")) {
309 } else if (!strcmp(*args, "-CAfile")) {
315 # ifndef OPENSSL_NO_ENGINE
316 } else if (!strcmp(*args, "-engine")) {
332 BIO_printf(bio_err, "Usage: pkcs12 [options]\n");
333 BIO_printf(bio_err, "where options are\n");
334 BIO_printf(bio_err, "-export output PKCS12 file\n");
335 BIO_printf(bio_err, "-chain add certificate chain\n");
336 BIO_printf(bio_err, "-inkey file private key if not infile\n");
337 BIO_printf(bio_err, "-certfile f add all certs in f\n");
338 BIO_printf(bio_err, "-CApath arg - PEM format directory of CA's\n");
339 BIO_printf(bio_err, "-CAfile arg - PEM format file of CA's\n");
340 BIO_printf(bio_err, "-name \"name\" use name as friendly name\n");
342 "-caname \"nm\" use nm as CA friendly name (can be used more than once).\n");
343 BIO_printf(bio_err, "-in infile input filename\n");
344 BIO_printf(bio_err, "-out outfile output filename\n");
346 "-noout don't output anything, just verify.\n");
347 BIO_printf(bio_err, "-nomacver don't verify MAC.\n");
348 BIO_printf(bio_err, "-nocerts don't output certificates.\n");
350 "-clcerts only output client certificates.\n");
351 BIO_printf(bio_err, "-cacerts only output CA certificates.\n");
352 BIO_printf(bio_err, "-nokeys don't output private keys.\n");
354 "-info give info about PKCS#12 structure.\n");
355 BIO_printf(bio_err, "-des encrypt private keys with DES\n");
357 "-des3 encrypt private keys with triple DES (default)\n");
358 # ifndef OPENSSL_NO_IDEA
359 BIO_printf(bio_err, "-idea encrypt private keys with idea\n");
361 # ifndef OPENSSL_NO_SEED
362 BIO_printf(bio_err, "-seed encrypt private keys with seed\n");
364 # ifndef OPENSSL_NO_AES
365 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
367 " encrypt PEM output with cbc aes\n");
369 # ifndef OPENSSL_NO_CAMELLIA
370 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
372 " encrypt PEM output with cbc camellia\n");
374 BIO_printf(bio_err, "-nodes don't encrypt private keys\n");
375 BIO_printf(bio_err, "-noiter don't use encryption iteration\n");
376 BIO_printf(bio_err, "-nomaciter don't use MAC iteration\n");
377 BIO_printf(bio_err, "-maciter use MAC iteration\n");
378 BIO_printf(bio_err, "-nomac don't generate MAC\n");
380 "-twopass separate MAC, encryption passwords\n");
382 "-descert encrypt PKCS#12 certificates with triple DES (default RC2-40)\n");
384 "-certpbe alg specify certificate PBE algorithm (default RC2-40)\n");
386 "-keypbe alg specify private key PBE algorithm (default 3DES)\n");
388 "-macalg alg digest algorithm used in MAC (default SHA1)\n");
389 BIO_printf(bio_err, "-keyex set MS key exchange type\n");
390 BIO_printf(bio_err, "-keysig set MS key signature type\n");
392 "-password p set import/export password source\n");
393 BIO_printf(bio_err, "-passin p input file pass phrase source\n");
394 BIO_printf(bio_err, "-passout p output file pass phrase source\n");
395 # ifndef OPENSSL_NO_ENGINE
397 "-engine e use engine e, possibly a hardware device.\n");
399 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
400 LIST_SEPARATOR_CHAR);
402 " load the file (or the files in the directory) into\n");
403 BIO_printf(bio_err, " the random number generator\n");
404 BIO_printf(bio_err, "-CSP name Microsoft CSP name\n");
406 "-LMK Add local machine keyset attribute to private key\n");
409 # ifndef OPENSSL_NO_ENGINE
410 e = setup_engine(bio_err, engine, 0);
415 passargout = passarg;
420 if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
421 BIO_printf(bio_err, "Error getting passwords\n");
440 if (export_cert || inrand) {
441 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
443 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
444 app_RAND_load_files(inrand));
446 ERR_load_crypto_strings();
448 # ifdef CRYPTO_MDEBUG
449 CRYPTO_push_info("read files");
453 in = BIO_new_fp(stdin, BIO_NOCLOSE);
455 in = BIO_new_file(infile, "rb");
457 BIO_printf(bio_err, "Error opening input file %s\n",
458 infile ? infile : "<stdin>");
462 # ifdef CRYPTO_MDEBUG
464 CRYPTO_push_info("write files");
468 out = BIO_new_fp(stdout, BIO_NOCLOSE);
469 # ifdef OPENSSL_SYS_VMS
471 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
472 out = BIO_push(tmpbio, out);
476 out = BIO_new_file(outfile, "wb");
478 BIO_printf(bio_err, "Error opening output file %s\n",
479 outfile ? outfile : "<stdout>");
484 # ifdef CRYPTO_MDEBUG
485 CRYPTO_push_info("read MAC password");
487 if (EVP_read_pw_string
488 (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
489 BIO_printf(bio_err, "Can't read Password\n");
492 # ifdef CRYPTO_MDEBUG
498 EVP_PKEY *key = NULL;
499 X509 *ucert = NULL, *x = NULL;
500 STACK_OF(X509) *certs = NULL;
501 const EVP_MD *macmd = NULL;
502 unsigned char *catmp = NULL;
505 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
506 BIO_printf(bio_err, "Nothing to do!\n");
510 if (options & NOCERTS)
513 # ifdef CRYPTO_MDEBUG
514 CRYPTO_push_info("process -export_cert");
515 CRYPTO_push_info("reading private key");
517 if (!(options & NOKEYS)) {
518 key = load_key(bio_err, keyname ? keyname : infile,
519 FORMAT_PEM, 1, passin, e, "private key");
523 # ifdef CRYPTO_MDEBUG
525 CRYPTO_push_info("reading certs from input");
528 /* Load in all certs in input file */
529 if (!(options & NOCERTS)) {
530 certs = load_certs(bio_err, infile, FORMAT_PEM, NULL, e,
536 /* Look for matching private key */
537 for (i = 0; i < sk_X509_num(certs); i++) {
538 x = sk_X509_value(certs, i);
539 if (X509_check_private_key(x, key)) {
541 /* Zero keyid and alias */
542 X509_keyid_set1(ucert, NULL, 0);
543 X509_alias_set1(ucert, NULL, 0);
544 /* Remove from list */
545 (void)sk_X509_delete(certs, i);
551 "No certificate matches private key\n");
557 # ifdef CRYPTO_MDEBUG
559 CRYPTO_push_info("reading certs from input 2");
562 /* Add any more certificates asked for */
564 STACK_OF(X509) *morecerts = NULL;
565 if (!(morecerts = load_certs(bio_err, certfile, FORMAT_PEM,
567 "certificates from certfile")))
569 while (sk_X509_num(morecerts) > 0)
570 sk_X509_push(certs, sk_X509_shift(morecerts));
571 sk_X509_free(morecerts);
573 # ifdef CRYPTO_MDEBUG
575 CRYPTO_push_info("reading certs from certfile");
578 # ifdef CRYPTO_MDEBUG
580 CRYPTO_push_info("building chain");
583 /* If chaining get chain from user cert */
586 STACK_OF(X509) *chain2;
587 X509_STORE *store = X509_STORE_new();
589 BIO_printf(bio_err, "Memory allocation error\n");
592 if (!X509_STORE_load_locations(store, CAfile, CApath))
593 X509_STORE_set_default_paths(store);
595 vret = get_cert_chain(ucert, store, &chain2);
596 X509_STORE_free(store);
598 if (vret == X509_V_OK) {
599 /* Exclude verified certificate */
600 for (i = 1; i < sk_X509_num(chain2); i++)
601 sk_X509_push(certs, sk_X509_value(chain2, i));
602 /* Free first certificate */
603 X509_free(sk_X509_value(chain2, 0));
604 sk_X509_free(chain2);
606 if (vret != X509_V_ERR_UNSPECIFIED)
607 BIO_printf(bio_err, "Error %s getting chain.\n",
608 X509_verify_cert_error_string(vret));
610 ERR_print_errors(bio_err);
615 /* Add any CA names */
617 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
618 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
619 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
623 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
624 MBSTRING_ASC, (unsigned char *)csp_name,
628 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
630 # ifdef CRYPTO_MDEBUG
632 CRYPTO_push_info("reading password");
636 EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
638 BIO_printf(bio_err, "Can't read Password\n");
642 BUF_strlcpy(macpass, pass, sizeof macpass);
644 # ifdef CRYPTO_MDEBUG
646 CRYPTO_push_info("creating PKCS#12 structure");
649 p12 = PKCS12_create(cpass, name, key, ucert, certs,
650 key_pbe, cert_pbe, iter, -1, keytype);
653 ERR_print_errors(bio_err);
658 macmd = EVP_get_digestbyname(macalg);
660 BIO_printf(bio_err, "Unknown digest algorithm %s\n", macalg);
665 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
667 # ifdef CRYPTO_MDEBUG
669 CRYPTO_push_info("writing pkcs12");
672 i2d_PKCS12_bio(out, p12);
677 # ifdef CRYPTO_MDEBUG
680 CRYPTO_push_info("process -export_cert: freeing");
686 sk_X509_pop_free(certs, X509_free);
690 # ifdef CRYPTO_MDEBUG
697 if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
698 ERR_print_errors(bio_err);
701 # ifdef CRYPTO_MDEBUG
702 CRYPTO_push_info("read import password");
705 && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
707 BIO_printf(bio_err, "Can't read Password\n");
710 # ifdef CRYPTO_MDEBUG
715 BUF_strlcpy(macpass, pass, sizeof macpass);
717 if ((options & INFO) && p12->mac)
718 BIO_printf(bio_err, "MAC Iteration %ld\n",
719 p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
721 # ifdef CRYPTO_MDEBUG
722 CRYPTO_push_info("verify MAC");
724 /* If we enter empty password try no password first */
725 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
726 /* If mac and crypto pass the same set it to NULL too */
729 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
730 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
731 ERR_print_errors(bio_err);
734 BIO_printf(bio_err, "MAC verified OK\n");
735 # ifdef CRYPTO_MDEBUG
739 # ifdef CRYPTO_MDEBUG
740 CRYPTO_push_info("output keys and certificates");
742 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout)) {
743 BIO_printf(bio_err, "Error outputting keys and certificates\n");
744 ERR_print_errors(bio_err);
747 # ifdef CRYPTO_MDEBUG
754 if (export_cert || inrand)
755 app_RAND_write_file(NULL, bio_err);
756 # ifdef CRYPTO_MDEBUG
757 CRYPTO_remove_all_info();
762 sk_OPENSSL_STRING_free(canames);
764 OPENSSL_free(passin);
766 OPENSSL_free(passout);
771 int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
772 int passlen, int options, char *pempass)
774 STACK_OF(PKCS7) *asafes = NULL;
775 STACK_OF(PKCS12_SAFEBAG) *bags;
780 if (!(asafes = PKCS12_unpack_authsafes(p12)))
782 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
783 p7 = sk_PKCS7_value(asafes, i);
784 bagnid = OBJ_obj2nid(p7->type);
785 if (bagnid == NID_pkcs7_data) {
786 bags = PKCS12_unpack_p7data(p7);
788 BIO_printf(bio_err, "PKCS7 Data\n");
789 } else if (bagnid == NID_pkcs7_encrypted) {
790 if (options & INFO) {
791 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
792 alg_print(bio_err, p7->d.encrypted->enc_data->algorithm);
794 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
799 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
801 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
804 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
812 sk_PKCS7_pop_free(asafes, PKCS7_free);
816 int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
817 char *pass, int passlen, int options, char *pempass)
820 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
821 if (!dump_certs_pkeys_bag(out,
822 sk_PKCS12_SAFEBAG_value(bags, i),
823 pass, passlen, options, pempass))
829 int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
830 int passlen, int options, char *pempass)
833 PKCS8_PRIV_KEY_INFO *p8;
836 switch (M_PKCS12_bag_type(bag)) {
839 BIO_printf(bio_err, "Key bag\n");
840 if (options & NOKEYS)
842 print_attribs(out, bag->attrib, "Bag Attributes");
843 p8 = bag->value.keybag;
844 if (!(pkey = EVP_PKCS82PKEY(p8)))
846 print_attribs(out, p8->attributes, "Key Attributes");
847 PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
851 case NID_pkcs8ShroudedKeyBag:
852 if (options & INFO) {
853 BIO_printf(bio_err, "Shrouded Keybag: ");
854 alg_print(bio_err, bag->value.shkeybag->algor);
856 if (options & NOKEYS)
858 print_attribs(out, bag->attrib, "Bag Attributes");
859 if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
861 if (!(pkey = EVP_PKCS82PKEY(p8))) {
862 PKCS8_PRIV_KEY_INFO_free(p8);
865 print_attribs(out, p8->attributes, "Key Attributes");
866 PKCS8_PRIV_KEY_INFO_free(p8);
867 PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
873 BIO_printf(bio_err, "Certificate bag\n");
874 if (options & NOCERTS)
876 if (PKCS12_get_attr(bag, NID_localKeyID)) {
877 if (options & CACERTS)
879 } else if (options & CLCERTS)
881 print_attribs(out, bag->attrib, "Bag Attributes");
882 if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
884 if (!(x509 = PKCS12_certbag2x509(bag)))
886 dump_cert_text(out, x509);
887 PEM_write_bio_X509(out, x509);
891 case NID_safeContentsBag:
893 BIO_printf(bio_err, "Safe Contents bag\n");
894 print_attribs(out, bag->attrib, "Bag Attributes");
895 return dump_certs_pkeys_bags(out, bag->value.safes, pass,
896 passlen, options, pempass);
899 BIO_printf(bio_err, "Warning unsupported bag type: ");
900 i2a_ASN1_OBJECT(bio_err, bag->type);
901 BIO_printf(bio_err, "\n");
908 /* Given a single certificate return a verified chain or NULL if error */
910 static int get_cert_chain(X509 *cert, X509_STORE *store,
911 STACK_OF(X509) **chain)
913 X509_STORE_CTX store_ctx;
914 STACK_OF(X509) *chn = NULL;
917 if (!X509_STORE_CTX_init(&store_ctx, store, cert, NULL)) {
919 return 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;
927 X509_STORE_CTX_cleanup(&store_ctx);
932 int alg_print(BIO *x, X509_ALGOR *alg)
935 const unsigned char *p;
936 p = alg->parameter->value.sequence->data;
937 pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
940 BIO_printf(bio_err, "%s, Iteration %ld\n",
941 OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
942 ASN1_INTEGER_get(pbe->iter));
947 /* Load all certificates from a given file */
949 int cert_load(BIO *in, STACK_OF(X509) *sk)
954 # ifdef CRYPTO_MDEBUG
955 CRYPTO_push_info("cert_load(): reading one cert");
957 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
958 # ifdef CRYPTO_MDEBUG
962 sk_X509_push(sk, cert);
963 # ifdef CRYPTO_MDEBUG
964 CRYPTO_push_info("cert_load(): reading one cert");
967 # ifdef CRYPTO_MDEBUG
975 /* Generalised attribute print: handle PKCS#8 and bag attributes */
977 int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
980 X509_ATTRIBUTE *attr;
985 BIO_printf(out, "%s: <No Attributes>\n", name);
988 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
989 BIO_printf(out, "%s: <Empty Attributes>\n", name);
992 BIO_printf(out, "%s\n", name);
993 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
994 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
995 attr_nid = OBJ_obj2nid(attr->object);
996 BIO_printf(out, " ");
997 if (attr_nid == NID_undef) {
998 i2a_ASN1_OBJECT(out, attr->object);
999 BIO_printf(out, ": ");
1001 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1003 if (sk_ASN1_TYPE_num(attr->value.set)) {
1004 av = sk_ASN1_TYPE_value(attr->value.set, 0);
1006 case V_ASN1_BMPSTRING:
1007 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1008 av->value.bmpstring->length);
1009 BIO_printf(out, "%s\n", value);
1010 OPENSSL_free(value);
1013 case V_ASN1_OCTET_STRING:
1014 hex_prin(out, av->value.octet_string->data,
1015 av->value.octet_string->length);
1016 BIO_printf(out, "\n");
1019 case V_ASN1_BIT_STRING:
1020 hex_prin(out, av->value.bit_string->data,
1021 av->value.bit_string->length);
1022 BIO_printf(out, "\n");
1026 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1030 BIO_printf(out, "<No Values>\n");
1035 void hex_prin(BIO *out, unsigned char *buf, int len)
1038 for (i = 0; i < len; i++)
1039 BIO_printf(out, "%02X ", buf[i]);
1042 static int set_pbe(BIO *err, int *ppbe, const char *str)
1046 if (!strcmp(str, "NONE")) {
1050 *ppbe = OBJ_txt2nid(str);
1051 if (*ppbe == NID_undef) {
1052 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);