xref: /onnv-gate/usr/src/common/openssl/apps/rsa.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/rsa.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
59*2139Sjp161948 #include <openssl/opensslconf.h>
600Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include <stdlib.h>
630Sstevel@tonic-gate #include <string.h>
640Sstevel@tonic-gate #include <time.h>
650Sstevel@tonic-gate #include "apps.h"
660Sstevel@tonic-gate #include <openssl/bio.h>
670Sstevel@tonic-gate #include <openssl/err.h>
680Sstevel@tonic-gate #include <openssl/rsa.h>
690Sstevel@tonic-gate #include <openssl/evp.h>
700Sstevel@tonic-gate #include <openssl/x509.h>
710Sstevel@tonic-gate #include <openssl/pem.h>
72*2139Sjp161948 #include <openssl/bn.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #undef PROG
750Sstevel@tonic-gate #define PROG	rsa_main
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /* -inform arg	- input format - default PEM (one of DER, NET or PEM)
780Sstevel@tonic-gate  * -outform arg - output format - default PEM
790Sstevel@tonic-gate  * -in arg	- input file - default stdin
800Sstevel@tonic-gate  * -out arg	- output file - default stdout
810Sstevel@tonic-gate  * -des		- encrypt output if PEM format with DES in cbc mode
820Sstevel@tonic-gate  * -des3	- encrypt output if PEM format
830Sstevel@tonic-gate  * -idea	- encrypt output if PEM format
840Sstevel@tonic-gate  * -aes128	- encrypt output if PEM format
850Sstevel@tonic-gate  * -aes192	- encrypt output if PEM format
860Sstevel@tonic-gate  * -aes256	- encrypt output if PEM format
870Sstevel@tonic-gate  * -text	- print a text version
880Sstevel@tonic-gate  * -modulus	- print the RSA key modulus
890Sstevel@tonic-gate  * -check	- verify key consistency
900Sstevel@tonic-gate  * -pubin	- Expect a public key in input file.
910Sstevel@tonic-gate  * -pubout	- Output a public key.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int MAIN(int, char **);
950Sstevel@tonic-gate 
MAIN(int argc,char ** argv)960Sstevel@tonic-gate int MAIN(int argc, char **argv)
970Sstevel@tonic-gate 	{
980Sstevel@tonic-gate 	ENGINE *e = NULL;
990Sstevel@tonic-gate 	int ret=1;
1000Sstevel@tonic-gate 	RSA *rsa=NULL;
1010Sstevel@tonic-gate 	int i,badops=0, sgckey=0;
1020Sstevel@tonic-gate 	const EVP_CIPHER *enc=NULL;
1030Sstevel@tonic-gate 	BIO *out=NULL;
1040Sstevel@tonic-gate 	int informat,outformat,text=0,check=0,noout=0;
1050Sstevel@tonic-gate 	int pubin = 0, pubout = 0;
1060Sstevel@tonic-gate 	char *infile,*outfile,*prog;
1070Sstevel@tonic-gate 	char *passargin = NULL, *passargout = NULL;
1080Sstevel@tonic-gate 	char *passin = NULL, *passout = NULL;
1090Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1100Sstevel@tonic-gate 	char *engine=NULL;
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 	int modulus=0;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	apps_startup();
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (bio_err == NULL)
1170Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1180Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
1210Sstevel@tonic-gate 		goto end;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	infile=NULL;
1240Sstevel@tonic-gate 	outfile=NULL;
1250Sstevel@tonic-gate 	informat=FORMAT_PEM;
1260Sstevel@tonic-gate 	outformat=FORMAT_PEM;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	prog=argv[0];
1290Sstevel@tonic-gate 	argc--;
1300Sstevel@tonic-gate 	argv++;
1310Sstevel@tonic-gate 	while (argc >= 1)
1320Sstevel@tonic-gate 		{
1330Sstevel@tonic-gate 		if 	(strcmp(*argv,"-inform") == 0)
1340Sstevel@tonic-gate 			{
1350Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1360Sstevel@tonic-gate 			informat=str2fmt(*(++argv));
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 		else if (strcmp(*argv,"-outform") == 0)
1390Sstevel@tonic-gate 			{
1400Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1410Sstevel@tonic-gate 			outformat=str2fmt(*(++argv));
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 		else if (strcmp(*argv,"-in") == 0)
1440Sstevel@tonic-gate 			{
1450Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1460Sstevel@tonic-gate 			infile= *(++argv);
1470Sstevel@tonic-gate 			}
1480Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
1490Sstevel@tonic-gate 			{
1500Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1510Sstevel@tonic-gate 			outfile= *(++argv);
1520Sstevel@tonic-gate 			}
1530Sstevel@tonic-gate 		else if (strcmp(*argv,"-passin") == 0)
1540Sstevel@tonic-gate 			{
1550Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1560Sstevel@tonic-gate 			passargin= *(++argv);
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 		else if (strcmp(*argv,"-passout") == 0)
1590Sstevel@tonic-gate 			{
1600Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1610Sstevel@tonic-gate 			passargout= *(++argv);
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1640Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
1650Sstevel@tonic-gate 			{
1660Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1670Sstevel@tonic-gate 			engine= *(++argv);
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate 		else if (strcmp(*argv,"-sgckey") == 0)
1710Sstevel@tonic-gate 			sgckey=1;
1720Sstevel@tonic-gate 		else if (strcmp(*argv,"-pubin") == 0)
1730Sstevel@tonic-gate 			pubin=1;
1740Sstevel@tonic-gate 		else if (strcmp(*argv,"-pubout") == 0)
1750Sstevel@tonic-gate 			pubout=1;
1760Sstevel@tonic-gate 		else if (strcmp(*argv,"-noout") == 0)
1770Sstevel@tonic-gate 			noout=1;
1780Sstevel@tonic-gate 		else if (strcmp(*argv,"-text") == 0)
1790Sstevel@tonic-gate 			text=1;
1800Sstevel@tonic-gate 		else if (strcmp(*argv,"-modulus") == 0)
1810Sstevel@tonic-gate 			modulus=1;
1820Sstevel@tonic-gate 		else if (strcmp(*argv,"-check") == 0)
1830Sstevel@tonic-gate 			check=1;
1840Sstevel@tonic-gate 		else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
1850Sstevel@tonic-gate 			{
1860Sstevel@tonic-gate 			BIO_printf(bio_err,"unknown option %s\n",*argv);
1870Sstevel@tonic-gate 			badops=1;
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 			}
1900Sstevel@tonic-gate 		argc--;
1910Sstevel@tonic-gate 		argv++;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (badops)
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate bad:
1970Sstevel@tonic-gate 		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
1980Sstevel@tonic-gate 		BIO_printf(bio_err,"where options are\n");
1990Sstevel@tonic-gate 		BIO_printf(bio_err," -inform arg     input format - one of DER NET PEM\n");
2000Sstevel@tonic-gate 		BIO_printf(bio_err," -outform arg    output format - one of DER NET PEM\n");
2010Sstevel@tonic-gate 		BIO_printf(bio_err," -in arg         input file\n");
2020Sstevel@tonic-gate 		BIO_printf(bio_err," -sgckey         Use IIS SGC key format\n");
2030Sstevel@tonic-gate 		BIO_printf(bio_err," -passin arg     input file pass phrase source\n");
2040Sstevel@tonic-gate 		BIO_printf(bio_err," -out arg        output file\n");
2050Sstevel@tonic-gate 		BIO_printf(bio_err," -passout arg    output file pass phrase source\n");
2060Sstevel@tonic-gate 		BIO_printf(bio_err," -des            encrypt PEM output with cbc des\n");
2070Sstevel@tonic-gate 		BIO_printf(bio_err," -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
2080Sstevel@tonic-gate #ifndef OPENSSL_NO_IDEA
2090Sstevel@tonic-gate 		BIO_printf(bio_err," -idea           encrypt PEM output with cbc idea\n");
2100Sstevel@tonic-gate #endif
2110Sstevel@tonic-gate #ifndef OPENSSL_NO_AES
2120Sstevel@tonic-gate 		BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
2130Sstevel@tonic-gate 		BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
2140Sstevel@tonic-gate #endif
2150Sstevel@tonic-gate 		BIO_printf(bio_err," -text           print the key in text\n");
2160Sstevel@tonic-gate 		BIO_printf(bio_err," -noout          don't print key out\n");
2170Sstevel@tonic-gate 		BIO_printf(bio_err," -modulus        print the RSA key modulus\n");
2180Sstevel@tonic-gate 		BIO_printf(bio_err," -check          verify key consistency\n");
2190Sstevel@tonic-gate 		BIO_printf(bio_err," -pubin          expect a public key in input file\n");
2200Sstevel@tonic-gate 		BIO_printf(bio_err," -pubout         output a public key\n");
2210Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2220Sstevel@tonic-gate 		BIO_printf(bio_err," -engine e       use engine e, possibly a hardware device.\n");
2230Sstevel@tonic-gate #endif
2240Sstevel@tonic-gate 		goto end;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	ERR_load_crypto_strings();
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2300Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
2310Sstevel@tonic-gate #endif
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
2340Sstevel@tonic-gate 		BIO_printf(bio_err, "Error getting passwords\n");
2350Sstevel@tonic-gate 		goto end;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if(check && pubin) {
2390Sstevel@tonic-gate 		BIO_printf(bio_err, "Only private keys can be checked\n");
2400Sstevel@tonic-gate 		goto end;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	out=BIO_new(BIO_s_file());
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	{
2460Sstevel@tonic-gate 		EVP_PKEY	*pkey;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		if (pubin)
2490Sstevel@tonic-gate 			pkey = load_pubkey(bio_err, infile,
2500Sstevel@tonic-gate 				(informat == FORMAT_NETSCAPE && sgckey ?
2510Sstevel@tonic-gate 					FORMAT_IISSGC : informat), 1,
2520Sstevel@tonic-gate 				passin, e, "Public Key");
2530Sstevel@tonic-gate 		else
2540Sstevel@tonic-gate 			pkey = load_key(bio_err, infile,
2550Sstevel@tonic-gate 				(informat == FORMAT_NETSCAPE && sgckey ?
2560Sstevel@tonic-gate 					FORMAT_IISSGC : informat), 1,
2570Sstevel@tonic-gate 				passin, e, "Private Key");
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		if (pkey != NULL)
2600Sstevel@tonic-gate 		rsa = pkey == NULL ? NULL : EVP_PKEY_get1_RSA(pkey);
2610Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (rsa == NULL)
2650Sstevel@tonic-gate 		{
2660Sstevel@tonic-gate 		ERR_print_errors(bio_err);
2670Sstevel@tonic-gate 		goto end;
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (outfile == NULL)
2710Sstevel@tonic-gate 		{
2720Sstevel@tonic-gate 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
2730Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2740Sstevel@tonic-gate 		{
2750Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2760Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate #endif
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	else
2810Sstevel@tonic-gate 		{
2820Sstevel@tonic-gate 		if (BIO_write_filename(out,outfile) <= 0)
2830Sstevel@tonic-gate 			{
2840Sstevel@tonic-gate 			perror(outfile);
2850Sstevel@tonic-gate 			goto end;
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (text)
2900Sstevel@tonic-gate 		if (!RSA_print(out,rsa,0))
2910Sstevel@tonic-gate 			{
2920Sstevel@tonic-gate 			perror(outfile);
2930Sstevel@tonic-gate 			ERR_print_errors(bio_err);
2940Sstevel@tonic-gate 			goto end;
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (modulus)
2980Sstevel@tonic-gate 		{
2990Sstevel@tonic-gate 		BIO_printf(out,"Modulus=");
3000Sstevel@tonic-gate 		BN_print(out,rsa->n);
3010Sstevel@tonic-gate 		BIO_printf(out,"\n");
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (check)
3050Sstevel@tonic-gate 		{
3060Sstevel@tonic-gate 		int r = RSA_check_key(rsa);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		if (r == 1)
3090Sstevel@tonic-gate 			BIO_printf(out,"RSA key ok\n");
3100Sstevel@tonic-gate 		else if (r == 0)
3110Sstevel@tonic-gate 			{
312*2139Sjp161948 			unsigned long err;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			while ((err = ERR_peek_error()) != 0 &&
3150Sstevel@tonic-gate 				ERR_GET_LIB(err) == ERR_LIB_RSA &&
3160Sstevel@tonic-gate 				ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
3170Sstevel@tonic-gate 				ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE)
3180Sstevel@tonic-gate 				{
3190Sstevel@tonic-gate 				BIO_printf(out, "RSA key error: %s\n", ERR_reason_error_string(err));
3200Sstevel@tonic-gate 				ERR_get_error(); /* remove e from error stack */
3210Sstevel@tonic-gate 				}
3220Sstevel@tonic-gate 			}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		if (r == -1 || ERR_peek_error() != 0) /* should happen only if r == -1 */
3250Sstevel@tonic-gate 			{
3260Sstevel@tonic-gate 			ERR_print_errors(bio_err);
3270Sstevel@tonic-gate 			goto end;
3280Sstevel@tonic-gate 			}
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (noout)
3320Sstevel@tonic-gate 		{
3330Sstevel@tonic-gate 		ret = 0;
3340Sstevel@tonic-gate 		goto end;
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 	BIO_printf(bio_err,"writing RSA key\n");
3370Sstevel@tonic-gate 	if 	(outformat == FORMAT_ASN1) {
3380Sstevel@tonic-gate 		if(pubout || pubin) i=i2d_RSA_PUBKEY_bio(out,rsa);
3390Sstevel@tonic-gate 		else i=i2d_RSAPrivateKey_bio(out,rsa);
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate #ifndef OPENSSL_NO_RC4
3420Sstevel@tonic-gate 	else if (outformat == FORMAT_NETSCAPE)
3430Sstevel@tonic-gate 		{
3440Sstevel@tonic-gate 		unsigned char *p,*pp;
3450Sstevel@tonic-gate 		int size;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		i=1;
3480Sstevel@tonic-gate 		size=i2d_RSA_NET(rsa,NULL,NULL, sgckey);
3490Sstevel@tonic-gate 		if ((p=(unsigned char *)OPENSSL_malloc(size)) == NULL)
3500Sstevel@tonic-gate 			{
3510Sstevel@tonic-gate 			BIO_printf(bio_err,"Memory allocation failure\n");
3520Sstevel@tonic-gate 			goto end;
3530Sstevel@tonic-gate 			}
3540Sstevel@tonic-gate 		pp=p;
3550Sstevel@tonic-gate 		i2d_RSA_NET(rsa,&p,NULL, sgckey);
3560Sstevel@tonic-gate 		BIO_write(out,(char *)pp,size);
3570Sstevel@tonic-gate 		OPENSSL_free(pp);
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate #endif
3600Sstevel@tonic-gate 	else if (outformat == FORMAT_PEM) {
3610Sstevel@tonic-gate 		if(pubout || pubin)
3620Sstevel@tonic-gate 		    i=PEM_write_bio_RSA_PUBKEY(out,rsa);
3630Sstevel@tonic-gate 		else i=PEM_write_bio_RSAPrivateKey(out,rsa,
3640Sstevel@tonic-gate 						enc,NULL,0,NULL,passout);
3650Sstevel@tonic-gate 	} else	{
3660Sstevel@tonic-gate 		BIO_printf(bio_err,"bad output format specified for outfile\n");
3670Sstevel@tonic-gate 		goto end;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 	if (!i)
3700Sstevel@tonic-gate 		{
3710Sstevel@tonic-gate 		BIO_printf(bio_err,"unable to write key\n");
3720Sstevel@tonic-gate 		ERR_print_errors(bio_err);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	else
3750Sstevel@tonic-gate 		ret=0;
3760Sstevel@tonic-gate end:
3770Sstevel@tonic-gate 	if(out != NULL) BIO_free_all(out);
3780Sstevel@tonic-gate 	if(rsa != NULL) RSA_free(rsa);
3790Sstevel@tonic-gate 	if(passin) OPENSSL_free(passin);
3800Sstevel@tonic-gate 	if(passout) OPENSSL_free(passout);
3810Sstevel@tonic-gate 	apps_shutdown();
3820Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate #else /* !OPENSSL_NO_RSA */
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate # if PEDANTIC
3870Sstevel@tonic-gate static void *dummy=&dummy;
3880Sstevel@tonic-gate # endif
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate #endif
391