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