174664626SKris Kennaway /* pkcs8.c */ 274664626SKris Kennaway /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 374664626SKris Kennaway * project 1999. 474664626SKris Kennaway */ 574664626SKris Kennaway /* ==================================================================== 674664626SKris Kennaway * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 774664626SKris Kennaway * 874664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 974664626SKris Kennaway * modification, are permitted provided that the following conditions 1074664626SKris Kennaway * are met: 1174664626SKris Kennaway * 1274664626SKris Kennaway * 1. Redistributions of source code must retain the above copyright 1374664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 1474664626SKris Kennaway * 1574664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 1674664626SKris Kennaway * notice, this list of conditions and the following disclaimer in 1774664626SKris Kennaway * the documentation and/or other materials provided with the 1874664626SKris Kennaway * distribution. 1974664626SKris Kennaway * 2074664626SKris Kennaway * 3. All advertising materials mentioning features or use of this 2174664626SKris Kennaway * software must display the following acknowledgment: 2274664626SKris Kennaway * "This product includes software developed by the OpenSSL Project 2374664626SKris Kennaway * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2474664626SKris Kennaway * 2574664626SKris Kennaway * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2674664626SKris Kennaway * endorse or promote products derived from this software without 2774664626SKris Kennaway * prior written permission. For written permission, please contact 2874664626SKris Kennaway * licensing@OpenSSL.org. 2974664626SKris Kennaway * 3074664626SKris Kennaway * 5. Products derived from this software may not be called "OpenSSL" 3174664626SKris Kennaway * nor may "OpenSSL" appear in their names without prior written 3274664626SKris Kennaway * permission of the OpenSSL Project. 3374664626SKris Kennaway * 3474664626SKris Kennaway * 6. Redistributions of any form whatsoever must retain the following 3574664626SKris Kennaway * acknowledgment: 3674664626SKris Kennaway * "This product includes software developed by the OpenSSL Project 3774664626SKris Kennaway * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3874664626SKris Kennaway * 3974664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4074664626SKris Kennaway * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4174664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4274664626SKris Kennaway * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4374664626SKris Kennaway * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4474664626SKris Kennaway * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4574664626SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4674664626SKris Kennaway * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4774664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4874664626SKris Kennaway * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4974664626SKris Kennaway * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5074664626SKris Kennaway * OF THE POSSIBILITY OF SUCH DAMAGE. 5174664626SKris Kennaway * ==================================================================== 5274664626SKris Kennaway * 5374664626SKris Kennaway * This product includes cryptographic software written by Eric Young 5474664626SKris Kennaway * (eay@cryptsoft.com). This product includes software written by Tim 5574664626SKris Kennaway * Hudson (tjh@cryptsoft.com). 5674664626SKris Kennaway * 5774664626SKris Kennaway */ 5874664626SKris Kennaway #include <stdio.h> 5974664626SKris Kennaway #include <string.h> 60f579bf8eSKris Kennaway #include "apps.h" 6174664626SKris Kennaway #include <openssl/pem.h> 6274664626SKris Kennaway #include <openssl/err.h> 6374664626SKris Kennaway #include <openssl/evp.h> 6474664626SKris Kennaway #include <openssl/pkcs12.h> 6574664626SKris Kennaway 6674664626SKris Kennaway #include "apps.h" 6774664626SKris Kennaway #define PROG pkcs8_main 6874664626SKris Kennaway 69f579bf8eSKris Kennaway int MAIN(int, char **); 7074664626SKris Kennaway 7174664626SKris Kennaway int MAIN(int argc, char **argv) 7274664626SKris Kennaway { 7374664626SKris Kennaway char **args, *infile = NULL, *outfile = NULL; 74f579bf8eSKris Kennaway char *passargin = NULL, *passargout = NULL; 7574664626SKris Kennaway BIO *in = NULL, *out = NULL; 7674664626SKris Kennaway int topk8 = 0; 7774664626SKris Kennaway int pbe_nid = -1; 7874664626SKris Kennaway const EVP_CIPHER *cipher = NULL; 7974664626SKris Kennaway int iter = PKCS12_DEFAULT_ITER; 8074664626SKris Kennaway int informat, outformat; 8174664626SKris Kennaway int p8_broken = PKCS8_OK; 8274664626SKris Kennaway int nocrypt = 0; 8374664626SKris Kennaway X509_SIG *p8; 8474664626SKris Kennaway PKCS8_PRIV_KEY_INFO *p8inf; 8574664626SKris Kennaway EVP_PKEY *pkey; 86f579bf8eSKris Kennaway char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL; 8774664626SKris Kennaway int badarg = 0; 8874664626SKris Kennaway if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE); 8974664626SKris Kennaway informat=FORMAT_PEM; 9074664626SKris Kennaway outformat=FORMAT_PEM; 9174664626SKris Kennaway ERR_load_crypto_strings(); 92f579bf8eSKris Kennaway OpenSSL_add_all_algorithms(); 9374664626SKris Kennaway args = argv + 1; 9474664626SKris Kennaway while (!badarg && *args && *args[0] == '-') { 9574664626SKris Kennaway if (!strcmp(*args,"-v2")) { 9674664626SKris Kennaway if (args[1]) { 9774664626SKris Kennaway args++; 9874664626SKris Kennaway cipher=EVP_get_cipherbyname(*args); 9974664626SKris Kennaway if(!cipher) { 10074664626SKris Kennaway BIO_printf(bio_err, 10174664626SKris Kennaway "Unknown cipher %s\n", *args); 10274664626SKris Kennaway badarg = 1; 10374664626SKris Kennaway } 10474664626SKris Kennaway } else badarg = 1; 105f579bf8eSKris Kennaway } else if (!strcmp(*args,"-v1")) { 106f579bf8eSKris Kennaway if (args[1]) { 107f579bf8eSKris Kennaway args++; 108f579bf8eSKris Kennaway pbe_nid=OBJ_txt2nid(*args); 109f579bf8eSKris Kennaway if(pbe_nid == NID_undef) { 110f579bf8eSKris Kennaway BIO_printf(bio_err, 111f579bf8eSKris Kennaway "Unknown PBE algorithm %s\n", *args); 112f579bf8eSKris Kennaway badarg = 1; 113f579bf8eSKris Kennaway } 114f579bf8eSKris Kennaway } else badarg = 1; 11574664626SKris Kennaway } else if (!strcmp(*args,"-inform")) { 11674664626SKris Kennaway if (args[1]) { 11774664626SKris Kennaway args++; 11874664626SKris Kennaway informat=str2fmt(*args); 11974664626SKris Kennaway } else badarg = 1; 12074664626SKris Kennaway } else if (!strcmp(*args,"-outform")) { 12174664626SKris Kennaway if (args[1]) { 12274664626SKris Kennaway args++; 12374664626SKris Kennaway outformat=str2fmt(*args); 12474664626SKris Kennaway } else badarg = 1; 12574664626SKris Kennaway } else if (!strcmp (*args, "-topk8")) topk8 = 1; 12674664626SKris Kennaway else if (!strcmp (*args, "-noiter")) iter = 1; 12774664626SKris Kennaway else if (!strcmp (*args, "-nocrypt")) nocrypt = 1; 12874664626SKris Kennaway else if (!strcmp (*args, "-nooct")) p8_broken = PKCS8_NO_OCTET; 129f579bf8eSKris Kennaway else if (!strcmp (*args, "-nsdb")) p8_broken = PKCS8_NS_DB; 130f579bf8eSKris Kennaway else if (!strcmp (*args, "-embed")) p8_broken = PKCS8_EMBEDDED_PARAM; 131f579bf8eSKris Kennaway else if (!strcmp(*args,"-passin")) 132f579bf8eSKris Kennaway { 133f579bf8eSKris Kennaway if (!args[1]) goto bad; 134f579bf8eSKris Kennaway passargin= *(++args); 135f579bf8eSKris Kennaway } 136f579bf8eSKris Kennaway else if (!strcmp(*args,"-passout")) 137f579bf8eSKris Kennaway { 138f579bf8eSKris Kennaway if (!args[1]) goto bad; 139f579bf8eSKris Kennaway passargout= *(++args); 140f579bf8eSKris Kennaway } 14174664626SKris Kennaway else if (!strcmp (*args, "-in")) { 14274664626SKris Kennaway if (args[1]) { 14374664626SKris Kennaway args++; 14474664626SKris Kennaway infile = *args; 14574664626SKris Kennaway } else badarg = 1; 14674664626SKris Kennaway } else if (!strcmp (*args, "-out")) { 14774664626SKris Kennaway if (args[1]) { 14874664626SKris Kennaway args++; 14974664626SKris Kennaway outfile = *args; 15074664626SKris Kennaway } else badarg = 1; 15174664626SKris Kennaway } else badarg = 1; 15274664626SKris Kennaway args++; 15374664626SKris Kennaway } 15474664626SKris Kennaway 15574664626SKris Kennaway if (badarg) { 156f579bf8eSKris Kennaway bad: 15774664626SKris Kennaway BIO_printf(bio_err, "Usage pkcs8 [options]\n"); 15874664626SKris Kennaway BIO_printf(bio_err, "where options are\n"); 15974664626SKris Kennaway BIO_printf(bio_err, "-in file input file\n"); 16074664626SKris Kennaway BIO_printf(bio_err, "-inform X input format (DER or PEM)\n"); 161f579bf8eSKris Kennaway BIO_printf(bio_err, "-passin arg input file pass phrase source\n"); 16274664626SKris Kennaway BIO_printf(bio_err, "-outform X output format (DER or PEM)\n"); 16374664626SKris Kennaway BIO_printf(bio_err, "-out file output file\n"); 164f579bf8eSKris Kennaway BIO_printf(bio_err, "-passout arg output file pass phrase source\n"); 16574664626SKris Kennaway BIO_printf(bio_err, "-topk8 output PKCS8 file\n"); 166f579bf8eSKris Kennaway BIO_printf(bio_err, "-nooct use (nonstandard) no octet format\n"); 167f579bf8eSKris Kennaway BIO_printf(bio_err, "-embed use (nonstandard) embedded DSA parameters format\n"); 168f579bf8eSKris Kennaway BIO_printf(bio_err, "-nsdb use (nonstandard) DSA Netscape DB format\n"); 16974664626SKris Kennaway BIO_printf(bio_err, "-noiter use 1 as iteration count\n"); 17074664626SKris Kennaway BIO_printf(bio_err, "-nocrypt use or expect unencrypted private key\n"); 17174664626SKris Kennaway BIO_printf(bio_err, "-v2 alg use PKCS#5 v2.0 and cipher \"alg\"\n"); 172f579bf8eSKris Kennaway BIO_printf(bio_err, "-v1 obj use PKCS#5 v1.5 and cipher \"alg\"\n"); 173f579bf8eSKris Kennaway return (1); 174f579bf8eSKris Kennaway } 175f579bf8eSKris Kennaway 176f579bf8eSKris Kennaway if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { 177f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting passwords\n"); 17874664626SKris Kennaway return (1); 17974664626SKris Kennaway } 18074664626SKris Kennaway 18174664626SKris Kennaway if ((pbe_nid == -1) && !cipher) pbe_nid = NID_pbeWithMD5AndDES_CBC; 18274664626SKris Kennaway 18374664626SKris Kennaway if (infile) { 18474664626SKris Kennaway if (!(in = BIO_new_file(infile, "rb"))) { 18574664626SKris Kennaway BIO_printf(bio_err, 18674664626SKris Kennaway "Can't open input file %s\n", infile); 18774664626SKris Kennaway return (1); 18874664626SKris Kennaway } 18974664626SKris Kennaway } else in = BIO_new_fp (stdin, BIO_NOCLOSE); 19074664626SKris Kennaway 19174664626SKris Kennaway if (outfile) { 19274664626SKris Kennaway if (!(out = BIO_new_file (outfile, "wb"))) { 19374664626SKris Kennaway BIO_printf(bio_err, 19474664626SKris Kennaway "Can't open output file %s\n", outfile); 19574664626SKris Kennaway return (1); 19674664626SKris Kennaway } 19774664626SKris Kennaway } else out = BIO_new_fp (stdout, BIO_NOCLOSE); 19874664626SKris Kennaway 19974664626SKris Kennaway if (topk8) { 200f579bf8eSKris Kennaway if(informat == FORMAT_PEM) 201f579bf8eSKris Kennaway pkey = PEM_read_bio_PrivateKey(in, NULL, NULL, passin); 202f579bf8eSKris Kennaway else if(informat == FORMAT_ASN1) 203f579bf8eSKris Kennaway pkey = d2i_PrivateKey_bio(in, NULL); 204f579bf8eSKris Kennaway else { 205f579bf8eSKris Kennaway BIO_printf(bio_err, "Bad format specified for key\n"); 206f579bf8eSKris Kennaway return (1); 207f579bf8eSKris Kennaway } 208f579bf8eSKris Kennaway if (!pkey) { 20974664626SKris Kennaway BIO_printf(bio_err, "Error reading key\n", outfile); 21074664626SKris Kennaway ERR_print_errors(bio_err); 21174664626SKris Kennaway return (1); 21274664626SKris Kennaway } 21374664626SKris Kennaway BIO_free(in); 214f579bf8eSKris Kennaway if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) { 21574664626SKris Kennaway BIO_printf(bio_err, "Error converting key\n", outfile); 21674664626SKris Kennaway ERR_print_errors(bio_err); 21774664626SKris Kennaway return (1); 21874664626SKris Kennaway } 21974664626SKris Kennaway if(nocrypt) { 22074664626SKris Kennaway if(outformat == FORMAT_PEM) 22174664626SKris Kennaway PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf); 22274664626SKris Kennaway else if(outformat == FORMAT_ASN1) 22374664626SKris Kennaway i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf); 22474664626SKris Kennaway else { 22574664626SKris Kennaway BIO_printf(bio_err, "Bad format specified for key\n"); 22674664626SKris Kennaway return (1); 22774664626SKris Kennaway } 22874664626SKris Kennaway } else { 229f579bf8eSKris Kennaway if(passout) p8pass = passout; 230f579bf8eSKris Kennaway else { 231f579bf8eSKris Kennaway p8pass = pass; 23274664626SKris Kennaway EVP_read_pw_string(pass, 50, "Enter Encryption Password:", 1); 233f579bf8eSKris Kennaway } 234f579bf8eSKris Kennaway app_RAND_load_file(NULL, bio_err, 0); 23574664626SKris Kennaway if (!(p8 = PKCS8_encrypt(pbe_nid, cipher, 236f579bf8eSKris Kennaway p8pass, strlen(p8pass), 23774664626SKris Kennaway NULL, 0, iter, p8inf))) { 23874664626SKris Kennaway BIO_printf(bio_err, "Error encrypting key\n", 23974664626SKris Kennaway outfile); 24074664626SKris Kennaway ERR_print_errors(bio_err); 24174664626SKris Kennaway return (1); 24274664626SKris Kennaway } 243f579bf8eSKris Kennaway app_RAND_write_file(NULL, bio_err); 24474664626SKris Kennaway if(outformat == FORMAT_PEM) 24574664626SKris Kennaway PEM_write_bio_PKCS8(out, p8); 24674664626SKris Kennaway else if(outformat == FORMAT_ASN1) 24774664626SKris Kennaway i2d_PKCS8_bio(out, p8); 24874664626SKris Kennaway else { 24974664626SKris Kennaway BIO_printf(bio_err, "Bad format specified for key\n"); 25074664626SKris Kennaway return (1); 25174664626SKris Kennaway } 25274664626SKris Kennaway X509_SIG_free(p8); 25374664626SKris Kennaway } 25474664626SKris Kennaway PKCS8_PRIV_KEY_INFO_free (p8inf); 25574664626SKris Kennaway EVP_PKEY_free(pkey); 25674664626SKris Kennaway BIO_free(out); 257f579bf8eSKris Kennaway if(passin) Free(passin); 258f579bf8eSKris Kennaway if(passout) Free(passout); 25974664626SKris Kennaway return (0); 26074664626SKris Kennaway } 26174664626SKris Kennaway 26274664626SKris Kennaway if(nocrypt) { 26374664626SKris Kennaway if(informat == FORMAT_PEM) 26474664626SKris Kennaway p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in,NULL,NULL, NULL); 26574664626SKris Kennaway else if(informat == FORMAT_ASN1) 26674664626SKris Kennaway p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL); 26774664626SKris Kennaway else { 26874664626SKris Kennaway BIO_printf(bio_err, "Bad format specified for key\n"); 26974664626SKris Kennaway return (1); 27074664626SKris Kennaway } 27174664626SKris Kennaway } else { 27274664626SKris Kennaway if(informat == FORMAT_PEM) 27374664626SKris Kennaway p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL); 27474664626SKris Kennaway else if(informat == FORMAT_ASN1) 27574664626SKris Kennaway p8 = d2i_PKCS8_bio(in, NULL); 27674664626SKris Kennaway else { 27774664626SKris Kennaway BIO_printf(bio_err, "Bad format specified for key\n"); 27874664626SKris Kennaway return (1); 27974664626SKris Kennaway } 28074664626SKris Kennaway 28174664626SKris Kennaway if (!p8) { 28274664626SKris Kennaway BIO_printf (bio_err, "Error reading key\n", outfile); 28374664626SKris Kennaway ERR_print_errors(bio_err); 28474664626SKris Kennaway return (1); 28574664626SKris Kennaway } 286f579bf8eSKris Kennaway if(passin) p8pass = passin; 287f579bf8eSKris Kennaway else { 288f579bf8eSKris Kennaway p8pass = pass; 28974664626SKris Kennaway EVP_read_pw_string(pass, 50, "Enter Password:", 0); 290f579bf8eSKris Kennaway } 291f579bf8eSKris Kennaway p8inf = M_PKCS8_decrypt(p8, p8pass, strlen(p8pass)); 29274664626SKris Kennaway X509_SIG_free(p8); 29374664626SKris Kennaway } 29474664626SKris Kennaway 29574664626SKris Kennaway if (!p8inf) { 29674664626SKris Kennaway BIO_printf(bio_err, "Error decrypting key\n", outfile); 29774664626SKris Kennaway ERR_print_errors(bio_err); 29874664626SKris Kennaway return (1); 29974664626SKris Kennaway } 30074664626SKris Kennaway 30174664626SKris Kennaway if (!(pkey = EVP_PKCS82PKEY(p8inf))) { 30274664626SKris Kennaway BIO_printf(bio_err, "Error converting key\n", outfile); 30374664626SKris Kennaway ERR_print_errors(bio_err); 30474664626SKris Kennaway return (1); 30574664626SKris Kennaway } 30674664626SKris Kennaway 30774664626SKris Kennaway if (p8inf->broken) { 30874664626SKris Kennaway BIO_printf(bio_err, "Warning: broken key encoding: "); 30974664626SKris Kennaway switch (p8inf->broken) { 31074664626SKris Kennaway case PKCS8_NO_OCTET: 311f579bf8eSKris Kennaway BIO_printf(bio_err, "No Octet String in PrivateKey\n"); 312f579bf8eSKris Kennaway break; 313f579bf8eSKris Kennaway 314f579bf8eSKris Kennaway case PKCS8_EMBEDDED_PARAM: 315f579bf8eSKris Kennaway BIO_printf(bio_err, "DSA parameters included in PrivateKey\n"); 316f579bf8eSKris Kennaway break; 317f579bf8eSKris Kennaway 318f579bf8eSKris Kennaway case PKCS8_NS_DB: 319f579bf8eSKris Kennaway BIO_printf(bio_err, "DSA public key include in PrivateKey\n"); 32074664626SKris Kennaway break; 32174664626SKris Kennaway 32274664626SKris Kennaway default: 32374664626SKris Kennaway BIO_printf(bio_err, "Unknown broken type\n"); 32474664626SKris Kennaway break; 32574664626SKris Kennaway } 32674664626SKris Kennaway } 32774664626SKris Kennaway 32874664626SKris Kennaway PKCS8_PRIV_KEY_INFO_free(p8inf); 329f579bf8eSKris Kennaway if(outformat == FORMAT_PEM) 330f579bf8eSKris Kennaway PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout); 331f579bf8eSKris Kennaway else if(outformat == FORMAT_ASN1) 332f579bf8eSKris Kennaway i2d_PrivateKey_bio(out, pkey); 333f579bf8eSKris Kennaway else { 334f579bf8eSKris Kennaway BIO_printf(bio_err, "Bad format specified for key\n"); 335f579bf8eSKris Kennaway return (1); 336f579bf8eSKris Kennaway } 33774664626SKris Kennaway 33874664626SKris Kennaway EVP_PKEY_free(pkey); 33974664626SKris Kennaway BIO_free(out); 34074664626SKris Kennaway BIO_free(in); 341f579bf8eSKris Kennaway if(passin) Free(passin); 342f579bf8eSKris Kennaway if(passout) Free(passout); 34374664626SKris Kennaway 34474664626SKris Kennaway return (0); 34574664626SKris Kennaway } 346