Annotation of src/usr.bin/openssl/ecparam.c, Revision 1.10
1.10 ! doug 1: /* $OpenBSD: ecparam.c,v 1.9 2015/06/20 14:24:49 jsing Exp $ */
1.1 jsing 2: /*
3: * Written by Nils Larsch for the OpenSSL project.
4: */
5: /* ====================================================================
6: * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
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: */
58: /* ====================================================================
59: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60: *
61: * Portions of the attached software ("Contribution") are developed by
62: * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63: *
64: * The Contribution is licensed pursuant to the OpenSSL open source
65: * license provided above.
66: *
67: * The elliptic curve binary polynomial software is originally written by
68: * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69: *
70: */
71:
72: #include <openssl/opensslconf.h>
73:
74: #ifndef OPENSSL_NO_EC
75:
76: #include <stdio.h>
77: #include <stdlib.h>
78: #include <string.h>
79: #include <time.h>
80:
81: #include "apps.h"
82:
83: #include <openssl/bio.h>
84: #include <openssl/bn.h>
85: #include <openssl/ec.h>
86: #include <openssl/err.h>
87: #include <openssl/pem.h>
88: #include <openssl/x509.h>
89:
1.3 jsing 90: static int ecparam_print_var(BIO *, BIGNUM *, const char *, int,
91: unsigned char *);
92:
93: static struct {
94: int C;
95: int asn1_flag;
96: int check;
97: char *curve_name;
98: char *engine;
99: point_conversion_form_t form;
100: int genkey;
101: char *infile;
102: int informat;
103: int list_curves;
104: int new_asn1_flag;
105: int new_form;
106: int no_seed;
107: int noout;
108: char *outfile;
109: int outformat;
110: int text;
111: } ecparam_config;
112:
113: static int
1.7 jsing 114: ecparam_opt_form(char *arg)
1.3 jsing 115: {
116: if (strcmp(arg, "compressed") == 0)
117: ecparam_config.form = POINT_CONVERSION_COMPRESSED;
118: else if (strcmp(arg, "uncompressed") == 0)
119: ecparam_config.form = POINT_CONVERSION_UNCOMPRESSED;
120: else if (strcmp(arg, "hybrid") == 0)
121: ecparam_config.form = POINT_CONVERSION_HYBRID;
122: else
123: return (1);
124:
125: ecparam_config.new_form = 1;
126: return (0);
127: }
1.1 jsing 128:
1.3 jsing 129: static int
1.7 jsing 130: ecparam_opt_enctype(char *arg)
1.3 jsing 131: {
132: if (strcmp(arg, "explicit") == 0)
133: ecparam_config.asn1_flag = 0;
134: else if (strcmp(arg, "named_curve") == 0)
135: ecparam_config.asn1_flag = OPENSSL_EC_NAMED_CURVE;
136: else
137: return (1);
138:
139: ecparam_config.new_asn1_flag = 1;
140: return (0);
141: }
142:
143: struct option ecparam_options[] = {
144: {
145: .name = "C",
146: .desc = "Convert the EC parameters into C code",
147: .type = OPTION_FLAG,
148: .opt.flag = &ecparam_config.C,
149: },
150: {
151: .name = "check",
152: .desc = "Validate the elliptic curve parameters",
153: .type = OPTION_FLAG,
154: .opt.flag = &ecparam_config.check,
155: },
156: {
157: .name = "conv_form",
158: .argname = "form",
159: .desc = "Specify point conversion form:\n"
160: " compressed, uncompressed (default), hybrid",
161: .type = OPTION_ARG_FUNC,
1.7 jsing 162: .opt.argfunc = ecparam_opt_form,
1.3 jsing 163: },
164: #ifndef OPENSSL_NO_ENGINE
165: {
166: .name = "engine",
167: .argname = "id",
168: .desc = "Use the engine specified by the given identifier",
169: .type = OPTION_ARG,
170: .opt.arg = &ecparam_config.engine,
171: },
172: #endif
173: {
174: .name = "genkey",
175: .desc = "Generate an EC private key using the specified "
176: "parameters",
177: .type = OPTION_FLAG,
178: .opt.flag = &ecparam_config.genkey,
179: },
180: {
181: .name = "in",
182: .argname = "file",
183: .desc = "Input file to read parameters from (default stdin)",
184: .type = OPTION_ARG,
185: .opt.arg = &ecparam_config.infile,
186: },
187: {
188: .name = "inform",
189: .argname = "format",
190: .desc = "Input format (DER or PEM)",
191: .type = OPTION_ARG_FORMAT,
192: .opt.value = &ecparam_config.informat,
193: },
194: {
195: .name = "list_curves",
196: .desc = "Print list of all currently implemented EC "
197: "parameter names",
198: .type = OPTION_FLAG,
199: .opt.flag = &ecparam_config.list_curves,
200: },
201: {
202: .name = "name",
203: .argname = "curve",
204: .desc = "Use the EC parameters with the specified name",
205: .type = OPTION_ARG,
206: .opt.arg = &ecparam_config.curve_name,
207: },
208: {
209: .name = "no_seed",
210: .desc = "Do not output seed with explicit parameter encoding",
211: .type = OPTION_FLAG,
212: .opt.flag = &ecparam_config.no_seed,
213: },
214: {
215: .name = "noout",
216: .desc = "Do not output encoded version of EC parameters",
217: .type = OPTION_FLAG,
218: .opt.flag = &ecparam_config.noout,
219: },
220: {
221: .name = "out",
222: .argname = "file",
223: .desc = "Output file to write parameters to (default stdout)",
224: .type = OPTION_ARG,
225: .opt.arg = &ecparam_config.outfile,
226: },
227: {
228: .name = "outform",
229: .argname = "format",
230: .desc = "Output format (DER or PEM)",
231: .type = OPTION_ARG_FORMAT,
232: .opt.value = &ecparam_config.outformat,
233: },
234: {
235: .name = "param_enc",
236: .argname = "type",
237: .desc = "Specify EC parameter ASN.1 encoding type:\n"
238: " explicit, named_curve (default)",
239: .type = OPTION_ARG_FUNC,
1.7 jsing 240: .opt.argfunc = ecparam_opt_enctype,
1.3 jsing 241: },
242: {
243: .name = "text",
244: .desc = "Print out the EC parameters in human readable form",
245: .type = OPTION_FLAG,
246: .opt.flag = &ecparam_config.text,
247: },
1.4 bcook 248: {NULL},
1.3 jsing 249: };
1.1 jsing 250:
1.3 jsing 251: static void
252: ecparam_usage(void)
253: {
254: fprintf(stderr, "usage: ecparam [-C] [-check] [-conv_form arg] "
255: "[-engine id] [-genkey]\n"
256: " [-in file] [-inform DER | PEM] [-list_curves] [-name arg]\n"
257: " [-no_seed] [-noout] [-out file] [-outform DER | PEM]\n"
258: " [-param_enc arg] [-text]\n\n");
259: options_usage(ecparam_options);
260: }
1.1 jsing 261:
262: int ecparam_main(int, char **);
263:
264: int
265: ecparam_main(int argc, char **argv)
266: {
1.3 jsing 267: BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL, *ec_gen = NULL;
268: BIGNUM *ec_order = NULL, *ec_cofactor = NULL;
1.1 jsing 269: EC_GROUP *group = NULL;
1.3 jsing 270: unsigned char *buffer = NULL;
1.1 jsing 271: BIO *in = NULL, *out = NULL;
1.3 jsing 272: int i, ret = 1;
1.1 jsing 273:
1.3 jsing 274: memset(&ecparam_config, 0, sizeof(ecparam_config));
275: ecparam_config.asn1_flag = OPENSSL_EC_NAMED_CURVE;
276: ecparam_config.form = POINT_CONVERSION_UNCOMPRESSED;
277: ecparam_config.informat = FORMAT_PEM;
278: ecparam_config.outformat = FORMAT_PEM;
1.1 jsing 279:
1.8 jsing 280: if (options_parse(argc, argv, ecparam_options, NULL, NULL) != 0) {
1.3 jsing 281: ecparam_usage();
1.1 jsing 282: goto end;
283: }
284:
285: in = BIO_new(BIO_s_file());
286: out = BIO_new(BIO_s_file());
287: if ((in == NULL) || (out == NULL)) {
288: ERR_print_errors(bio_err);
289: goto end;
290: }
1.3 jsing 291: if (ecparam_config.infile == NULL)
1.1 jsing 292: BIO_set_fp(in, stdin, BIO_NOCLOSE);
293: else {
1.3 jsing 294: if (BIO_read_filename(in, ecparam_config.infile) <= 0) {
295: perror(ecparam_config.infile);
1.1 jsing 296: goto end;
297: }
298: }
1.3 jsing 299: if (ecparam_config.outfile == NULL) {
1.1 jsing 300: BIO_set_fp(out, stdout, BIO_NOCLOSE);
301: } else {
1.3 jsing 302: if (BIO_write_filename(out, ecparam_config.outfile) <= 0) {
303: perror(ecparam_config.outfile);
1.1 jsing 304: goto end;
305: }
306: }
307:
308: #ifndef OPENSSL_NO_ENGINE
1.3 jsing 309: setup_engine(bio_err, ecparam_config.engine, 0);
1.1 jsing 310: #endif
311:
1.3 jsing 312: if (ecparam_config.list_curves) {
1.1 jsing 313: EC_builtin_curve *curves = NULL;
314: size_t crv_len = 0;
315: size_t n = 0;
316:
317: crv_len = EC_get_builtin_curves(NULL, 0);
318:
319: curves = reallocarray(NULL, crv_len, sizeof(EC_builtin_curve));
320: if (curves == NULL)
321: goto end;
322:
323: if (!EC_get_builtin_curves(curves, crv_len)) {
324: free(curves);
325: goto end;
326: }
327: for (n = 0; n < crv_len; n++) {
328: const char *comment;
329: const char *sname;
330: comment = curves[n].comment;
331: sname = OBJ_nid2sn(curves[n].nid);
332: if (comment == NULL)
333: comment = "CURVE DESCRIPTION NOT AVAILABLE";
334: if (sname == NULL)
335: sname = "";
336:
337: BIO_printf(out, " %-10s: ", sname);
338: BIO_printf(out, "%s\n", comment);
339: }
340:
341: free(curves);
342: ret = 0;
343: goto end;
344: }
1.3 jsing 345: if (ecparam_config.curve_name != NULL) {
1.1 jsing 346: int nid;
347:
348: /*
349: * workaround for the SECG curve names secp192r1 and
350: * secp256r1 (which are the same as the curves prime192v1 and
351: * prime256v1 defined in X9.62)
352: */
1.3 jsing 353: if (!strcmp(ecparam_config.curve_name, "secp192r1")) {
1.1 jsing 354: BIO_printf(bio_err, "using curve name prime192v1 "
355: "instead of secp192r1\n");
356: nid = NID_X9_62_prime192v1;
1.3 jsing 357: } else if (!strcmp(ecparam_config.curve_name, "secp256r1")) {
1.1 jsing 358: BIO_printf(bio_err, "using curve name prime256v1 "
359: "instead of secp256r1\n");
360: nid = NID_X9_62_prime256v1;
361: } else
1.3 jsing 362: nid = OBJ_sn2nid(ecparam_config.curve_name);
1.9 jsing 363:
364: if (nid == 0)
365: nid = EC_curve_nist2nid(ecparam_config.curve_name);
1.1 jsing 366:
367: if (nid == 0) {
368: BIO_printf(bio_err, "unknown curve name (%s)\n",
1.3 jsing 369: ecparam_config.curve_name);
1.1 jsing 370: goto end;
371: }
372: group = EC_GROUP_new_by_curve_name(nid);
373: if (group == NULL) {
374: BIO_printf(bio_err, "unable to create curve (%s)\n",
1.3 jsing 375: ecparam_config.curve_name);
1.1 jsing 376: goto end;
377: }
1.3 jsing 378: EC_GROUP_set_asn1_flag(group, ecparam_config.asn1_flag);
379: EC_GROUP_set_point_conversion_form(group, ecparam_config.form);
380: } else if (ecparam_config.informat == FORMAT_ASN1) {
1.1 jsing 381: group = d2i_ECPKParameters_bio(in, NULL);
1.3 jsing 382: } else if (ecparam_config.informat == FORMAT_PEM) {
1.1 jsing 383: group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
384: } else {
385: BIO_printf(bio_err, "bad input format specified\n");
386: goto end;
387: }
388:
389: if (group == NULL) {
390: BIO_printf(bio_err,
391: "unable to load elliptic curve parameters\n");
392: ERR_print_errors(bio_err);
393: goto end;
394: }
1.3 jsing 395: if (ecparam_config.new_form)
396: EC_GROUP_set_point_conversion_form(group, ecparam_config.form);
1.1 jsing 397:
1.3 jsing 398: if (ecparam_config.new_asn1_flag)
399: EC_GROUP_set_asn1_flag(group, ecparam_config.asn1_flag);
1.1 jsing 400:
1.3 jsing 401: if (ecparam_config.no_seed)
1.1 jsing 402: EC_GROUP_set_seed(group, NULL, 0);
1.3 jsing 403:
404: if (ecparam_config.text) {
1.1 jsing 405: if (!ECPKParameters_print(out, group, 0))
406: goto end;
407: }
1.3 jsing 408: if (ecparam_config.check) {
1.1 jsing 409: BIO_printf(bio_err, "checking elliptic curve parameters: ");
410: if (!EC_GROUP_check(group, NULL)) {
411: BIO_printf(bio_err, "failed\n");
412: ERR_print_errors(bio_err);
413: } else
414: BIO_printf(bio_err, "ok\n");
415:
416: }
1.3 jsing 417: if (ecparam_config.C) {
1.1 jsing 418: size_t buf_len = 0, tmp_len = 0;
419: const EC_POINT *point;
420: int is_prime, len = 0;
421: const EC_METHOD *meth = EC_GROUP_method_of(group);
422:
423: if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
424: (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
425: (ec_order = BN_new()) == NULL ||
426: (ec_cofactor = BN_new()) == NULL) {
427: perror("malloc");
428: goto end;
429: }
430: is_prime = (EC_METHOD_get_field_type(meth) ==
431: NID_X9_62_prime_field);
432:
433: if (is_prime) {
434: if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
1.3 jsing 435: ec_b, NULL))
1.1 jsing 436: goto end;
437: } else {
1.5 jsing 438: if (!EC_GROUP_get_curve_GF2m(group, ec_p, ec_a,
439: ec_b, NULL))
440: goto end;
1.1 jsing 441: }
442:
443: if ((point = EC_GROUP_get0_generator(group)) == NULL)
444: goto end;
445: if (!EC_POINT_point2bn(group, point,
446: EC_GROUP_get_point_conversion_form(group), ec_gen,
447: NULL))
448: goto end;
449: if (!EC_GROUP_get_order(group, ec_order, NULL))
450: goto end;
451: if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
452: goto end;
453:
454: if (!ec_p || !ec_a || !ec_b || !ec_gen ||
455: !ec_order || !ec_cofactor)
456: goto end;
457:
458: len = BN_num_bits(ec_order);
459:
460: if ((tmp_len = (size_t) BN_num_bytes(ec_p)) > buf_len)
461: buf_len = tmp_len;
462: if ((tmp_len = (size_t) BN_num_bytes(ec_a)) > buf_len)
463: buf_len = tmp_len;
464: if ((tmp_len = (size_t) BN_num_bytes(ec_b)) > buf_len)
465: buf_len = tmp_len;
466: if ((tmp_len = (size_t) BN_num_bytes(ec_gen)) > buf_len)
467: buf_len = tmp_len;
468: if ((tmp_len = (size_t) BN_num_bytes(ec_order)) > buf_len)
469: buf_len = tmp_len;
470: if ((tmp_len = (size_t) BN_num_bytes(ec_cofactor)) > buf_len)
471: buf_len = tmp_len;
472:
473: buffer = malloc(buf_len);
474:
475: if (buffer == NULL) {
476: perror("malloc");
477: goto end;
478: }
479: ecparam_print_var(out, ec_p, "ec_p", len, buffer);
480: ecparam_print_var(out, ec_a, "ec_a", len, buffer);
481: ecparam_print_var(out, ec_b, "ec_b", len, buffer);
482: ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
483: ecparam_print_var(out, ec_order, "ec_order", len, buffer);
484: ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
485: buffer);
486:
487: BIO_printf(out, "\n\n");
488:
489: BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
490: BIO_printf(out, "\tint ok=0;\n");
491: BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
492: BIO_printf(out, "\tEC_POINT *point = NULL;\n");
493: BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
494: "*tmp_3 = NULL;\n\n");
495: BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
496: "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
497: "goto err;\n", len, len);
498: BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
499: "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
500: "goto err;\n", len, len);
501: BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
502: "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
503: "goto err;\n", len, len);
504: if (is_prime) {
505: BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
506: "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
507: "\n\t\tgoto err;\n\n");
508: } else {
1.5 jsing 509: BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
510: "GF2m(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
511: "\n\t\tgoto err;\n\n");
1.1 jsing 512: }
513: BIO_printf(out, "\t/* build generator */\n");
514: BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
515: "sizeof(ec_gen_%d), tmp_1)) == NULL)"
516: "\n\t\tgoto err;\n", len, len);
517: BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
518: "NULL, NULL);\n");
519: BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
520: BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
521: "sizeof(ec_order_%d), tmp_2)) == NULL)"
522: "\n\t\tgoto err;\n", len, len);
523: BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
524: "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
525: "\n\t\tgoto err;\n", len, len);
526: BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
527: " tmp_2, tmp_3))\n\t\tgoto err;\n");
528: BIO_printf(out, "\n\tok=1;\n");
529: BIO_printf(out, "err:\n");
530: BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
531: BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
532: BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
533: BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
534: BIO_printf(out, "\tif (!ok)\n");
535: BIO_printf(out, "\t\t{\n");
536: BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
537: BIO_printf(out, "\t\tgroup = NULL;\n");
538: BIO_printf(out, "\t\t}\n");
539: BIO_printf(out, "\treturn(group);\n\t}\n");
540: }
1.3 jsing 541: if (!ecparam_config.noout) {
542: if (ecparam_config.outformat == FORMAT_ASN1)
1.1 jsing 543: i = i2d_ECPKParameters_bio(out, group);
1.3 jsing 544: else if (ecparam_config.outformat == FORMAT_PEM)
1.1 jsing 545: i = PEM_write_bio_ECPKParameters(out, group);
546: else {
547: BIO_printf(bio_err, "bad output format specified for"
548: " outfile\n");
549: goto end;
550: }
551: if (!i) {
552: BIO_printf(bio_err, "unable to write elliptic "
553: "curve parameters\n");
554: ERR_print_errors(bio_err);
555: goto end;
556: }
557: }
1.3 jsing 558: if (ecparam_config.genkey) {
1.1 jsing 559: EC_KEY *eckey = EC_KEY_new();
560:
561: if (eckey == NULL)
562: goto end;
563:
564: if (EC_KEY_set_group(eckey, group) == 0) {
565: EC_KEY_free(eckey);
566: goto end;
567: }
568:
569: if (!EC_KEY_generate_key(eckey)) {
570: EC_KEY_free(eckey);
571: goto end;
572: }
1.3 jsing 573: if (ecparam_config.outformat == FORMAT_ASN1)
1.1 jsing 574: i = i2d_ECPrivateKey_bio(out, eckey);
1.3 jsing 575: else if (ecparam_config.outformat == FORMAT_PEM)
1.1 jsing 576: i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
577: NULL, 0, NULL, NULL);
578: else {
579: BIO_printf(bio_err, "bad output format specified "
580: "for outfile\n");
581: EC_KEY_free(eckey);
582: goto end;
583: }
584: EC_KEY_free(eckey);
585: }
586: ret = 0;
1.6 jsing 587:
1.1 jsing 588: end:
1.6 jsing 589: BN_free(ec_p);
590: BN_free(ec_a);
591: BN_free(ec_b);
592: BN_free(ec_gen);
593: BN_free(ec_order);
594: BN_free(ec_cofactor);
595:
1.1 jsing 596: free(buffer);
1.6 jsing 597:
1.1 jsing 598: BIO_free(in);
1.6 jsing 599: BIO_free_all(out);
600: EC_GROUP_free(group);
1.1 jsing 601:
602: return (ret);
603: }
604:
605: static int
606: ecparam_print_var(BIO * out, BIGNUM * in, const char *var,
607: int len, unsigned char *buffer)
608: {
609: BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
610: if (BN_is_zero(in))
611: BIO_printf(out, "\n\t0x00");
612: else {
613: int i, l;
614:
615: l = BN_bn2bin(in, buffer);
616: for (i = 0; i < l - 1; i++) {
617: if ((i % 12) == 0)
618: BIO_printf(out, "\n\t");
619: BIO_printf(out, "0x%02X,", buffer[i]);
620: }
621: if ((i % 12) == 0)
622: BIO_printf(out, "\n\t");
623: BIO_printf(out, "0x%02X", buffer[i]);
624: }
625: BIO_printf(out, "\n\t};\n\n");
626: return 1;
627: }
628: #endif