Skip to content

Commit 2e06b85

Browse files
tniessentargos
authored andcommitted
crypto: automatically manage memory for ECDSA_SIG
Refs: #29292 PR-URL: #30641 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent fb4f71b commit 2e06b85

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/node_crypto.cc

+8-11
Original file line numberDiff line numberDiff line change
@@ -5038,20 +5038,18 @@ static AllocatedBuffer ConvertSignatureToP1363(Environment* env,
50385038
const unsigned char* sig_data =
50395039
reinterpret_cast<unsigned char*>(signature.data());
50405040

5041-
ECDSA_SIG* asn1_sig = d2i_ECDSA_SIG(nullptr, &sig_data, signature.size());
5042-
if (asn1_sig == nullptr)
5041+
ECDSASigPointer asn1_sig(d2i_ECDSA_SIG(nullptr, &sig_data, signature.size()));
5042+
if (!asn1_sig)
50435043
return AllocatedBuffer();
50445044

50455045
AllocatedBuffer buf = env->AllocateManaged(2 * n);
50465046
unsigned char* data = reinterpret_cast<unsigned char*>(buf.data());
50475047

5048-
const BIGNUM* r = ECDSA_SIG_get0_r(asn1_sig);
5049-
const BIGNUM* s = ECDSA_SIG_get0_s(asn1_sig);
5048+
const BIGNUM* r = ECDSA_SIG_get0_r(asn1_sig.get());
5049+
const BIGNUM* s = ECDSA_SIG_get0_s(asn1_sig.get());
50505050
CHECK_EQ(n, static_cast<unsigned int>(BN_bn2binpad(r, data, n)));
50515051
CHECK_EQ(n, static_cast<unsigned int>(BN_bn2binpad(s, data + n, n)));
50525052

5053-
ECDSA_SIG_free(asn1_sig);
5054-
50555053
return buf;
50565054
}
50575055

@@ -5068,19 +5066,18 @@ static ByteSource ConvertSignatureToDER(
50685066
if (signature.length() != 2 * n)
50695067
return ByteSource();
50705068

5071-
ECDSA_SIG* asn1_sig = ECDSA_SIG_new();
5072-
CHECK_NOT_NULL(asn1_sig);
5069+
ECDSASigPointer asn1_sig(ECDSA_SIG_new());
5070+
CHECK(asn1_sig);
50735071
BIGNUM* r = BN_new();
50745072
CHECK_NOT_NULL(r);
50755073
BIGNUM* s = BN_new();
50765074
CHECK_NOT_NULL(s);
50775075
CHECK_EQ(r, BN_bin2bn(sig_data, n, r));
50785076
CHECK_EQ(s, BN_bin2bn(sig_data + n, n, s));
5079-
CHECK_EQ(1, ECDSA_SIG_set0(asn1_sig, r, s));
5077+
CHECK_EQ(1, ECDSA_SIG_set0(asn1_sig.get(), r, s));
50805078

50815079
unsigned char* data = nullptr;
5082-
int len = i2d_ECDSA_SIG(asn1_sig, &data);
5083-
ECDSA_SIG_free(asn1_sig);
5080+
int len = i2d_ECDSA_SIG(asn1_sig.get(), &data);
50845081

50855082
if (len <= 0)
50865083
return ByteSource();

src/node_crypto.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>;
7272
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
7373
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
7474
using DHPointer = DeleteFnPtr<DH, DH_free>;
75+
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
7576

7677
extern int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx);
7778

0 commit comments

Comments
 (0)