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