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