]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/gendsa.c
APPS: Add check for multiple 'unknown' options
[thirdparty/openssl.git] / apps / gendsa.c
CommitLineData
846e33c7 1/*
a28d06f3 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
d02b48c6
RE
8 */
9
effaf4de 10#include <openssl/opensslconf.h>
1ae56f2f
RS
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>
d02b48c6 24
7e1b7485 25typedef enum OPTION_choice {
b0f96018 26 OPT_COMMON,
b6a07f67 27 OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE,
6bd4e3f2 28 OPT_R_ENUM, OPT_PROV_ENUM
7e1b7485
RS
29} OPTION_CHOICE;
30
44c83ebd 31const OPTIONS gendsa_options[] = {
92de469f 32 {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
5388f986
RS
33
34 OPT_SECTION("General"),
7e1b7485 35 {"help", OPT_HELP, '-', "Display this summary"},
1ae56f2f 36#ifndef OPENSSL_NO_ENGINE
5388f986 37 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
1ae56f2f 38#endif
5388f986
RS
39
40 OPT_SECTION("Output"),
7e1b7485 41 {"out", OPT_OUT, '>', "Output the key to the specified file"},
12d56b29 42 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
3ee1eac2 43 OPT_R_OPTIONS,
6bd4e3f2 44 OPT_PROV_OPTIONS,
9c3bcfa0 45 {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
b6a07f67 46 {"verbose", OPT_VERBOSE, '-', "Verbose output"},
92de469f
RS
47
48 OPT_PARAMETERS(),
49 {"dsaparam-file", 0, 0, "File containing DSA parameters"},
7e1b7485
RS
50 {NULL}
51};
667ac4ec 52
7e1b7485 53int gendsa_main(int argc, char **argv)
0f113f3e 54{
dd1abd44 55 ENGINE *e = NULL;
0f113f3e 56 BIO *out = NULL, *in = NULL;
dddbbc6f
P
57 EVP_PKEY *pkey = NULL;
58 EVP_PKEY_CTX *ctx = NULL;
606a417f 59 EVP_CIPHER *enc = NULL;
03bbd346 60 char *dsaparams = NULL, *ciphername = NULL;
7e1b7485
RS
61 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
62 OPTION_CHOICE o;
26d52442 63 int ret = 1, private = 0, verbose = 0, nbits;
d02b48c6 64
2c272447 65 opt_set_unknown_name("cipher");
7e1b7485
RS
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:
dd1abd44 85 e = setup_engine(opt_arg(), 0);
7e1b7485 86 break;
3ee1eac2
RS
87 case OPT_R_CASES:
88 if (!opt_rand(o))
89 goto end;
7e1b7485 90 break;
6bd4e3f2
P
91 case OPT_PROV_CASES:
92 if (!opt_provider(o))
93 goto end;
94 break;
7e1b7485 95 case OPT_CIPHER:
03bbd346 96 ciphername = opt_unknown();
0f113f3e 97 break;
b6a07f67
PP
98 case OPT_VERBOSE:
99 verbose = 1;
100 break;
0f113f3e 101 }
0f113f3e 102 }
021410ea
RS
103
104 /* One argument, the params file. */
d9f07357 105 if (!opt_check_rest_arg("params file"))
7e1b7485 106 goto opthelp;
d9f07357 107 argv = opt_rest();
03bbd346 108 dsaparams = argv[0];
021410ea 109
3ad60309
DDO
110 if (!app_RAND_load())
111 goto end;
112
d9f07357
DDO
113 if (!opt_cipher(ciphername, &enc))
114 goto end;
021410ea 115 private = 1;
d02b48c6 116
7e1b7485 117 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e
MC
118 BIO_printf(bio_err, "Error getting password\n");
119 goto end;
120 }
a3fe382e 121
d382e796 122 pkey = load_keyparams(dsaparams, FORMAT_UNDEF, 1, "DSA", "DSA parameters");
d02b48c6 123
bdd58d98 124 out = bio_open_owner(outfile, FORMAT_PEM, private);
0f113f3e 125 if (out == NULL)
7e1b7485 126 goto end2;
d02b48c6 127
ed576acd 128 nbits = EVP_PKEY_get_bits(pkey);
26d52442 129 if (nbits > OPENSSL_DSA_MAX_MODULUS_BITS)
0336df2f
GS
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",
ed576acd 133 OPENSSL_DSA_MAX_MODULUS_BITS, EVP_PKEY_get_bits(pkey));
0336df2f 134
dddbbc6f
P
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 }
a7e4ca5b 146 pkey = app_keygen(ctx, "DSA", nbits, verbose);
d02b48c6 147
3b061a00 148 assert(private);
dddbbc6f
P
149 if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
150 BIO_printf(bio_err, "unable to output generated key\n");
0f113f3e 151 goto end;
dddbbc6f 152 }
0f113f3e
MC
153 ret = 0;
154 end:
155 if (ret != 0)
156 ERR_print_errors(bio_err);
7e1b7485 157 end2:
ca3a82c3
RS
158 BIO_free(in);
159 BIO_free_all(out);
dddbbc6f
P
160 EVP_PKEY_free(pkey);
161 EVP_PKEY_CTX_free(ctx);
606a417f 162 EVP_CIPHER_free(enc);
dd1abd44 163 release_engine(e);
b548a1f1 164 OPENSSL_free(passout);
26a7d938 165 return ret;
0f113f3e 166}