cf24416bd7fa3874b1f69ba36fe3e23ef71ed04c
[openssl.git] / apps / gendsa.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
8  */
9
10 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <openssl/opensslconf.h>
14 #ifdef OPENSSL_NO_DSA
15 NON_EMPTY_TRANSLATION_UNIT
16 #else
17
18 # include <stdio.h>
19 # include <string.h>
20 # include <sys/types.h>
21 # include <sys/stat.h>
22 # include "apps.h"
23 # include "progs.h"
24 # include <openssl/bio.h>
25 # include <openssl/err.h>
26 # include <openssl/bn.h>
27 # include <openssl/dsa.h>
28 # include <openssl/x509.h>
29 # include <openssl/pem.h>
30
31 typedef enum OPTION_choice {
32     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
33     OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE,
34     OPT_R_ENUM, OPT_PROV_ENUM
35 } OPTION_CHOICE;
36
37 const OPTIONS gendsa_options[] = {
38     {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
39
40     OPT_SECTION("General"),
41     {"help", OPT_HELP, '-', "Display this summary"},
42 # ifndef OPENSSL_NO_ENGINE
43     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44 # endif
45
46     OPT_SECTION("Output"),
47     {"out", OPT_OUT, '>', "Output the key to the specified file"},
48     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
49     OPT_R_OPTIONS,
50     OPT_PROV_OPTIONS,
51     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
52     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
53
54     OPT_PARAMETERS(),
55     {"dsaparam-file", 0, 0, "File containing DSA parameters"},
56     {NULL}
57 };
58
59 int gendsa_main(int argc, char **argv)
60 {
61     ENGINE *e = NULL;
62     BIO *out = NULL, *in = NULL;
63     DSA *dsa = NULL;
64     const EVP_CIPHER *enc = NULL;
65     char *dsaparams = NULL;
66     char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
67     OPTION_CHOICE o;
68     int ret = 1, private = 0, verbose = 0;
69     const BIGNUM *p = NULL;
70
71     prog = opt_init(argc, argv, gendsa_options);
72     while ((o = opt_next()) != OPT_EOF) {
73         switch (o) {
74         case OPT_EOF:
75         case OPT_ERR:
76  opthelp:
77             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
78             goto end;
79         case OPT_HELP:
80             ret = 0;
81             opt_help(gendsa_options);
82             goto end;
83         case OPT_OUT:
84             outfile = opt_arg();
85             break;
86         case OPT_PASSOUT:
87             passoutarg = opt_arg();
88             break;
89         case OPT_ENGINE:
90             e = setup_engine(opt_arg(), 0);
91             break;
92         case OPT_R_CASES:
93             if (!opt_rand(o))
94                 goto end;
95             break;
96         case OPT_PROV_CASES:
97             if (!opt_provider(o))
98                 goto end;
99             break;
100         case OPT_CIPHER:
101             if (!opt_cipher(opt_unknown(), &enc))
102                 goto end;
103             break;
104         case OPT_VERBOSE:
105             verbose = 1;
106             break;
107         }
108     }
109     argc = opt_num_rest();
110     argv = opt_rest();
111     private = 1;
112
113     if (argc != 1)
114         goto opthelp;
115     dsaparams = *argv;
116
117     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
118         BIO_printf(bio_err, "Error getting password\n");
119         goto end;
120     }
121
122     in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
123     if (in == NULL)
124         goto end2;
125
126     if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
127         BIO_printf(bio_err, "unable to load DSA parameter file\n");
128         goto end;
129     }
130     BIO_free(in);
131     in = NULL;
132
133     out = bio_open_owner(outfile, FORMAT_PEM, private);
134     if (out == NULL)
135         goto end2;
136
137     DSA_get0_pqg(dsa, &p, NULL, NULL);
138
139     if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
140         BIO_printf(bio_err,
141                    "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
142                    "         Your key size is %d! Larger key size may behave not as expected.\n",
143                    OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
144
145     if (verbose)
146         BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
147     if (!DSA_generate_key(dsa))
148         goto end;
149
150     assert(private);
151     if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
152         goto end;
153     ret = 0;
154  end:
155     if (ret != 0)
156         ERR_print_errors(bio_err);
157  end2:
158     BIO_free(in);
159     BIO_free_all(out);
160     DSA_free(dsa);
161     release_engine(e);
162     OPENSSL_free(passout);
163     return ret;
164 }
165 #endif