Skip to content

Commit

Permalink
src: register external references in crypto bindings
Browse files Browse the repository at this point in the history
PR-URL: #40239
Refs: #38905
Refs: #37476
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed Oct 23, 2021
1 parent fafc553 commit a11f9ea
Show file tree
Hide file tree
Showing 38 changed files with 405 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/crypto/crypto_aes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,9 @@ void AES::Initialize(Environment* env, Local<Object> target) {
#undef V
}

void AES::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
AESCryptoJob::RegisterExternalReferences(registry);
}

} // namespace crypto
} // namespace node
1 change: 1 addition & 0 deletions src/crypto/crypto_aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ using AESCryptoJob = CipherJob<AESCipherTraits>;

namespace AES {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
} // namespace AES
} // namespace crypto
} // namespace node
Expand Down
32 changes: 32 additions & 0 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,38 @@ void CipherBase::Initialize(Environment* env, Local<Object> target) {
NODE_DEFINE_CONSTANT(target, kWebCryptoCipherDecrypt);
}

void CipherBase::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);

registry->Register(Init);
registry->Register(InitIv);
registry->Register(Update);
registry->Register(Final);
registry->Register(SetAutoPadding);
registry->Register(GetAuthTag);
registry->Register(SetAuthTag);
registry->Register(SetAAD);

registry->Register(GetSSLCiphers);
registry->Register(GetCiphers);

registry->Register(PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_encrypt_init,
EVP_PKEY_encrypt>);
registry->Register(PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_decrypt_init,
EVP_PKEY_decrypt>);
registry->Register(PublicKeyCipher::Cipher<PublicKeyCipher::kPrivate,
EVP_PKEY_sign_init,
EVP_PKEY_sign>);
registry->Register(PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_verify_recover_init,
EVP_PKEY_verify_recover>);

registry->Register(GetCipherInfo);
}

void CipherBase::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall());
Environment* env = Environment::GetCurrent(args);
Expand Down
5 changes: 5 additions & 0 deletions src/crypto/crypto_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CipherBase : public BaseObject {
static void GetCiphers(const v8::FunctionCallbackInfo<v8::Value>& args);

static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(CipherBase)
Expand Down Expand Up @@ -190,6 +191,10 @@ class CipherJob final : public CryptoJob<CipherTraits> {
CryptoJob<CipherTraits>::Initialize(New, env, target);
}

static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
CryptoJob<CipherTraits>::RegisterExternalReferences(New, registry);
}

CipherJob(
Environment* env,
v8::Local<v8::Object> object,
Expand Down
41 changes: 41 additions & 0 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,47 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
IsExtraRootCertsFileLoaded);
}

void SecureContext::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Init);
registry->Register(SetKey);
registry->Register(SetCert);
registry->Register(AddCACert);
registry->Register(AddCRL);
registry->Register(AddRootCerts);
registry->Register(SetCipherSuites);
registry->Register(SetCiphers);
registry->Register(SetSigalgs);
registry->Register(SetECDHCurve);
registry->Register(SetDHParam);
registry->Register(SetMaxProto);
registry->Register(SetMinProto);
registry->Register(GetMaxProto);
registry->Register(GetMinProto);
registry->Register(SetOptions);
registry->Register(SetSessionIdContext);
registry->Register(SetSessionTimeout);
registry->Register(Close);
registry->Register(LoadPKCS12);
registry->Register(SetTicketKeys);
registry->Register(SetFreeListLength);
registry->Register(EnableTicketKeyCallback);
registry->Register(GetTicketKeys);
registry->Register(GetCertificate<true>);
registry->Register(GetCertificate<false>);

#ifndef OPENSSL_NO_ENGINE
registry->Register(SetEngineKey);
registry->Register(SetClientCertEngine);
#endif // !OPENSSL_NO_ENGINE

registry->Register(CtxGetter);

registry->Register(GetRootCertificates);
registry->Register(IsExtraRootCertsFileLoaded);
}

SecureContext* SecureContext::Create(Environment* env) {
Local<Object> obj;
if (!GetConstructorTemplate(env)
Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SecureContext final : public BaseObject {
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static SecureContext* Create(Environment* env);

SSL_CTX* operator*() const { return ctx_.get(); }
Expand Down
22 changes: 22 additions & 0 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
DHBitsJob::Initialize(env, target);
}

void DiffieHellman::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(DiffieHellmanGroup);

registry->Register(GenerateKeys);
registry->Register(ComputeSecret);
registry->Register(GetPrime);
registry->Register(GetGenerator);
registry->Register(GetPublicKey);
registry->Register(GetPrivateKey);
registry->Register(SetPublicKey);
registry->Register(SetPrivateKey);

registry->Register(DiffieHellman::VerifyErrorGetter);
registry->Register(DiffieHellman::Stateless);

DHKeyPairGenJob::RegisterExternalReferences(registry);
DHKeyExportJob::RegisterExternalReferences(registry);
DHBitsJob::RegisterExternalReferences(registry);
}

bool DiffieHellman::Init(int primeLength, int g) {
dh_.reset(DH_new());
if (!DH_generate_parameters_ex(dh_.get(), primeLength, g, nullptr))
Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto_dh.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace crypto {
class DiffieHellman : public BaseObject {
public:
static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

bool Init(int primeLength, int g);
bool Init(const char* p, int p_len, int g);
Expand Down
5 changes: 5 additions & 0 deletions src/crypto/crypto_dsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ void Initialize(Environment* env, Local<Object> target) {
DsaKeyPairGenJob::Initialize(env, target);
DSAKeyExportJob::Initialize(env, target);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
DsaKeyPairGenJob::RegisterExternalReferences(registry);
DSAKeyExportJob::RegisterExternalReferences(registry);
}
} // namespace DSAAlg
} // namespace crypto
} // namespace node
1 change: 1 addition & 0 deletions src/crypto/crypto_dsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ v8::Maybe<bool> GetDsaKeyDetail(

namespace DSAAlg {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
} // namespace DSAAlg
} // namespace crypto
} // namespace node
Expand Down
16 changes: 16 additions & 0 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
NODE_DEFINE_CONSTANT(target, OPENSSL_EC_EXPLICIT_CURVE);
}

void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(GenerateKeys);
registry->Register(ComputeSecret);
registry->Register(GetPublicKey);
registry->Register(GetPrivateKey);
registry->Register(SetPublicKey);
registry->Register(SetPrivateKey);
registry->Register(ECDH::ConvertKey);
registry->Register(ECDH::GetCurves);

ECDHBitsJob::RegisterExternalReferences(registry);
ECKeyPairGenJob::RegisterExternalReferences(registry);
ECKeyExportJob::RegisterExternalReferences(registry);
}

void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ECDH final : public BaseObject {
~ECDH() override;

static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

static ECPointPointer BufferToPoint(Environment* env,
const EC_GROUP* group,
v8::Local<v8::Value> buf);
Expand Down
9 changes: 9 additions & 0 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ void Hash::Initialize(Environment* env, Local<Object> target) {
HashJob::Initialize(env, target);
}

void Hash::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(HashUpdate);
registry->Register(HashDigest);
registry->Register(GetHashes);

HashJob::RegisterExternalReferences(registry);
}

void Hash::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace crypto {
class Hash final : public BaseObject {
public:
static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Hash)
Expand Down
8 changes: 8 additions & 0 deletions src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ void Hmac::Initialize(Environment* env, Local<Object> target) {
HmacJob::Initialize(env, target);
}

void Hmac::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(HmacInit);
registry->Register(HmacUpdate);
registry->Register(HmacDigest);
HmacJob::RegisterExternalReferences(registry);
}

void Hmac::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
new Hmac(env, args.This());
Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto_hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace crypto {
class Hmac : public BaseObject {
public:
static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Hmac)
Expand Down
6 changes: 6 additions & 0 deletions src/crypto/crypto_keygen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ void Initialize(Environment* env, Local<Object> target) {
NidKeyPairGenJob::Initialize(env, target);
SecretKeyGenJob::Initialize(env, target);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
NidKeyPairGenJob::RegisterExternalReferences(registry);
SecretKeyGenJob::RegisterExternalReferences(registry);
}

} // namespace Keygen
} // namespace crypto
} // namespace node
5 changes: 5 additions & 0 deletions src/crypto/crypto_keygen.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace node {
namespace crypto {
namespace Keygen {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
} // namespace Keygen

enum class KeyGenJobStatus {
Expand Down Expand Up @@ -58,6 +59,10 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
CryptoJob<KeyGenTraits>::Initialize(New, env, target);
}

static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
CryptoJob<KeyGenTraits>::RegisterExternalReferences(New, registry);
}

KeyGenJob(
Environment* env,
v8::Local<v8::Object> object,
Expand Down
24 changes: 24 additions & 0 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,20 @@ v8::Local<v8::Function> KeyObjectHandle::Initialize(Environment* env) {
return function;
}

void KeyObjectHandle::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Init);
registry->Register(GetSymmetricKeySize);
registry->Register(GetAsymmetricKeyType);
registry->Register(Export);
registry->Register(ExportJWK);
registry->Register(InitECRaw);
registry->Register(InitEDRaw);
registry->Register(InitJWK);
registry->Register(GetKeyDetail);
}

MaybeLocal<Object> KeyObjectHandle::Create(
Environment* env,
std::shared_ptr<KeyObjectData> data) {
Expand Down Expand Up @@ -1256,6 +1270,12 @@ void NativeKeyObject::Initialize(Environment* env, Local<Object> target) {
NativeKeyObject::CreateNativeKeyObjectClass);
}

void NativeKeyObject::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(NativeKeyObject::CreateNativeKeyObjectClass);
registry->Register(NativeKeyObject::New);
}

void NativeKeyObject::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_EQ(args.Length(), 1);
Expand Down Expand Up @@ -1405,6 +1425,10 @@ void Initialize(Environment* env, Local<Object> target) {
NODE_DEFINE_CONSTANT(target, kSigEncDER);
NODE_DEFINE_CONSTANT(target, kSigEncP1363);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
KeyObjectHandle::RegisterExternalReferences(registry);
}
} // namespace Keys

} // namespace crypto
Expand Down
7 changes: 7 additions & 0 deletions src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class KeyObjectData : public MemoryRetainer {
class KeyObjectHandle : public BaseObject {
public:
static v8::Local<v8::Function> Initialize(Environment* env);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

static v8::MaybeLocal<v8::Object> Create(Environment* env,
std::shared_ptr<KeyObjectData> data);
Expand Down Expand Up @@ -216,6 +217,7 @@ class KeyObjectHandle : public BaseObject {
class NativeKeyObject : public BaseObject {
public:
static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void CreateNativeKeyObjectClass(
Expand Down Expand Up @@ -316,6 +318,10 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
CryptoJob<KeyExportTraits>::Initialize(New, env, target);
}

static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
CryptoJob<KeyExportTraits>::RegisterExternalReferences(New, registry);
}

KeyExportJob(
Environment* env,
v8::Local<v8::Object> object,
Expand Down Expand Up @@ -403,6 +409,7 @@ WebCryptoKeyExportStatus PKEY_PKCS8_Export(

namespace Keys {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
} // namespace Keys

} // namespace crypto
Expand Down
6 changes: 6 additions & 0 deletions src/crypto/crypto_random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ void Initialize(Environment* env, Local<Object> target) {
RandomPrimeJob::Initialize(env, target);
CheckPrimeJob::Initialize(env, target);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
RandomBytesJob::RegisterExternalReferences(registry);
RandomPrimeJob::RegisterExternalReferences(registry);
CheckPrimeJob::RegisterExternalReferences(registry);
}
} // namespace Random
} // namespace crypto
} // namespace node
Loading

0 comments on commit a11f9ea

Please sign in to comment.