2 * Copyright 1995-2017 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>
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 dsa_cb(int p, int n, BN_GENCB *cb);
30 typedef enum OPTION_choice {
31 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
32 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
33 OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_R_ENUM
36 const OPTIONS dsaparam_options[] = {
37 {"help", OPT_HELP, '-', "Display this summary"},
38 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
39 {"in", OPT_IN, '<', "Input file"},
40 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
41 {"out", OPT_OUT, '>', "Output file"},
42 {"text", OPT_TEXT, '-', "Print as text"},
43 {"C", OPT_C, '-', "Output C code"},
44 {"noout", OPT_NOOUT, '-', "No output"},
45 {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
47 # ifndef OPENSSL_NO_ENGINE
48 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
53 int dsaparam_main(int argc, char **argv)
57 BIO *in = NULL, *out = NULL;
59 int numbits = -1, num = 0, genkey = 0;
60 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
61 int ret = 1, i, text = 0, private = 0;
62 char *infile = NULL, *outfile = NULL, *prog;
65 prog = opt_init(argc, argv, dsaparam_options);
66 while ((o = opt_next()) != OPT_EOF) {
71 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74 opt_help(dsaparam_options);
78 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
85 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
92 e = setup_engine(opt_arg(), 0);
112 argc = opt_num_rest();
116 if (!opt_int(argv[0], &num) || num < 0)
121 private = genkey ? 1 : 0;
123 in = bio_open_default(infile, 'r', informat);
126 out = bio_open_owner(outfile, outformat, private);
133 BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
136 BN_GENCB_set(cb, dsa_cb, bio_err);
139 BIO_printf(bio_err, "Error allocating DSA object\n");
142 BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
144 BIO_printf(bio_err, "This could take some time\n");
145 if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
146 ERR_print_errors(bio_err);
147 BIO_printf(bio_err, "Error, DSA key generation failed\n");
150 } else if (informat == FORMAT_ASN1) {
151 dsa = d2i_DSAparams_bio(in, NULL);
153 dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
156 BIO_printf(bio_err, "unable to load DSA parameters\n");
157 ERR_print_errors(bio_err);
162 DSAparams_print(out, dsa);
166 const BIGNUM *p = NULL, *q = NULL, *g = NULL;
170 DSA_get0_pqg(dsa, &p, &q, &g);
171 len = BN_num_bytes(p);
172 bits_p = BN_num_bits(p);
174 data = app_malloc(len + 20, "BN space");
176 BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
177 print_bignum_var(bio_out, p, "dsap", len, data);
178 print_bignum_var(bio_out, q, "dsaq", len, data);
179 print_bignum_var(bio_out, g, "dsag", len, data);
180 BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
182 BIO_printf(bio_out, " if (dsa == NULL)\n"
184 BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL);\n",
186 BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL);\n",
188 BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL);\n",
190 BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n"
194 " return(dsa);\n}\n");
199 if (outformat == FORMAT_ASN1)
200 i = i2d_DSAparams_bio(out, dsa);
202 i = PEM_write_bio_DSAparams(out, dsa);
204 BIO_printf(bio_err, "unable to write DSA parameters\n");
205 ERR_print_errors(bio_err);
212 if ((dsakey = DSAparams_dup(dsa)) == NULL)
214 if (!DSA_generate_key(dsakey)) {
215 ERR_print_errors(bio_err);
220 if (outformat == FORMAT_ASN1)
221 i = i2d_DSAPrivateKey_bio(out, dsakey);
223 i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
237 static int dsa_cb(int p, int n, BN_GENCB *cb)
249 BIO_write(BN_GENCB_get_arg(cb), &c, 1);
250 (void)BIO_flush(BN_GENCB_get_arg(cb));