]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/gendsa.c
Check non-option arguments
[thirdparty/openssl.git] / apps / gendsa.c
CommitLineData
846e33c7 1/*
33388b44 2 * Copyright 1995-2020 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
RS
25typedef enum OPTION_choice {
26 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
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;
7e1b7485 57 DSA *dsa = NULL;
dddbbc6f
P
58 EVP_PKEY *pkey = NULL;
59 EVP_PKEY_CTX *ctx = NULL;
0f113f3e 60 const EVP_CIPHER *enc = NULL;
3ee1eac2 61 char *dsaparams = NULL;
7e1b7485
RS
62 char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
63 OPTION_CHOICE o;
b6a07f67 64 int ret = 1, private = 0, verbose = 0;
2ac6115d 65 const BIGNUM *p = NULL;
d02b48c6 66
7e1b7485
RS
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:
dd1abd44 86 e = setup_engine(opt_arg(), 0);
7e1b7485 87 break;
3ee1eac2
RS
88 case OPT_R_CASES:
89 if (!opt_rand(o))
90 goto end;
7e1b7485 91 break;
6bd4e3f2
P
92 case OPT_PROV_CASES:
93 if (!opt_provider(o))
94 goto end;
95 break;
7e1b7485
RS
96 case OPT_CIPHER:
97 if (!opt_cipher(opt_unknown(), &enc))
98 goto end;
0f113f3e 99 break;
b6a07f67
PP
100 case OPT_VERBOSE:
101 verbose = 1;
102 break;
0f113f3e 103 }
0f113f3e 104 }
021410ea
RS
105
106 /* One argument, the params file. */
7e1b7485
RS
107 argc = opt_num_rest();
108 argv = opt_rest();
7e1b7485
RS
109 if (argc != 1)
110 goto opthelp;
021410ea
RS
111
112 dsaparams = argv[0];
113 private = 1;
d02b48c6 114
7e1b7485 115 if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
0f113f3e
MC
116 BIO_printf(bio_err, "Error getting password\n");
117 goto end;
118 }
a3fe382e 119
bdd58d98 120 in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
7e1b7485
RS
121 if (in == NULL)
122 goto end2;
a3fe382e 123
0f113f3e
MC
124 if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
125 BIO_printf(bio_err, "unable to load DSA parameter file\n");
126 goto end;
127 }
128 BIO_free(in);
129 in = NULL;
d02b48c6 130
bdd58d98 131 out = bio_open_owner(outfile, FORMAT_PEM, private);
0f113f3e 132 if (out == NULL)
7e1b7485 133 goto end2;
d02b48c6 134
6e9fa57c 135 DSA_get0_pqg(dsa, &p, NULL, NULL);
0336df2f
GS
136
137 if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
138 BIO_printf(bio_err,
139 "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
140 " Your key size is %d! Larger key size may behave not as expected.\n",
141 OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
142
dddbbc6f
P
143 pkey = EVP_PKEY_new();
144 if (pkey == NULL) {
145 BIO_printf(bio_err, "unable to allocate PKEY\n");
146 goto end;
147 }
148 if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
149 BIO_printf(bio_err, "unable to associate DSA parameters with PKEY\n");
150 goto end;
151 }
152 ctx = EVP_PKEY_CTX_new(pkey, NULL);
153 if (ctx == NULL) {
154 BIO_printf(bio_err, "unable to create PKEY context\n");
155 goto end;
156 }
157 EVP_PKEY_free(pkey);
158 pkey = NULL;
159 if (EVP_PKEY_keygen_init(ctx) <= 0) {
160 BIO_printf(bio_err, "unable to set up for key generation\n");
161 goto end;
162 }
b6a07f67
PP
163 if (verbose)
164 BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
dddbbc6f
P
165 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
166 BIO_printf(bio_err, "unable to generate key\n");
0f113f3e 167 goto end;
dddbbc6f 168 }
d02b48c6 169
3b061a00 170 assert(private);
dddbbc6f
P
171 if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
172 BIO_printf(bio_err, "unable to output generated key\n");
0f113f3e 173 goto end;
dddbbc6f 174 }
0f113f3e
MC
175 ret = 0;
176 end:
177 if (ret != 0)
178 ERR_print_errors(bio_err);
7e1b7485 179 end2:
ca3a82c3
RS
180 BIO_free(in);
181 BIO_free_all(out);
d6407083 182 DSA_free(dsa);
dddbbc6f
P
183 EVP_PKEY_free(pkey);
184 EVP_PKEY_CTX_free(ctx);
dd1abd44 185 release_engine(e);
b548a1f1 186 OPENSSL_free(passout);
26a7d938 187 return ret;
0f113f3e 188}