1 /* $OpenBSD: dsa.c,v 1.11 2018/02/07 05:47:55 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */
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>
78 const EVP_CIPHER
*enc
;
94 dsa_opt_enc(int argc
, char **argv
, int *argsused
)
101 if ((dsa_config
.enc
= EVP_get_cipherbyname(name
)) != NULL
) {
109 static struct option dsa_options
[] = {
113 .desc
= "Input file (default stdin)",
115 .opt
.arg
= &dsa_config
.infile
,
120 .desc
= "Input format (PEM (default) or any other supported"
122 .type
= OPTION_ARG_FORMAT
,
123 .opt
.value
= &dsa_config
.informat
,
129 .opt
.flag
= &dsa_config
.noout
,
134 .desc
= "Output file (default stdout)",
136 .opt
.arg
= &dsa_config
.outfile
,
141 .desc
= "Output format (DER, MSBLOB, PEM (default) or PVK)",
142 .type
= OPTION_ARG_FORMAT
,
143 .opt
.value
= &dsa_config
.outformat
,
148 .desc
= "Input file passphrase source",
150 .opt
.arg
= &dsa_config
.passargin
,
155 .desc
= "Output file passphrase source",
157 .opt
.arg
= &dsa_config
.passargout
,
161 .desc
= "Read a public key from the input file instead of"
164 .opt
.flag
= &dsa_config
.pubin
,
168 .desc
= "Output a public key instead of private key",
170 .opt
.flag
= &dsa_config
.pubout
,
174 .desc
= "PVK encryption level",
175 .type
= OPTION_VALUE
,
177 .opt
.value
= &dsa_config
.pvk_encr
,
180 .name
= "pvk-strong",
181 .desc
= "PVK encryption level (default)",
182 .type
= OPTION_VALUE
,
184 .opt
.value
= &dsa_config
.pvk_encr
,
188 .desc
= "PVK encryption level",
189 .type
= OPTION_VALUE
,
191 .opt
.value
= &dsa_config
.pvk_encr
,
195 .desc
= "Print the key in text form",
197 .opt
.flag
= &dsa_config
.text
,
201 .type
= OPTION_ARGV_FUNC
,
202 .opt
.argvfunc
= dsa_opt_enc
,
208 show_ciphers(const OBJ_NAME
*name
, void *arg
)
212 if (!islower((unsigned char)*name
->name
))
215 fprintf(stderr
, " -%-24s%s", name
->name
, (++n
% 3 ? "" : "\n"));
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");
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");
235 dsa_main(int argc
, char **argv
)
240 BIO
*in
= NULL
, *out
= NULL
;
241 char *passin
= NULL
, *passout
= NULL
;
243 if (single_execution
) {
244 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
250 memset(&dsa_config
, 0, sizeof(dsa_config
));
252 dsa_config
.pvk_encr
= 2;
253 dsa_config
.informat
= FORMAT_PEM
;
254 dsa_config
.outformat
= FORMAT_PEM
;
256 if (options_parse(argc
, argv
, dsa_options
, NULL
, NULL
) != 0) {
261 if (!app_passwd(bio_err
, dsa_config
.passargin
, dsa_config
.passargout
,
262 &passin
, &passout
)) {
263 BIO_printf(bio_err
, "Error getting passwords\n");
267 in
= BIO_new(BIO_s_file());
268 out
= BIO_new(BIO_s_file());
269 if (in
== NULL
|| out
== NULL
) {
270 ERR_print_errors(bio_err
);
273 if (dsa_config
.infile
== NULL
)
274 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
276 if (BIO_read_filename(in
, dsa_config
.infile
) <= 0) {
277 perror(dsa_config
.infile
);
282 BIO_printf(bio_err
, "read DSA key\n");
287 if (dsa_config
.pubin
)
288 pkey
= load_pubkey(bio_err
, dsa_config
.infile
,
289 dsa_config
.informat
, 1, passin
, "Public Key");
291 pkey
= load_key(bio_err
, dsa_config
.infile
,
292 dsa_config
.informat
, 1, passin
, "Private Key");
295 dsa
= EVP_PKEY_get1_DSA(pkey
);
300 BIO_printf(bio_err
, "unable to load Key\n");
301 ERR_print_errors(bio_err
);
304 if (dsa_config
.outfile
== NULL
) {
305 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
307 if (BIO_write_filename(out
, dsa_config
.outfile
) <= 0) {
308 perror(dsa_config
.outfile
);
313 if (dsa_config
.text
) {
314 if (!DSA_print(out
, dsa
, 0)) {
315 perror(dsa_config
.outfile
);
316 ERR_print_errors(bio_err
);
320 if (dsa_config
.modulus
) {
321 fprintf(stdout
, "Public Key=");
322 BN_print(out
, dsa
->pub_key
);
323 fprintf(stdout
, "\n");
325 if (dsa_config
.noout
)
327 BIO_printf(bio_err
, "writing DSA key\n");
328 if (dsa_config
.outformat
== FORMAT_ASN1
) {
329 if (dsa_config
.pubin
|| dsa_config
.pubout
)
330 i
= i2d_DSA_PUBKEY_bio(out
, dsa
);
332 i
= i2d_DSAPrivateKey_bio(out
, dsa
);
333 } else if (dsa_config
.outformat
== FORMAT_PEM
) {
334 if (dsa_config
.pubin
|| dsa_config
.pubout
)
335 i
= PEM_write_bio_DSA_PUBKEY(out
, dsa
);
337 i
= PEM_write_bio_DSAPrivateKey(out
, dsa
, dsa_config
.enc
,
338 NULL
, 0, NULL
, passout
);
339 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
340 } else if (dsa_config
.outformat
== FORMAT_MSBLOB
||
341 dsa_config
.outformat
== FORMAT_PVK
) {
344 EVP_PKEY_set1_DSA(pk
, dsa
);
345 if (dsa_config
.outformat
== FORMAT_PVK
)
346 i
= i2b_PVK_bio(out
, pk
, dsa_config
.pvk_encr
, 0,
348 else if (dsa_config
.pubin
|| dsa_config
.pubout
)
349 i
= i2b_PublicKey_bio(out
, pk
);
351 i
= i2b_PrivateKey_bio(out
, pk
);
355 BIO_printf(bio_err
, "bad output format specified for outfile\n");
359 BIO_printf(bio_err
, "unable to write private key\n");
360 ERR_print_errors(bio_err
);