Annotation of src/usr.bin/openssl/dsa.c, Revision 1.12
1.12 ! inoguchi 1: /* $OpenBSD: dsa.c,v 1.11 2018/02/07 05:47:55 jsing Exp $ */
1.1 jsing 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 <stdio.h>
62: #include <stdlib.h>
63: #include <time.h>
64: #include <string.h>
65:
66: #include "apps.h"
67:
68: #include <openssl/bio.h>
69: #include <openssl/bn.h>
70: #include <openssl/dsa.h>
71: #include <openssl/err.h>
72: #include <openssl/evp.h>
73: #include <openssl/pem.h>
74: #include <openssl/x509.h>
75:
1.3 doug 76: static struct {
77: const EVP_CIPHER *enc;
78: char *infile;
79: int informat;
80: int modulus;
81: int noout;
82: char *outfile;
83: int outformat;
84: char *passargin;
85: char *passargout;
86: int pubin;
87: int pubout;
88: int pvk_encr;
89: int text;
90: } dsa_config;
91:
92: static int
93: dsa_opt_enc(int argc, char **argv, int *argsused)
94: {
95: char *name = argv[0];
96:
97: if (*name++ != '-')
98: return (1);
99:
100: if ((dsa_config.enc = EVP_get_cipherbyname(name)) != NULL) {
101: *argsused = 1;
102: return (0);
103: }
104:
105: return (1);
106: }
107:
108: static struct option dsa_options[] = {
109: {
110: .name = "in",
111: .argname = "file",
112: .desc = "Input file (default stdin)",
113: .type = OPTION_ARG,
114: .opt.arg = &dsa_config.infile,
115: },
116: {
117: .name = "inform",
118: .argname = "format",
119: .desc = "Input format (PEM (default) or any other supported"
120: " format)",
121: .type = OPTION_ARG_FORMAT,
122: .opt.value = &dsa_config.informat,
123: },
124: {
125: .name = "noout",
126: .desc = "No output",
127: .type = OPTION_FLAG,
128: .opt.flag = &dsa_config.noout,
129: },
130: {
131: .name = "out",
132: .argname = "file",
133: .desc = "Output file (default stdout)",
134: .type = OPTION_ARG,
135: .opt.arg = &dsa_config.outfile,
136: },
137: {
138: .name = "outform",
139: .argname = "format",
140: .desc = "Output format (DER, MSBLOB, PEM (default) or PVK)",
141: .type = OPTION_ARG_FORMAT,
142: .opt.value = &dsa_config.outformat,
143: },
144: {
145: .name = "passin",
146: .argname = "source",
147: .desc = "Input file passphrase source",
148: .type = OPTION_ARG,
149: .opt.arg = &dsa_config.passargin,
150: },
151: {
152: .name = "passout",
153: .argname = "source",
154: .desc = "Output file passphrase source",
155: .type = OPTION_ARG,
156: .opt.arg = &dsa_config.passargout,
157: },
158: {
159: .name = "pubin",
160: .desc = "Read a public key from the input file instead of"
161: " private key",
162: .type = OPTION_FLAG,
163: .opt.flag = &dsa_config.pubin,
164: },
165: {
166: .name = "pubout",
167: .desc = "Output a public key instead of private key",
168: .type = OPTION_FLAG,
169: .opt.flag = &dsa_config.pubout,
170: },
171: {
172: .name = "pvk-none",
173: .desc = "PVK encryption level",
174: .type = OPTION_VALUE,
175: .value = 0,
176: .opt.value = &dsa_config.pvk_encr,
177: },
178: {
179: .name = "pvk-strong",
180: .desc = "PVK encryption level (default)",
181: .type = OPTION_VALUE,
182: .value = 2,
183: .opt.value = &dsa_config.pvk_encr,
184: },
185: {
186: .name = "pvk-weak",
187: .desc = "PVK encryption level",
188: .type = OPTION_VALUE,
189: .value = 1,
190: .opt.value = &dsa_config.pvk_encr,
191: },
192: {
193: .name = "text",
194: .desc = "Print the key in text form",
195: .type = OPTION_FLAG,
196: .opt.flag = &dsa_config.text,
197: },
198: {
199: .name = NULL,
200: .type = OPTION_ARGV_FUNC,
201: .opt.argvfunc = dsa_opt_enc,
202: },
203: { NULL },
204: };
205:
206: static void
207: dsa_usage(void)
208: {
209: fprintf(stderr,
1.5 bcook 210: "usage: dsa [-in file] [-inform format] [-noout]\n"
1.3 doug 211: " [-out file] [-outform format] [-passin src] [-passout src]\n"
212: " [-pubin] [-pubout] [-pvk-none | -pvk-strong | -pvk-weak]\n"
213: " [-text] [-ciphername]\n\n");
214: options_usage(dsa_options);
215: fprintf(stderr, "\n");
216:
217: fprintf(stderr, "Valid ciphername values:\n\n");
1.12 ! inoguchi 218: OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, show_cipher, NULL);
1.3 doug 219: fprintf(stderr, "\n");
220: }
1.1 jsing 221:
222: int
223: dsa_main(int argc, char **argv)
224: {
225: int ret = 1;
226: DSA *dsa = NULL;
1.3 doug 227: int i;
1.1 jsing 228: BIO *in = NULL, *out = NULL;
229: char *passin = NULL, *passout = NULL;
1.6 doug 230:
231: if (single_execution) {
1.9 deraadt 232: if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
1.6 doug 233: perror("pledge");
1.8 doug 234: exit(1);
235: }
1.6 doug 236: }
1.1 jsing 237:
1.3 doug 238: memset(&dsa_config, 0, sizeof(dsa_config));
1.1 jsing 239:
1.3 doug 240: dsa_config.pvk_encr = 2;
241: dsa_config.informat = FORMAT_PEM;
242: dsa_config.outformat = FORMAT_PEM;
1.1 jsing 243:
1.3 doug 244: if (options_parse(argc, argv, dsa_options, NULL, NULL) != 0) {
245: dsa_usage();
1.1 jsing 246: goto end;
247: }
248:
1.3 doug 249: if (!app_passwd(bio_err, dsa_config.passargin, dsa_config.passargout,
250: &passin, &passout)) {
1.1 jsing 251: BIO_printf(bio_err, "Error getting passwords\n");
252: goto end;
253: }
1.3 doug 254:
1.1 jsing 255: in = BIO_new(BIO_s_file());
256: out = BIO_new(BIO_s_file());
1.3 doug 257: if (in == NULL || out == NULL) {
1.1 jsing 258: ERR_print_errors(bio_err);
259: goto end;
260: }
1.3 doug 261: if (dsa_config.infile == NULL)
1.1 jsing 262: BIO_set_fp(in, stdin, BIO_NOCLOSE);
263: else {
1.3 doug 264: if (BIO_read_filename(in, dsa_config.infile) <= 0) {
265: perror(dsa_config.infile);
1.1 jsing 266: goto end;
267: }
268: }
269:
270: BIO_printf(bio_err, "read DSA key\n");
271:
272: {
273: EVP_PKEY *pkey;
274:
1.3 doug 275: if (dsa_config.pubin)
276: pkey = load_pubkey(bio_err, dsa_config.infile,
1.5 bcook 277: dsa_config.informat, 1, passin, "Public Key");
1.1 jsing 278: else
1.3 doug 279: pkey = load_key(bio_err, dsa_config.infile,
1.5 bcook 280: dsa_config.informat, 1, passin, "Private Key");
1.1 jsing 281:
282: if (pkey) {
283: dsa = EVP_PKEY_get1_DSA(pkey);
284: EVP_PKEY_free(pkey);
285: }
286: }
287: if (dsa == NULL) {
288: BIO_printf(bio_err, "unable to load Key\n");
289: ERR_print_errors(bio_err);
290: goto end;
291: }
1.3 doug 292: if (dsa_config.outfile == NULL) {
1.1 jsing 293: BIO_set_fp(out, stdout, BIO_NOCLOSE);
294: } else {
1.3 doug 295: if (BIO_write_filename(out, dsa_config.outfile) <= 0) {
296: perror(dsa_config.outfile);
1.1 jsing 297: goto end;
298: }
299: }
300:
1.3 doug 301: if (dsa_config.text) {
1.1 jsing 302: if (!DSA_print(out, dsa, 0)) {
1.3 doug 303: perror(dsa_config.outfile);
1.1 jsing 304: ERR_print_errors(bio_err);
305: goto end;
306: }
307: }
1.3 doug 308: if (dsa_config.modulus) {
1.1 jsing 309: fprintf(stdout, "Public Key=");
310: BN_print(out, dsa->pub_key);
311: fprintf(stdout, "\n");
312: }
1.3 doug 313: if (dsa_config.noout)
1.1 jsing 314: goto end;
315: BIO_printf(bio_err, "writing DSA key\n");
1.3 doug 316: if (dsa_config.outformat == FORMAT_ASN1) {
317: if (dsa_config.pubin || dsa_config.pubout)
1.1 jsing 318: i = i2d_DSA_PUBKEY_bio(out, dsa);
319: else
320: i = i2d_DSAPrivateKey_bio(out, dsa);
1.3 doug 321: } else if (dsa_config.outformat == FORMAT_PEM) {
322: if (dsa_config.pubin || dsa_config.pubout)
1.1 jsing 323: i = PEM_write_bio_DSA_PUBKEY(out, dsa);
324: else
1.3 doug 325: i = PEM_write_bio_DSAPrivateKey(out, dsa, dsa_config.enc,
1.1 jsing 326: NULL, 0, NULL, passout);
327: #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
1.3 doug 328: } else if (dsa_config.outformat == FORMAT_MSBLOB ||
329: dsa_config.outformat == FORMAT_PVK) {
1.1 jsing 330: EVP_PKEY *pk;
331: pk = EVP_PKEY_new();
332: EVP_PKEY_set1_DSA(pk, dsa);
1.3 doug 333: if (dsa_config.outformat == FORMAT_PVK)
334: i = i2b_PVK_bio(out, pk, dsa_config.pvk_encr, 0,
335: passout);
336: else if (dsa_config.pubin || dsa_config.pubout)
1.1 jsing 337: i = i2b_PublicKey_bio(out, pk);
338: else
339: i = i2b_PrivateKey_bio(out, pk);
340: EVP_PKEY_free(pk);
341: #endif
342: } else {
343: BIO_printf(bio_err, "bad output format specified for outfile\n");
344: goto end;
345: }
346: if (i <= 0) {
347: BIO_printf(bio_err, "unable to write private key\n");
348: ERR_print_errors(bio_err);
349: } else
350: ret = 0;
1.11 jsing 351: end:
1.1 jsing 352: BIO_free(in);
1.10 jsing 353: BIO_free_all(out);
354: DSA_free(dsa);
1.1 jsing 355: free(passin);
356: free(passout);
357:
358: return (ret);
359: }