Fetch cipher after loading providers
[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_ERR = -1, OPT_EOF = 0, OPT_HELP,
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 *in = NULL, *out = NULL;
71     ENGINE *e = NULL;
72     EVP_PKEY *pkey = NULL;
73     EVP_PKEY_CTX *ctx = NULL;
74     const 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_PEM, 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     argc = opt_num_rest();
175     if (argc != 0)
176         goto opthelp;
177
178     if (noout && pubout)
179         BIO_printf(bio_err,
180                    "Warning: The -pubout option is ignored with -noout\n");
181     if (text && text_pub)
182         BIO_printf(bio_err,
183                    "Warning: The -text option is ignored with -text_pub\n");
184     if (traditional && (noout || outformat != FORMAT_PEM))
185         BIO_printf(bio_err,
186                    "Warning: The -traditional is ignored since there is no PEM output\n");
187     private = (!noout && !pubout) || (text && !text_pub);
188
189     if (ciphername != NULL) {
190         if (!opt_cipher(ciphername, &cipher))
191             goto opthelp;
192     }
193     if (cipher == NULL) {
194         if (passoutarg != NULL)
195             BIO_printf(bio_err,
196                        "Warning: The -passout option is ignored without a cipher option\n");
197     } else {
198         if (noout || outformat != FORMAT_PEM) {
199             BIO_printf(bio_err,
200                        "Error: Cipher options are supported only for PEM 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     if (asn1_encoding != NULL || point_format != NULL) {
222         OSSL_PARAM params[3], *p = params;
223
224         if (!EVP_PKEY_is_a(pkey, "EC"))
225             goto end;
226
227         if (asn1_encoding != NULL)
228             *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
229                                                     asn1_encoding, 0);
230         if (point_format != NULL)
231             *p++ = OSSL_PARAM_construct_utf8_string(
232                        OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
233                        point_format, 0);
234         *p = OSSL_PARAM_construct_end();
235         if (EVP_PKEY_set_params(pkey, params) <= 0)
236             goto end;
237     }
238 #endif
239
240     if (check || pub_check) {
241         int r;
242
243         ctx = EVP_PKEY_CTX_new(pkey, e);
244         if (ctx == NULL) {
245             ERR_print_errors(bio_err);
246             goto end;
247         }
248
249         if (check)
250             r = EVP_PKEY_check(ctx);
251         else
252             r = EVP_PKEY_public_check(ctx);
253
254         if (r == 1) {
255             BIO_printf(out, "Key is valid\n");
256         } else {
257             /*
258              * Note: at least for RSA keys if this function returns
259              * -1, there will be no error reasons.
260              */
261             unsigned long err;
262
263             BIO_printf(out, "Key is invalid\n");
264
265             while ((err = ERR_peek_error()) != 0) {
266                 BIO_printf(out, "Detailed error: %s\n",
267                            ERR_reason_error_string(err));
268                 ERR_get_error(); /* remove err from error stack */
269             }
270             goto end;
271         }
272     }
273
274     if (!noout) {
275         if (outformat == FORMAT_PEM) {
276             if (pubout) {
277                 if (!PEM_write_bio_PUBKEY(out, pkey))
278                     goto end;
279             } else {
280                 assert(private);
281                 if (traditional) {
282                     if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
283                                                               NULL, 0, NULL,
284                                                               passout))
285                         goto end;
286                 } else {
287                     if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
288                                                   NULL, 0, NULL, passout))
289                         goto end;
290                 }
291             }
292         } else if (outformat == FORMAT_ASN1) {
293             if (text || text_pub) {
294                 BIO_printf(bio_err,
295                            "Error: Text output cannot be combined with DER output\n");
296                 goto end;
297             }
298             if (pubout) {
299                 if (!i2d_PUBKEY_bio(out, pkey))
300                     goto end;
301             } else {
302                 assert(private);
303                 if (!i2d_PrivateKey_bio(out, pkey))
304                     goto end;
305             }
306         } else {
307             BIO_printf(bio_err, "Bad format specified for key\n");
308             goto end;
309         }
310     }
311
312     if (text_pub) {
313         if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
314             goto end;
315     } else if (text) {
316         assert(private);
317         if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
318             goto end;
319     }
320
321     ret = 0;
322
323  end:
324     if (ret != 0)
325         ERR_print_errors(bio_err);
326     EVP_PKEY_CTX_free(ctx);
327     EVP_PKEY_free(pkey);
328     release_engine(e);
329     BIO_free_all(out);
330     BIO_free(in);
331     OPENSSL_free(passin);
332     OPENSSL_free(passout);
333
334     return ret;
335 }