]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ec.c
"Show" more respect to no-sha* config options.
[thirdparty/openssl.git] / apps / ec.c
CommitLineData
d4a8f90c
BM
1/* apps/ec.c */
2/*
3 * Written by Nils Larsch for the OpenSSL project.
4 */
4d94ae00 5/* ====================================================================
9dd84053 6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4d94ae00
BM
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
4d94ae00 58
d4a8f90c 59#ifndef OPENSSL_NO_EC
4d94ae00
BM
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
4d94ae00
BM
63#include "apps.h"
64#include <openssl/bio.h>
65#include <openssl/err.h>
4d94ae00 66#include <openssl/evp.h>
4d94ae00
BM
67#include <openssl/pem.h>
68
69#undef PROG
d4a8f90c 70#define PROG ec_main
4d94ae00 71
eefa6e4e
BM
72/* -inform arg - input format - default PEM (one of DER, NET or PEM)
73 * -outform arg - output format - default PEM
74 * -in arg - input file - default stdin
75 * -out arg - output file - default stdout
76 * -des - encrypt output if PEM format with DES in cbc mode
77 * -text - print a text version
78 * -param_out - print the elliptic curve parameters
79 * -conv_form arg - specifies the point encoding form
80 * -param_enc arg - specifies the parameter encoding
4d94ae00
BM
81 */
82
83int MAIN(int, char **);
84
85int MAIN(int argc, char **argv)
86{
58ae65cd 87#ifndef OPENSSL_NO_ENGINE
4d94ae00 88 ENGINE *e = NULL;
58ae65cd 89#endif
4d94ae00 90 int ret = 1;
14a7cfb3 91 EC_KEY *eckey = NULL;
9dd84053 92 const EC_GROUP *group;
4d94ae00
BM
93 int i, badops = 0;
94 const EVP_CIPHER *enc = NULL;
95 BIO *in = NULL, *out = NULL;
96 int informat, outformat, text=0, noout=0;
eefa6e4e 97 int pubin = 0, pubout = 0, param_out = 0;
4d94ae00
BM
98 char *infile, *outfile, *prog, *engine;
99 char *passargin = NULL, *passargout = NULL;
100 char *passin = NULL, *passout = NULL;
eefa6e4e
BM
101 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
102 int new_form = 0;
103 int asn1_flag = OPENSSL_EC_NAMED_CURVE;
104 int new_asn1_flag = 0;
4d94ae00
BM
105
106 apps_startup();
107
108 if (bio_err == NULL)
109 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
110 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
111
3647bee2
DSH
112 if (!load_config(bio_err, NULL))
113 goto end;
114
4d94ae00
BM
115 engine = NULL;
116 infile = NULL;
117 outfile = NULL;
118 informat = FORMAT_PEM;
119 outformat = FORMAT_PEM;
120
121 prog = argv[0];
122 argc--;
123 argv++;
124 while (argc >= 1)
4d94ae00 125 {
eefa6e4e
BM
126 if (strcmp(*argv,"-inform") == 0)
127 {
4d94ae00
BM
128 if (--argc < 1) goto bad;
129 informat=str2fmt(*(++argv));
eefa6e4e 130 }
4d94ae00 131 else if (strcmp(*argv,"-outform") == 0)
eefa6e4e 132 {
4d94ae00
BM
133 if (--argc < 1) goto bad;
134 outformat=str2fmt(*(++argv));
eefa6e4e 135 }
4d94ae00 136 else if (strcmp(*argv,"-in") == 0)
eefa6e4e 137 {
4d94ae00
BM
138 if (--argc < 1) goto bad;
139 infile= *(++argv);
eefa6e4e 140 }
4d94ae00 141 else if (strcmp(*argv,"-out") == 0)
eefa6e4e 142 {
4d94ae00
BM
143 if (--argc < 1) goto bad;
144 outfile= *(++argv);
eefa6e4e 145 }
4d94ae00 146 else if (strcmp(*argv,"-passin") == 0)
eefa6e4e 147 {
4d94ae00
BM
148 if (--argc < 1) goto bad;
149 passargin= *(++argv);
eefa6e4e 150 }
4d94ae00 151 else if (strcmp(*argv,"-passout") == 0)
eefa6e4e 152 {
4d94ae00
BM
153 if (--argc < 1) goto bad;
154 passargout= *(++argv);
eefa6e4e 155 }
4d94ae00 156 else if (strcmp(*argv, "-engine") == 0)
eefa6e4e 157 {
4d94ae00
BM
158 if (--argc < 1) goto bad;
159 engine= *(++argv);
eefa6e4e 160 }
4d94ae00
BM
161 else if (strcmp(*argv, "-noout") == 0)
162 noout = 1;
163 else if (strcmp(*argv, "-text") == 0)
164 text = 1;
eefa6e4e 165 else if (strcmp(*argv, "-conv_form") == 0)
4d94ae00 166 {
eefa6e4e
BM
167 if (--argc < 1)
168 goto bad;
169 ++argv;
170 new_form = 1;
171 if (strcmp(*argv, "compressed") == 0)
172 form = POINT_CONVERSION_COMPRESSED;
173 else if (strcmp(*argv, "uncompressed") == 0)
174 form = POINT_CONVERSION_UNCOMPRESSED;
175 else if (strcmp(*argv, "hybrid") == 0)
176 form = POINT_CONVERSION_HYBRID;
177 else
178 goto bad;
4d94ae00 179 }
eefa6e4e
BM
180 else if (strcmp(*argv, "-param_enc") == 0)
181 {
182 if (--argc < 1)
183 goto bad;
184 ++argv;
185 new_asn1_flag = 1;
186 if (strcmp(*argv, "named_curve") == 0)
187 asn1_flag = OPENSSL_EC_NAMED_CURVE;
188 else if (strcmp(*argv, "explicit") == 0)
189 asn1_flag = 0;
190 else
191 goto bad;
192 }
193 else if (strcmp(*argv, "-param_out") == 0)
194 param_out = 1;
4d94ae00
BM
195 else if (strcmp(*argv, "-pubin") == 0)
196 pubin=1;
197 else if (strcmp(*argv, "-pubout") == 0)
198 pubout=1;
199 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
eefa6e4e
BM
200 {
201 BIO_printf(bio_err, "unknown option %s\n", *argv);
4d94ae00
BM
202 badops=1;
203 break;
eefa6e4e 204 }
4d94ae00
BM
205 argc--;
206 argv++;
eefa6e4e 207 }
4d94ae00
BM
208
209 if (badops)
eefa6e4e 210 {
4d94ae00 211bad:
eefa6e4e 212 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
4d94ae00 213 BIO_printf(bio_err, "where options are\n");
eefa6e4e
BM
214 BIO_printf(bio_err, " -inform arg input format - "
215 "DER or PEM\n");
216 BIO_printf(bio_err, " -outform arg output format - "
217 "DER or PEM\n");
4d94ae00 218 BIO_printf(bio_err, " -in arg input file\n");
eefa6e4e
BM
219 BIO_printf(bio_err, " -passin arg input file pass "
220 "phrase source\n");
4d94ae00 221 BIO_printf(bio_err, " -out arg output file\n");
eefa6e4e
BM
222 BIO_printf(bio_err, " -passout arg output file pass "
223 "phrase source\n");
224 BIO_printf(bio_err, " -engine e use engine e, "
225 "possibly a hardware device.\n");
226 BIO_printf(bio_err, " -des encrypt PEM output, "
227 "instead of 'des' every other \n"
228 " cipher "
229 "supported by OpenSSL can be used\n");
230 BIO_printf(bio_err, " -text print the key\n");
4d94ae00 231 BIO_printf(bio_err, " -noout don't print key out\n");
eefa6e4e
BM
232 BIO_printf(bio_err, " -param_out print the elliptic "
233 "curve parameters\n");
234 BIO_printf(bio_err, " -conv_form arg specifies the "
235 "point conversion form \n");
c96f0fd2 236 BIO_printf(bio_err, " possible values:"
eefa6e4e 237 " compressed\n");
c96f0fd2 238 BIO_printf(bio_err, " "
eefa6e4e
BM
239 " uncompressed (default)\n");
240 BIO_printf(bio_err, " "
241 " hybrid\n");
242 BIO_printf(bio_err, " -param_enc arg specifies the way"
243 " the ec parameters are encoded\n");
244 BIO_printf(bio_err, " in the asn1 der "
245 "encoding\n");
c96f0fd2 246 BIO_printf(bio_err, " possilbe values:"
eefa6e4e 247 " named_curve (default)\n");
c96f0fd2 248 BIO_printf(bio_err," "
eefa6e4e 249 "explicit\n");
4d94ae00 250 goto end;
eefa6e4e 251 }
4d94ae00
BM
252
253 ERR_load_crypto_strings();
254
58ae65cd 255#ifndef OPENSSL_NO_ENGINE
4d94ae00 256 e = setup_engine(bio_err, engine, 0);
58ae65cd 257#endif
4d94ae00
BM
258
259 if(!app_passwd(bio_err, passargin, passargout, &passin, &passout))
eefa6e4e 260 {
4d94ae00
BM
261 BIO_printf(bio_err, "Error getting passwords\n");
262 goto end;
eefa6e4e 263 }
4d94ae00
BM
264
265 in = BIO_new(BIO_s_file());
266 out = BIO_new(BIO_s_file());
267 if ((in == NULL) || (out == NULL))
eefa6e4e 268 {
4d94ae00
BM
269 ERR_print_errors(bio_err);
270 goto end;
eefa6e4e 271 }
4d94ae00
BM
272
273 if (infile == NULL)
eefa6e4e 274 BIO_set_fp(in, stdin, BIO_NOCLOSE);
4d94ae00 275 else
4d94ae00 276 {
eefa6e4e
BM
277 if (BIO_read_filename(in, infile) <= 0)
278 {
4d94ae00
BM
279 perror(infile);
280 goto end;
eefa6e4e 281 }
4d94ae00 282 }
4d94ae00 283
d4a8f90c 284 BIO_printf(bio_err, "read EC key\n");
4d94ae00 285 if (informat == FORMAT_ASN1)
eefa6e4e 286 {
4d94ae00 287 if (pubin)
14a7cfb3 288 eckey = d2i_EC_PUBKEY_bio(in, NULL);
4d94ae00 289 else
14a7cfb3 290 eckey = d2i_ECPrivateKey_bio(in, NULL);
eefa6e4e
BM
291 }
292 else if (informat == FORMAT_PEM)
293 {
4d94ae00 294 if (pubin)
14a7cfb3 295 eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL,
eefa6e4e 296 NULL);
4d94ae00 297 else
14a7cfb3 298 eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL,
eefa6e4e
BM
299 passin);
300 }
301 else
302 {
4d94ae00
BM
303 BIO_printf(bio_err, "bad input format specified for key\n");
304 goto end;
eefa6e4e 305 }
14a7cfb3 306 if (eckey == NULL)
eefa6e4e 307 {
4d94ae00
BM
308 BIO_printf(bio_err,"unable to load Key\n");
309 ERR_print_errors(bio_err);
310 goto end;
eefa6e4e 311 }
4d94ae00
BM
312
313 if (outfile == NULL)
eefa6e4e 314 {
4d94ae00
BM
315 BIO_set_fp(out, stdout, BIO_NOCLOSE);
316#ifdef OPENSSL_SYS_VMS
eefa6e4e 317 {
4d94ae00
BM
318 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
319 out = BIO_push(tmpbio, out);
eefa6e4e 320 }
4d94ae00 321#endif
eefa6e4e 322 }
4d94ae00 323 else
4d94ae00 324 {
eefa6e4e
BM
325 if (BIO_write_filename(out, outfile) <= 0)
326 {
4d94ae00
BM
327 perror(outfile);
328 goto end;
eefa6e4e 329 }
4d94ae00 330 }
4d94ae00 331
9dd84053
NL
332 group = EC_KEY_get0_group(eckey);
333
eefa6e4e 334 if (new_form)
9dd84053 335 EC_KEY_set_conv_form(eckey, form);
4d94ae00 336
eefa6e4e 337 if (new_asn1_flag)
9dd84053 338 EC_KEY_set_asn1_flag(eckey, asn1_flag);
eefa6e4e
BM
339
340 if (text)
14a7cfb3 341 if (!EC_KEY_print(out, eckey, 0))
eefa6e4e
BM
342 {
343 perror(outfile);
4d94ae00 344 ERR_print_errors(bio_err);
4d94ae00 345 goto end;
eefa6e4e 346 }
4d94ae00
BM
347
348 if (noout)
349 goto end;
eefa6e4e 350
14a7cfb3 351 BIO_printf(bio_err, "writing EC key\n");
4d94ae00 352 if (outformat == FORMAT_ASN1)
eefa6e4e
BM
353 {
354 if (param_out)
9dd84053 355 i = i2d_ECPKParameters_bio(out, group);
eefa6e4e 356 else if (pubin || pubout)
14a7cfb3 357 i = i2d_EC_PUBKEY_bio(out, eckey);
4d94ae00 358 else
14a7cfb3 359 i = i2d_ECPrivateKey_bio(out, eckey);
eefa6e4e
BM
360 }
361 else if (outformat == FORMAT_PEM)
362 {
363 if (param_out)
9dd84053 364 i = PEM_write_bio_ECPKParameters(out, group);
eefa6e4e 365 else if (pubin || pubout)
14a7cfb3 366 i = PEM_write_bio_EC_PUBKEY(out, eckey);
4d94ae00 367 else
14a7cfb3 368 i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
eefa6e4e
BM
369 NULL, 0, NULL, passout);
370 }
371 else
372 {
373 BIO_printf(bio_err, "bad output format specified for "
374 "outfile\n");
4d94ae00 375 goto end;
eefa6e4e
BM
376 }
377
4d94ae00 378 if (!i)
eefa6e4e 379 {
4d94ae00
BM
380 BIO_printf(bio_err, "unable to write private key\n");
381 ERR_print_errors(bio_err);
eefa6e4e 382 }
4d94ae00
BM
383 else
384 ret=0;
385end:
eefa6e4e
BM
386 if (in)
387 BIO_free(in);
388 if (out)
389 BIO_free_all(out);
14a7cfb3
BM
390 if (eckey)
391 EC_KEY_free(eckey);
eefa6e4e
BM
392 if (passin)
393 OPENSSL_free(passin);
394 if (passout)
395 OPENSSL_free(passout);
4d94ae00 396 apps_shutdown();
1c3e4a36 397 OPENSSL_EXIT(ret);
4d94ae00
BM
398}
399#endif