xref: /freebsd/crypto/openssl/apps/pkey.c (revision dafba19e42e78cd3d7c9264ece49ddd3d7d70da5)
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,
23     OPT_OUTFORM,
24     OPT_PASSIN,
25     OPT_PASSOUT,
26     OPT_ENGINE,
27     OPT_IN,
28     OPT_OUT,
29     OPT_PUBIN,
30     OPT_PUBOUT,
31     OPT_TEXT_PUB,
32     OPT_TEXT,
33     OPT_NOOUT,
34     OPT_CIPHER,
35     OPT_TRADITIONAL,
36     OPT_CHECK,
37     OPT_PUB_CHECK,
38     OPT_EC_PARAM_ENC,
39     OPT_EC_CONV_FORM,
40     OPT_PROV_ENUM
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     OPT_PROV_OPTIONS,
50 
51     { "check", OPT_CHECK, '-', "Check key consistency" },
52     { "pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency" },
53 
54     OPT_SECTION("Input"),
55     { "in", OPT_IN, 's', "Input key" },
56     { "inform", OPT_INFORM, 'f',
57         "Key input format (ENGINE, other values ignored)" },
58     { "passin", OPT_PASSIN, 's', "Key input pass phrase source" },
59     { "pubin", OPT_PUBIN, '-',
60         "Read only public components from key input" },
61 
62     OPT_SECTION("Output"),
63     { "out", OPT_OUT, '>', "Output file for encoded and/or text output" },
64     { "outform", OPT_OUTFORM, 'F', "Output encoding format (DER or PEM)" },
65     { "", OPT_CIPHER, '-', "Any supported cipher to be used for encryption" },
66     { "passout", OPT_PASSOUT, 's', "Output PEM file pass phrase source" },
67     { "traditional", OPT_TRADITIONAL, '-',
68         "Use traditional format for private key PEM output" },
69     { "pubout", OPT_PUBOUT, '-', "Restrict encoded output to public components" },
70     { "noout", OPT_NOOUT, '-', "Do not output the key in encoded form" },
71     { "text", OPT_TEXT, '-', "Output key components in plaintext" },
72     { "text_pub", OPT_TEXT_PUB, '-',
73         "Output only public key components in text form" },
74     { "ec_conv_form", OPT_EC_CONV_FORM, 's',
75         "Specifies the EC point conversion form in the encoding" },
76     { "ec_param_enc", OPT_EC_PARAM_ENC, 's',
77         "Specifies the way the EC parameters are encoded" },
78 
79     { NULL }
80 };
81 
82 int pkey_main(int argc, char **argv)
83 {
84     BIO *out = NULL;
85     ENGINE *e = NULL;
86     EVP_PKEY *pkey = NULL;
87     EVP_PKEY_CTX *ctx = NULL;
88     EVP_CIPHER *cipher = NULL;
89     char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
90     char *passinarg = NULL, *passoutarg = NULL, *ciphername = NULL, *prog;
91     OPTION_CHOICE o;
92     int informat = FORMAT_UNDEF, outformat = FORMAT_PEM;
93     int pubin = 0, pubout = 0, text_pub = 0, text = 0, noout = 0, ret = 1;
94     int private = 0, traditional = 0, check = 0, pub_check = 0;
95 #ifndef OPENSSL_NO_EC
96     char *asn1_encoding = NULL;
97     char *point_format = NULL;
98 #endif
99 
100     opt_set_unknown_name("cipher");
101     prog = opt_init(argc, argv, pkey_options);
102     while ((o = opt_next()) != OPT_EOF) {
103         switch (o) {
104         case OPT_EOF:
105         case OPT_ERR:
106         opthelp:
107             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
108             goto end;
109         case OPT_HELP:
110             opt_help(pkey_options);
111             ret = 0;
112             goto end;
113         case OPT_INFORM:
114             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
115                 goto opthelp;
116             break;
117         case OPT_OUTFORM:
118             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
119                 goto opthelp;
120             break;
121         case OPT_PASSIN:
122             passinarg = opt_arg();
123             break;
124         case OPT_PASSOUT:
125             passoutarg = opt_arg();
126             break;
127         case OPT_ENGINE:
128             e = setup_engine(opt_arg(), 0);
129             break;
130         case OPT_IN:
131             infile = opt_arg();
132             break;
133         case OPT_OUT:
134             outfile = opt_arg();
135             break;
136         case OPT_PUBIN:
137             pubin = pubout = 1;
138             break;
139         case OPT_PUBOUT:
140             pubout = 1;
141             break;
142         case OPT_TEXT_PUB:
143             text_pub = 1;
144             break;
145         case OPT_TEXT:
146             text = 1;
147             break;
148         case OPT_NOOUT:
149             noout = 1;
150             break;
151         case OPT_TRADITIONAL:
152             traditional = 1;
153             break;
154         case OPT_CHECK:
155             check = 1;
156             break;
157         case OPT_PUB_CHECK:
158             pub_check = 1;
159             break;
160         case OPT_CIPHER:
161             ciphername = opt_unknown();
162             break;
163         case OPT_EC_CONV_FORM:
164 #ifdef OPENSSL_NO_EC
165             goto opthelp;
166 #else
167             point_format = opt_arg();
168             if (!opt_string(point_format, point_format_options))
169                 goto opthelp;
170             break;
171 #endif
172         case OPT_EC_PARAM_ENC:
173 #ifdef OPENSSL_NO_EC
174             goto opthelp;
175 #else
176             asn1_encoding = opt_arg();
177             if (!opt_string(asn1_encoding, asn1_encoding_options))
178                 goto opthelp;
179             break;
180 #endif
181         case OPT_PROV_CASES:
182             if (!opt_provider(o))
183                 goto end;
184             break;
185         }
186     }
187 
188     /* No extra arguments. */
189     if (!opt_check_rest_arg(NULL))
190         goto opthelp;
191 
192     if (text && text_pub)
193         BIO_printf(bio_err,
194             "Warning: The -text option is ignored with -text_pub\n");
195     if (traditional && (noout || outformat != FORMAT_PEM))
196         BIO_printf(bio_err,
197             "Warning: The -traditional is ignored since there is no PEM output\n");
198 
199     /* -pubout and -text is the same as -text_pub */
200     if (!text_pub && pubout && text) {
201         text = 0;
202         text_pub = 1;
203     }
204 
205     private = (!noout && !pubout) || (text && !text_pub);
206 
207     if (!opt_cipher(ciphername, &cipher))
208         goto opthelp;
209     if (cipher == NULL) {
210         if (passoutarg != NULL)
211             BIO_printf(bio_err,
212                 "Warning: The -passout option is ignored without a cipher option\n");
213     } else {
214         if (noout || outformat != FORMAT_PEM) {
215             BIO_printf(bio_err,
216                 "Error: Cipher options are supported only for PEM output\n");
217             goto end;
218         }
219     }
220     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
221         BIO_printf(bio_err, "Error getting passwords\n");
222         goto end;
223     }
224 
225     if (pubin)
226         pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
227     else
228         pkey = load_key(infile, informat, 1, passin, e, "key");
229     if (pkey == NULL)
230         goto end;
231 
232     out = bio_open_owner(outfile, outformat, private);
233     if (out == NULL)
234         goto end;
235 
236 #ifndef OPENSSL_NO_EC
237     if (asn1_encoding != NULL || point_format != NULL) {
238         OSSL_PARAM params[3], *p = params;
239 
240         if (!EVP_PKEY_is_a(pkey, "EC"))
241             goto end;
242 
243         if (asn1_encoding != NULL)
244             *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
245                 asn1_encoding, 0);
246         if (point_format != NULL)
247             *p++ = OSSL_PARAM_construct_utf8_string(
248                 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
249                 point_format, 0);
250         *p = OSSL_PARAM_construct_end();
251         if (EVP_PKEY_set_params(pkey, params) <= 0)
252             goto end;
253     }
254 #endif
255 
256     if (check || pub_check) {
257         int r;
258 
259         ctx = EVP_PKEY_CTX_new(pkey, e);
260         if (ctx == NULL) {
261             ERR_print_errors(bio_err);
262             goto end;
263         }
264 
265         if (check && !pubin)
266             r = EVP_PKEY_check(ctx);
267         else
268             r = EVP_PKEY_public_check(ctx);
269 
270         if (r == 1) {
271             BIO_printf(out, "Key is valid\n");
272         } else {
273             /*
274              * Note: at least for RSA keys if this function returns
275              * -1, there will be no error reasons.
276              */
277             BIO_printf(bio_err, "Key is invalid\n");
278             ERR_print_errors(bio_err);
279             goto end;
280         }
281     }
282 
283     if (!noout) {
284         if (outformat == FORMAT_PEM) {
285             if (pubout) {
286                 if (!PEM_write_bio_PUBKEY(out, pkey))
287                     goto end;
288             } else {
289                 assert(private);
290                 if (traditional) {
291                     if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
292                             NULL, 0, NULL,
293                             passout))
294                         goto end;
295                 } else {
296                     if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
297                             NULL, 0, NULL, passout))
298                         goto end;
299                 }
300             }
301         } else if (outformat == FORMAT_ASN1) {
302             if (text || text_pub) {
303                 BIO_printf(bio_err,
304                     "Error: Text output cannot be combined with DER output\n");
305                 goto end;
306             }
307             if (pubout) {
308                 if (!i2d_PUBKEY_bio(out, pkey))
309                     goto end;
310             } else {
311                 assert(private);
312                 if (!i2d_PrivateKey_bio(out, pkey))
313                     goto end;
314             }
315         } else {
316             BIO_printf(bio_err, "Bad format specified for key\n");
317             goto end;
318         }
319     }
320 
321     if (text_pub) {
322         if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
323             goto end;
324     } else if (text) {
325         assert(private);
326         if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
327             goto end;
328     }
329 
330     ret = 0;
331 
332 end:
333     if (ret != 0)
334         ERR_print_errors(bio_err);
335     EVP_PKEY_CTX_free(ctx);
336     EVP_PKEY_free(pkey);
337     EVP_CIPHER_free(cipher);
338     release_engine(e);
339     BIO_free_all(out);
340     OPENSSL_free(passin);
341     OPENSSL_free(passout);
342 
343     return ret;
344 }
345