summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortb <>2024-10-28 17:59:45 +0000
committertb <>2024-10-28 17:59:45 +0000
commit3730b3cfc1d512f1a6c71b83d5cbdecf724b845a (patch)
tree260aa70957b18c7a81a8ab39cfe3f65a9bd1b252 /src/lib
parent0bd57c6657dcc22878debfe15918c1d2a351a425 (diff)
downloadopenbsd-3730b3cfc1d512f1a6c71b83d5cbdecf724b845a.tar.gz
openbsd-3730b3cfc1d512f1a6c71b83d5cbdecf724b845a.tar.bz2
openbsd-3730b3cfc1d512f1a6c71b83d5cbdecf724b845a.zip
d2i_ECPrivateKey: split private key setting into a helper
Contrary to domain parameters and public key, the private key most be part of the DER. Convert that to a BIGNUM and set it on the EC_KEY. Use the dedicated setter for this (which will possibly call the handler of the EC_KEY_METHOD) rather than doing this by hand. ok jsing
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/ec/ec_asn1.c45
1 files changed, 31 insertions, 14 deletions
diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c
index 1ba1e36435..5234d4380a 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.90 2024/10/28 17:58:18 tb Exp $ */ 1/* $OpenBSD: ec_asn1.c,v 1.91 2024/10/28 17:59:45 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -1113,6 +1113,35 @@ ec_key_set_group_from_parameters(EC_KEY *ec_key, const ECPKPARAMETERS *params)
1113 return ret; 1113 return ret;
1114} 1114}
1115 1115
1116static int
1117ec_key_set_private_key(EC_KEY *ec_key, const ASN1_OCTET_STRING *aos)
1118{
1119 BIGNUM *priv_key = NULL;
1120 int ret = 0;
1121
1122 if (aos == NULL) {
1123 ECerror(EC_R_MISSING_PRIVATE_KEY);
1124 goto err;
1125 }
1126
1127 /*
1128 * XXX - Sec 1, C.4 requires that this octet string be padded to the
1129 * byte length of the group's order. This can't be enforced because
1130 * i2d_ECPrivateKey() produces a semi-compatible ad hoc format.
1131 */
1132 if ((priv_key = BN_bin2bn(aos->data, aos->length, NULL)) == NULL)
1133 goto err;
1134 if (!EC_KEY_set_private_key(ec_key, priv_key))
1135 goto err;
1136
1137 ret = 1;
1138
1139 err:
1140 BN_free(priv_key);
1141
1142 return ret;
1143}
1144
1116EC_KEY * 1145EC_KEY *
1117d2i_ECPrivateKey(EC_KEY **out_ec_key, const unsigned char **in, long len) 1146d2i_ECPrivateKey(EC_KEY **out_ec_key, const unsigned char **in, long len)
1118{ 1147{
@@ -1132,20 +1161,8 @@ d2i_ECPrivateKey(EC_KEY **out_ec_key, const unsigned char **in, long len)
1132 ec_key->version = ec_privatekey->version; 1161 ec_key->version = ec_privatekey->version;
1133 if (!ec_key_set_group_from_parameters(ec_key, ec_privatekey->parameters)) 1162 if (!ec_key_set_group_from_parameters(ec_key, ec_privatekey->parameters))
1134 goto err; 1163 goto err;
1135 1164 if (!ec_key_set_private_key(ec_key, ec_privatekey->privateKey))
1136 if (ec_privatekey->privateKey) {
1137 ec_key->priv_key = BN_bin2bn(
1138 ASN1_STRING_data(ec_privatekey->privateKey),
1139 ASN1_STRING_length(ec_privatekey->privateKey),
1140 ec_key->priv_key);
1141 if (ec_key->priv_key == NULL) {
1142 ECerror(ERR_R_BN_LIB);
1143 goto err;
1144 }
1145 } else {
1146 ECerror(EC_R_MISSING_PRIVATE_KEY);
1147 goto err; 1165 goto err;
1148 }
1149 1166
1150 if (ec_key->pub_key) 1167 if (ec_key->pub_key)
1151 EC_POINT_free(ec_key->pub_key); 1168 EC_POINT_free(ec_key->pub_key);