genrsa.c revision 160814
1112158Sdas/* apps/genrsa.c */
2112158Sdas/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3112158Sdas * All rights reserved.
4112158Sdas *
5112158Sdas * This package is an SSL implementation written
6112158Sdas * by Eric Young (eay@cryptsoft.com).
7112158Sdas * The implementation was written so as to conform with Netscapes SSL.
8112158Sdas *
9112158Sdas * This library is free for commercial and non-commercial use as long as
10112158Sdas * the following conditions are aheared to.  The following conditions
11112158Sdas * apply to all code found in this distribution, be it the RC4, RSA,
12112158Sdas * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13112158Sdas * included with this distribution is covered by the same copyright terms
14112158Sdas * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15112158Sdas *
16112158Sdas * Copyright remains Eric Young's, and as such any Copyright notices in
17112158Sdas * the code are not to be removed.
18112158Sdas * If this package is used in a product, Eric Young should be given attribution
19112158Sdas * as the author of the parts of the library used.
20112158Sdas * This can be in the form of a textual message at program startup or
21112158Sdas * in documentation (online or textual) provided with the package.
22112158Sdas *
23112158Sdas * Redistribution and use in source and binary forms, with or without
24112158Sdas * modification, are permitted provided that the following conditions
25112158Sdas * are met:
26112158Sdas * 1. Redistributions of source code must retain the copyright
27112158Sdas *    notice, this list of conditions and the following disclaimer.
28112158Sdas * 2. Redistributions in binary form must reproduce the above copyright
29112158Sdas *    notice, this list of conditions and the following disclaimer in the
30112158Sdas *    documentation and/or other materials provided with the distribution.
31112158Sdas * 3. All advertising materials mentioning features or use of this software
32112158Sdas *    must display the following acknowledgement:
33112158Sdas *    "This product includes cryptographic software written by
34112158Sdas *     Eric Young (eay@cryptsoft.com)"
35112158Sdas *    The word 'cryptographic' can be left out if the rouines from the library
36112158Sdas *    being used are not cryptographic related :-).
37112158Sdas * 4. If you include any Windows specific code (or a derivative thereof) from
38112158Sdas *    the apps directory (application code) you must include an acknowledgement:
39112158Sdas *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40112158Sdas *
41112158Sdas * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42112158Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43112158Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44112158Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45112158Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46112158Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47112158Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48112158Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49112158Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50112158Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51112158Sdas * SUCH DAMAGE.
52112158Sdas *
53112158Sdas * The licence and distribution terms for any publically available version or
54112158Sdas * derivative of this code cannot be changed.  i.e. this code cannot simply be
55112158Sdas * copied and put under another distribution licence
56112158Sdas * [including the GNU Public Licence.]
57112158Sdas */
58112158Sdas
59187808Sdas#include <openssl/opensslconf.h>
60187808Sdas/* Until the key-gen callbacks are modified to use newer prototypes, we allow
61187808Sdas * deprecated functions for openssl-internal code */
62112158Sdas#ifdef OPENSSL_NO_DEPRECATED
63112158Sdas#undef OPENSSL_NO_DEPRECATED
64112158Sdas#endif
65112158Sdas
66112158Sdas#ifndef OPENSSL_NO_RSA
67112158Sdas#include <stdio.h>
68112158Sdas#include <string.h>
69112158Sdas#include <sys/types.h>
70112158Sdas#include <sys/stat.h>
71112158Sdas#include "apps.h"
72112158Sdas#include <openssl/bio.h>
73112158Sdas#include <openssl/err.h>
74112158Sdas#include <openssl/bn.h>
75112158Sdas#include <openssl/rsa.h>
76112158Sdas#include <openssl/evp.h>
77112158Sdas#include <openssl/x509.h>
78112158Sdas#include <openssl/pem.h>
79112158Sdas#include <openssl/rand.h>
80112158Sdas
81112158Sdas#define DEFBITS	512
82112158Sdas#undef PROG
83112158Sdas#define PROG genrsa_main
84112158Sdas
85112158Sdasstatic int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb);
86112158Sdas
87112158Sdasint MAIN(int, char **);
88112158Sdas
89112158Sdasint MAIN(int argc, char **argv)
90112158Sdas	{
91112158Sdas	BN_GENCB cb;
92112158Sdas#ifndef OPENSSL_NO_ENGINE
93112158Sdas	ENGINE *e = NULL;
94112158Sdas#endif
95112158Sdas	int ret=1;
96112158Sdas	int i,num=DEFBITS;
97112158Sdas	long l;
98112158Sdas	const EVP_CIPHER *enc=NULL;
99112158Sdas	unsigned long f4=RSA_F4;
100112158Sdas	char *outfile=NULL;
101112158Sdas	char *passargout = NULL, *passout = NULL;
102112158Sdas#ifndef OPENSSL_NO_ENGINE
103112158Sdas	char *engine=NULL;
104112158Sdas#endif
105112158Sdas	char *inrand=NULL;
106112158Sdas	BIO *out=NULL;
107112158Sdas	BIGNUM *bn = BN_new();
108112158Sdas	RSA *rsa = RSA_new();
109112158Sdas
110112158Sdas	if(!bn || !rsa) goto err;
111112158Sdas
112112158Sdas	apps_startup();
113112158Sdas	BN_GENCB_set(&cb, genrsa_cb, bio_err);
114112158Sdas
115112158Sdas	if (bio_err == NULL)
116112158Sdas		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
117112158Sdas			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
118112158Sdas
119112158Sdas	if (!load_config(bio_err, NULL))
120112158Sdas		goto err;
121112158Sdas	if ((out=BIO_new(BIO_s_file())) == NULL)
122112158Sdas		{
123112158Sdas		BIO_printf(bio_err,"unable to create BIO for output\n");
124112158Sdas		goto err;
125112158Sdas		}
126112158Sdas
127112158Sdas	argv++;
128112158Sdas	argc--;
129112158Sdas	for (;;)
130112158Sdas		{
131112158Sdas		if (argc <= 0) break;
132112158Sdas		if (strcmp(*argv,"-out") == 0)
133112158Sdas			{
134112158Sdas			if (--argc < 1) goto bad;
135112158Sdas			outfile= *(++argv);
136112158Sdas			}
137112158Sdas		else if (strcmp(*argv,"-3") == 0)
138112158Sdas			f4=3;
139112158Sdas		else if (strcmp(*argv,"-F4") == 0 || strcmp(*argv,"-f4") == 0)
140112158Sdas			f4=RSA_F4;
141112158Sdas#ifndef OPENSSL_NO_ENGINE
142112158Sdas		else if (strcmp(*argv,"-engine") == 0)
143112158Sdas			{
144112158Sdas			if (--argc < 1) goto bad;
145112158Sdas			engine= *(++argv);
146112158Sdas			}
147112158Sdas#endif
148112158Sdas		else if (strcmp(*argv,"-rand") == 0)
149165743Sdas			{
150165743Sdas			if (--argc < 1) goto bad;
151165743Sdas			inrand= *(++argv);
152165743Sdas			}
153165743Sdas#ifndef OPENSSL_NO_DES
154112158Sdas		else if (strcmp(*argv,"-des") == 0)
155165743Sdas			enc=EVP_des_cbc();
156165743Sdas		else if (strcmp(*argv,"-des3") == 0)
157165743Sdas			enc=EVP_des_ede3_cbc();
158165743Sdas#endif
159165743Sdas#ifndef OPENSSL_NO_IDEA
160165743Sdas		else if (strcmp(*argv,"-idea") == 0)
161165743Sdas			enc=EVP_idea_cbc();
162112158Sdas#endif
163112158Sdas#ifndef OPENSSL_NO_AES
164112158Sdas		else if (strcmp(*argv,"-aes128") == 0)
165112158Sdas			enc=EVP_aes_128_cbc();
166165743Sdas		else if (strcmp(*argv,"-aes192") == 0)
167165743Sdas			enc=EVP_aes_192_cbc();
168165743Sdas		else if (strcmp(*argv,"-aes256") == 0)
169165743Sdas			enc=EVP_aes_256_cbc();
170165743Sdas#endif
171165743Sdas		else if (strcmp(*argv,"-passout") == 0)
172165743Sdas			{
173165743Sdas			if (--argc < 1) goto bad;
174165743Sdas			passargout= *(++argv);
175165743Sdas			}
176112158Sdas		else
177112158Sdas			break;
178112158Sdas		argv++;
179112158Sdas		argc--;
180112158Sdas		}
181112158Sdas	if ((argc >= 1) && ((sscanf(*argv,"%d",&num) == 0) || (num < 0)))
182112158Sdas		{
183112158Sdasbad:
184112158Sdas		BIO_printf(bio_err,"usage: genrsa [args] [numbits]\n");
185112158Sdas		BIO_printf(bio_err," -des            encrypt the generated key with DES in cbc mode\n");
186112158Sdas		BIO_printf(bio_err," -des3           encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
187112158Sdas#ifndef OPENSSL_NO_IDEA
188112158Sdas		BIO_printf(bio_err," -idea           encrypt the generated key with IDEA in cbc mode\n");
189112158Sdas#endif
190112158Sdas#ifndef OPENSSL_NO_AES
191112158Sdas		BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
192112158Sdas		BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
193112158Sdas#endif
194112158Sdas		BIO_printf(bio_err," -out file       output the key to 'file\n");
195112158Sdas		BIO_printf(bio_err," -passout arg    output file pass phrase source\n");
196187808Sdas		BIO_printf(bio_err," -f4             use F4 (0x10001) for the E value\n");
197187808Sdas		BIO_printf(bio_err," -3              use 3 for the E value\n");
198187808Sdas#ifndef OPENSSL_NO_ENGINE
199112158Sdas		BIO_printf(bio_err," -engine e       use engine e, possibly a hardware device.\n");
200112158Sdas#endif
201112158Sdas		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
202112158Sdas		BIO_printf(bio_err,"                 load the file (or the files in the directory) into\n");
203112158Sdas		BIO_printf(bio_err,"                 the random number generator\n");
204112158Sdas		goto err;
205112158Sdas		}
206112158Sdas
207112158Sdas	ERR_load_crypto_strings();
208112158Sdas
209112158Sdas	if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
210112158Sdas		BIO_printf(bio_err, "Error getting password\n");
211112158Sdas		goto err;
212112158Sdas	}
213112158Sdas
214112158Sdas#ifndef OPENSSL_NO_ENGINE
215112158Sdas        e = setup_engine(bio_err, engine, 0);
216112158Sdas#endif
217112158Sdas
218112158Sdas	if (outfile == NULL)
219112158Sdas		{
220112158Sdas		BIO_set_fp(out,stdout,BIO_NOCLOSE);
221112158Sdas#ifdef OPENSSL_SYS_VMS
222112158Sdas		{
223112158Sdas		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
224112158Sdas		out = BIO_push(tmpbio, out);
225112158Sdas		}
226112158Sdas#endif
227112158Sdas		}
228112158Sdas	else
229112158Sdas		{
230112158Sdas		if (BIO_write_filename(out,outfile) <= 0)
231112158Sdas			{
232112158Sdas			perror(outfile);
233112158Sdas			goto err;
234112158Sdas			}
235112158Sdas		}
236165743Sdas
237112158Sdas	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL
238112158Sdas		&& !RAND_status())
239112158Sdas		{
240112158Sdas		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
241112158Sdas		}
242112158Sdas	if (inrand != NULL)
243112158Sdas		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
244112158Sdas			app_RAND_load_files(inrand));
245112158Sdas
246112158Sdas	BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n",
247112158Sdas		num);
248112158Sdas
249112158Sdas	if(!BN_set_word(bn, f4) || !RSA_generate_key_ex(rsa, num, bn, &cb))
250112158Sdas		goto err;
251112158Sdas
252112158Sdas	app_RAND_write_file(NULL, bio_err);
253112158Sdas
254112158Sdas	/* We need to do the following for when the base number size is <
255112158Sdas	 * long, esp windows 3.1 :-(. */
256112158Sdas	l=0L;
257112158Sdas	for (i=0; i<rsa->e->top; i++)
258112158Sdas		{
259112158Sdas#ifndef SIXTY_FOUR_BIT
260112158Sdas		l<<=BN_BITS4;
261112158Sdas		l<<=BN_BITS4;
262112158Sdas#endif
263112158Sdas		l+=rsa->e->d[i];
264112158Sdas		}
265112158Sdas	BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l);
266112158Sdas	{
267112158Sdas	PW_CB_DATA cb_data;
268112158Sdas	cb_data.password = passout;
269112158Sdas	cb_data.prompt_info = outfile;
270112158Sdas	if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,
271112158Sdas		(pem_password_cb *)password_callback,&cb_data))
272112158Sdas		goto err;
273112158Sdas	}
274112158Sdas
275112158Sdas	ret=0;
276112158Sdaserr:
277112158Sdas	if (bn) BN_free(bn);
278112158Sdas	if (rsa) RSA_free(rsa);
279112158Sdas	if (out) BIO_free_all(out);
280112158Sdas	if(passout) OPENSSL_free(passout);
281112158Sdas	if (ret != 0)
282112158Sdas		ERR_print_errors(bio_err);
283112158Sdas	apps_shutdown();
284112158Sdas	OPENSSL_EXIT(ret);
285112158Sdas	}
286112158Sdas
287112158Sdasstatic int MS_CALLBACK genrsa_cb(int p, int n, BN_GENCB *cb)
288112158Sdas	{
289112158Sdas	char c='*';
290112158Sdas
291112158Sdas	if (p == 0) c='.';
292112158Sdas	if (p == 1) c='+';
293112158Sdas	if (p == 2) c='*';
294112158Sdas	if (p == 3) c='\n';
295112158Sdas	BIO_write(cb->arg,&c,1);
296112158Sdas	(void)BIO_flush(cb->arg);
297112158Sdas#ifdef LINT
298112158Sdas	p=n;
299112158Sdas#endif
300112158Sdas	return 1;
301112158Sdas	}
302112158Sdas#else /* !OPENSSL_NO_RSA */
303112158Sdas
304112158Sdas# if PEDANTIC
305112158Sdasstatic void *dummy=&dummy;
306112158Sdas# endif
307112158Sdas
308112158Sdas#endif
309112158Sdas