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