apps/pkey.c: Forther improve user guidance, also on non-sensical option combinations
[openssl.git] / apps / pkey.c
1 /*
2  * Copyright 2006-2020 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 <openssl/pem.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17
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
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,
39     OPT_TEXT, OPT_NOOUT, OPT_CIPHER, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK,
40     OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM,
41     OPT_PROV_ENUM
42 } OPTION_CHOICE;
43
44 const OPTIONS pkey_options[] = {
45     OPT_SECTION("General"),
46     {"help", OPT_HELP, '-', "Display this summary"},
47 #ifndef OPENSSL_NO_ENGINE
48     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
49 #endif
50     OPT_PROV_OPTIONS,
51
52     {"check", OPT_CHECK, '-', "Check key consistency"},
53     {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
54
55     OPT_SECTION("Input"),
56     {"in", OPT_IN, 's', "Input key"},
57     {"inform", OPT_INFORM, 'f',
58      "Key input format (ENGINE, other values ignored)"},
59     {"passin", OPT_PASSIN, 's', "Key input pass phrase source"},
60     {"pubin", OPT_PUBIN, '-',
61      "Read only public components from key input"},
62
63     OPT_SECTION("Output"),
64     {"out", OPT_OUT, '>', "Output file for encoded and/or text output"},
65     {"outform", OPT_OUTFORM, 'F', "Output encoding format (DER or PEM)"},
66     {"", OPT_CIPHER, '-', "Any supported cipher to be used for encryption"},
67     {"passout", OPT_PASSOUT, 's', "Output PEM file pass phrase source"},
68     {"traditional", OPT_TRADITIONAL, '-',
69      "Use traditional format for private key PEM output"},
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"},
73     {"text_pub", OPT_TEXT_PUB, '-',
74      "Output only public key components in text form"},
75     {"ec_conv_form", OPT_EC_CONV_FORM, 's',
76      "Specifies the EC point conversion form in the encoding"},
77     {"ec_param_enc", OPT_EC_PARAM_ENC, 's',
78      "Specifies the way the EC parameters are encoded"},
79
80     {NULL}
81 };
82
83 int pkey_main(int argc, char **argv)
84 {
85     BIO *in = NULL, *out = NULL;
86     ENGINE *e = NULL;
87     EVP_PKEY *pkey = NULL;
88     EVP_PKEY_CTX *ctx = NULL;
89     const EVP_CIPHER *cipher = NULL;
90     char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
91     char *passinarg = NULL, *passoutarg = NULL, *prog;
92     OPTION_CHOICE o;
93     int informat = FORMAT_PEM, outformat = FORMAT_PEM;
94     int pubin = 0, pubout = 0, text_pub = 0, text = 0, noout = 0, ret = 1;
95     int private = 0, traditional = 0, check = 0, pub_check = 0;
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
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:
116             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
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:
130             e = setup_engine(opt_arg(), 0);
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:
139             pubin = pubout = 1;
140             break;
141         case OPT_PUBOUT:
142             pubout = 1;
143             break;
144         case OPT_TEXT_PUB:
145             text_pub = 1;
146             break;
147         case OPT_TEXT:
148             text = 1;
149             break;
150         case OPT_NOOUT:
151             noout = 1;
152             break;
153         case OPT_TRADITIONAL:
154             traditional = 1;
155             break;
156         case OPT_CHECK:
157             check = 1;
158             break;
159         case OPT_PUB_CHECK:
160             pub_check = 1;
161             break;
162         case OPT_CIPHER:
163             if (!opt_cipher(opt_unknown(), &cipher))
164                 goto opthelp;
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
186         case OPT_PROV_CASES:
187             if (!opt_provider(o))
188                 goto end;
189             break;
190         }
191     }
192
193     /* No extra arguments. */
194     argc = opt_num_rest();
195     if (argc != 0)
196         goto opthelp;
197
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);
208
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         }
219     }
220     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
221         BIO_printf(bio_err, "Error getting passwords\n");
222         goto end;
223     }
224
225     out = bio_open_owner(outfile, outformat, private);
226     if (out == NULL)
227         goto end;
228
229     if (pubin)
230         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
231     else
232         pkey = load_key(infile, informat, 1, passin, e, "key");
233     if (pkey == NULL)
234         goto end;
235
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
254     if (check || pub_check) {
255         int r;
256
257         ctx = EVP_PKEY_CTX_new(pkey, e);
258         if (ctx == NULL) {
259             ERR_print_errors(bio_err);
260             goto end;
261         }
262
263         if (check)
264             r = EVP_PKEY_check(ctx);
265         else
266             r = EVP_PKEY_public_check(ctx);
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));
282                 ERR_get_error(); /* remove err from error stack */
283             }
284             goto end;
285         }
286     }
287
288     if (!noout) {
289         if (outformat == FORMAT_PEM) {
290             if (pubout) {
291                 if (!PEM_write_bio_PUBKEY(out, pkey))
292                     goto end;
293             } else {
294                 assert(private);
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                 }
305             }
306         } else if (outformat == FORMAT_ASN1) {
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             }
312             if (pubout) {
313                 if (!i2d_PUBKEY_bio(out, pkey))
314                     goto end;
315             } else {
316                 assert(private);
317                 if (!i2d_PrivateKey_bio(out, pkey))
318                     goto end;
319             }
320         } else {
321             BIO_printf(bio_err, "Bad format specified for key\n");
322             goto end;
323         }
324     }
325
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;
333     }
334
335     ret = 0;
336
337  end:
338     if (ret != 0)
339         ERR_print_errors(bio_err);
340     EVP_PKEY_CTX_free(ctx);
341     EVP_PKEY_free(pkey);
342     release_engine(e);
343     BIO_free_all(out);
344     BIO_free(in);
345     OPENSSL_free(passin);
346     OPENSSL_free(passout);
347
348     return ret;
349 }