source: rtems-libbsd/freebsd/crypto/openssl/apps/ecparam.c @ 0a699e7

5-freebsd-126-freebsd-12
Last change on this file since 0a699e7 was 0a699e7, checked in by Christian Mauderer <christian.mauderer@…>, on 03/28/19 at 06:13:59

bin/openssl: Import from FreeBSD.

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