]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/gendsa.c
Update copyright year
[thirdparty/openssl.git] / apps / gendsa.c
1 /*
2 * Copyright 1995-2021 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 EVP_PKEY *pkey = NULL;
58 EVP_PKEY_CTX *ctx = NULL;
59 const EVP_CIPHER *enc = NULL;
60 char *dsaparams = NULL, *ciphername = NULL;
61 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
62 OPTION_CHOICE o;
63 int ret = 1, private = 0, verbose = 0;
64 const BIGNUM *p = NULL;
65
66 prog = opt_init(argc, argv, gendsa_options);
67 while ((o = opt_next()) != OPT_EOF) {
68 switch (o) {
69 case OPT_EOF:
70 case OPT_ERR:
71 opthelp:
72 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
73 goto end;
74 case OPT_HELP:
75 ret = 0;
76 opt_help(gendsa_options);
77 goto end;
78 case OPT_OUT:
79 outfile = opt_arg();
80 break;
81 case OPT_PASSOUT:
82 passoutarg = opt_arg();
83 break;
84 case OPT_ENGINE:
85 e = setup_engine(opt_arg(), 0);
86 break;
87 case OPT_R_CASES:
88 if (!opt_rand(o))
89 goto end;
90 break;
91 case OPT_PROV_CASES:
92 if (!opt_provider(o))
93 goto end;
94 break;
95 case OPT_CIPHER:
96 ciphername = opt_unknown();
97 break;
98 case OPT_VERBOSE:
99 verbose = 1;
100 break;
101 }
102 }
103
104 /* One argument, the params file. */
105 argc = opt_num_rest();
106 argv = opt_rest();
107 if (argc != 1)
108 goto opthelp;
109 dsaparams = argv[0];
110
111 app_RAND_load();
112 if (ciphername != NULL) {
113 if (!opt_cipher(ciphername, &enc))
114 goto end;
115 }
116 private = 1;
117
118 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
119 BIO_printf(bio_err, "Error getting password\n");
120 goto end;
121 }
122
123 pkey = load_keyparams(dsaparams, 1, "DSA", "DSA parameters");
124
125 out = bio_open_owner(outfile, FORMAT_PEM, private);
126 if (out == NULL)
127 goto end2;
128
129 if (EVP_PKEY_bits(pkey) > OPENSSL_DSA_MAX_MODULUS_BITS)
130 BIO_printf(bio_err,
131 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
132 " Your key size is %d! Larger key size may behave not as expected.\n",
133 OPENSSL_DSA_MAX_MODULUS_BITS, EVP_PKEY_bits(pkey));
134
135 ctx = EVP_PKEY_CTX_new(pkey, NULL);
136 if (ctx == NULL) {
137 BIO_printf(bio_err, "unable to create PKEY context\n");
138 goto end;
139 }
140 EVP_PKEY_free(pkey);
141 pkey = NULL;
142 if (EVP_PKEY_keygen_init(ctx) <= 0) {
143 BIO_printf(bio_err, "unable to set up for key generation\n");
144 goto end;
145 }
146 if (verbose)
147 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
148 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
149 BIO_printf(bio_err, "unable to generate key\n");
150 goto end;
151 }
152
153 assert(private);
154 if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
155 BIO_printf(bio_err, "unable to output generated key\n");
156 goto end;
157 }
158 ret = 0;
159 end:
160 if (ret != 0)
161 ERR_print_errors(bio_err);
162 end2:
163 BIO_free(in);
164 BIO_free_all(out);
165 EVP_PKEY_free(pkey);
166 EVP_PKEY_CTX_free(ctx);
167 release_engine(e);
168 OPENSSL_free(passout);
169 return ret;
170 }