155714Skris/* apps/rsa.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59160814Ssimon#include <openssl/opensslconf.h>
60109998Smarkm#ifndef OPENSSL_NO_RSA
6155714Skris#include <stdio.h>
6255714Skris#include <stdlib.h>
6355714Skris#include <string.h>
6455714Skris#include <time.h>
6555714Skris#include "apps.h"
6655714Skris#include <openssl/bio.h>
6755714Skris#include <openssl/err.h>
6855714Skris#include <openssl/rsa.h>
6955714Skris#include <openssl/evp.h>
7055714Skris#include <openssl/x509.h>
7155714Skris#include <openssl/pem.h>
72160814Ssimon#include <openssl/bn.h>
7355714Skris
7455714Skris#undef PROG
7555714Skris#define PROG	rsa_main
7655714Skris
7755714Skris/* -inform arg	- input format - default PEM (one of DER, NET or PEM)
7855714Skris * -outform arg - output format - default PEM
7955714Skris * -in arg	- input file - default stdin
8055714Skris * -out arg	- output file - default stdout
8155714Skris * -des		- encrypt output if PEM format with DES in cbc mode
8255714Skris * -des3	- encrypt output if PEM format
8355714Skris * -idea	- encrypt output if PEM format
84194206Ssimon * -seed	- encrypt output if PEM format
85109998Smarkm * -aes128	- encrypt output if PEM format
86109998Smarkm * -aes192	- encrypt output if PEM format
87109998Smarkm * -aes256	- encrypt output if PEM format
88162911Ssimon * -camellia128 - encrypt output if PEM format
89162911Ssimon * -camellia192 - encrypt output if PEM format
90162911Ssimon * -camellia256 - encrypt output if PEM format
9155714Skris * -text	- print a text version
9255714Skris * -modulus	- print the RSA key modulus
9355714Skris * -check	- verify key consistency
9459191Skris * -pubin	- Expect a public key in input file.
9559191Skris * -pubout	- Output a public key.
9655714Skris */
9755714Skris
9859191Skrisint MAIN(int, char **);
9959191Skris
10055714Skrisint MAIN(int argc, char **argv)
10155714Skris	{
102109998Smarkm	ENGINE *e = NULL;
10355714Skris	int ret=1;
10455714Skris	RSA *rsa=NULL;
10568651Skris	int i,badops=0, sgckey=0;
10655714Skris	const EVP_CIPHER *enc=NULL;
107109998Smarkm	BIO *out=NULL;
10855714Skris	int informat,outformat,text=0,check=0,noout=0;
10959191Skris	int pubin = 0, pubout = 0;
11055714Skris	char *infile,*outfile,*prog;
11159191Skris	char *passargin = NULL, *passargout = NULL;
11259191Skris	char *passin = NULL, *passout = NULL;
113111147Snectar#ifndef OPENSSL_NO_ENGINE
114109998Smarkm	char *engine=NULL;
115111147Snectar#endif
11655714Skris	int modulus=0;
11755714Skris
118238405Sjkim	int pvk_encr = 2;
119238405Sjkim
12055714Skris	apps_startup();
12155714Skris
12255714Skris	if (bio_err == NULL)
12355714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
12455714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
12555714Skris
126109998Smarkm	if (!load_config(bio_err, NULL))
127109998Smarkm		goto end;
128109998Smarkm
12955714Skris	infile=NULL;
13055714Skris	outfile=NULL;
13155714Skris	informat=FORMAT_PEM;
13255714Skris	outformat=FORMAT_PEM;
13355714Skris
13455714Skris	prog=argv[0];
13555714Skris	argc--;
13655714Skris	argv++;
13755714Skris	while (argc >= 1)
13855714Skris		{
13955714Skris		if 	(strcmp(*argv,"-inform") == 0)
14055714Skris			{
14155714Skris			if (--argc < 1) goto bad;
14255714Skris			informat=str2fmt(*(++argv));
14355714Skris			}
14455714Skris		else if (strcmp(*argv,"-outform") == 0)
14555714Skris			{
14655714Skris			if (--argc < 1) goto bad;
14755714Skris			outformat=str2fmt(*(++argv));
14855714Skris			}
14955714Skris		else if (strcmp(*argv,"-in") == 0)
15055714Skris			{
15155714Skris			if (--argc < 1) goto bad;
15255714Skris			infile= *(++argv);
15355714Skris			}
15455714Skris		else if (strcmp(*argv,"-out") == 0)
15555714Skris			{
15655714Skris			if (--argc < 1) goto bad;
15755714Skris			outfile= *(++argv);
15855714Skris			}
15959191Skris		else if (strcmp(*argv,"-passin") == 0)
16059191Skris			{
16159191Skris			if (--argc < 1) goto bad;
16259191Skris			passargin= *(++argv);
16359191Skris			}
16459191Skris		else if (strcmp(*argv,"-passout") == 0)
16559191Skris			{
16659191Skris			if (--argc < 1) goto bad;
16759191Skris			passargout= *(++argv);
16859191Skris			}
169111147Snectar#ifndef OPENSSL_NO_ENGINE
170109998Smarkm		else if (strcmp(*argv,"-engine") == 0)
171109998Smarkm			{
172109998Smarkm			if (--argc < 1) goto bad;
173109998Smarkm			engine= *(++argv);
174109998Smarkm			}
175111147Snectar#endif
17668651Skris		else if (strcmp(*argv,"-sgckey") == 0)
17768651Skris			sgckey=1;
17859191Skris		else if (strcmp(*argv,"-pubin") == 0)
17959191Skris			pubin=1;
18059191Skris		else if (strcmp(*argv,"-pubout") == 0)
18159191Skris			pubout=1;
182238405Sjkim		else if (strcmp(*argv,"-RSAPublicKey_in") == 0)
183238405Sjkim			pubin = 2;
184238405Sjkim		else if (strcmp(*argv,"-RSAPublicKey_out") == 0)
185238405Sjkim			pubout = 2;
186238405Sjkim		else if (strcmp(*argv,"-pvk-strong") == 0)
187238405Sjkim			pvk_encr=2;
188238405Sjkim		else if (strcmp(*argv,"-pvk-weak") == 0)
189238405Sjkim			pvk_encr=1;
190238405Sjkim		else if (strcmp(*argv,"-pvk-none") == 0)
191238405Sjkim			pvk_encr=0;
19255714Skris		else if (strcmp(*argv,"-noout") == 0)
19355714Skris			noout=1;
19455714Skris		else if (strcmp(*argv,"-text") == 0)
19555714Skris			text=1;
19655714Skris		else if (strcmp(*argv,"-modulus") == 0)
19755714Skris			modulus=1;
19855714Skris		else if (strcmp(*argv,"-check") == 0)
19955714Skris			check=1;
20055714Skris		else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
20155714Skris			{
20255714Skris			BIO_printf(bio_err,"unknown option %s\n",*argv);
20355714Skris			badops=1;
20455714Skris			break;
20555714Skris			}
20655714Skris		argc--;
20755714Skris		argv++;
20855714Skris		}
20955714Skris
21055714Skris	if (badops)
21155714Skris		{
21255714Skrisbad:
21355714Skris		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
21455714Skris		BIO_printf(bio_err,"where options are\n");
21559191Skris		BIO_printf(bio_err," -inform arg     input format - one of DER NET PEM\n");
21659191Skris		BIO_printf(bio_err," -outform arg    output format - one of DER NET PEM\n");
21759191Skris		BIO_printf(bio_err," -in arg         input file\n");
21868651Skris		BIO_printf(bio_err," -sgckey         Use IIS SGC key format\n");
21959191Skris		BIO_printf(bio_err," -passin arg     input file pass phrase source\n");
22059191Skris		BIO_printf(bio_err," -out arg        output file\n");
22159191Skris		BIO_printf(bio_err," -passout arg    output file pass phrase source\n");
22259191Skris		BIO_printf(bio_err," -des            encrypt PEM output with cbc des\n");
22359191Skris		BIO_printf(bio_err," -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
224109998Smarkm#ifndef OPENSSL_NO_IDEA
22559191Skris		BIO_printf(bio_err," -idea           encrypt PEM output with cbc idea\n");
22655714Skris#endif
227194206Ssimon#ifndef OPENSSL_NO_SEED
228194206Ssimon		BIO_printf(bio_err," -seed           encrypt PEM output with cbc seed\n");
229194206Ssimon#endif
230109998Smarkm#ifndef OPENSSL_NO_AES
231109998Smarkm		BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
232109998Smarkm		BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
233109998Smarkm#endif
234162911Ssimon#ifndef OPENSSL_NO_CAMELLIA
235162911Ssimon		BIO_printf(bio_err," -camellia128, -camellia192, -camellia256\n");
236162911Ssimon		BIO_printf(bio_err,"                 encrypt PEM output with cbc camellia\n");
237162911Ssimon#endif
23859191Skris		BIO_printf(bio_err," -text           print the key in text\n");
23959191Skris		BIO_printf(bio_err," -noout          don't print key out\n");
24059191Skris		BIO_printf(bio_err," -modulus        print the RSA key modulus\n");
24159191Skris		BIO_printf(bio_err," -check          verify key consistency\n");
24259191Skris		BIO_printf(bio_err," -pubin          expect a public key in input file\n");
24359191Skris		BIO_printf(bio_err," -pubout         output a public key\n");
244111147Snectar#ifndef OPENSSL_NO_ENGINE
245109998Smarkm		BIO_printf(bio_err," -engine e       use engine e, possibly a hardware device.\n");
246111147Snectar#endif
24755714Skris		goto end;
24855714Skris		}
24955714Skris
25055714Skris	ERR_load_crypto_strings();
25155714Skris
252111147Snectar#ifndef OPENSSL_NO_ENGINE
253109998Smarkm        e = setup_engine(bio_err, engine, 0);
254111147Snectar#endif
255109998Smarkm
25659191Skris	if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
25759191Skris		BIO_printf(bio_err, "Error getting passwords\n");
25859191Skris		goto end;
25959191Skris	}
26059191Skris
26159191Skris	if(check && pubin) {
26259191Skris		BIO_printf(bio_err, "Only private keys can be checked\n");
26359191Skris		goto end;
26459191Skris	}
26559191Skris
26655714Skris	out=BIO_new(BIO_s_file());
26755714Skris
268109998Smarkm	{
269109998Smarkm		EVP_PKEY	*pkey;
27055714Skris
271109998Smarkm		if (pubin)
272238405Sjkim			{
273238405Sjkim			int tmpformat=-1;
274238405Sjkim			if (pubin == 2)
275238405Sjkim				{
276238405Sjkim				if (informat == FORMAT_PEM)
277238405Sjkim					tmpformat = FORMAT_PEMRSA;
278238405Sjkim				else if (informat == FORMAT_ASN1)
279238405Sjkim					tmpformat = FORMAT_ASN1RSA;
280238405Sjkim				}
281238405Sjkim			else if (informat == FORMAT_NETSCAPE && sgckey)
282238405Sjkim				tmpformat = FORMAT_IISSGC;
283238405Sjkim			else
284238405Sjkim				tmpformat = informat;
285238405Sjkim
286238405Sjkim			pkey = load_pubkey(bio_err, infile, tmpformat, 1,
287109998Smarkm				passin, e, "Public Key");
288238405Sjkim			}
289109998Smarkm		else
290109998Smarkm			pkey = load_key(bio_err, infile,
291109998Smarkm				(informat == FORMAT_NETSCAPE && sgckey ?
292109998Smarkm					FORMAT_IISSGC : informat), 1,
293109998Smarkm				passin, e, "Private Key");
294109998Smarkm
295109998Smarkm		if (pkey != NULL)
296238405Sjkim			rsa = EVP_PKEY_get1_RSA(pkey);
297109998Smarkm		EVP_PKEY_free(pkey);
29859191Skris	}
29955714Skris
30055714Skris	if (rsa == NULL)
30155714Skris		{
30255714Skris		ERR_print_errors(bio_err);
30355714Skris		goto end;
30455714Skris		}
30555714Skris
30655714Skris	if (outfile == NULL)
30768651Skris		{
30855714Skris		BIO_set_fp(out,stdout,BIO_NOCLOSE);
309109998Smarkm#ifdef OPENSSL_SYS_VMS
31068651Skris		{
31168651Skris		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
31268651Skris		out = BIO_push(tmpbio, out);
31368651Skris		}
31468651Skris#endif
31568651Skris		}
31655714Skris	else
31755714Skris		{
31855714Skris		if (BIO_write_filename(out,outfile) <= 0)
31955714Skris			{
32055714Skris			perror(outfile);
32155714Skris			goto end;
32255714Skris			}
32355714Skris		}
32455714Skris
32555714Skris	if (text)
32655714Skris		if (!RSA_print(out,rsa,0))
32755714Skris			{
32855714Skris			perror(outfile);
32955714Skris			ERR_print_errors(bio_err);
33055714Skris			goto end;
33155714Skris			}
33255714Skris
33355714Skris	if (modulus)
33455714Skris		{
33559191Skris		BIO_printf(out,"Modulus=");
33655714Skris		BN_print(out,rsa->n);
33759191Skris		BIO_printf(out,"\n");
33855714Skris		}
33955714Skris
34055714Skris	if (check)
34155714Skris		{
34255714Skris		int r = RSA_check_key(rsa);
34355714Skris
34455714Skris		if (r == 1)
34555714Skris			BIO_printf(out,"RSA key ok\n");
34655714Skris		else if (r == 0)
34755714Skris			{
348160814Ssimon			unsigned long err;
34955714Skris
350109998Smarkm			while ((err = ERR_peek_error()) != 0 &&
351109998Smarkm				ERR_GET_LIB(err) == ERR_LIB_RSA &&
352109998Smarkm				ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
353109998Smarkm				ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE)
35455714Skris				{
355109998Smarkm				BIO_printf(out, "RSA key error: %s\n", ERR_reason_error_string(err));
35655714Skris				ERR_get_error(); /* remove e from error stack */
35755714Skris				}
35855714Skris			}
35955714Skris
36055714Skris		if (r == -1 || ERR_peek_error() != 0) /* should happen only if r == -1 */
36155714Skris			{
36255714Skris			ERR_print_errors(bio_err);
36355714Skris			goto end;
36455714Skris			}
36555714Skris		}
36655714Skris
36759191Skris	if (noout)
36859191Skris		{
36959191Skris		ret = 0;
37059191Skris		goto end;
37159191Skris		}
37259191Skris	BIO_printf(bio_err,"writing RSA key\n");
37359191Skris	if 	(outformat == FORMAT_ASN1) {
374238405Sjkim		if(pubout || pubin)
375238405Sjkim			{
376238405Sjkim			if (pubout == 2)
377238405Sjkim				i=i2d_RSAPublicKey_bio(out,rsa);
378238405Sjkim			else
379238405Sjkim				i=i2d_RSA_PUBKEY_bio(out,rsa);
380238405Sjkim			}
38159191Skris		else i=i2d_RSAPrivateKey_bio(out,rsa);
38259191Skris	}
383109998Smarkm#ifndef OPENSSL_NO_RC4
38455714Skris	else if (outformat == FORMAT_NETSCAPE)
38555714Skris		{
38655714Skris		unsigned char *p,*pp;
38755714Skris		int size;
38855714Skris
38955714Skris		i=1;
39068651Skris		size=i2d_RSA_NET(rsa,NULL,NULL, sgckey);
39168651Skris		if ((p=(unsigned char *)OPENSSL_malloc(size)) == NULL)
39255714Skris			{
39368651Skris			BIO_printf(bio_err,"Memory allocation failure\n");
39455714Skris			goto end;
39555714Skris			}
39655714Skris		pp=p;
39768651Skris		i2d_RSA_NET(rsa,&p,NULL, sgckey);
39855714Skris		BIO_write(out,(char *)pp,size);
39968651Skris		OPENSSL_free(pp);
40055714Skris		}
40155714Skris#endif
40259191Skris	else if (outformat == FORMAT_PEM) {
40359191Skris		if(pubout || pubin)
404238405Sjkim			{
405238405Sjkim			if (pubout == 2)
406238405Sjkim		    		i=PEM_write_bio_RSAPublicKey(out,rsa);
407238405Sjkim			else
408238405Sjkim		    		i=PEM_write_bio_RSA_PUBKEY(out,rsa);
409238405Sjkim			}
41059191Skris		else i=PEM_write_bio_RSAPrivateKey(out,rsa,
41159191Skris						enc,NULL,0,NULL,passout);
412238405Sjkim#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
413238405Sjkim	} else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
414238405Sjkim		EVP_PKEY *pk;
415238405Sjkim		pk = EVP_PKEY_new();
416238405Sjkim		EVP_PKEY_set1_RSA(pk, rsa);
417238405Sjkim		if (outformat == FORMAT_PVK)
418238405Sjkim			i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
419238405Sjkim		else if (pubin || pubout)
420238405Sjkim			i = i2b_PublicKey_bio(out, pk);
421238405Sjkim		else
422238405Sjkim			i = i2b_PrivateKey_bio(out, pk);
423238405Sjkim		EVP_PKEY_free(pk);
424238405Sjkim#endif
42559191Skris	} else	{
42655714Skris		BIO_printf(bio_err,"bad output format specified for outfile\n");
42755714Skris		goto end;
42855714Skris		}
429238405Sjkim	if (i <= 0)
43055714Skris		{
43159191Skris		BIO_printf(bio_err,"unable to write key\n");
43255714Skris		ERR_print_errors(bio_err);
43355714Skris		}
43455714Skris	else
43555714Skris		ret=0;
43655714Skrisend:
43768651Skris	if(out != NULL) BIO_free_all(out);
43859191Skris	if(rsa != NULL) RSA_free(rsa);
43968651Skris	if(passin) OPENSSL_free(passin);
44068651Skris	if(passout) OPENSSL_free(passout);
441109998Smarkm	apps_shutdown();
442109998Smarkm	OPENSSL_EXIT(ret);
44355714Skris	}
444109998Smarkm#else /* !OPENSSL_NO_RSA */
44559191Skris
44659191Skris# if PEDANTIC
44759191Skrisstatic void *dummy=&dummy;
44859191Skris# endif
44959191Skris
45055714Skris#endif
451