70c698dbec79aa804daa16be0635c07c9ecf1ce7
[openssl.git] / apps / dsaparam.c
1 /*
2  * Copyright 1995-2020 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 #include <openssl/opensslconf.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <time.h>
15 #include <string.h>
16 #include "apps.h"
17 #include "progs.h"
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/bn.h>
21 #include <openssl/dsa.h>
22 #include <openssl/x509.h>
23 #include <openssl/pem.h>
24
25 static int verbose = 0;
26
27 static int gendsa_cb(EVP_PKEY_CTX *ctx);
28
29 typedef enum OPTION_choice {
30     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
32     OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
33     OPT_R_ENUM, OPT_PROV_ENUM
34 } OPTION_CHOICE;
35
36 const OPTIONS dsaparam_options[] = {
37     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
38
39     OPT_SECTION("General"),
40     {"help", OPT_HELP, '-', "Display this summary"},
41 #ifndef OPENSSL_NO_ENGINE
42     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
43 #endif
44
45     OPT_SECTION("Input"),
46     {"in", OPT_IN, '<', "Input file"},
47     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
48
49     OPT_SECTION("Output"),
50     {"out", OPT_OUT, '>', "Output file"},
51     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
52     {"text", OPT_TEXT, '-', "Print as text"},
53     {"noout", OPT_NOOUT, '-', "No output"},
54     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
55     {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
56
57     OPT_R_OPTIONS,
58     OPT_PROV_OPTIONS,
59
60     OPT_PARAMETERS(),
61     {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
62     {NULL}
63 };
64
65 int dsaparam_main(int argc, char **argv)
66 {
67     ENGINE *e = NULL;
68     BIO *out = NULL;
69     EVP_PKEY *params = NULL, *pkey = NULL;
70     EVP_PKEY_CTX *ctx = NULL;
71     int numbits = -1, num = 0, genkey = 0;
72     int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0;
73     int ret = 1, i, text = 0, private = 0;
74     char *infile = NULL, *outfile = NULL, *prog;
75     OPTION_CHOICE o;
76
77     prog = opt_init(argc, argv, dsaparam_options);
78     while ((o = opt_next()) != OPT_EOF) {
79         switch (o) {
80         case OPT_EOF:
81         case OPT_ERR:
82  opthelp:
83             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
84             goto end;
85         case OPT_HELP:
86             opt_help(dsaparam_options);
87             ret = 0;
88             goto end;
89         case OPT_INFORM:
90             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
91                 goto opthelp;
92             break;
93         case OPT_IN:
94             infile = opt_arg();
95             break;
96         case OPT_OUTFORM:
97             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
98                 goto opthelp;
99             break;
100         case OPT_OUT:
101             outfile = opt_arg();
102             break;
103         case OPT_ENGINE:
104             e = setup_engine(opt_arg(), 0);
105             break;
106         case OPT_TEXT:
107             text = 1;
108             break;
109         case OPT_GENKEY:
110             genkey = 1;
111             break;
112         case OPT_R_CASES:
113             if (!opt_rand(o))
114                 goto end;
115             break;
116         case OPT_PROV_CASES:
117             if (!opt_provider(o))
118                 goto end;
119             break;
120         case OPT_NOOUT:
121             noout = 1;
122             break;
123         case OPT_VERBOSE:
124             verbose = 1;
125             break;
126         }
127     }
128
129     /* Optional arg is bitsize. */
130     argc = opt_num_rest();
131     argv = opt_rest();
132     if (argc == 1) {
133         if (!opt_int(argv[0], &num) || num < 0)
134             goto opthelp;
135     } else if (argc != 0) {
136         goto opthelp;
137     }
138     app_RAND_load();
139
140     /* generate a key */
141     numbits = num;
142     private = genkey ? 1 : 0;
143
144     out = bio_open_owner(outfile, outformat, private);
145     if (out == NULL)
146         goto end;
147
148     ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
149     if (ctx == NULL) {
150         BIO_printf(bio_err,
151                    "Error, DSA parameter generation context allocation failed\n");
152         goto end;
153     }
154     if (numbits > 0) {
155         if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
156             BIO_printf(bio_err,
157                        "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
158                        "         Your key size is %d! Larger key size may behave not as expected.\n",
159                        OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
160
161         EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
162         EVP_PKEY_CTX_set_app_data(ctx, bio_err);
163         if (verbose) {
164             BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
165                        num);
166             BIO_printf(bio_err, "This could take some time\n");
167         }
168         if (EVP_PKEY_paramgen_init(ctx) <= 0) {
169             BIO_printf(bio_err,
170                        "Error, DSA key generation paramgen init failed\n");
171             goto end;
172         }
173         if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
174             BIO_printf(bio_err,
175                        "Error, DSA key generation setting bit length failed\n");
176             goto end;
177         }
178         if (EVP_PKEY_paramgen(ctx, &params) <= 0) {
179             BIO_printf(bio_err, "Error, DSA key generation failed\n");
180             goto end;
181         }
182     } else {
183         params = load_keyparams(infile, 1, "DSA", "DSA parameters");
184     }
185     if (params == NULL) {
186         /* Error message should already have been displayed */
187         goto end;
188     }
189
190     if (text) {
191         EVP_PKEY_print_params(out, params, 0, NULL);
192     }
193
194     if (outformat == FORMAT_ASN1 && genkey)
195         noout = 1;
196
197     if (!noout) {
198         if (outformat == FORMAT_ASN1)
199             i = i2d_KeyParams_bio(out, params);
200         else
201             i = PEM_write_bio_Parameters(out, params);
202         if (!i) {
203             BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
204             goto end;
205         }
206     }
207     if (genkey) {
208         EVP_PKEY_CTX_free(ctx);
209         ctx = EVP_PKEY_CTX_new(params, NULL);
210         if (ctx == NULL) {
211             BIO_printf(bio_err,
212                        "Error, DSA key generation context allocation failed\n");
213             goto end;
214         }
215         if (!EVP_PKEY_keygen_init(ctx)) {
216             BIO_printf(bio_err,
217                        "Error, unable to initialise for key generation\n");
218             goto end;
219         }
220         if (!EVP_PKEY_keygen(ctx, &pkey)) {
221             BIO_printf(bio_err, "Error, unable to generate key\n");
222             goto end;
223         }
224         assert(private);
225         if (outformat == FORMAT_ASN1)
226             i = i2d_PrivateKey_bio(out, pkey);
227         else
228             i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
229     }
230     ret = 0;
231  end:
232     if (ret != 0)
233         ERR_print_errors(bio_err);
234     BIO_free_all(out);
235     EVP_PKEY_CTX_free(ctx);
236     EVP_PKEY_free(pkey);
237     EVP_PKEY_free(params);
238     release_engine(e);
239     return ret;
240 }
241
242 static int gendsa_cb(EVP_PKEY_CTX *ctx)
243 {
244     static const char symbols[] = ".+*\n";
245     int p;
246     char c;
247     BIO *b;
248
249     if (!verbose)
250         return 1;
251
252     b = EVP_PKEY_CTX_get_app_data(ctx);
253     p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
254     c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
255
256     BIO_write(b, &c, 1);
257     (void)BIO_flush(b);
258     return 1;
259 }