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