sl@0: /* pkcs8.c */
sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
sl@0:  * project 1999-2004.
sl@0:  */
sl@0: /* ====================================================================
sl@0:  * Copyright (c) 1999 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: #include <stdio.h>
sl@0: #include <string.h>
sl@0: #include "apps.h"
sl@0: #include <openssl/pem.h>
sl@0: #include <openssl/err.h>
sl@0: #include <openssl/evp.h>
sl@0: #include <openssl/pkcs12.h>
sl@0: 
sl@0: #define PROG pkcs8_main
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 **args, *infile = NULL, *outfile = NULL;
sl@0: 	char *passargin = NULL, *passargout = NULL;
sl@0: 	BIO *in = NULL, *out = NULL;
sl@0: 	int topk8 = 0;
sl@0: 	int pbe_nid = -1;
sl@0: 	const EVP_CIPHER *cipher = NULL;
sl@0: 	int iter = PKCS12_DEFAULT_ITER;
sl@0: 	int informat, outformat;
sl@0: 	int p8_broken = PKCS8_OK;
sl@0: 	int nocrypt = 0;
sl@0: 	X509_SIG *p8;
sl@0: 	PKCS8_PRIV_KEY_INFO *p8inf;
sl@0: 	EVP_PKEY *pkey=NULL;
sl@0: 	char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL;
sl@0: 	int badarg = 0;
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: 	char *engine=NULL;
sl@0: #endif
sl@0: 
sl@0: 	if (bio_err == NULL) 
sl@0: 	bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
sl@0: 	
sl@0: 
sl@0: 	if (!load_config(bio_err, NULL))
sl@0: 		goto end;
sl@0: 
sl@0: 	informat=FORMAT_PEM;
sl@0: 	outformat=FORMAT_PEM;
sl@0: 
sl@0: 	ERR_load_crypto_strings();
sl@0: 	OpenSSL_add_all_algorithms();
sl@0: 	args = argv + 1;
sl@0: 	while (!badarg && *args && *args[0] == '-')
sl@0: 		{
sl@0: 		if (!strcmp(*args,"-v2"))
sl@0: 			{
sl@0: 			if (args[1])
sl@0: 				{
sl@0: 				args++;
sl@0: 				cipher=EVP_get_cipherbyname(*args);
sl@0: 				if (!cipher)
sl@0: 					{
sl@0: 					BIO_printf(bio_err,
sl@0: 						 "Unknown cipher %s\n", *args);
sl@0: 					badarg = 1;
sl@0: 					}
sl@0: 				}
sl@0: 			else
sl@0: 				badarg = 1;
sl@0: 			}
sl@0: 		else if (!strcmp(*args,"-v1"))
sl@0: 			{
sl@0: 			if (args[1])
sl@0: 				{
sl@0: 				args++;
sl@0: 				pbe_nid=OBJ_txt2nid(*args);
sl@0: 				if (pbe_nid == NID_undef)
sl@0: 					{
sl@0: 					BIO_printf(bio_err,
sl@0: 						 "Unknown PBE algorithm %s\n", *args);
sl@0: 					badarg = 1;
sl@0: 					}
sl@0: 				}
sl@0: 			else
sl@0: 				badarg = 1;
sl@0: 			}
sl@0: 		else if (!strcmp(*args,"-inform"))
sl@0: 			{
sl@0: 			if (args[1])
sl@0: 				{
sl@0: 				args++;
sl@0: 				informat=str2fmt(*args);
sl@0: 				}
sl@0: 			else badarg = 1;
sl@0: 			}
sl@0: 		else if (!strcmp(*args,"-outform"))
sl@0: 			{
sl@0: 			if (args[1])
sl@0: 				{
sl@0: 				args++;
sl@0: 				outformat=str2fmt(*args);
sl@0: 				}
sl@0: 			else badarg = 1;
sl@0: 			}
sl@0: 		else if (!strcmp (*args, "-topk8"))
sl@0: 			topk8 = 1;
sl@0: 		else if (!strcmp (*args, "-noiter"))
sl@0: 			iter = 1;
sl@0: 		else if (!strcmp (*args, "-nocrypt"))
sl@0: 			nocrypt = 1;
sl@0: 		else if (!strcmp (*args, "-nooct"))
sl@0: 			p8_broken = PKCS8_NO_OCTET;
sl@0: 		else if (!strcmp (*args, "-nsdb"))
sl@0: 			p8_broken = PKCS8_NS_DB;
sl@0: 		else if (!strcmp (*args, "-embed"))
sl@0: 			p8_broken = PKCS8_EMBEDDED_PARAM;
sl@0: 		else if (!strcmp(*args,"-passin"))
sl@0: 			{
sl@0: 			if (!args[1]) goto bad;
sl@0: 			passargin= *(++args);
sl@0: 			}
sl@0: 		else if (!strcmp(*args,"-passout"))
sl@0: 			{
sl@0: 			if (!args[1]) goto bad;
sl@0: 			passargout= *(++args);
sl@0: 			}
sl@0: #ifndef OPENSSL_NO_ENGINE
sl@0: 		else if (strcmp(*args,"-engine") == 0)
sl@0: 			{
sl@0: 			if (!args[1]) goto bad;
sl@0: 			engine= *(++args);
sl@0: 			}
sl@0: #endif
sl@0: 		else if (!strcmp (*args, "-in"))
sl@0: 			{
sl@0: 			if (args[1])
sl@0: 				{
sl@0: 				args++;
sl@0: 				infile = *args;
sl@0: 				}
sl@0: 			else badarg = 1;
sl@0: 			}
sl@0: 		else if (!strcmp (*args, "-out"))
sl@0: 			{
sl@0: 			if (args[1])
sl@0: 				{
sl@0: 				args++;
sl@0: 				outfile = *args;
sl@0: 				}
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: 		{
sl@0: 		bad:
sl@0: 		BIO_printf(bio_err, "Usage pkcs8 [options]\n");
sl@0: 		BIO_printf(bio_err, "where options are\n");
sl@0: 		BIO_printf(bio_err, "-in file        input file\n");
sl@0: 		BIO_printf(bio_err, "-inform X       input format (DER or PEM)\n");
sl@0: 		BIO_printf(bio_err, "-passin arg     input file pass phrase source\n");
sl@0: 		BIO_printf(bio_err, "-outform X      output format (DER or PEM)\n");
sl@0: 		BIO_printf(bio_err, "-out file       output file\n");
sl@0: 		BIO_printf(bio_err, "-passout arg    output file pass phrase source\n");
sl@0: 		BIO_printf(bio_err, "-topk8          output PKCS8 file\n");
sl@0: 		BIO_printf(bio_err, "-nooct          use (nonstandard) no octet format\n");
sl@0: 		BIO_printf(bio_err, "-embed          use (nonstandard) embedded DSA parameters format\n");
sl@0: 		BIO_printf(bio_err, "-nsdb           use (nonstandard) DSA Netscape DB format\n");
sl@0: 		BIO_printf(bio_err, "-noiter         use 1 as iteration count\n");
sl@0: 		BIO_printf(bio_err, "-nocrypt        use or expect unencrypted private key\n");
sl@0: 		BIO_printf(bio_err, "-v2 alg         use PKCS#5 v2.0 and cipher \"alg\"\n");
sl@0: 		BIO_printf(bio_err, "-v1 obj         use PKCS#5 v1.5 and cipher \"alg\"\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: 		return 1;
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 (!app_passwd(bio_err, passargin, passargout, &passin, &passout))
sl@0: 		{
sl@0: 		BIO_printf(bio_err, "Error getting passwords\n");
sl@0: 		return 1;
sl@0: 		}
sl@0: 
sl@0: 	if ((pbe_nid == -1) && !cipher)
sl@0: 		pbe_nid = NID_pbeWithMD5AndDES_CBC;
sl@0: 
sl@0: 	if (infile)
sl@0: 		{
sl@0: 		if (!(in = BIO_new_file(infile, "rb")))
sl@0: 			{
sl@0: 			BIO_printf(bio_err,
sl@0: 				 "Can't open input file %s\n", infile);
sl@0: 			return (1);
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		in = BIO_new_fp (stdin, BIO_NOCLOSE);
sl@0: 	if (outfile)
sl@0: 		{
sl@0: 		if (!(out = BIO_new_file (outfile, "wb")))
sl@0: 			{
sl@0: 			BIO_printf(bio_err,
sl@0: 				 "Can't open output file %s\n", outfile);
sl@0: 			return (1);
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		out = BIO_new_fp (stdout, BIO_NOCLOSE);
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: 		}
sl@0: 	if (topk8)
sl@0: 		{
sl@0: 		BIO_free(in); /* Not needed in this section */
sl@0: 		pkey = load_key(bio_err, infile, informat, 1,
sl@0: 			passin, e, "key");
sl@0: 		if (!pkey)
sl@0: 			{
sl@0: 			BIO_free_all(out);
sl@0: 			return 1;
sl@0: 			}
sl@0: 		if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken)))
sl@0: 			{
sl@0: 			BIO_printf(bio_err, "Error converting key\n");
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			EVP_PKEY_free(pkey);
sl@0: 			BIO_free_all(out);
sl@0: 			return 1;
sl@0: 			}
sl@0: 		if (nocrypt)
sl@0: 			{
sl@0: 			if (outformat == FORMAT_PEM) 
sl@0: 				PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
sl@0: 			else if (outformat == FORMAT_ASN1)
sl@0: 				i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
sl@0: 			else
sl@0: 				{
sl@0: 				BIO_printf(bio_err, "Bad format specified for key\n");
sl@0: 				PKCS8_PRIV_KEY_INFO_free(p8inf);
sl@0: 				EVP_PKEY_free(pkey);
sl@0: 				BIO_free_all(out);
sl@0: 				return (1);
sl@0: 				}
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			if (passout)
sl@0: 				p8pass = passout;
sl@0: 			else
sl@0: 				{
sl@0: 				p8pass = pass;
sl@0: 				if (EVP_read_pw_string(pass, sizeof pass, "Enter Encryption Password:", 1))
sl@0: 					{
sl@0: 					PKCS8_PRIV_KEY_INFO_free(p8inf);
sl@0: 					EVP_PKEY_free(pkey);
sl@0: 					BIO_free_all(out);
sl@0: 					return (1);
sl@0: 					}
sl@0: 				}
sl@0: 			app_RAND_load_file(NULL, bio_err, 0);
sl@0: 			if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
sl@0: 					p8pass, strlen(p8pass),
sl@0: 					NULL, 0, iter, p8inf)))
sl@0: 				{
sl@0: 				BIO_printf(bio_err, "Error encrypting key\n");
sl@0: 				ERR_print_errors(bio_err);
sl@0: 				PKCS8_PRIV_KEY_INFO_free(p8inf);
sl@0: 				EVP_PKEY_free(pkey);
sl@0: 				BIO_free_all(out);
sl@0: 				return (1);
sl@0: 				}
sl@0: 			app_RAND_write_file(NULL, bio_err);
sl@0: 			if (outformat == FORMAT_PEM) 
sl@0: 				PEM_write_bio_PKCS8(out, p8);
sl@0: 			else if (outformat == FORMAT_ASN1)
sl@0: 				i2d_PKCS8_bio(out, p8);
sl@0: 			else
sl@0: 				{
sl@0: 				BIO_printf(bio_err, "Bad format specified for key\n");
sl@0: 				PKCS8_PRIV_KEY_INFO_free(p8inf);
sl@0: 				EVP_PKEY_free(pkey);
sl@0: 				BIO_free_all(out);
sl@0: 				return (1);
sl@0: 				}
sl@0: 			X509_SIG_free(p8);
sl@0: 			}
sl@0: 
sl@0: 		PKCS8_PRIV_KEY_INFO_free (p8inf);
sl@0: 		EVP_PKEY_free(pkey);
sl@0: 		BIO_free_all(out);
sl@0: 		if (passin)
sl@0: 			OPENSSL_free(passin);
sl@0: 		if (passout)
sl@0: 			OPENSSL_free(passout);
sl@0: 		return (0);
sl@0: 		}
sl@0: 
sl@0: 	if (nocrypt)
sl@0: 		{
sl@0: 		if (informat == FORMAT_PEM) 
sl@0: 			p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in,NULL,NULL, NULL);
sl@0: 		else if (informat == FORMAT_ASN1)
sl@0: 			p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
sl@0: 		else
sl@0: 			{
sl@0: 			BIO_printf(bio_err, "Bad format specified for key\n");
sl@0: 			return (1);
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		if (informat == FORMAT_PEM) 
sl@0: 			p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
sl@0: 		else if (informat == FORMAT_ASN1)
sl@0: 			p8 = d2i_PKCS8_bio(in, NULL);
sl@0: 		else
sl@0: 			{
sl@0: 			BIO_printf(bio_err, "Bad format specified for key\n");
sl@0: 			return (1);
sl@0: 			}
sl@0: 
sl@0: 		if (!p8)
sl@0: 			{
sl@0: 			BIO_printf (bio_err, "Error reading key\n");
sl@0: 			ERR_print_errors(bio_err);
sl@0: 			return (1);
sl@0: 			}
sl@0: 		if (passin)
sl@0: 			p8pass = passin;
sl@0: 		else
sl@0: 			{
sl@0: 			p8pass = pass;
sl@0: 			EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
sl@0: 			}
sl@0: 		p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
sl@0: 		X509_SIG_free(p8);
sl@0: 		}
sl@0: 
sl@0: 	if (!p8inf)
sl@0: 		{
sl@0: 		BIO_printf(bio_err, "Error decrypting key\n");
sl@0: 		ERR_print_errors(bio_err);
sl@0: 		return (1);
sl@0: 		}
sl@0: 
sl@0: 	if (!(pkey = EVP_PKCS82PKEY(p8inf)))
sl@0: 		{
sl@0: 		BIO_printf(bio_err, "Error converting key\n");
sl@0: 		ERR_print_errors(bio_err);
sl@0: 		return (1);
sl@0: 		}
sl@0: 	
sl@0: 	if (p8inf->broken)
sl@0: 		{
sl@0: 		BIO_printf(bio_err, "Warning: broken key encoding: ");
sl@0: 		switch (p8inf->broken)
sl@0: 			{
sl@0: 			case PKCS8_NO_OCTET:
sl@0: 			BIO_printf(bio_err, "No Octet String in PrivateKey\n");
sl@0: 			break;
sl@0: 
sl@0: 			case PKCS8_EMBEDDED_PARAM:
sl@0: 			BIO_printf(bio_err, "DSA parameters included in PrivateKey\n");
sl@0: 			break;
sl@0: 
sl@0: 			case PKCS8_NS_DB:
sl@0: 			BIO_printf(bio_err, "DSA public key include in PrivateKey\n");
sl@0: 			break;
sl@0: 
sl@0: 			default:
sl@0: 			BIO_printf(bio_err, "Unknown broken type\n");
sl@0: 			break;
sl@0: 		}
sl@0: 	}
sl@0: 	
sl@0: 	PKCS8_PRIV_KEY_INFO_free(p8inf);
sl@0: 	if (outformat == FORMAT_PEM) 
sl@0: 		PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
sl@0: 	else if (outformat == FORMAT_ASN1)
sl@0: 		i2d_PrivateKey_bio(out, pkey);
sl@0: 	else
sl@0: 		{
sl@0: 		BIO_printf(bio_err, "Bad format specified for key\n");
sl@0: 			return (1);
sl@0: 		}
sl@0: 
sl@0: 	end:
sl@0: 	EVP_PKEY_free(pkey);
sl@0: 	BIO_free_all(out);
sl@0: 	BIO_free(in);
sl@0: 	if (passin)
sl@0: 		OPENSSL_free(passin);
sl@0: 	if (passout)
sl@0: 		OPENSSL_free(passout);
sl@0: 
sl@0: 	return (0);
sl@0: 	}