src: add NativeKeyObject base class · nodejs/node@bf3aaa3 · GitHub
Skip to content

Commit bf3aaa3

Browse files
tniessenaddaleax
authored andcommitted
src: add NativeKeyObject base class
+---------------------+ | BaseObject | +---------------------+ | | | +---------------------+ | NativeKeyObject | +---------------------+ | | | +---------------------+ | KeyObject | +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | SecretKeyObject | | AsymmetricKeyObject | +---------------------+ +---------------------+ / \ / \ / \ / \ +---------------------+ +---------------------+ | PublicKeyObject | | PrivateKeyObject | +---------------------+ +---------------------+ PR-URL: #33360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9197882 commit bf3aaa3

3 files changed

Lines changed: 112 additions & 59 deletions

File tree

lib/internal/crypto/keys.js

Lines changed: 76 additions & 59 deletions

src/node_crypto.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,6 +3430,31 @@ MaybeLocal<Value> KeyObjectHandle::ExportPrivateKey(
34303430
return WritePrivateKey(env(), asymmetric_key_.get(), config);
34313431
}
34323432

3433+
void NativeKeyObject::New(const FunctionCallbackInfo<Value>& args) {
3434+
CHECK_EQ(args.Length(), 0);
3435+
}
3436+
3437+
static void CreateNativeKeyObjectClass(
3438+
const FunctionCallbackInfo<Value>& args) {
3439+
Environment* env = Environment::GetCurrent(args);
3440+
3441+
CHECK_EQ(args.Length(), 1);
3442+
Local<Value> callback = args[0];
3443+
CHECK(callback->IsFunction());
3444+
3445+
Local<FunctionTemplate> t = env->NewFunctionTemplate(NativeKeyObject::New);
3446+
t->InstanceTemplate()->SetInternalFieldCount(
3447+
KeyObjectHandle::kInternalFieldCount);
3448+
3449+
Local<Value> ctor = t->GetFunction(env->context()).ToLocalChecked();
3450+
3451+
Local<Value> recv = Undefined(env->isolate());
3452+
Local<Value> ret =
3453+
callback.As<Function>()->Call(env->context(), recv, 1, &ctor)
3454+
.ToLocalChecked();
3455+
args.GetReturnValue().Set(ret);
3456+
}
3457+
34333458
CipherBase::CipherBase(Environment* env,
34343459
Local<Object> wrap,
34353460
CipherKind kind)
@@ -6901,6 +6926,8 @@ void Initialize(Local<Object> target,
69016926
SecureContext::Initialize(env, target);
69026927
env->set_crypto_key_object_handle_constructor(
69036928
KeyObjectHandle::Initialize(env, target));
6929+
env->SetMethod(target, "createNativeKeyObjectClass",
6930+
CreateNativeKeyObjectClass);
69046931
CipherBase::Initialize(env, target);
69056932
DiffieHellman::Initialize(env, target);
69066933
ECDH::Initialize(env, target);

src/node_crypto.h

Lines changed: 9 additions & 0 deletions

0 commit comments

Comments
 (0)