summaryrefslogtreecommitdiff
path: root/src/usr.bin/openssl/gendsa.c
diff options
context:
space:
mode:
authorinoguchi <>2019-06-07 02:32:22 +0000
committerinoguchi <>2019-06-07 02:32:22 +0000
commite8b669f2076c79bee41d4c83b46469fece481ee8 (patch)
tree81d1d440f27ca77fcc9d4b9d7f0c5c18ffe9afb0 /src/usr.bin/openssl/gendsa.c
parentcc3b0029b71b5fced0a6c6099d13f1d7df3401be (diff)
downloadopenbsd-e8b669f2076c79bee41d4c83b46469fece481ee8.tar.gz
openbsd-e8b669f2076c79bee41d4c83b46469fece481ee8.tar.bz2
openbsd-e8b669f2076c79bee41d4c83b46469fece481ee8.zip
Convert openssl(1) gendsa to the newer style of option handling
- Adapt openssl(1) gendsa command to new option handling. - Add lacking ciphers and passout description in openssl.1 manpage. - Describe paramfile as argument in openssl.1 manpage. ok bcook@
Diffstat (limited to 'src/usr.bin/openssl/gendsa.c')
-rw-r--r--src/usr.bin/openssl/gendsa.c241
1 files changed, 164 insertions, 77 deletions
diff --git a/src/usr.bin/openssl/gendsa.c b/src/usr.bin/openssl/gendsa.c
index 3197e7be7c..f2e155128c 100644
--- a/src/usr.bin/openssl/gendsa.c
+++ b/src/usr.bin/openssl/gendsa.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gendsa.c,v 1.10 2018/02/07 05:47:55 jsing Exp $ */ 1/* $OpenBSD: gendsa.c,v 1.11 2019/06/07 02:32:22 inoguchi Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -74,16 +74,114 @@
74#include <openssl/pem.h> 74#include <openssl/pem.h>
75#include <openssl/x509.h> 75#include <openssl/x509.h>
76 76
77static int set_enc(int argc, char **argv, int *argsused);
78static const EVP_CIPHER *get_cipher_by_name(char *name);
79
80static struct {
81 const EVP_CIPHER *enc;
82 char *outfile;
83 char *passargout;
84} gendsa_config;
85
86static struct option gendsa_options[] = {
87#ifndef OPENSSL_NO_AES
88 {
89 .name = "aes128",
90 .desc = "Encrypt PEM output with cbc aes",
91 .type = OPTION_ARGV_FUNC,
92 .opt.argvfunc = set_enc,
93 },
94 {
95 .name = "aes192",
96 .desc = "Encrypt PEM output with cbc aes",
97 .type = OPTION_ARGV_FUNC,
98 .opt.argvfunc = set_enc,
99 },
100 {
101 .name = "aes256",
102 .desc = "Encrypt PEM output with cbc aes",
103 .type = OPTION_ARGV_FUNC,
104 .opt.argvfunc = set_enc,
105 },
106#endif
107#ifndef OPENSSL_NO_CAMELLIA
108 {
109 .name = "camellia128",
110 .desc = "Encrypt PEM output with cbc camellia",
111 .type = OPTION_ARGV_FUNC,
112 .opt.argvfunc = set_enc,
113 },
114 {
115 .name = "camellia192",
116 .desc = "Encrypt PEM output with cbc camellia",
117 .type = OPTION_ARGV_FUNC,
118 .opt.argvfunc = set_enc,
119 },
120 {
121 .name = "camellia256",
122 .desc = "Encrypt PEM output with cbc camellia",
123 .type = OPTION_ARGV_FUNC,
124 .opt.argvfunc = set_enc,
125 },
126#endif
127#ifndef OPENSSL_NO_DES
128 {
129 .name = "des",
130 .desc = "Encrypt the generated key with DES in cbc mode",
131 .type = OPTION_ARGV_FUNC,
132 .opt.argvfunc = set_enc,
133 },
134 {
135 .name = "des3",
136 .desc = "Encrypt the generated key with DES in ede cbc mode (168 bit key)",
137 .type = OPTION_ARGV_FUNC,
138 .opt.argvfunc = set_enc,
139 },
140#endif
141#ifndef OPENSSL_NO_IDEA
142 {
143 .name = "idea",
144 .desc = "Encrypt the generated key with IDEA in cbc mode",
145 .type = OPTION_ARGV_FUNC,
146 .opt.argvfunc = set_enc,
147 },
148#endif
149 {
150 .name = "out",
151 .argname = "file",
152 .desc = "Output the key to 'file'",
153 .type = OPTION_ARG,
154 .opt.arg = &gendsa_config.outfile,
155 },
156 {
157 .name = "passout",
158 .argname = "src",
159 .desc = "Output file passphrase source",
160 .type = OPTION_ARG,
161 .opt.arg = &gendsa_config.passargout,
162 },
163 { NULL },
164};
165
166static void
167gendsa_usage(void)
168{
169 fprintf(stderr, "usage: gendsa [-aes128 | -aes192 | -aes256 |\n");
170 fprintf(stderr, " -camellia128 | -camellia192 | -camellia256 |\n");
171 fprintf(stderr, " -des | -des3 | -idea] [-out file] [-passout src]");
172 fprintf(stderr, " paramfile\n\n");
173 options_usage(gendsa_options);
174 fprintf(stderr, "\n");
175}
176
77int 177int
78gendsa_main(int argc, char **argv) 178gendsa_main(int argc, char **argv)
79{ 179{
80 DSA *dsa = NULL; 180 DSA *dsa = NULL;
81 int ret = 1; 181 int ret = 1;
82 char *outfile = NULL;
83 char *dsaparams = NULL; 182 char *dsaparams = NULL;
84 char *passargout = NULL, *passout = NULL; 183 char *passout = NULL;
85 BIO *out = NULL, *in = NULL; 184 BIO *out = NULL, *in = NULL;
86 const EVP_CIPHER *enc = NULL;
87 185
88 if (single_execution) { 186 if (single_execution) {
89 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) { 187 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
@@ -92,80 +190,19 @@ gendsa_main(int argc, char **argv)
92 } 190 }
93 } 191 }
94 192
95 argv++; 193 memset(&gendsa_config, 0, sizeof(gendsa_config));
96 argc--; 194
97 for (;;) { 195 if (options_parse(argc, argv, gendsa_options, &dsaparams, NULL) != 0) {
98 if (argc <= 0) 196 gendsa_usage();
99 break; 197 goto end;
100 if (strcmp(*argv, "-out") == 0) {
101 if (--argc < 1)
102 goto bad;
103 outfile = *(++argv);
104 } else if (strcmp(*argv, "-passout") == 0) {
105 if (--argc < 1)
106 goto bad;
107 passargout = *(++argv);
108 }
109 else if (strcmp(*argv, "-") == 0)
110 goto bad;
111#ifndef OPENSSL_NO_DES
112 else if (strcmp(*argv, "-des") == 0)
113 enc = EVP_des_cbc();
114 else if (strcmp(*argv, "-des3") == 0)
115 enc = EVP_des_ede3_cbc();
116#endif
117#ifndef OPENSSL_NO_IDEA
118 else if (strcmp(*argv, "-idea") == 0)
119 enc = EVP_idea_cbc();
120#endif
121#ifndef OPENSSL_NO_AES
122 else if (strcmp(*argv, "-aes128") == 0)
123 enc = EVP_aes_128_cbc();
124 else if (strcmp(*argv, "-aes192") == 0)
125 enc = EVP_aes_192_cbc();
126 else if (strcmp(*argv, "-aes256") == 0)
127 enc = EVP_aes_256_cbc();
128#endif
129#ifndef OPENSSL_NO_CAMELLIA
130 else if (strcmp(*argv, "-camellia128") == 0)
131 enc = EVP_camellia_128_cbc();
132 else if (strcmp(*argv, "-camellia192") == 0)
133 enc = EVP_camellia_192_cbc();
134 else if (strcmp(*argv, "-camellia256") == 0)
135 enc = EVP_camellia_256_cbc();
136#endif
137 else if (**argv != '-' && dsaparams == NULL) {
138 dsaparams = *argv;
139 } else
140 goto bad;
141 argv++;
142 argc--;
143 } 198 }
144 199
145 if (dsaparams == NULL) { 200 if (dsaparams == NULL) {
146 bad: 201 gendsa_usage();
147 BIO_printf(bio_err, "usage: gendsa [args] dsaparam-file\n");
148 BIO_printf(bio_err, " -out file - output the key to 'file'\n");
149#ifndef OPENSSL_NO_DES
150 BIO_printf(bio_err, " -des - encrypt the generated key with DES in cbc mode\n");
151 BIO_printf(bio_err, " -des3 - encrypt the generated key with DES in ede cbc mode (168 bit key)\n");
152#endif
153#ifndef OPENSSL_NO_IDEA
154 BIO_printf(bio_err, " -idea - encrypt the generated key with IDEA in cbc mode\n");
155#endif
156#ifndef OPENSSL_NO_AES
157 BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
158 BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
159#endif
160#ifndef OPENSSL_NO_CAMELLIA
161 BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
162 BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n");
163#endif
164 BIO_printf(bio_err, " dsaparam-file\n");
165 BIO_printf(bio_err, " - a DSA parameter file as generated by the dsaparam command\n");
166 goto end; 202 goto end;
167 } 203 }
168 if (!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { 204 if (!app_passwd(bio_err, NULL, gendsa_config.passargout, NULL,
205 &passout)) {
169 BIO_printf(bio_err, "Error getting password\n"); 206 BIO_printf(bio_err, "Error getting password\n");
170 goto end; 207 goto end;
171 } 208 }
@@ -185,11 +222,11 @@ gendsa_main(int argc, char **argv)
185 if (out == NULL) 222 if (out == NULL)
186 goto end; 223 goto end;
187 224
188 if (outfile == NULL) { 225 if (gendsa_config.outfile == NULL) {
189 BIO_set_fp(out, stdout, BIO_NOCLOSE); 226 BIO_set_fp(out, stdout, BIO_NOCLOSE);
190 } else { 227 } else {
191 if (BIO_write_filename(out, outfile) <= 0) { 228 if (BIO_write_filename(out, gendsa_config.outfile) <= 0) {
192 perror(outfile); 229 perror(gendsa_config.outfile);
193 goto end; 230 goto end;
194 } 231 }
195 } 232 }
@@ -199,7 +236,8 @@ gendsa_main(int argc, char **argv)
199 if (!DSA_generate_key(dsa)) 236 if (!DSA_generate_key(dsa))
200 goto end; 237 goto end;
201 238
202 if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout)) 239 if (!PEM_write_bio_DSAPrivateKey(out, dsa, gendsa_config.enc, NULL, 0,
240 NULL, passout))
203 goto end; 241 goto end;
204 ret = 0; 242 ret = 0;
205 end: 243 end:
@@ -212,3 +250,52 @@ gendsa_main(int argc, char **argv)
212 250
213 return (ret); 251 return (ret);
214} 252}
253
254static int
255set_enc(int argc, char **argv, int *argsused)
256{
257 char *name = argv[0];
258
259 if (*name++ != '-')
260 return (1);
261
262 if ((gendsa_config.enc = get_cipher_by_name(name)) == NULL)
263 return (1);
264
265 *argsused = 1;
266 return (0);
267}
268
269static const EVP_CIPHER *get_cipher_by_name(char *name)
270{
271 if (name == NULL || strcmp(name, "") == 0)
272 return (NULL);
273#ifndef OPENSSL_NO_AES
274 else if (strcmp(name, "aes128") == 0)
275 return EVP_aes_128_cbc();
276 else if (strcmp(name, "aes192") == 0)
277 return EVP_aes_192_cbc();
278 else if (strcmp(name, "aes256") == 0)
279 return EVP_aes_256_cbc();
280#endif
281#ifndef OPENSSL_NO_CAMELLIA
282 else if (strcmp(name, "camellia128") == 0)
283 return EVP_camellia_128_cbc();
284 else if (strcmp(name, "camellia192") == 0)
285 return EVP_camellia_192_cbc();
286 else if (strcmp(name, "camellia256") == 0)
287 return EVP_camellia_256_cbc();
288#endif
289#ifndef OPENSSL_NO_DES
290 else if (strcmp(name, "des") == 0)
291 return EVP_des_cbc();
292 else if (strcmp(name, "des3") == 0)
293 return EVP_des_ede3_cbc();
294#endif
295#ifndef OPENSSL_NO_IDEA
296 else if (strcmp(name, "idea") == 0)
297 return EVP_idea_cbc();
298#endif
299 else
300 return (NULL);
301}