2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.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>
25 static int verbose = 0;
27 static int gendsa_cb(EVP_PKEY_CTX *ctx);
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, OPT_C,
32 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
33 OPT_R_ENUM, OPT_PROV_ENUM
36 const OPTIONS dsaparam_options[] = {
37 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
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"},
46 {"in", OPT_IN, '<', "Input file"},
47 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
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 {"C", OPT_C, '-', "Output C code"},
54 {"noout", OPT_NOOUT, '-', "No output"},
55 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
56 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
62 {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
66 int dsaparam_main(int argc, char **argv)
70 BIO *in = NULL, *out = NULL;
71 EVP_PKEY *pkey = NULL;
72 EVP_PKEY_CTX *ctx = NULL;
73 int numbits = -1, num = 0, genkey = 0;
74 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
75 int ret = 1, i, text = 0, private = 0;
76 char *infile = NULL, *outfile = NULL, *prog;
79 prog = opt_init(argc, argv, dsaparam_options);
80 while ((o = opt_next()) != OPT_EOF) {
85 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
88 opt_help(dsaparam_options);
92 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
99 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
106 e = setup_engine(opt_arg(), 0);
122 if (!opt_provider(o))
133 argc = opt_num_rest();
137 if (!opt_int(argv[0], &num) || num < 0)
142 private = genkey ? 1 : 0;
144 in = bio_open_default(infile, 'r', informat);
147 out = bio_open_owner(outfile, outformat, private);
151 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
153 ERR_print_errors(bio_err);
155 "Error, DSA parameter generation context allocation failed\n");
159 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
161 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
162 " Your key size is %d! Larger key size may behave not as expected.\n",
163 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
165 EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
166 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
168 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
170 BIO_printf(bio_err, "This could take some time\n");
172 if (EVP_PKEY_paramgen_init(ctx) <= 0) {
173 ERR_print_errors(bio_err);
175 "Error, DSA key generation paramgen init failed\n");
178 if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
179 ERR_print_errors(bio_err);
181 "Error, DSA key generation setting bit length failed\n");
184 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
185 ERR_print_errors(bio_err);
186 BIO_printf(bio_err, "Error, DSA key generation failed\n");
189 dsa = EVP_PKEY_get1_DSA(pkey);
191 ERR_print_errors(bio_err);
192 BIO_printf(bio_err, "Error, DSA key extraction failed\n");
195 } else if (informat == FORMAT_ASN1) {
196 dsa = d2i_DSAparams_bio(in, NULL);
198 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
201 BIO_printf(bio_err, "unable to load DSA parameters\n");
202 ERR_print_errors(bio_err);
207 pkey = EVP_PKEY_new();
209 BIO_printf(bio_err, "Error, unable to allocate PKEY object\n");
210 ERR_print_errors(bio_err);
213 if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
214 BIO_printf(bio_err, "Error, unable to set DSA parameters\n");
215 ERR_print_errors(bio_err);
220 EVP_PKEY_print_params(out, pkey, 0, NULL);
224 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
228 DSA_get0_pqg(dsa, &p, &q, &g);
229 len = BN_num_bytes(p);
230 bits_p = BN_num_bits(p);
232 data = app_malloc(len + 20, "BN space");
234 BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
235 print_bignum_var(bio_out, p, "dsap", bits_p, data);
236 print_bignum_var(bio_out, q, "dsaq", bits_p, data);
237 print_bignum_var(bio_out, g, "dsag", bits_p, data);
238 BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
239 " BIGNUM *p, *q, *g;\n"
241 BIO_printf(bio_out, " if (dsa == NULL)\n"
243 BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
245 BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
247 BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
249 BIO_printf(bio_out, " DSA_free(dsa);\n"
255 " return dsa;\n}\n");
259 if (outformat == FORMAT_ASN1 && genkey)
263 if (outformat == FORMAT_ASN1)
264 i = i2d_DSAparams_bio(out, dsa);
266 i = PEM_write_bio_DSAparams(out, dsa);
268 BIO_printf(bio_err, "unable to write DSA parameters\n");
269 ERR_print_errors(bio_err);
276 EVP_PKEY_CTX_free(ctx);
277 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
279 ERR_print_errors(bio_err);
281 "Error, DSA key generation context allocation failed\n");
284 if (!EVP_PKEY_keygen_init(ctx)) {
285 BIO_printf(bio_err, "unable to initialise for key generation\n");
286 ERR_print_errors(bio_err);
289 if (!EVP_PKEY_keygen(ctx, &pkey)) {
290 BIO_printf(bio_err, "unable to generate key\n");
291 ERR_print_errors(bio_err);
294 dsakey = EVP_PKEY_get0_DSA(pkey);
295 if (dsakey == NULL) {
296 BIO_printf(bio_err, "unable to extract generated key\n");
297 ERR_print_errors(bio_err);
301 if (outformat == FORMAT_ASN1)
302 i = i2d_DSAPrivateKey_bio(out, dsakey);
304 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
311 EVP_PKEY_CTX_free(ctx);
318 static int gendsa_cb(EVP_PKEY_CTX *ctx)
320 static const char symbols[] = ".+*\n";
328 b = EVP_PKEY_CTX_get_app_data(ctx);
329 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
330 c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';