xref: /minix3/crypto/external/bsd/openssl/dist/apps/gendsa.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/gendsa.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */
60ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
61ebfedea0SLionel Sambuc # include <stdio.h>
62ebfedea0SLionel Sambuc # include <string.h>
63ebfedea0SLionel Sambuc # include <sys/types.h>
64ebfedea0SLionel Sambuc # include <sys/stat.h>
65ebfedea0SLionel Sambuc # include "apps.h"
66ebfedea0SLionel Sambuc # include <openssl/bio.h>
67ebfedea0SLionel Sambuc # include <openssl/err.h>
68ebfedea0SLionel Sambuc # include <openssl/bn.h>
69ebfedea0SLionel Sambuc # include <openssl/dsa.h>
70ebfedea0SLionel Sambuc # include <openssl/x509.h>
71ebfedea0SLionel Sambuc # include <openssl/pem.h>
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc # define DEFBITS 512
74ebfedea0SLionel Sambuc # undef PROG
75ebfedea0SLionel Sambuc # define PROG gendsa_main
76ebfedea0SLionel Sambuc 
77ebfedea0SLionel Sambuc int MAIN(int, char **);
78ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)79ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
80ebfedea0SLionel Sambuc {
81ebfedea0SLionel Sambuc     DSA *dsa = NULL;
82ebfedea0SLionel Sambuc     int ret = 1;
83ebfedea0SLionel Sambuc     char *outfile = NULL;
84ebfedea0SLionel Sambuc     char *inrand = NULL, *dsaparams = NULL;
85ebfedea0SLionel Sambuc     char *passargout = NULL, *passout = NULL;
86ebfedea0SLionel Sambuc     BIO *out = NULL, *in = NULL;
87ebfedea0SLionel Sambuc     const EVP_CIPHER *enc = NULL;
88ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
89ebfedea0SLionel Sambuc     char *engine = NULL;
90ebfedea0SLionel Sambuc # endif
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc     apps_startup();
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc     if (bio_err == NULL)
95ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
96ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
99ebfedea0SLionel Sambuc         goto end;
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     argv++;
102ebfedea0SLionel Sambuc     argc--;
103*0a6a1f1dSLionel Sambuc     for (;;) {
104*0a6a1f1dSLionel Sambuc         if (argc <= 0)
105*0a6a1f1dSLionel Sambuc             break;
106*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-out") == 0) {
107*0a6a1f1dSLionel Sambuc             if (--argc < 1)
108*0a6a1f1dSLionel Sambuc                 goto bad;
109ebfedea0SLionel Sambuc             outfile = *(++argv);
110*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-passout") == 0) {
111*0a6a1f1dSLionel Sambuc             if (--argc < 1)
112*0a6a1f1dSLionel Sambuc                 goto bad;
113ebfedea0SLionel Sambuc             passargout = *(++argv);
114ebfedea0SLionel Sambuc         }
115ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
116*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-engine") == 0) {
117*0a6a1f1dSLionel Sambuc             if (--argc < 1)
118*0a6a1f1dSLionel Sambuc                 goto bad;
119ebfedea0SLionel Sambuc             engine = *(++argv);
120ebfedea0SLionel Sambuc         }
121ebfedea0SLionel Sambuc # endif
122*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-rand") == 0) {
123*0a6a1f1dSLionel Sambuc             if (--argc < 1)
124*0a6a1f1dSLionel Sambuc                 goto bad;
125ebfedea0SLionel Sambuc             inrand = *(++argv);
126*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-") == 0)
127ebfedea0SLionel Sambuc             goto bad;
128ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DES
129ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-des") == 0)
130ebfedea0SLionel Sambuc             enc = EVP_des_cbc();
131ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-des3") == 0)
132ebfedea0SLionel Sambuc             enc = EVP_des_ede3_cbc();
133ebfedea0SLionel Sambuc # endif
134ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_IDEA
135ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-idea") == 0)
136ebfedea0SLionel Sambuc             enc = EVP_idea_cbc();
137ebfedea0SLionel Sambuc # endif
138ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SEED
139ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-seed") == 0)
140ebfedea0SLionel Sambuc             enc = EVP_seed_cbc();
141ebfedea0SLionel Sambuc # endif
142ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_AES
143ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-aes128") == 0)
144ebfedea0SLionel Sambuc             enc = EVP_aes_128_cbc();
145ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-aes192") == 0)
146ebfedea0SLionel Sambuc             enc = EVP_aes_192_cbc();
147ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-aes256") == 0)
148ebfedea0SLionel Sambuc             enc = EVP_aes_256_cbc();
149ebfedea0SLionel Sambuc # endif
150ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_CAMELLIA
151ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-camellia128") == 0)
152ebfedea0SLionel Sambuc             enc = EVP_camellia_128_cbc();
153ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-camellia192") == 0)
154ebfedea0SLionel Sambuc             enc = EVP_camellia_192_cbc();
155ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-camellia256") == 0)
156ebfedea0SLionel Sambuc             enc = EVP_camellia_256_cbc();
157ebfedea0SLionel Sambuc # endif
158*0a6a1f1dSLionel Sambuc         else if (**argv != '-' && dsaparams == NULL) {
159ebfedea0SLionel Sambuc             dsaparams = *argv;
160*0a6a1f1dSLionel Sambuc         } else
161ebfedea0SLionel Sambuc             goto bad;
162ebfedea0SLionel Sambuc         argv++;
163ebfedea0SLionel Sambuc         argc--;
164ebfedea0SLionel Sambuc     }
165ebfedea0SLionel Sambuc 
166*0a6a1f1dSLionel Sambuc     if (dsaparams == NULL) {
167ebfedea0SLionel Sambuc  bad:
168ebfedea0SLionel Sambuc         BIO_printf(bio_err, "usage: gendsa [args] dsaparam-file\n");
169ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -out file - output the key to 'file'\n");
170ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DES
171*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
172*0a6a1f1dSLionel Sambuc                    " -des      - encrypt the generated key with DES in cbc mode\n");
173*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
174*0a6a1f1dSLionel Sambuc                    " -des3     - encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
175ebfedea0SLionel Sambuc # endif
176ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_IDEA
177*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
178*0a6a1f1dSLionel Sambuc                    " -idea     - encrypt the generated key with IDEA in cbc mode\n");
179ebfedea0SLionel Sambuc # endif
180ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SEED
181ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -seed\n");
182*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
183*0a6a1f1dSLionel Sambuc                    "                 encrypt PEM output with cbc seed\n");
184ebfedea0SLionel Sambuc # endif
185ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_AES
186ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
187*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
188*0a6a1f1dSLionel Sambuc                    "                 encrypt PEM output with cbc aes\n");
189ebfedea0SLionel Sambuc # endif
190ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_CAMELLIA
191ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
192*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
193*0a6a1f1dSLionel Sambuc                    "                 encrypt PEM output with cbc camellia\n");
194ebfedea0SLionel Sambuc # endif
195ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
196*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
197*0a6a1f1dSLionel Sambuc                    " -engine e - use engine e, possibly a hardware device.\n");
198ebfedea0SLionel Sambuc # endif
199*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
200*0a6a1f1dSLionel Sambuc                    LIST_SEPARATOR_CHAR);
201*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
202*0a6a1f1dSLionel Sambuc                    "           - load the file (or the files in the directory) into\n");
203ebfedea0SLionel Sambuc         BIO_printf(bio_err, "             the random number generator\n");
204ebfedea0SLionel Sambuc         BIO_printf(bio_err, " dsaparam-file\n");
205*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
206*0a6a1f1dSLionel Sambuc                    "           - a DSA parameter file as generated by the dsaparam command\n");
207ebfedea0SLionel Sambuc         goto end;
208ebfedea0SLionel Sambuc     }
209ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
210ebfedea0SLionel Sambuc     setup_engine(bio_err, engine, 0);
211ebfedea0SLionel Sambuc # endif
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
214ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error getting password\n");
215ebfedea0SLionel Sambuc         goto end;
216ebfedea0SLionel Sambuc     }
217ebfedea0SLionel Sambuc 
218ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file());
219*0a6a1f1dSLionel Sambuc     if (!(BIO_read_filename(in, dsaparams))) {
220ebfedea0SLionel Sambuc         perror(dsaparams);
221ebfedea0SLionel Sambuc         goto end;
222ebfedea0SLionel Sambuc     }
223ebfedea0SLionel Sambuc 
224*0a6a1f1dSLionel Sambuc     if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
225ebfedea0SLionel Sambuc         BIO_printf(bio_err, "unable to load DSA parameter file\n");
226ebfedea0SLionel Sambuc         goto end;
227ebfedea0SLionel Sambuc     }
228ebfedea0SLionel Sambuc     BIO_free(in);
229ebfedea0SLionel Sambuc     in = NULL;
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
232*0a6a1f1dSLionel Sambuc     if (out == NULL)
233*0a6a1f1dSLionel Sambuc         goto end;
234ebfedea0SLionel Sambuc 
235*0a6a1f1dSLionel Sambuc     if (outfile == NULL) {
236ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
237ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
238ebfedea0SLionel Sambuc         {
239ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
240ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
241ebfedea0SLionel Sambuc         }
242ebfedea0SLionel Sambuc # endif
243*0a6a1f1dSLionel Sambuc     } else {
244*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outfile) <= 0) {
245ebfedea0SLionel Sambuc             perror(outfile);
246ebfedea0SLionel Sambuc             goto end;
247ebfedea0SLionel Sambuc         }
248ebfedea0SLionel Sambuc     }
249ebfedea0SLionel Sambuc 
250*0a6a1f1dSLionel Sambuc     if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) {
251*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
252*0a6a1f1dSLionel Sambuc                    "warning, not much extra random data, consider using the -rand option\n");
253ebfedea0SLionel Sambuc     }
254ebfedea0SLionel Sambuc     if (inrand != NULL)
255ebfedea0SLionel Sambuc         BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
256ebfedea0SLionel Sambuc                    app_RAND_load_files(inrand));
257ebfedea0SLionel Sambuc 
258*0a6a1f1dSLionel Sambuc     BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(dsa->p));
259*0a6a1f1dSLionel Sambuc     if (!DSA_generate_key(dsa))
260*0a6a1f1dSLionel Sambuc         goto end;
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc     app_RAND_write_file(NULL, bio_err);
263ebfedea0SLionel Sambuc 
264ebfedea0SLionel Sambuc     if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
265ebfedea0SLionel Sambuc         goto end;
266ebfedea0SLionel Sambuc     ret = 0;
267ebfedea0SLionel Sambuc  end:
268ebfedea0SLionel Sambuc     if (ret != 0)
269ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
270*0a6a1f1dSLionel Sambuc     if (in != NULL)
271*0a6a1f1dSLionel Sambuc         BIO_free(in);
272*0a6a1f1dSLionel Sambuc     if (out != NULL)
273*0a6a1f1dSLionel Sambuc         BIO_free_all(out);
274*0a6a1f1dSLionel Sambuc     if (dsa != NULL)
275*0a6a1f1dSLionel Sambuc         DSA_free(dsa);
276*0a6a1f1dSLionel Sambuc     if (passout)
277*0a6a1f1dSLionel Sambuc         OPENSSL_free(passout);
278ebfedea0SLionel Sambuc     apps_shutdown();
279ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
280ebfedea0SLionel Sambuc }
281ebfedea0SLionel Sambuc #else                           /* !OPENSSL_NO_DSA */
282ebfedea0SLionel Sambuc 
283ebfedea0SLionel Sambuc # if PEDANTIC
284ebfedea0SLionel Sambuc static void *dummy = &dummy;
285ebfedea0SLionel Sambuc # endif
286ebfedea0SLionel Sambuc 
287ebfedea0SLionel Sambuc #endif
288