]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/dsa.c
cmdline app: add provider commandline options.
[thirdparty/openssl.git] / apps / dsa.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10 /* We need to use the deprecated DSA_print */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <openssl/opensslconf.h>
14 #ifdef OPENSSL_NO_DSA
15 NON_EMPTY_TRANSLATION_UNIT
16 #else
17
18 # include <stdio.h>
19 # include <stdlib.h>
20 # include <string.h>
21 # include <time.h>
22 # include "apps.h"
23 # include "progs.h"
24 # include <openssl/bio.h>
25 # include <openssl/err.h>
26 # include <openssl/dsa.h>
27 # include <openssl/evp.h>
28 # include <openssl/x509.h>
29 # include <openssl/pem.h>
30 # include <openssl/bn.h>
31
32 typedef enum OPTION_choice {
33 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
34 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENGINE,
35 /* Do not change the order here; see case statements below */
36 OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
37 OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_PUBIN,
38 OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT,
39 OPT_PROV_ENUM
40 } OPTION_CHOICE;
41
42 const OPTIONS dsa_options[] = {
43 OPT_SECTION("General"),
44 {"help", OPT_HELP, '-', "Display this summary"},
45 {"", OPT_CIPHER, '-', "Any supported cipher"},
46 # ifndef OPENSSL_NO_RC4
47 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
48 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
49 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
50 # endif
51 # ifndef OPENSSL_NO_ENGINE
52 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
53 # endif
54
55 OPT_SECTION("Input"),
56 {"in", OPT_IN, 's', "Input key"},
57 {"inform", OPT_INFORM, 'f', "Input format, DER PEM PVK"},
58 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
59 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
60
61 OPT_SECTION("Output"),
62 {"out", OPT_OUT, '>', "Output file"},
63 {"outform", OPT_OUTFORM, 'f', "Output format, DER PEM PVK"},
64 {"noout", OPT_NOOUT, '-', "Don't print key out"},
65 {"text", OPT_TEXT, '-', "Print the key in text"},
66 {"modulus", OPT_MODULUS, '-', "Print the DSA public value"},
67 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
68 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
69
70 OPT_PROV_OPTIONS,
71 {NULL}
72 };
73
74 int dsa_main(int argc, char **argv)
75 {
76 BIO *out = NULL;
77 DSA *dsa = NULL;
78 ENGINE *e = NULL;
79 const EVP_CIPHER *enc = NULL;
80 char *infile = NULL, *outfile = NULL, *prog;
81 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
82 OPTION_CHOICE o;
83 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
84 int i, modulus = 0, pubin = 0, pubout = 0, ret = 1;
85 # ifndef OPENSSL_NO_RC4
86 int pvk_encr = 2;
87 # endif
88 int private = 0;
89
90 prog = opt_init(argc, argv, dsa_options);
91 while ((o = opt_next()) != OPT_EOF) {
92 switch (o) {
93 case OPT_EOF:
94 case OPT_ERR:
95 opthelp:
96 ret = 0;
97 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
98 goto end;
99 case OPT_HELP:
100 opt_help(dsa_options);
101 ret = 0;
102 goto end;
103 case OPT_INFORM:
104 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
105 goto opthelp;
106 break;
107 case OPT_IN:
108 infile = opt_arg();
109 break;
110 case OPT_OUTFORM:
111 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
112 goto opthelp;
113 break;
114 case OPT_OUT:
115 outfile = opt_arg();
116 break;
117 case OPT_ENGINE:
118 e = setup_engine(opt_arg(), 0);
119 break;
120 case OPT_PASSIN:
121 passinarg = opt_arg();
122 break;
123 case OPT_PASSOUT:
124 passoutarg = opt_arg();
125 break;
126 case OPT_PVK_STRONG: /* pvk_encr:= 2 */
127 case OPT_PVK_WEAK: /* pvk_encr:= 1 */
128 case OPT_PVK_NONE: /* pvk_encr:= 0 */
129 #ifndef OPENSSL_NO_RC4
130 pvk_encr = (o - OPT_PVK_NONE);
131 #endif
132 break;
133 case OPT_NOOUT:
134 noout = 1;
135 break;
136 case OPT_TEXT:
137 text = 1;
138 break;
139 case OPT_MODULUS:
140 modulus = 1;
141 break;
142 case OPT_PUBIN:
143 pubin = 1;
144 break;
145 case OPT_PUBOUT:
146 pubout = 1;
147 break;
148 case OPT_CIPHER:
149 if (!opt_cipher(opt_unknown(), &enc))
150 goto end;
151 break;
152 case OPT_PROV_CASES:
153 if (!opt_provider(o))
154 goto end;
155 break;
156 }
157 }
158 argc = opt_num_rest();
159 if (argc != 0)
160 goto opthelp;
161
162 private = pubin || pubout ? 0 : 1;
163 if (text && !pubin)
164 private = 1;
165
166 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
167 BIO_printf(bio_err, "Error getting passwords\n");
168 goto end;
169 }
170
171 BIO_printf(bio_err, "read DSA key\n");
172 {
173 EVP_PKEY *pkey;
174
175 if (pubin)
176 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
177 else
178 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
179
180 if (pkey != NULL) {
181 dsa = EVP_PKEY_get1_DSA(pkey);
182 EVP_PKEY_free(pkey);
183 }
184 }
185
186 if (dsa == NULL) {
187 BIO_printf(bio_err, "unable to load Key\n");
188 ERR_print_errors(bio_err);
189 goto end;
190 }
191
192 out = bio_open_owner(outfile, outformat, private);
193 if (out == NULL)
194 goto end;
195
196 if (text) {
197 assert(pubin || private);
198 if (!DSA_print(out, dsa, 0)) {
199 perror(outfile);
200 ERR_print_errors(bio_err);
201 goto end;
202 }
203 }
204
205 if (modulus) {
206 const BIGNUM *pub_key = NULL;
207 DSA_get0_key(dsa, &pub_key, NULL);
208 BIO_printf(out, "Public Key=");
209 BN_print(out, pub_key);
210 BIO_printf(out, "\n");
211 }
212
213 if (noout) {
214 ret = 0;
215 goto end;
216 }
217 BIO_printf(bio_err, "writing DSA key\n");
218 if (outformat == FORMAT_ASN1) {
219 if (pubin || pubout) {
220 i = i2d_DSA_PUBKEY_bio(out, dsa);
221 } else {
222 assert(private);
223 i = i2d_DSAPrivateKey_bio(out, dsa);
224 }
225 } else if (outformat == FORMAT_PEM) {
226 if (pubin || pubout) {
227 i = PEM_write_bio_DSA_PUBKEY(out, dsa);
228 } else {
229 assert(private);
230 i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
231 NULL, 0, NULL, passout);
232 }
233 # ifndef OPENSSL_NO_RSA
234 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
235 EVP_PKEY *pk;
236 pk = EVP_PKEY_new();
237 if (pk == NULL)
238 goto end;
239
240 EVP_PKEY_set1_DSA(pk, dsa);
241 if (outformat == FORMAT_PVK) {
242 if (pubin) {
243 BIO_printf(bio_err, "PVK form impossible with public key input\n");
244 EVP_PKEY_free(pk);
245 goto end;
246 }
247 assert(private);
248 # ifdef OPENSSL_NO_RC4
249 BIO_printf(bio_err, "PVK format not supported\n");
250 EVP_PKEY_free(pk);
251 goto end;
252 # else
253 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
254 # endif
255 } else if (pubin || pubout) {
256 i = i2b_PublicKey_bio(out, pk);
257 } else {
258 assert(private);
259 i = i2b_PrivateKey_bio(out, pk);
260 }
261 EVP_PKEY_free(pk);
262 # endif
263 } else {
264 BIO_printf(bio_err, "bad output format specified for outfile\n");
265 goto end;
266 }
267 if (i <= 0) {
268 BIO_printf(bio_err, "unable to write private key\n");
269 ERR_print_errors(bio_err);
270 goto end;
271 }
272 ret = 0;
273 end:
274 BIO_free_all(out);
275 DSA_free(dsa);
276 release_engine(e);
277 OPENSSL_free(passin);
278 OPENSSL_free(passout);
279 return ret;
280 }
281 #endif