gendsa.c revision 109998
1166124Srafan/* apps/gendsa.c */
2166124Srafan/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3166124Srafan * All rights reserved.
4166124Srafan *
5166124Srafan * This package is an SSL implementation written
6166124Srafan * by Eric Young (eay@cryptsoft.com).
7166124Srafan * The implementation was written so as to conform with Netscapes SSL.
8166124Srafan *
9166124Srafan * This library is free for commercial and non-commercial use as long as
10166124Srafan * the following conditions are aheared to.  The following conditions
11166124Srafan * apply to all code found in this distribution, be it the RC4, RSA,
12166124Srafan * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13166124Srafan * included with this distribution is covered by the same copyright terms
14166124Srafan * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15166124Srafan *
16166124Srafan * Copyright remains Eric Young's, and as such any Copyright notices in
17166124Srafan * the code are not to be removed.
18166124Srafan * If this package is used in a product, Eric Young should be given attribution
19166124Srafan * as the author of the parts of the library used.
20166124Srafan * This can be in the form of a textual message at program startup or
21166124Srafan * in documentation (online or textual) provided with the package.
22166124Srafan *
23166124Srafan * Redistribution and use in source and binary forms, with or without
24166124Srafan * modification, are permitted provided that the following conditions
25166124Srafan * are met:
26166124Srafan * 1. Redistributions of source code must retain the copyright
27166124Srafan *    notice, this list of conditions and the following disclaimer.
28166124Srafan * 2. Redistributions in binary form must reproduce the above copyright
29166124Srafan *    notice, this list of conditions and the following disclaimer in the
30166124Srafan *    documentation and/or other materials provided with the distribution.
31166124Srafan * 3. All advertising materials mentioning features or use of this software
32166124Srafan *    must display the following acknowledgement:
33166124Srafan *    "This product includes cryptographic software written by
34166124Srafan *     Eric Young (eay@cryptsoft.com)"
35166124Srafan *    The word 'cryptographic' can be left out if the rouines from the library
36166124Srafan *    being used are not cryptographic related :-).
37166124Srafan * 4. If you include any Windows specific code (or a derivative thereof) from
38166124Srafan *    the apps directory (application code) you must include an acknowledgement:
39166124Srafan *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40166124Srafan *
41166124Srafan * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42166124Srafan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43166124Srafan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44166124Srafan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45166124Srafan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46166124Srafan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47166124Srafan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48166124Srafan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49166124Srafan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50166124Srafan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51166124Srafan * SUCH DAMAGE.
52166124Srafan *
53166124Srafan * The licence and distribution terms for any publically available version or
54166124Srafan * derivative of this code cannot be changed.  i.e. this code cannot simply be
55166124Srafan * copied and put under another distribution licence
56166124Srafan * [including the GNU Public Licence.]
57166124Srafan */
58166124Srafan
59166124Srafan#ifndef OPENSSL_NO_DSA
60166124Srafan#include <stdio.h>
61166124Srafan#include <string.h>
62166124Srafan#include <sys/types.h>
63166124Srafan#include <sys/stat.h>
64166124Srafan#include "apps.h"
65166124Srafan#include <openssl/bio.h>
66166124Srafan#include <openssl/err.h>
67166124Srafan#include <openssl/bn.h>
68166124Srafan#include <openssl/dsa.h>
69166124Srafan#include <openssl/x509.h>
70166124Srafan#include <openssl/pem.h>
71166124Srafan
72166124Srafan#define DEFBITS	512
73166124Srafan#undef PROG
74166124Srafan#define PROG gendsa_main
75166124Srafan
76166124Srafanint MAIN(int, char **);
77166124Srafan
78166124Srafanint MAIN(int argc, char **argv)
79166124Srafan	{
80166124Srafan	ENGINE *e = NULL;
81166124Srafan	DSA *dsa=NULL;
82166124Srafan	int ret=1;
83166124Srafan	char *outfile=NULL;
84166124Srafan	char *inrand=NULL,*dsaparams=NULL;
85166124Srafan	char *passargout = NULL, *passout = NULL;
86166124Srafan	BIO *out=NULL,*in=NULL;
87166124Srafan	const EVP_CIPHER *enc=NULL;
88166124Srafan	char *engine=NULL;
89166124Srafan
90166124Srafan	apps_startup();
91166124Srafan
92166124Srafan	if (bio_err == NULL)
93166124Srafan		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
94166124Srafan			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
95166124Srafan
96166124Srafan	if (!load_config(bio_err, NULL))
97166124Srafan		goto end;
98166124Srafan
99166124Srafan	argv++;
100166124Srafan	argc--;
101166124Srafan	for (;;)
102166124Srafan		{
103166124Srafan		if (argc <= 0) break;
104166124Srafan		if (strcmp(*argv,"-out") == 0)
105166124Srafan			{
106166124Srafan			if (--argc < 1) goto bad;
107166124Srafan			outfile= *(++argv);
108166124Srafan			}
109166124Srafan		else if (strcmp(*argv,"-passout") == 0)
110166124Srafan			{
111166124Srafan			if (--argc < 1) goto bad;
112166124Srafan			passargout= *(++argv);
113166124Srafan			}
114166124Srafan		else if (strcmp(*argv,"-engine") == 0)
115166124Srafan			{
116166124Srafan			if (--argc < 1) goto bad;
117166124Srafan			engine= *(++argv);
118166124Srafan			}
119166124Srafan		else if (strcmp(*argv,"-rand") == 0)
120166124Srafan			{
121166124Srafan			if (--argc < 1) goto bad;
122166124Srafan			inrand= *(++argv);
123166124Srafan			}
124166124Srafan		else if (strcmp(*argv,"-") == 0)
125166124Srafan			goto bad;
126166124Srafan#ifndef OPENSSL_NO_DES
127166124Srafan		else if (strcmp(*argv,"-des") == 0)
128166124Srafan			enc=EVP_des_cbc();
129166124Srafan		else if (strcmp(*argv,"-des3") == 0)
130166124Srafan			enc=EVP_des_ede3_cbc();
131166124Srafan#endif
132166124Srafan#ifndef OPENSSL_NO_IDEA
133166124Srafan		else if (strcmp(*argv,"-idea") == 0)
134166124Srafan			enc=EVP_idea_cbc();
135166124Srafan#endif
136166124Srafan#ifndef OPENSSL_NO_AES
137166124Srafan		else if (strcmp(*argv,"-aes128") == 0)
138166124Srafan			enc=EVP_aes_128_cbc();
139166124Srafan		else if (strcmp(*argv,"-aes192") == 0)
140166124Srafan			enc=EVP_aes_192_cbc();
141166124Srafan		else if (strcmp(*argv,"-aes256") == 0)
142166124Srafan			enc=EVP_aes_256_cbc();
143166124Srafan#endif
144166124Srafan		else if (**argv != '-' && dsaparams == NULL)
145166124Srafan			{
146166124Srafan			dsaparams = *argv;
147166124Srafan			}
148166124Srafan		else
149166124Srafan			goto bad;
150166124Srafan		argv++;
151166124Srafan		argc--;
152166124Srafan		}
153166124Srafan
154166124Srafan	if (dsaparams == NULL)
155166124Srafan		{
156166124Srafanbad:
157166124Srafan		BIO_printf(bio_err,"usage: gendsa [args] dsaparam-file\n");
158166124Srafan		BIO_printf(bio_err," -out file - output the key to 'file'\n");
159166124Srafan#ifndef OPENSSL_NO_DES
160166124Srafan		BIO_printf(bio_err," -des      - encrypt the generated key with DES in cbc mode\n");
161166124Srafan		BIO_printf(bio_err," -des3     - encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
162166124Srafan#endif
163166124Srafan#ifndef OPENSSL_NO_IDEA
164166124Srafan		BIO_printf(bio_err," -idea     - encrypt the generated key with IDEA in cbc mode\n");
165166124Srafan#endif
166166124Srafan#ifndef OPENSSL_NO_AES
167166124Srafan		BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
168166124Srafan		BIO_printf(bio_err,"                 encrypt PEM output with cbc aes\n");
169166124Srafan#endif
170166124Srafan		BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n");
171166124Srafan		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
172166124Srafan		BIO_printf(bio_err,"           - load the file (or the files in the directory) into\n");
173166124Srafan		BIO_printf(bio_err,"             the random number generator\n");
174166124Srafan		BIO_printf(bio_err," dsaparam-file\n");
175166124Srafan		BIO_printf(bio_err,"           - a DSA parameter file as generated by the dsaparam command\n");
176166124Srafan		goto end;
177166124Srafan		}
178166124Srafan
179166124Srafan        e = setup_engine(bio_err, engine, 0);
180166124Srafan
181166124Srafan	if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
182166124Srafan		BIO_printf(bio_err, "Error getting password\n");
183166124Srafan		goto end;
184166124Srafan	}
185166124Srafan
186166124Srafan
187166124Srafan	in=BIO_new(BIO_s_file());
188166124Srafan	if (!(BIO_read_filename(in,dsaparams)))
189166124Srafan		{
190166124Srafan		perror(dsaparams);
191166124Srafan		goto end;
192166124Srafan		}
193166124Srafan
194166124Srafan	if ((dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
195166124Srafan		{
196166124Srafan		BIO_printf(bio_err,"unable to load DSA parameter file\n");
197166124Srafan		goto end;
198166124Srafan		}
199166124Srafan	BIO_free(in);
200166124Srafan	in = NULL;
201166124Srafan
202166124Srafan	out=BIO_new(BIO_s_file());
203166124Srafan	if (out == NULL) goto end;
204166124Srafan
205166124Srafan	if (outfile == NULL)
206166124Srafan		{
207166124Srafan		BIO_set_fp(out,stdout,BIO_NOCLOSE);
208166124Srafan#ifdef OPENSSL_SYS_VMS
209166124Srafan		{
210166124Srafan		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
211166124Srafan		out = BIO_push(tmpbio, out);
212166124Srafan		}
213166124Srafan#endif
214166124Srafan		}
215166124Srafan	else
216166124Srafan		{
217166124Srafan		if (BIO_write_filename(out,outfile) <= 0)
218166124Srafan			{
219166124Srafan			perror(outfile);
220166124Srafan			goto end;
221166124Srafan			}
222166124Srafan		}
223166124Srafan
224166124Srafan	if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
225166124Srafan		{
226166124Srafan		BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
227166124Srafan		}
228166124Srafan	if (inrand != NULL)
229166124Srafan		BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
230166124Srafan			app_RAND_load_files(inrand));
231166124Srafan
232166124Srafan	BIO_printf(bio_err,"Generating DSA key, %d bits\n",
233166124Srafan							BN_num_bits(dsa->p));
234166124Srafan	if (!DSA_generate_key(dsa)) goto end;
235166124Srafan
236166124Srafan	app_RAND_write_file(NULL, bio_err);
237166124Srafan
238166124Srafan	if (!PEM_write_bio_DSAPrivateKey(out,dsa,enc,NULL,0,NULL, passout))
239166124Srafan		goto end;
240166124Srafan	ret=0;
241166124Srafanend:
242166124Srafan	if (ret != 0)
243166124Srafan		ERR_print_errors(bio_err);
244166124Srafan	if (in != NULL) BIO_free(in);
245166124Srafan	if (out != NULL) BIO_free_all(out);
246166124Srafan	if (dsa != NULL) DSA_free(dsa);
247166124Srafan	if(passout) OPENSSL_free(passout);
248166124Srafan	apps_shutdown();
249166124Srafan	OPENSSL_EXIT(ret);
250166124Srafan	}
251166124Srafan#endif
252166124Srafan