Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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 |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Rich Salz | effaf4d | 2016-01-31 13:08:23 -0500 | [diff] [blame] | 10 | #include <openssl/opensslconf.h> |
| 11 | #ifdef OPENSSL_NO_DSA |
| 12 | NON_EMPTY_TRANSLATION_UNIT |
| 13 | #else |
Geoff Thorpe | 5daec7e | 2002-12-08 05:38:44 +0000 | [diff] [blame] | 14 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 15 | # include <stdio.h> |
| 16 | # include <stdlib.h> |
| 17 | # include <time.h> |
| 18 | # include <string.h> |
| 19 | # include "apps.h" |
| 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> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 26 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 27 | # ifdef GENCB_TEST |
Geoff Thorpe | 5daec7e | 2002-12-08 05:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | static int stop_keygen_flag = 0; |
| 30 | |
Geoff Thorpe | f92570f | 2002-12-11 03:34:26 +0000 | [diff] [blame] | 31 | static void timebomb_sigalarm(int foo) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 32 | { |
| 33 | stop_keygen_flag = 1; |
| 34 | } |
Geoff Thorpe | 5daec7e | 2002-12-08 05:38:44 +0000 | [diff] [blame] | 35 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 36 | # endif |
Geoff Thorpe | 5daec7e | 2002-12-08 05:38:44 +0000 | [diff] [blame] | 37 | |
Rich Salz | 6d23cf9 | 2015-01-12 17:29:26 -0500 | [diff] [blame] | 38 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 39 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 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, |
Rich Salz | 700b4a4 | 2015-12-14 15:24:27 -0500 | [diff] [blame] | 43 | OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 44 | OPT_TIMEBOMB |
| 45 | } OPTION_CHOICE; |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 46 | |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 47 | const OPTIONS dsaparam_options[] = { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 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"}, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 58 | # ifdef GENCB_TEST |
| 59 | {"timebomb", OPT_TIMEBOMB, 'p', "Interrupt keygen after 'pnum' seconds"}, |
| 60 | # endif |
Rich Salz | 9c3bcfa | 2015-05-15 13:50:38 -0400 | [diff] [blame] | 61 | # ifndef OPENSSL_NO_ENGINE |
| 62 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, |
| 63 | # endif |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 64 | {NULL} |
| 65 | }; |
| 66 | |
| 67 | int dsaparam_main(int argc, char **argv) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 68 | { |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 69 | ENGINE *e = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 70 | DSA *dsa = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 71 | BIO *in = NULL, *out = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 72 | BN_GENCB *cb = NULL; |
Rich Salz | 700b4a4 | 2015-12-14 15:24:27 -0500 | [diff] [blame] | 73 | int numbits = -1, num = 0, genkey = 0, need_rand = 0; |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 74 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
| 75 | int ret = 1, i, text = 0, private = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 76 | # ifdef GENCB_TEST |
| 77 | int timebomb = 0; |
| 78 | # endif |
Rich Salz | 333b070 | 2015-04-25 15:41:29 -0400 | [diff] [blame] | 79 | char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 80 | OPTION_CHOICE o; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 81 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 82 | prog = opt_init(argc, argv, dsaparam_options); |
| 83 | while ((o = opt_next()) != OPT_EOF) { |
| 84 | switch (o) { |
| 85 | case OPT_EOF: |
| 86 | case OPT_ERR: |
| 87 | opthelp: |
| 88 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 89 | goto end; |
| 90 | case OPT_HELP: |
| 91 | opt_help(dsaparam_options); |
| 92 | ret = 0; |
| 93 | goto end; |
| 94 | case OPT_INFORM: |
| 95 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
| 96 | goto opthelp; |
| 97 | break; |
| 98 | case OPT_IN: |
| 99 | infile = opt_arg(); |
| 100 | break; |
| 101 | case OPT_OUTFORM: |
| 102 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
| 103 | goto opthelp; |
| 104 | break; |
| 105 | case OPT_OUT: |
| 106 | outfile = opt_arg(); |
| 107 | break; |
| 108 | case OPT_ENGINE: |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 109 | e = setup_engine(opt_arg(), 0); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 110 | break; |
| 111 | case OPT_TIMEBOMB: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 112 | # ifdef GENCB_TEST |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 113 | timebomb = atoi(opt_arg()); |
| 114 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 115 | # endif |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 116 | case OPT_TEXT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 117 | text = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 118 | break; |
| 119 | case OPT_C: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 120 | C = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 121 | break; |
| 122 | case OPT_GENKEY: |
| 123 | genkey = need_rand = 1; |
| 124 | break; |
| 125 | case OPT_RAND: |
| 126 | inrand = opt_arg(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 127 | need_rand = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 128 | break; |
| 129 | case OPT_NOOUT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 130 | noout = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 131 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 132 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 133 | } |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 134 | argc = opt_num_rest(); |
| 135 | argv = opt_rest(); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 136 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 137 | if (argc == 1) { |
Rich Salz | bd4850d | 2016-01-11 20:40:38 -0500 | [diff] [blame] | 138 | if (!opt_int(argv[0], &num) || num < 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 139 | goto end; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 140 | /* generate a key */ |
| 141 | numbits = num; |
| 142 | need_rand = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 143 | } |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 144 | private = genkey ? 1 : 0; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 145 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 146 | in = bio_open_default(infile, 'r', informat); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 147 | if (in == NULL) |
| 148 | goto end; |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 149 | out = bio_open_owner(outfile, outformat, private); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 150 | if (out == NULL) |
| 151 | goto end; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 152 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 153 | if (need_rand) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 154 | app_RAND_load_file(NULL, (inrand != NULL)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 155 | if (inrand != NULL) |
| 156 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", |
| 157 | app_RAND_load_files(inrand)); |
| 158 | } |
Bodo Möller | a31011e | 1999-10-26 01:56:29 +0000 | [diff] [blame] | 159 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 160 | if (numbits > 0) { |
| 161 | cb = BN_GENCB_new(); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 162 | if (cb == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 163 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); |
| 164 | goto end; |
| 165 | } |
| 166 | BN_GENCB_set(cb, dsa_cb, bio_err); |
| 167 | assert(need_rand); |
| 168 | dsa = DSA_new(); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 169 | if (dsa == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 170 | BIO_printf(bio_err, "Error allocating DSA object\n"); |
| 171 | goto end; |
| 172 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 173 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", |
| 174 | num); |
| 175 | BIO_printf(bio_err, "This could take some time\n"); |
| 176 | # ifdef GENCB_TEST |
| 177 | if (timebomb > 0) { |
| 178 | struct sigaction act; |
| 179 | act.sa_handler = timebomb_sigalarm; |
| 180 | act.sa_flags = 0; |
| 181 | BIO_printf(bio_err, |
| 182 | "(though I'll stop it if not done within %d secs)\n", |
| 183 | timebomb); |
| 184 | if (sigaction(SIGALRM, &act, NULL) != 0) { |
| 185 | BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n"); |
| 186 | goto end; |
| 187 | } |
| 188 | alarm(timebomb); |
| 189 | } |
| 190 | # endif |
| 191 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { |
| 192 | # ifdef GENCB_TEST |
| 193 | if (stop_keygen_flag) { |
| 194 | BIO_printf(bio_err, "DSA key generation time-stopped\n"); |
| 195 | /* This is an asked-for behaviour! */ |
| 196 | ret = 0; |
| 197 | goto end; |
| 198 | } |
| 199 | # endif |
| 200 | ERR_print_errors(bio_err); |
| 201 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); |
| 202 | goto end; |
| 203 | } |
| 204 | } else if (informat == FORMAT_ASN1) |
| 205 | dsa = d2i_DSAparams_bio(in, NULL); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 206 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 207 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 208 | if (dsa == NULL) { |
| 209 | BIO_printf(bio_err, "unable to load DSA parameters\n"); |
| 210 | ERR_print_errors(bio_err); |
| 211 | goto end; |
| 212 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 213 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 214 | if (text) { |
| 215 | DSAparams_print(out, dsa); |
| 216 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 217 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 218 | if (C) { |
Richard Levitte | 2ac6115 | 2016-06-14 15:49:05 +0200 | [diff] [blame] | 219 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; |
Viktor Dukhovni | ae6c553 | 2016-04-03 20:58:09 -0400 | [diff] [blame] | 220 | unsigned char *data; |
Matt Caswell | 6e9fa57 | 2016-03-30 17:18:55 +0100 | [diff] [blame] | 221 | int len, bits_p; |
| 222 | |
| 223 | DSA_get0_pqg(dsa, &p, &q, &g); |
| 224 | len = BN_num_bytes(p); |
| 225 | bits_p = BN_num_bits(p); |
| 226 | |
Viktor Dukhovni | ae6c553 | 2016-04-03 20:58:09 -0400 | [diff] [blame] | 227 | data = app_malloc(len + 20, "BN space"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 228 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 229 | BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p); |
Matt Caswell | 6e9fa57 | 2016-03-30 17:18:55 +0100 | [diff] [blame] | 230 | print_bignum_var(bio_out, p, "dsap", len, data); |
| 231 | print_bignum_var(bio_out, q, "dsaq", len, data); |
| 232 | print_bignum_var(bio_out, g, "dsag", len, data); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 233 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" |
| 234 | "\n"); |
| 235 | BIO_printf(bio_out, " if (dsa == NULL)\n" |
| 236 | " return NULL;\n"); |
| 237 | BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n", |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 238 | bits_p, bits_p); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 239 | BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n", |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 240 | bits_p, bits_p); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 241 | BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n", |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 242 | bits_p, bits_p); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 243 | BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n" |
| 244 | " DSA_free(dsa);\n" |
| 245 | " return NULL;\n" |
| 246 | " }\n" |
| 247 | " return(dsa);\n}\n"); |
Matt Caswell | a855d1a | 2016-04-27 14:54:58 +0100 | [diff] [blame] | 248 | OPENSSL_free(data); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 249 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 250 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 251 | if (!noout) { |
| 252 | if (outformat == FORMAT_ASN1) |
| 253 | i = i2d_DSAparams_bio(out, dsa); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 254 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 255 | i = PEM_write_bio_DSAparams(out, dsa); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 256 | if (!i) { |
| 257 | BIO_printf(bio_err, "unable to write DSA parameters\n"); |
| 258 | ERR_print_errors(bio_err); |
| 259 | goto end; |
| 260 | } |
| 261 | } |
| 262 | if (genkey) { |
| 263 | DSA *dsakey; |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 264 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 265 | assert(need_rand); |
| 266 | if ((dsakey = DSAparams_dup(dsa)) == NULL) |
| 267 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 268 | if (!DSA_generate_key(dsakey)) { |
| 269 | ERR_print_errors(bio_err); |
| 270 | DSA_free(dsakey); |
| 271 | goto end; |
| 272 | } |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 273 | assert(private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 274 | if (outformat == FORMAT_ASN1) |
| 275 | i = i2d_DSAPrivateKey_bio(out, dsakey); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 276 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 277 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
| 278 | NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 279 | DSA_free(dsakey); |
| 280 | } |
| 281 | if (need_rand) |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 282 | app_RAND_write_file(NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 283 | ret = 0; |
| 284 | end: |
Rich Salz | 23a1d5e | 2015-04-30 21:37:06 -0400 | [diff] [blame] | 285 | BN_GENCB_free(cb); |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 286 | BIO_free(in); |
| 287 | BIO_free_all(out); |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 288 | DSA_free(dsa); |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 289 | release_engine(e); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 290 | return (ret); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 291 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 292 | |
Rich Salz | 6d23cf9 | 2015-01-12 17:29:26 -0500 | [diff] [blame] | 293 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 294 | { |
| 295 | char c = '*'; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 296 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 297 | if (p == 0) |
| 298 | c = '.'; |
| 299 | if (p == 1) |
| 300 | c = '+'; |
| 301 | if (p == 2) |
| 302 | c = '*'; |
| 303 | if (p == 3) |
| 304 | c = '\n'; |
| 305 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); |
| 306 | (void)BIO_flush(BN_GENCB_get_arg(cb)); |
| 307 | # ifdef GENCB_TEST |
| 308 | if (stop_keygen_flag) |
| 309 | return 0; |
| 310 | # endif |
| 311 | return 1; |
| 312 | } |
Ulf Möller | f5d7a03 | 1999-04-27 01:14:46 +0000 | [diff] [blame] | 313 | #endif |