]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
3e84b6e1 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
3e84b6e1 | 8 | */ |
846e33c7 | 9 | |
3e84b6e1 DSH |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include "apps.h" | |
dab2cd68 | 13 | #include "progs.h" |
3e84b6e1 DSH |
14 | #include <openssl/pem.h> |
15 | #include <openssl/err.h> | |
16 | #include <openssl/evp.h> | |
17 | ||
92fee421 P |
18 | #ifndef OPENSSL_NO_EC |
19 | # include <openssl/ec.h> | |
20 | ||
21 | static OPT_PAIR ec_conv_forms[] = { | |
22 | {"compressed", POINT_CONVERSION_COMPRESSED}, | |
23 | {"uncompressed", POINT_CONVERSION_UNCOMPRESSED}, | |
24 | {"hybrid", POINT_CONVERSION_HYBRID}, | |
25 | {NULL} | |
26 | }; | |
27 | ||
28 | static OPT_PAIR ec_param_enc[] = { | |
29 | {"named_curve", OPENSSL_EC_NAMED_CURVE}, | |
30 | {"explicit", 0}, | |
31 | {NULL} | |
32 | }; | |
33 | #endif | |
34 | ||
7e1b7485 RS |
35 | typedef enum OPTION_choice { |
36 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
37 | OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE, | |
38 | OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB, | |
92fee421 | 39 | OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK, |
6bd4e3f2 P |
40 | OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM, |
41 | OPT_PROV_ENUM | |
7e1b7485 RS |
42 | } OPTION_CHOICE; |
43 | ||
44c83ebd | 44 | const OPTIONS pkey_options[] = { |
5388f986 | 45 | OPT_SECTION("General"), |
7e1b7485 | 46 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
47 | #ifndef OPENSSL_NO_ENGINE |
48 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
49 | #endif | |
50 | {"check", OPT_CHECK, '-', "Check key consistency"}, | |
51 | {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"}, | |
52 | {"", OPT_MD, '-', "Any supported cipher"}, | |
92fee421 P |
53 | {"ec_param_enc", OPT_EC_PARAM_ENC, 's', |
54 | "Specifies the way the ec parameters are encoded"}, | |
55 | {"ec_conv_form", OPT_EC_CONV_FORM, 's', | |
56 | "Specifies the point conversion form "}, | |
5388f986 RS |
57 | |
58 | OPT_SECTION("Input"), | |
59 | {"in", OPT_IN, 's', "Input key"}, | |
d18ba3cc | 60 | {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"}, |
7e1b7485 | 61 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
7e1b7485 RS |
62 | {"pubin", OPT_PUBIN, '-', |
63 | "Read public key from input (default is private key)"}, | |
5388f986 RS |
64 | {"traditional", OPT_TRADITIONAL, '-', |
65 | "Use traditional format for private keys"}, | |
66 | ||
67 | OPT_SECTION("Output"), | |
68 | {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"}, | |
69 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
70 | {"out", OPT_OUT, '>', "Output file"}, | |
7e1b7485 RS |
71 | {"pubout", OPT_PUBOUT, '-', "Output public key, not private"}, |
72 | {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"}, | |
73 | {"text", OPT_TEXT, '-', "Output in plaintext as well"}, | |
74 | {"noout", OPT_NOOUT, '-', "Don't output the key"}, | |
5388f986 | 75 | |
6bd4e3f2 | 76 | OPT_PROV_OPTIONS, |
7e1b7485 RS |
77 | {NULL} |
78 | }; | |
3e84b6e1 | 79 | |
7e1b7485 | 80 | int pkey_main(int argc, char **argv) |
0f113f3e | 81 | { |
0f113f3e | 82 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 83 | ENGINE *e = NULL; |
0f113f3e | 84 | EVP_PKEY *pkey = NULL; |
7e1b7485 RS |
85 | const EVP_CIPHER *cipher = NULL; |
86 | char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL; | |
333b070e | 87 | char *passinarg = NULL, *passoutarg = NULL, *prog; |
7e1b7485 RS |
88 | OPTION_CHOICE o; |
89 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; | |
90 | int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1; | |
b0004708 | 91 | int private = 0, traditional = 0, check = 0, pub_check = 0; |
92fee421 P |
92 | #ifndef OPENSSL_NO_EC |
93 | EC_KEY *eckey; | |
94 | int ec_asn1_flag = OPENSSL_EC_NAMED_CURVE, new_ec_asn1_flag = 0; | |
95 | int i, new_ec_form = 0; | |
96 | point_conversion_form_t ec_form = POINT_CONVERSION_UNCOMPRESSED; | |
97 | #endif | |
7e1b7485 RS |
98 | |
99 | prog = opt_init(argc, argv, pkey_options); | |
100 | while ((o = opt_next()) != OPT_EOF) { | |
101 | switch (o) { | |
102 | case OPT_EOF: | |
103 | case OPT_ERR: | |
104 | opthelp: | |
105 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
106 | goto end; | |
107 | case OPT_HELP: | |
108 | opt_help(pkey_options); | |
109 | ret = 0; | |
110 | goto end; | |
111 | case OPT_INFORM: | |
dd958974 | 112 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) |
7e1b7485 RS |
113 | goto opthelp; |
114 | break; | |
115 | case OPT_OUTFORM: | |
116 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
117 | goto opthelp; | |
118 | break; | |
119 | case OPT_PASSIN: | |
120 | passinarg = opt_arg(); | |
121 | break; | |
122 | case OPT_PASSOUT: | |
123 | passoutarg = opt_arg(); | |
124 | break; | |
125 | case OPT_ENGINE: | |
333b070e | 126 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
127 | break; |
128 | case OPT_IN: | |
129 | infile = opt_arg(); | |
130 | break; | |
131 | case OPT_OUT: | |
132 | outfile = opt_arg(); | |
133 | break; | |
134 | case OPT_PUBIN: | |
135 | pubin = pubout = pubtext = 1; | |
136 | break; | |
137 | case OPT_PUBOUT: | |
0f113f3e | 138 | pubout = 1; |
7e1b7485 RS |
139 | break; |
140 | case OPT_TEXT_PUB: | |
141 | pubtext = text = 1; | |
142 | break; | |
143 | case OPT_TEXT: | |
0f113f3e | 144 | text = 1; |
7e1b7485 RS |
145 | break; |
146 | case OPT_NOOUT: | |
0f113f3e | 147 | noout = 1; |
7e1b7485 | 148 | break; |
05dba815 DSH |
149 | case OPT_TRADITIONAL: |
150 | traditional = 1; | |
151 | break; | |
2aee35d3 PY |
152 | case OPT_CHECK: |
153 | check = 1; | |
154 | break; | |
b0004708 PY |
155 | case OPT_PUB_CHECK: |
156 | pub_check = 1; | |
157 | break; | |
7e1b7485 RS |
158 | case OPT_MD: |
159 | if (!opt_cipher(opt_unknown(), &cipher)) | |
160 | goto opthelp; | |
92fee421 P |
161 | break; |
162 | case OPT_EC_CONV_FORM: | |
163 | #ifdef OPENSSL_NO_EC | |
164 | goto opthelp; | |
165 | #else | |
166 | if (!opt_pair(opt_arg(), ec_conv_forms, &i)) | |
167 | goto opthelp; | |
168 | new_ec_form = 1; | |
169 | ec_form = i; | |
170 | break; | |
171 | #endif | |
172 | case OPT_EC_PARAM_ENC: | |
173 | #ifdef OPENSSL_NO_EC | |
174 | goto opthelp; | |
175 | #else | |
176 | if (!opt_pair(opt_arg(), ec_param_enc, &i)) | |
177 | goto opthelp; | |
178 | new_ec_asn1_flag = 1; | |
179 | ec_asn1_flag = i; | |
180 | break; | |
181 | #endif | |
6bd4e3f2 P |
182 | case OPT_PROV_CASES: |
183 | if (!opt_provider(o)) | |
184 | goto end; | |
185 | break; | |
0f113f3e | 186 | } |
0f113f3e | 187 | } |
7e1b7485 | 188 | argc = opt_num_rest(); |
03358517 KR |
189 | if (argc != 0) |
190 | goto opthelp; | |
191 | ||
3b061a00 RS |
192 | private = !noout && !pubout ? 1 : 0; |
193 | if (text && !pubtext) | |
194 | private = 1; | |
0f113f3e | 195 | |
7e1b7485 | 196 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
197 | BIO_printf(bio_err, "Error getting passwords\n"); |
198 | goto end; | |
199 | } | |
200 | ||
bdd58d98 | 201 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
202 | if (out == NULL) |
203 | goto end; | |
0f113f3e MC |
204 | |
205 | if (pubin) | |
7e1b7485 | 206 | pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key"); |
0f113f3e | 207 | else |
7e1b7485 | 208 | pkey = load_key(infile, informat, 1, passin, e, "key"); |
2234212c | 209 | if (pkey == NULL) |
0f113f3e MC |
210 | goto end; |
211 | ||
92fee421 P |
212 | #ifndef OPENSSL_NO_EC |
213 | /* | |
214 | * TODO: remove this and use a set params call with a 'pkeyopt' command | |
215 | * line option instead. | |
216 | */ | |
217 | if (new_ec_form || new_ec_asn1_flag) { | |
218 | if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) { | |
219 | ERR_print_errors(bio_err); | |
220 | goto end; | |
221 | } | |
222 | if (new_ec_form) | |
223 | EC_KEY_set_conv_form(eckey, ec_form); | |
224 | ||
225 | if (new_ec_asn1_flag) | |
226 | EC_KEY_set_asn1_flag(eckey, ec_asn1_flag); | |
227 | } | |
228 | #endif | |
229 | ||
b0004708 | 230 | if (check || pub_check) { |
2aee35d3 PY |
231 | int r; |
232 | EVP_PKEY_CTX *ctx; | |
233 | ||
234 | ctx = EVP_PKEY_CTX_new(pkey, e); | |
235 | if (ctx == NULL) { | |
236 | ERR_print_errors(bio_err); | |
237 | goto end; | |
238 | } | |
239 | ||
b0004708 PY |
240 | if (check) |
241 | r = EVP_PKEY_check(ctx); | |
242 | else | |
243 | r = EVP_PKEY_public_check(ctx); | |
2aee35d3 PY |
244 | |
245 | if (r == 1) { | |
246 | BIO_printf(out, "Key is valid\n"); | |
247 | } else { | |
248 | /* | |
249 | * Note: at least for RSA keys if this function returns | |
250 | * -1, there will be no error reasons. | |
251 | */ | |
252 | unsigned long err; | |
253 | ||
254 | BIO_printf(out, "Key is invalid\n"); | |
255 | ||
256 | while ((err = ERR_peek_error()) != 0) { | |
257 | BIO_printf(out, "Detailed error: %s\n", | |
258 | ERR_reason_error_string(err)); | |
eff1752b | 259 | ERR_get_error(); /* remove err from error stack */ |
2aee35d3 PY |
260 | } |
261 | } | |
262 | EVP_PKEY_CTX_free(ctx); | |
263 | } | |
264 | ||
0f113f3e MC |
265 | if (!noout) { |
266 | if (outformat == FORMAT_PEM) { | |
2234212c | 267 | if (pubout) { |
c87af534 DB |
268 | if (!PEM_write_bio_PUBKEY(out, pkey)) |
269 | goto end; | |
2234212c | 270 | } else { |
7eff6aa0 | 271 | assert(private); |
c87af534 DB |
272 | if (traditional) { |
273 | if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher, | |
274 | NULL, 0, NULL, | |
275 | passout)) | |
276 | goto end; | |
277 | } else { | |
278 | if (!PEM_write_bio_PrivateKey(out, pkey, cipher, | |
279 | NULL, 0, NULL, passout)) | |
280 | goto end; | |
281 | } | |
7eff6aa0 | 282 | } |
0f113f3e | 283 | } else if (outformat == FORMAT_ASN1) { |
2234212c | 284 | if (pubout) { |
c87af534 DB |
285 | if (!i2d_PUBKEY_bio(out, pkey)) |
286 | goto end; | |
2234212c | 287 | } else { |
7eff6aa0 | 288 | assert(private); |
c87af534 DB |
289 | if (!i2d_PrivateKey_bio(out, pkey)) |
290 | goto end; | |
7eff6aa0 | 291 | } |
0f113f3e MC |
292 | } else { |
293 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
294 | goto end; | |
295 | } | |
0f113f3e MC |
296 | } |
297 | ||
298 | if (text) { | |
2234212c | 299 | if (pubtext) { |
c87af534 DB |
300 | if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) |
301 | goto end; | |
2234212c | 302 | } else { |
3b061a00 | 303 | assert(private); |
c87af534 DB |
304 | if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0) |
305 | goto end; | |
3b061a00 | 306 | } |
0f113f3e MC |
307 | } |
308 | ||
309 | ret = 0; | |
310 | ||
311 | end: | |
0396a447 DSH |
312 | if (ret != 0) |
313 | ERR_print_errors(bio_err); | |
0f113f3e | 314 | EVP_PKEY_free(pkey); |
dd1abd44 | 315 | release_engine(e); |
0f113f3e MC |
316 | BIO_free_all(out); |
317 | BIO_free(in); | |
b548a1f1 RS |
318 | OPENSSL_free(passin); |
319 | OPENSSL_free(passout); | |
0f113f3e MC |
320 | |
321 | return ret; | |
322 | } |