xref: /openssl/apps/pkey.c (revision c8359abb)
1 /*
2  * Copyright 2006-2023 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_COMMON,
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 
pkey_main(int argc,char ** argv)68 int pkey_main(int argc, char **argv)
69 {
70     BIO *out = NULL;
71     ENGINE *e = NULL;
72     EVP_PKEY *pkey = NULL;
73     EVP_PKEY_CTX *ctx = NULL;
74     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_UNDEF, 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     opt_set_unknown_name("cipher");
87     prog = opt_init(argc, argv, pkey_options);
88     while ((o = opt_next()) != OPT_EOF) {
89         switch (o) {
90         case OPT_EOF:
91         case OPT_ERR:
92  opthelp:
93             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
94             goto end;
95         case OPT_HELP:
96             opt_help(pkey_options);
97             ret = 0;
98             goto end;
99         case OPT_INFORM:
100             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
101                 goto opthelp;
102             break;
103         case OPT_OUTFORM:
104             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
105                 goto opthelp;
106             break;
107         case OPT_PASSIN:
108             passinarg = opt_arg();
109             break;
110         case OPT_PASSOUT:
111             passoutarg = opt_arg();
112             break;
113         case OPT_ENGINE:
114             e = setup_engine(opt_arg(), 0);
115             break;
116         case OPT_IN:
117             infile = opt_arg();
118             break;
119         case OPT_OUT:
120             outfile = opt_arg();
121             break;
122         case OPT_PUBIN:
123             pubin = pubout = 1;
124             break;
125         case OPT_PUBOUT:
126             pubout = 1;
127             break;
128         case OPT_TEXT_PUB:
129             text_pub = 1;
130             break;
131         case OPT_TEXT:
132             text = 1;
133             break;
134         case OPT_NOOUT:
135             noout = 1;
136             break;
137         case OPT_TRADITIONAL:
138             traditional = 1;
139             break;
140         case OPT_CHECK:
141             check = 1;
142             break;
143         case OPT_PUB_CHECK:
144             pub_check = 1;
145             break;
146         case OPT_CIPHER:
147             ciphername = opt_unknown();
148             break;
149         case OPT_EC_CONV_FORM:
150 #ifdef OPENSSL_NO_EC
151             goto opthelp;
152 #else
153             point_format = opt_arg();
154             if (!opt_string(point_format, point_format_options))
155                 goto opthelp;
156             break;
157 #endif
158         case OPT_EC_PARAM_ENC:
159 #ifdef OPENSSL_NO_EC
160             goto opthelp;
161 #else
162             asn1_encoding = opt_arg();
163             if (!opt_string(asn1_encoding, asn1_encoding_options))
164                 goto opthelp;
165             break;
166 #endif
167         case OPT_PROV_CASES:
168             if (!opt_provider(o))
169                 goto end;
170             break;
171         }
172     }
173 
174     /* No extra arguments. */
175     if (!opt_check_rest_arg(NULL))
176         goto opthelp;
177 
178     if (text && text_pub)
179         BIO_printf(bio_err,
180                    "Warning: The -text option is ignored with -text_pub\n");
181     if (traditional && (noout || outformat != FORMAT_PEM))
182         BIO_printf(bio_err,
183                    "Warning: The -traditional is ignored since there is no PEM output\n");
184 
185     /* -pubout and -text is the same as -text_pub */
186     if (!text_pub && pubout && text) {
187         text = 0;
188         text_pub = 1;
189     }
190 
191     private = (!noout && !pubout) || (text && !text_pub);
192 
193     if (!opt_cipher(ciphername, &cipher))
194         goto opthelp;
195     if (cipher == NULL) {
196         if (passoutarg != NULL)
197             BIO_printf(bio_err,
198                        "Warning: The -passout option is ignored without a cipher option\n");
199     } else {
200         if (noout || outformat != FORMAT_PEM) {
201             BIO_printf(bio_err,
202                        "Error: Cipher options are supported only for PEM output\n");
203             goto end;
204         }
205     }
206     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
207         BIO_printf(bio_err, "Error getting passwords\n");
208         goto end;
209     }
210 
211     if (pubin)
212         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
213     else
214         pkey = load_key(infile, informat, 1, passin, e, "key");
215     if (pkey == NULL)
216         goto end;
217 
218     out = bio_open_owner(outfile, outformat, private);
219     if (out == NULL)
220         goto end;
221 
222 #ifndef OPENSSL_NO_EC
223     if (asn1_encoding != NULL || point_format != NULL) {
224         OSSL_PARAM params[3], *p = params;
225 
226         if (!EVP_PKEY_is_a(pkey, "EC"))
227             goto end;
228 
229         if (asn1_encoding != NULL)
230             *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
231                                                     asn1_encoding, 0);
232         if (point_format != NULL)
233             *p++ = OSSL_PARAM_construct_utf8_string(
234                        OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
235                        point_format, 0);
236         *p = OSSL_PARAM_construct_end();
237         if (EVP_PKEY_set_params(pkey, params) <= 0)
238             goto end;
239     }
240 #endif
241 
242     if (check || pub_check) {
243         int r;
244 
245         ctx = EVP_PKEY_CTX_new(pkey, e);
246         if (ctx == NULL) {
247             ERR_print_errors(bio_err);
248             goto end;
249         }
250 
251         if (check && !pubin)
252             r = EVP_PKEY_check(ctx);
253         else
254             r = EVP_PKEY_public_check(ctx);
255 
256         if (r == 1) {
257             BIO_printf(out, "Key is valid\n");
258         } else {
259             /*
260              * Note: at least for RSA keys if this function returns
261              * -1, there will be no error reasons.
262              */
263             BIO_printf(bio_err, "Key is invalid\n");
264             ERR_print_errors(bio_err);
265             goto end;
266         }
267     }
268 
269     if (!noout) {
270         if (outformat == FORMAT_PEM) {
271             if (pubout) {
272                 if (!PEM_write_bio_PUBKEY(out, pkey))
273                     goto end;
274             } else {
275                 assert(private);
276                 if (traditional) {
277                     if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
278                                                               NULL, 0, NULL,
279                                                               passout))
280                         goto end;
281                 } else {
282                     if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
283                                                   NULL, 0, NULL, passout))
284                         goto end;
285                 }
286             }
287         } else if (outformat == FORMAT_ASN1) {
288             if (text || text_pub) {
289                 BIO_printf(bio_err,
290                            "Error: Text output cannot be combined with DER output\n");
291                 goto end;
292             }
293             if (pubout) {
294                 if (!i2d_PUBKEY_bio(out, pkey))
295                     goto end;
296             } else {
297                 assert(private);
298                 if (!i2d_PrivateKey_bio(out, pkey))
299                     goto end;
300             }
301         } else {
302             BIO_printf(bio_err, "Bad format specified for key\n");
303             goto end;
304         }
305     }
306 
307     if (text_pub) {
308         if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
309             goto end;
310     } else if (text) {
311         assert(private);
312         if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
313             goto end;
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     EVP_CIPHER_free(cipher);
324     release_engine(e);
325     BIO_free_all(out);
326     OPENSSL_free(passin);
327     OPENSSL_free(passout);
328 
329     return ret;
330 }
331