Skip to content

Commit dd118b7

Browse files
tniessenBethGriggs
authored andcommitted
crypto: automatically manage memory for ECDSA_SIG
Refs: #29292 PR-URL: #30641 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent a4ae272 commit dd118b7

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
@@ -5041,20 +5041,18 @@ static AllocatedBuffer ConvertSignatureToP1363(Environment* env,
50415041
const unsigned char* sig_data =
50425042
reinterpret_cast<unsigned char*>(signature.data());
50435043

5044-
ECDSA_SIG* asn1_sig = d2i_ECDSA_SIG(nullptr, &sig_data, signature.size());
5045-
if (asn1_sig == nullptr)
5044+
ECDSASigPointer asn1_sig(d2i_ECDSA_SIG(nullptr, &sig_data, signature.size()));
5045+
if (!asn1_sig)
50465046
return AllocatedBuffer();
50475047

50485048
AllocatedBuffer buf = env->AllocateManaged(2 * n);
50495049
unsigned char* data = reinterpret_cast<unsigned char*>(buf.data());
50505050

5051-
const BIGNUM* r = ECDSA_SIG_get0_r(asn1_sig);
5052-
const BIGNUM* s = ECDSA_SIG_get0_s(asn1_sig);
5051+
const BIGNUM* r = ECDSA_SIG_get0_r(asn1_sig.get());
5052+
const BIGNUM* s = ECDSA_SIG_get0_s(asn1_sig.get());
50535053
CHECK_EQ(n, static_cast<unsigned int>(BN_bn2binpad(r, data, n)));
50545054
CHECK_EQ(n, static_cast<unsigned int>(BN_bn2binpad(s, data + n, n)));
50555055

5056-
ECDSA_SIG_free(asn1_sig);
5057-
50585056
return buf;
50595057
}
50605058

@@ -5071,19 +5069,18 @@ static ByteSource ConvertSignatureToDER(
50715069
if (signature.length() != 2 * n)
50725070
return ByteSource();
50735071

5074-
ECDSA_SIG* asn1_sig = ECDSA_SIG_new();
5075-
CHECK_NOT_NULL(asn1_sig);
5072+
ECDSASigPointer asn1_sig(ECDSA_SIG_new());
5073+
CHECK(asn1_sig);
50765074
BIGNUM* r = BN_new();
50775075
CHECK_NOT_NULL(r);
50785076
BIGNUM* s = BN_new();
50795077
CHECK_NOT_NULL(s);
50805078
CHECK_EQ(r, BN_bin2bn(sig_data, n, r));
50815079
CHECK_EQ(s, BN_bin2bn(sig_data + n, n, s));
5082-
CHECK_EQ(1, ECDSA_SIG_set0(asn1_sig, r, s));
5080+
CHECK_EQ(1, ECDSA_SIG_set0(asn1_sig.get(), r, s));
50835081

50845082
unsigned char* data = nullptr;
5085-
int len = i2d_ECDSA_SIG(asn1_sig, &data);
5086-
ECDSA_SIG_free(asn1_sig);
5083+
int len = i2d_ECDSA_SIG(asn1_sig.get(), &data);
50875084

50885085
if (len <= 0)
50895086
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)