apps/pkey.c: Make clear that -passout is not supported for DER output
[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 PEM 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 (outformat == FORMAT_ASN1 && passoutarg != NULL) {
200         BIO_printf(bio_err, "The -passout option is not supported for DER output\n");
201         goto end;
202     }
203
204     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
205         BIO_printf(bio_err, "Error getting passwords\n");
206         goto end;
207     }
208
209     out = bio_open_owner(outfile, outformat, private);
210     if (out == NULL)
211         goto end;
212
213     if (pubin)
214         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
215     else
216         pkey = load_key(infile, informat, 1, passin, e, "key");
217     if (pkey == NULL)
218         goto end;
219
220 #ifndef OPENSSL_NO_EC
221     /*
222      * TODO: remove this and use a set params call with a 'pkeyopt' command
223      * line option instead.
224      */
225     if (new_ec_form || new_ec_asn1_flag) {
226         if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) {
227             ERR_print_errors(bio_err);
228             goto end;
229         }
230         if (new_ec_form)
231             EC_KEY_set_conv_form(eckey, ec_form);
232
233         if (new_ec_asn1_flag)
234             EC_KEY_set_asn1_flag(eckey, ec_asn1_flag);
235     }
236 #endif
237
238     if (check || pub_check) {
239         int r;
240
241         ctx = EVP_PKEY_CTX_new(pkey, e);
242         if (ctx == NULL) {
243             ERR_print_errors(bio_err);
244             goto end;
245         }
246
247         if (check)
248             r = EVP_PKEY_check(ctx);
249         else
250             r = EVP_PKEY_public_check(ctx);
251
252         if (r == 1) {
253             BIO_printf(out, "Key is valid\n");
254         } else {
255             /*
256              * Note: at least for RSA keys if this function returns
257              * -1, there will be no error reasons.
258              */
259             unsigned long err;
260
261             BIO_printf(out, "Key is invalid\n");
262
263             while ((err = ERR_peek_error()) != 0) {
264                 BIO_printf(out, "Detailed error: %s\n",
265                            ERR_reason_error_string(err));
266                 ERR_get_error(); /* remove err from error stack */
267             }
268             goto end;
269         }
270     }
271
272     if (!noout) {
273         if (outformat == FORMAT_PEM) {
274             if (pubout) {
275                 if (!PEM_write_bio_PUBKEY(out, pkey))
276                     goto end;
277             } else {
278                 assert(private);
279                 if (traditional) {
280                     if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
281                                                               NULL, 0, NULL,
282                                                               passout))
283                         goto end;
284                 } else {
285                     if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
286                                                   NULL, 0, NULL, passout))
287                         goto end;
288                 }
289             }
290         } else if (outformat == FORMAT_ASN1) {
291             if (pubout) {
292                 if (!i2d_PUBKEY_bio(out, pkey))
293                     goto end;
294             } else {
295                 assert(private);
296                 if (!i2d_PrivateKey_bio(out, pkey))
297                     goto end;
298             }
299         } else {
300             BIO_printf(bio_err, "Bad format specified for key\n");
301             goto end;
302         }
303     }
304
305     if (text) {
306         if (pubtext) {
307             if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
308                 goto end;
309         } else {
310             assert(private);
311             if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
312                 goto end;
313         }
314     }
315
316     ret = 0;
317
318  end:
319     if (ret != 0)
320         ERR_print_errors(bio_err);
321     EVP_PKEY_CTX_free(ctx);
322     EVP_PKEY_free(pkey);
323     release_engine(e);
324     BIO_free_all(out);
325     BIO_free(in);
326     OPENSSL_free(passin);
327     OPENSSL_free(passout);
328
329     return ret;
330 }