2 * Copyright 1995-2018 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>
12 NON_EMPTY_TRANSLATION_UNIT
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>
28 static int verbose = 0;
30 static int dsa_cb(int p, int n, BN_GENCB *cb);
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,
39 const OPTIONS dsaparam_options[] = {
40 {"help", OPT_HELP, '-', "Display this summary"},
41 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
42 {"in", OPT_IN, '<', "Input file"},
43 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
44 {"out", OPT_OUT, '>', "Output file"},
45 {"text", OPT_TEXT, '-', "Print as text"},
46 {"C", OPT_C, '-', "Output C code"},
47 {"noout", OPT_NOOUT, '-', "No output"},
48 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
50 # ifndef OPENSSL_NO_ENGINE
51 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
53 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
57 int dsaparam_main(int argc, char **argv)
61 BIO *in = NULL, *out = NULL;
63 int numbits = -1, num = 0, genkey = 0;
64 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
65 int ret = 1, i, text = 0, private = 0;
66 char *infile = NULL, *outfile = NULL, *prog;
69 prog = opt_init(argc, argv, dsaparam_options);
70 while ((o = opt_next()) != OPT_EOF) {
75 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
78 opt_help(dsaparam_options);
82 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
89 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
96 e = setup_engine(opt_arg(), 0);
119 argc = opt_num_rest();
123 if (!opt_int(argv[0], &num) || num < 0)
128 private = genkey ? 1 : 0;
130 in = bio_open_default(infile, 'r', informat);
133 out = bio_open_owner(outfile, outformat, private);
138 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
140 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
141 " Your key size is %d! Larger key size may behave not as expected.\n",
142 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
146 BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
149 BN_GENCB_set(cb, dsa_cb, bio_err);
152 BIO_printf(bio_err, "Error allocating DSA object\n");
156 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
158 BIO_printf(bio_err, "This could take some time\n");
160 if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
161 ERR_print_errors(bio_err);
162 BIO_printf(bio_err, "Error, DSA key generation failed\n");
165 } else if (informat == FORMAT_ASN1) {
166 dsa = d2i_DSAparams_bio(in, NULL);
168 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
171 BIO_printf(bio_err, "unable to load DSA parameters\n");
172 ERR_print_errors(bio_err);
177 DSAparams_print(out, dsa);
181 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
185 DSA_get0_pqg(dsa, &p, &q, &g);
186 len = BN_num_bytes(p);
187 bits_p = BN_num_bits(p);
189 data = app_malloc(len + 20, "BN space");
191 BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
192 print_bignum_var(bio_out, p, "dsap", bits_p, data);
193 print_bignum_var(bio_out, q, "dsaq", bits_p, data);
194 print_bignum_var(bio_out, g, "dsag", bits_p, data);
195 BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
196 " BIGNUM *p, *q, *g;\n"
198 BIO_printf(bio_out, " if (dsa == NULL)\n"
200 BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
202 BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
204 BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
206 BIO_printf(bio_out, " DSA_free(dsa);\n"
212 " return dsa;\n}\n");
216 if (outformat == FORMAT_ASN1 && genkey)
220 if (outformat == FORMAT_ASN1)
221 i = i2d_DSAparams_bio(out, dsa);
223 i = PEM_write_bio_DSAparams(out, dsa);
225 BIO_printf(bio_err, "unable to write DSA parameters\n");
226 ERR_print_errors(bio_err);
233 if ((dsakey = DSAparams_dup(dsa)) == NULL)
235 if (!DSA_generate_key(dsakey)) {
236 ERR_print_errors(bio_err);
241 if (outformat == FORMAT_ASN1)
242 i = i2d_DSAPrivateKey_bio(out, dsakey);
244 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
258 static int dsa_cb(int p, int n, BN_GENCB *cb)
260 static const char symbols[] = ".+*\n";
261 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
266 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
267 (void)BIO_flush(BN_GENCB_get_arg(cb));