Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf3aaa3

Browse filesBrowse 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
Copy full SHA for bf3aaa3

File tree

Expand file treeCollapse file tree

3 files changed

+112
-59
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+112
-59
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/keys.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js
+76-59Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77

88
const {
99
KeyObjectHandle,
10+
createNativeKeyObjectClass,
1011
kKeyTypeSecret,
1112
kKeyTypePublic,
1213
kKeyTypePrivate,
@@ -42,80 +43,96 @@ for (const m of [[kKeyEncodingPKCS1, 'pkcs1'], [kKeyEncodingPKCS8, 'pkcs8'],
4243
[kKeyEncodingSPKI, 'spki'], [kKeyEncodingSEC1, 'sec1']])
4344
encodingNames[m[0]] = m[1];
4445

45-
class KeyObject {
46-
constructor(type, handle) {
47-
if (type !== 'secret' && type !== 'public' && type !== 'private')
48-
throw new ERR_INVALID_ARG_VALUE('type', type);
49-
if (typeof handle !== 'object')
50-
throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle);
51-
52-
this[kKeyType] = type;
53-
54-
ObjectDefineProperty(this, kHandle, {
55-
value: handle,
56-
enumerable: false,
57-
configurable: false,
58-
writable: false
59-
});
60-
}
46+
// Creating the KeyObject class is a little complicated due to inheritance
47+
// and that fact that KeyObjects should be transferrable between threads,
48+
// which requires the KeyObject base class to be implemented in C++.
49+
// The creation requires a callback to make sure that the NativeKeyObject
50+
// base class cannot exist without the other KeyObject implementations.
51+
const [
52+
KeyObject,
53+
SecretKeyObject,
54+
PublicKeyObject,
55+
PrivateKeyObject
56+
] = createNativeKeyObjectClass((NativeKeyObject) => {
57+
// Publicly visible KeyObject class.
58+
class KeyObject extends NativeKeyObject {
59+
constructor(type, handle) {
60+
super();
61+
if (type !== 'secret' && type !== 'public' && type !== 'private')
62+
throw new ERR_INVALID_ARG_VALUE('type', type);
63+
if (typeof handle !== 'object')
64+
throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle);
65+
66+
this[kKeyType] = type;
67+
68+
ObjectDefineProperty(this, kHandle, {
69+
value: handle,
70+
enumerable: false,
71+
configurable: false,
72+
writable: false
73+
});
74+
}
6175

62-
get type() {
63-
return this[kKeyType];
76+
get type() {
77+
return this[kKeyType];
78+
}
6479
}
65-
}
6680

67-
class SecretKeyObject extends KeyObject {
68-
constructor(handle) {
69-
super('secret', handle);
70-
}
81+
class SecretKeyObject extends KeyObject {
82+
constructor(handle) {
83+
super('secret', handle);
84+
}
7185

72-
get symmetricKeySize() {
73-
return this[kHandle].getSymmetricKeySize();
74-
}
86+
get symmetricKeySize() {
87+
return this[kHandle].getSymmetricKeySize();
88+
}
7589

76-
export() {
77-
return this[kHandle].export();
90+
export() {
91+
return this[kHandle].export();
92+
}
7893
}
79-
}
8094

81-
const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
95+
const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
8296

83-
class AsymmetricKeyObject extends KeyObject {
84-
get asymmetricKeyType() {
85-
return this[kAsymmetricKeyType] ||
86-
(this[kAsymmetricKeyType] = this[kHandle].getAsymmetricKeyType());
97+
class AsymmetricKeyObject extends KeyObject {
98+
get asymmetricKeyType() {
99+
return this[kAsymmetricKeyType] ||
100+
(this[kAsymmetricKeyType] = this[kHandle].getAsymmetricKeyType());
101+
}
87102
}
88-
}
89103

90-
class PublicKeyObject extends AsymmetricKeyObject {
91-
constructor(handle) {
92-
super('public', handle);
93-
}
104+
class PublicKeyObject extends AsymmetricKeyObject {
105+
constructor(handle) {
106+
super('public', handle);
107+
}
94108

95-
export(encoding) {
96-
const {
97-
format,
98-
type
99-
} = parsePublicKeyEncoding(encoding, this.asymmetricKeyType);
100-
return this[kHandle].export(format, type);
109+
export(encoding) {
110+
const {
111+
format,
112+
type
113+
} = parsePublicKeyEncoding(encoding, this.asymmetricKeyType);
114+
return this[kHandle].export(format, type);
115+
}
101116
}
102-
}
103117

104-
class PrivateKeyObject extends AsymmetricKeyObject {
105-
constructor(handle) {
106-
super('private', handle);
107-
}
118+
class PrivateKeyObject extends AsymmetricKeyObject {
119+
constructor(handle) {
120+
super('private', handle);
121+
}
108122

109-
export(encoding) {
110-
const {
111-
format,
112-
type,
113-
cipher,
114-
passphrase
115-
} = parsePrivateKeyEncoding(encoding, this.asymmetricKeyType);
116-
return this[kHandle].export(format, type, cipher, passphrase);
123+
export(encoding) {
124+
const {
125+
format,
126+
type,
127+
cipher,
128+
passphrase
129+
} = parsePrivateKeyEncoding(encoding, this.asymmetricKeyType);
130+
return this[kHandle].export(format, type, cipher, passphrase);
131+
}
117132
}
118-
}
133+
134+
return [KeyObject, SecretKeyObject, PublicKeyObject, PrivateKeyObject];
135+
});
119136

120137
function parseKeyFormat(formatStr, defaultFormat, optionName) {
121138
if (formatStr === undefined && defaultFormat !== undefined)
Collapse file

‎src/node_crypto.cc‎

Copy file name to clipboardExpand all lines: src/node_crypto.cc
+27Lines 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);
Collapse file

‎src/node_crypto.h‎

Copy file name to clipboardExpand all lines: src/node_crypto.h
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,15 @@ class KeyObjectHandle : public BaseObject {
462462
ManagedEVPPKey asymmetric_key_;
463463
};
464464

465+
class NativeKeyObject : public BaseObject {
466+
public:
467+
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
468+
469+
SET_NO_MEMORY_INFO()
470+
SET_MEMORY_INFO_NAME(NativeKeyObject)
471+
SET_SELF_SIZE(NativeKeyObject)
472+
};
473+
465474
class CipherBase : public BaseObject {
466475
public:
467476
static void Initialize(Environment* env, v8::Local<v8::Object> target);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.