]>
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, | |
1f7643e8 | 39 | OPT_TEXT, OPT_NOOUT, OPT_CIPHER, 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 | |
1f7643e8 DDO |
50 | OPT_PROV_OPTIONS, |
51 | ||
5388f986 RS |
52 | {"check", OPT_CHECK, '-', "Check key consistency"}, |
53 | {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"}, | |
5388f986 RS |
54 | |
55 | OPT_SECTION("Input"), | |
56 | {"in", OPT_IN, 's', "Input key"}, | |
1f7643e8 DDO |
57 | {"inform", OPT_INFORM, 'f', |
58 | "Key input format (ENGINE, other values ignored)"}, | |
59 | {"passin", OPT_PASSIN, 's', "Key input pass phrase source"}, | |
7e1b7485 | 60 | {"pubin", OPT_PUBIN, '-', |
046a7aaa | 61 | "Read only public components from key input"}, |
5388f986 RS |
62 | |
63 | OPT_SECTION("Output"), | |
046a7aaa DDO |
64 | {"out", OPT_OUT, '>', "Output file for encoded and/or text output"}, |
65 | {"outform", OPT_OUTFORM, 'F', "Output encoding format (DER or PEM)"}, | |
1f7643e8 | 66 | {"", OPT_CIPHER, '-', "Any supported cipher to be used for encryption"}, |
475d1002 | 67 | {"passout", OPT_PASSOUT, 's', "Output PEM file pass phrase source"}, |
1f7643e8 DDO |
68 | {"traditional", OPT_TRADITIONAL, '-', |
69 | "Use traditional format for private key PEM output"}, | |
046a7aaa DDO |
70 | {"pubout", OPT_PUBOUT, '-', "Restrict encoded output to public components"}, |
71 | {"noout", OPT_NOOUT, '-', "Do not output the key in encoded form"}, | |
72 | {"text", OPT_TEXT, '-', "Output key components in plaintext"}, | |
1f7643e8 | 73 | {"text_pub", OPT_TEXT_PUB, '-', |
046a7aaa | 74 | "Output only public key components in text form"}, |
1f7643e8 | 75 | {"ec_conv_form", OPT_EC_CONV_FORM, 's', |
046a7aaa | 76 | "Specifies the EC point conversion form in the encoding"}, |
1f7643e8 | 77 | {"ec_param_enc", OPT_EC_PARAM_ENC, 's', |
046a7aaa | 78 | "Specifies the way the EC parameters are encoded"}, |
5388f986 | 79 | |
7e1b7485 RS |
80 | {NULL} |
81 | }; | |
3e84b6e1 | 82 | |
7e1b7485 | 83 | int pkey_main(int argc, char **argv) |
0f113f3e | 84 | { |
0f113f3e | 85 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 86 | ENGINE *e = NULL; |
0f113f3e | 87 | EVP_PKEY *pkey = NULL; |
ed37336b | 88 | EVP_PKEY_CTX *ctx = NULL; |
7e1b7485 RS |
89 | const EVP_CIPHER *cipher = NULL; |
90 | char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL; | |
333b070e | 91 | char *passinarg = NULL, *passoutarg = NULL, *prog; |
7e1b7485 RS |
92 | OPTION_CHOICE o; |
93 | int informat = FORMAT_PEM, outformat = FORMAT_PEM; | |
046a7aaa | 94 | int pubin = 0, pubout = 0, text_pub = 0, text = 0, noout = 0, ret = 1; |
b0004708 | 95 | int private = 0, traditional = 0, check = 0, pub_check = 0; |
92fee421 P |
96 | #ifndef OPENSSL_NO_EC |
97 | EC_KEY *eckey; | |
98 | int ec_asn1_flag = OPENSSL_EC_NAMED_CURVE, new_ec_asn1_flag = 0; | |
99 | int i, new_ec_form = 0; | |
100 | point_conversion_form_t ec_form = POINT_CONVERSION_UNCOMPRESSED; | |
101 | #endif | |
7e1b7485 RS |
102 | |
103 | prog = opt_init(argc, argv, pkey_options); | |
104 | while ((o = opt_next()) != OPT_EOF) { | |
105 | switch (o) { | |
106 | case OPT_EOF: | |
107 | case OPT_ERR: | |
108 | opthelp: | |
109 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
110 | goto end; | |
111 | case OPT_HELP: | |
112 | opt_help(pkey_options); | |
113 | ret = 0; | |
114 | goto end; | |
115 | case OPT_INFORM: | |
dd958974 | 116 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) |
7e1b7485 RS |
117 | goto opthelp; |
118 | break; | |
119 | case OPT_OUTFORM: | |
120 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
121 | goto opthelp; | |
122 | break; | |
123 | case OPT_PASSIN: | |
124 | passinarg = opt_arg(); | |
125 | break; | |
126 | case OPT_PASSOUT: | |
127 | passoutarg = opt_arg(); | |
128 | break; | |
129 | case OPT_ENGINE: | |
333b070e | 130 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
131 | break; |
132 | case OPT_IN: | |
133 | infile = opt_arg(); | |
134 | break; | |
135 | case OPT_OUT: | |
136 | outfile = opt_arg(); | |
137 | break; | |
138 | case OPT_PUBIN: | |
046a7aaa | 139 | pubin = pubout = 1; |
7e1b7485 RS |
140 | break; |
141 | case OPT_PUBOUT: | |
0f113f3e | 142 | pubout = 1; |
7e1b7485 RS |
143 | break; |
144 | case OPT_TEXT_PUB: | |
046a7aaa | 145 | text_pub = 1; |
7e1b7485 RS |
146 | break; |
147 | case OPT_TEXT: | |
0f113f3e | 148 | text = 1; |
7e1b7485 RS |
149 | break; |
150 | case OPT_NOOUT: | |
0f113f3e | 151 | noout = 1; |
7e1b7485 | 152 | break; |
05dba815 DSH |
153 | case OPT_TRADITIONAL: |
154 | traditional = 1; | |
155 | break; | |
2aee35d3 PY |
156 | case OPT_CHECK: |
157 | check = 1; | |
158 | break; | |
b0004708 PY |
159 | case OPT_PUB_CHECK: |
160 | pub_check = 1; | |
161 | break; | |
1f7643e8 | 162 | case OPT_CIPHER: |
7e1b7485 RS |
163 | if (!opt_cipher(opt_unknown(), &cipher)) |
164 | goto opthelp; | |
92fee421 P |
165 | break; |
166 | case OPT_EC_CONV_FORM: | |
167 | #ifdef OPENSSL_NO_EC | |
168 | goto opthelp; | |
169 | #else | |
170 | if (!opt_pair(opt_arg(), ec_conv_forms, &i)) | |
171 | goto opthelp; | |
172 | new_ec_form = 1; | |
173 | ec_form = i; | |
174 | break; | |
175 | #endif | |
176 | case OPT_EC_PARAM_ENC: | |
177 | #ifdef OPENSSL_NO_EC | |
178 | goto opthelp; | |
179 | #else | |
180 | if (!opt_pair(opt_arg(), ec_param_enc, &i)) | |
181 | goto opthelp; | |
182 | new_ec_asn1_flag = 1; | |
183 | ec_asn1_flag = i; | |
184 | break; | |
185 | #endif | |
6bd4e3f2 P |
186 | case OPT_PROV_CASES: |
187 | if (!opt_provider(o)) | |
188 | goto end; | |
189 | break; | |
0f113f3e | 190 | } |
0f113f3e | 191 | } |
021410ea RS |
192 | |
193 | /* No extra arguments. */ | |
7e1b7485 | 194 | argc = opt_num_rest(); |
03358517 KR |
195 | if (argc != 0) |
196 | goto opthelp; | |
197 | ||
046a7aaa DDO |
198 | if (noout && pubout) |
199 | BIO_printf(bio_err, | |
200 | "Warning: The -pubout option is ignored with -noout\n"); | |
201 | if (text && text_pub) | |
202 | BIO_printf(bio_err, | |
203 | "Warning: The -text option is ignored with -text_pub\n"); | |
204 | if (traditional && (noout || outformat != FORMAT_PEM)) | |
205 | BIO_printf(bio_err, | |
206 | "Warning: The -traditional is ignored since there is no PEM output\n"); | |
207 | private = (!noout && !pubout) || (text && !text_pub); | |
0f113f3e | 208 | |
046a7aaa DDO |
209 | if (cipher == NULL) { |
210 | if (passoutarg != NULL) | |
211 | BIO_printf(bio_err, | |
212 | "Warning: The -passout option is ignored without a cipher option\n"); | |
213 | } else { | |
214 | if (noout || outformat != FORMAT_PEM) { | |
215 | BIO_printf(bio_err, | |
216 | "Error: Cipher options are supported only for PEM output\n"); | |
217 | goto end; | |
218 | } | |
475d1002 | 219 | } |
7e1b7485 | 220 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
221 | BIO_printf(bio_err, "Error getting passwords\n"); |
222 | goto end; | |
223 | } | |
224 | ||
bdd58d98 | 225 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
226 | if (out == NULL) |
227 | goto end; | |
0f113f3e MC |
228 | |
229 | if (pubin) | |
7e1b7485 | 230 | pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key"); |
0f113f3e | 231 | else |
7e1b7485 | 232 | pkey = load_key(infile, informat, 1, passin, e, "key"); |
2234212c | 233 | if (pkey == NULL) |
0f113f3e MC |
234 | goto end; |
235 | ||
92fee421 P |
236 | #ifndef OPENSSL_NO_EC |
237 | /* | |
238 | * TODO: remove this and use a set params call with a 'pkeyopt' command | |
239 | * line option instead. | |
240 | */ | |
241 | if (new_ec_form || new_ec_asn1_flag) { | |
242 | if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) { | |
243 | ERR_print_errors(bio_err); | |
244 | goto end; | |
245 | } | |
246 | if (new_ec_form) | |
247 | EC_KEY_set_conv_form(eckey, ec_form); | |
248 | ||
249 | if (new_ec_asn1_flag) | |
250 | EC_KEY_set_asn1_flag(eckey, ec_asn1_flag); | |
251 | } | |
252 | #endif | |
253 | ||
b0004708 | 254 | if (check || pub_check) { |
2aee35d3 | 255 | int r; |
2aee35d3 PY |
256 | |
257 | ctx = EVP_PKEY_CTX_new(pkey, e); | |
258 | if (ctx == NULL) { | |
259 | ERR_print_errors(bio_err); | |
260 | goto end; | |
261 | } | |
262 | ||
b0004708 PY |
263 | if (check) |
264 | r = EVP_PKEY_check(ctx); | |
265 | else | |
266 | r = EVP_PKEY_public_check(ctx); | |
2aee35d3 PY |
267 | |
268 | if (r == 1) { | |
269 | BIO_printf(out, "Key is valid\n"); | |
270 | } else { | |
271 | /* | |
272 | * Note: at least for RSA keys if this function returns | |
273 | * -1, there will be no error reasons. | |
274 | */ | |
275 | unsigned long err; | |
276 | ||
277 | BIO_printf(out, "Key is invalid\n"); | |
278 | ||
279 | while ((err = ERR_peek_error()) != 0) { | |
280 | BIO_printf(out, "Detailed error: %s\n", | |
281 | ERR_reason_error_string(err)); | |
eff1752b | 282 | ERR_get_error(); /* remove err from error stack */ |
2aee35d3 | 283 | } |
ed37336b | 284 | goto end; |
2aee35d3 | 285 | } |
2aee35d3 PY |
286 | } |
287 | ||
0f113f3e MC |
288 | if (!noout) { |
289 | if (outformat == FORMAT_PEM) { | |
2234212c | 290 | if (pubout) { |
c87af534 DB |
291 | if (!PEM_write_bio_PUBKEY(out, pkey)) |
292 | goto end; | |
2234212c | 293 | } else { |
7eff6aa0 | 294 | assert(private); |
c87af534 DB |
295 | if (traditional) { |
296 | if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher, | |
297 | NULL, 0, NULL, | |
298 | passout)) | |
299 | goto end; | |
300 | } else { | |
301 | if (!PEM_write_bio_PrivateKey(out, pkey, cipher, | |
302 | NULL, 0, NULL, passout)) | |
303 | goto end; | |
304 | } | |
7eff6aa0 | 305 | } |
0f113f3e | 306 | } else if (outformat == FORMAT_ASN1) { |
046a7aaa DDO |
307 | if (text || text_pub) { |
308 | BIO_printf(bio_err, | |
309 | "Error: Text output cannot be combined with DER output\n"); | |
310 | goto end; | |
311 | } | |
2234212c | 312 | if (pubout) { |
c87af534 DB |
313 | if (!i2d_PUBKEY_bio(out, pkey)) |
314 | goto end; | |
2234212c | 315 | } else { |
7eff6aa0 | 316 | assert(private); |
c87af534 DB |
317 | if (!i2d_PrivateKey_bio(out, pkey)) |
318 | goto end; | |
7eff6aa0 | 319 | } |
0f113f3e MC |
320 | } else { |
321 | BIO_printf(bio_err, "Bad format specified for key\n"); | |
322 | goto end; | |
323 | } | |
0f113f3e MC |
324 | } |
325 | ||
046a7aaa DDO |
326 | if (text_pub) { |
327 | if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) | |
328 | goto end; | |
329 | } else if (text) { | |
330 | assert(private); | |
331 | if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0) | |
332 | goto end; | |
0f113f3e MC |
333 | } |
334 | ||
335 | ret = 0; | |
336 | ||
337 | end: | |
0396a447 DSH |
338 | if (ret != 0) |
339 | ERR_print_errors(bio_err); | |
ed37336b | 340 | EVP_PKEY_CTX_free(ctx); |
0f113f3e | 341 | EVP_PKEY_free(pkey); |
dd1abd44 | 342 | release_engine(e); |
0f113f3e MC |
343 | BIO_free_all(out); |
344 | BIO_free(in); | |
b548a1f1 RS |
345 | OPENSSL_free(passin); |
346 | OPENSSL_free(passout); | |
0f113f3e MC |
347 | |
348 | return ret; | |
349 | } |