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 "prov/providercommonerr.h" /* PROV_R_MISSING_OID */
15 #include "encoder_local.h"
17 void ec_get_new_free_import(OSSL_FUNC_keymgmt_new_fn
**ec_new
,
18 OSSL_FUNC_keymgmt_free_fn
**ec_free
,
19 OSSL_FUNC_keymgmt_import_fn
**ec_import
)
21 *ec_new
= ossl_prov_get_keymgmt_new(ec_keymgmt_functions
);
22 *ec_free
= ossl_prov_get_keymgmt_free(ec_keymgmt_functions
);
23 *ec_import
= ossl_prov_get_keymgmt_import(ec_keymgmt_functions
);
26 static int ossl_prov_print_ec_param(BIO
*out
, const EC_GROUP
*group
)
28 const char *curve_name
;
29 int curve_nid
= EC_GROUP_get_curve_name(group
);
31 /* TODO(3.0): Explicit parameters are currently not supported */
32 if (curve_nid
== NID_undef
)
35 if (BIO_printf(out
, "%s: %s\n", "ASN1 OID", 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 || BIO_printf(out
, "%s: %s\n", "NIST CURVE", curve_name
) > 0);
44 int ossl_prov_print_eckey(BIO
*out
, EC_KEY
*eckey
, enum ec_print_type type
)
47 const char *type_label
= NULL
;
48 unsigned char *priv
= NULL
, *pub
= NULL
;
49 size_t priv_len
= 0, pub_len
= 0;
50 const EC_GROUP
*group
;
52 if (eckey
== NULL
|| (group
= EC_KEY_get0_group(eckey
)) == NULL
)
57 type_label
= "Private-Key";
60 type_label
= "Public-Key";
63 type_label
= "EC-Parameters";
67 if (type
== ec_print_priv
) {
68 const BIGNUM
*priv_key
= EC_KEY_get0_private_key(eckey
);
72 priv_len
= EC_KEY_priv2buf(eckey
, &priv
);
77 if (type
== ec_print_priv
|| type
== ec_print_pub
) {
78 const EC_POINT
*pub_pt
= EC_KEY_get0_public_key(eckey
);
83 pub_len
= EC_KEY_key2buf(eckey
, EC_KEY_get_conv_form(eckey
), &pub
, NULL
);
88 if (BIO_printf(out
, "%s: (%d bit)\n", type_label
,
89 EC_GROUP_order_bits(group
)) <= 0)
92 && !ossl_prov_print_labeled_buf(out
, "priv:", priv
, priv_len
))
95 && !ossl_prov_print_labeled_buf(out
, "pub:", pub
, pub_len
))
97 ret
= ossl_prov_print_ec_param(out
, group
);
99 OPENSSL_clear_free(priv
, priv_len
);
103 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
107 int ossl_prov_prepare_ec_params(const void *eckey
, int nid
,
108 void **pstr
, int *pstrtype
)
111 const EC_GROUP
*group
= EC_KEY_get0_group(eckey
);
115 || ((curve_nid
= EC_GROUP_get_curve_name(group
)) == NID_undef
)
116 || ((params
= OBJ_nid2obj(curve_nid
)) == NULL
)) {
117 /* TODO(3.0): Explicit curves are not supported */
121 if (OBJ_length(params
) == 0) {
122 /* Some curves might not have an associated OID */
123 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_OID
);
124 ASN1_OBJECT_free(params
);
129 *pstrtype
= V_ASN1_OBJECT
;
133 int ossl_prov_ec_pub_to_der(const void *eckey
, unsigned char **pder
)
135 return i2o_ECPublicKey(eckey
, pder
);
138 int ossl_prov_ec_priv_to_der(const void *veckey
, unsigned char **pder
)
140 EC_KEY
*eckey
= (EC_KEY
*)veckey
;
141 unsigned int old_flags
;
145 * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object
146 * as the pkeyalg->parameter field. (For a named curve this is an OID)
147 * The pkey field is an octet string that holds the encoded
148 * ECPrivateKey SEQUENCE with the optional parameters field omitted.
149 * We omit this by setting the EC_PKEY_NO_PARAMETERS flag.
151 old_flags
= EC_KEY_get_enc_flags(eckey
); /* save old flags */
152 EC_KEY_set_enc_flags(eckey
, old_flags
| EC_PKEY_NO_PARAMETERS
);
153 ret
= i2d_ECPrivateKey(eckey
, pder
);
154 EC_KEY_set_enc_flags(eckey
, old_flags
); /* restore old flags */
155 return ret
; /* return the length of the der encoded data */