diff options
| author | tb <> | 2024-10-30 06:12:47 +0000 | 
|---|---|---|
| committer | tb <> | 2024-10-30 06:12:47 +0000 | 
| commit | fa9d25519948c3bde6618189431ae19d466f004c (patch) | |
| tree | cce29c4b46357e35b78470cd6265629b2468f593 /src | |
| parent | 39811d03692554850f931608c0c2a3e9536c85fb (diff) | |
| download | openbsd-fa9d25519948c3bde6618189431ae19d466f004c.tar.gz openbsd-fa9d25519948c3bde6618189431ae19d466f004c.tar.bz2 openbsd-fa9d25519948c3bde6618189431ae19d466f004c.zip | |
Provide ec_point_to_asn1_bit_string()
This adds a specialized helper for creating an ASN.1 bit string
out of an elliptic curve point (the public key) and use it in
i2d_ECPrivateKey().
ok jsing
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/ec/ec_asn1.c | 44 | 
1 files changed, 14 insertions, 30 deletions
| diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c index 09aa947b71..5881580b0d 100644 --- a/src/lib/libcrypto/ec/ec_asn1.c +++ b/src/lib/libcrypto/ec/ec_asn1.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ec_asn1.c,v 1.95 2024/10/30 06:11:50 tb Exp $ */ | 1 | /* $OpenBSD: ec_asn1.c,v 1.96 2024/10/30 06:12:47 tb Exp $ */ | 
| 2 | /* | 2 | /* | 
| 3 | * Written by Nils Larsch for the OpenSSL project. | 3 | * Written by Nils Larsch for the OpenSSL project. | 
| 4 | */ | 4 | */ | 
| @@ -597,6 +597,17 @@ ec_point_to_asn1_string_type(const EC_GROUP *group, const EC_POINT *point, | |||
| 597 | } | 597 | } | 
| 598 | 598 | ||
| 599 | static int | 599 | static int | 
| 600 | ec_point_to_asn1_bit_string(const EC_GROUP *group, const EC_POINT *point, | ||
| 601 | int form, ASN1_BIT_STRING **out_abs) | ||
| 602 | { | ||
| 603 | if (!ec_point_to_asn1_string_type(group, point, form, | ||
| 604 | V_ASN1_BIT_STRING, out_abs)) | ||
| 605 | return 0; | ||
| 606 | |||
| 607 | return asn1_abs_set_unused_bits(*out_abs, 0); | ||
| 608 | } | ||
| 609 | |||
| 610 | static int | ||
| 600 | ec_point_to_asn1_octet_string(const EC_GROUP *group, const EC_POINT *point, | 611 | ec_point_to_asn1_octet_string(const EC_GROUP *group, const EC_POINT *point, | 
| 601 | int form, ASN1_OCTET_STRING **out_aos) | 612 | int form, ASN1_OCTET_STRING **out_aos) | 
| 602 | { | 613 | { | 
| @@ -1271,8 +1282,6 @@ int | |||
| 1271 | i2d_ECPrivateKey(EC_KEY *ec_key, unsigned char **out) | 1282 | i2d_ECPrivateKey(EC_KEY *ec_key, unsigned char **out) | 
| 1272 | { | 1283 | { | 
| 1273 | int ret = 0, ok = 0; | 1284 | int ret = 0, ok = 0; | 
| 1274 | unsigned char *buffer = NULL; | ||
| 1275 | size_t buf_len = 0; | ||
| 1276 | EC_PRIVATEKEY *ec_privatekey = NULL; | 1285 | EC_PRIVATEKEY *ec_privatekey = NULL; | 
| 1277 | 1286 | ||
| 1278 | if (ec_key == NULL || ec_key->group == NULL || ec_key->priv_key == NULL || | 1287 | if (ec_key == NULL || ec_key->group == NULL || ec_key->priv_key == NULL || | 
| @@ -1299,33 +1308,9 @@ i2d_ECPrivateKey(EC_KEY *ec_key, unsigned char **out) | |||
| 1299 | ec_privatekey->parameters = parameters; | 1308 | ec_privatekey->parameters = parameters; | 
| 1300 | } | 1309 | } | 
| 1301 | if (!(ec_key->enc_flag & EC_PKEY_NO_PUBKEY) && ec_key->pub_key != NULL) { | 1310 | if (!(ec_key->enc_flag & EC_PKEY_NO_PUBKEY) && ec_key->pub_key != NULL) { | 
| 1302 | ec_privatekey->publicKey = ASN1_BIT_STRING_new(); | 1311 | if (!ec_point_to_asn1_bit_string(ec_key->group, ec_key->pub_key, | 
| 1303 | if (ec_privatekey->publicKey == NULL) { | 1312 | ec_key->conv_form, &ec_privatekey->publicKey)) | 
| 1304 | ECerror(ERR_R_MALLOC_FAILURE); | ||
| 1305 | goto err; | ||
| 1306 | } | ||
| 1307 | if ((buf_len = EC_POINT_point2oct(ec_key->group, ec_key->pub_key, | ||
| 1308 | ec_key->conv_form, NULL, 0, NULL)) == 0) { | ||
| 1309 | ECerror(ERR_R_EC_LIB); | ||
| 1310 | goto err; | ||
| 1311 | } | ||
| 1312 | if ((buffer = calloc(1, buf_len)) == NULL) { | ||
| 1313 | ECerror(ERR_R_MALLOC_FAILURE); | ||
| 1314 | goto err; | ||
| 1315 | } | ||
| 1316 | if (!EC_POINT_point2oct(ec_key->group, ec_key->pub_key, | ||
| 1317 | ec_key->conv_form, buffer, buf_len, NULL)) { | ||
| 1318 | ECerror(ERR_R_EC_LIB); | ||
| 1319 | goto err; | ||
| 1320 | } | ||
| 1321 | if (!ASN1_STRING_set(ec_privatekey->publicKey, buffer, buf_len)) { | ||
| 1322 | ECerror(ERR_R_ASN1_LIB); | ||
| 1323 | goto err; | ||
| 1324 | } | ||
| 1325 | if (!asn1_abs_set_unused_bits(ec_privatekey->publicKey, 0)) { | ||
| 1326 | ECerror(ERR_R_ASN1_LIB); | ||
| 1327 | goto err; | 1313 | goto err; | 
| 1328 | } | ||
| 1329 | } | 1314 | } | 
| 1330 | if ((ret = i2d_EC_PRIVATEKEY(ec_privatekey, out)) == 0) { | 1315 | if ((ret = i2d_EC_PRIVATEKEY(ec_privatekey, out)) == 0) { | 
| 1331 | ECerror(ERR_R_EC_LIB); | 1316 | ECerror(ERR_R_EC_LIB); | 
| @@ -1333,7 +1318,6 @@ i2d_ECPrivateKey(EC_KEY *ec_key, unsigned char **out) | |||
| 1333 | } | 1318 | } | 
| 1334 | ok = 1; | 1319 | ok = 1; | 
| 1335 | err: | 1320 | err: | 
| 1336 | free(buffer); | ||
| 1337 | if (ec_privatekey) | 1321 | if (ec_privatekey) | 
| 1338 | EC_PRIVATEKEY_free(ec_privatekey); | 1322 | EC_PRIVATEKEY_free(ec_privatekey); | 
| 1339 | return (ok ? ret : 0); | 1323 | return (ok ? ret : 0); | 
