Skip to content

Commit e8f94f2

Browse files
committed
crypto: add memory tracking for secureContext openssl objects
1 parent 049664b commit e8f94f2

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/crypto/crypto_context.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,12 @@ void SecureContext::GetCertificate(const FunctionCallbackInfo<Value>& args) {
20952095
args.GetReturnValue().Set(buff);
20962096
}
20972097

2098+
void SecureContext::MemoryInfo(MemoryTracker* tracker) const {
2099+
tracker->TrackFieldWithSize("ctx", ctx_ ? kSizeOf_SSL_CTX : 0);
2100+
tracker->TrackFieldWithSize("cert", cert_ ? kSizeOf_X509 : 0);
2101+
tracker->TrackFieldWithSize("issuer", issuer_ ? kSizeOf_X509 : 0);
2102+
}
2103+
20982104
// UseExtraCaCerts is called only once at the start of the Node.js process.
20992105
void UseExtraCaCerts(std::string_view file) {
21002106
extra_root_certs_file = file;

src/crypto/crypto_context.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class SecureContext final : public BaseObject {
6767
void SetX509StoreFlag(unsigned long flags); // NOLINT(runtime/int)
6868
X509_STORE* GetCertStoreOwnedByThisSecureContext();
6969

70-
// TODO(joyeecheung): track the memory used by OpenSSL types
71-
SET_NO_MEMORY_INFO()
70+
void MemoryInfo(MemoryTracker* tracker) const override;
7271
SET_MEMORY_INFO_NAME(SecureContext)
7372
SET_SELF_SIZE(SecureContext)
7473

src/crypto/crypto_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ constexpr size_t kSizeOf_EVP_MD_CTX = 48;
3636
constexpr size_t kSizeOf_EVP_PKEY = 72;
3737
constexpr size_t kSizeOf_EVP_PKEY_CTX = 80;
3838
constexpr size_t kSizeOf_HMAC_CTX = 32;
39+
constexpr size_t kSizeOf_SSL_CTX = 240;
40+
constexpr size_t kSizeOf_X509 = 128;
3941

4042
bool ProcessFipsOptions();
4143

test/cctest/test_node_crypto.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,30 @@ TEST(NodeCrypto, NewRootCertStore) {
2121
"any errors on the OpenSSL error stack\n";
2222
X509_STORE_free(store);
2323
}
24+
25+
/*
26+
* This test verifies that OpenSSL memory tracking constants are properly
27+
* defined.
28+
*/
29+
TEST(NodeCrypto, MemoryTrackingConstants) {
30+
// Verify that our memory tracking constants are defined and reasonable
31+
EXPECT_GT(node::crypto::kSizeOf_SSL_CTX, 0)
32+
<< "SSL_CTX size constant should be positive";
33+
EXPECT_GT(node::crypto::kSizeOf_X509, 0)
34+
<< "X509 size constant should be positive";
35+
EXPECT_GT(node::crypto::kSizeOf_EVP_MD_CTX, 0)
36+
<< "EVP_MD_CTX size constant should be positive";
37+
38+
// Verify reasonable size ranges (basic sanity check)
39+
EXPECT_LT(node::crypto::kSizeOf_SSL_CTX, 10000)
40+
<< "SSL_CTX size should be reasonable";
41+
EXPECT_LT(node::crypto::kSizeOf_X509, 10000)
42+
<< "X509 size should be reasonable";
43+
EXPECT_LT(node::crypto::kSizeOf_EVP_MD_CTX, 1000)
44+
<< "EVP_MD_CTX size should be reasonable";
45+
46+
// Specific values we expect based on our implementation
47+
EXPECT_EQ(node::crypto::kSizeOf_SSL_CTX, 240);
48+
EXPECT_EQ(node::crypto::kSizeOf_X509, 128);
49+
EXPECT_EQ(node::crypto::kSizeOf_EVP_MD_CTX, 48);
50+
}

0 commit comments

Comments
 (0)