| 1 |
/* |
| 2 |
* Copyright 2006-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 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 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* ECDH and ECDSA low level APIs are deprecated for public use, but still ok |
| 12 |
* for internal use. |
| 13 |
*/ |
| 14 |
#include "internal/deprecated.h" |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include "internal/cryptlib.h" |
| 18 |
#include <openssl/x509.h> |
| 19 |
#include <openssl/ec.h> |
| 20 |
#include <openssl/bn.h> |
| 21 |
#include <openssl/asn1t.h> |
| 22 |
#include "crypto/asn1.h" |
| 23 |
#include "crypto/evp.h" |
| 24 |
#include "crypto/x509.h" |
| 25 |
#include <openssl/core_names.h> |
| 26 |
#include <openssl/param_build.h> |
| 27 |
#include "ec_local.h" |
| 28 |
|
| 29 |
static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key) |
| 30 |
{ |
| 31 |
const EC_GROUP *group; |
| 32 |
int nid; |
| 33 |
|
| 34 |
if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) { |
| 35 |
ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
| 36 |
return 0; |
| 37 |
} |
| 38 |
if (EC_GROUP_get_asn1_flag(group) |
| 39 |
&& (nid = EC_GROUP_get_curve_name(group))) |
| 40 |
/* we have a 'named curve' => just set the OID */ |
| 41 |
{ |
| 42 |
ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid); |
| 43 |
|
| 44 |
if (asn1obj == NULL || OBJ_length(asn1obj) == 0) { |
| 45 |
ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID); |
| 46 |
return 0; |
| 47 |
} |
| 48 |
*ppval = asn1obj; |
| 49 |
*pptype = V_ASN1_OBJECT; |
| 50 |
} else { /* explicit parameters */ |
| 51 |
|
| 52 |
ASN1_STRING *pstr = NULL; |
| 53 |
pstr = ASN1_STRING_new(); |
| 54 |
if (pstr == NULL) |
| 55 |
return 0; |
| 56 |
pstr->length = i2d_ECParameters(ec_key, &pstr->data); |
| 57 |
if (pstr->length <= 0) { |
| 58 |
ASN1_STRING_free(pstr); |
| 59 |
ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
*ppval = pstr; |
| 63 |
*pptype = V_ASN1_SEQUENCE; |
| 64 |
} |
| 65 |
return 1; |
| 66 |
} |
| 67 |
|
| 68 |
static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
| 69 |
{ |
| 70 |
const EC_KEY *ec_key = pkey->pkey.ec; |
| 71 |
void *pval = NULL; |
| 72 |
int ptype; |
| 73 |
unsigned char *penc = NULL, *p; |
| 74 |
int penclen; |
| 75 |
|
| 76 |
if (!eckey_param2type(&ptype, &pval, ec_key)) { |
| 77 |
ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
penclen = i2o_ECPublicKey(ec_key, NULL); |
| 81 |
if (penclen <= 0) |
| 82 |
goto err; |
| 83 |
penc = OPENSSL_malloc(penclen); |
| 84 |
if (penc == NULL) |
| 85 |
goto err; |
| 86 |
p = penc; |
| 87 |
penclen = i2o_ECPublicKey(ec_key, &p); |
| 88 |
if (penclen <= 0) |
| 89 |
goto err; |
| 90 |
if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC), |
| 91 |
ptype, pval, penc, penclen)) |
| 92 |
return 1; |
| 93 |
err: |
| 94 |
if (ptype == V_ASN1_SEQUENCE) |
| 95 |
ASN1_STRING_free(pval); |
| 96 |
OPENSSL_free(penc); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey) |
| 101 |
{ |
| 102 |
const unsigned char *p = NULL; |
| 103 |
int pklen; |
| 104 |
EC_KEY *eckey = NULL; |
| 105 |
X509_ALGOR *palg; |
| 106 |
OSSL_LIB_CTX *libctx = NULL; |
| 107 |
const char *propq = NULL; |
| 108 |
|
| 109 |
if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey) |
| 110 |
|| !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) |
| 111 |
return 0; |
| 112 |
eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq); |
| 113 |
|
| 114 |
if (!eckey) |
| 115 |
return 0; |
| 116 |
|
| 117 |
/* We have parameters now set public key */ |
| 118 |
if (!o2i_ECPublicKey(&eckey, &p, pklen)) { |
| 119 |
ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
| 120 |
goto ecerr; |
| 121 |
} |
| 122 |
|
| 123 |
EVP_PKEY_assign_EC_KEY(pkey, eckey); |
| 124 |
return 1; |
| 125 |
|
| 126 |
ecerr: |
| 127 |
EC_KEY_free(eckey); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
| 132 |
{ |
| 133 |
int r; |
| 134 |
const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); |
| 135 |
const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), |
| 136 |
*pb = EC_KEY_get0_public_key(b->pkey.ec); |
| 137 |
|
| 138 |
if (group == NULL || pa == NULL || pb == NULL) |
| 139 |
return -2; |
| 140 |
r = EC_POINT_cmp(group, pa, pb, NULL); |
| 141 |
if (r == 0) |
| 142 |
return 1; |
| 143 |
if (r == 1) |
| 144 |
return 0; |
| 145 |
return -2; |
| 146 |
} |
| 147 |
|
| 148 |
static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8, |
| 149 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 150 |
{ |
| 151 |
int ret = 0; |
| 152 |
EC_KEY *eckey = ossl_ec_key_from_pkcs8(p8, libctx, propq); |
| 153 |
|
| 154 |
if (eckey != NULL) { |
| 155 |
ret = 1; |
| 156 |
EVP_PKEY_assign_EC_KEY(pkey, eckey); |
| 157 |
} |
| 158 |
|
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
| 163 |
{ |
| 164 |
EC_KEY ec_key = *(pkey->pkey.ec); |
| 165 |
unsigned char *ep = NULL; |
| 166 |
int eplen, ptype; |
| 167 |
void *pval; |
| 168 |
unsigned int old_flags; |
| 169 |
|
| 170 |
if (!eckey_param2type(&ptype, &pval, &ec_key)) { |
| 171 |
ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
/* set the private key */ |
| 176 |
|
| 177 |
/* |
| 178 |
* do not include the parameters in the SEC1 private key see PKCS#11 |
| 179 |
* 12.11 |
| 180 |
*/ |
| 181 |
old_flags = EC_KEY_get_enc_flags(&ec_key); |
| 182 |
EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS); |
| 183 |
|
| 184 |
eplen = i2d_ECPrivateKey(&ec_key, &ep); |
| 185 |
if (eplen <= 0) { |
| 186 |
ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
| 187 |
goto err; |
| 188 |
} |
| 189 |
|
| 190 |
if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0, |
| 191 |
ptype, pval, ep, eplen)) { |
| 192 |
ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
| 193 |
OPENSSL_clear_free(ep, eplen); |
| 194 |
goto err; |
| 195 |
} |
| 196 |
|
| 197 |
return 1; |
| 198 |
|
| 199 |
err: |
| 200 |
if (ptype == V_ASN1_SEQUENCE) |
| 201 |
ASN1_STRING_free(pval); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static int int_ec_size(const EVP_PKEY *pkey) |
| 206 |
{ |
| 207 |
return ECDSA_size(pkey->pkey.ec); |
| 208 |
} |
| 209 |
|
| 210 |
static int ec_bits(const EVP_PKEY *pkey) |
| 211 |
{ |
| 212 |
return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec)); |
| 213 |
} |
| 214 |
|
| 215 |
static int ec_security_bits(const EVP_PKEY *pkey) |
| 216 |
{ |
| 217 |
int ecbits = ec_bits(pkey); |
| 218 |
|
| 219 |
if (ecbits >= 512) |
| 220 |
return 256; |
| 221 |
if (ecbits >= 384) |
| 222 |
return 192; |
| 223 |
if (ecbits >= 256) |
| 224 |
return 128; |
| 225 |
if (ecbits >= 224) |
| 226 |
return 112; |
| 227 |
if (ecbits >= 160) |
| 228 |
return 80; |
| 229 |
return ecbits / 2; |
| 230 |
} |
| 231 |
|
| 232 |
static int ec_missing_parameters(const EVP_PKEY *pkey) |
| 233 |
{ |
| 234 |
if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL) |
| 235 |
return 1; |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
| 240 |
{ |
| 241 |
EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec)); |
| 242 |
|
| 243 |
if (group == NULL) |
| 244 |
return 0; |
| 245 |
if (to->pkey.ec == NULL) { |
| 246 |
to->pkey.ec = EC_KEY_new(); |
| 247 |
if (to->pkey.ec == NULL) |
| 248 |
goto err; |
| 249 |
} |
| 250 |
if (EC_KEY_set_group(to->pkey.ec, group) == 0) |
| 251 |
goto err; |
| 252 |
EC_GROUP_free(group); |
| 253 |
return 1; |
| 254 |
err: |
| 255 |
EC_GROUP_free(group); |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
| 260 |
{ |
| 261 |
const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), |
| 262 |
*group_b = EC_KEY_get0_group(b->pkey.ec); |
| 263 |
|
| 264 |
if (group_a == NULL || group_b == NULL) |
| 265 |
return -2; |
| 266 |
if (EC_GROUP_cmp(group_a, group_b, NULL)) |
| 267 |
return 0; |
| 268 |
else |
| 269 |
return 1; |
| 270 |
} |
| 271 |
|
| 272 |
static void int_ec_free(EVP_PKEY *pkey) |
| 273 |
{ |
| 274 |
EC_KEY_free(pkey->pkey.ec); |
| 275 |
} |
| 276 |
|
| 277 |
typedef enum { |
| 278 |
EC_KEY_PRINT_PRIVATE, |
| 279 |
EC_KEY_PRINT_PUBLIC, |
| 280 |
EC_KEY_PRINT_PARAM |
| 281 |
} ec_print_t; |
| 282 |
|
| 283 |
static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype) |
| 284 |
{ |
| 285 |
const char *ecstr; |
| 286 |
unsigned char *priv = NULL, *pub = NULL; |
| 287 |
size_t privlen = 0, publen = 0; |
| 288 |
int ret = 0; |
| 289 |
const EC_GROUP *group; |
| 290 |
|
| 291 |
if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) { |
| 292 |
ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) { |
| 297 |
publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL); |
| 298 |
if (publen == 0) |
| 299 |
goto err; |
| 300 |
} |
| 301 |
|
| 302 |
if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) { |
| 303 |
privlen = EC_KEY_priv2buf(x, &priv); |
| 304 |
if (privlen == 0) |
| 305 |
goto err; |
| 306 |
} |
| 307 |
|
| 308 |
if (ktype == EC_KEY_PRINT_PRIVATE) |
| 309 |
ecstr = "Private-Key"; |
| 310 |
else if (ktype == EC_KEY_PRINT_PUBLIC) |
| 311 |
ecstr = "Public-Key"; |
| 312 |
else |
| 313 |
ecstr = "ECDSA-Parameters"; |
| 314 |
|
| 315 |
if (!BIO_indent(bp, off, 128)) |
| 316 |
goto err; |
| 317 |
if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, |
| 318 |
EC_GROUP_order_bits(group)) |
| 319 |
<= 0) |
| 320 |
goto err; |
| 321 |
|
| 322 |
if (privlen != 0) { |
| 323 |
if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0) |
| 324 |
goto err; |
| 325 |
if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0) |
| 326 |
goto err; |
| 327 |
} |
| 328 |
|
| 329 |
if (publen != 0) { |
| 330 |
if (BIO_printf(bp, "%*spub:\n", off, "") <= 0) |
| 331 |
goto err; |
| 332 |
if (ASN1_buf_print(bp, pub, publen, off + 4) == 0) |
| 333 |
goto err; |
| 334 |
} |
| 335 |
|
| 336 |
if (!ECPKParameters_print(bp, group, off)) |
| 337 |
goto err; |
| 338 |
ret = 1; |
| 339 |
err: |
| 340 |
if (!ret) |
| 341 |
ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
| 342 |
OPENSSL_clear_free(priv, privlen); |
| 343 |
OPENSSL_free(pub); |
| 344 |
return ret; |
| 345 |
} |
| 346 |
|
| 347 |
static int eckey_param_decode(EVP_PKEY *pkey, |
| 348 |
const unsigned char **pder, int derlen) |
| 349 |
{ |
| 350 |
EC_KEY *eckey; |
| 351 |
|
| 352 |
if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) |
| 353 |
return 0; |
| 354 |
EVP_PKEY_assign_EC_KEY(pkey, eckey); |
| 355 |
return 1; |
| 356 |
} |
| 357 |
|
| 358 |
static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder) |
| 359 |
{ |
| 360 |
return i2d_ECParameters(pkey->pkey.ec, pder); |
| 361 |
} |
| 362 |
|
| 363 |
static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, |
| 364 |
ASN1_PCTX *ctx) |
| 365 |
{ |
| 366 |
return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM); |
| 367 |
} |
| 368 |
|
| 369 |
static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, |
| 370 |
ASN1_PCTX *ctx) |
| 371 |
{ |
| 372 |
return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC); |
| 373 |
} |
| 374 |
|
| 375 |
static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, |
| 376 |
ASN1_PCTX *ctx) |
| 377 |
{ |
| 378 |
return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE); |
| 379 |
} |
| 380 |
|
| 381 |
static int old_ec_priv_decode(EVP_PKEY *pkey, |
| 382 |
const unsigned char **pder, int derlen) |
| 383 |
{ |
| 384 |
EC_KEY *ec; |
| 385 |
|
| 386 |
if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) |
| 387 |
return 0; |
| 388 |
EVP_PKEY_assign_EC_KEY(pkey, ec); |
| 389 |
return 1; |
| 390 |
} |
| 391 |
|
| 392 |
static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) |
| 393 |
{ |
| 394 |
return i2d_ECPrivateKey(pkey->pkey.ec, pder); |
| 395 |
} |
| 396 |
|
| 397 |
static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
| 398 |
{ |
| 399 |
switch (op) { |
| 400 |
case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
| 401 |
if (EVP_PKEY_get_id(pkey) == EVP_PKEY_SM2) { |
| 402 |
/* For SM2, the only valid digest-alg is SM3 */ |
| 403 |
*(int *)arg2 = NID_sm3; |
| 404 |
return 2; /* Make it mandatory */ |
| 405 |
} |
| 406 |
*(int *)arg2 = NID_sha256; |
| 407 |
return 1; |
| 408 |
|
| 409 |
case ASN1_PKEY_CTRL_SET1_TLS_ENCPT: |
| 410 |
/* We should only be here if we have a legacy key */ |
| 411 |
if (!ossl_assert(evp_pkey_is_legacy(pkey))) |
| 412 |
return 0; |
| 413 |
return EC_KEY_oct2key(evp_pkey_get0_EC_KEY_int(pkey), arg2, arg1, NULL); |
| 414 |
|
| 415 |
case ASN1_PKEY_CTRL_GET1_TLS_ENCPT: |
| 416 |
return (int)EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey), |
| 417 |
POINT_CONVERSION_UNCOMPRESSED, arg2, NULL); |
| 418 |
|
| 419 |
default: |
| 420 |
return -2; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
static int ec_pkey_check(const EVP_PKEY *pkey) |
| 425 |
{ |
| 426 |
EC_KEY *eckey = pkey->pkey.ec; |
| 427 |
|
| 428 |
/* stay consistent to what EVP_PKEY_check demands */ |
| 429 |
if (eckey->priv_key == NULL) { |
| 430 |
ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
return EC_KEY_check_key(eckey); |
| 435 |
} |
| 436 |
|
| 437 |
static int ec_pkey_public_check(const EVP_PKEY *pkey) |
| 438 |
{ |
| 439 |
EC_KEY *eckey = pkey->pkey.ec; |
| 440 |
|
| 441 |
/* |
| 442 |
* Note: it unnecessary to check eckey->pub_key here since |
| 443 |
* it will be checked in EC_KEY_check_key(). In fact, the |
| 444 |
* EC_KEY_check_key() mainly checks the public key, and checks |
| 445 |
* the private key optionally (only if there is one). So if |
| 446 |
* someone passes a whole EC key (public + private), this |
| 447 |
* will also work... |
| 448 |
*/ |
| 449 |
|
| 450 |
return EC_KEY_check_key(eckey); |
| 451 |
} |
| 452 |
|
| 453 |
static int ec_pkey_param_check(const EVP_PKEY *pkey) |
| 454 |
{ |
| 455 |
EC_KEY *eckey = pkey->pkey.ec; |
| 456 |
|
| 457 |
/* stay consistent to what EVP_PKEY_check demands */ |
| 458 |
if (eckey->group == NULL) { |
| 459 |
ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
return EC_GROUP_check(eckey->group, NULL); |
| 464 |
} |
| 465 |
|
| 466 |
static size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey) |
| 467 |
{ |
| 468 |
return pkey->pkey.ec->dirty_cnt; |
| 469 |
} |
| 470 |
|
| 471 |
static int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata, |
| 472 |
OSSL_FUNC_keymgmt_import_fn *importer, |
| 473 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 474 |
{ |
| 475 |
const EC_KEY *eckey = NULL; |
| 476 |
const EC_GROUP *ecg = NULL; |
| 477 |
unsigned char *pub_key_buf = NULL, *gen_buf = NULL; |
| 478 |
size_t pub_key_buflen; |
| 479 |
OSSL_PARAM_BLD *tmpl; |
| 480 |
OSSL_PARAM *params = NULL; |
| 481 |
const BIGNUM *priv_key = NULL; |
| 482 |
const EC_POINT *pub_point = NULL; |
| 483 |
int selection = 0; |
| 484 |
int rv = 0; |
| 485 |
BN_CTX *bnctx = NULL; |
| 486 |
|
| 487 |
if (from == NULL |
| 488 |
|| (eckey = from->pkey.ec) == NULL |
| 489 |
|| (ecg = EC_KEY_get0_group(eckey)) == NULL) |
| 490 |
return 0; |
| 491 |
|
| 492 |
tmpl = OSSL_PARAM_BLD_new(); |
| 493 |
if (tmpl == NULL) |
| 494 |
return 0; |
| 495 |
|
| 496 |
/* |
| 497 |
* EC_POINT_point2buf() can generate random numbers in some |
| 498 |
* implementations so we need to ensure we use the correct libctx. |
| 499 |
*/ |
| 500 |
bnctx = BN_CTX_new_ex(libctx); |
| 501 |
if (bnctx == NULL) |
| 502 |
goto err; |
| 503 |
BN_CTX_start(bnctx); |
| 504 |
|
| 505 |
/* export the domain parameters */ |
| 506 |
if (!ossl_ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf)) |
| 507 |
goto err; |
| 508 |
selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; |
| 509 |
|
| 510 |
priv_key = EC_KEY_get0_private_key(eckey); |
| 511 |
pub_point = EC_KEY_get0_public_key(eckey); |
| 512 |
|
| 513 |
if (pub_point != NULL) { |
| 514 |
/* convert pub_point to a octet string according to the SECG standard */ |
| 515 |
point_conversion_form_t format = EC_KEY_get_conv_form(eckey); |
| 516 |
|
| 517 |
if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point, |
| 518 |
format, |
| 519 |
&pub_key_buf, bnctx)) |
| 520 |
== 0 |
| 521 |
|| !OSSL_PARAM_BLD_push_octet_string(tmpl, |
| 522 |
OSSL_PKEY_PARAM_PUB_KEY, |
| 523 |
pub_key_buf, |
| 524 |
pub_key_buflen)) |
| 525 |
goto err; |
| 526 |
selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
| 527 |
} |
| 528 |
|
| 529 |
if (priv_key != NULL) { |
| 530 |
size_t sz; |
| 531 |
int ecbits; |
| 532 |
int ecdh_cofactor_mode; |
| 533 |
|
| 534 |
/* |
| 535 |
* Key import/export should never leak the bit length of the secret |
| 536 |
* scalar in the key. |
| 537 |
* |
| 538 |
* For this reason, on export we use padded BIGNUMs with fixed length. |
| 539 |
* |
| 540 |
* When importing we also should make sure that, even if short lived, |
| 541 |
* the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as |
| 542 |
* soon as possible, so that any processing of this BIGNUM might opt for |
| 543 |
* constant time implementations in the backend. |
| 544 |
* |
| 545 |
* Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have |
| 546 |
* to preallocate the BIGNUM internal buffer to a fixed public size big |
| 547 |
* enough that operations performed during the processing never trigger |
| 548 |
* a realloc which would leak the size of the scalar through memory |
| 549 |
* accesses. |
| 550 |
* |
| 551 |
* Fixed Length |
| 552 |
* ------------ |
| 553 |
* |
| 554 |
* The order of the large prime subgroup of the curve is our choice for |
| 555 |
* a fixed public size, as that is generally the upper bound for |
| 556 |
* generating a private key in EC cryptosystems and should fit all valid |
| 557 |
* secret scalars. |
| 558 |
* |
| 559 |
* For padding on export we just use the bit length of the order |
| 560 |
* converted to bytes (rounding up). |
| 561 |
* |
| 562 |
* For preallocating the BIGNUM storage we look at the number of "words" |
| 563 |
* required for the internal representation of the order, and we |
| 564 |
* preallocate 2 extra "words" in case any of the subsequent processing |
| 565 |
* might temporarily overflow the order length. |
| 566 |
*/ |
| 567 |
ecbits = EC_GROUP_order_bits(ecg); |
| 568 |
if (ecbits <= 0) |
| 569 |
goto err; |
| 570 |
|
| 571 |
sz = (ecbits + 7) / 8; |
| 572 |
if (!OSSL_PARAM_BLD_push_BN_pad(tmpl, |
| 573 |
OSSL_PKEY_PARAM_PRIV_KEY, |
| 574 |
priv_key, sz)) |
| 575 |
goto err; |
| 576 |
selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
| 577 |
|
| 578 |
/* |
| 579 |
* The ECDH Cofactor Mode is defined only if the EC_KEY actually |
| 580 |
* contains a private key, so we check for the flag and export it only |
| 581 |
* in this case. |
| 582 |
*/ |
| 583 |
ecdh_cofactor_mode = (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0; |
| 584 |
|
| 585 |
/* Export the ECDH_COFACTOR_MODE parameter */ |
| 586 |
if (!OSSL_PARAM_BLD_push_int(tmpl, |
| 587 |
OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, |
| 588 |
ecdh_cofactor_mode)) |
| 589 |
goto err; |
| 590 |
selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS; |
| 591 |
} |
| 592 |
|
| 593 |
params = OSSL_PARAM_BLD_to_param(tmpl); |
| 594 |
|
| 595 |
/* We export, the provider imports */ |
| 596 |
rv = importer(to_keydata, selection, params); |
| 597 |
|
| 598 |
err: |
| 599 |
OSSL_PARAM_BLD_free(tmpl); |
| 600 |
OSSL_PARAM_free(params); |
| 601 |
OPENSSL_free(pub_key_buf); |
| 602 |
OPENSSL_free(gen_buf); |
| 603 |
BN_CTX_end(bnctx); |
| 604 |
BN_CTX_free(bnctx); |
| 605 |
return rv; |
| 606 |
} |
| 607 |
|
| 608 |
static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx) |
| 609 |
{ |
| 610 |
EVP_PKEY_CTX *pctx = vpctx; |
| 611 |
EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx); |
| 612 |
EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery); |
| 613 |
|
| 614 |
if (ec == NULL) { |
| 615 |
ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
if (!ossl_ec_group_fromdata(ec, params) |
| 620 |
|| !ossl_ec_key_otherparams_fromdata(ec, params) |
| 621 |
|| !ossl_ec_key_fromdata(ec, params, 1) |
| 622 |
|| !EVP_PKEY_assign_EC_KEY(pkey, ec)) { |
| 623 |
EC_KEY_free(ec); |
| 624 |
return 0; |
| 625 |
} |
| 626 |
return 1; |
| 627 |
} |
| 628 |
|
| 629 |
static int ec_pkey_copy(EVP_PKEY *to, EVP_PKEY *from) |
| 630 |
{ |
| 631 |
EC_KEY *eckey = from->pkey.ec; |
| 632 |
EC_KEY *dupkey = NULL; |
| 633 |
int ret; |
| 634 |
|
| 635 |
if (eckey != NULL) { |
| 636 |
dupkey = EC_KEY_dup(eckey); |
| 637 |
if (dupkey == NULL) |
| 638 |
return 0; |
| 639 |
} else { |
| 640 |
/* necessary to properly copy empty SM2 keys */ |
| 641 |
return EVP_PKEY_set_type(to, from->type); |
| 642 |
} |
| 643 |
|
| 644 |
ret = EVP_PKEY_assign_EC_KEY(to, dupkey); |
| 645 |
if (!ret) |
| 646 |
EC_KEY_free(dupkey); |
| 647 |
return ret; |
| 648 |
} |
| 649 |
|
| 650 |
const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth = { |
| 651 |
EVP_PKEY_EC, |
| 652 |
EVP_PKEY_EC, |
| 653 |
0, |
| 654 |
"EC", |
| 655 |
"OpenSSL EC algorithm", |
| 656 |
|
| 657 |
eckey_pub_decode, |
| 658 |
eckey_pub_encode, |
| 659 |
eckey_pub_cmp, |
| 660 |
eckey_pub_print, |
| 661 |
|
| 662 |
NULL, |
| 663 |
eckey_priv_encode, |
| 664 |
eckey_priv_print, |
| 665 |
|
| 666 |
int_ec_size, |
| 667 |
ec_bits, |
| 668 |
ec_security_bits, |
| 669 |
|
| 670 |
eckey_param_decode, |
| 671 |
eckey_param_encode, |
| 672 |
ec_missing_parameters, |
| 673 |
ec_copy_parameters, |
| 674 |
ec_cmp_parameters, |
| 675 |
eckey_param_print, |
| 676 |
0, |
| 677 |
|
| 678 |
int_ec_free, |
| 679 |
ec_pkey_ctrl, |
| 680 |
old_ec_priv_decode, |
| 681 |
old_ec_priv_encode, |
| 682 |
|
| 683 |
0, 0, 0, |
| 684 |
|
| 685 |
ec_pkey_check, |
| 686 |
ec_pkey_public_check, |
| 687 |
ec_pkey_param_check, |
| 688 |
|
| 689 |
0, /* set_priv_key */ |
| 690 |
0, /* set_pub_key */ |
| 691 |
0, /* get_priv_key */ |
| 692 |
0, /* get_pub_key */ |
| 693 |
|
| 694 |
ec_pkey_dirty_cnt, |
| 695 |
ec_pkey_export_to, |
| 696 |
ec_pkey_import_from, |
| 697 |
ec_pkey_copy, |
| 698 |
eckey_priv_decode_ex |
| 699 |
}; |
| 700 |
|
| 701 |
#if !defined(OPENSSL_NO_SM2) |
| 702 |
const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth = { |
| 703 |
EVP_PKEY_SM2, |
| 704 |
EVP_PKEY_EC, |
| 705 |
ASN1_PKEY_ALIAS |
| 706 |
}; |
| 707 |
#endif |
| 708 |
|
| 709 |
int EC_KEY_print(BIO *bp, const EC_KEY *x, int off) |
| 710 |
{ |
| 711 |
int private = EC_KEY_get0_private_key(x) != NULL; |
| 712 |
|
| 713 |
return do_EC_KEY_print(bp, x, off, |
| 714 |
private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC); |
| 715 |
} |
| 716 |
|
| 717 |
int ECParameters_print(BIO *bp, const EC_KEY *x) |
| 718 |
{ |
| 719 |
return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM); |
| 720 |
} |