]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ec.c
More tweaks for comments due indent issues
[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
6e04afb8 59#include <openssl/opensslconf.h>
d4a8f90c 60#ifndef OPENSSL_NO_EC
4d94ae00
BM
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
4d94ae00
BM
64#include "apps.h"
65#include <openssl/bio.h>
66#include <openssl/err.h>
4d94ae00 67#include <openssl/evp.h>
4d94ae00
BM
68#include <openssl/pem.h>
69
70#undef PROG
d4a8f90c 71#define PROG ec_main
4d94ae00 72
6977c7e2
TH
73/*-
74 * -inform arg - input format - default PEM (one of DER, NET or PEM)
eefa6e4e
BM
75 * -outform arg - output format - default PEM
76 * -in arg - input file - default stdin
77 * -out arg - output file - default stdout
78 * -des - encrypt output if PEM format with DES in cbc mode
79 * -text - print a text version
80 * -param_out - print the elliptic curve parameters
81 * -conv_form arg - specifies the point encoding form
82 * -param_enc arg - specifies the parameter encoding
4d94ae00
BM
83 */
84
85int MAIN(int, char **);
86
87int MAIN(int argc, char **argv)
88{
4d94ae00 89 int ret = 1;
14a7cfb3 90 EC_KEY *eckey = NULL;
9dd84053 91 const EC_GROUP *group;
4d94ae00
BM
92 int i, badops = 0;
93 const EVP_CIPHER *enc = NULL;
94 BIO *in = NULL, *out = NULL;
95 int informat, outformat, text=0, noout=0;
eefa6e4e 96 int pubin = 0, pubout = 0, param_out = 0;
4d94ae00
BM
97 char *infile, *outfile, *prog, *engine;
98 char *passargin = NULL, *passargout = NULL;
99 char *passin = NULL, *passout = NULL;
eefa6e4e
BM
100 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
101 int new_form = 0;
102 int asn1_flag = OPENSSL_EC_NAMED_CURVE;
103 int new_asn1_flag = 0;
4d94ae00
BM
104
105 apps_startup();
106
107 if (bio_err == NULL)
108 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
109 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
110
3647bee2
DSH
111 if (!load_config(bio_err, NULL))
112 goto end;
113
4d94ae00
BM
114 engine = NULL;
115 infile = NULL;
116 outfile = NULL;
117 informat = FORMAT_PEM;
118 outformat = FORMAT_PEM;
119
120 prog = argv[0];
121 argc--;
122 argv++;
123 while (argc >= 1)
4d94ae00 124 {
eefa6e4e
BM
125 if (strcmp(*argv,"-inform") == 0)
126 {
4d94ae00
BM
127 if (--argc < 1) goto bad;
128 informat=str2fmt(*(++argv));
eefa6e4e 129 }
4d94ae00 130 else if (strcmp(*argv,"-outform") == 0)
eefa6e4e 131 {
4d94ae00
BM
132 if (--argc < 1) goto bad;
133 outformat=str2fmt(*(++argv));
eefa6e4e 134 }
4d94ae00 135 else if (strcmp(*argv,"-in") == 0)
eefa6e4e 136 {
4d94ae00
BM
137 if (--argc < 1) goto bad;
138 infile= *(++argv);
eefa6e4e 139 }
4d94ae00 140 else if (strcmp(*argv,"-out") == 0)
eefa6e4e 141 {
4d94ae00
BM
142 if (--argc < 1) goto bad;
143 outfile= *(++argv);
eefa6e4e 144 }
4d94ae00 145 else if (strcmp(*argv,"-passin") == 0)
eefa6e4e 146 {
4d94ae00
BM
147 if (--argc < 1) goto bad;
148 passargin= *(++argv);
eefa6e4e 149 }
4d94ae00 150 else if (strcmp(*argv,"-passout") == 0)
eefa6e4e 151 {
4d94ae00
BM
152 if (--argc < 1) goto bad;
153 passargout= *(++argv);
eefa6e4e 154 }
4d94ae00 155 else if (strcmp(*argv, "-engine") == 0)
eefa6e4e 156 {
4d94ae00
BM
157 if (--argc < 1) goto bad;
158 engine= *(++argv);
eefa6e4e 159 }
4d94ae00
BM
160 else if (strcmp(*argv, "-noout") == 0)
161 noout = 1;
162 else if (strcmp(*argv, "-text") == 0)
163 text = 1;
eefa6e4e 164 else if (strcmp(*argv, "-conv_form") == 0)
4d94ae00 165 {
eefa6e4e
BM
166 if (--argc < 1)
167 goto bad;
168 ++argv;
169 new_form = 1;
170 if (strcmp(*argv, "compressed") == 0)
171 form = POINT_CONVERSION_COMPRESSED;
172 else if (strcmp(*argv, "uncompressed") == 0)
173 form = POINT_CONVERSION_UNCOMPRESSED;
174 else if (strcmp(*argv, "hybrid") == 0)
175 form = POINT_CONVERSION_HYBRID;
176 else
177 goto bad;
4d94ae00 178 }
eefa6e4e
BM
179 else if (strcmp(*argv, "-param_enc") == 0)
180 {
181 if (--argc < 1)
182 goto bad;
183 ++argv;
184 new_asn1_flag = 1;
185 if (strcmp(*argv, "named_curve") == 0)
186 asn1_flag = OPENSSL_EC_NAMED_CURVE;
187 else if (strcmp(*argv, "explicit") == 0)
188 asn1_flag = 0;
189 else
190 goto bad;
191 }
192 else if (strcmp(*argv, "-param_out") == 0)
193 param_out = 1;
4d94ae00
BM
194 else if (strcmp(*argv, "-pubin") == 0)
195 pubin=1;
196 else if (strcmp(*argv, "-pubout") == 0)
197 pubout=1;
198 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
eefa6e4e
BM
199 {
200 BIO_printf(bio_err, "unknown option %s\n", *argv);
4d94ae00
BM
201 badops=1;
202 break;
eefa6e4e 203 }
4d94ae00
BM
204 argc--;
205 argv++;
eefa6e4e 206 }
4d94ae00
BM
207
208 if (badops)
eefa6e4e 209 {
4d94ae00 210bad:
eefa6e4e 211 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
4d94ae00 212 BIO_printf(bio_err, "where options are\n");
eefa6e4e
BM
213 BIO_printf(bio_err, " -inform arg input format - "
214 "DER or PEM\n");
215 BIO_printf(bio_err, " -outform arg output format - "
216 "DER or PEM\n");
4d94ae00 217 BIO_printf(bio_err, " -in arg input file\n");
eefa6e4e
BM
218 BIO_printf(bio_err, " -passin arg input file pass "
219 "phrase source\n");
4d94ae00 220 BIO_printf(bio_err, " -out arg output file\n");
eefa6e4e
BM
221 BIO_printf(bio_err, " -passout arg output file pass "
222 "phrase source\n");
223 BIO_printf(bio_err, " -engine e use engine e, "
224 "possibly a hardware device.\n");
225 BIO_printf(bio_err, " -des encrypt PEM output, "
226 "instead of 'des' every other \n"
227 " cipher "
228 "supported by OpenSSL can be used\n");
229 BIO_printf(bio_err, " -text print the key\n");
4d94ae00 230 BIO_printf(bio_err, " -noout don't print key out\n");
eefa6e4e
BM
231 BIO_printf(bio_err, " -param_out print the elliptic "
232 "curve parameters\n");
233 BIO_printf(bio_err, " -conv_form arg specifies the "
234 "point conversion form \n");
c96f0fd2 235 BIO_printf(bio_err, " possible values:"
eefa6e4e 236 " compressed\n");
c96f0fd2 237 BIO_printf(bio_err, " "
eefa6e4e
BM
238 " uncompressed (default)\n");
239 BIO_printf(bio_err, " "
240 " hybrid\n");
241 BIO_printf(bio_err, " -param_enc arg specifies the way"
242 " the ec parameters are encoded\n");
243 BIO_printf(bio_err, " in the asn1 der "
244 "encoding\n");
15bd07e9 245 BIO_printf(bio_err, " possible values:"
eefa6e4e 246 " named_curve (default)\n");
c96f0fd2 247 BIO_printf(bio_err," "
eefa6e4e 248 "explicit\n");
4d94ae00 249 goto end;
eefa6e4e 250 }
4d94ae00
BM
251
252 ERR_load_crypto_strings();
253
58ae65cd 254#ifndef OPENSSL_NO_ENGINE
e9735943 255 setup_engine(bio_err, engine, 0);
58ae65cd 256#endif
4d94ae00
BM
257
258 if(!app_passwd(bio_err, passargin, passargout, &passin, &passout))
eefa6e4e 259 {
4d94ae00
BM
260 BIO_printf(bio_err, "Error getting passwords\n");
261 goto end;
eefa6e4e 262 }
4d94ae00
BM
263
264 in = BIO_new(BIO_s_file());
265 out = BIO_new(BIO_s_file());
266 if ((in == NULL) || (out == NULL))
eefa6e4e 267 {
4d94ae00
BM
268 ERR_print_errors(bio_err);
269 goto end;
eefa6e4e 270 }
4d94ae00
BM
271
272 if (infile == NULL)
eefa6e4e 273 BIO_set_fp(in, stdin, BIO_NOCLOSE);
4d94ae00 274 else
4d94ae00 275 {
eefa6e4e
BM
276 if (BIO_read_filename(in, infile) <= 0)
277 {
4d94ae00
BM
278 perror(infile);
279 goto end;
eefa6e4e 280 }
4d94ae00 281 }
4d94ae00 282
d4a8f90c 283 BIO_printf(bio_err, "read EC key\n");
4d94ae00 284 if (informat == FORMAT_ASN1)
eefa6e4e 285 {
4d94ae00 286 if (pubin)
14a7cfb3 287 eckey = d2i_EC_PUBKEY_bio(in, NULL);
4d94ae00 288 else
14a7cfb3 289 eckey = d2i_ECPrivateKey_bio(in, NULL);
eefa6e4e
BM
290 }
291 else if (informat == FORMAT_PEM)
292 {
4d94ae00 293 if (pubin)
14a7cfb3 294 eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL,
eefa6e4e 295 NULL);
4d94ae00 296 else
14a7cfb3 297 eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL,
eefa6e4e
BM
298 passin);
299 }
300 else
301 {
4d94ae00
BM
302 BIO_printf(bio_err, "bad input format specified for key\n");
303 goto end;
eefa6e4e 304 }
14a7cfb3 305 if (eckey == NULL)
eefa6e4e 306 {
4d94ae00
BM
307 BIO_printf(bio_err,"unable to load Key\n");
308 ERR_print_errors(bio_err);
309 goto end;
eefa6e4e 310 }
4d94ae00
BM
311
312 if (outfile == NULL)
eefa6e4e 313 {
4d94ae00
BM
314 BIO_set_fp(out, stdout, BIO_NOCLOSE);
315#ifdef OPENSSL_SYS_VMS
eefa6e4e 316 {
4d94ae00
BM
317 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
318 out = BIO_push(tmpbio, out);
eefa6e4e 319 }
4d94ae00 320#endif
eefa6e4e 321 }
4d94ae00 322 else
4d94ae00 323 {
eefa6e4e
BM
324 if (BIO_write_filename(out, outfile) <= 0)
325 {
4d94ae00
BM
326 perror(outfile);
327 goto end;
eefa6e4e 328 }
4d94ae00 329 }
4d94ae00 330
9dd84053
NL
331 group = EC_KEY_get0_group(eckey);
332
eefa6e4e 333 if (new_form)
9dd84053 334 EC_KEY_set_conv_form(eckey, form);
4d94ae00 335
eefa6e4e 336 if (new_asn1_flag)
9dd84053 337 EC_KEY_set_asn1_flag(eckey, asn1_flag);
eefa6e4e
BM
338
339 if (text)
14a7cfb3 340 if (!EC_KEY_print(out, eckey, 0))
eefa6e4e
BM
341 {
342 perror(outfile);
4d94ae00 343 ERR_print_errors(bio_err);
4d94ae00 344 goto end;
eefa6e4e 345 }
4d94ae00
BM
346
347 if (noout)
10a10fb8
NL
348 {
349 ret = 0;
4d94ae00 350 goto end;
10a10fb8 351 }
eefa6e4e 352
14a7cfb3 353 BIO_printf(bio_err, "writing EC key\n");
4d94ae00 354 if (outformat == FORMAT_ASN1)
eefa6e4e
BM
355 {
356 if (param_out)
9dd84053 357 i = i2d_ECPKParameters_bio(out, group);
eefa6e4e 358 else if (pubin || pubout)
14a7cfb3 359 i = i2d_EC_PUBKEY_bio(out, eckey);
4d94ae00 360 else
14a7cfb3 361 i = i2d_ECPrivateKey_bio(out, eckey);
eefa6e4e
BM
362 }
363 else if (outformat == FORMAT_PEM)
364 {
365 if (param_out)
9dd84053 366 i = PEM_write_bio_ECPKParameters(out, group);
eefa6e4e 367 else if (pubin || pubout)
14a7cfb3 368 i = PEM_write_bio_EC_PUBKEY(out, eckey);
4d94ae00 369 else
14a7cfb3 370 i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
eefa6e4e
BM
371 NULL, 0, NULL, passout);
372 }
373 else
374 {
375 BIO_printf(bio_err, "bad output format specified for "
376 "outfile\n");
4d94ae00 377 goto end;
eefa6e4e
BM
378 }
379
4d94ae00 380 if (!i)
eefa6e4e 381 {
4d94ae00
BM
382 BIO_printf(bio_err, "unable to write private key\n");
383 ERR_print_errors(bio_err);
eefa6e4e 384 }
4d94ae00
BM
385 else
386 ret=0;
387end:
eefa6e4e
BM
388 if (in)
389 BIO_free(in);
390 if (out)
391 BIO_free_all(out);
14a7cfb3
BM
392 if (eckey)
393 EC_KEY_free(eckey);
eefa6e4e
BM
394 if (passin)
395 OPENSSL_free(passin);
396 if (passout)
397 OPENSSL_free(passout);
4d94ae00 398 apps_shutdown();
1c3e4a36 399 OPENSSL_EXIT(ret);
4d94ae00 400}
fe41d985
DSH
401#else /* !OPENSSL_NO_EC */
402
403# if PEDANTIC
404static void *dummy=&dummy;
405# endif
406
4d94ae00 407#endif