gendsa: update command line app to use EVP calls
[oweals/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
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include "apps.h"
20 #include "progs.h"
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>
27
28 typedef enum OPTION_choice {
29     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30     OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE,
31     OPT_R_ENUM, OPT_PROV_ENUM
32 } OPTION_CHOICE;
33
34 const OPTIONS gendsa_options[] = {
35     {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
36
37     OPT_SECTION("General"),
38     {"help", OPT_HELP, '-', "Display this summary"},
39 #ifndef OPENSSL_NO_ENGINE
40     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
41 #endif
42
43     OPT_SECTION("Output"),
44     {"out", OPT_OUT, '>', "Output the key to the specified file"},
45     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
46     OPT_R_OPTIONS,
47     OPT_PROV_OPTIONS,
48     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
49     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
50
51     OPT_PARAMETERS(),
52     {"dsaparam-file", 0, 0, "File containing DSA parameters"},
53     {NULL}
54 };
55
56 int gendsa_main(int argc, char **argv)
57 {
58     ENGINE *e = NULL;
59     BIO *out = NULL, *in = NULL;
60     DSA *dsa = NULL;
61     EVP_PKEY *pkey = NULL;
62     EVP_PKEY_CTX *ctx = 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_PROV_CASES:
96             if (!opt_provider(o))
97                 goto end;
98             break;
99         case OPT_CIPHER:
100             if (!opt_cipher(opt_unknown(), &enc))
101                 goto end;
102             break;
103         case OPT_VERBOSE:
104             verbose = 1;
105             break;
106         }
107     }
108     argc = opt_num_rest();
109     argv = opt_rest();
110     private = 1;
111
112     if (argc != 1)
113         goto opthelp;
114     dsaparams = *argv;
115
116     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
117         BIO_printf(bio_err, "Error getting password\n");
118         goto end;
119     }
120
121     in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
122     if (in == NULL)
123         goto end2;
124
125     if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
126         BIO_printf(bio_err, "unable to load DSA parameter file\n");
127         goto end;
128     }
129     BIO_free(in);
130     in = NULL;
131
132     out = bio_open_owner(outfile, FORMAT_PEM, private);
133     if (out == NULL)
134         goto end2;
135
136     DSA_get0_pqg(dsa, &p, NULL, NULL);
137
138     if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
139         BIO_printf(bio_err,
140                    "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
141                    "         Your key size is %d! Larger key size may behave not as expected.\n",
142                    OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
143
144     pkey = EVP_PKEY_new();
145     if (pkey == NULL) {
146         BIO_printf(bio_err, "unable to allocate PKEY\n");
147         goto end;
148     }
149     if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
150         BIO_printf(bio_err, "unable to associate DSA parameters with PKEY\n");
151         goto end;
152     }
153     ctx = EVP_PKEY_CTX_new(pkey, NULL);
154     if (ctx == NULL) {
155         BIO_printf(bio_err, "unable to create PKEY context\n");
156         goto end;
157     }
158     EVP_PKEY_free(pkey);
159     pkey = NULL;
160     if (EVP_PKEY_keygen_init(ctx) <= 0) {
161         BIO_printf(bio_err, "unable to set up for key generation\n");
162         goto end;
163     }
164     if (verbose)
165         BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
166     if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
167         BIO_printf(bio_err, "unable to generate key\n");
168         goto end;
169     }
170
171     assert(private);
172     if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
173         BIO_printf(bio_err, "unable to output generated key\n");
174         goto end;
175     }
176     ret = 0;
177  end:
178     if (ret != 0)
179         ERR_print_errors(bio_err);
180  end2:
181     BIO_free(in);
182     BIO_free_all(out);
183     DSA_free(dsa);
184     EVP_PKEY_free(pkey);
185     EVP_PKEY_CTX_free(ctx);
186     release_engine(e);
187     OPENSSL_free(passout);
188     return ret;
189 }