blob: 06e37924e44d205a5a3a67b370ebcb124a035c4c [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
Matt Caswell6738bf12018-02-13 12:51:29 +00002 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
9
Rich Salzeffaf4d2016-01-31 13:08:23 -050010#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_DSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
Matt Caswell0f113f32015-01-22 03:40:55 +000015# include <stdio.h>
16# include <string.h>
17# include <sys/types.h>
18# include <sys/stat.h>
19# include "apps.h"
Richard Levittedab2cd62018-01-31 11:13:10 +010020# include "progs.h"
Matt Caswell0f113f32015-01-22 03:40:55 +000021# 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. Engelschalld02b48c1998-12-21 10:52:47 +000027
Rich Salz7e1b7482015-04-24 15:26:15 -040028typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
Rich Salz3ee1eac2017-07-05 10:58:48 -040030 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER,
31 OPT_R_ENUM
Rich Salz7e1b7482015-04-24 15:26:15 -040032} OPTION_CHOICE;
Ralf S. Engelschall667ac4e2000-02-11 09:47:18 +000033
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010034const OPTIONS gendsa_options[] = {
Rich Salz7e1b7482015-04-24 15:26:15 -040035 {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"},
FdaSilvaYY12d56b22016-07-31 19:02:50 +020039 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
Rich Salz3ee1eac2017-07-05 10:58:48 -040040 OPT_R_OPTIONS,
Rich Salz9c3bcfa2015-05-15 13:50:38 -040041 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
Rich Salz7e1b7482015-04-24 15:26:15 -040042# ifndef OPENSSL_NO_ENGINE
43 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44# endif
Rich Salz7e1b7482015-04-24 15:26:15 -040045 {NULL}
46};
47
48int gendsa_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +000049{
Richard Levittedd1abd42016-09-28 23:39:18 +020050 ENGINE *e = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000051 BIO *out = NULL, *in = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -040052 DSA *dsa = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000053 const EVP_CIPHER *enc = NULL;
Rich Salz3ee1eac2017-07-05 10:58:48 -040054 char *dsaparams = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -040055 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
56 OPTION_CHOICE o;
Rich Salz3b061a02015-05-02 10:01:33 -040057 int ret = 1, private = 0;
Richard Levitte2ac61152016-06-14 15:49:05 +020058 const BIGNUM *p = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000059
Rich Salz7e1b7482015-04-24 15:26:15 -040060 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 Caswell0f113f32015-01-22 03:40:55 +000074 break;
Rich Salz7e1b7482015-04-24 15:26:15 -040075 case OPT_PASSOUT:
76 passoutarg = opt_arg();
77 break;
78 case OPT_ENGINE:
Richard Levittedd1abd42016-09-28 23:39:18 +020079 e = setup_engine(opt_arg(), 0);
Rich Salz7e1b7482015-04-24 15:26:15 -040080 break;
Rich Salz3ee1eac2017-07-05 10:58:48 -040081 case OPT_R_CASES:
82 if (!opt_rand(o))
83 goto end;
Rich Salz7e1b7482015-04-24 15:26:15 -040084 break;
85 case OPT_CIPHER:
86 if (!opt_cipher(opt_unknown(), &enc))
87 goto end;
88 break;
Matt Caswell0f113f32015-01-22 03:40:55 +000089 }
Matt Caswell0f113f32015-01-22 03:40:55 +000090 }
Rich Salz7e1b7482015-04-24 15:26:15 -040091 argc = opt_num_rest();
92 argv = opt_rest();
Rich Salz3b061a02015-05-02 10:01:33 -040093 private = 1;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000094
Rich Salz7e1b7482015-04-24 15:26:15 -040095 if (argc != 1)
96 goto opthelp;
97 dsaparams = *argv;
98
Rich Salz7e1b7482015-04-24 15:26:15 -040099 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000100 BIO_printf(bio_err, "Error getting password\n");
101 goto end;
102 }
Richard Levitte5270e702000-10-26 21:07:28 +0000103
Richard Levittebdd58d92015-09-04 12:49:06 +0200104 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
Rich Salz7e1b7482015-04-24 15:26:15 -0400105 if (in == NULL)
106 goto end2;
Dr. Stephen Hensona3fe3822000-02-16 23:16:01 +0000107
Matt Caswell0f113f32015-01-22 03:40:55 +0000108 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 Hensona3fe3822000-02-16 23:16:01 +0000114
Richard Levittebdd58d92015-09-04 12:49:06 +0200115 out = bio_open_owner(outfile, FORMAT_PEM, private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000116 if (out == NULL)
Rich Salz7e1b7482015-04-24 15:26:15 -0400117 goto end2;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000118
Matt Caswell6e9fa572016-03-30 17:18:55 +0100119 DSA_get0_pqg(dsa, &p, NULL, NULL);
120 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
Matt Caswell0f113f32015-01-22 03:40:55 +0000121 if (!DSA_generate_key(dsa))
122 goto end;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000123
Rich Salz3b061a02015-05-02 10:01:33 -0400124 assert(private);
Matt Caswell0f113f32015-01-22 03:40:55 +0000125 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 Salz7e1b7482015-04-24 15:26:15 -0400131 end2:
Rich Salzca3a82c2015-03-25 11:31:18 -0400132 BIO_free(in);
133 BIO_free_all(out);
Rich Salzd6407082015-03-24 10:17:37 -0400134 DSA_free(dsa);
Richard Levittedd1abd42016-09-28 23:39:18 +0200135 release_engine(e);
Rich Salzb548a1f2015-05-01 10:02:07 -0400136 OPENSSL_free(passout);
KaoruToda26a7d932017-10-17 23:04:09 +0900137 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000138}
Ulf Möllerf5d7a031999-04-27 01:14:46 +0000139#endif