10Sstevel@tonic-gate /* apps/genrsa.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>
60*2139Sjp161948 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
61*2139Sjp161948 * deprecated functions for openssl-internal code */
62*2139Sjp161948 #ifdef OPENSSL_NO_DEPRECATED
63*2139Sjp161948 #undef OPENSSL_NO_DEPRECATED
64*2139Sjp161948 #endif
65*2139Sjp161948
660Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
670Sstevel@tonic-gate #include <stdio.h>
680Sstevel@tonic-gate #include <string.h>
690Sstevel@tonic-gate #include <sys/types.h>
700Sstevel@tonic-gate #include <sys/stat.h>
710Sstevel@tonic-gate #include "apps.h"
720Sstevel@tonic-gate #include <openssl/bio.h>
730Sstevel@tonic-gate #include <openssl/err.h>
740Sstevel@tonic-gate #include <openssl/bn.h>
750Sstevel@tonic-gate #include <openssl/rsa.h>
760Sstevel@tonic-gate #include <openssl/evp.h>
770Sstevel@tonic-gate #include <openssl/x509.h>
780Sstevel@tonic-gate #include <openssl/pem.h>
790Sstevel@tonic-gate #include <openssl/rand.h>
800Sstevel@tonic-gate
810Sstevel@tonic-gate #define DEFBITS 512
820Sstevel@tonic-gate #undef PROG
830Sstevel@tonic-gate #define PROG genrsa_main
840Sstevel@tonic-gate
85*2139Sjp161948 static int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb);
860Sstevel@tonic-gate
870Sstevel@tonic-gate int MAIN(int, char **);
880Sstevel@tonic-gate
MAIN(int argc,char ** argv)890Sstevel@tonic-gate int MAIN(int argc, char **argv)
900Sstevel@tonic-gate {
91*2139Sjp161948 BN_GENCB cb;
920Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
930Sstevel@tonic-gate ENGINE *e = NULL;
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate int ret=1;
960Sstevel@tonic-gate int i,num=DEFBITS;
970Sstevel@tonic-gate long l;
980Sstevel@tonic-gate const EVP_CIPHER *enc=NULL;
990Sstevel@tonic-gate unsigned long f4=RSA_F4;
1000Sstevel@tonic-gate char *outfile=NULL;
1010Sstevel@tonic-gate char *passargout = NULL, *passout = NULL;
1020Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1030Sstevel@tonic-gate char *engine=NULL;
1040Sstevel@tonic-gate #endif
1050Sstevel@tonic-gate char *inrand=NULL;
1060Sstevel@tonic-gate BIO *out=NULL;
107*2139Sjp161948 BIGNUM *bn = BN_new();
108*2139Sjp161948 RSA *rsa = RSA_new();
109*2139Sjp161948
110*2139Sjp161948 if(!bn || !rsa) goto err;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate apps_startup();
113*2139Sjp161948 BN_GENCB_set(&cb, genrsa_cb, bio_err);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate if (bio_err == NULL)
1160Sstevel@tonic-gate if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1170Sstevel@tonic-gate BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (!load_config(bio_err, NULL))
1200Sstevel@tonic-gate goto err;
1210Sstevel@tonic-gate if ((out=BIO_new(BIO_s_file())) == NULL)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate BIO_printf(bio_err,"unable to create BIO for output\n");
1240Sstevel@tonic-gate goto err;
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate argv++;
1280Sstevel@tonic-gate argc--;
1290Sstevel@tonic-gate for (;;)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate if (argc <= 0) break;
1320Sstevel@tonic-gate if (strcmp(*argv,"-out") == 0)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate if (--argc < 1) goto bad;
1350Sstevel@tonic-gate outfile= *(++argv);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate else if (strcmp(*argv,"-3") == 0)
1380Sstevel@tonic-gate f4=3;
1390Sstevel@tonic-gate else if (strcmp(*argv,"-F4") == 0 || strcmp(*argv,"-f4") == 0)
1400Sstevel@tonic-gate f4=RSA_F4;
1410Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1420Sstevel@tonic-gate else if (strcmp(*argv,"-engine") == 0)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate if (--argc < 1) goto bad;
1450Sstevel@tonic-gate engine= *(++argv);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate else if (strcmp(*argv,"-rand") == 0)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate if (--argc < 1) goto bad;
1510Sstevel@tonic-gate inrand= *(++argv);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate #ifndef OPENSSL_NO_DES
1540Sstevel@tonic-gate else if (strcmp(*argv,"-des") == 0)
1550Sstevel@tonic-gate enc=EVP_des_cbc();
1560Sstevel@tonic-gate else if (strcmp(*argv,"-des3") == 0)
1570Sstevel@tonic-gate enc=EVP_des_ede3_cbc();
1580Sstevel@tonic-gate #endif
1590Sstevel@tonic-gate #ifndef OPENSSL_NO_IDEA
1600Sstevel@tonic-gate else if (strcmp(*argv,"-idea") == 0)
1610Sstevel@tonic-gate enc=EVP_idea_cbc();
1620Sstevel@tonic-gate #endif
1630Sstevel@tonic-gate #ifndef OPENSSL_NO_AES
1640Sstevel@tonic-gate else if (strcmp(*argv,"-aes128") == 0)
1650Sstevel@tonic-gate enc=EVP_aes_128_cbc();
1660Sstevel@tonic-gate else if (strcmp(*argv,"-aes192") == 0)
1670Sstevel@tonic-gate enc=EVP_aes_192_cbc();
1680Sstevel@tonic-gate else if (strcmp(*argv,"-aes256") == 0)
1690Sstevel@tonic-gate enc=EVP_aes_256_cbc();
170688Sjp161948 #endif
1710Sstevel@tonic-gate else if (strcmp(*argv,"-passout") == 0)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate if (--argc < 1) goto bad;
1740Sstevel@tonic-gate passargout= *(++argv);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate else
1770Sstevel@tonic-gate break;
1780Sstevel@tonic-gate argv++;
1790Sstevel@tonic-gate argc--;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0)))
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate bad:
1840Sstevel@tonic-gate BIO_printf(bio_err,"usage: genrsa [args] [numbits]\n");
1850Sstevel@tonic-gate BIO_printf(bio_err," -des encrypt the generated key with DES in cbc mode\n");
1860Sstevel@tonic-gate BIO_printf(bio_err," -des3 encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
1870Sstevel@tonic-gate #ifndef OPENSSL_NO_IDEA
1880Sstevel@tonic-gate BIO_printf(bio_err," -idea encrypt the generated key with IDEA in cbc mode\n");
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate #ifndef OPENSSL_NO_AES
1910Sstevel@tonic-gate BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
1920Sstevel@tonic-gate BIO_printf(bio_err," encrypt PEM output with cbc aes\n");
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate BIO_printf(bio_err," -out file output the key to 'file\n");
1950Sstevel@tonic-gate BIO_printf(bio_err," -passout arg output file pass phrase source\n");
1960Sstevel@tonic-gate BIO_printf(bio_err," -f4 use F4 (0x10001) for the E value\n");
1970Sstevel@tonic-gate BIO_printf(bio_err," -3 use 3 for the E value\n");
1980Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1990Sstevel@tonic-gate BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
2000Sstevel@tonic-gate #endif
2010Sstevel@tonic-gate BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
2020Sstevel@tonic-gate BIO_printf(bio_err," load the file (or the files in the directory) into\n");
2030Sstevel@tonic-gate BIO_printf(bio_err," the random number generator\n");
2040Sstevel@tonic-gate goto err;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate ERR_load_crypto_strings();
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
2100Sstevel@tonic-gate BIO_printf(bio_err, "Error getting password\n");
2110Sstevel@tonic-gate goto err;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2150Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0);
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (outfile == NULL)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate BIO_set_fp(out,stdout,BIO_NOCLOSE);
2210Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2240Sstevel@tonic-gate out = BIO_push(tmpbio, out);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate #endif
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate else
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate if (BIO_write_filename(out,outfile) <= 0)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate perror(outfile);
2330Sstevel@tonic-gate goto err;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
2380Sstevel@tonic-gate && !RAND_status())
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate if (inrand != NULL)
2430Sstevel@tonic-gate BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
2440Sstevel@tonic-gate app_RAND_load_files(inrand));
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n",
2470Sstevel@tonic-gate num);
248*2139Sjp161948
249*2139Sjp161948 if(!BN_set_word(bn, f4) || !RSA_generate_key_ex(rsa, num, bn, &cb))
250*2139Sjp161948 goto err;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate app_RAND_write_file(NULL, bio_err);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /* We need to do the following for when the base number size is <
2550Sstevel@tonic-gate * long, esp windows 3.1 :-(. */
2560Sstevel@tonic-gate l=0L;
2570Sstevel@tonic-gate for (i=0; i<rsa->e->top; i++)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate #ifndef SIXTY_FOUR_BIT
2600Sstevel@tonic-gate l<<=BN_BITS4;
2610Sstevel@tonic-gate l<<=BN_BITS4;
2620Sstevel@tonic-gate #endif
2630Sstevel@tonic-gate l+=rsa->e->d[i];
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l);
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate PW_CB_DATA cb_data;
2680Sstevel@tonic-gate cb_data.password = passout;
2690Sstevel@tonic-gate cb_data.prompt_info = outfile;
2700Sstevel@tonic-gate if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,
2710Sstevel@tonic-gate (pem_password_cb *)password_callback,&cb_data))
2720Sstevel@tonic-gate goto err;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate ret=0;
2760Sstevel@tonic-gate err:
277*2139Sjp161948 if (bn) BN_free(bn);
278*2139Sjp161948 if (rsa) RSA_free(rsa);
279*2139Sjp161948 if (out) BIO_free_all(out);
2800Sstevel@tonic-gate if(passout) OPENSSL_free(passout);
2810Sstevel@tonic-gate if (ret != 0)
2820Sstevel@tonic-gate ERR_print_errors(bio_err);
2830Sstevel@tonic-gate apps_shutdown();
2840Sstevel@tonic-gate OPENSSL_EXIT(ret);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
genrsa_cb(int p,int n,BN_GENCB * cb)287*2139Sjp161948 static int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate char c='*';
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (p == 0) c='.';
2920Sstevel@tonic-gate if (p == 1) c='+';
2930Sstevel@tonic-gate if (p == 2) c='*';
2940Sstevel@tonic-gate if (p == 3) c='\n';
295*2139Sjp161948 BIO_write(cb->arg,&c,1);
296*2139Sjp161948 (void)BIO_flush(cb->arg);
2970Sstevel@tonic-gate #ifdef LINT
2980Sstevel@tonic-gate p=n;
2990Sstevel@tonic-gate #endif
300*2139Sjp161948 return 1;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate #else /* !OPENSSL_NO_RSA */
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate # if PEDANTIC
3050Sstevel@tonic-gate static void *dummy=&dummy;
3060Sstevel@tonic-gate # endif
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate #endif
309