| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/ec.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/bn.h> |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/nid.h> |
| 25 |
| 26 | #include "../bytestring/internal.h" |
| 27 | #include "../fipsmodule/ec/internal.h" |
| 28 | #include "../internal.h" |
| 29 |
| 30 |
| 31 | static const CBS_ASN1_TAG kParametersTag = |
| 32 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 33 | static const CBS_ASN1_TAG kPublicKeyTag = |
| 34 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 35 |
| 36 | // TODO(https://crbug.com/boringssl/497): Allow parsers to specify a list of |
| 37 | // acceptable groups, so parsers don't have to pull in all four. |
| 38 | typedef const EC_GROUP *(*ec_group_func)(void); |
| 39 | static const ec_group_func kAllGroups[] = { |
| 40 | &EC_group_p224, |
| 41 | &EC_group_p256, |
| 42 | &EC_group_p384, |
| 43 | &EC_group_p521, |
| 44 | }; |
| 45 |
| 46 | EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) { |
| 47 | CBS ec_private_key, private_key; |
| 48 | uint64_t version; |
| 49 | if (!CBS_get_asn1(cbs, &ec_private_key, CBS_ASN1_SEQUENCE) || |
| 50 | !CBS_get_asn1_uint64(&ec_private_key, &version) || // |
| 51 | version != 1 || |
| 52 | !CBS_get_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING)) { |
| 53 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 54 | return NULL; |
| 55 | } |
| 56 |
| 57 | // Parse the optional parameters field. |
| 58 | EC_KEY *ret = NULL; |
| 59 | BIGNUM *priv_key = NULL; |
| 60 | if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) { |
| 61 | // Per SEC 1, as an alternative to omitting it, one is allowed to specify |
| 62 | // this field and put in a NULL to mean inheriting this value. This was |
| 63 | // omitted in a previous version of this logic without problems, so leave it |
| 64 | // unimplemented. |
| 65 | CBS child; |
| 66 | if (!CBS_get_asn1(&ec_private_key, &child, kParametersTag)) { |
| 67 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 68 | goto err; |
| 69 | } |
| 70 | const EC_GROUP *inner_group = EC_KEY_parse_parameters(&child); |
| 71 | if (inner_group == NULL) { |
| 72 | goto err; |
| 73 | } |
| 74 | if (group == NULL) { |
| 75 | group = inner_group; |
| 76 | } else if (EC_GROUP_cmp(group, inner_group, NULL) != 0) { |
| 77 | // If a group was supplied externally, it must match. |
| 78 | OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH); |
| 79 | goto err; |
| 80 | } |
| 81 | if (CBS_len(&child) != 0) { |
| 82 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 83 | goto err; |
| 84 | } |
| 85 | } |
| 86 |
| 87 | if (group == NULL) { |
| 88 | OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS); |
| 89 | goto err; |
| 90 | } |
| 91 |
| 92 | ret = EC_KEY_new(); |
| 93 | if (ret == NULL || !EC_KEY_set_group(ret, group)) { |
| 94 | goto err; |
| 95 | } |
| 96 |
| 97 | // Although RFC 5915 specifies the length of the key, OpenSSL historically |
| 98 | // got this wrong, so accept any length. See upstream's |
| 99 | // 30cd4ff294252c4b6a4b69cbef6a5b4117705d22. |
| 100 | priv_key = BN_bin2bn(CBS_data(&private_key), CBS_len(&private_key), NULL); |
| 101 | ret->pub_key = EC_POINT_new(group); |
| 102 | if (priv_key == NULL || ret->pub_key == NULL || |
| 103 | !EC_KEY_set_private_key(ret, priv_key)) { |
| 104 | goto err; |
| 105 | } |
| 106 |
| 107 | if (CBS_peek_asn1_tag(&ec_private_key, kPublicKeyTag)) { |
| 108 | CBS child, public_key; |
| 109 | uint8_t padding; |
| 110 | if (!CBS_get_asn1(&ec_private_key, &child, kPublicKeyTag) || |
| 111 | !CBS_get_asn1(&child, &public_key, CBS_ASN1_BITSTRING) || |
| 112 | // As in a SubjectPublicKeyInfo, the byte-encoded public key is then |
| 113 | // encoded as a BIT STRING with bits ordered as in the DER encoding. |
| 114 | !CBS_get_u8(&public_key, &padding) || // |
| 115 | padding != 0 || |
| 116 | // Explicitly check |public_key| is non-empty to save the conversion |
| 117 | // form later. |
| 118 | CBS_len(&public_key) == 0 || |
| 119 | !EC_POINT_oct2point(group, ret->pub_key, CBS_data(&public_key), |
| 120 | CBS_len(&public_key), NULL) || |
| 121 | CBS_len(&child) != 0) { |
| 122 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 123 | goto err; |
| 124 | } |
| 125 |
| 126 | // Save the point conversion form. |
| 127 | // TODO(davidben): Consider removing this. |
| 128 | ret->conv_form = |
| 129 | (point_conversion_form_t)(CBS_data(&public_key)[0] & ~0x01); |
| 130 | } else { |
| 131 | // Compute the public key instead. |
| 132 | if (!ec_point_mul_scalar_base(group, &ret->pub_key->raw, |
| 133 | &ret->priv_key->scalar)) { |
| 134 | goto err; |
| 135 | } |
| 136 | // Remember the original private-key-only encoding. |
| 137 | // TODO(davidben): Consider removing this. |
| 138 | ret->enc_flag |= EC_PKEY_NO_PUBKEY; |
| 139 | } |
| 140 |
| 141 | if (CBS_len(&ec_private_key) != 0) { |
| 142 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 143 | goto err; |
| 144 | } |
| 145 |
| 146 | // Ensure the resulting key is valid. |
| 147 | if (!EC_KEY_check_key(ret)) { |
| 148 | goto err; |
| 149 | } |
| 150 |
| 151 | BN_free(priv_key); |
| 152 | return ret; |
| 153 |
| 154 | err: |
| 155 | EC_KEY_free(ret); |
| 156 | BN_free(priv_key); |
| 157 | return NULL; |
| 158 | } |
| 159 |
| 160 | int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key, |
| 161 | unsigned enc_flags) { |
| 162 | if (key == NULL || key->group == NULL || key->priv_key == NULL) { |
| 163 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 164 | return 0; |
| 165 | } |
| 166 |
| 167 | CBB ec_private_key, private_key; |
| 168 | if (!CBB_add_asn1(cbb, &ec_private_key, CBS_ASN1_SEQUENCE) || |
| 169 | !CBB_add_asn1_uint64(&ec_private_key, 1 /* version */) || |
| 170 | !CBB_add_asn1(&ec_private_key, &private_key, CBS_ASN1_OCTETSTRING) || |
| 171 | !BN_bn2cbb_padded(&private_key, |
| 172 | BN_num_bytes(EC_GROUP_get0_order(key->group)), |
| 173 | EC_KEY_get0_private_key(key))) { |
| 174 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 175 | return 0; |
| 176 | } |
| 177 |
| 178 | if (!(enc_flags & EC_PKEY_NO_PARAMETERS)) { |
| 179 | CBB child; |
| 180 | if (!CBB_add_asn1(&ec_private_key, &child, kParametersTag) || |
| 181 | !EC_KEY_marshal_curve_name(&child, key->group) || |
| 182 | !CBB_flush(&ec_private_key)) { |
| 183 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 184 | return 0; |
| 185 | } |
| 186 | } |
| 187 |
| 188 | // TODO(fork): replace this flexibility with sensible default? |
| 189 | if (!(enc_flags & EC_PKEY_NO_PUBKEY) && key->pub_key != NULL) { |
| 190 | CBB child, public_key; |
| 191 | if (!CBB_add_asn1(&ec_private_key, &child, kPublicKeyTag) || |
| 192 | !CBB_add_asn1(&child, &public_key, CBS_ASN1_BITSTRING) || |
| 193 | // As in a SubjectPublicKeyInfo, the byte-encoded public key is then |
| 194 | // encoded as a BIT STRING with bits ordered as in the DER encoding. |
| 195 | !CBB_add_u8(&public_key, 0 /* padding */) || |
| 196 | !EC_POINT_point2cbb(&public_key, key->group, key->pub_key, |
| 197 | key->conv_form, NULL) || |
| 198 | !CBB_flush(&ec_private_key)) { |
| 199 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 |
| 204 | if (!CBB_flush(cbb)) { |
| 205 | OPENSSL_PUT_ERROR(EC, EC_R_ENCODE_ERROR); |
| 206 | return 0; |
| 207 | } |
| 208 |
| 209 | return 1; |
| 210 | } |
| 211 |
| 212 | // kPrimeFieldOID is the encoding of 1.2.840.10045.1.1. |
| 213 | static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01}; |
| 214 |
| 215 | namespace { |
| 216 | struct explicit_prime_curve { |
| 217 | CBS prime, a, b, base_x, base_y, order; |
| 218 | }; |
| 219 | } // namespace |
| 220 |
| 221 | static int parse_explicit_prime_curve(CBS *in, |
| 222 | struct explicit_prime_curve *out) { |
| 223 | // See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an |
| 224 | // ECParameters while RFC 5480 calls it a SpecifiedECDomain. |
| 225 | CBS params, field_id, field_type, curve, base, cofactor; |
| 226 | int has_cofactor; |
| 227 | uint64_t version; |
| 228 | if (!CBS_get_asn1(in, ¶ms, CBS_ASN1_SEQUENCE) || |
| 229 | !CBS_get_asn1_uint64(¶ms, &version) || // |
| 230 | version != 1 || // |
| 231 | !CBS_get_asn1(¶ms, &field_id, CBS_ASN1_SEQUENCE) || |
| 232 | !CBS_get_asn1(&field_id, &field_type, CBS_ASN1_OBJECT) || |
| 233 | CBS_len(&field_type) != sizeof(kPrimeField) || |
| 234 | OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != |
| 235 | 0 || |
| 236 | !CBS_get_asn1(&field_id, &out->prime, CBS_ASN1_INTEGER) || |
| 237 | !CBS_is_unsigned_asn1_integer(&out->prime) || // |
| 238 | CBS_len(&field_id) != 0 || |
| 239 | !CBS_get_asn1(¶ms, &curve, CBS_ASN1_SEQUENCE) || |
| 240 | !CBS_get_asn1(&curve, &out->a, CBS_ASN1_OCTETSTRING) || |
| 241 | !CBS_get_asn1(&curve, &out->b, CBS_ASN1_OCTETSTRING) || |
| 242 | // |curve| has an optional BIT STRING seed which we ignore. |
| 243 | !CBS_get_optional_asn1(&curve, NULL, NULL, CBS_ASN1_BITSTRING) || |
| 244 | CBS_len(&curve) != 0 || |
| 245 | !CBS_get_asn1(¶ms, &base, CBS_ASN1_OCTETSTRING) || |
| 246 | !CBS_get_asn1(¶ms, &out->order, CBS_ASN1_INTEGER) || |
| 247 | !CBS_is_unsigned_asn1_integer(&out->order) || |
| 248 | !CBS_get_optional_asn1(¶ms, &cofactor, &has_cofactor, |
| 249 | CBS_ASN1_INTEGER) || |
| 250 | CBS_len(¶ms) != 0) { |
| 251 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 252 | return 0; |
| 253 | } |
| 254 |
| 255 | if (has_cofactor) { |
| 256 | // We only support prime-order curves so the cofactor must be one. |
| 257 | if (CBS_len(&cofactor) != 1 || // |
| 258 | CBS_data(&cofactor)[0] != 1) { |
| 259 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 |
| 264 | // Require that the base point use uncompressed form. |
| 265 | uint8_t form; |
| 266 | if (!CBS_get_u8(&base, &form) || form != POINT_CONVERSION_UNCOMPRESSED) { |
| 267 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
| 268 | return 0; |
| 269 | } |
| 270 |
| 271 | if (CBS_len(&base) % 2 != 0) { |
| 272 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 273 | return 0; |
| 274 | } |
| 275 | size_t field_len = CBS_len(&base) / 2; |
| 276 | CBS_init(&out->base_x, CBS_data(&base), field_len); |
| 277 | CBS_init(&out->base_y, CBS_data(&base) + field_len, field_len); |
| 278 |
| 279 | return 1; |
| 280 | } |
| 281 |
| 282 | // integers_equal returns one if |bytes| is a big-endian encoding of |bn|, and |
| 283 | // zero otherwise. |
| 284 | static int integers_equal(const CBS *bytes, const BIGNUM *bn) { |
| 285 | // Although, in SEC 1, Field-Element-to-Octet-String has a fixed width, |
| 286 | // OpenSSL mis-encodes the |a| and |b|, so we tolerate any number of leading |
| 287 | // zeros. (This matters for P-521 whose |b| has a leading 0.) |
| 288 | CBS copy = *bytes; |
| 289 | while (CBS_len(©) > 0 && CBS_data(©)[0] == 0) { |
| 290 | CBS_skip(©, 1); |
| 291 | } |
| 292 |
| 293 | if (CBS_len(©) > EC_MAX_BYTES) { |
| 294 | return 0; |
| 295 | } |
| 296 | uint8_t buf[EC_MAX_BYTES]; |
| 297 | if (!BN_bn2bin_padded(buf, CBS_len(©), bn)) { |
| 298 | ERR_clear_error(); |
| 299 | return 0; |
| 300 | } |
| 301 |
| 302 | return CBS_mem_equal(©, buf, CBS_len(©)); |
| 303 | } |
| 304 |
| 305 | EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) { |
| 306 | CBS named_curve; |
| 307 | if (!CBS_get_asn1(cbs, &named_curve, CBS_ASN1_OBJECT)) { |
| 308 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 309 | return NULL; |
| 310 | } |
| 311 |
| 312 | // Look for a matching curve. |
| 313 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kAllGroups); i++) { |
| 314 | const EC_GROUP *group = kAllGroups[i](); |
| 315 | if (CBS_mem_equal(&named_curve, group->oid, group->oid_len)) { |
| 316 | return (EC_GROUP *)group; |
| 317 | } |
| 318 | } |
| 319 |
| 320 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 321 | return NULL; |
| 322 | } |
| 323 |
| 324 | int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group) { |
| 325 | if (group->oid_len == 0) { |
| 326 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 327 | return 0; |
| 328 | } |
| 329 |
| 330 | CBB child; |
| 331 | return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) && |
| 332 | CBB_add_bytes(&child, group->oid, group->oid_len) && // |
| 333 | CBB_flush(cbb); |
| 334 | } |
| 335 |
| 336 | EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) { |
| 337 | if (!CBS_peek_asn1_tag(cbs, CBS_ASN1_SEQUENCE)) { |
| 338 | return EC_KEY_parse_curve_name(cbs); |
| 339 | } |
| 340 |
| 341 | // OpenSSL sometimes produces ECPrivateKeys with explicitly-encoded versions |
| 342 | // of named curves. |
| 343 | // |
| 344 | // TODO(davidben): Remove support for this. |
| 345 | struct explicit_prime_curve curve; |
| 346 | if (!parse_explicit_prime_curve(cbs, &curve)) { |
| 347 | return NULL; |
| 348 | } |
| 349 |
| 350 | const EC_GROUP *ret = NULL; |
| 351 | BIGNUM *p = BN_new(), *a = BN_new(), *b = BN_new(), *x = BN_new(), |
| 352 | *y = BN_new(); |
| 353 | if (p == NULL || a == NULL || b == NULL || x == NULL || y == NULL) { |
| 354 | goto err; |
| 355 | } |
| 356 |
| 357 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kAllGroups); i++) { |
| 358 | const EC_GROUP *group = kAllGroups[i](); |
| 359 | if (!integers_equal(&curve.order, EC_GROUP_get0_order(group))) { |
| 360 | continue; |
| 361 | } |
| 362 |
| 363 | // The order alone uniquely identifies the group, but we check the other |
| 364 | // parameters to avoid misinterpreting the group. |
| 365 | if (!EC_GROUP_get_curve_GFp(group, p, a, b, NULL)) { |
| 366 | goto err; |
| 367 | } |
| 368 | if (!integers_equal(&curve.prime, p) || !integers_equal(&curve.a, a) || |
| 369 | !integers_equal(&curve.b, b)) { |
| 370 | break; |
| 371 | } |
| 372 | if (!EC_POINT_get_affine_coordinates_GFp( |
| 373 | group, EC_GROUP_get0_generator(group), x, y, NULL)) { |
| 374 | goto err; |
| 375 | } |
| 376 | if (!integers_equal(&curve.base_x, x) || |
| 377 | !integers_equal(&curve.base_y, y)) { |
| 378 | break; |
| 379 | } |
| 380 | ret = group; |
| 381 | break; |
| 382 | } |
| 383 |
| 384 | if (ret == NULL) { |
| 385 | OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); |
| 386 | } |
| 387 |
| 388 | err: |
| 389 | BN_free(p); |
| 390 | BN_free(a); |
| 391 | BN_free(b); |
| 392 | BN_free(x); |
| 393 | BN_free(y); |
| 394 | return (EC_GROUP *)ret; |
| 395 | } |
| 396 |
| 397 | int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point, |
| 398 | point_conversion_form_t form, BN_CTX *ctx) { |
| 399 | size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); |
| 400 | if (len == 0) { |
| 401 | return 0; |
| 402 | } |
| 403 | uint8_t *p; |
| 404 | return CBB_add_space(out, &p, len) && |
| 405 | EC_POINT_point2oct(group, point, form, p, len, ctx) == len; |
| 406 | } |
| 407 |
| 408 | EC_KEY *d2i_ECPrivateKey(EC_KEY **out, const uint8_t **inp, long len) { |
| 409 | // This function treats its |out| parameter differently from other |d2i| |
| 410 | // functions. If supplied, take the group from |*out|. |
| 411 | const EC_GROUP *group = NULL; |
| 412 | if (out != NULL && *out != NULL) { |
| 413 | group = EC_KEY_get0_group(*out); |
| 414 | } |
| 415 |
| 416 | if (len < 0) { |
| 417 | OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); |
| 418 | return NULL; |
| 419 | } |
| 420 | CBS cbs; |
| 421 | CBS_init(&cbs, *inp, (size_t)len); |
| 422 | EC_KEY *ret = EC_KEY_parse_private_key(&cbs, group); |
| 423 | if (ret == NULL) { |
| 424 | return NULL; |
| 425 | } |
| 426 | if (out != NULL) { |
| 427 | EC_KEY_free(*out); |
| 428 | *out = ret; |
| 429 | } |
| 430 | *inp = CBS_data(&cbs); |
| 431 | return ret; |
| 432 | } |
| 433 |
| 434 | int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp) { |
| 435 | CBB cbb; |
| 436 | if (!CBB_init(&cbb, 0) || |
| 437 | !EC_KEY_marshal_private_key(&cbb, key, EC_KEY_get_enc_flags(key))) { |
| 438 | CBB_cleanup(&cbb); |
| 439 | return -1; |
| 440 | } |
| 441 | return CBB_finish_i2d(&cbb, outp); |
| 442 | } |
| 443 |
| 444 | EC_GROUP *d2i_ECPKParameters(EC_GROUP **out, const uint8_t **inp, long len) { |
| 445 | if (len < 0) { |
| 446 | return NULL; |
| 447 | } |
| 448 |
| 449 | CBS cbs; |
| 450 | CBS_init(&cbs, *inp, (size_t)len); |
| 451 | EC_GROUP *ret = EC_KEY_parse_parameters(&cbs); |
| 452 | if (ret == NULL) { |
| 453 | return NULL; |
| 454 | } |
| 455 |
| 456 | if (out != NULL) { |
| 457 | EC_GROUP_free(*out); |
| 458 | *out = ret; |
| 459 | } |
| 460 | *inp = CBS_data(&cbs); |
| 461 | return ret; |
| 462 | } |
| 463 |
| 464 | int i2d_ECPKParameters(const EC_GROUP *group, uint8_t **outp) { |
| 465 | if (group == NULL) { |
| 466 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 467 | return -1; |
| 468 | } |
| 469 |
| 470 | CBB cbb; |
| 471 | if (!CBB_init(&cbb, 0) || // |
| 472 | !EC_KEY_marshal_curve_name(&cbb, group)) { |
| 473 | CBB_cleanup(&cbb); |
| 474 | return -1; |
| 475 | } |
| 476 | return CBB_finish_i2d(&cbb, outp); |
| 477 | } |
| 478 |
| 479 | EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) { |
| 480 | if (len < 0) { |
| 481 | return NULL; |
| 482 | } |
| 483 |
| 484 | CBS cbs; |
| 485 | CBS_init(&cbs, *inp, (size_t)len); |
| 486 | const EC_GROUP *group = EC_KEY_parse_parameters(&cbs); |
| 487 | if (group == NULL) { |
| 488 | return NULL; |
| 489 | } |
| 490 |
| 491 | EC_KEY *ret = EC_KEY_new(); |
| 492 | if (ret == NULL || !EC_KEY_set_group(ret, group)) { |
| 493 | EC_KEY_free(ret); |
| 494 | return NULL; |
| 495 | } |
| 496 |
| 497 | if (out_key != NULL) { |
| 498 | EC_KEY_free(*out_key); |
| 499 | *out_key = ret; |
| 500 | } |
| 501 | *inp = CBS_data(&cbs); |
| 502 | return ret; |
| 503 | } |
| 504 |
| 505 | int i2d_ECParameters(const EC_KEY *key, uint8_t **outp) { |
| 506 | if (key == NULL || key->group == NULL) { |
| 507 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 508 | return -1; |
| 509 | } |
| 510 |
| 511 | CBB cbb; |
| 512 | if (!CBB_init(&cbb, 0) || // |
| 513 | !EC_KEY_marshal_curve_name(&cbb, key->group)) { |
| 514 | CBB_cleanup(&cbb); |
| 515 | return -1; |
| 516 | } |
| 517 | return CBB_finish_i2d(&cbb, outp); |
| 518 | } |
| 519 |
| 520 | EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) { |
| 521 | EC_KEY *ret = NULL; |
| 522 |
| 523 | if (keyp == NULL || *keyp == NULL || (*keyp)->group == NULL) { |
| 524 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 525 | return NULL; |
| 526 | } |
| 527 | ret = *keyp; |
| 528 | if (ret->pub_key == NULL && |
| 529 | (ret->pub_key = EC_POINT_new(ret->group)) == NULL) { |
| 530 | return NULL; |
| 531 | } |
| 532 | if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) { |
| 533 | OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB); |
| 534 | return NULL; |
| 535 | } |
| 536 | // save the point conversion form |
| 537 | ret->conv_form = (point_conversion_form_t)(*inp[0] & ~0x01); |
| 538 | *inp += len; |
| 539 | return ret; |
| 540 | } |
| 541 |
| 542 | int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) { |
| 543 | if (key == NULL) { |
| 544 | OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); |
| 545 | return 0; |
| 546 | } |
| 547 | CBB cbb; |
| 548 | if (!CBB_init(&cbb, 0) || // |
| 549 | !EC_POINT_point2cbb(&cbb, key->group, key->pub_key, key->conv_form, |
| 550 | NULL)) { |
| 551 | CBB_cleanup(&cbb); |
| 552 | return -1; |
| 553 | } |
| 554 | int ret = CBB_finish_i2d(&cbb, outp); |
| 555 | // Historically, this function used the wrong return value on error. |
| 556 | return ret > 0 ? ret : 0; |
| 557 | } |
| 558 |
| 559 | size_t EC_get_builtin_curves(EC_builtin_curve *out_curves, |
| 560 | size_t max_num_curves) { |
| 561 | if (max_num_curves > OPENSSL_ARRAY_SIZE(kAllGroups)) { |
| 562 | max_num_curves = OPENSSL_ARRAY_SIZE(kAllGroups); |
| 563 | } |
| 564 | for (size_t i = 0; i < max_num_curves; i++) { |
| 565 | const EC_GROUP *group = kAllGroups[i](); |
| 566 | out_curves[i].nid = group->curve_name; |
| 567 | out_curves[i].comment = group->comment; |
| 568 | } |
| 569 | return OPENSSL_ARRAY_SIZE(kAllGroups); |
| 570 | } |
| 571 |