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