2 * Copyright 1995-2016 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
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/dsa.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
29 static int stop_keygen_flag
= 0;
31 static void timebomb_sigalarm(int foo
)
38 static int dsa_cb(int p
, int n
, BN_GENCB
*cb
);
40 typedef enum OPTION_choice
{
41 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
42 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_TEXT
, OPT_C
,
43 OPT_NOOUT
, OPT_GENKEY
, OPT_RAND
, OPT_ENGINE
,
47 OPTIONS dsaparam_options
[] = {
48 {"help", OPT_HELP
, '-', "Display this summary"},
49 {"inform", OPT_INFORM
, 'F', "Input format - DER or PEM"},
50 {"in", OPT_IN
, '<', "Input file"},
51 {"outform", OPT_OUTFORM
, 'F', "Output format - DER or PEM"},
52 {"out", OPT_OUT
, '>', "Output file"},
53 {"text", OPT_TEXT
, '-', "Print as text"},
54 {"C", OPT_C
, '-', "Output C code"},
55 {"noout", OPT_NOOUT
, '-', "No output"},
56 {"genkey", OPT_GENKEY
, '-', "Generate a DSA key"},
57 {"rand", OPT_RAND
, 's', "Files to use for random number input"},
59 {"timebomb", OPT_TIMEBOMB
, 'p', "Interrupt keygen after 'pnum' seconds"},
61 # ifndef OPENSSL_NO_ENGINE
62 {"engine", OPT_ENGINE
, 's', "Use engine e, possibly a hardware device"},
67 int dsaparam_main(int argc
, char **argv
)
70 BIO
*in
= NULL
, *out
= NULL
;
72 int numbits
= -1, num
= 0, genkey
= 0, need_rand
= 0;
73 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, noout
= 0, C
= 0;
74 int ret
= 1, i
, text
= 0, private = 0;
78 char *infile
= NULL
, *outfile
= NULL
, *prog
, *inrand
= NULL
;
81 prog
= opt_init(argc
, argv
, dsaparam_options
);
82 while ((o
= opt_next()) != OPT_EOF
) {
87 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
90 opt_help(dsaparam_options
);
94 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
101 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
108 (void)setup_engine(opt_arg(), 0);
112 timebomb
= atoi(opt_arg());
122 genkey
= need_rand
= 1;
133 argc
= opt_num_rest();
137 if (!opt_int(argv
[0], &num
) || num
< 0)
143 private = genkey
? 1 : 0;
145 in
= bio_open_default(infile
, 'r', informat
);
148 out
= bio_open_owner(outfile
, outformat
, private);
153 app_RAND_load_file(NULL
, (inrand
!= NULL
));
155 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
156 app_RAND_load_files(inrand
));
162 BIO_printf(bio_err
, "Error allocating BN_GENCB object\n");
165 BN_GENCB_set(cb
, dsa_cb
, bio_err
);
169 BIO_printf(bio_err
, "Error allocating DSA object\n");
172 BIO_printf(bio_err
, "Generating DSA parameters, %d bit long prime\n",
174 BIO_printf(bio_err
, "This could take some time\n");
177 struct sigaction act
;
178 act
.sa_handler
= timebomb_sigalarm
;
181 "(though I'll stop it if not done within %d secs)\n",
183 if (sigaction(SIGALRM
, &act
, NULL
) != 0) {
184 BIO_printf(bio_err
, "Error, couldn't set SIGALRM handler\n");
190 if (!DSA_generate_parameters_ex(dsa
, num
, NULL
, 0, NULL
, NULL
, cb
)) {
192 if (stop_keygen_flag
) {
193 BIO_printf(bio_err
, "DSA key generation time-stopped\n");
194 /* This is an asked-for behaviour! */
199 ERR_print_errors(bio_err
);
200 BIO_printf(bio_err
, "Error, DSA key generation failed\n");
203 } else if (informat
== FORMAT_ASN1
)
204 dsa
= d2i_DSAparams_bio(in
, NULL
);
206 dsa
= PEM_read_bio_DSAparams(in
, NULL
, NULL
, NULL
);
208 BIO_printf(bio_err
, "unable to load DSA parameters\n");
209 ERR_print_errors(bio_err
);
214 DSAparams_print(out
, dsa
);
218 const BIGNUM
*p
= NULL
, *q
= NULL
, *g
= NULL
;
222 DSA_get0_pqg(dsa
, &p
, &q
, &g
);
223 len
= BN_num_bytes(p
);
224 bits_p
= BN_num_bits(p
);
226 data
= app_malloc(len
+ 20, "BN space");
228 BIO_printf(bio_out
, "DSA *get_dsa%d()\n{\n", bits_p
);
229 print_bignum_var(bio_out
, p
, "dsap", len
, data
);
230 print_bignum_var(bio_out
, q
, "dsaq", len
, data
);
231 print_bignum_var(bio_out
, g
, "dsag", len
, data
);
232 BIO_printf(bio_out
, " DSA *dsa = DSA_new();\n"
234 BIO_printf(bio_out
, " if (dsa == NULL)\n"
236 BIO_printf(bio_out
, " dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n",
238 BIO_printf(bio_out
, " dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n",
240 BIO_printf(bio_out
, " dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n",
242 BIO_printf(bio_out
, " if (!dsa->p || !dsa->q || !dsa->g) {\n"
246 " return(dsa);\n}\n");
251 if (outformat
== FORMAT_ASN1
)
252 i
= i2d_DSAparams_bio(out
, dsa
);
254 i
= PEM_write_bio_DSAparams(out
, dsa
);
256 BIO_printf(bio_err
, "unable to write DSA parameters\n");
257 ERR_print_errors(bio_err
);
265 if ((dsakey
= DSAparams_dup(dsa
)) == NULL
)
267 if (!DSA_generate_key(dsakey
)) {
268 ERR_print_errors(bio_err
);
273 if (outformat
== FORMAT_ASN1
)
274 i
= i2d_DSAPrivateKey_bio(out
, dsakey
);
276 i
= PEM_write_bio_DSAPrivateKey(out
, dsakey
, NULL
, NULL
, 0, NULL
,
281 app_RAND_write_file(NULL
);
291 static int dsa_cb(int p
, int n
, BN_GENCB
*cb
)
303 BIO_write(BN_GENCB_get_arg(cb
), &c
, 1);
304 (void)BIO_flush(BN_GENCB_get_arg(cb
));
306 if (stop_keygen_flag
)