|
7 | 7 |
|
8 | 8 | const {
|
9 | 9 | KeyObjectHandle,
|
| 10 | + createNativeKeyObjectClass, |
10 | 11 | kKeyTypeSecret,
|
11 | 12 | kKeyTypePublic,
|
12 | 13 | kKeyTypePrivate,
|
@@ -42,80 +43,96 @@ for (const m of [[kKeyEncodingPKCS1, 'pkcs1'], [kKeyEncodingPKCS8, 'pkcs8'],
|
42 | 43 | [kKeyEncodingSPKI, 'spki'], [kKeyEncodingSEC1, 'sec1']])
|
43 | 44 | encodingNames[m[0]] = m[1];
|
44 | 45 |
|
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 | + } |
61 | 75 |
|
62 |
| - get type() { |
63 |
| - return this[kKeyType]; |
| 76 | + get type() { |
| 77 | + return this[kKeyType]; |
| 78 | + } |
64 | 79 | }
|
65 |
| -} |
66 | 80 |
|
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 | + } |
71 | 85 |
|
72 |
| - get symmetricKeySize() { |
73 |
| - return this[kHandle].getSymmetricKeySize(); |
74 |
| - } |
| 86 | + get symmetricKeySize() { |
| 87 | + return this[kHandle].getSymmetricKeySize(); |
| 88 | + } |
75 | 89 |
|
76 |
| - export() { |
77 |
| - return this[kHandle].export(); |
| 90 | + export() { |
| 91 | + return this[kHandle].export(); |
| 92 | + } |
78 | 93 | }
|
79 |
| -} |
80 | 94 |
|
81 |
| -const kAsymmetricKeyType = Symbol('kAsymmetricKeyType'); |
| 95 | + const kAsymmetricKeyType = Symbol('kAsymmetricKeyType'); |
82 | 96 |
|
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 | + } |
87 | 102 | }
|
88 |
| -} |
89 | 103 |
|
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 | + } |
94 | 108 |
|
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 | + } |
101 | 116 | }
|
102 |
| -} |
103 | 117 |
|
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 | + } |
108 | 122 |
|
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 | + } |
117 | 132 | }
|
118 |
| -} |
| 133 | + |
| 134 | + return [KeyObject, SecretKeyObject, PublicKeyObject, PrivateKeyObject]; |
| 135 | +}); |
119 | 136 |
|
120 | 137 | function parseKeyFormat(formatStr, defaultFormat, optionName) {
|
121 | 138 | if (formatStr === undefined && defaultFormat !== undefined)
|
|
0 commit comments