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