RT4639: Typo when -DSSL_DEBUG
[openssl.git] / apps / dsaparam.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11 #ifdef OPENSSL_NO_DSA
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <stdio.h>
16 # include <stdlib.h>
17 # include <time.h>
18 # include <string.h>
19 # include "apps.h"
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/dsa.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
26
27 # ifdef GENCB_TEST
28
29 static int stop_keygen_flag = 0;
30
31 static void timebomb_sigalarm(int foo)
32 {
33     stop_keygen_flag = 1;
34 }
35
36 # endif
37
38 static int dsa_cb(int p, int n, BN_GENCB *cb);
39
40 typedef enum OPTION_choice {
41     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
42     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
43     OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE,
44     OPT_TIMEBOMB
45 } OPTION_CHOICE;
46
47 OPTIONS dsaparam_options[] = {
48     {"help", OPT_HELP, '-', "Display this summary"},
49     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
50     {"in", OPT_IN, '<', "Input file"},
51     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
52     {"out", OPT_OUT, '>', "Output file"},
53     {"text", OPT_TEXT, '-', "Print as text"},
54     {"C", OPT_C, '-', "Output C code"},
55     {"noout", OPT_NOOUT, '-', "No output"},
56     {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
57     {"rand", OPT_RAND, 's', "Files to use for random number input"},
58 # ifdef GENCB_TEST
59     {"timebomb", OPT_TIMEBOMB, 'p', "Interrupt keygen after 'pnum' seconds"},
60 # endif
61 # ifndef OPENSSL_NO_ENGINE
62     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
63 # endif
64     {NULL}
65 };
66
67 int dsaparam_main(int argc, char **argv)
68 {
69     DSA *dsa = NULL;
70     BIO *in = NULL, *out = NULL;
71     BN_GENCB *cb = NULL;
72     int numbits = -1, num = 0, genkey = 0, need_rand = 0;
73     int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
74     int ret = 1, i, text = 0, private = 0;
75 # ifdef GENCB_TEST
76     int timebomb = 0;
77 # endif
78     char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL;
79     OPTION_CHOICE o;
80
81     prog = opt_init(argc, argv, dsaparam_options);
82     while ((o = opt_next()) != OPT_EOF) {
83         switch (o) {
84         case OPT_EOF:
85         case OPT_ERR:
86  opthelp:
87             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
88             goto end;
89         case OPT_HELP:
90             opt_help(dsaparam_options);
91             ret = 0;
92             goto end;
93         case OPT_INFORM:
94             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
95                 goto opthelp;
96             break;
97         case OPT_IN:
98             infile = opt_arg();
99             break;
100         case OPT_OUTFORM:
101             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
102                 goto opthelp;
103             break;
104         case OPT_OUT:
105             outfile = opt_arg();
106             break;
107         case OPT_ENGINE:
108             (void)setup_engine(opt_arg(), 0);
109             break;
110         case OPT_TIMEBOMB:
111 # ifdef GENCB_TEST
112             timebomb = atoi(opt_arg());
113             break;
114 # endif
115         case OPT_TEXT:
116             text = 1;
117             break;
118         case OPT_C:
119             C = 1;
120             break;
121         case OPT_GENKEY:
122             genkey = need_rand = 1;
123             break;
124         case OPT_RAND:
125             inrand = opt_arg();
126             need_rand = 1;
127             break;
128         case OPT_NOOUT:
129             noout = 1;
130             break;
131         }
132     }
133     argc = opt_num_rest();
134     argv = opt_rest();
135
136     if (argc == 1) {
137         if (!opt_int(argv[0], &num) || num < 0)
138             goto end;
139         /* generate a key */
140         numbits = num;
141         need_rand = 1;
142     }
143     private = genkey ? 1 : 0;
144
145     in = bio_open_default(infile, 'r', informat);
146     if (in == NULL)
147         goto end;
148     out = bio_open_owner(outfile, outformat, private);
149     if (out == NULL)
150         goto end;
151
152     if (need_rand) {
153         app_RAND_load_file(NULL, (inrand != NULL));
154         if (inrand != NULL)
155             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
156                        app_RAND_load_files(inrand));
157     }
158
159     if (numbits > 0) {
160         cb = BN_GENCB_new();
161         if (cb == NULL) {
162             BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
163             goto end;
164         }
165         BN_GENCB_set(cb, dsa_cb, bio_err);
166         assert(need_rand);
167         dsa = DSA_new();
168         if (dsa == NULL) {
169             BIO_printf(bio_err, "Error allocating DSA object\n");
170             goto end;
171         }
172         BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
173                    num);
174         BIO_printf(bio_err, "This could take some time\n");
175 # ifdef GENCB_TEST
176         if (timebomb > 0) {
177             struct sigaction act;
178             act.sa_handler = timebomb_sigalarm;
179             act.sa_flags = 0;
180             BIO_printf(bio_err,
181                        "(though I'll stop it if not done within %d secs)\n",
182                        timebomb);
183             if (sigaction(SIGALRM, &act, NULL) != 0) {
184                 BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n");
185                 goto end;
186             }
187             alarm(timebomb);
188         }
189 # endif
190         if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
191 # ifdef GENCB_TEST
192             if (stop_keygen_flag) {
193                 BIO_printf(bio_err, "DSA key generation time-stopped\n");
194                 /* This is an asked-for behaviour! */
195                 ret = 0;
196                 goto end;
197             }
198 # endif
199             ERR_print_errors(bio_err);
200             BIO_printf(bio_err, "Error, DSA key generation failed\n");
201             goto end;
202         }
203     } else if (informat == FORMAT_ASN1)
204         dsa = d2i_DSAparams_bio(in, NULL);
205     else
206         dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
207     if (dsa == NULL) {
208         BIO_printf(bio_err, "unable to load DSA parameters\n");
209         ERR_print_errors(bio_err);
210         goto end;
211     }
212
213     if (text) {
214         DSAparams_print(out, dsa);
215     }
216
217     if (C) {
218         const BIGNUM *p = NULL, *q = NULL, *g = NULL;
219         unsigned char *data;
220         int len, bits_p;
221
222         DSA_get0_pqg(dsa, &p, &q, &g);
223         len = BN_num_bytes(p);
224         bits_p = BN_num_bits(p);
225
226         data = app_malloc(len + 20, "BN space");
227
228         BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
229         print_bignum_var(bio_out, p, "dsap", len, data);
230         print_bignum_var(bio_out, q, "dsaq", len, data);
231         print_bignum_var(bio_out, g, "dsag", len, data);
232         BIO_printf(bio_out, "    DSA *dsa = DSA_new();\n"
233                             "\n");
234         BIO_printf(bio_out, "    if (dsa == NULL)\n"
235                             "        return NULL;\n");
236         BIO_printf(bio_out, "    dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n",
237                bits_p, bits_p);
238         BIO_printf(bio_out, "    dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n",
239                bits_p, bits_p);
240         BIO_printf(bio_out, "    dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n",
241                bits_p, bits_p);
242         BIO_printf(bio_out, "    if (!dsa->p || !dsa->q || !dsa->g) {\n"
243                             "        DSA_free(dsa);\n"
244                             "        return NULL;\n"
245                             "    }\n"
246                             "    return(dsa);\n}\n");
247         OPENSSL_free(data);
248     }
249
250     if (!noout) {
251         if (outformat == FORMAT_ASN1)
252             i = i2d_DSAparams_bio(out, dsa);
253         else
254             i = PEM_write_bio_DSAparams(out, dsa);
255         if (!i) {
256             BIO_printf(bio_err, "unable to write DSA parameters\n");
257             ERR_print_errors(bio_err);
258             goto end;
259         }
260     }
261     if (genkey) {
262         DSA *dsakey;
263
264         assert(need_rand);
265         if ((dsakey = DSAparams_dup(dsa)) == NULL)
266             goto end;
267         if (!DSA_generate_key(dsakey)) {
268             ERR_print_errors(bio_err);
269             DSA_free(dsakey);
270             goto end;
271         }
272         assert(private);
273         if (outformat == FORMAT_ASN1)
274             i = i2d_DSAPrivateKey_bio(out, dsakey);
275         else
276             i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
277                                             NULL);
278         DSA_free(dsakey);
279     }
280     if (need_rand)
281         app_RAND_write_file(NULL);
282     ret = 0;
283  end:
284     BN_GENCB_free(cb);
285     BIO_free(in);
286     BIO_free_all(out);
287     DSA_free(dsa);
288     return (ret);
289 }
290
291 static int dsa_cb(int p, int n, BN_GENCB *cb)
292 {
293     char c = '*';
294
295     if (p == 0)
296         c = '.';
297     if (p == 1)
298         c = '+';
299     if (p == 2)
300         c = '*';
301     if (p == 3)
302         c = '\n';
303     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
304     (void)BIO_flush(BN_GENCB_get_arg(cb));
305 # ifdef GENCB_TEST
306     if (stop_keygen_flag)
307         return 0;
308 # endif
309     return 1;
310 }
311 #endif