1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2002-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4e1051a39Sopenharmony_ci *
5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
6e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
9e1051a39Sopenharmony_ci */
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci#include <string.h>
12e1051a39Sopenharmony_ci#include <openssl/opensslconf.h>
13e1051a39Sopenharmony_ci#include <openssl/evp.h>
14e1051a39Sopenharmony_ci#include <openssl/encoder.h>
15e1051a39Sopenharmony_ci#include <openssl/decoder.h>
16e1051a39Sopenharmony_ci#include <openssl/core_names.h>
17e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h>
18e1051a39Sopenharmony_ci#include <openssl/params.h>
19e1051a39Sopenharmony_ci#include <openssl/err.h>
20e1051a39Sopenharmony_ci#include "apps.h"
21e1051a39Sopenharmony_ci#include "progs.h"
22e1051a39Sopenharmony_ci#include "ec_common.h"
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_citypedef enum OPTION_choice {
25e1051a39Sopenharmony_ci    OPT_COMMON,
26e1051a39Sopenharmony_ci    OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
27e1051a39Sopenharmony_ci    OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
28e1051a39Sopenharmony_ci    OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_ENGINE, OPT_CHECK_NAMED,
29e1051a39Sopenharmony_ci    OPT_R_ENUM, OPT_PROV_ENUM
30e1051a39Sopenharmony_ci} OPTION_CHOICE;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ciconst OPTIONS ecparam_options[] = {
33e1051a39Sopenharmony_ci    OPT_SECTION("General"),
34e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
35e1051a39Sopenharmony_ci    {"list_curves", OPT_LIST_CURVES, '-',
36e1051a39Sopenharmony_ci     "Prints a list of all curve 'short names'"},
37e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
38e1051a39Sopenharmony_ci    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
39e1051a39Sopenharmony_ci#endif
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    {"genkey", OPT_GENKEY, '-', "Generate ec key"},
42e1051a39Sopenharmony_ci    {"in", OPT_IN, '<', "Input file  - default stdin"},
43e1051a39Sopenharmony_ci    {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
44e1051a39Sopenharmony_ci    {"out", OPT_OUT, '>', "Output file - default stdout"},
45e1051a39Sopenharmony_ci    {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci    OPT_SECTION("Output"),
48e1051a39Sopenharmony_ci    {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
49e1051a39Sopenharmony_ci    {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
50e1051a39Sopenharmony_ci    {"param_enc", OPT_PARAM_ENC, 's',
51e1051a39Sopenharmony_ci     "Specifies the way the ec parameters are encoded"},
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci    OPT_SECTION("Parameter"),
54e1051a39Sopenharmony_ci    {"check", OPT_CHECK, '-', "Validate the ec parameters"},
55e1051a39Sopenharmony_ci    {"check_named", OPT_CHECK_NAMED, '-',
56e1051a39Sopenharmony_ci     "Check that named EC curve parameters have not been modified"},
57e1051a39Sopenharmony_ci    {"no_seed", OPT_NO_SEED, '-',
58e1051a39Sopenharmony_ci     "If 'explicit' parameters are chosen do not use the seed"},
59e1051a39Sopenharmony_ci    {"name", OPT_NAME, 's',
60e1051a39Sopenharmony_ci     "Use the ec parameters with specified 'short name'"},
61e1051a39Sopenharmony_ci    {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    OPT_R_OPTIONS,
64e1051a39Sopenharmony_ci    OPT_PROV_OPTIONS,
65e1051a39Sopenharmony_ci    {NULL}
66e1051a39Sopenharmony_ci};
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_cistatic int list_builtin_curves(BIO *out)
69e1051a39Sopenharmony_ci{
70e1051a39Sopenharmony_ci    int ret = 0;
71e1051a39Sopenharmony_ci    EC_builtin_curve *curves = NULL;
72e1051a39Sopenharmony_ci    size_t n, crv_len = EC_get_builtin_curves(NULL, 0);
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
75e1051a39Sopenharmony_ci    if (!EC_get_builtin_curves(curves, crv_len))
76e1051a39Sopenharmony_ci        goto end;
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ci    for (n = 0; n < crv_len; n++) {
79e1051a39Sopenharmony_ci        const char *comment = curves[n].comment;
80e1051a39Sopenharmony_ci        const char *sname = OBJ_nid2sn(curves[n].nid);
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci        if (comment == NULL)
83e1051a39Sopenharmony_ci            comment = "CURVE DESCRIPTION NOT AVAILABLE";
84e1051a39Sopenharmony_ci        if (sname == NULL)
85e1051a39Sopenharmony_ci            sname = "";
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci        BIO_printf(out, "  %-10s: ", sname);
88e1051a39Sopenharmony_ci        BIO_printf(out, "%s\n", comment);
89e1051a39Sopenharmony_ci    }
90e1051a39Sopenharmony_ci    ret = 1;
91e1051a39Sopenharmony_ciend:
92e1051a39Sopenharmony_ci    OPENSSL_free(curves);
93e1051a39Sopenharmony_ci    return ret;
94e1051a39Sopenharmony_ci}
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ciint ecparam_main(int argc, char **argv)
97e1051a39Sopenharmony_ci{
98e1051a39Sopenharmony_ci    EVP_PKEY_CTX *gctx_params = NULL, *gctx_key = NULL, *pctx = NULL;
99e1051a39Sopenharmony_ci    EVP_PKEY *params_key = NULL, *key = NULL;
100e1051a39Sopenharmony_ci    OSSL_ENCODER_CTX *ectx_key = NULL, *ectx_params = NULL;
101e1051a39Sopenharmony_ci    OSSL_DECODER_CTX *dctx_params = NULL;
102e1051a39Sopenharmony_ci    ENGINE *e = NULL;
103e1051a39Sopenharmony_ci    BIO *out = NULL;
104e1051a39Sopenharmony_ci    char *curve_name = NULL;
105e1051a39Sopenharmony_ci    char *asn1_encoding = NULL;
106e1051a39Sopenharmony_ci    char *point_format = NULL;
107e1051a39Sopenharmony_ci    char *infile = NULL, *outfile = NULL, *prog;
108e1051a39Sopenharmony_ci    OPTION_CHOICE o;
109e1051a39Sopenharmony_ci    int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0;
110e1051a39Sopenharmony_ci    int ret = 1, private = 0;
111e1051a39Sopenharmony_ci    int no_seed = 0, check = 0, check_named = 0, text = 0, genkey = 0;
112e1051a39Sopenharmony_ci    int list_curves = 0;
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, ecparam_options);
115e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
116e1051a39Sopenharmony_ci        switch (o) {
117e1051a39Sopenharmony_ci        case OPT_EOF:
118e1051a39Sopenharmony_ci        case OPT_ERR:
119e1051a39Sopenharmony_ci opthelp:
120e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
121e1051a39Sopenharmony_ci            goto end;
122e1051a39Sopenharmony_ci        case OPT_HELP:
123e1051a39Sopenharmony_ci            opt_help(ecparam_options);
124e1051a39Sopenharmony_ci            ret = 0;
125e1051a39Sopenharmony_ci            goto end;
126e1051a39Sopenharmony_ci        case OPT_INFORM:
127e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
128e1051a39Sopenharmony_ci                goto opthelp;
129e1051a39Sopenharmony_ci            break;
130e1051a39Sopenharmony_ci        case OPT_IN:
131e1051a39Sopenharmony_ci            infile = opt_arg();
132e1051a39Sopenharmony_ci            break;
133e1051a39Sopenharmony_ci        case OPT_OUTFORM:
134e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
135e1051a39Sopenharmony_ci                goto opthelp;
136e1051a39Sopenharmony_ci            break;
137e1051a39Sopenharmony_ci        case OPT_OUT:
138e1051a39Sopenharmony_ci            outfile = opt_arg();
139e1051a39Sopenharmony_ci            break;
140e1051a39Sopenharmony_ci        case OPT_TEXT:
141e1051a39Sopenharmony_ci            text = 1;
142e1051a39Sopenharmony_ci            break;
143e1051a39Sopenharmony_ci        case OPT_CHECK:
144e1051a39Sopenharmony_ci            check = 1;
145e1051a39Sopenharmony_ci            break;
146e1051a39Sopenharmony_ci        case OPT_CHECK_NAMED:
147e1051a39Sopenharmony_ci            check_named = 1;
148e1051a39Sopenharmony_ci            break;
149e1051a39Sopenharmony_ci        case OPT_LIST_CURVES:
150e1051a39Sopenharmony_ci            list_curves = 1;
151e1051a39Sopenharmony_ci            break;
152e1051a39Sopenharmony_ci        case OPT_NO_SEED:
153e1051a39Sopenharmony_ci            no_seed = 1;
154e1051a39Sopenharmony_ci            break;
155e1051a39Sopenharmony_ci        case OPT_NOOUT:
156e1051a39Sopenharmony_ci            noout = 1;
157e1051a39Sopenharmony_ci            break;
158e1051a39Sopenharmony_ci        case OPT_NAME:
159e1051a39Sopenharmony_ci            curve_name = opt_arg();
160e1051a39Sopenharmony_ci            break;
161e1051a39Sopenharmony_ci        case OPT_CONV_FORM:
162e1051a39Sopenharmony_ci            point_format = opt_arg();
163e1051a39Sopenharmony_ci            if (!opt_string(point_format, point_format_options))
164e1051a39Sopenharmony_ci                goto opthelp;
165e1051a39Sopenharmony_ci            break;
166e1051a39Sopenharmony_ci        case OPT_PARAM_ENC:
167e1051a39Sopenharmony_ci            asn1_encoding = opt_arg();
168e1051a39Sopenharmony_ci            if (!opt_string(asn1_encoding, asn1_encoding_options))
169e1051a39Sopenharmony_ci                goto opthelp;
170e1051a39Sopenharmony_ci            break;
171e1051a39Sopenharmony_ci        case OPT_GENKEY:
172e1051a39Sopenharmony_ci            genkey = 1;
173e1051a39Sopenharmony_ci            break;
174e1051a39Sopenharmony_ci        case OPT_R_CASES:
175e1051a39Sopenharmony_ci            if (!opt_rand(o))
176e1051a39Sopenharmony_ci                goto end;
177e1051a39Sopenharmony_ci            break;
178e1051a39Sopenharmony_ci        case OPT_PROV_CASES:
179e1051a39Sopenharmony_ci            if (!opt_provider(o))
180e1051a39Sopenharmony_ci                goto end;
181e1051a39Sopenharmony_ci            break;
182e1051a39Sopenharmony_ci        case OPT_ENGINE:
183e1051a39Sopenharmony_ci            e = setup_engine(opt_arg(), 0);
184e1051a39Sopenharmony_ci            break;
185e1051a39Sopenharmony_ci        }
186e1051a39Sopenharmony_ci    }
187e1051a39Sopenharmony_ci
188e1051a39Sopenharmony_ci    /* No extra args. */
189e1051a39Sopenharmony_ci    argc = opt_num_rest();
190e1051a39Sopenharmony_ci    if (argc != 0)
191e1051a39Sopenharmony_ci        goto opthelp;
192e1051a39Sopenharmony_ci
193e1051a39Sopenharmony_ci    if (!app_RAND_load())
194e1051a39Sopenharmony_ci        goto end;
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ci    private = genkey ? 1 : 0;
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci    out = bio_open_owner(outfile, outformat, private);
199e1051a39Sopenharmony_ci    if (out == NULL)
200e1051a39Sopenharmony_ci        goto end;
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_ci    if (list_curves) {
203e1051a39Sopenharmony_ci        if (list_builtin_curves(out))
204e1051a39Sopenharmony_ci            ret = 0;
205e1051a39Sopenharmony_ci        goto end;
206e1051a39Sopenharmony_ci    }
207e1051a39Sopenharmony_ci
208e1051a39Sopenharmony_ci    if (curve_name != NULL) {
209e1051a39Sopenharmony_ci        OSSL_PARAM params[4];
210e1051a39Sopenharmony_ci        OSSL_PARAM *p = params;
211e1051a39Sopenharmony_ci
212e1051a39Sopenharmony_ci        if (strcmp(curve_name, "secp192r1") == 0) {
213e1051a39Sopenharmony_ci            BIO_printf(bio_err,
214e1051a39Sopenharmony_ci                       "using curve name prime192v1 instead of secp192r1\n");
215e1051a39Sopenharmony_ci            curve_name = SN_X9_62_prime192v1;
216e1051a39Sopenharmony_ci        } else if (strcmp(curve_name, "secp256r1") == 0) {
217e1051a39Sopenharmony_ci            BIO_printf(bio_err,
218e1051a39Sopenharmony_ci                       "using curve name prime256v1 instead of secp256r1\n");
219e1051a39Sopenharmony_ci            curve_name = SN_X9_62_prime256v1;
220e1051a39Sopenharmony_ci        }
221e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
222e1051a39Sopenharmony_ci                                                curve_name, 0);
223e1051a39Sopenharmony_ci        if (asn1_encoding != NULL)
224e1051a39Sopenharmony_ci            *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING,
225e1051a39Sopenharmony_ci                                                    asn1_encoding, 0);
226e1051a39Sopenharmony_ci        if (point_format != NULL)
227e1051a39Sopenharmony_ci            *p++ = OSSL_PARAM_construct_utf8_string(
228e1051a39Sopenharmony_ci                       OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
229e1051a39Sopenharmony_ci                       point_format, 0);
230e1051a39Sopenharmony_ci        *p = OSSL_PARAM_construct_end();
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ci        if (OPENSSL_strcasecmp(curve_name, "SM2") == 0)
233e1051a39Sopenharmony_ci            gctx_params = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "sm2",
234e1051a39Sopenharmony_ci                                                     app_get0_propq());
235e1051a39Sopenharmony_ci        else
236e1051a39Sopenharmony_ci            gctx_params = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "ec",
237e1051a39Sopenharmony_ci                                                     app_get0_propq());
238e1051a39Sopenharmony_ci        if (gctx_params == NULL
239e1051a39Sopenharmony_ci            || EVP_PKEY_keygen_init(gctx_params) <= 0
240e1051a39Sopenharmony_ci            || EVP_PKEY_CTX_set_params(gctx_params, params) <= 0
241e1051a39Sopenharmony_ci            || EVP_PKEY_keygen(gctx_params, &params_key) <= 0) {
242e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to generate key\n");
243e1051a39Sopenharmony_ci            goto end;
244e1051a39Sopenharmony_ci        }
245e1051a39Sopenharmony_ci    } else {
246e1051a39Sopenharmony_ci        params_key = load_keyparams(infile, informat, 1, "EC", "EC parameters");
247e1051a39Sopenharmony_ci        if (params_key == NULL || !EVP_PKEY_is_a(params_key, "EC"))
248e1051a39Sopenharmony_ci            goto end;
249e1051a39Sopenharmony_ci        if (point_format
250e1051a39Sopenharmony_ci            && !EVP_PKEY_set_utf8_string_param(
251e1051a39Sopenharmony_ci                    params_key, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
252e1051a39Sopenharmony_ci                    point_format)) {
253e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to set point conversion format\n");
254e1051a39Sopenharmony_ci            goto end;
255e1051a39Sopenharmony_ci        }
256e1051a39Sopenharmony_ci
257e1051a39Sopenharmony_ci        if (asn1_encoding != NULL
258e1051a39Sopenharmony_ci            && !EVP_PKEY_set_utf8_string_param(
259e1051a39Sopenharmony_ci                    params_key, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
260e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to set asn1 encoding format\n");
261e1051a39Sopenharmony_ci            goto end;
262e1051a39Sopenharmony_ci        }
263e1051a39Sopenharmony_ci    }
264e1051a39Sopenharmony_ci
265e1051a39Sopenharmony_ci    if (no_seed
266e1051a39Sopenharmony_ci        && !EVP_PKEY_set_octet_string_param(params_key, OSSL_PKEY_PARAM_EC_SEED,
267e1051a39Sopenharmony_ci                                            NULL, 0)) {
268e1051a39Sopenharmony_ci        BIO_printf(bio_err, "unable to clear seed\n");
269e1051a39Sopenharmony_ci        goto end;
270e1051a39Sopenharmony_ci    }
271e1051a39Sopenharmony_ci
272e1051a39Sopenharmony_ci    if (text
273e1051a39Sopenharmony_ci        && !EVP_PKEY_print_params(out, params_key, 0, NULL)) {
274e1051a39Sopenharmony_ci        BIO_printf(bio_err, "unable to print params\n");
275e1051a39Sopenharmony_ci        goto end;
276e1051a39Sopenharmony_ci    }
277e1051a39Sopenharmony_ci
278e1051a39Sopenharmony_ci    if (check || check_named) {
279e1051a39Sopenharmony_ci        BIO_printf(bio_err, "checking elliptic curve parameters: ");
280e1051a39Sopenharmony_ci
281e1051a39Sopenharmony_ci        if (check_named
282e1051a39Sopenharmony_ci            && !EVP_PKEY_set_utf8_string_param(params_key,
283e1051a39Sopenharmony_ci                                           OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
284e1051a39Sopenharmony_ci                                           OSSL_PKEY_EC_GROUP_CHECK_NAMED)) {
285e1051a39Sopenharmony_ci                BIO_printf(bio_err, "unable to set check_type\n");
286e1051a39Sopenharmony_ci                goto end;
287e1051a39Sopenharmony_ci        }
288e1051a39Sopenharmony_ci        pctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params_key,
289e1051a39Sopenharmony_ci                                          app_get0_propq());
290e1051a39Sopenharmony_ci        if (pctx == NULL || EVP_PKEY_param_check(pctx) <= 0) {
291e1051a39Sopenharmony_ci            BIO_printf(bio_err, "failed\n");
292e1051a39Sopenharmony_ci            goto end;
293e1051a39Sopenharmony_ci        }
294e1051a39Sopenharmony_ci        BIO_printf(bio_err, "ok\n");
295e1051a39Sopenharmony_ci    }
296e1051a39Sopenharmony_ci
297e1051a39Sopenharmony_ci    if (outformat == FORMAT_ASN1 && genkey)
298e1051a39Sopenharmony_ci        noout = 1;
299e1051a39Sopenharmony_ci
300e1051a39Sopenharmony_ci    if (!noout) {
301e1051a39Sopenharmony_ci        ectx_params = OSSL_ENCODER_CTX_new_for_pkey(
302e1051a39Sopenharmony_ci                          params_key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
303e1051a39Sopenharmony_ci                          outformat == FORMAT_ASN1 ? "DER" : "PEM", NULL, NULL);
304e1051a39Sopenharmony_ci        if (!OSSL_ENCODER_to_bio(ectx_params, out)) {
305e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to write elliptic curve parameters\n");
306e1051a39Sopenharmony_ci            goto end;
307e1051a39Sopenharmony_ci        }
308e1051a39Sopenharmony_ci    }
309e1051a39Sopenharmony_ci
310e1051a39Sopenharmony_ci    if (genkey) {
311e1051a39Sopenharmony_ci        /*
312e1051a39Sopenharmony_ci         * NOTE: EC keygen does not normally need to pass in the param_key
313e1051a39Sopenharmony_ci         * for named curves. This can be achieved using:
314e1051a39Sopenharmony_ci         *    gctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
315e1051a39Sopenharmony_ci         *    EVP_PKEY_keygen_init(gctx);
316e1051a39Sopenharmony_ci         *    EVP_PKEY_CTX_set_group_name(gctx, curvename);
317e1051a39Sopenharmony_ci         *    EVP_PKEY_keygen(gctx, &key) <= 0)
318e1051a39Sopenharmony_ci         */
319e1051a39Sopenharmony_ci        gctx_key = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params_key,
320e1051a39Sopenharmony_ci                                              app_get0_propq());
321e1051a39Sopenharmony_ci        if (EVP_PKEY_keygen_init(gctx_key) <= 0
322e1051a39Sopenharmony_ci            || EVP_PKEY_keygen(gctx_key, &key) <= 0) {
323e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to generate key\n");
324e1051a39Sopenharmony_ci            goto end;
325e1051a39Sopenharmony_ci        }
326e1051a39Sopenharmony_ci        assert(private);
327e1051a39Sopenharmony_ci        ectx_key = OSSL_ENCODER_CTX_new_for_pkey(
328e1051a39Sopenharmony_ci                       key, OSSL_KEYMGMT_SELECT_ALL,
329e1051a39Sopenharmony_ci                       outformat == FORMAT_ASN1 ? "DER" : "PEM", NULL, NULL);
330e1051a39Sopenharmony_ci        if (!OSSL_ENCODER_to_bio(ectx_key, out)) {
331e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to write elliptic "
332e1051a39Sopenharmony_ci                       "curve parameters\n");
333e1051a39Sopenharmony_ci            goto end;
334e1051a39Sopenharmony_ci        }
335e1051a39Sopenharmony_ci    }
336e1051a39Sopenharmony_ci
337e1051a39Sopenharmony_ci    ret = 0;
338e1051a39Sopenharmony_ciend:
339e1051a39Sopenharmony_ci    if (ret != 0)
340e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
341e1051a39Sopenharmony_ci    release_engine(e);
342e1051a39Sopenharmony_ci    EVP_PKEY_free(params_key);
343e1051a39Sopenharmony_ci    EVP_PKEY_free(key);
344e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
345e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(gctx_params);
346e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(gctx_key);
347e1051a39Sopenharmony_ci    OSSL_DECODER_CTX_free(dctx_params);
348e1051a39Sopenharmony_ci    OSSL_ENCODER_CTX_free(ectx_params);
349e1051a39Sopenharmony_ci    OSSL_ENCODER_CTX_free(ectx_key);
350e1051a39Sopenharmony_ci    BIO_free_all(out);
351e1051a39Sopenharmony_ci    return ret;
352e1051a39Sopenharmony_ci}
353