2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/bn.h>
20 #include <openssl/dsa.h>
21 #include <openssl/x509.h>
22 #include <openssl/pem.h>
24 static int dsa_cb(int p, int n, BN_GENCB *cb);
26 typedef enum OPTION_choice {
27 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
28 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
29 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_R_ENUM
32 const OPTIONS dsaparam_options[] = {
33 {"help", OPT_HELP, '-', "Display this summary"},
34 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
35 {"in", OPT_IN, '<', "Input file"},
36 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
37 {"out", OPT_OUT, '>', "Output file"},
38 {"text", OPT_TEXT, '-', "Print as text"},
39 {"C", OPT_C, '-', "Output C code"},
40 {"noout", OPT_NOOUT, '-', "No output"},
41 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
43 #ifndef OPENSSL_NO_ENGINE
44 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
49 int dsaparam_main(int argc, char **argv)
53 BIO *in = NULL, *out = NULL;
55 int numbits = -1, num = 0, genkey = 0;
56 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
57 int ret = 1, i, text = 0, private = 0;
58 char *infile = NULL, *outfile = NULL, *prog;
61 prog = opt_init(argc, argv, dsaparam_options);
62 while ((o = opt_next()) != OPT_EOF) {
67 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
70 opt_help(dsaparam_options);
74 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
81 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
88 e = setup_engine(opt_arg(), 0);
108 argc = opt_num_rest();
112 if (!opt_int(argv[0], &num) || num < 0)
117 private = genkey ? 1 : 0;
119 in = bio_open_default(infile, 'r', informat);
122 out = bio_open_owner(outfile, outformat, private);
127 if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
129 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
130 " Your key size is %d! Larger key size may behave not as expected.\n",
131 OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
135 BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
138 BN_GENCB_set(cb, dsa_cb, bio_err);
141 BIO_printf(bio_err, "Error allocating DSA object\n");
144 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
146 BIO_printf(bio_err, "This could take some time\n");
147 if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
148 ERR_print_errors(bio_err);
149 BIO_printf(bio_err, "Error, DSA key generation failed\n");
152 } else if (informat == FORMAT_ASN1) {
153 dsa = d2i_DSAparams_bio(in, NULL);
155 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
158 BIO_printf(bio_err, "unable to load DSA parameters\n");
159 ERR_print_errors(bio_err);
164 DSAparams_print(out, dsa);
168 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
172 DSA_get0_pqg(dsa, &p, &q, &g);
173 len = BN_num_bytes(p);
174 bits_p = BN_num_bits(p);
176 data = app_malloc(len + 20, "BN space");
178 BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
179 print_bignum_var(bio_out, p, "dsap", bits_p, data);
180 print_bignum_var(bio_out, q, "dsaq", bits_p, data);
181 print_bignum_var(bio_out, g, "dsag", bits_p, data);
182 BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
183 " BIGNUM *p, *q, *g;\n"
185 BIO_printf(bio_out, " if (dsa == NULL)\n"
187 BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
189 BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
191 BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
193 BIO_printf(bio_out, " DSA_free(dsa);\n"
199 " return dsa;\n}\n");
203 if (outformat == FORMAT_ASN1 && genkey)
207 if (outformat == FORMAT_ASN1)
208 i = i2d_DSAparams_bio(out, dsa);
210 i = PEM_write_bio_DSAparams(out, dsa);
212 BIO_printf(bio_err, "unable to write DSA parameters\n");
213 ERR_print_errors(bio_err);
220 if ((dsakey = DSAparams_dup(dsa)) == NULL)
222 if (!DSA_generate_key(dsakey)) {
223 ERR_print_errors(bio_err);
228 if (outformat == FORMAT_ASN1)
229 i = i2d_DSAPrivateKey_bio(out, dsakey);
231 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
245 static int dsa_cb(int p, int n, BN_GENCB *cb)
247 static const char symbols[] = ".+*\n";
248 char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
250 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
251 (void)BIO_flush(BN_GENCB_get_arg(cb));