]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/ec.c
load_key_certs_crls(): Restore output of fatal errors
[thirdparty/openssl.git] / apps / ec.c
1 /*
2 * Copyright 2002-2020 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 <openssl/opensslconf.h>
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "apps.h"
16 #include "progs.h"
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/pem.h>
21
22 static OPT_PAIR conv_forms[] = {
23 {"compressed", POINT_CONVERSION_COMPRESSED},
24 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
25 {"hybrid", POINT_CONVERSION_HYBRID},
26 {NULL}
27 };
28
29 static OPT_PAIR param_enc[] = {
30 {"named_curve", OPENSSL_EC_NAMED_CURVE},
31 {"explicit", 0},
32 {NULL}
33 };
34
35 typedef enum OPTION_choice {
36 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
37 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
38 OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
39 OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER,
40 OPT_NO_PUBLIC, OPT_CHECK, OPT_PROV_ENUM
41 } OPTION_CHOICE;
42
43 const OPTIONS ec_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
50 OPT_SECTION("Input"),
51 {"in", OPT_IN, 's', "Input file"},
52 {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE)"},
53 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
54 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
55 {"check", OPT_CHECK, '-', "check key consistency"},
56 {"", OPT_CIPHER, '-', "Any supported cipher"},
57 {"param_enc", OPT_PARAM_ENC, 's',
58 "Specifies the way the ec parameters are encoded"},
59 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
60
61 OPT_SECTION("Output"),
62 {"out", OPT_OUT, '>', "Output file"},
63 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
64 {"noout", OPT_NOOUT, '-', "Don't print key out"},
65 {"text", OPT_TEXT, '-', "Print the key"},
66 {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
67 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
68 {"no_public", OPT_NO_PUBLIC, '-', "exclude public key from private key"},
69 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
70
71 OPT_PROV_OPTIONS,
72 {NULL}
73 };
74
75 int ec_main(int argc, char **argv)
76 {
77 BIO *in = NULL, *out = NULL;
78 ENGINE *e = NULL;
79 EC_KEY *eckey = NULL;
80 const EC_GROUP *group;
81 const EVP_CIPHER *enc = NULL;
82 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
83 char *infile = NULL, *outfile = NULL, *prog;
84 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
85 OPTION_CHOICE o;
86 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
87 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
88 int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
89 int no_public = 0, check = 0;
90
91 prog = opt_init(argc, argv, ec_options);
92 while ((o = opt_next()) != OPT_EOF) {
93 switch (o) {
94 case OPT_EOF:
95 case OPT_ERR:
96 opthelp:
97 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
98 goto end;
99 case OPT_HELP:
100 opt_help(ec_options);
101 ret = 0;
102 goto end;
103 case OPT_INFORM:
104 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
105 goto opthelp;
106 break;
107 case OPT_IN:
108 infile = opt_arg();
109 break;
110 case OPT_OUTFORM:
111 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
112 goto opthelp;
113 break;
114 case OPT_OUT:
115 outfile = opt_arg();
116 break;
117 case OPT_NOOUT:
118 noout = 1;
119 break;
120 case OPT_TEXT:
121 text = 1;
122 break;
123 case OPT_PARAM_OUT:
124 param_out = 1;
125 break;
126 case OPT_PUBIN:
127 pubin = 1;
128 break;
129 case OPT_PUBOUT:
130 pubout = 1;
131 break;
132 case OPT_PASSIN:
133 passinarg = opt_arg();
134 break;
135 case OPT_PASSOUT:
136 passoutarg = opt_arg();
137 break;
138 case OPT_ENGINE:
139 e = setup_engine(opt_arg(), 0);
140 break;
141 case OPT_CIPHER:
142 if (!opt_cipher(opt_unknown(), &enc))
143 goto opthelp;
144 break;
145 case OPT_CONV_FORM:
146 if (!opt_pair(opt_arg(), conv_forms, &i))
147 goto opthelp;
148 new_form = 1;
149 form = i;
150 break;
151 case OPT_PARAM_ENC:
152 if (!opt_pair(opt_arg(), param_enc, &i))
153 goto opthelp;
154 new_asn1_flag = 1;
155 asn1_flag = i;
156 break;
157 case OPT_NO_PUBLIC:
158 no_public = 1;
159 break;
160 case OPT_CHECK:
161 check = 1;
162 break;
163 case OPT_PROV_CASES:
164 if (!opt_provider(o))
165 goto end;
166 break;
167 }
168 }
169 argc = opt_num_rest();
170 if (argc != 0)
171 goto opthelp;
172
173 private = param_out || pubin || pubout ? 0 : 1;
174 if (text && !pubin)
175 private = 1;
176
177 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
178 BIO_printf(bio_err, "Error getting passwords\n");
179 goto end;
180 }
181
182 if (informat != FORMAT_ENGINE) {
183 in = bio_open_default(infile, 'r', informat);
184 if (in == NULL)
185 goto end;
186 }
187
188 BIO_printf(bio_err, "read EC key\n");
189 if (informat == FORMAT_ASN1) {
190 if (pubin)
191 eckey = d2i_EC_PUBKEY_bio(in, NULL);
192 else
193 eckey = d2i_ECPrivateKey_bio(in, NULL);
194 } else if (informat == FORMAT_ENGINE) {
195 EVP_PKEY *pkey;
196 if (pubin)
197 pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
198 else
199 pkey = load_key(infile, informat, 1, passin, e, "private key");
200 if (pkey != NULL) {
201 eckey = EVP_PKEY_get1_EC_KEY(pkey);
202 EVP_PKEY_free(pkey);
203 }
204 } else {
205 if (pubin)
206 eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
207 else
208 eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
209 }
210 if (eckey == NULL) {
211 BIO_printf(bio_err, "unable to load Key\n");
212 ERR_print_errors(bio_err);
213 goto end;
214 }
215
216 out = bio_open_owner(outfile, outformat, private);
217 if (out == NULL)
218 goto end;
219
220 group = EC_KEY_get0_group(eckey);
221
222 if (new_form)
223 EC_KEY_set_conv_form(eckey, form);
224
225 if (new_asn1_flag)
226 EC_KEY_set_asn1_flag(eckey, asn1_flag);
227
228 if (no_public)
229 EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
230
231 if (text) {
232 assert(pubin || private);
233 if (!EC_KEY_print(out, eckey, 0)) {
234 perror(outfile);
235 ERR_print_errors(bio_err);
236 goto end;
237 }
238 }
239
240 if (check) {
241 if (EC_KEY_check_key(eckey) == 1) {
242 BIO_printf(bio_err, "EC Key valid.\n");
243 } else {
244 BIO_printf(bio_err, "EC Key Invalid!\n");
245 ERR_print_errors(bio_err);
246 }
247 }
248
249 if (noout) {
250 ret = 0;
251 goto end;
252 }
253
254 BIO_printf(bio_err, "writing EC key\n");
255 if (outformat == FORMAT_ASN1) {
256 if (param_out) {
257 i = i2d_ECPKParameters_bio(out, group);
258 } else if (pubin || pubout) {
259 i = i2d_EC_PUBKEY_bio(out, eckey);
260 } else {
261 assert(private);
262 i = i2d_ECPrivateKey_bio(out, eckey);
263 }
264 } else {
265 if (param_out) {
266 i = PEM_write_bio_ECPKParameters(out, group);
267 } else if (pubin || pubout) {
268 i = PEM_write_bio_EC_PUBKEY(out, eckey);
269 } else {
270 assert(private);
271 i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
272 NULL, 0, NULL, passout);
273 }
274 }
275
276 if (!i) {
277 BIO_printf(bio_err, "unable to write private key\n");
278 ERR_print_errors(bio_err);
279 } else {
280 ret = 0;
281 }
282 end:
283 BIO_free(in);
284 BIO_free_all(out);
285 EC_KEY_free(eckey);
286 release_engine(e);
287 OPENSSL_free(passin);
288 OPENSSL_free(passout);
289 return ret;
290 }