]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkey.c
Reduce optimization in hppa builds
[thirdparty/openssl.git] / apps / pkey.c
CommitLineData
0f113f3e 1/*
da1c088f 2 * Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
3e84b6e1 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
3e84b6e1 8 */
846e33c7 9
3e84b6e1
DSH
10#include <stdio.h>
11#include <string.h>
12#include "apps.h"
dab2cd68 13#include "progs.h"
5b5eea4b 14#include "ec_common.h"
3e84b6e1
DSH
15#include <openssl/pem.h>
16#include <openssl/err.h>
17#include <openssl/evp.h>
5b5eea4b 18#include <openssl/core_names.h>
92fee421 19
7e1b7485 20typedef enum OPTION_choice {
b0f96018 21 OPT_COMMON,
7e1b7485
RS
22 OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
23 OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
1f7643e8 24 OPT_TEXT, OPT_NOOUT, OPT_CIPHER, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK,
6bd4e3f2
P
25 OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM,
26 OPT_PROV_ENUM
7e1b7485
RS
27} OPTION_CHOICE;
28
44c83ebd 29const OPTIONS pkey_options[] = {
5388f986 30 OPT_SECTION("General"),
7e1b7485 31 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
32#ifndef OPENSSL_NO_ENGINE
33 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
34#endif
1f7643e8
DDO
35 OPT_PROV_OPTIONS,
36
5388f986
RS
37 {"check", OPT_CHECK, '-', "Check key consistency"},
38 {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
5388f986
RS
39
40 OPT_SECTION("Input"),
41 {"in", OPT_IN, 's', "Input key"},
1f7643e8
DDO
42 {"inform", OPT_INFORM, 'f',
43 "Key input format (ENGINE, other values ignored)"},
44 {"passin", OPT_PASSIN, 's', "Key input pass phrase source"},
7e1b7485 45 {"pubin", OPT_PUBIN, '-',
046a7aaa 46 "Read only public components from key input"},
5388f986
RS
47
48 OPT_SECTION("Output"),
046a7aaa
DDO
49 {"out", OPT_OUT, '>', "Output file for encoded and/or text output"},
50 {"outform", OPT_OUTFORM, 'F', "Output encoding format (DER or PEM)"},
1f7643e8 51 {"", OPT_CIPHER, '-', "Any supported cipher to be used for encryption"},
475d1002 52 {"passout", OPT_PASSOUT, 's', "Output PEM file pass phrase source"},
1f7643e8
DDO
53 {"traditional", OPT_TRADITIONAL, '-',
54 "Use traditional format for private key PEM output"},
046a7aaa
DDO
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"},
1f7643e8 58 {"text_pub", OPT_TEXT_PUB, '-',
046a7aaa 59 "Output only public key components in text form"},
1f7643e8 60 {"ec_conv_form", OPT_EC_CONV_FORM, 's',
046a7aaa 61 "Specifies the EC point conversion form in the encoding"},
1f7643e8 62 {"ec_param_enc", OPT_EC_PARAM_ENC, 's',
046a7aaa 63 "Specifies the way the EC parameters are encoded"},
5388f986 64
7e1b7485
RS
65 {NULL}
66};
3e84b6e1 67
7e1b7485 68int pkey_main(int argc, char **argv)
0f113f3e 69{
57cea5ba 70 BIO *out = NULL;
7e1b7485 71 ENGINE *e = NULL;
0f113f3e 72 EVP_PKEY *pkey = NULL;
ed37336b 73 EVP_PKEY_CTX *ctx = NULL;
606a417f 74 EVP_CIPHER *cipher = NULL;
7e1b7485 75 char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
03bbd346 76 char *passinarg = NULL, *passoutarg = NULL, *ciphername = NULL, *prog;
7e1b7485 77 OPTION_CHOICE o;
d382e796 78 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM;
046a7aaa 79 int pubin = 0, pubout = 0, text_pub = 0, text = 0, noout = 0, ret = 1;
b0004708 80 int private = 0, traditional = 0, check = 0, pub_check = 0;
92fee421 81#ifndef OPENSSL_NO_EC
5b5eea4b
SL
82 char *asn1_encoding = NULL;
83 char *point_format = NULL;
92fee421 84#endif
7e1b7485 85
2c272447 86 opt_set_unknown_name("cipher");
7e1b7485
RS
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:
dd958974 100 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
7e1b7485
RS
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:
333b070e 114 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
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:
046a7aaa 123 pubin = pubout = 1;
7e1b7485
RS
124 break;
125 case OPT_PUBOUT:
0f113f3e 126 pubout = 1;
7e1b7485
RS
127 break;
128 case OPT_TEXT_PUB:
046a7aaa 129 text_pub = 1;
7e1b7485
RS
130 break;
131 case OPT_TEXT:
0f113f3e 132 text = 1;
7e1b7485
RS
133 break;
134 case OPT_NOOUT:
0f113f3e 135 noout = 1;
7e1b7485 136 break;
05dba815
DSH
137 case OPT_TRADITIONAL:
138 traditional = 1;
139 break;
2aee35d3
PY
140 case OPT_CHECK:
141 check = 1;
142 break;
b0004708
PY
143 case OPT_PUB_CHECK:
144 pub_check = 1;
145 break;
1f7643e8 146 case OPT_CIPHER:
03bbd346 147 ciphername = opt_unknown();
92fee421
P
148 break;
149 case OPT_EC_CONV_FORM:
150#ifdef OPENSSL_NO_EC
151 goto opthelp;
152#else
5b5eea4b
SL
153 point_format = opt_arg();
154 if (!opt_string(point_format, point_format_options))
92fee421 155 goto opthelp;
92fee421
P
156 break;
157#endif
158 case OPT_EC_PARAM_ENC:
159#ifdef OPENSSL_NO_EC
160 goto opthelp;
161#else
5b5eea4b
SL
162 asn1_encoding = opt_arg();
163 if (!opt_string(asn1_encoding, asn1_encoding_options))
92fee421 164 goto opthelp;
92fee421
P
165 break;
166#endif
6bd4e3f2
P
167 case OPT_PROV_CASES:
168 if (!opt_provider(o))
169 goto end;
170 break;
0f113f3e 171 }
0f113f3e 172 }
021410ea
RS
173
174 /* No extra arguments. */
d9f07357 175 if (!opt_check_rest_arg(NULL))
03358517
KR
176 goto opthelp;
177
046a7aaa
DDO
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");
5ac6d7d2
RL
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
046a7aaa 191 private = (!noout && !pubout) || (text && !text_pub);
0f113f3e 192
d9f07357
DDO
193 if (!opt_cipher(ciphername, &cipher))
194 goto opthelp;
046a7aaa
DDO
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 }
475d1002 205 }
7e1b7485 206 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
207 BIO_printf(bio_err, "Error getting passwords\n");
208 goto end;
209 }
210
bdd58d98 211 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
212 if (out == NULL)
213 goto end;
0f113f3e
MC
214
215 if (pubin)
7e1b7485 216 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
0f113f3e 217 else
7e1b7485 218 pkey = load_key(infile, informat, 1, passin, e, "key");
2234212c 219 if (pkey == NULL)
0f113f3e
MC
220 goto end;
221
92fee421 222#ifndef OPENSSL_NO_EC
5b5eea4b
SL
223 if (asn1_encoding != NULL || point_format != NULL) {
224 OSSL_PARAM params[3], *p = params;
225
226 if (!EVP_PKEY_is_a(pkey, "EC"))
92fee421 227 goto end;
92fee421 228
5b5eea4b
SL
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;
92fee421
P
239 }
240#endif
241
b0004708 242 if (check || pub_check) {
2aee35d3 243 int r;
2aee35d3
PY
244
245 ctx = EVP_PKEY_CTX_new(pkey, e);
246 if (ctx == NULL) {
247 ERR_print_errors(bio_err);
248 goto end;
249 }
250
3b1c0c8f 251 if (check && !pubin)
b0004708
PY
252 r = EVP_PKEY_check(ctx);
253 else
254 r = EVP_PKEY_public_check(ctx);
2aee35d3
PY
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 */
7f90026b
DDO
263 BIO_printf(bio_err, "Key is invalid\n");
264 ERR_print_errors(bio_err);
ed37336b 265 goto end;
2aee35d3 266 }
2aee35d3
PY
267 }
268
0f113f3e
MC
269 if (!noout) {
270 if (outformat == FORMAT_PEM) {
2234212c 271 if (pubout) {
c87af534
DB
272 if (!PEM_write_bio_PUBKEY(out, pkey))
273 goto end;
2234212c 274 } else {
7eff6aa0 275 assert(private);
c87af534
DB
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 }
7eff6aa0 286 }
0f113f3e 287 } else if (outformat == FORMAT_ASN1) {
046a7aaa
DDO
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 }
2234212c 293 if (pubout) {
c87af534
DB
294 if (!i2d_PUBKEY_bio(out, pkey))
295 goto end;
2234212c 296 } else {
7eff6aa0 297 assert(private);
c87af534
DB
298 if (!i2d_PrivateKey_bio(out, pkey))
299 goto end;
7eff6aa0 300 }
0f113f3e
MC
301 } else {
302 BIO_printf(bio_err, "Bad format specified for key\n");
303 goto end;
304 }
0f113f3e
MC
305 }
306
046a7aaa
DDO
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;
0f113f3e
MC
314 }
315
316 ret = 0;
317
318 end:
0396a447
DSH
319 if (ret != 0)
320 ERR_print_errors(bio_err);
ed37336b 321 EVP_PKEY_CTX_free(ctx);
0f113f3e 322 EVP_PKEY_free(pkey);
606a417f 323 EVP_CIPHER_free(cipher);
dd1abd44 324 release_engine(e);
0f113f3e 325 BIO_free_all(out);
b548a1f1
RS
326 OPENSSL_free(passin);
327 OPENSSL_free(passout);
0f113f3e
MC
328
329 return ret;
330}