Don't compile commands if disabled
[oweals/openssl.git] / apps / dsaparam.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <openssl/opensslconf.h>
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <string.h>
19 #include "apps.h"
20 #include "progs.h"
21 #include <openssl/bio.h>
22 #include <openssl/err.h>
23 #include <openssl/bn.h>
24 #include <openssl/dsa.h>
25 #include <openssl/x509.h>
26 #include <openssl/pem.h>
27
28 static int verbose = 0;
29
30 static int dsa_cb(int p, int n, BN_GENCB *cb);
31
32 typedef enum OPTION_choice {
33     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
34     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
35     OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
36     OPT_R_ENUM, OPT_PROV_ENUM
37 } OPTION_CHOICE;
38
39 const OPTIONS dsaparam_options[] = {
40     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
41
42     OPT_SECTION("General"),
43     {"help", OPT_HELP, '-', "Display this summary"},
44 #ifndef OPENSSL_NO_ENGINE
45     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
46 #endif
47
48     OPT_SECTION("Input"),
49     {"in", OPT_IN, '<', "Input file"},
50     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
51
52     OPT_SECTION("Output"),
53     {"out", OPT_OUT, '>', "Output file"},
54     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
55     {"text", OPT_TEXT, '-', "Print as text"},
56     {"C", OPT_C, '-', "Output C code"},
57     {"noout", OPT_NOOUT, '-', "No output"},
58     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
59     {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
60
61     OPT_R_OPTIONS,
62     OPT_PROV_OPTIONS,
63
64     OPT_PARAMETERS(),
65     {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
66     {NULL}
67 };
68
69 int dsaparam_main(int argc, char **argv)
70 {
71     ENGINE *e = NULL;
72     DSA *dsa = NULL;
73     BIO *in = NULL, *out = NULL;
74     BN_GENCB *cb = NULL;
75     int numbits = -1, num = 0, genkey = 0;
76     int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
77     int ret = 1, i, text = 0, private = 0;
78     char *infile = NULL, *outfile = NULL, *prog;
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             e = setup_engine(opt_arg(), 0);
109             break;
110         case OPT_TEXT:
111             text = 1;
112             break;
113         case OPT_C:
114             C = 1;
115             break;
116         case OPT_GENKEY:
117             genkey = 1;
118             break;
119         case OPT_R_CASES:
120             if (!opt_rand(o))
121                 goto end;
122             break;
123         case OPT_PROV_CASES:
124             if (!opt_provider(o))
125                 goto end;
126             break;
127         case OPT_NOOUT:
128             noout = 1;
129             break;
130         case OPT_VERBOSE:
131             verbose = 1;
132             break;
133         }
134     }
135     argc = opt_num_rest();
136     argv = opt_rest();
137
138     if (argc == 1) {
139         if (!opt_int(argv[0], &num) || num < 0)
140             goto end;
141         /* generate a key */
142         numbits = num;
143     }
144     private = genkey ? 1 : 0;
145
146     in = bio_open_default(infile, 'r', informat);
147     if (in == NULL)
148         goto end;
149     out = bio_open_owner(outfile, outformat, private);
150     if (out == NULL)
151         goto end;
152
153     if (numbits > 0) {
154         if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
155             BIO_printf(bio_err,
156                        "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
157                        "         Your key size is %d! Larger key size may behave not as expected.\n",
158                        OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
159
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         dsa = DSA_new();
167         if (dsa == NULL) {
168             BIO_printf(bio_err, "Error allocating DSA object\n");
169             goto end;
170         }
171         if (verbose) {
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         }
176         if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
177             ERR_print_errors(bio_err);
178             BIO_printf(bio_err, "Error, DSA key generation failed\n");
179             goto end;
180         }
181     } else if (informat == FORMAT_ASN1) {
182         dsa = d2i_DSAparams_bio(in, NULL);
183     } else {
184         dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
185     }
186     if (dsa == NULL) {
187         BIO_printf(bio_err, "unable to load DSA parameters\n");
188         ERR_print_errors(bio_err);
189         goto end;
190     }
191
192     if (text) {
193         DSAparams_print(out, dsa);
194     }
195
196     if (C) {
197         const BIGNUM *p = NULL, *q = NULL, *g = NULL;
198         unsigned char *data;
199         int len, bits_p;
200
201         DSA_get0_pqg(dsa, &p, &q, &g);
202         len = BN_num_bytes(p);
203         bits_p = BN_num_bits(p);
204
205         data = app_malloc(len + 20, "BN space");
206
207         BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
208         print_bignum_var(bio_out, p, "dsap", bits_p, data);
209         print_bignum_var(bio_out, q, "dsaq", bits_p, data);
210         print_bignum_var(bio_out, g, "dsag", bits_p, data);
211         BIO_printf(bio_out, "    DSA *dsa = DSA_new();\n"
212                             "    BIGNUM *p, *q, *g;\n"
213                             "\n");
214         BIO_printf(bio_out, "    if (dsa == NULL)\n"
215                             "        return NULL;\n");
216         BIO_printf(bio_out, "    if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
217                    bits_p, bits_p);
218         BIO_printf(bio_out, "                           q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
219                    bits_p, bits_p);
220         BIO_printf(bio_out, "                           g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
221                    bits_p, bits_p);
222         BIO_printf(bio_out, "        DSA_free(dsa);\n"
223                             "        BN_free(p);\n"
224                             "        BN_free(q);\n"
225                             "        BN_free(g);\n"
226                             "        return NULL;\n"
227                             "    }\n"
228                             "    return dsa;\n}\n");
229         OPENSSL_free(data);
230     }
231
232     if (outformat == FORMAT_ASN1 && genkey)
233         noout = 1;
234
235     if (!noout) {
236         if (outformat == FORMAT_ASN1)
237             i = i2d_DSAparams_bio(out, dsa);
238         else
239             i = PEM_write_bio_DSAparams(out, dsa);
240         if (!i) {
241             BIO_printf(bio_err, "unable to write DSA parameters\n");
242             ERR_print_errors(bio_err);
243             goto end;
244         }
245     }
246     if (genkey) {
247         DSA *dsakey;
248
249         if ((dsakey = DSAparams_dup(dsa)) == NULL)
250             goto end;
251         if (!DSA_generate_key(dsakey)) {
252             ERR_print_errors(bio_err);
253             DSA_free(dsakey);
254             goto end;
255         }
256         assert(private);
257         if (outformat == FORMAT_ASN1)
258             i = i2d_DSAPrivateKey_bio(out, dsakey);
259         else
260             i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
261                                             NULL);
262         DSA_free(dsakey);
263     }
264     ret = 0;
265  end:
266     BN_GENCB_free(cb);
267     BIO_free(in);
268     BIO_free_all(out);
269     DSA_free(dsa);
270     release_engine(e);
271     return ret;
272 }
273
274 static int dsa_cb(int p, int n, BN_GENCB *cb)
275 {
276     static const char symbols[] = ".+*\n";
277     char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
278
279     if (!verbose)
280         return 1;
281
282     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
283     (void)BIO_flush(BN_GENCB_get_arg(cb));
284     return 1;
285 }