]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ecparam.c
Rework the provider digest constructor to provide implementation get_params
[thirdparty/openssl.git] / apps / ecparam.c
CommitLineData
14a7cfb3 1/*
6738bf14 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5dbd3efc 4 *
dffa7520 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
5dbd3efc 9 */
846e33c7 10
6e04afb8 11#include <openssl/opensslconf.h>
effaf4de
RS
12#ifdef OPENSSL_NO_EC
13NON_EMPTY_TRANSLATION_UNIT
14#else
15
0f113f3e
MC
16# include <stdio.h>
17# include <stdlib.h>
18# include <time.h>
19# include <string.h>
20# include "apps.h"
dab2cd68 21# include "progs.h"
0f113f3e
MC
22# include <openssl/bio.h>
23# include <openssl/err.h>
24# include <openssl/bn.h>
25# include <openssl/ec.h>
26# include <openssl/x509.h>
27# include <openssl/pem.h>
28
7e1b7485
RS
29typedef enum OPTION_choice {
30 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
32 OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
8402cd5f 33 OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_ENGINE, OPT_CHECK_NAMED,
3ee1eac2 34 OPT_R_ENUM
7e1b7485
RS
35} OPTION_CHOICE;
36
44c83ebd 37const OPTIONS ecparam_options[] = {
7e1b7485
RS
38 {"help", OPT_HELP, '-', "Display this summary"},
39 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
40 {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
41 {"in", OPT_IN, '<', "Input file - default stdin"},
42 {"out", OPT_OUT, '>', "Output file - default stdout"},
43 {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
44 {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
45 {"check", OPT_CHECK, '-', "Validate the ec parameters"},
8402cd5f
SL
46 {"check_named", OPT_CHECK_NAMED, '-',
47 "Check that named EC curve parameters have not been modified"},
7e1b7485
RS
48 {"list_curves", OPT_LIST_CURVES, '-',
49 "Prints a list of all curve 'short names'"},
50 {"no_seed", OPT_NO_SEED, '-',
51 "If 'explicit' parameters are chosen do not use the seed"},
52 {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
53 {"name", OPT_NAME, 's',
54 "Use the ec parameters with specified 'short name'"},
55 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
56 {"param_enc", OPT_PARAM_ENC, 's',
57 "Specifies the way the ec parameters are encoded"},
58 {"genkey", OPT_GENKEY, '-', "Generate ec key"},
3ee1eac2 59 OPT_R_OPTIONS,
7e1b7485
RS
60# ifndef OPENSSL_NO_ENGINE
61 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
62# endif
63 {NULL}
64};
65
df2ee0e2 66static OPT_PAIR forms[] = {
7e1b7485
RS
67 {"compressed", POINT_CONVERSION_COMPRESSED},
68 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
69 {"hybrid", POINT_CONVERSION_HYBRID},
70 {NULL}
71};
72
df2ee0e2 73static OPT_PAIR encodings[] = {
7e1b7485
RS
74 {"named_curve", OPENSSL_EC_NAMED_CURVE},
75 {"explicit", 0},
76 {NULL}
77};
78
79int ecparam_main(int argc, char **argv)
0f113f3e 80{
dd1abd44 81 ENGINE *e = NULL;
7e1b7485
RS
82 BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
83 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
84 BIO *in = NULL, *out = NULL;
0f113f3e
MC
85 EC_GROUP *group = NULL;
86 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
3ee1eac2 87 char *curve_name = NULL;
333b070e 88 char *infile = NULL, *outfile = NULL, *prog;
0f113f3e 89 unsigned char *buffer = NULL;
7e1b7485
RS
90 OPTION_CHOICE o;
91 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
3b061a00
RS
92 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
93 int ret = 1, private = 0;
7e1b7485 94 int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
8402cd5f 95 int text = 0, i, genkey = 0, check_named = 0;
7e1b7485
RS
96
97 prog = opt_init(argc, argv, ecparam_options);
98 while ((o = opt_next()) != OPT_EOF) {
99 switch (o) {
100 case OPT_EOF:
101 case OPT_ERR:
102 opthelp:
103 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
104 goto end;
105 case OPT_HELP:
106 opt_help(ecparam_options);
107 ret = 0;
108 goto end;
109 case OPT_INFORM:
110 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
111 goto opthelp;
112 break;
113 case OPT_IN:
114 infile = opt_arg();
115 break;
116 case OPT_OUTFORM:
117 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
118 goto opthelp;
119 break;
120 case OPT_OUT:
121 outfile = opt_arg();
122 break;
123 case OPT_TEXT:
0f113f3e 124 text = 1;
7e1b7485
RS
125 break;
126 case OPT_C:
0f113f3e 127 C = 1;
7e1b7485
RS
128 break;
129 case OPT_CHECK:
0f113f3e 130 check = 1;
7e1b7485 131 break;
8402cd5f
SL
132 case OPT_CHECK_NAMED:
133 check_named = 1;
134 break;
7e1b7485 135 case OPT_LIST_CURVES:
0f113f3e 136 list_curves = 1;
7e1b7485
RS
137 break;
138 case OPT_NO_SEED:
0f113f3e 139 no_seed = 1;
7e1b7485
RS
140 break;
141 case OPT_NOOUT:
0f113f3e 142 noout = 1;
7e1b7485
RS
143 break;
144 case OPT_NAME:
145 curve_name = opt_arg();
146 break;
147 case OPT_CONV_FORM:
148 if (!opt_pair(opt_arg(), forms, &new_form))
149 goto opthelp;
150 form = new_form;
151 new_form = 1;
152 break;
153 case OPT_PARAM_ENC:
154 if (!opt_pair(opt_arg(), encodings, &asn1_flag))
155 goto opthelp;
156 new_asn1_flag = 1;
157 break;
158 case OPT_GENKEY:
3ee1eac2 159 genkey = 1;
7e1b7485 160 break;
3ee1eac2
RS
161 case OPT_R_CASES:
162 if (!opt_rand(o))
163 goto end;
7e1b7485
RS
164 break;
165 case OPT_ENGINE:
dd1abd44 166 e = setup_engine(opt_arg(), 0);
0f113f3e
MC
167 break;
168 }
0f113f3e 169 }
7e1b7485 170 argc = opt_num_rest();
03358517
KR
171 if (argc != 0)
172 goto opthelp;
173
3b061a00 174 private = genkey ? 1 : 0;
0f113f3e 175
bdd58d98 176 in = bio_open_default(infile, 'r', informat);
7e1b7485 177 if (in == NULL)
0f113f3e 178 goto end;
bdd58d98 179 out = bio_open_owner(outfile, outformat, private);
7e1b7485 180 if (out == NULL)
0f113f3e 181 goto end;
0f113f3e 182
0f113f3e
MC
183 if (list_curves) {
184 EC_builtin_curve *curves = NULL;
68dc6824
RS
185 size_t crv_len = EC_get_builtin_curves(NULL, 0);
186 size_t n;
0f113f3e 187
b4faea50 188 curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
0f113f3e
MC
189 if (!EC_get_builtin_curves(curves, crv_len)) {
190 OPENSSL_free(curves);
191 goto end;
192 }
193
194 for (n = 0; n < crv_len; n++) {
195 const char *comment;
196 const char *sname;
197 comment = curves[n].comment;
198 sname = OBJ_nid2sn(curves[n].nid);
199 if (comment == NULL)
200 comment = "CURVE DESCRIPTION NOT AVAILABLE";
201 if (sname == NULL)
202 sname = "";
203
204 BIO_printf(out, " %-10s: ", sname);
205 BIO_printf(out, "%s\n", comment);
206 }
207
208 OPENSSL_free(curves);
209 ret = 0;
210 goto end;
211 }
212
213 if (curve_name != NULL) {
214 int nid;
215
216 /*
217 * workaround for the SECG curve names secp192r1 and secp256r1 (which
218 * are the same as the curves prime192v1 and prime256v1 defined in
219 * X9.62)
220 */
86885c28 221 if (strcmp(curve_name, "secp192r1") == 0) {
0f113f3e
MC
222 BIO_printf(bio_err, "using curve name prime192v1 "
223 "instead of secp192r1\n");
224 nid = NID_X9_62_prime192v1;
86885c28 225 } else if (strcmp(curve_name, "secp256r1") == 0) {
0f113f3e
MC
226 BIO_printf(bio_err, "using curve name prime256v1 "
227 "instead of secp256r1\n");
228 nid = NID_X9_62_prime256v1;
2234212c 229 } else {
0f113f3e 230 nid = OBJ_sn2nid(curve_name);
2234212c 231 }
0f113f3e
MC
232
233 if (nid == 0)
234 nid = EC_curve_nist2nid(curve_name);
235
236 if (nid == 0) {
237 BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
238 goto end;
239 }
240
241 group = EC_GROUP_new_by_curve_name(nid);
242 if (group == NULL) {
243 BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
244 goto end;
245 }
246 EC_GROUP_set_asn1_flag(group, asn1_flag);
247 EC_GROUP_set_point_conversion_form(group, form);
2234212c 248 } else if (informat == FORMAT_ASN1) {
0f113f3e 249 group = d2i_ECPKParameters_bio(in, NULL);
2234212c 250 } else {
0f113f3e 251 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
2234212c 252 }
0f113f3e
MC
253 if (group == NULL) {
254 BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
255 ERR_print_errors(bio_err);
256 goto end;
257 }
258
259 if (new_form)
260 EC_GROUP_set_point_conversion_form(group, form);
261
262 if (new_asn1_flag)
263 EC_GROUP_set_asn1_flag(group, asn1_flag);
264
265 if (no_seed) {
266 EC_GROUP_set_seed(group, NULL, 0);
267 }
268
269 if (text) {
270 if (!ECPKParameters_print(out, group, 0))
271 goto end;
272 }
273
8402cd5f
SL
274 if (check_named) {
275 BIO_printf(bio_err, "validating named elliptic curve parameters: ");
a9612d6c 276 if (EC_GROUP_check_named_curve(group, 0, NULL) <= 0) {
8402cd5f
SL
277 BIO_printf(bio_err, "failed\n");
278 ERR_print_errors(bio_err);
279 goto end;
280 }
281 BIO_printf(bio_err, "ok\n");
282 }
283
0f113f3e 284 if (check) {
0f113f3e
MC
285 BIO_printf(bio_err, "checking elliptic curve parameters: ");
286 if (!EC_GROUP_check(group, NULL)) {
287 BIO_printf(bio_err, "failed\n");
288 ERR_print_errors(bio_err);
99dcd880
PW
289 goto end;
290 }
291 BIO_printf(bio_err, "ok\n");
0f113f3e
MC
292
293 }
294
295 if (C) {
296 size_t buf_len = 0, tmp_len = 0;
297 const EC_POINT *point;
298 int is_prime, len = 0;
299 const EC_METHOD *meth = EC_GROUP_method_of(group);
300
7e1b7485
RS
301 if ((ec_p = BN_new()) == NULL
302 || (ec_a = BN_new()) == NULL
303 || (ec_b = BN_new()) == NULL
304 || (ec_gen = BN_new()) == NULL
305 || (ec_order = BN_new()) == NULL
306 || (ec_cofactor = BN_new()) == NULL) {
68dc6824 307 perror("Can't allocate BN");
0f113f3e
MC
308 goto end;
309 }
310
311 is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
7e1b7485
RS
312 if (!is_prime) {
313 BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
0f113f3e
MC
314 goto end;
315 }
316
9cc570d4 317 if (!EC_GROUP_get_curve(group, ec_p, ec_a, ec_b, NULL))
7e1b7485
RS
318 goto end;
319
0f113f3e
MC
320 if ((point = EC_GROUP_get0_generator(group)) == NULL)
321 goto end;
322 if (!EC_POINT_point2bn(group, point,
323 EC_GROUP_get_point_conversion_form(group),
324 ec_gen, NULL))
325 goto end;
326 if (!EC_GROUP_get_order(group, ec_order, NULL))
327 goto end;
328 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
329 goto end;
330
331 if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
332 goto end;
333
334 len = BN_num_bits(ec_order);
335
336 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
337 buf_len = tmp_len;
338 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
339 buf_len = tmp_len;
340 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
341 buf_len = tmp_len;
342 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
343 buf_len = tmp_len;
344 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
345 buf_len = tmp_len;
346 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
347 buf_len = tmp_len;
348
68dc6824 349 buffer = app_malloc(buf_len, "BN buffer");
0f113f3e 350
7e1b7485
RS
351 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
352 print_bignum_var(out, ec_p, "ec_p", len, buffer);
353 print_bignum_var(out, ec_a, "ec_a", len, buffer);
354 print_bignum_var(out, ec_b, "ec_b", len, buffer);
355 print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
356 print_bignum_var(out, ec_order, "ec_order", len, buffer);
357 print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
358 BIO_printf(out, " int ok = 0;\n"
359 " EC_GROUP *group = NULL;\n"
360 " EC_POINT *point = NULL;\n"
361 " BIGNUM *tmp_1 = NULL;\n"
362 " BIGNUM *tmp_2 = NULL;\n"
363 " BIGNUM *tmp_3 = NULL;\n"
364 "\n");
365
cbe29648 366 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof(ec_p_%d), NULL)) == NULL)\n"
7e1b7485 367 " goto err;\n", len, len);
cbe29648 368 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof(ec_a_%d), NULL)) == NULL)\n"
7e1b7485 369 " goto err;\n", len, len);
cbe29648 370 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof(ec_b_%d), NULL)) == NULL)\n"
7e1b7485
RS
371 " goto err;\n", len, len);
372 BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
373 " goto err;\n"
374 "\n");
375 BIO_printf(out, " /* build generator */\n");
cbe29648 376 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof(ec_gen_%d), tmp_1)) == NULL)\n"
7e1b7485
RS
377 " goto err;\n", len, len);
378 BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
379 BIO_printf(out, " if (point == NULL)\n"
380 " goto err;\n");
cbe29648 381 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof(ec_order_%d), tmp_2)) == NULL)\n"
7e1b7485 382 " goto err;\n", len, len);
cbe29648 383 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof(ec_cofactor_%d), tmp_3)) == NULL)\n"
7e1b7485
RS
384 " goto err;\n", len, len);
385 BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
386 " goto err;\n"
387 "ok = 1;"
388 "\n");
389 BIO_printf(out, "err:\n"
390 " BN_free(tmp_1);\n"
391 " BN_free(tmp_2);\n"
392 " BN_free(tmp_3);\n"
393 " EC_POINT_free(point);\n"
394 " if (!ok) {\n"
395 " EC_GROUP_free(group);\n"
396 " return NULL;\n"
397 " }\n"
398 " return (group);\n"
399 "}\n");
0f113f3e
MC
400 }
401
4bdc25b0
BE
402 if (outformat == FORMAT_ASN1 && genkey)
403 noout = 1;
404
0f113f3e
MC
405 if (!noout) {
406 if (outformat == FORMAT_ASN1)
407 i = i2d_ECPKParameters_bio(out, group);
7e1b7485 408 else
0f113f3e 409 i = PEM_write_bio_ECPKParameters(out, group);
0f113f3e
MC
410 if (!i) {
411 BIO_printf(bio_err, "unable to write elliptic "
412 "curve parameters\n");
413 ERR_print_errors(bio_err);
414 goto end;
415 }
416 }
417
0f113f3e
MC
418 if (genkey) {
419 EC_KEY *eckey = EC_KEY_new();
420
421 if (eckey == NULL)
422 goto end;
423
f6de4eb7
DSH
424 if (EC_KEY_set_group(eckey, group) == 0) {
425 BIO_printf(bio_err, "unable to set group when generating key\n");
426 EC_KEY_free(eckey);
427 ERR_print_errors(bio_err);
0f113f3e 428 goto end;
f6de4eb7 429 }
0f113f3e 430
4bdc25b0
BE
431 if (new_form)
432 EC_KEY_set_conv_form(eckey, form);
433
0f113f3e 434 if (!EC_KEY_generate_key(eckey)) {
f6de4eb7 435 BIO_printf(bio_err, "unable to generate key\n");
0f113f3e 436 EC_KEY_free(eckey);
f6de4eb7 437 ERR_print_errors(bio_err);
0f113f3e
MC
438 goto end;
439 }
3b061a00 440 assert(private);
0f113f3e
MC
441 if (outformat == FORMAT_ASN1)
442 i = i2d_ECPrivateKey_bio(out, eckey);
7e1b7485 443 else
0f113f3e
MC
444 i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
445 NULL, 0, NULL, NULL);
0f113f3e
MC
446 EC_KEY_free(eckey);
447 }
448
0f113f3e
MC
449 ret = 0;
450 end:
23a1d5e9
RS
451 BN_free(ec_p);
452 BN_free(ec_a);
453 BN_free(ec_b);
454 BN_free(ec_gen);
455 BN_free(ec_order);
456 BN_free(ec_cofactor);
b548a1f1 457 OPENSSL_free(buffer);
dd1abd44
RL
458 EC_GROUP_free(group);
459 release_engine(e);
ca3a82c3
RS
460 BIO_free(in);
461 BIO_free_all(out);
26a7d938 462 return ret;
5dbd3efc
BM
463}
464
5dbd3efc 465#endif