blob: ebee7f0269550df5f055a14b417c5bbd4b4b6f5d [file] [log] [blame]
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001/* apps/gendsa.c */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
Richard Levittecf1b7d92001-02-19 16:06:34 +000059#ifndef OPENSSL_NO_DSA
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000060#include <stdio.h>
61#include <string.h>
62#include <sys/types.h>
63#include <sys/stat.h>
64#include "apps.h"
Bodo Möllerec577821999-04-23 22:13:45 +000065#include <openssl/bio.h>
Bodo Möllerec577821999-04-23 22:13:45 +000066#include <openssl/err.h>
67#include <openssl/bn.h>
68#include <openssl/dsa.h>
69#include <openssl/x509.h>
70#include <openssl/pem.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000071
72#define DEFBITS 512
73#undef PROG
74#define PROG gendsa_main
75
Ralf S. Engelschall667ac4e2000-02-11 09:47:18 +000076int MAIN(int, char **);
77
Ulf Möller6b691a51999-04-19 21:31:43 +000078int MAIN(int argc, char **argv)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000079 {
Richard Levitte5270e702000-10-26 21:07:28 +000080 ENGINE *e = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000081 DSA *dsa=NULL;
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +000082 int ret=1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000083 char *outfile=NULL;
Richard Levittef3656112000-06-28 16:47:45 +000084 char *inrand=NULL,*dsaparams=NULL;
Dr. Stephen Hensona3fe3822000-02-16 23:16:01 +000085 char *passargout = NULL, *passout = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000086 BIO *out=NULL,*in=NULL;
Dr. Stephen Henson13588352001-03-09 02:51:02 +000087 const EVP_CIPHER *enc=NULL;
Richard Levitte5270e702000-10-26 21:07:28 +000088 char *engine=NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000089
90 apps_startup();
91
92 if (bio_err == NULL)
93 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000094 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000095
96 argv++;
97 argc--;
98 for (;;)
99 {
100 if (argc <= 0) break;
101 if (strcmp(*argv,"-out") == 0)
102 {
103 if (--argc < 1) goto bad;
104 outfile= *(++argv);
105 }
Dr. Stephen Hensonf07fb9b2000-02-08 01:34:59 +0000106 else if (strcmp(*argv,"-passout") == 0)
107 {
108 if (--argc < 1) goto bad;
Dr. Stephen Hensona3fe3822000-02-16 23:16:01 +0000109 passargout= *(++argv);
Dr. Stephen Hensonf07fb9b2000-02-08 01:34:59 +0000110 }
Richard Levitte5270e702000-10-26 21:07:28 +0000111 else if (strcmp(*argv,"-engine") == 0)
112 {
113 if (--argc < 1) goto bad;
114 engine= *(++argv);
115 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000116 else if (strcmp(*argv,"-rand") == 0)
117 {
118 if (--argc < 1) goto bad;
119 inrand= *(++argv);
120 }
121 else if (strcmp(*argv,"-") == 0)
122 goto bad;
Richard Levittecf1b7d92001-02-19 16:06:34 +0000123#ifndef OPENSSL_NO_DES
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000124 else if (strcmp(*argv,"-des") == 0)
125 enc=EVP_des_cbc();
126 else if (strcmp(*argv,"-des3") == 0)
127 enc=EVP_des_ede3_cbc();
128#endif
Richard Levittecf1b7d92001-02-19 16:06:34 +0000129#ifndef OPENSSL_NO_IDEA
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000130 else if (strcmp(*argv,"-idea") == 0)
131 enc=EVP_idea_cbc();
132#endif
Richard Levitteb3dfaaa2002-02-20 18:03:07 +0000133#ifndef OPENSSL_NO_AES
134 else if (strcmp(*argv,"-aes128") == 0)
135 enc=EVP_aes_128_cbc();
136 else if (strcmp(*argv,"-aes192") == 0)
137 enc=EVP_aes_192_cbc();
138 else if (strcmp(*argv,"-aes256") == 0)
139 enc=EVP_aes_256_cbc();
140#endif
Bodo Möllere42979f1999-08-06 11:18:44 +0000141 else if (**argv != '-' && dsaparams == NULL)
Bodo Möllerc69e3611999-07-14 18:37:51 +0000142 {
143 dsaparams = *argv;
144 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000145 else
146 goto bad;
147 argv++;
148 argc--;
149 }
150
151 if (dsaparams == NULL)
152 {
153bad:
Ralf S. Engelschallf2f351c1999-02-23 08:52:20 +0000154 BIO_printf(bio_err,"usage: gendsa [args] dsaparam-file\n");
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000155 BIO_printf(bio_err," -out file - output the key to 'file'\n");
Richard Levittecf1b7d92001-02-19 16:06:34 +0000156#ifndef OPENSSL_NO_DES
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000157 BIO_printf(bio_err," -des - encrypt the generated key with DES in cbc mode\n");
158 BIO_printf(bio_err," -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
159#endif
Richard Levittecf1b7d92001-02-19 16:06:34 +0000160#ifndef OPENSSL_NO_IDEA
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000161 BIO_printf(bio_err," -idea - encrypt the generated key with IDEA in cbc mode\n");
162#endif
Richard Levitteb3dfaaa2002-02-20 18:03:07 +0000163#ifndef OPENSSL_NO_AES
164 BIO_printf(bio_err," -aes128, -aes192, -aes256\n");
165 BIO_printf(bio_err," encrypt PEM output with cbc aes\n");
166#endif
Richard Levitte5270e702000-10-26 21:07:28 +0000167 BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\n");
Bodo Möllerafbd0742000-03-01 11:45:53 +0000168 BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000169 BIO_printf(bio_err," - load the file (or the files in the directory) into\n");
170 BIO_printf(bio_err," the random number generator\n");
Ralf S. Engelschallf2f351c1999-02-23 08:52:20 +0000171 BIO_printf(bio_err," dsaparam-file\n");
172 BIO_printf(bio_err," - a DSA parameter file as generated by the dsaparam command\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000173 goto end;
174 }
175
Dr. Stephen Hensona3376fe2001-06-19 00:23:47 +0000176 e = setup_engine(bio_err, engine, 0);
Richard Levitte5270e702000-10-26 21:07:28 +0000177
Dr. Stephen Hensona3fe3822000-02-16 23:16:01 +0000178 if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
179 BIO_printf(bio_err, "Error getting password\n");
180 goto end;
181 }
182
183
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000184 in=BIO_new(BIO_s_file());
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000185 if (!(BIO_read_filename(in,dsaparams)))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000186 {
187 perror(dsaparams);
188 goto end;
189 }
190
Bodo Möller74678cc1999-07-21 20:57:16 +0000191 if ((dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000192 {
193 BIO_printf(bio_err,"unable to load DSA parameter file\n");
194 goto end;
195 }
196 BIO_free(in);
Bodo Möller8652d1c2000-02-06 02:48:53 +0000197 in = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000198
199 out=BIO_new(BIO_s_file());
200 if (out == NULL) goto end;
201
202 if (outfile == NULL)
Richard Levitte645749e2000-09-20 13:55:50 +0000203 {
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000204 BIO_set_fp(out,stdout,BIO_NOCLOSE);
Richard Levittebc36ee62001-02-20 08:13:47 +0000205#ifdef OPENSSL_SYS_VMS
Richard Levitte645749e2000-09-20 13:55:50 +0000206 {
207 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
208 out = BIO_push(tmpbio, out);
209 }
210#endif
211 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000212 else
213 {
214 if (BIO_write_filename(out,outfile) <= 0)
215 {
216 perror(outfile);
217 goto end;
218 }
219 }
220
Richard Levittef3656112000-06-28 16:47:45 +0000221 if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000222 {
Bodo Möllera31011e1999-10-26 01:56:29 +0000223 BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000224 }
Bodo Möllera31011e1999-10-26 01:56:29 +0000225 if (inrand != NULL)
226 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
227 app_RAND_load_files(inrand));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000228
Dr. Stephen Henson7f9b7b01999-01-09 17:29:34 +0000229 BIO_printf(bio_err,"Generating DSA key, %d bits\n",
230 BN_num_bits(dsa->p));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000231 if (!DSA_generate_key(dsa)) goto end;
232
Bodo Möllera31011e1999-10-26 01:56:29 +0000233 app_RAND_write_file(NULL, bio_err);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000234
Dr. Stephen Hensona3fe3822000-02-16 23:16:01 +0000235 if (!PEM_write_bio_DSAPrivateKey(out,dsa,enc,NULL,0,NULL, passout))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000236 goto end;
237 ret=0;
238end:
239 if (ret != 0)
240 ERR_print_errors(bio_err);
Bodo Möller8652d1c2000-02-06 02:48:53 +0000241 if (in != NULL) BIO_free(in);
Richard Levitte645749e2000-09-20 13:55:50 +0000242 if (out != NULL) BIO_free_all(out);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000243 if (dsa != NULL) DSA_free(dsa);
Richard Levitte26a3a482000-06-01 22:19:21 +0000244 if(passout) OPENSSL_free(passout);
Richard Levittec04f8cf2001-06-23 16:37:32 +0000245 apps_shutdown();
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000246 EXIT(ret);
247 }
Ulf Möllerf5d7a031999-04-27 01:14:46 +0000248#endif