2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/err.h>
11 #include "crypto/ec.h"
12 #include "prov/bio.h" /* ossl_prov_bio_printf() */
13 #include "prov/implementations.h" /* ec_keymgmt_functions */
14 #include "serializer_local.h"
16 void ec_get_new_free_import(OSSL_OP_keymgmt_new_fn **ec_new,
17 OSSL_OP_keymgmt_free_fn **ec_free,
18 OSSL_OP_keymgmt_import_fn **ec_import)
20 *ec_new = ossl_prov_get_keymgmt_new(ec_keymgmt_functions);
21 *ec_free = ossl_prov_get_keymgmt_free(ec_keymgmt_functions);
22 *ec_import = ossl_prov_get_keymgmt_import(ec_keymgmt_functions);
25 static int ossl_prov_print_ec_param(BIO *out, const EC_GROUP *group)
27 const char *curve_name;
28 int curve_nid = EC_GROUP_get_curve_name(group);
30 /* TODO(3.0): Explicit parameters are currently not supported */
31 if (curve_nid == NID_undef)
34 if (ossl_prov_bio_printf(out, "%s: %s\n", "ASN1 OID",
35 OBJ_nid2sn(curve_nid)) <= 0)
38 /* TODO(3.0): Only named curves are currently supported */
39 curve_name = EC_curve_nid2nist(curve_nid);
40 return (curve_name == NULL
41 || ossl_prov_bio_printf(out, "%s: %s\n", "NIST CURVE",
45 int ossl_prov_print_eckey(BIO *out, EC_KEY *eckey, enum ec_print_type type)
48 const char *type_label = NULL;
49 unsigned char *priv = NULL, *pub = NULL;
50 size_t priv_len = 0, pub_len = 0;
51 const EC_GROUP *group;
53 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
58 type_label = "Private-Key";
61 type_label = "Public-Key";
64 type_label = "EC-Parameters";
68 if (type == ec_print_priv) {
69 const BIGNUM *priv_key = EC_KEY_get0_private_key(eckey);
73 priv_len = EC_KEY_priv2buf(eckey, &priv);
78 if (type == ec_print_priv || type == ec_print_pub) {
79 const EC_POINT *pub_pt = EC_KEY_get0_public_key(eckey);
84 pub_len = EC_KEY_key2buf(eckey, EC_KEY_get_conv_form(eckey), &pub, NULL);
89 if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label,
90 EC_GROUP_order_bits(group)) <= 0)
93 && !ossl_prov_print_labeled_buf(out, "priv:", priv, priv_len))
96 && !ossl_prov_print_labeled_buf(out, "pub:", pub, pub_len))
98 ret = ossl_prov_print_ec_param(out, group);
100 OPENSSL_clear_free(priv, priv_len);
104 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
108 int ossl_prov_prepare_ec_params(const void *eckey, int nid,
109 void **pstr, int *pstrtype)
112 const EC_GROUP *group = EC_KEY_get0_group(eckey);
116 || ((curve_nid = EC_GROUP_get_curve_name(group)) == NID_undef)
117 || ((params = OBJ_nid2obj(curve_nid)) == NULL)) {
118 /* TODO(3.0): Explicit curves are not supported */
123 *pstrtype = V_ASN1_OBJECT;
127 int ossl_prov_ec_pub_to_der(const void *eckey, unsigned char **pder)
129 return i2o_ECPublicKey(eckey, pder);
132 int ossl_prov_ec_priv_to_der(const void *veckey, unsigned char **pder)
134 EC_KEY *eckey = (EC_KEY *)veckey;
135 unsigned int old_flags;
139 * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object
140 * as the pkeyalg->parameter field. (For a named curve this is an OID)
141 * The pkey field is an octet string that holds the encoded
142 * ECPrivateKey SEQUENCE with the optional parameters field omitted.
143 * We omit this by setting the EC_PKEY_NO_PARAMETERS flag.
145 old_flags = EC_KEY_get_enc_flags(eckey); /* save old flags */
146 EC_KEY_set_enc_flags(eckey, old_flags | EC_PKEY_NO_PARAMETERS);
147 ret = i2d_ECPrivateKey(eckey, pder);
148 EC_KEY_set_enc_flags(eckey, old_flags); /* restore old flags */
149 return ret; /* return the length of the der encoded data */