Skip to content

Commit ab76382

Browse files
author
Weedshaker
committed
fix webworker
improve crypto error handling
1 parent e120b92 commit ab76382

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/WebWorker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const WebWorker = (ChosenHTMLElement = HTMLElement) => class WebWorker ex
4141
func = func.replace(/this\./g, '')
4242
func = func.replace(new RegExp(`${this.constructor.name}\\.`, 'g'), '')
4343
func = /^.*?=>.*?/.test(func) ? `(${func})` : !/^function/.test(func) ? `function ${func}` : func
44-
func = func.replace(/(.*)async\s(.*)/s, 'await async $1$2') // fix async function
44+
func = func.replace(/(function\s)async\s(.*)/s, 'await async $1$2') // fix async function
4545
func = func.replace(/function\s#/, 'function ') // fix private functions
4646
const response = `onmessage=async (event)=>{postMessage(${func}(...event.data))}`
4747
let blob

src/controllers/Crypto.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { WebWorker } from '../WebWorker.js'
88
/** @typedef {{ cryptoKey: CryptoKey, jsonWebKey?: JSONWEBKEY_STRING | string, epoch: string, derived?: DERIVED_KEY }} KEY */
99
/** @typedef {{ text: string, iv: Uint8Array<ArrayBuffer>, name: string, key: KEY_EPOCH }} ENCRYPTED */ // text: JSON.stringify({text: string, epoch: string})
1010
/** @typedef {{ text: string, epoch: string, encrypted: { epoch: string, key: KEY_EPOCH }, key: KEY_EPOCH }} DECRYPTED */
11+
/** @typedef {{ error: true, message: string, privateKey: KEY, publicKey: KEY }} DERIVE_ERROR */
1112
/** @typedef {{ error: true, message: string, encrypted: ENCRYPTED, key: KEY }} DECRYPTED_ERROR */
1213
/** @typedef {{ error: true, message: string, jsonWebKey: JsonWebKey }} JSON_WEB_KEY_TO_CRYPTOKEY_ERROR */
1314

@@ -313,7 +314,11 @@ export default class Crypto extends WebWorker() {
313314
if (publicKey.cryptoKey.error) return publicKey.cryptoKey
314315
}
315316
const cryptoKey = await this.deriveSyncKeyFromAsyncKeyPair(privateKey, publicKey, keyUsages)
317+
// @ts-ignore
318+
if (cryptoKey.error) return cryptoKey
319+
// @ts-ignore
316320
cryptoKey.jsonWebKey = await this.cryptoKeyToJsonWebKey(cryptoKey.cryptoKey)
321+
// @ts-ignore
317322
if (mapKey) Crypto.#derivedKeysCache.set(mapKey, cryptoKey)
318323
// @ts-ignore
319324
return cryptoKey
@@ -327,7 +332,7 @@ export default class Crypto extends WebWorker() {
327332
* @param {KEY} privateKey
328333
* @param {KEY} publicKey
329334
* @param {KeyUsage[]} [keyUsages=['encrypt', 'decrypt']]
330-
* @returns {Promise<KEY>}
335+
* @returns {Promise<KEY|DERIVE_ERROR>}
331336
*/
332337
async deriveSyncKeyFromAsyncKeyPair (privateKey, publicKey, keyUsages = ['encrypt', 'decrypt']) {
333338
return this.webWorker(Crypto.#_deriveSyncKeyFromAsyncKeyPair, privateKey, publicKey, keyUsages, Crypto.#epochDateNow)
@@ -343,27 +348,36 @@ export default class Crypto extends WebWorker() {
343348
* @param {KEY} publicKey
344349
* @param {KeyUsage[]} keyUsages
345350
* @param {string} epoch
346-
* @returns {Promise<KEY>}
351+
* @returns {Promise<KEY|DERIVE_ERROR>}
347352
*/
348353
static async #_deriveSyncKeyFromAsyncKeyPair (privateKey, publicKey, keyUsages, epoch) {
349-
return {
350-
cryptoKey: await self.crypto.subtle.deriveKey(
351-
{
352-
name: 'ECDH',
353-
public: publicKey.cryptoKey
354-
},
355-
privateKey.cryptoKey,
356-
{
357-
name: 'AES-GCM',
358-
length: 256
359-
},
360-
true,
361-
keyUsages
362-
),
363-
epoch,
364-
derived: {
365-
privateKeyEpoch: privateKey.epoch,
366-
publicKeyEpoch: publicKey.epoch
354+
try {
355+
return {
356+
cryptoKey: await self.crypto.subtle.deriveKey(
357+
{
358+
name: 'ECDH',
359+
public: publicKey.cryptoKey
360+
},
361+
privateKey.cryptoKey,
362+
{
363+
name: 'AES-GCM',
364+
length: 256
365+
},
366+
true,
367+
keyUsages
368+
),
369+
epoch,
370+
derived: {
371+
privateKeyEpoch: privateKey.epoch,
372+
publicKeyEpoch: publicKey.epoch
373+
}
374+
}
375+
} catch (error) {
376+
return {
377+
error: true,
378+
message: `Error deriving sync key from async key pair: ${error}`,
379+
privateKey,
380+
publicKey
367381
}
368382
}
369383
}
@@ -549,6 +563,11 @@ export default class Crypto extends WebWorker() {
549563
* @returns {Promise<CryptoKey|JSON_WEB_KEY_TO_CRYPTOKEY_ERROR>}
550564
*/
551565
static async #_jsonWebKeyToCryptoKey (jsonWebKey, algorithm, keyUsages, format) {
566+
if (!jsonWebKey) return {
567+
error: true,
568+
message: `Error missing JsonWebKey!`,
569+
jsonWebKey
570+
}
552571
if (typeof jsonWebKey === 'string') jsonWebKey = JSON.parse(jsonWebKey)
553572
if (!algorithm) {
554573
algorithm = {

0 commit comments

Comments
 (0)