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 | |
Rich Salz | 6d23cf9 | 2015-01-12 17:29:26 -0500 | [diff] [blame] | 27 | static int dsa_cb(int p, int n, BN_GENCB *cb); |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 28 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 29 | typedef enum OPTION_choice { |
| 30 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
| 31 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, |
Rich Salz | a2b22cd | 2017-02-28 12:08:54 -0500 | [diff] [blame] | 32 | OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 33 | } OPTION_CHOICE; |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 34 | |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 35 | const OPTIONS dsaparam_options[] = { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 36 | {"help", OPT_HELP, '-', "Display this summary"}, |
| 37 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, |
| 38 | {"in", OPT_IN, '<', "Input file"}, |
| 39 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, |
| 40 | {"out", OPT_OUT, '>', "Output file"}, |
| 41 | {"text", OPT_TEXT, '-', "Print as text"}, |
| 42 | {"C", OPT_C, '-', "Output C code"}, |
| 43 | {"noout", OPT_NOOUT, '-', "No output"}, |
| 44 | {"genkey", OPT_GENKEY, '-', "Generate a DSA key"}, |
| 45 | {"rand", OPT_RAND, 's', "Files to use for random number input"}, |
Rich Salz | 9c3bcfa | 2015-05-15 13:50:38 -0400 | [diff] [blame] | 46 | # ifndef OPENSSL_NO_ENGINE |
| 47 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, |
| 48 | # endif |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 49 | {NULL} |
| 50 | }; |
| 51 | |
| 52 | int dsaparam_main(int argc, char **argv) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | { |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 54 | ENGINE *e = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 55 | DSA *dsa = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 56 | BIO *in = NULL, *out = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 57 | BN_GENCB *cb = NULL; |
Rich Salz | 700b4a4 | 2015-12-14 15:24:27 -0500 | [diff] [blame] | 58 | int numbits = -1, num = 0, genkey = 0, need_rand = 0; |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 59 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
| 60 | int ret = 1, i, text = 0, private = 0; |
Rich Salz | 333b070 | 2015-04-25 15:41:29 -0400 | [diff] [blame] | 61 | char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 62 | OPTION_CHOICE o; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 63 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 64 | prog = opt_init(argc, argv, dsaparam_options); |
| 65 | while ((o = opt_next()) != OPT_EOF) { |
| 66 | switch (o) { |
| 67 | case OPT_EOF: |
| 68 | case OPT_ERR: |
| 69 | opthelp: |
| 70 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 71 | goto end; |
| 72 | case OPT_HELP: |
| 73 | opt_help(dsaparam_options); |
| 74 | ret = 0; |
| 75 | goto end; |
| 76 | case OPT_INFORM: |
| 77 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
| 78 | goto opthelp; |
| 79 | break; |
| 80 | case OPT_IN: |
| 81 | infile = opt_arg(); |
| 82 | break; |
| 83 | case OPT_OUTFORM: |
| 84 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
| 85 | goto opthelp; |
| 86 | break; |
| 87 | case OPT_OUT: |
| 88 | outfile = opt_arg(); |
| 89 | break; |
| 90 | case OPT_ENGINE: |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 91 | e = setup_engine(opt_arg(), 0); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 92 | break; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 93 | case OPT_TEXT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 94 | text = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 95 | break; |
| 96 | case OPT_C: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 97 | C = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 98 | break; |
| 99 | case OPT_GENKEY: |
| 100 | genkey = need_rand = 1; |
| 101 | break; |
| 102 | case OPT_RAND: |
| 103 | inrand = opt_arg(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 104 | need_rand = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 105 | break; |
| 106 | case OPT_NOOUT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 107 | noout = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 108 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 109 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 110 | } |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 111 | argc = opt_num_rest(); |
| 112 | argv = opt_rest(); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 113 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 114 | if (argc == 1) { |
Rich Salz | bd4850d | 2016-01-11 20:40:38 -0500 | [diff] [blame] | 115 | if (!opt_int(argv[0], &num) || num < 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 116 | goto end; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 117 | /* generate a key */ |
| 118 | numbits = num; |
| 119 | need_rand = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 120 | } |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 121 | private = genkey ? 1 : 0; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 122 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 123 | in = bio_open_default(infile, 'r', informat); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 124 | if (in == NULL) |
| 125 | goto end; |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 126 | out = bio_open_owner(outfile, outformat, private); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 127 | if (out == NULL) |
| 128 | goto end; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 129 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 130 | if (need_rand) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 131 | app_RAND_load_file(NULL, (inrand != NULL)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 132 | if (inrand != NULL) |
| 133 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", |
| 134 | app_RAND_load_files(inrand)); |
| 135 | } |
Bodo Möller | a31011e | 1999-10-26 01:56:29 +0000 | [diff] [blame] | 136 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | if (numbits > 0) { |
| 138 | cb = BN_GENCB_new(); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 139 | if (cb == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 140 | BIO_printf(bio_err, "Error allocating BN_GENCB object\n"); |
| 141 | goto end; |
| 142 | } |
| 143 | BN_GENCB_set(cb, dsa_cb, bio_err); |
| 144 | assert(need_rand); |
| 145 | dsa = DSA_new(); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 146 | if (dsa == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 147 | BIO_printf(bio_err, "Error allocating DSA object\n"); |
| 148 | goto end; |
| 149 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 150 | BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", |
| 151 | num); |
| 152 | BIO_printf(bio_err, "This could take some time\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 153 | if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 154 | ERR_print_errors(bio_err); |
| 155 | BIO_printf(bio_err, "Error, DSA key generation failed\n"); |
| 156 | goto end; |
| 157 | } |
| 158 | } else if (informat == FORMAT_ASN1) |
| 159 | dsa = d2i_DSAparams_bio(in, NULL); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 160 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 161 | dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 162 | if (dsa == NULL) { |
| 163 | BIO_printf(bio_err, "unable to load DSA parameters\n"); |
| 164 | ERR_print_errors(bio_err); |
| 165 | goto end; |
| 166 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 167 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 168 | if (text) { |
| 169 | DSAparams_print(out, dsa); |
| 170 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 171 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 172 | if (C) { |
Richard Levitte | 2ac6115 | 2016-06-14 15:49:05 +0200 | [diff] [blame] | 173 | const BIGNUM *p = NULL, *q = NULL, *g = NULL; |
Viktor Dukhovni | ae6c553 | 2016-04-03 20:58:09 -0400 | [diff] [blame] | 174 | unsigned char *data; |
Matt Caswell | 6e9fa57 | 2016-03-30 17:18:55 +0100 | [diff] [blame] | 175 | int len, bits_p; |
| 176 | |
| 177 | DSA_get0_pqg(dsa, &p, &q, &g); |
| 178 | len = BN_num_bytes(p); |
| 179 | bits_p = BN_num_bits(p); |
| 180 | |
Viktor Dukhovni | ae6c553 | 2016-04-03 20:58:09 -0400 | [diff] [blame] | 181 | data = app_malloc(len + 20, "BN space"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 182 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 183 | BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p); |
Matt Caswell | 6e9fa57 | 2016-03-30 17:18:55 +0100 | [diff] [blame] | 184 | print_bignum_var(bio_out, p, "dsap", len, data); |
| 185 | print_bignum_var(bio_out, q, "dsaq", len, data); |
| 186 | print_bignum_var(bio_out, g, "dsag", len, data); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 187 | BIO_printf(bio_out, " DSA *dsa = DSA_new();\n" |
| 188 | "\n"); |
| 189 | BIO_printf(bio_out, " if (dsa == NULL)\n" |
| 190 | " return NULL;\n"); |
| 191 | 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] | 192 | bits_p, bits_p); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 193 | 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] | 194 | bits_p, bits_p); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 195 | 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] | 196 | bits_p, bits_p); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 197 | BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n" |
| 198 | " DSA_free(dsa);\n" |
| 199 | " return NULL;\n" |
| 200 | " }\n" |
| 201 | " return(dsa);\n}\n"); |
Matt Caswell | a855d1a | 2016-04-27 14:54:58 +0100 | [diff] [blame] | 202 | OPENSSL_free(data); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 203 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 204 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 205 | if (!noout) { |
| 206 | if (outformat == FORMAT_ASN1) |
| 207 | i = i2d_DSAparams_bio(out, dsa); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 208 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 209 | i = PEM_write_bio_DSAparams(out, dsa); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 210 | if (!i) { |
| 211 | BIO_printf(bio_err, "unable to write DSA parameters\n"); |
| 212 | ERR_print_errors(bio_err); |
| 213 | goto end; |
| 214 | } |
| 215 | } |
| 216 | if (genkey) { |
| 217 | DSA *dsakey; |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 218 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 219 | assert(need_rand); |
| 220 | if ((dsakey = DSAparams_dup(dsa)) == NULL) |
| 221 | goto end; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 222 | if (!DSA_generate_key(dsakey)) { |
| 223 | ERR_print_errors(bio_err); |
| 224 | DSA_free(dsakey); |
| 225 | goto end; |
| 226 | } |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 227 | assert(private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 228 | if (outformat == FORMAT_ASN1) |
| 229 | i = i2d_DSAPrivateKey_bio(out, dsakey); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 230 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 231 | i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, |
| 232 | NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 233 | DSA_free(dsakey); |
| 234 | } |
| 235 | if (need_rand) |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 236 | app_RAND_write_file(NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 237 | ret = 0; |
| 238 | end: |
Rich Salz | 23a1d5e | 2015-04-30 21:37:06 -0400 | [diff] [blame] | 239 | BN_GENCB_free(cb); |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 240 | BIO_free(in); |
| 241 | BIO_free_all(out); |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 242 | DSA_free(dsa); |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 243 | release_engine(e); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 244 | return (ret); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 245 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 246 | |
Rich Salz | 6d23cf9 | 2015-01-12 17:29:26 -0500 | [diff] [blame] | 247 | static int dsa_cb(int p, int n, BN_GENCB *cb) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 248 | { |
| 249 | char c = '*'; |
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 (p == 0) |
| 252 | c = '.'; |
| 253 | if (p == 1) |
| 254 | c = '+'; |
| 255 | if (p == 2) |
| 256 | c = '*'; |
| 257 | if (p == 3) |
| 258 | c = '\n'; |
| 259 | BIO_write(BN_GENCB_get_arg(cb), &c, 1); |
| 260 | (void)BIO_flush(BN_GENCB_get_arg(cb)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 261 | return 1; |
| 262 | } |
Ulf Möller | f5d7a03 | 1999-04-27 01:14:46 +0000 | [diff] [blame] | 263 | #endif |