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