xref: /openbsd-src/usr.bin/openssl/dsa.c (revision 15a0f5356ba433fa15c76c28e6592c9085f1ed98)
1 /* $OpenBSD: dsa.c,v 1.5 2015/09/11 14:30:23 bcook Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <openssl/opensslconf.h>	/* for OPENSSL_NO_DSA */
60 
61 #include <ctype.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <time.h>
65 #include <string.h>
66 
67 #include "apps.h"
68 
69 #include <openssl/bio.h>
70 #include <openssl/bn.h>
71 #include <openssl/dsa.h>
72 #include <openssl/err.h>
73 #include <openssl/evp.h>
74 #include <openssl/pem.h>
75 #include <openssl/x509.h>
76 
77 static struct {
78 	const EVP_CIPHER *enc;
79 	char *infile;
80 	int informat;
81 	int modulus;
82 	int noout;
83 	char *outfile;
84 	int outformat;
85 	char *passargin;
86 	char *passargout;
87 	int pubin;
88 	int pubout;
89 	int pvk_encr;
90 	int text;
91 } dsa_config;
92 
93 static int
94 dsa_opt_enc(int argc, char **argv, int *argsused)
95 {
96 	char *name = argv[0];
97 
98 	if (*name++ != '-')
99 		return (1);
100 
101 	if ((dsa_config.enc = EVP_get_cipherbyname(name)) != NULL) {
102 		*argsused = 1;
103 		return (0);
104 	}
105 
106 	return (1);
107 }
108 
109 static struct option dsa_options[] = {
110 	{
111 		.name = "in",
112 		.argname = "file",
113 		.desc = "Input file (default stdin)",
114 		.type = OPTION_ARG,
115 		.opt.arg = &dsa_config.infile,
116 	},
117 	{
118 		.name = "inform",
119 		.argname = "format",
120 		.desc = "Input format (PEM (default) or any other supported"
121 		    " format)",
122 		.type = OPTION_ARG_FORMAT,
123 		.opt.value = &dsa_config.informat,
124 	},
125 	{
126 		.name = "noout",
127 		.desc = "No output",
128 		.type = OPTION_FLAG,
129 		.opt.flag = &dsa_config.noout,
130 	},
131 	{
132 		.name = "out",
133 		.argname = "file",
134 		.desc = "Output file (default stdout)",
135 		.type = OPTION_ARG,
136 		.opt.arg = &dsa_config.outfile,
137 	},
138 	{
139 		.name = "outform",
140 		.argname = "format",
141 		.desc = "Output format (DER, MSBLOB, PEM (default) or PVK)",
142 		.type = OPTION_ARG_FORMAT,
143 		.opt.value = &dsa_config.outformat,
144 	},
145 	{
146 		.name = "passin",
147 		.argname = "source",
148 		.desc = "Input file passphrase source",
149 		.type = OPTION_ARG,
150 		.opt.arg = &dsa_config.passargin,
151 	},
152 	{
153 		.name = "passout",
154 		.argname = "source",
155 		.desc = "Output file passphrase source",
156 		.type = OPTION_ARG,
157 		.opt.arg = &dsa_config.passargout,
158 	},
159 	{
160 		.name = "pubin",
161 		.desc = "Read a public key from the input file instead of"
162 		    " private key",
163 		.type = OPTION_FLAG,
164 		.opt.flag = &dsa_config.pubin,
165 	},
166 	{
167 		.name = "pubout",
168 		.desc = "Output a public key instead of private key",
169 		.type = OPTION_FLAG,
170 		.opt.flag = &dsa_config.pubout,
171 	},
172 	{
173 		.name = "pvk-none",
174 		.desc = "PVK encryption level",
175 		.type = OPTION_VALUE,
176 		.value = 0,
177 		.opt.value = &dsa_config.pvk_encr,
178 	},
179 	{
180 		.name = "pvk-strong",
181 		.desc = "PVK encryption level (default)",
182 		.type = OPTION_VALUE,
183 		.value = 2,
184 		.opt.value = &dsa_config.pvk_encr,
185 	},
186 	{
187 		.name = "pvk-weak",
188 		.desc = "PVK encryption level",
189 		.type = OPTION_VALUE,
190 		.value = 1,
191 		.opt.value = &dsa_config.pvk_encr,
192 	},
193 	{
194 		.name = "text",
195 		.desc = "Print the key in text form",
196 		.type = OPTION_FLAG,
197 		.opt.flag = &dsa_config.text,
198 	},
199 	{
200 		.name = NULL,
201 		.type = OPTION_ARGV_FUNC,
202 		.opt.argvfunc = dsa_opt_enc,
203 	},
204 	{ NULL },
205 };
206 
207 static void
208 show_ciphers(const OBJ_NAME *name, void *arg)
209 {
210 	static int n;
211 
212 	if (!islower((unsigned char)*name->name))
213 		return;
214 
215 	fprintf(stderr, " -%-24s%s", name->name, (++n % 3 ? "" : "\n"));
216 }
217 
218 static void
219 dsa_usage(void)
220 {
221 	fprintf(stderr,
222 	    "usage: dsa [-in file] [-inform format] [-noout]\n"
223 	    "    [-out file] [-outform format] [-passin src] [-passout src]\n"
224 	    "    [-pubin] [-pubout] [-pvk-none | -pvk-strong | -pvk-weak]\n"
225 	    "    [-text] [-ciphername]\n\n");
226 	options_usage(dsa_options);
227 	fprintf(stderr, "\n");
228 
229 	fprintf(stderr, "Valid ciphername values:\n\n");
230 	OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_ciphers, NULL);
231 	fprintf(stderr, "\n");
232 }
233 
234 int
235 dsa_main(int argc, char **argv)
236 {
237 	int ret = 1;
238 	DSA *dsa = NULL;
239 	int i;
240 	BIO *in = NULL, *out = NULL;
241 	char *passin = NULL, *passout = NULL;
242 
243 	memset(&dsa_config, 0, sizeof(dsa_config));
244 
245 	dsa_config.pvk_encr = 2;
246 	dsa_config.informat = FORMAT_PEM;
247 	dsa_config.outformat = FORMAT_PEM;
248 
249 	if (options_parse(argc, argv, dsa_options, NULL, NULL) != 0) {
250 		dsa_usage();
251 		goto end;
252 	}
253 
254 	if (!app_passwd(bio_err, dsa_config.passargin, dsa_config.passargout,
255 	    &passin, &passout)) {
256 		BIO_printf(bio_err, "Error getting passwords\n");
257 		goto end;
258 	}
259 
260 	in = BIO_new(BIO_s_file());
261 	out = BIO_new(BIO_s_file());
262 	if (in == NULL || out == NULL) {
263 		ERR_print_errors(bio_err);
264 		goto end;
265 	}
266 	if (dsa_config.infile == NULL)
267 		BIO_set_fp(in, stdin, BIO_NOCLOSE);
268 	else {
269 		if (BIO_read_filename(in, dsa_config.infile) <= 0) {
270 			perror(dsa_config.infile);
271 			goto end;
272 		}
273 	}
274 
275 	BIO_printf(bio_err, "read DSA key\n");
276 
277 	{
278 		EVP_PKEY *pkey;
279 
280 		if (dsa_config.pubin)
281 			pkey = load_pubkey(bio_err, dsa_config.infile,
282 			    dsa_config.informat, 1, passin, "Public Key");
283 		else
284 			pkey = load_key(bio_err, dsa_config.infile,
285 			    dsa_config.informat, 1, passin, "Private Key");
286 
287 		if (pkey) {
288 			dsa = EVP_PKEY_get1_DSA(pkey);
289 			EVP_PKEY_free(pkey);
290 		}
291 	}
292 	if (dsa == NULL) {
293 		BIO_printf(bio_err, "unable to load Key\n");
294 		ERR_print_errors(bio_err);
295 		goto end;
296 	}
297 	if (dsa_config.outfile == NULL) {
298 		BIO_set_fp(out, stdout, BIO_NOCLOSE);
299 	} else {
300 		if (BIO_write_filename(out, dsa_config.outfile) <= 0) {
301 			perror(dsa_config.outfile);
302 			goto end;
303 		}
304 	}
305 
306 	if (dsa_config.text) {
307 		if (!DSA_print(out, dsa, 0)) {
308 			perror(dsa_config.outfile);
309 			ERR_print_errors(bio_err);
310 			goto end;
311 		}
312 	}
313 	if (dsa_config.modulus) {
314 		fprintf(stdout, "Public Key=");
315 		BN_print(out, dsa->pub_key);
316 		fprintf(stdout, "\n");
317 	}
318 	if (dsa_config.noout)
319 		goto end;
320 	BIO_printf(bio_err, "writing DSA key\n");
321 	if (dsa_config.outformat == FORMAT_ASN1) {
322 		if (dsa_config.pubin || dsa_config.pubout)
323 			i = i2d_DSA_PUBKEY_bio(out, dsa);
324 		else
325 			i = i2d_DSAPrivateKey_bio(out, dsa);
326 	} else if (dsa_config.outformat == FORMAT_PEM) {
327 		if (dsa_config.pubin || dsa_config.pubout)
328 			i = PEM_write_bio_DSA_PUBKEY(out, dsa);
329 		else
330 			i = PEM_write_bio_DSAPrivateKey(out, dsa, dsa_config.enc,
331 			    NULL, 0, NULL, passout);
332 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
333 	} else if (dsa_config.outformat == FORMAT_MSBLOB ||
334 	    dsa_config.outformat == FORMAT_PVK) {
335 		EVP_PKEY *pk;
336 		pk = EVP_PKEY_new();
337 		EVP_PKEY_set1_DSA(pk, dsa);
338 		if (dsa_config.outformat == FORMAT_PVK)
339 			i = i2b_PVK_bio(out, pk, dsa_config.pvk_encr, 0,
340 			    passout);
341 		else if (dsa_config.pubin || dsa_config.pubout)
342 			i = i2b_PublicKey_bio(out, pk);
343 		else
344 			i = i2b_PrivateKey_bio(out, pk);
345 		EVP_PKEY_free(pk);
346 #endif
347 	} else {
348 		BIO_printf(bio_err, "bad output format specified for outfile\n");
349 		goto end;
350 	}
351 	if (i <= 0) {
352 		BIO_printf(bio_err, "unable to write private key\n");
353 		ERR_print_errors(bio_err);
354 	} else
355 		ret = 0;
356 end:
357 	BIO_free(in);
358 	if (out != NULL)
359 		BIO_free_all(out);
360 	if (dsa != NULL)
361 		DSA_free(dsa);
362 	free(passin);
363 	free(passout);
364 
365 	return (ret);
366 }
367