]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/pkey.c
APPS: Improve diagnostics on missing/extra args and unknown cipher/digest
[thirdparty/openssl.git] / apps / pkey.c
1 /*
2 * Copyright 2006-2021 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 <stdio.h>
11 #include <string.h>
12 #include "apps.h"
13 #include "progs.h"
14 #include "ec_common.h"
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/core_names.h>
19
20 typedef enum OPTION_choice {
21 OPT_COMMON,
22 OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
23 OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
24 OPT_TEXT, OPT_NOOUT, OPT_CIPHER, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK,
25 OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM,
26 OPT_PROV_ENUM
27 } OPTION_CHOICE;
28
29 const OPTIONS pkey_options[] = {
30 OPT_SECTION("General"),
31 {"help", OPT_HELP, '-', "Display this summary"},
32 #ifndef OPENSSL_NO_ENGINE
33 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
34 #endif
35 OPT_PROV_OPTIONS,
36
37 {"check", OPT_CHECK, '-', "Check key consistency"},
38 {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
39
40 OPT_SECTION("Input"),
41 {"in", OPT_IN, 's', "Input key"},
42 {"inform", OPT_INFORM, 'f',
43 "Key input format (ENGINE, other values ignored)"},
44 {"passin", OPT_PASSIN, 's', "Key input pass phrase source"},
45 {"pubin", OPT_PUBIN, '-',
46 "Read only public components from key input"},
47
48 OPT_SECTION("Output"),
49 {"out", OPT_OUT, '>', "Output file for encoded and/or text output"},
50 {"outform", OPT_OUTFORM, 'F', "Output encoding format (DER or PEM)"},
51 {"", OPT_CIPHER, '-', "Any supported cipher to be used for encryption"},
52 {"passout", OPT_PASSOUT, 's', "Output PEM file pass phrase source"},
53 {"traditional", OPT_TRADITIONAL, '-',
54 "Use traditional format for private key PEM output"},
55 {"pubout", OPT_PUBOUT, '-', "Restrict encoded output to public components"},
56 {"noout", OPT_NOOUT, '-', "Do not output the key in encoded form"},
57 {"text", OPT_TEXT, '-', "Output key components in plaintext"},
58 {"text_pub", OPT_TEXT_PUB, '-',
59 "Output only public key components in text form"},
60 {"ec_conv_form", OPT_EC_CONV_FORM, 's',
61 "Specifies the EC point conversion form in the encoding"},
62 {"ec_param_enc", OPT_EC_PARAM_ENC, 's',
63 "Specifies the way the EC parameters are encoded"},
64
65 {NULL}
66 };
67
68 int pkey_main(int argc, char **argv)
69 {
70 BIO *out = NULL;
71 ENGINE *e = NULL;
72 EVP_PKEY *pkey = NULL;
73 EVP_PKEY_CTX *ctx = NULL;
74 EVP_CIPHER *cipher = NULL;
75 char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
76 char *passinarg = NULL, *passoutarg = NULL, *ciphername = NULL, *prog;
77 OPTION_CHOICE o;
78 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM;
79 int pubin = 0, pubout = 0, text_pub = 0, text = 0, noout = 0, ret = 1;
80 int private = 0, traditional = 0, check = 0, pub_check = 0;
81 #ifndef OPENSSL_NO_EC
82 char *asn1_encoding = NULL;
83 char *point_format = NULL;
84 #endif
85
86 prog = opt_init(argc, argv, pkey_options);
87 while ((o = opt_next()) != OPT_EOF) {
88 switch (o) {
89 case OPT_EOF:
90 case OPT_ERR:
91 opthelp:
92 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
93 goto end;
94 case OPT_HELP:
95 opt_help(pkey_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_OUTFORM:
103 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
104 goto opthelp;
105 break;
106 case OPT_PASSIN:
107 passinarg = opt_arg();
108 break;
109 case OPT_PASSOUT:
110 passoutarg = opt_arg();
111 break;
112 case OPT_ENGINE:
113 e = setup_engine(opt_arg(), 0);
114 break;
115 case OPT_IN:
116 infile = opt_arg();
117 break;
118 case OPT_OUT:
119 outfile = opt_arg();
120 break;
121 case OPT_PUBIN:
122 pubin = pubout = 1;
123 break;
124 case OPT_PUBOUT:
125 pubout = 1;
126 break;
127 case OPT_TEXT_PUB:
128 text_pub = 1;
129 break;
130 case OPT_TEXT:
131 text = 1;
132 break;
133 case OPT_NOOUT:
134 noout = 1;
135 break;
136 case OPT_TRADITIONAL:
137 traditional = 1;
138 break;
139 case OPT_CHECK:
140 check = 1;
141 break;
142 case OPT_PUB_CHECK:
143 pub_check = 1;
144 break;
145 case OPT_CIPHER:
146 ciphername = opt_unknown();
147 break;
148 case OPT_EC_CONV_FORM:
149 #ifdef OPENSSL_NO_EC
150 goto opthelp;
151 #else
152 point_format = opt_arg();
153 if (!opt_string(point_format, point_format_options))
154 goto opthelp;
155 break;
156 #endif
157 case OPT_EC_PARAM_ENC:
158 #ifdef OPENSSL_NO_EC
159 goto opthelp;
160 #else
161 asn1_encoding = opt_arg();
162 if (!opt_string(asn1_encoding, asn1_encoding_options))
163 goto opthelp;
164 break;
165 #endif
166 case OPT_PROV_CASES:
167 if (!opt_provider(o))
168 goto end;
169 break;
170 }
171 }
172
173 /* No extra arguments. */
174 if (!opt_check_rest_arg(NULL))
175 goto opthelp;
176
177 if (text && text_pub)
178 BIO_printf(bio_err,
179 "Warning: The -text option is ignored with -text_pub\n");
180 if (traditional && (noout || outformat != FORMAT_PEM))
181 BIO_printf(bio_err,
182 "Warning: The -traditional is ignored since there is no PEM output\n");
183
184 /* -pubout and -text is the same as -text_pub */
185 if (!text_pub && pubout && text) {
186 text = 0;
187 text_pub = 1;
188 }
189
190 private = (!noout && !pubout) || (text && !text_pub);
191
192 if (!opt_cipher(ciphername, &cipher))
193 goto opthelp;
194 if (cipher == NULL) {
195 if (passoutarg != NULL)
196 BIO_printf(bio_err,
197 "Warning: The -passout option is ignored without a cipher option\n");
198 } else {
199 if (noout || outformat != FORMAT_PEM) {
200 BIO_printf(bio_err,
201 "Error: Cipher options are supported only for PEM output\n");
202 goto end;
203 }
204 }
205 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
206 BIO_printf(bio_err, "Error getting passwords\n");
207 goto end;
208 }
209
210 out = bio_open_owner(outfile, outformat, private);
211 if (out == NULL)
212 goto end;
213
214 if (pubin)
215 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
216 else
217 pkey = load_key(infile, informat, 1, passin, e, "key");
218 if (pkey == NULL)
219 goto end;
220
221 #ifndef OPENSSL_NO_EC
222 if (asn1_encoding != NULL || point_format != NULL) {
223 OSSL_PARAM params[3], *p = params;
224
225 if (!EVP_PKEY_is_a(pkey, "EC"))
226 goto end;
227
228 if (asn1_encoding != NULL)
229 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
230 asn1_encoding, 0);
231 if (point_format != NULL)
232 *p++ = OSSL_PARAM_construct_utf8_string(
233 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
234 point_format, 0);
235 *p = OSSL_PARAM_construct_end();
236 if (EVP_PKEY_set_params(pkey, params) <= 0)
237 goto end;
238 }
239 #endif
240
241 if (check || pub_check) {
242 int r;
243
244 ctx = EVP_PKEY_CTX_new(pkey, e);
245 if (ctx == NULL) {
246 ERR_print_errors(bio_err);
247 goto end;
248 }
249
250 if (check)
251 r = EVP_PKEY_check(ctx);
252 else
253 r = EVP_PKEY_public_check(ctx);
254
255 if (r == 1) {
256 BIO_printf(out, "Key is valid\n");
257 } else {
258 /*
259 * Note: at least for RSA keys if this function returns
260 * -1, there will be no error reasons.
261 */
262 BIO_printf(bio_err, "Key is invalid\n");
263 ERR_print_errors(bio_err);
264 goto end;
265 }
266 }
267
268 if (!noout) {
269 if (outformat == FORMAT_PEM) {
270 if (pubout) {
271 if (!PEM_write_bio_PUBKEY(out, pkey))
272 goto end;
273 } else {
274 assert(private);
275 if (traditional) {
276 if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
277 NULL, 0, NULL,
278 passout))
279 goto end;
280 } else {
281 if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
282 NULL, 0, NULL, passout))
283 goto end;
284 }
285 }
286 } else if (outformat == FORMAT_ASN1) {
287 if (text || text_pub) {
288 BIO_printf(bio_err,
289 "Error: Text output cannot be combined with DER output\n");
290 goto end;
291 }
292 if (pubout) {
293 if (!i2d_PUBKEY_bio(out, pkey))
294 goto end;
295 } else {
296 assert(private);
297 if (!i2d_PrivateKey_bio(out, pkey))
298 goto end;
299 }
300 } else {
301 BIO_printf(bio_err, "Bad format specified for key\n");
302 goto end;
303 }
304 }
305
306 if (text_pub) {
307 if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
308 goto end;
309 } else if (text) {
310 assert(private);
311 if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
312 goto end;
313 }
314
315 ret = 0;
316
317 end:
318 if (ret != 0)
319 ERR_print_errors(bio_err);
320 EVP_PKEY_CTX_free(ctx);
321 EVP_PKEY_free(pkey);
322 EVP_CIPHER_free(cipher);
323 release_engine(e);
324 BIO_free_all(out);
325 OPENSSL_free(passin);
326 OPENSSL_free(passout);
327
328 return ret;
329 }