[apps/pkey] Return error on failed `-[pub]check`
[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_MD, 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     {"check", OPT_CHECK, '-', "Check key consistency"},
51     {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
52     {"", OPT_MD, '-', "Any supported cipher"},
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 "},
57
58     OPT_SECTION("Input"),
59     {"in", OPT_IN, 's', "Input key"},
60     {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE)"},
61     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
62     {"pubin", OPT_PUBIN, '-',
63      "Read public key from input (default is private key)"},
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"},
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"},
75
76     OPT_PROV_OPTIONS,
77     {NULL}
78 };
79
80 int pkey_main(int argc, char **argv)
81 {
82     BIO *in = NULL, *out = NULL;
83     ENGINE *e = NULL;
84     EVP_PKEY *pkey = NULL;
85     EVP_PKEY_CTX *ctx = NULL;
86     const EVP_CIPHER *cipher = NULL;
87     char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
88     char *passinarg = NULL, *passoutarg = NULL, *prog;
89     OPTION_CHOICE o;
90     int informat = FORMAT_PEM, outformat = FORMAT_PEM;
91     int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
92     int private = 0, traditional = 0, check = 0, pub_check = 0;
93 #ifndef OPENSSL_NO_EC
94     EC_KEY *eckey;
95     int ec_asn1_flag = OPENSSL_EC_NAMED_CURVE, new_ec_asn1_flag = 0;
96     int i, new_ec_form = 0;
97     point_conversion_form_t ec_form = POINT_CONVERSION_UNCOMPRESSED;
98 #endif
99
100     prog = opt_init(argc, argv, pkey_options);
101     while ((o = opt_next()) != OPT_EOF) {
102         switch (o) {
103         case OPT_EOF:
104         case OPT_ERR:
105  opthelp:
106             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
107             goto end;
108         case OPT_HELP:
109             opt_help(pkey_options);
110             ret = 0;
111             goto end;
112         case OPT_INFORM:
113             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
114                 goto opthelp;
115             break;
116         case OPT_OUTFORM:
117             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
118                 goto opthelp;
119             break;
120         case OPT_PASSIN:
121             passinarg = opt_arg();
122             break;
123         case OPT_PASSOUT:
124             passoutarg = opt_arg();
125             break;
126         case OPT_ENGINE:
127             e = setup_engine(opt_arg(), 0);
128             break;
129         case OPT_IN:
130             infile = opt_arg();
131             break;
132         case OPT_OUT:
133             outfile = opt_arg();
134             break;
135         case OPT_PUBIN:
136             pubin = pubout = pubtext = 1;
137             break;
138         case OPT_PUBOUT:
139             pubout = 1;
140             break;
141         case OPT_TEXT_PUB:
142             pubtext = text = 1;
143             break;
144         case OPT_TEXT:
145             text = 1;
146             break;
147         case OPT_NOOUT:
148             noout = 1;
149             break;
150         case OPT_TRADITIONAL:
151             traditional = 1;
152             break;
153         case OPT_CHECK:
154             check = 1;
155             break;
156         case OPT_PUB_CHECK:
157             pub_check = 1;
158             break;
159         case OPT_MD:
160             if (!opt_cipher(opt_unknown(), &cipher))
161                 goto opthelp;
162             break;
163         case OPT_EC_CONV_FORM:
164 #ifdef OPENSSL_NO_EC
165             goto opthelp;
166 #else
167             if (!opt_pair(opt_arg(), ec_conv_forms, &i))
168                 goto opthelp;
169             new_ec_form = 1;
170             ec_form = i;
171             break;
172 #endif
173         case OPT_EC_PARAM_ENC:
174 #ifdef OPENSSL_NO_EC
175             goto opthelp;
176 #else
177             if (!opt_pair(opt_arg(), ec_param_enc, &i))
178                 goto opthelp;
179             new_ec_asn1_flag = 1;
180             ec_asn1_flag = i;
181             break;
182 #endif
183         case OPT_PROV_CASES:
184             if (!opt_provider(o))
185                 goto end;
186             break;
187         }
188     }
189
190     /* No extra arguments. */
191     argc = opt_num_rest();
192     if (argc != 0)
193         goto opthelp;
194
195     private = !noout && !pubout ? 1 : 0;
196     if (text && !pubtext)
197         private = 1;
198
199     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
200         BIO_printf(bio_err, "Error getting passwords\n");
201         goto end;
202     }
203
204     out = bio_open_owner(outfile, outformat, private);
205     if (out == NULL)
206         goto end;
207
208     if (pubin)
209         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
210     else
211         pkey = load_key(infile, informat, 1, passin, e, "key");
212     if (pkey == NULL)
213         goto end;
214
215 #ifndef OPENSSL_NO_EC
216     /*
217      * TODO: remove this and use a set params call with a 'pkeyopt' command
218      * line option instead.
219      */
220     if (new_ec_form || new_ec_asn1_flag) {
221         if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) {
222             ERR_print_errors(bio_err);
223             goto end;
224         }
225         if (new_ec_form)
226             EC_KEY_set_conv_form(eckey, ec_form);
227
228         if (new_ec_asn1_flag)
229             EC_KEY_set_asn1_flag(eckey, ec_asn1_flag);
230     }
231 #endif
232
233     if (check || pub_check) {
234         int r;
235
236         ctx = EVP_PKEY_CTX_new(pkey, e);
237         if (ctx == NULL) {
238             ERR_print_errors(bio_err);
239             goto end;
240         }
241
242         if (check)
243             r = EVP_PKEY_check(ctx);
244         else
245             r = EVP_PKEY_public_check(ctx);
246
247         if (r == 1) {
248             BIO_printf(out, "Key is valid\n");
249         } else {
250             /*
251              * Note: at least for RSA keys if this function returns
252              * -1, there will be no error reasons.
253              */
254             unsigned long err;
255
256             BIO_printf(out, "Key is invalid\n");
257
258             while ((err = ERR_peek_error()) != 0) {
259                 BIO_printf(out, "Detailed error: %s\n",
260                            ERR_reason_error_string(err));
261                 ERR_get_error(); /* remove err from error stack */
262             }
263             goto end;
264         }
265     }
266
267     if (!noout) {
268         if (outformat == FORMAT_PEM) {
269             if (pubout) {
270                 if (!PEM_write_bio_PUBKEY(out, pkey))
271                     goto end;
272             } else {
273                 assert(private);
274                 if (traditional) {
275                     if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
276                                                               NULL, 0, NULL,
277                                                               passout))
278                         goto end;
279                 } else {
280                     if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
281                                                   NULL, 0, NULL, passout))
282                         goto end;
283                 }
284             }
285         } else if (outformat == FORMAT_ASN1) {
286             if (pubout) {
287                 if (!i2d_PUBKEY_bio(out, pkey))
288                     goto end;
289             } else {
290                 assert(private);
291                 if (!i2d_PrivateKey_bio(out, pkey))
292                     goto end;
293             }
294         } else {
295             BIO_printf(bio_err, "Bad format specified for key\n");
296             goto end;
297         }
298     }
299
300     if (text) {
301         if (pubtext) {
302             if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
303                 goto end;
304         } else {
305             assert(private);
306             if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
307                 goto end;
308         }
309     }
310
311     ret = 0;
312
313  end:
314     if (ret != 0)
315         ERR_print_errors(bio_err);
316     EVP_PKEY_CTX_free(ctx);
317     EVP_PKEY_free(pkey);
318     release_engine(e);
319     BIO_free_all(out);
320     BIO_free(in);
321     OPENSSL_free(passin);
322     OPENSSL_free(passout);
323
324     return ret;
325 }