@tdxvolt/volt-client-grpc 0.16.0-beta.10 → 0.16.0-beta.13

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.
@@ -1,16 +1,17 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
2
  import debug from "debug";
3
3
  import fs from "fs";
4
- import forge from "node-forge";
5
- import jwt from "jsonwebtoken";
6
- import crypto from "crypto";
7
- import * as voltUtils from "./utils.js";
8
4
  import { constants } from "./constants.js";
5
+ import {
6
+ createKey,
7
+ fingerprintFromPublicKey,
8
+ getIdentityToken,
9
+ keyFromPem,
10
+ privateKeyToPem,
11
+ publicKeyToPem
12
+ } from "@tdxvolt/volt-client-web";
9
13
 
10
- const { pki } = forge;
11
14
  const log = debug("volt-client-grpc:volt-credential");
12
- const aesAlgorithm = "aes-256-cbc";
13
- const rs256Algorithm = "RS256";
14
15
 
15
16
  export class VoltCredential {
16
17
  constructor(config, configPath) {
@@ -34,14 +35,13 @@ export class VoltCredential {
34
35
  }
35
36
 
36
37
  // Need to unencrypt the private key.
37
- const decrypted = pki.decryptRsaPrivateKey(
38
- this._cryptoCache.key,
39
- config.p
40
- );
38
+ const decrypted = keyFromPem(this._cryptoCache.key, config.p);
39
+
41
40
  if (!decrypted) {
42
41
  throw new Error("failed to decrypt key - check passphrase");
43
42
  }
44
- this._cryptoCache.key = pki.privateKeyToPem(decrypted);
43
+
44
+ this._cryptoCache.key = privateKeyToPem(decrypted.privateKey);
45
45
  }
46
46
 
47
47
  if (this._voltConfig.ca_pem) {
@@ -50,14 +50,11 @@ export class VoltCredential {
50
50
  this._cryptoCache.ca = this._voltConfig.ca_pem;
51
51
  }
52
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
53
  // 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
- );
54
+ this._voltPublicKey = this._voltConfig.public_key;
55
+
56
+ const voltKey = keyFromPem(this._voltPublicKey).publicKey;
57
+ this._voltFingerprint = fingerprintFromPublicKey(voltKey);
61
58
  }
62
59
  }
63
60
 
@@ -78,7 +75,7 @@ export class VoltCredential {
78
75
  }
79
76
 
80
77
  get identityId() {
81
- return this._cryptoCache.identity_id;
78
+ return this._cryptoCache.identity_did;
82
79
  }
83
80
 
84
81
  get voltPublicKey() {
@@ -86,7 +83,7 @@ export class VoltCredential {
86
83
  }
87
84
 
88
85
  get publicKey() {
89
- return pki.publicKeyToPem(this.getKey().publicKey);
86
+ return publicKeyToPem(this.getKey().publicKey);
90
87
  }
91
88
 
92
89
  get voltFingerprint() {
@@ -94,9 +91,8 @@ export class VoltCredential {
94
91
  }
95
92
 
96
93
  getKey() {
97
- const privateKey = pki.privateKeyFromPem(this._cryptoCache.key);
98
- const publicKey = pki.rsa.setPublicKey(privateKey.n, privateKey.e);
99
- return { privateKey, publicKey };
94
+ const key = keyFromPem(this._cryptoCache.key);
95
+ return key;
100
96
  }
101
97
 
102
98
  /**
@@ -109,17 +105,10 @@ export class VoltCredential {
109
105
  return Promise.resolve(this.getKey());
110
106
  } else {
111
107
  log("creating key");
112
- return voltUtils
113
- .createRSAKey()
114
- .then((keyInfo) => {
115
- log("successfully created key");
116
- this._cryptoCache.key = keyInfo.pem;
117
- return keyInfo.keypair;
118
- })
119
- .catch((err) => {
120
- log("failure creating key [%s]", err.message);
121
- return Promise.reject(err);
122
- });
108
+ this._cryptoCache.key = createKey();
109
+ const key = keyFromPem(this._cryptoCache.key);
110
+ log("successfully created key");
111
+ return Promise.resolve(key);
123
112
  }
124
113
  }
125
114
 
@@ -144,55 +133,6 @@ export class VoltCredential {
144
133
  return !!this._cryptoCache.cert;
145
134
  }
146
135
 
147
- /**
148
- * Signs the identity resource id using the private key.
149
- * @param {*} audience the token audience (usually the target volt)
150
- * @param {*} tunnelling flag indicating if the token is required for a tunnelled connection
151
- * @param {*} ttl time to live in seconds (default to 1 minute)
152
- */
153
- getIdentityToken(audience, publicKey, tunnelling = false, ttl = 60) {
154
- if (!this._cryptoCache.key) {
155
- throw new Error("crypto cache invalid");
156
- }
157
-
158
- const keyPair = this.getKey();
159
- const publicKeyPem = pki.publicKeyToPem(keyPair.publicKey);
160
- const base58Key = voltUtils.createPublicKeyFingerprint(publicKeyPem);
161
-
162
- log("base58 key is %s", base58Key);
163
-
164
- // Allow TTL either side of the current time.
165
- // @todo - fix with server time sync on volt connection.
166
- const payload = {
167
- aud: audience,
168
- iat: Math.floor(Date.now() / 1000) - ttl,
169
- exp: Math.floor(Date.now() / 1000) + ttl,
170
- sub: this._cryptoCache.session_id
171
- };
172
-
173
- let sharedKey;
174
- if (tunnelling) {
175
- // Initialise the Relay encryption key.
176
- sharedKey = VoltCredential.aesCreateKey();
177
-
178
- // Encrypt the key details using the target Volt public key and include this in the JWT payload.
179
- payload.sk = VoltCredential.rsaEncrypt(publicKey, sharedKey.key);
180
- payload.iv = VoltCredential.rsaEncrypt(publicKey, sharedKey.iv);
181
- }
182
-
183
- // Sign synchronously.
184
- const token = jwt.sign(payload, this._cryptoCache.key, {
185
- algorithm: rs256Algorithm,
186
- keyid: base58Key
187
- });
188
-
189
- // Return the token along with any encryption key details.
190
- return {
191
- token,
192
- sharedKey
193
- };
194
- }
195
-
196
136
  /**
197
137
  * Creates the metadata required for a grpc call, including the Volt authentication token
198
138
  * and optionally tunnel encryption parameters.
@@ -203,9 +143,12 @@ export class VoltCredential {
203
143
  * @returns
204
144
  */
205
145
  getIdentityMetadata(grpc, audience, publicKey, tunnelling = false, ttl = 60) {
206
- const identityToken = this.getIdentityToken(
146
+ const key = keyFromPem(this._cryptoCache.key);
147
+
148
+ const identityToken = getIdentityToken(
149
+ this.sessionId,
150
+ key,
207
151
  audience || this._voltConfig.id,
208
- publicKey,
209
152
  tunnelling,
210
153
  ttl
211
154
  );
@@ -215,47 +158,17 @@ export class VoltCredential {
215
158
  }
216
159
 
217
160
  saveCache() {
161
+ const clone = { ...this._config, credential: { ...this._cryptoCache } };
162
+ if (this._config.p) {
163
+ // A passphrase option exists => encrypt the key before writing the cache file.
164
+ const key = keyFromPem(this._cryptoCache.key);
165
+ clone.credential.key = privateKeyToPem(key.privateKey, this._config.p);
166
+ }
167
+
218
168
  if (this._persistCache) {
219
- const clone = { ...this._config, credential: { ...this._cryptoCache } };
220
- if (this._config.p) {
221
- // A passphrase option exists => encrypt the key before writing the cache file.
222
- const privateKey = pki.privateKeyFromPem(this._cryptoCache.key);
223
- clone.credential.key = pki.encryptRsaPrivateKey(
224
- privateKey,
225
- this._config.p
226
- );
227
- }
228
169
  fs.writeFileSync(this._configPath, JSON.stringify(clone, null, 2));
229
170
  }
230
- }
231
-
232
- static aesCreateKey() {
233
- // Generate random key for the AES-256 algorithm (32 bytes = 256 bits).
234
- const key = crypto.randomBytes(32);
235
- const iv = crypto.randomBytes(16);
236
- return { key, iv };
237
- }
238
-
239
- static aesEncrypt(buffer, key, iv) {
240
- const cipher = crypto.createCipheriv(aesAlgorithm, key, iv);
241
- const crypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
242
- return crypted;
243
- }
244
-
245
- static aesDecrypt(buffer, key, iv) {
246
- const cipher = crypto.createDecipheriv(aesAlgorithm, key, iv);
247
- const decrypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
248
- return decrypted;
249
- }
250
-
251
- static rsaEncrypt(key, buffer) {
252
- const cipher = crypto.publicEncrypt(key, buffer);
253
- return cipher.toString("base64url");
254
- }
255
171
 
256
- static rsaDecrypt(key, buffer) {
257
- const keyObj = crypto.createPrivateKey(key);
258
- const cipher = Buffer.from(buffer, "base64url");
259
- return Buffer.from(crypto.privateDecrypt(keyObj, cipher));
172
+ return clone;
260
173
  }
261
174
  }