708cb9a6488ded6b1eaf656729cc0a6d0ba658aa
[openssl.git] / apps / dsaparam.c
1 /*
2  * Copyright 1995-2021 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 typedef enum OPTION_choice {
28     OPT_COMMON,
29     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
30     OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
31     OPT_R_ENUM, OPT_PROV_ENUM
32 } OPTION_CHOICE;
33
34 const OPTIONS dsaparam_options[] = {
35     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
36
37     OPT_SECTION("General"),
38     {"help", OPT_HELP, '-', "Display this summary"},
39 #ifndef OPENSSL_NO_ENGINE
40     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
41 #endif
42
43     OPT_SECTION("Input"),
44     {"in", OPT_IN, '<', "Input file"},
45     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
46
47     OPT_SECTION("Output"),
48     {"out", OPT_OUT, '>', "Output file"},
49     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
50     {"text", OPT_TEXT, '-', "Print as text"},
51     {"noout", OPT_NOOUT, '-', "No output"},
52     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
53     {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
54
55     OPT_R_OPTIONS,
56     OPT_PROV_OPTIONS,
57
58     OPT_PARAMETERS(),
59     {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
60     {NULL}
61 };
62
63 int dsaparam_main(int argc, char **argv)
64 {
65     ENGINE *e = NULL;
66     BIO *out = NULL;
67     EVP_PKEY *params = NULL, *pkey = NULL;
68     EVP_PKEY_CTX *ctx = NULL;
69     int numbits = -1, num = 0, genkey = 0;
70     int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 0;
71     int ret = 1, i, text = 0, private = 0;
72     char *infile = NULL, *outfile = NULL, *prog;
73     OPTION_CHOICE o;
74
75     prog = opt_init(argc, argv, dsaparam_options);
76     while ((o = opt_next()) != OPT_EOF) {
77         switch (o) {
78         case OPT_EOF:
79         case OPT_ERR:
80  opthelp:
81             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
82             goto end;
83         case OPT_HELP:
84             opt_help(dsaparam_options);
85             ret = 0;
86             goto end;
87         case OPT_INFORM:
88             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
89                 goto opthelp;
90             break;
91         case OPT_IN:
92             infile = opt_arg();
93             break;
94         case OPT_OUTFORM:
95             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
96                 goto opthelp;
97             break;
98         case OPT_OUT:
99             outfile = opt_arg();
100             break;
101         case OPT_ENGINE:
102             e = setup_engine(opt_arg(), 0);
103             break;
104         case OPT_TEXT:
105             text = 1;
106             break;
107         case OPT_GENKEY:
108             genkey = 1;
109             break;
110         case OPT_R_CASES:
111             if (!opt_rand(o))
112                 goto end;
113             break;
114         case OPT_PROV_CASES:
115             if (!opt_provider(o))
116                 goto end;
117             break;
118         case OPT_NOOUT:
119             noout = 1;
120             break;
121         case OPT_VERBOSE:
122             verbose = 1;
123             break;
124         }
125     }
126
127     /* Optional arg is bitsize. */
128     argc = opt_num_rest();
129     argv = opt_rest();
130     if (argc == 1) {
131         if (!opt_int(argv[0], &num) || num < 0)
132             goto opthelp;
133     } else if (!opt_check_rest_arg(NULL)) {
134         goto opthelp;
135     }
136     if (!app_RAND_load())
137         goto end;
138
139     /* generate a key */
140     numbits = num;
141     private = genkey ? 1 : 0;
142
143     out = bio_open_owner(outfile, outformat, private);
144     if (out == NULL)
145         goto end;
146
147     ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
148     if (ctx == NULL) {
149         BIO_printf(bio_err,
150                    "Error, DSA parameter generation context allocation failed\n");
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         EVP_PKEY_CTX_set_app_data(ctx, bio_err);
161         if (verbose) {
162             EVP_PKEY_CTX_set_cb(ctx, progress_cb);
163             BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
164                        num);
165             BIO_printf(bio_err, "This could take some time\n");
166         }
167         if (EVP_PKEY_paramgen_init(ctx) <= 0) {
168             BIO_printf(bio_err,
169                        "Error, DSA key generation paramgen init failed\n");
170             goto end;
171         }
172         if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
173             BIO_printf(bio_err,
174                        "Error, DSA key generation setting bit length failed\n");
175             goto end;
176         }
177         params = app_paramgen(ctx, "DSA");
178     } else {
179         params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
180     }
181     if (params == NULL) {
182         /* Error message should already have been displayed */
183         goto end;
184     }
185
186     if (text) {
187         EVP_PKEY_print_params(out, params, 0, NULL);
188     }
189
190     if (outformat == FORMAT_ASN1 && genkey)
191         noout = 1;
192
193     if (!noout) {
194         if (outformat == FORMAT_ASN1)
195             i = i2d_KeyParams_bio(out, params);
196         else
197             i = PEM_write_bio_Parameters(out, params);
198         if (!i) {
199             BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
200             goto end;
201         }
202     }
203     if (genkey) {
204         EVP_PKEY_CTX_free(ctx);
205         ctx = EVP_PKEY_CTX_new(params, NULL);
206         if (ctx == NULL) {
207             BIO_printf(bio_err,
208                        "Error, DSA key generation context allocation failed\n");
209             goto end;
210         }
211         if (EVP_PKEY_keygen_init(ctx) <= 0) {
212             BIO_printf(bio_err,
213                        "Error, unable to initialise for key generation\n");
214             goto end;
215         }
216         pkey = app_keygen(ctx, "DSA", numbits, verbose);
217         assert(private);
218         if (outformat == FORMAT_ASN1)
219             i = i2d_PrivateKey_bio(out, pkey);
220         else
221             i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
222     }
223     ret = 0;
224  end:
225     if (ret != 0)
226         ERR_print_errors(bio_err);
227     BIO_free_all(out);
228     EVP_PKEY_CTX_free(ctx);
229     EVP_PKEY_free(pkey);
230     EVP_PKEY_free(params);
231     release_engine(e);
232     return ret;
233 }
234