@tdxvolt/volt-client-grpc 0.11.0 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,58 +50,53 @@ function _doConnect() {
50
50
  const helloPayload = {
51
51
  hello: {},
52
52
  };
53
-
54
- this.call.start(grpcClient, helloPayload, (err, response) => {
55
- if (err) {
56
- log("error on connection stream [%s]", err.message);
57
- _cleanUp.call(this);
58
- this.emit("error", err);
59
- } else {
60
- switch (response.payload) {
61
- case "ping": {
62
- log("pinged");
63
-
64
- // Received ping response from server => clear timeout.
65
- if (this._pingTimeoutTimer) {
66
- clearTimeout(this._pingTimeoutTimer);
67
- }
68
-
69
- // Schedule the next ping.
70
- _startPingTimer.call(this);
71
- break;
72
- }
73
- case "hello": {
74
- if (!this._session) {
75
- this._session = true;
76
- this.emit("connected", this._session);
77
- }
78
- break;
79
- }
80
- case "evt": {
81
- log("received event %s", response.evt.event_type);
82
- this.emit("evt", response.evt);
83
- break;
84
- }
85
- default:
86
- log("unimplemented connect response payload: %s", response.payload);
87
- break;
88
- }
89
- }
90
- });
91
-
92
- this.call
93
- .wait()
94
- .then(() => {
95
- if (this._session) {
96
- this.emit("connected", false);
97
- }
98
- })
99
- .catch((err) => {
100
- log("unexpected error on connect call [%s]", err.message);
101
- })
102
- .finally(() => {
103
- _cleanUp.call(this);
104
- });
53
+
54
+ this.call.on("error", (err) => {
55
+ log("error on connection stream [%s]", err.message);
56
+ _cleanUp.call(this);
57
+ this.emit("error", err);
58
+ });
59
+
60
+ this.call.on("data", (response) => {
61
+ switch (response.payload) {
62
+ case "ping": {
63
+ log("pinged");
64
+
65
+ // Received ping response from server => clear timeout.
66
+ if (this._pingTimeoutTimer) {
67
+ clearTimeout(this._pingTimeoutTimer);
68
+ }
69
+
70
+ // Schedule the next ping.
71
+ _startPingTimer.call(this);
72
+ break;
73
+ }
74
+ case "hello": {
75
+ if (!this._session) {
76
+ this._session = true;
77
+ this.emit("connected", this._session);
78
+ }
79
+ break;
80
+ }
81
+ case "evt": {
82
+ log("received event %s", response.evt.event_type);
83
+ this.emit("evt", response.evt);
84
+ break;
85
+ }
86
+ default:
87
+ log("unimplemented connect response payload: %s", response.payload);
88
+ break;
89
+ }
90
+ });
91
+
92
+ this.call.on("end", () => {
93
+ if (this._session) {
94
+ this.emit("connected", false);
95
+ }
96
+ _cleanUp.call(this);
97
+ });
98
+
99
+ this.call.start(grpcClient, helloPayload);
105
100
 
106
101
  _startPingTimer.call(this);
107
102
  }
@@ -13,225 +13,225 @@ const aesAlgorithm = "aes-256-cbc";
13
13
  const rs256Algorithm = "RS256";
14
14
 
15
15
  export class VoltCredential {
16
- constructor(config, configPath) {
17
- this._config = config;
18
- this._voltConfig = config.volt;
19
- this._configPath = configPath;
20
- this._persistCache = !!configPath;
21
-
22
- if (this._config.credential) {
23
- this._cryptoCache = this._config.credential;
24
- } else {
25
- this._cryptoCache = {};
26
- }
27
-
28
- // Determine if we're dealing with an encrypted key.
29
- if (
30
- this._cryptoCache.key?.startsWith(constants.privateEncryptedKeyPrefix)
31
- ) {
32
- if (!config.p) {
33
- throw new Error("encrypted key but no passphrase given");
34
- }
35
-
36
- // Need to unencrypt the private key.
37
- const decrypted = pki.decryptRsaPrivateKey(
38
- this._cryptoCache.key,
39
- config.p,
40
- );
41
- if (!decrypted) {
42
- throw new Error("failed to decrypt key - check passphrase");
43
- }
44
- this._cryptoCache.key = pki.privateKeyToPem(decrypted);
45
- }
46
-
47
- if (this._voltConfig.ca_pem) {
48
- if (!this._cryptoCache.ca) {
49
- // Copy the volt signing CA to the crypto cache.
50
- this._cryptoCache.ca = this._voltConfig.ca_pem;
51
- }
52
-
53
- // Pre-cache the target Volt public key by extracting it from the signing certificate.
54
- const voltCert = forge.pki.certificateFromPem(this._voltConfig.ca_pem);
55
-
56
- // Extract Volt public key (used for encrypting Relay payloads).
57
- this._voltPublicKey = forge.pki.publicKeyToPem(voltCert.publicKey);
58
- this._voltFingerprint = voltUtils.createPublicKeyFingerprint(
59
- this._voltPublicKey,
60
- );
61
- }
62
- }
63
-
64
- get config() {
65
- return this._config;
66
- }
67
-
68
- get cache() {
69
- return this._cryptoCache;
70
- }
71
-
72
- get voltFingerprint() {
73
- return this._voltFingerprint;
74
- }
75
-
76
- getKey() {
77
- const privateKey = pki.privateKeyFromPem(this._cryptoCache.key);
78
- const publicKey = pki.rsa.setPublicKey(privateKey.n, privateKey.e);
79
- return { privateKey, publicKey };
80
- }
81
-
82
- /**
83
- * Retrieves and re-hydrates the pub/priv key data from the crypto cache, or creates one if not found.
84
- */
85
- createKey() {
86
- if (this._cryptoCache.key) {
87
- // Already have key data in the cache in pem format => create fully-formed pki instances.
88
- log("createKey - key already exists");
89
- return Promise.resolve(this.getKey());
90
- } else {
91
- log("creating key");
92
- return new Promise((resolve, reject) => {
93
- pki.rsa.generateKeyPair({ bits: 2048, workers: -1 }, (err, keypair) => {
94
- if (err) {
95
- log("failure creating key [%s]", err.message);
96
- reject(err);
97
- } else {
98
- log("successfully created key");
99
- const pem = pki.privateKeyToPem(keypair.privateKey);
100
- this._cryptoCache.key = pem;
101
- resolve(keypair);
102
- }
103
- });
104
- });
105
- }
106
- }
107
-
108
- /**
109
- * Create cryptocache from scratch.
110
- */
111
- initialise() {
112
- return this.createKey()
113
- .then((/* keypair */) => {
114
- return this.saveCache();
115
- })
116
- .then(() => {
117
- return this._cryptoCache;
118
- })
119
- .catch((err) => {
120
- log("failure initialising crypto [%s]", err.message);
121
- return Promise.reject(err);
122
- });
123
- }
124
-
125
- get isBound() {
126
- return !!this._cryptoCache.cert;
127
- }
128
-
129
- /**
130
- * Signs the identity resource id using the private key.
131
- * @param {*} audience the token audience (usually the target volt)
132
- * @param {*} tunnelling flag indicating if the token is required for a tunnelled connection
133
- * @param {*} ttl time to live in seconds (default to 1 minute)
134
- */
135
- getIdentityToken(audience, tunnelling = false, ttl = 60) {
136
- if (!this._cryptoCache.key) {
137
- throw new Error("crypto cache invalid");
138
- }
139
-
140
- const keyPair = this.getKey();
141
- const publicKeyPem = pki.publicKeyToPem(keyPair.publicKey);
142
- const base58Key = voltUtils.createPublicKeyFingerprint(publicKeyPem);
143
-
144
- log("base58 key is %s", base58Key);
145
-
146
- // Allow TTL either side of the current time.
147
- // @todo - fix with server time sync on volt connection.
148
- const payload = {
149
- aud: audience,
150
- iat: Math.floor(Date.now() / 1000) - ttl,
151
- exp: Math.floor(Date.now() / 1000) + ttl,
152
- };
153
-
154
- let sharedKey;
155
- if (tunnelling) {
156
- // Initialise the Relay encryption key.
157
- sharedKey = VoltCredential.aesCreateKey();
158
-
159
- // Encrypt the key details using the target Volt public key and include this in the JWT payload.
160
- payload.sk = VoltCredential.rsaEncrypt(
161
- this._voltPublicKey,
162
- sharedKey.key,
163
- );
164
- payload.iv = VoltCredential.rsaEncrypt(this._voltPublicKey, sharedKey.iv);
165
- }
166
-
167
- // Sign synchronously.
168
- const token = jwt.sign(payload, this._cryptoCache.key, {
169
- algorithm: rs256Algorithm,
170
- keyid: base58Key,
171
- });
172
-
173
- // Return the token along with any encryption key details.
174
- return {
175
- token,
176
- sharedKey,
177
- };
178
- }
179
-
180
- /**
181
- * Creates the metadata required for a grpc call, including the Volt authentication token
182
- * and optionally tunnel encryption parameters.
183
- * @param {*} grpc
184
- * @param {*} audience
185
- * @param {*} tunnelling
186
- * @param {*} ttl
187
- * @returns
188
- */
189
- getIdentityMetadata(grpc, audience, tunnelling = false, ttl = 60) {
190
- const identityToken = this.getIdentityToken(
191
- audience || this._voltConfig.id,
192
- tunnelling,
193
- ttl,
194
- );
195
- const metadata = new grpc.Metadata();
196
- metadata.add(constants.authTokenName, identityToken.token);
197
- return { ...identityToken, metadata };
198
- }
199
-
200
- saveCache() {
201
- if (this._persistCache) {
202
- const clone = { ...this._config, credential: { ...this._cryptoCache } };
203
- if (this._config.p) {
204
- // A passphrase option exists => encrypt the key before writing the cache file.
205
- const privateKey = pki.privateKeyFromPem(this._cryptoCache.key);
206
- clone.credential.key = pki.encryptRsaPrivateKey(
207
- privateKey,
208
- this._config.p,
209
- );
210
- }
211
- fs.writeFileSync(this._configPath, JSON.stringify(clone, null, 2));
212
- }
213
- }
214
-
215
- static aesCreateKey() {
216
- // Generate random key for the AES-256 algorithm (32 bytes = 256 bits).
217
- const key = crypto.randomBytes(32);
218
- const iv = crypto.randomBytes(16);
219
- return { key, iv };
220
- }
221
-
222
- static aesEncrypt(buffer, key, iv) {
223
- const cipher = crypto.createCipheriv(aesAlgorithm, key, iv);
224
- const crypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
225
- return crypted;
226
- }
227
-
228
- static aesDecrypt(buffer, key, iv) {
229
- const cipher = crypto.createDecipheriv(aesAlgorithm, key, iv);
230
- const decrypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
231
- return decrypted;
232
- }
233
-
234
- static rsaEncrypt(key, buffer) {
235
- return crypto.publicEncrypt(key, buffer).toString("base64");
236
- }
16
+ constructor(config, configPath) {
17
+ this._config = config;
18
+ this._voltConfig = config.volt;
19
+ this._configPath = configPath;
20
+ this._persistCache = !!configPath;
21
+
22
+ if (this._config.credential) {
23
+ this._cryptoCache = this._config.credential;
24
+ } else {
25
+ this._cryptoCache = {};
26
+ }
27
+
28
+ // Determine if we're dealing with an encrypted key.
29
+ if (
30
+ this._cryptoCache.key?.startsWith(constants.privateEncryptedKeyPrefix)
31
+ ) {
32
+ if (!config.p) {
33
+ throw new Error("encrypted key but no passphrase given");
34
+ }
35
+
36
+ // Need to unencrypt the private key.
37
+ const decrypted = pki.decryptRsaPrivateKey(
38
+ this._cryptoCache.key,
39
+ config.p,
40
+ );
41
+ if (!decrypted) {
42
+ throw new Error("failed to decrypt key - check passphrase");
43
+ }
44
+ this._cryptoCache.key = pki.privateKeyToPem(decrypted);
45
+ }
46
+
47
+ if (this._voltConfig.ca_pem) {
48
+ if (!this._cryptoCache.ca) {
49
+ // Copy the volt signing CA to the crypto cache.
50
+ this._cryptoCache.ca = this._voltConfig.ca_pem;
51
+ }
52
+
53
+ // Pre-cache the target Volt public key by extracting it from the signing certificate.
54
+ const voltCert = forge.pki.certificateFromPem(this._voltConfig.ca_pem);
55
+
56
+ // Extract Volt public key (used for encrypting Relay payloads).
57
+ this._voltPublicKey = forge.pki.publicKeyToPem(voltCert.publicKey);
58
+ this._voltFingerprint = voltUtils.createPublicKeyFingerprint(
59
+ this._voltPublicKey,
60
+ );
61
+ }
62
+ }
63
+
64
+ get config() {
65
+ return this._config;
66
+ }
67
+
68
+ get cache() {
69
+ return this._cryptoCache;
70
+ }
71
+
72
+ get voltFingerprint() {
73
+ return this._voltFingerprint;
74
+ }
75
+
76
+ getKey() {
77
+ const privateKey = pki.privateKeyFromPem(this._cryptoCache.key);
78
+ const publicKey = pki.rsa.setPublicKey(privateKey.n, privateKey.e);
79
+ return { privateKey, publicKey };
80
+ }
81
+
82
+ /**
83
+ * Retrieves and re-hydrates the pub/priv key data from the crypto cache, or creates one if not found.
84
+ */
85
+ createKey() {
86
+ if (this._cryptoCache.key) {
87
+ // Already have key data in the cache in pem format => create fully-formed pki instances.
88
+ log("createKey - key already exists");
89
+ return Promise.resolve(this.getKey());
90
+ } else {
91
+ log("creating key");
92
+ return new Promise((resolve, reject) => {
93
+ pki.rsa.generateKeyPair({ bits: 2048, workers: -1 }, (err, keypair) => {
94
+ if (err) {
95
+ log("failure creating key [%s]", err.message);
96
+ reject(err);
97
+ } else {
98
+ log("successfully created key");
99
+ const pem = pki.privateKeyToPem(keypair.privateKey);
100
+ this._cryptoCache.key = pem;
101
+ resolve(keypair);
102
+ }
103
+ });
104
+ });
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Create cryptocache from scratch.
110
+ */
111
+ initialise() {
112
+ return this.createKey()
113
+ .then((/* keypair */) => {
114
+ return this.saveCache();
115
+ })
116
+ .then(() => {
117
+ return this._cryptoCache;
118
+ })
119
+ .catch((err) => {
120
+ log("failure initialising crypto [%s]", err.message);
121
+ return Promise.reject(err);
122
+ });
123
+ }
124
+
125
+ get isBound() {
126
+ return !!this._cryptoCache.cert;
127
+ }
128
+
129
+ /**
130
+ * Signs the identity resource id using the private key.
131
+ * @param {*} audience the token audience (usually the target volt)
132
+ * @param {*} tunnelling flag indicating if the token is required for a tunnelled connection
133
+ * @param {*} ttl time to live in seconds (default to 1 minute)
134
+ */
135
+ getIdentityToken(audience, tunnelling = false, ttl = 60) {
136
+ if (!this._cryptoCache.key) {
137
+ throw new Error("crypto cache invalid");
138
+ }
139
+
140
+ const keyPair = this.getKey();
141
+ const publicKeyPem = pki.publicKeyToPem(keyPair.publicKey);
142
+ const base58Key = voltUtils.createPublicKeyFingerprint(publicKeyPem);
143
+
144
+ log("base58 key is %s", base58Key);
145
+
146
+ // Allow TTL either side of the current time.
147
+ // @todo - fix with server time sync on volt connection.
148
+ const payload = {
149
+ aud: audience,
150
+ iat: Math.floor(Date.now() / 1000) - ttl,
151
+ exp: Math.floor(Date.now() / 1000) + ttl,
152
+ };
153
+
154
+ let sharedKey;
155
+ if (tunnelling) {
156
+ // Initialise the Relay encryption key.
157
+ sharedKey = VoltCredential.aesCreateKey();
158
+
159
+ // Encrypt the key details using the target Volt public key and include this in the JWT payload.
160
+ payload.sk = VoltCredential.rsaEncrypt(
161
+ this._voltPublicKey,
162
+ sharedKey.key,
163
+ );
164
+ payload.iv = VoltCredential.rsaEncrypt(this._voltPublicKey, sharedKey.iv);
165
+ }
166
+
167
+ // Sign synchronously.
168
+ const token = jwt.sign(payload, this._cryptoCache.key, {
169
+ algorithm: rs256Algorithm,
170
+ keyid: base58Key,
171
+ });
172
+
173
+ // Return the token along with any encryption key details.
174
+ return {
175
+ token,
176
+ sharedKey,
177
+ };
178
+ }
179
+
180
+ /**
181
+ * Creates the metadata required for a grpc call, including the Volt authentication token
182
+ * and optionally tunnel encryption parameters.
183
+ * @param {*} grpc
184
+ * @param {*} audience
185
+ * @param {*} tunnelling
186
+ * @param {*} ttl
187
+ * @returns
188
+ */
189
+ getIdentityMetadata(grpc, audience, tunnelling = false, ttl = 60) {
190
+ const identityToken = this.getIdentityToken(
191
+ audience || this._voltConfig.id,
192
+ tunnelling,
193
+ ttl,
194
+ );
195
+ const metadata = new grpc.Metadata();
196
+ metadata.add(constants.authTokenName, identityToken.token);
197
+ return { ...identityToken, metadata };
198
+ }
199
+
200
+ saveCache() {
201
+ if (this._persistCache) {
202
+ const clone = { ...this._config, credential: { ...this._cryptoCache } };
203
+ if (this._config.p) {
204
+ // A passphrase option exists => encrypt the key before writing the cache file.
205
+ const privateKey = pki.privateKeyFromPem(this._cryptoCache.key);
206
+ clone.credential.key = pki.encryptRsaPrivateKey(
207
+ privateKey,
208
+ this._config.p,
209
+ );
210
+ }
211
+ fs.writeFileSync(this._configPath, JSON.stringify(clone, null, 2));
212
+ }
213
+ }
214
+
215
+ static aesCreateKey() {
216
+ // Generate random key for the AES-256 algorithm (32 bytes = 256 bits).
217
+ const key = crypto.randomBytes(32);
218
+ const iv = crypto.randomBytes(16);
219
+ return { key, iv };
220
+ }
221
+
222
+ static aesEncrypt(buffer, key, iv) {
223
+ const cipher = crypto.createCipheriv(aesAlgorithm, key, iv);
224
+ const crypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
225
+ return crypted;
226
+ }
227
+
228
+ static aesDecrypt(buffer, key, iv) {
229
+ const cipher = crypto.createDecipheriv(aesAlgorithm, key, iv);
230
+ const decrypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
231
+ return decrypted;
232
+ }
233
+
234
+ static rsaEncrypt(key, buffer) {
235
+ return crypto.publicEncrypt(key, buffer).toString("base64");
236
+ }
237
237
  }