Apps: don't build deprecated DH and DSA apps.
[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
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_CIPHER, '-', "Encrypt the output with any supported cipher"},
51     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
52
53     OPT_PARAMETERS(),
54     {"dsaparam-file", 0, 0, "File containing DSA parameters"},
55     {NULL}
56 };
57
58 int gendsa_main(int argc, char **argv)
59 {
60     ENGINE *e = NULL;
61     BIO *out = NULL, *in = NULL;
62     DSA *dsa = NULL;
63     const EVP_CIPHER *enc = NULL;
64     char *dsaparams = NULL;
65     char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
66     OPTION_CHOICE o;
67     int ret = 1, private = 0, verbose = 0;
68     const BIGNUM *p = NULL;
69
70     prog = opt_init(argc, argv, gendsa_options);
71     while ((o = opt_next()) != OPT_EOF) {
72         switch (o) {
73         case OPT_EOF:
74         case OPT_ERR:
75  opthelp:
76             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
77             goto end;
78         case OPT_HELP:
79             ret = 0;
80             opt_help(gendsa_options);
81             goto end;
82         case OPT_OUT:
83             outfile = opt_arg();
84             break;
85         case OPT_PASSOUT:
86             passoutarg = opt_arg();
87             break;
88         case OPT_ENGINE:
89             e = setup_engine(opt_arg(), 0);
90             break;
91         case OPT_R_CASES:
92             if (!opt_rand(o))
93                 goto end;
94             break;
95         case OPT_CIPHER:
96             if (!opt_cipher(opt_unknown(), &enc))
97                 goto end;
98             break;
99         case OPT_VERBOSE:
100             verbose = 1;
101             break;
102         }
103     }
104     argc = opt_num_rest();
105     argv = opt_rest();
106     private = 1;
107
108     if (argc != 1)
109         goto opthelp;
110     dsaparams = *argv;
111
112     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
113         BIO_printf(bio_err, "Error getting password\n");
114         goto end;
115     }
116
117     in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
118     if (in == NULL)
119         goto end2;
120
121     if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
122         BIO_printf(bio_err, "unable to load DSA parameter file\n");
123         goto end;
124     }
125     BIO_free(in);
126     in = NULL;
127
128     out = bio_open_owner(outfile, FORMAT_PEM, private);
129     if (out == NULL)
130         goto end2;
131
132     DSA_get0_pqg(dsa, &p, NULL, NULL);
133
134     if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
135         BIO_printf(bio_err,
136                    "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
137                    "         Your key size is %d! Larger key size may behave not as expected.\n",
138                    OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
139
140     if (verbose)
141         BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
142     if (!DSA_generate_key(dsa))
143         goto end;
144
145     assert(private);
146     if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
147         goto end;
148     ret = 0;
149  end:
150     if (ret != 0)
151         ERR_print_errors(bio_err);
152  end2:
153     BIO_free(in);
154     BIO_free_all(out);
155     DSA_free(dsa);
156     release_engine(e);
157     OPENSSL_free(passout);
158     return ret;
159 }
160 #endif