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