sl@0: /* pkcs12.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1) sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #define PROG pkcs12_main sl@0: sl@0: const EVP_CIPHER *enc; sl@0: sl@0: sl@0: #define NOKEYS 0x1 sl@0: #define NOCERTS 0x2 sl@0: #define INFO 0x4 sl@0: #define CLCERTS 0x8 sl@0: #define CACERTS 0x10 sl@0: sl@0: int get_cert_chain (X509 *cert, X509_STORE *store, STACK_OF(X509) **chain); sl@0: int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen, int options, char *pempass); sl@0: int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags, char *pass, sl@0: int passlen, int options, char *pempass); sl@0: int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass, int passlen, int options, char *pempass); sl@0: int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,const char *name); sl@0: void hex_prin(BIO *out, unsigned char *buf, int len); sl@0: int alg_print(BIO *x, X509_ALGOR *alg); sl@0: int cert_load(BIO *in, STACK_OF(X509) *sk); sl@0: sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: ENGINE *e = NULL; sl@0: char *infile=NULL, *outfile=NULL, *keyname = NULL; sl@0: char *certfile=NULL; sl@0: BIO *in=NULL, *out = NULL; sl@0: char **args; sl@0: char *name = NULL; sl@0: char *csp_name = NULL; sl@0: PKCS12 *p12 = NULL; sl@0: char pass[50], macpass[50]; sl@0: int export_cert = 0; sl@0: int options = 0; sl@0: int chain = 0; sl@0: int badarg = 0; sl@0: int iter = PKCS12_DEFAULT_ITER; sl@0: int maciter = PKCS12_DEFAULT_ITER; sl@0: int twopass = 0; sl@0: int keytype = 0; sl@0: int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC; sl@0: int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; sl@0: int ret = 1; sl@0: int macver = 1; sl@0: int noprompt = 0; sl@0: STACK *canames = NULL; sl@0: char *cpass = NULL, *mpass = NULL; sl@0: char *passargin = NULL, *passargout = NULL, *passarg = NULL; sl@0: char *passin = NULL, *passout = NULL; sl@0: char *inrand = NULL; sl@0: char *CApath = NULL, *CAfile = NULL; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine=NULL; sl@0: #endif sl@0: sl@0: apps_startup(); sl@0: sl@0: enc = EVP_des_ede3_cbc(); sl@0: if (bio_err == NULL ) sl@0: bio_err = BIO_new_fp (stderr, BIO_NOCLOSE); sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: sl@0: args = argv + 1; sl@0: sl@0: sl@0: while (*args) { sl@0: if (*args[0] == '-') { sl@0: if (!strcmp (*args, "-nokeys")) options |= NOKEYS; sl@0: else if (!strcmp (*args, "-keyex")) keytype = KEY_EX; sl@0: else if (!strcmp (*args, "-keysig")) keytype = KEY_SIG; sl@0: else if (!strcmp (*args, "-nocerts")) options |= NOCERTS; sl@0: else if (!strcmp (*args, "-clcerts")) options |= CLCERTS; sl@0: else if (!strcmp (*args, "-cacerts")) options |= CACERTS; sl@0: else if (!strcmp (*args, "-noout")) options |= (NOKEYS|NOCERTS); sl@0: else if (!strcmp (*args, "-info")) options |= INFO; sl@0: else if (!strcmp (*args, "-chain")) chain = 1; sl@0: else if (!strcmp (*args, "-twopass")) twopass = 1; sl@0: else if (!strcmp (*args, "-nomacver")) macver = 0; sl@0: else if (!strcmp (*args, "-descert")) sl@0: cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; sl@0: else if (!strcmp (*args, "-export")) export_cert = 1; sl@0: else if (!strcmp (*args, "-des")) enc=EVP_des_cbc(); sl@0: else if (!strcmp (*args, "-des3")) enc = EVP_des_ede3_cbc(); sl@0: #ifndef OPENSSL_NO_IDEA sl@0: else if (!strcmp (*args, "-idea")) enc=EVP_idea_cbc(); sl@0: #endif sl@0: else if (!strcmp (*args, "-des3")) enc = EVP_des_ede3_cbc(); sl@0: #ifndef OPENSSL_NO_AES sl@0: else if (!strcmp(*args,"-aes128")) enc=EVP_aes_128_cbc(); sl@0: else if (!strcmp(*args,"-aes192")) enc=EVP_aes_192_cbc(); sl@0: else if (!strcmp(*args,"-aes256")) enc=EVP_aes_256_cbc(); sl@0: #endif sl@0: else if (!strcmp (*args, "-noiter")) iter = 1; sl@0: else if (!strcmp (*args, "-maciter")) sl@0: maciter = PKCS12_DEFAULT_ITER; sl@0: else if (!strcmp (*args, "-nomaciter")) sl@0: maciter = 1; sl@0: else if (!strcmp (*args, "-nomac")) sl@0: maciter = -1; sl@0: else if (!strcmp (*args, "-nodes")) enc=NULL; sl@0: else if (!strcmp (*args, "-certpbe")) { sl@0: if (args[1]) { sl@0: args++; sl@0: if (!strcmp(*args, "NONE")) sl@0: cert_pbe = -1; sl@0: else sl@0: cert_pbe=OBJ_txt2nid(*args); sl@0: if(cert_pbe == NID_undef) { sl@0: BIO_printf(bio_err, sl@0: "Unknown PBE algorithm %s\n", *args); sl@0: badarg = 1; sl@0: } sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-keypbe")) { sl@0: if (args[1]) { sl@0: args++; sl@0: if (!strcmp(*args, "NONE")) sl@0: key_pbe = -1; sl@0: else sl@0: key_pbe=OBJ_txt2nid(*args); sl@0: if(key_pbe == NID_undef) { sl@0: BIO_printf(bio_err, sl@0: "Unknown PBE algorithm %s\n", *args); sl@0: badarg = 1; sl@0: } sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-rand")) { sl@0: if (args[1]) { sl@0: args++; sl@0: inrand = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-inkey")) { sl@0: if (args[1]) { sl@0: args++; sl@0: keyname = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-certfile")) { sl@0: if (args[1]) { sl@0: args++; sl@0: certfile = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-name")) { sl@0: if (args[1]) { sl@0: args++; sl@0: name = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-CSP")) { sl@0: if (args[1]) { sl@0: args++; sl@0: csp_name = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-caname")) { sl@0: if (args[1]) { sl@0: args++; sl@0: if (!canames) canames = sk_new_null(); sl@0: sk_push(canames, *args); sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-in")) { sl@0: if (args[1]) { sl@0: args++; sl@0: infile = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-out")) { sl@0: if (args[1]) { sl@0: args++; sl@0: outfile = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp(*args,"-passin")) { sl@0: if (args[1]) { sl@0: args++; sl@0: passargin = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp(*args,"-passout")) { sl@0: if (args[1]) { sl@0: args++; sl@0: passargout = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp (*args, "-password")) { sl@0: if (args[1]) { sl@0: args++; sl@0: passarg = *args; sl@0: noprompt = 1; sl@0: } else badarg = 1; sl@0: } else if (!strcmp(*args,"-CApath")) { sl@0: if (args[1]) { sl@0: args++; sl@0: CApath = *args; sl@0: } else badarg = 1; sl@0: } else if (!strcmp(*args,"-CAfile")) { sl@0: if (args[1]) { sl@0: args++; sl@0: CAfile = *args; sl@0: } else badarg = 1; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: } else if (!strcmp(*args,"-engine")) { sl@0: if (args[1]) { sl@0: args++; sl@0: engine = *args; sl@0: } else badarg = 1; sl@0: #endif sl@0: } else badarg = 1; sl@0: sl@0: } else badarg = 1; sl@0: args++; sl@0: } sl@0: sl@0: if (badarg) { sl@0: BIO_printf (bio_err, "Usage: pkcs12 [options]\n"); sl@0: BIO_printf (bio_err, "where options are\n"); sl@0: BIO_printf (bio_err, "-export output PKCS12 file\n"); sl@0: BIO_printf (bio_err, "-chain add certificate chain\n"); sl@0: BIO_printf (bio_err, "-inkey file private key if not infile\n"); sl@0: BIO_printf (bio_err, "-certfile f add all certs in f\n"); sl@0: BIO_printf (bio_err, "-CApath arg - PEM format directory of CA's\n"); sl@0: BIO_printf (bio_err, "-CAfile arg - PEM format file of CA's\n"); sl@0: BIO_printf (bio_err, "-name \"name\" use name as friendly name\n"); sl@0: BIO_printf (bio_err, "-caname \"nm\" use nm as CA friendly name (can be used more than once).\n"); sl@0: BIO_printf (bio_err, "-in infile input filename\n"); sl@0: BIO_printf (bio_err, "-out outfile output filename\n"); sl@0: BIO_printf (bio_err, "-noout don't output anything, just verify.\n"); sl@0: BIO_printf (bio_err, "-nomacver don't verify MAC.\n"); sl@0: BIO_printf (bio_err, "-nocerts don't output certificates.\n"); sl@0: BIO_printf (bio_err, "-clcerts only output client certificates.\n"); sl@0: BIO_printf (bio_err, "-cacerts only output CA certificates.\n"); sl@0: BIO_printf (bio_err, "-nokeys don't output private keys.\n"); sl@0: BIO_printf (bio_err, "-info give info about PKCS#12 structure.\n"); sl@0: BIO_printf (bio_err, "-des encrypt private keys with DES\n"); sl@0: BIO_printf (bio_err, "-des3 encrypt private keys with triple DES (default)\n"); sl@0: #ifndef OPENSSL_NO_IDEA sl@0: BIO_printf (bio_err, "-idea encrypt private keys with idea\n"); sl@0: #endif sl@0: #ifndef OPENSSL_NO_AES sl@0: BIO_printf (bio_err, "-aes128, -aes192, -aes256\n"); sl@0: BIO_printf (bio_err, " encrypt PEM output with cbc aes\n"); sl@0: #endif sl@0: BIO_printf (bio_err, "-nodes don't encrypt private keys\n"); sl@0: BIO_printf (bio_err, "-noiter don't use encryption iteration\n"); sl@0: BIO_printf (bio_err, "-maciter use MAC iteration\n"); sl@0: BIO_printf (bio_err, "-twopass separate MAC, encryption passwords\n"); sl@0: BIO_printf (bio_err, "-descert encrypt PKCS#12 certificates with triple DES (default RC2-40)\n"); sl@0: BIO_printf (bio_err, "-certpbe alg specify certificate PBE algorithm (default RC2-40)\n"); sl@0: BIO_printf (bio_err, "-keypbe alg specify private key PBE algorithm (default 3DES)\n"); sl@0: BIO_printf (bio_err, "-keyex set MS key exchange type\n"); sl@0: BIO_printf (bio_err, "-keysig set MS key signature type\n"); sl@0: BIO_printf (bio_err, "-password p set import/export password source\n"); sl@0: BIO_printf (bio_err, "-passin p input file pass phrase source\n"); sl@0: BIO_printf (bio_err, "-passout p output file pass phrase source\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n"); sl@0: #endif sl@0: BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); sl@0: BIO_printf(bio_err, " load the file (or the files in the directory) into\n"); sl@0: BIO_printf(bio_err, " the random number generator\n"); sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine, 0); sl@0: #endif sl@0: sl@0: if(passarg) { sl@0: if(export_cert) passargout = passarg; sl@0: else passargin = passarg; sl@0: } sl@0: sl@0: if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { sl@0: BIO_printf(bio_err, "Error getting passwords\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if(!cpass) { sl@0: if(export_cert) cpass = passout; sl@0: else cpass = passin; sl@0: } sl@0: sl@0: if(cpass) { sl@0: mpass = cpass; sl@0: noprompt = 1; sl@0: } else { sl@0: cpass = pass; sl@0: mpass = macpass; sl@0: } sl@0: sl@0: if(export_cert || inrand) { sl@0: app_RAND_load_file(NULL, bio_err, (inrand != NULL)); sl@0: if (inrand != NULL) sl@0: BIO_printf(bio_err,"%ld semi-random bytes loaded\n", sl@0: app_RAND_load_files(inrand)); sl@0: } sl@0: ERR_load_crypto_strings(); sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("read files"); sl@0: #endif sl@0: if (!infile) in = BIO_new_fp(stdin, BIO_NOCLOSE); sl@0: else in = BIO_new_file(infile, "rb"); sl@0: if (!in) { sl@0: BIO_printf(bio_err, "Error opening input file %s\n", sl@0: infile ? infile : ""); sl@0: perror (infile); sl@0: goto end; sl@0: } sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("write files"); sl@0: #endif sl@0: sl@0: if (!outfile) { sl@0: out = BIO_new_fp(stdout, BIO_NOCLOSE); sl@0: sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: } else out = BIO_new_file(outfile, "wb"); sl@0: if (!out) { sl@0: BIO_printf(bio_err, "Error opening output file %s\n", sl@0: outfile ? outfile : ""); sl@0: perror (outfile); sl@0: goto end; sl@0: } sl@0: if (twopass) { sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("read MAC password"); sl@0: #endif sl@0: if(EVP_read_pw_string (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) sl@0: { sl@0: BIO_printf (bio_err, "Can't read Password\n"); sl@0: goto end; sl@0: } sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: } sl@0: sl@0: if (export_cert) { sl@0: EVP_PKEY *key = NULL; sl@0: X509 *ucert = NULL, *x = NULL; sl@0: STACK_OF(X509) *certs=NULL; sl@0: unsigned char *catmp = NULL; sl@0: int i; sl@0: sl@0: if ((options & (NOCERTS|NOKEYS)) == (NOCERTS|NOKEYS)) sl@0: { sl@0: BIO_printf(bio_err, "Nothing to do!\n"); sl@0: goto export_end; sl@0: } sl@0: sl@0: if (options & NOCERTS) sl@0: chain = 0; sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("process -export_cert"); sl@0: CRYPTO_push_info("reading private key"); sl@0: #endif sl@0: if (!(options & NOKEYS)) sl@0: { sl@0: key = load_key(bio_err, keyname ? keyname : infile, sl@0: FORMAT_PEM, 1, passin, e, "private key"); sl@0: if (!key) sl@0: goto export_end; sl@0: } sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("reading certs from input"); sl@0: #endif sl@0: sl@0: /* Load in all certs in input file */ sl@0: if(!(options & NOCERTS)) sl@0: { sl@0: certs = load_certs(bio_err, infile, FORMAT_PEM, NULL, e, sl@0: "certificates"); sl@0: if (!certs) sl@0: goto export_end; sl@0: sl@0: if (key) sl@0: { sl@0: /* Look for matching private key */ sl@0: for(i = 0; i < sk_X509_num(certs); i++) sl@0: { sl@0: x = sk_X509_value(certs, i); sl@0: if(X509_check_private_key(x, key)) sl@0: { sl@0: ucert = x; sl@0: /* Zero keyid and alias */ sl@0: X509_keyid_set1(ucert, NULL, 0); sl@0: X509_alias_set1(ucert, NULL, 0); sl@0: /* Remove from list */ sl@0: (void)sk_X509_delete(certs, i); sl@0: break; sl@0: } sl@0: } sl@0: if (!ucert) sl@0: { sl@0: BIO_printf(bio_err, "No certificate matches private key\n"); sl@0: goto export_end; sl@0: } sl@0: } sl@0: sl@0: } sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("reading certs from input 2"); sl@0: #endif sl@0: sl@0: /* Add any more certificates asked for */ sl@0: if(certfile) sl@0: { sl@0: STACK_OF(X509) *morecerts=NULL; sl@0: if(!(morecerts = load_certs(bio_err, certfile, FORMAT_PEM, sl@0: NULL, e, sl@0: "certificates from certfile"))) sl@0: goto export_end; sl@0: while(sk_X509_num(morecerts) > 0) sl@0: sk_X509_push(certs, sk_X509_shift(morecerts)); sl@0: sk_X509_free(morecerts); sl@0: } sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("reading certs from certfile"); sl@0: #endif sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("building chain"); sl@0: #endif sl@0: sl@0: /* If chaining get chain from user cert */ sl@0: if (chain) { sl@0: int vret; sl@0: STACK_OF(X509) *chain2; sl@0: X509_STORE *store = X509_STORE_new(); sl@0: if (!store) sl@0: { sl@0: BIO_printf (bio_err, "Memory allocation error\n"); sl@0: goto export_end; sl@0: } sl@0: if (!X509_STORE_load_locations(store, CAfile, CApath)) sl@0: X509_STORE_set_default_paths (store); sl@0: sl@0: vret = get_cert_chain (ucert, store, &chain2); sl@0: X509_STORE_free(store); sl@0: sl@0: if (!vret) { sl@0: /* Exclude verified certificate */ sl@0: for (i = 1; i < sk_X509_num (chain2) ; i++) sl@0: sk_X509_push(certs, sk_X509_value (chain2, i)); sl@0: /* Free first certificate */ sl@0: X509_free(sk_X509_value(chain2, 0)); sl@0: sk_X509_free(chain2); sl@0: } else { sl@0: if (vret >= 0) sl@0: BIO_printf (bio_err, "Error %s getting chain.\n", sl@0: X509_verify_cert_error_string(vret)); sl@0: else sl@0: ERR_print_errors(bio_err); sl@0: goto export_end; sl@0: } sl@0: } sl@0: sl@0: /* Add any CA names */ sl@0: sl@0: for (i = 0; i < sk_num(canames); i++) sl@0: { sl@0: catmp = (unsigned char *)sk_value(canames, i); sl@0: X509_alias_set1(sk_X509_value(certs, i), catmp, -1); sl@0: } sl@0: sl@0: if (csp_name && key) sl@0: EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name, sl@0: MBSTRING_ASC, (unsigned char *)csp_name, -1); sl@0: sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("reading password"); sl@0: #endif sl@0: sl@0: if(!noprompt && sl@0: EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:", 1)) sl@0: { sl@0: BIO_printf (bio_err, "Can't read Password\n"); sl@0: goto export_end; sl@0: } sl@0: if (!twopass) BUF_strlcpy(macpass, pass, sizeof macpass); sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("creating PKCS#12 structure"); sl@0: #endif sl@0: sl@0: p12 = PKCS12_create(cpass, name, key, ucert, certs, sl@0: key_pbe, cert_pbe, iter, -1, keytype); sl@0: sl@0: if (!p12) sl@0: { sl@0: ERR_print_errors (bio_err); sl@0: goto export_end; sl@0: } sl@0: sl@0: if (maciter != -1) sl@0: PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, NULL); sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("writing pkcs12"); sl@0: #endif sl@0: sl@0: i2d_PKCS12_bio(out, p12); sl@0: sl@0: ret = 0; sl@0: sl@0: export_end: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_pop_info(); sl@0: CRYPTO_push_info("process -export_cert: freeing"); sl@0: #endif sl@0: sl@0: if (key) EVP_PKEY_free(key); sl@0: if (certs) sk_X509_pop_free(certs, X509_free); sl@0: if (ucert) X509_free(ucert); sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: goto end; sl@0: sl@0: } sl@0: sl@0: if (!(p12 = d2i_PKCS12_bio (in, NULL))) { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("read import password"); sl@0: #endif sl@0: if(!noprompt && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:", 0)) { sl@0: BIO_printf (bio_err, "Can't read Password\n"); sl@0: goto end; sl@0: } sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: sl@0: if (!twopass) BUF_strlcpy(macpass, pass, sizeof macpass); sl@0: sl@0: if (options & INFO) BIO_printf (bio_err, "MAC Iteration %ld\n", p12->mac->iter ? ASN1_INTEGER_get (p12->mac->iter) : 1); sl@0: if(macver) { sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("verify MAC"); sl@0: #endif sl@0: /* If we enter empty password try no password first */ sl@0: if(!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) { sl@0: /* If mac and crypto pass the same set it to NULL too */ sl@0: if(!twopass) cpass = NULL; sl@0: } else if (!PKCS12_verify_mac(p12, mpass, -1)) { sl@0: BIO_printf (bio_err, "Mac verify error: invalid password?\n"); sl@0: ERR_print_errors (bio_err); sl@0: goto end; sl@0: } sl@0: BIO_printf (bio_err, "MAC verified OK\n"); sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: } sl@0: sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("output keys and certificates"); sl@0: #endif sl@0: if (!dump_certs_keys_p12 (out, p12, cpass, -1, options, passout)) { sl@0: BIO_printf(bio_err, "Error outputting keys and certificates\n"); sl@0: ERR_print_errors (bio_err); sl@0: goto end; sl@0: } sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: ret = 0; sl@0: end: sl@0: if (p12) PKCS12_free(p12); sl@0: if(export_cert || inrand) app_RAND_write_file(NULL, bio_err); sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_remove_all_info(); sl@0: #endif sl@0: BIO_free(in); sl@0: BIO_free_all(out); sl@0: if (canames) sk_free(canames); sl@0: if(passin) OPENSSL_free(passin); sl@0: if(passout) OPENSSL_free(passout); sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: int dump_certs_keys_p12 (BIO *out, PKCS12 *p12, char *pass, sl@0: int passlen, int options, char *pempass) sl@0: { sl@0: STACK_OF(PKCS7) *asafes = NULL; sl@0: STACK_OF(PKCS12_SAFEBAG) *bags; sl@0: int i, bagnid; sl@0: int ret = 0; sl@0: PKCS7 *p7; sl@0: sl@0: if (!( asafes = PKCS12_unpack_authsafes(p12))) return 0; sl@0: for (i = 0; i < sk_PKCS7_num (asafes); i++) { sl@0: p7 = sk_PKCS7_value (asafes, i); sl@0: bagnid = OBJ_obj2nid (p7->type); sl@0: if (bagnid == NID_pkcs7_data) { sl@0: bags = PKCS12_unpack_p7data(p7); sl@0: if (options & INFO) BIO_printf (bio_err, "PKCS7 Data\n"); sl@0: } else if (bagnid == NID_pkcs7_encrypted) { sl@0: if (options & INFO) { sl@0: BIO_printf(bio_err, "PKCS7 Encrypted data: "); sl@0: alg_print(bio_err, sl@0: p7->d.encrypted->enc_data->algorithm); sl@0: } sl@0: bags = PKCS12_unpack_p7encdata(p7, pass, passlen); sl@0: } else continue; sl@0: if (!bags) goto err; sl@0: if (!dump_certs_pkeys_bags (out, bags, pass, passlen, sl@0: options, pempass)) { sl@0: sk_PKCS12_SAFEBAG_pop_free (bags, PKCS12_SAFEBAG_free); sl@0: goto err; sl@0: } sl@0: sk_PKCS12_SAFEBAG_pop_free (bags, PKCS12_SAFEBAG_free); sl@0: bags = NULL; sl@0: } sl@0: ret = 1; sl@0: sl@0: err: sl@0: sl@0: if (asafes) sl@0: sk_PKCS7_pop_free (asafes, PKCS7_free); sl@0: return ret; sl@0: } sl@0: sl@0: int dump_certs_pkeys_bags (BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags, sl@0: char *pass, int passlen, int options, char *pempass) sl@0: { sl@0: int i; sl@0: for (i = 0; i < sk_PKCS12_SAFEBAG_num (bags); i++) { sl@0: if (!dump_certs_pkeys_bag (out, sl@0: sk_PKCS12_SAFEBAG_value (bags, i), sl@0: pass, passlen, sl@0: options, pempass)) sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: int dump_certs_pkeys_bag (BIO *out, PKCS12_SAFEBAG *bag, char *pass, sl@0: int passlen, int options, char *pempass) sl@0: { sl@0: EVP_PKEY *pkey; sl@0: PKCS8_PRIV_KEY_INFO *p8; sl@0: X509 *x509; sl@0: sl@0: switch (M_PKCS12_bag_type(bag)) sl@0: { sl@0: case NID_keyBag: sl@0: if (options & INFO) BIO_printf (bio_err, "Key bag\n"); sl@0: if (options & NOKEYS) return 1; sl@0: print_attribs (out, bag->attrib, "Bag Attributes"); sl@0: p8 = bag->value.keybag; sl@0: if (!(pkey = EVP_PKCS82PKEY (p8))) return 0; sl@0: print_attribs (out, p8->attributes, "Key Attributes"); sl@0: PEM_write_bio_PrivateKey (out, pkey, enc, NULL, 0, NULL, pempass); sl@0: EVP_PKEY_free(pkey); sl@0: break; sl@0: sl@0: case NID_pkcs8ShroudedKeyBag: sl@0: if (options & INFO) { sl@0: BIO_printf (bio_err, "Shrouded Keybag: "); sl@0: alg_print (bio_err, bag->value.shkeybag->algor); sl@0: } sl@0: if (options & NOKEYS) return 1; sl@0: print_attribs (out, bag->attrib, "Bag Attributes"); sl@0: if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen))) sl@0: return 0; sl@0: if (!(pkey = EVP_PKCS82PKEY (p8))) { sl@0: PKCS8_PRIV_KEY_INFO_free(p8); sl@0: return 0; sl@0: } sl@0: print_attribs (out, p8->attributes, "Key Attributes"); sl@0: PKCS8_PRIV_KEY_INFO_free(p8); sl@0: PEM_write_bio_PrivateKey (out, pkey, enc, NULL, 0, NULL, pempass); sl@0: EVP_PKEY_free(pkey); sl@0: break; sl@0: sl@0: case NID_certBag: sl@0: if (options & INFO) BIO_printf (bio_err, "Certificate bag\n"); sl@0: if (options & NOCERTS) return 1; sl@0: if (PKCS12_get_attr(bag, NID_localKeyID)) { sl@0: if (options & CACERTS) return 1; sl@0: } else if (options & CLCERTS) return 1; sl@0: print_attribs (out, bag->attrib, "Bag Attributes"); sl@0: if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate ) sl@0: return 1; sl@0: if (!(x509 = PKCS12_certbag2x509(bag))) return 0; sl@0: dump_cert_text (out, x509); sl@0: PEM_write_bio_X509 (out, x509); sl@0: X509_free(x509); sl@0: break; sl@0: sl@0: case NID_safeContentsBag: sl@0: if (options & INFO) BIO_printf (bio_err, "Safe Contents bag\n"); sl@0: print_attribs (out, bag->attrib, "Bag Attributes"); sl@0: return dump_certs_pkeys_bags (out, bag->value.safes, pass, sl@0: passlen, options, pempass); sl@0: sl@0: default: sl@0: BIO_printf (bio_err, "Warning unsupported bag type: "); sl@0: i2a_ASN1_OBJECT (bio_err, bag->type); sl@0: BIO_printf (bio_err, "\n"); sl@0: return 1; sl@0: break; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: /* Given a single certificate return a verified chain or NULL if error */ sl@0: sl@0: /* Hope this is OK .... */ sl@0: sl@0: int get_cert_chain (X509 *cert, X509_STORE *store, STACK_OF(X509) **chain) sl@0: { sl@0: X509_STORE_CTX store_ctx; sl@0: STACK_OF(X509) *chn; sl@0: int i = 0; sl@0: sl@0: /* FIXME: Should really check the return status of X509_STORE_CTX_init sl@0: * for an error, but how that fits into the return value of this sl@0: * function is less obvious. */ sl@0: X509_STORE_CTX_init(&store_ctx, store, cert, NULL); sl@0: if (X509_verify_cert(&store_ctx) <= 0) { sl@0: i = X509_STORE_CTX_get_error (&store_ctx); sl@0: if (i == 0) sl@0: /* avoid returning 0 if X509_verify_cert() did not sl@0: * set an appropriate error value in the context */ sl@0: i = -1; sl@0: chn = NULL; sl@0: goto err; sl@0: } else sl@0: chn = X509_STORE_CTX_get1_chain(&store_ctx); sl@0: err: sl@0: X509_STORE_CTX_cleanup(&store_ctx); sl@0: *chain = chn; sl@0: sl@0: return i; sl@0: } sl@0: sl@0: int alg_print (BIO *x, X509_ALGOR *alg) sl@0: { sl@0: PBEPARAM *pbe; sl@0: const unsigned char *p; sl@0: p = alg->parameter->value.sequence->data; sl@0: pbe = d2i_PBEPARAM (NULL, &p, alg->parameter->value.sequence->length); sl@0: if (!pbe) sl@0: return 1; sl@0: BIO_printf (bio_err, "%s, Iteration %ld\n", sl@0: OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)), sl@0: ASN1_INTEGER_get(pbe->iter)); sl@0: PBEPARAM_free (pbe); sl@0: return 1; sl@0: } sl@0: sl@0: /* Load all certificates from a given file */ sl@0: sl@0: int cert_load(BIO *in, STACK_OF(X509) *sk) sl@0: { sl@0: int ret; sl@0: X509 *cert; sl@0: ret = 0; sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("cert_load(): reading one cert"); sl@0: #endif sl@0: while((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) { sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: ret = 1; sl@0: sk_X509_push(sk, cert); sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_push_info("cert_load(): reading one cert"); sl@0: #endif sl@0: } sl@0: #ifdef CRYPTO_MDEBUG sl@0: CRYPTO_pop_info(); sl@0: #endif sl@0: if(ret) ERR_clear_error(); sl@0: return ret; sl@0: } sl@0: sl@0: /* Generalised attribute print: handle PKCS#8 and bag attributes */ sl@0: sl@0: int print_attribs (BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,const char *name) sl@0: { sl@0: X509_ATTRIBUTE *attr; sl@0: ASN1_TYPE *av; sl@0: char *value; sl@0: int i, attr_nid; sl@0: if(!attrlst) { sl@0: BIO_printf(out, "%s: \n", name); sl@0: return 1; sl@0: } sl@0: if(!sk_X509_ATTRIBUTE_num(attrlst)) { sl@0: BIO_printf(out, "%s: \n", name); sl@0: return 1; sl@0: } sl@0: BIO_printf(out, "%s\n", name); sl@0: for(i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) { sl@0: attr = sk_X509_ATTRIBUTE_value(attrlst, i); sl@0: attr_nid = OBJ_obj2nid(attr->object); sl@0: BIO_printf(out, " "); sl@0: if(attr_nid == NID_undef) { sl@0: i2a_ASN1_OBJECT (out, attr->object); sl@0: BIO_printf(out, ": "); sl@0: } else BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid)); sl@0: sl@0: if(sk_ASN1_TYPE_num(attr->value.set)) { sl@0: av = sk_ASN1_TYPE_value(attr->value.set, 0); sl@0: switch(av->type) { sl@0: case V_ASN1_BMPSTRING: sl@0: value = uni2asc(av->value.bmpstring->data, sl@0: av->value.bmpstring->length); sl@0: BIO_printf(out, "%s\n", value); sl@0: OPENSSL_free(value); sl@0: break; sl@0: sl@0: case V_ASN1_OCTET_STRING: sl@0: hex_prin(out, av->value.octet_string->data, sl@0: av->value.octet_string->length); sl@0: BIO_printf(out, "\n"); sl@0: break; sl@0: sl@0: case V_ASN1_BIT_STRING: sl@0: hex_prin(out, av->value.bit_string->data, sl@0: av->value.bit_string->length); sl@0: BIO_printf(out, "\n"); sl@0: break; sl@0: sl@0: default: sl@0: BIO_printf(out, "\n", av->type); sl@0: break; sl@0: } sl@0: } else BIO_printf(out, "\n"); sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: void hex_prin(BIO *out, unsigned char *buf, int len) sl@0: { sl@0: int i; sl@0: for (i = 0; i < len; i++) BIO_printf (out, "%02X ", buf[i]); sl@0: } sl@0: sl@0: #endif