Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 1 | /* |
Matt Caswell | 6738bf1 | 2018-02-13 12:51:29 +0000 | [diff] [blame] | 2 | * Copyright 1995-2018 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 |
| 14 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 15 | # include <stdio.h> |
| 16 | # include <string.h> |
| 17 | # include <sys/types.h> |
| 18 | # include <sys/stat.h> |
| 19 | # include "apps.h" |
Richard Levitte | dab2cd6 | 2018-01-31 11:13:10 +0100 | [diff] [blame] | 20 | # include "progs.h" |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 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> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 27 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 28 | typedef enum OPTION_choice { |
| 29 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 30 | OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, |
| 31 | OPT_R_ENUM |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 32 | } OPTION_CHOICE; |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 33 | |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 34 | const OPTIONS gendsa_options[] = { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 35 | {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"}, |
| 36 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, |
| 37 | {"help", OPT_HELP, '-', "Display this summary"}, |
| 38 | {"out", OPT_OUT, '>', "Output the key to the specified file"}, |
FdaSilvaYY | 12d56b2 | 2016-07-31 19:02:50 +0200 | [diff] [blame] | 39 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 40 | OPT_R_OPTIONS, |
Rich Salz | 9c3bcfa | 2015-05-15 13:50:38 -0400 | [diff] [blame] | 41 | {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"}, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 42 | # ifndef OPENSSL_NO_ENGINE |
| 43 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
| 44 | # endif |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 45 | {NULL} |
| 46 | }; |
| 47 | |
| 48 | int gendsa_main(int argc, char **argv) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 49 | { |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 50 | ENGINE *e = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 51 | BIO *out = NULL, *in = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 52 | DSA *dsa = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | const EVP_CIPHER *enc = NULL; |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 54 | char *dsaparams = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 55 | char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog; |
| 56 | OPTION_CHOICE o; |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 57 | int ret = 1, private = 0; |
Richard Levitte | 2ac6115 | 2016-06-14 15:49:05 +0200 | [diff] [blame] | 58 | const BIGNUM *p = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 59 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 60 | prog = opt_init(argc, argv, gendsa_options); |
| 61 | while ((o = opt_next()) != OPT_EOF) { |
| 62 | switch (o) { |
| 63 | case OPT_EOF: |
| 64 | case OPT_ERR: |
| 65 | opthelp: |
| 66 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 67 | goto end; |
| 68 | case OPT_HELP: |
| 69 | ret = 0; |
| 70 | opt_help(gendsa_options); |
| 71 | goto end; |
| 72 | case OPT_OUT: |
| 73 | outfile = opt_arg(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 74 | break; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 75 | case OPT_PASSOUT: |
| 76 | passoutarg = opt_arg(); |
| 77 | break; |
| 78 | case OPT_ENGINE: |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 79 | e = setup_engine(opt_arg(), 0); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 80 | break; |
Rich Salz | 3ee1eac | 2017-07-05 10:58:48 -0400 | [diff] [blame] | 81 | case OPT_R_CASES: |
| 82 | if (!opt_rand(o)) |
| 83 | goto end; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 84 | break; |
| 85 | case OPT_CIPHER: |
| 86 | if (!opt_cipher(opt_unknown(), &enc)) |
| 87 | goto end; |
| 88 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 89 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 90 | } |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 91 | argc = opt_num_rest(); |
| 92 | argv = opt_rest(); |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 93 | private = 1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 94 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 95 | if (argc != 1) |
| 96 | goto opthelp; |
| 97 | dsaparams = *argv; |
| 98 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 99 | if (!app_passwd(NULL, passoutarg, NULL, &passout)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 100 | BIO_printf(bio_err, "Error getting password\n"); |
| 101 | goto end; |
| 102 | } |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 103 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 104 | in = bio_open_default(dsaparams, 'r', FORMAT_PEM); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 105 | if (in == NULL) |
| 106 | goto end2; |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 107 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 108 | if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) { |
| 109 | BIO_printf(bio_err, "unable to load DSA parameter file\n"); |
| 110 | goto end; |
| 111 | } |
| 112 | BIO_free(in); |
| 113 | in = NULL; |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 114 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 115 | out = bio_open_owner(outfile, FORMAT_PEM, private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 116 | if (out == NULL) |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 117 | goto end2; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 118 | |
Matt Caswell | 6e9fa57 | 2016-03-30 17:18:55 +0100 | [diff] [blame] | 119 | DSA_get0_pqg(dsa, &p, NULL, NULL); |
| 120 | BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 121 | if (!DSA_generate_key(dsa)) |
| 122 | goto end; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 123 | |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 124 | assert(private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 125 | if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) |
| 126 | goto end; |
| 127 | ret = 0; |
| 128 | end: |
| 129 | if (ret != 0) |
| 130 | ERR_print_errors(bio_err); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 131 | end2: |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 132 | BIO_free(in); |
| 133 | BIO_free_all(out); |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 134 | DSA_free(dsa); |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 135 | release_engine(e); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 136 | OPENSSL_free(passout); |
KaoruToda | 26a7d93 | 2017-10-17 23:04:09 +0900 | [diff] [blame] | 137 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 138 | } |
Ulf Möller | f5d7a03 | 1999-04-27 01:14:46 +0000 | [diff] [blame] | 139 | #endif |