ec.c 10.4 KB
Newer Older
B
Bodo Möller 已提交
1 2 3 4
/* apps/ec.c */
/*
 * Written by Nils Larsch for the OpenSSL project.
 */
B
Bodo Möller 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* ====================================================================
 * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

B
Bodo Möller 已提交
59
#ifndef OPENSSL_NO_EC
B
Bodo Möller 已提交
60 61 62 63 64 65 66 67 68 69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "apps.h"
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>

#undef PROG
B
Bodo Möller 已提交
70
#define PROG	ec_main
B
Bodo Möller 已提交
71

72 73 74 75 76 77 78 79 80
/* -inform arg    - input format - default PEM (one of DER, NET or PEM)
 * -outform arg   - output format - default PEM
 * -in arg        - input file - default stdin
 * -out arg       - output file - default stdout
 * -des           - encrypt output if PEM format with DES in cbc mode
 * -text          - print a text version
 * -param_out     - print the elliptic curve parameters
 * -conv_form arg - specifies the point encoding form
 * -param_enc arg - specifies the parameter encoding
B
Bodo Möller 已提交
81 82 83 84 85 86 87 88
 */

int MAIN(int, char **);

int MAIN(int argc, char **argv)
{
	ENGINE 	*e = NULL;
	int 	ret = 1;
89
	EC_KEY 	*eckey = NULL;
B
Bodo Möller 已提交
90 91 92 93
	int 	i, badops = 0;
	const EVP_CIPHER *enc = NULL;
	BIO 	*in = NULL, *out = NULL;
	int 	informat, outformat, text=0, noout=0;
94
	int  	pubin = 0, pubout = 0, param_out = 0;
B
Bodo Möller 已提交
95 96 97
	char 	*infile, *outfile, *prog, *engine;
	char 	*passargin = NULL, *passargout = NULL;
	char 	*passin = NULL, *passout = NULL;
98 99 100 101
	point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
	int	new_form = 0;
	int	asn1_flag = OPENSSL_EC_NAMED_CURVE;
	int 	new_asn1_flag = 0;
B
Bodo Möller 已提交
102 103 104 105 106 107 108

	apps_startup();

	if (bio_err == NULL)
		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
			BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);

D
Dr. Stephen Henson 已提交
109 110 111
	if (!load_config(bio_err, NULL))
		goto end;

B
Bodo Möller 已提交
112 113 114 115 116 117 118 119 120 121 122
	engine = NULL;
	infile = NULL;
	outfile = NULL;
	informat = FORMAT_PEM;
	outformat = FORMAT_PEM;

	prog = argv[0];
	argc--;
	argv++;
	while (argc >= 1)
		{
123 124
		if (strcmp(*argv,"-inform") == 0)
			{
B
Bodo Möller 已提交
125 126
			if (--argc < 1) goto bad;
			informat=str2fmt(*(++argv));
127
			}
B
Bodo Möller 已提交
128
		else if (strcmp(*argv,"-outform") == 0)
129
			{
B
Bodo Möller 已提交
130 131
			if (--argc < 1) goto bad;
			outformat=str2fmt(*(++argv));
132
			}
B
Bodo Möller 已提交
133
		else if (strcmp(*argv,"-in") == 0)
134
			{
B
Bodo Möller 已提交
135 136
			if (--argc < 1) goto bad;
			infile= *(++argv);
137
			}
B
Bodo Möller 已提交
138
		else if (strcmp(*argv,"-out") == 0)
139
			{
B
Bodo Möller 已提交
140 141
			if (--argc < 1) goto bad;
			outfile= *(++argv);
142
			}
B
Bodo Möller 已提交
143
		else if (strcmp(*argv,"-passin") == 0)
144
			{
B
Bodo Möller 已提交
145 146
			if (--argc < 1) goto bad;
			passargin= *(++argv);
147
			}
B
Bodo Möller 已提交
148
		else if (strcmp(*argv,"-passout") == 0)
149
			{
B
Bodo Möller 已提交
150 151
			if (--argc < 1) goto bad;
			passargout= *(++argv);
152
			}
B
Bodo Möller 已提交
153
		else if (strcmp(*argv, "-engine") == 0)
154
			{
B
Bodo Möller 已提交
155 156
			if (--argc < 1) goto bad;
			engine= *(++argv);
157
			}
B
Bodo Möller 已提交
158 159 160 161
		else if (strcmp(*argv, "-noout") == 0)
			noout = 1;
		else if (strcmp(*argv, "-text") == 0)
			text = 1;
162
		else if (strcmp(*argv, "-conv_form") == 0)
B
Bodo Möller 已提交
163
			{
164 165 166 167 168 169 170 171 172 173 174 175
			if (--argc < 1)
				goto bad;
			++argv;
			new_form = 1;
			if (strcmp(*argv, "compressed") == 0)
				form = POINT_CONVERSION_COMPRESSED;
			else if (strcmp(*argv, "uncompressed") == 0)
				form = POINT_CONVERSION_UNCOMPRESSED;
			else if (strcmp(*argv, "hybrid") == 0)
				form = POINT_CONVERSION_HYBRID;
			else
				goto bad;
B
Bodo Möller 已提交
176
			}
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		else if (strcmp(*argv, "-param_enc") == 0)
			{
			if (--argc < 1)
				goto bad;
			++argv;
			new_asn1_flag = 1;
			if (strcmp(*argv, "named_curve") == 0)
				asn1_flag = OPENSSL_EC_NAMED_CURVE;
			else if (strcmp(*argv, "explicit") == 0)
				asn1_flag = 0;
			else
				goto bad;
			}
		else if (strcmp(*argv, "-param_out") == 0)
			param_out = 1;
B
Bodo Möller 已提交
192 193 194 195 196
		else if (strcmp(*argv, "-pubin") == 0)
			pubin=1;
		else if (strcmp(*argv, "-pubout") == 0)
			pubout=1;
		else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
197 198
			{
			BIO_printf(bio_err, "unknown option %s\n", *argv);
B
Bodo Möller 已提交
199 200
			badops=1;
			break;
201
			}
B
Bodo Möller 已提交
202 203
		argc--;
		argv++;
204
		}
B
Bodo Möller 已提交
205 206

	if (badops)
207
		{
B
Bodo Möller 已提交
208
bad:
209
		BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
B
Bodo Möller 已提交
210
		BIO_printf(bio_err, "where options are\n");
211 212 213 214
		BIO_printf(bio_err, " -inform arg     input format - "
				"DER or PEM\n");
		BIO_printf(bio_err, " -outform arg    output format - "
				"DER or PEM\n");
B
Bodo Möller 已提交
215
		BIO_printf(bio_err, " -in arg         input file\n");
216 217
		BIO_printf(bio_err, " -passin arg     input file pass "
				"phrase source\n");
B
Bodo Möller 已提交
218
		BIO_printf(bio_err, " -out arg        output file\n");
219 220 221 222 223 224 225 226 227
		BIO_printf(bio_err, " -passout arg    output file pass "
				"phrase source\n");
		BIO_printf(bio_err, " -engine e       use engine e, "
				"possibly a hardware device.\n");
		BIO_printf(bio_err, " -des            encrypt PEM output, "
				"instead of 'des' every other \n"
				"                 cipher "
				"supported by OpenSSL can be used\n");
		BIO_printf(bio_err, " -text           print the key\n");
B
Bodo Möller 已提交
228
		BIO_printf(bio_err, " -noout          don't print key out\n");
229 230 231 232
		BIO_printf(bio_err, " -param_out      print the elliptic "
				"curve parameters\n");
		BIO_printf(bio_err, " -conv_form arg  specifies the "
				"point conversion form \n");
B
Bodo Möller 已提交
233
		BIO_printf(bio_err, "                 possible values:"
234
				" compressed\n");
B
Bodo Möller 已提交
235
		BIO_printf(bio_err, "                                 "
236 237 238 239 240 241 242
				" uncompressed (default)\n");
		BIO_printf(bio_err, "                                  "
				" hybrid\n");
		BIO_printf(bio_err, " -param_enc arg  specifies the way"
				" the ec parameters are encoded\n");
		BIO_printf(bio_err, "                 in the asn1 der "
				"encoding\n");
B
Bodo Möller 已提交
243
		BIO_printf(bio_err, "                 possilbe values:"
244
				" named_curve (default)\n");
B
Bodo Möller 已提交
245
		BIO_printf(bio_err,"                                  "
246
				"explicit\n");
B
Bodo Möller 已提交
247
		goto end;
248
		}
B
Bodo Möller 已提交
249 250 251 252 253 254

	ERR_load_crypto_strings();

        e = setup_engine(bio_err, engine, 0);

	if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) 
255
		{
B
Bodo Möller 已提交
256 257
		BIO_printf(bio_err, "Error getting passwords\n");
		goto end;
258
		}
B
Bodo Möller 已提交
259 260 261 262

	in = BIO_new(BIO_s_file());
	out = BIO_new(BIO_s_file());
	if ((in == NULL) || (out == NULL))
263
		{
B
Bodo Möller 已提交
264 265
		ERR_print_errors(bio_err);
		goto end;
266
		}
B
Bodo Möller 已提交
267 268

	if (infile == NULL)
269
		BIO_set_fp(in, stdin, BIO_NOCLOSE);
B
Bodo Möller 已提交
270 271
	else
		{
272 273
		if (BIO_read_filename(in, infile) <= 0)
			{
B
Bodo Möller 已提交
274 275
			perror(infile);
			goto end;
276
			}
B
Bodo Möller 已提交
277 278
		}

B
Bodo Möller 已提交
279
	BIO_printf(bio_err, "read EC key\n");
B
Bodo Möller 已提交
280
	if (informat == FORMAT_ASN1) 
281
		{
B
Bodo Möller 已提交
282
		if (pubin) 
283
			eckey = d2i_EC_PUBKEY_bio(in, NULL);
B
Bodo Möller 已提交
284
		else 
285
			eckey = d2i_ECPrivateKey_bio(in, NULL);
286 287 288
		} 
	else if (informat == FORMAT_PEM) 
		{
B
Bodo Möller 已提交
289
		if (pubin) 
290
			eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, 
291
				NULL);
B
Bodo Möller 已提交
292
		else 
293
			eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL,
294 295 296 297
				passin);
		} 
	else
		{
B
Bodo Möller 已提交
298 299
		BIO_printf(bio_err, "bad input format specified for key\n");
		goto end;
300
		}
301
	if (eckey == NULL)
302
		{
B
Bodo Möller 已提交
303 304 305
		BIO_printf(bio_err,"unable to load Key\n");
		ERR_print_errors(bio_err);
		goto end;
306
		}
B
Bodo Möller 已提交
307 308

	if (outfile == NULL)
309
		{
B
Bodo Möller 已提交
310 311
		BIO_set_fp(out, stdout, BIO_NOCLOSE);
#ifdef OPENSSL_SYS_VMS
312
			{
B
Bodo Möller 已提交
313 314
			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
			out = BIO_push(tmpbio, out);
315
			}
B
Bodo Möller 已提交
316
#endif
317
		}
B
Bodo Möller 已提交
318 319
	else
		{
320 321
		if (BIO_write_filename(out, outfile) <= 0)
			{
B
Bodo Möller 已提交
322 323
			perror(outfile);
			goto end;
324
			}
B
Bodo Möller 已提交
325 326
		}

327
	if (new_form)
B
Bodo Möller 已提交
328
		{
329 330
		EC_GROUP_set_point_conversion_form(eckey->group, form);
		eckey->conv_form = form;
B
Bodo Möller 已提交
331 332
		}

333
	if (new_asn1_flag)
334
		EC_GROUP_set_asn1_flag(eckey->group, asn1_flag);
335 336

	if (text) 
337
		if (!EC_KEY_print(out, eckey, 0))
338 339
			{
			perror(outfile);
B
Bodo Möller 已提交
340 341
			ERR_print_errors(bio_err);
			goto end;
342
			}
B
Bodo Möller 已提交
343 344 345

	if (noout) 
		goto end;
346

347
	BIO_printf(bio_err, "writing EC key\n");
B
Bodo Möller 已提交
348
	if (outformat == FORMAT_ASN1) 
349 350
		{
		if (param_out)
351
			i = i2d_ECPKParameters_bio(out, eckey->group);
352
		else if (pubin || pubout) 
353
			i = i2d_EC_PUBKEY_bio(out, eckey);
B
Bodo Möller 已提交
354
		else 
355
			i = i2d_ECPrivateKey_bio(out, eckey);
356 357 358 359
		} 
	else if (outformat == FORMAT_PEM) 
		{
		if (param_out)
360
			i = PEM_write_bio_ECPKParameters(out, eckey->group);
361
		else if (pubin || pubout)
362
			i = PEM_write_bio_EC_PUBKEY(out, eckey);
B
Bodo Möller 已提交
363
		else 
364
			i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
365 366 367 368 369 370
						NULL, 0, NULL, passout);
		} 
	else 
		{
		BIO_printf(bio_err, "bad output format specified for "
			"outfile\n");
B
Bodo Möller 已提交
371
		goto end;
372 373
		}

B
Bodo Möller 已提交
374
	if (!i)
375
		{
B
Bodo Möller 已提交
376 377
		BIO_printf(bio_err, "unable to write private key\n");
		ERR_print_errors(bio_err);
378
		}
B
Bodo Möller 已提交
379 380 381
	else
		ret=0;
end:
382 383 384 385
	if (in)
		BIO_free(in);
	if (out)
		BIO_free_all(out);
386 387
	if (eckey)
		EC_KEY_free(eckey);
388 389 390 391
	if (passin)
		OPENSSL_free(passin);
	if (passout)
		OPENSSL_free(passout);
B
Bodo Möller 已提交
392 393 394 395
	apps_shutdown();
	EXIT(ret);
}
#endif