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