Skip to content

Commit

Permalink
Fix JS AES fallback when browserCrypto.subtle returns undefined (rath…
Browse files Browse the repository at this point in the history
…er than null) (#6446)

(cherry picked from commit d0832c6)
  • Loading branch information
robwalch committed May 24, 2024
1 parent fa2d6bc commit 3e6987b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/crypt/decrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export default class Decrypter {
/* no-op */
}
}
if (this.subtle === null) {
this.useSoftware = true;
}

this.useSoftware = !this.subtle;
}

destroy() {
Expand Down Expand Up @@ -148,20 +147,22 @@ export default class Decrypter {
key: ArrayBuffer,
iv: ArrayBuffer,
): Promise<ArrayBuffer> {
const subtle = this.subtle;
if (this.key !== key || !this.fastAesKey) {
if (!this.subtle) {
return Promise.resolve(this.onWebCryptoError(data, key, iv));
}
this.key = key;
this.fastAesKey = new FastAESKey(subtle, key);
this.fastAesKey = new FastAESKey(this.subtle, key);
}
return this.fastAesKey
.expandKey()
.then((aesKey) => {
// decrypt using web crypto
if (!subtle) {
if (!this.subtle) {
return Promise.reject(new Error('web crypto not initialized'));
}
this.logOnce('WebCrypto AES decrypt');
const crypto = new AESCrypto(subtle, new Uint8Array(iv));
const crypto = new AESCrypto(this.subtle, new Uint8Array(iv));
return crypto.decrypt(data.buffer, aesKey);
})
.catch((err) => {
Expand All @@ -173,7 +174,11 @@ export default class Decrypter {
});
}

private onWebCryptoError(data, key, iv): ArrayBuffer | never {
private onWebCryptoError(
data: Uint8Array,
key: ArrayBuffer,
iv: ArrayBuffer,
): ArrayBuffer | never {
this.useSoftware = true;
this.logEnabled = true;
this.softwareDecrypt(data, key, iv);
Expand Down
4 changes: 2 additions & 2 deletions src/crypt/fast-aes-key.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default class FastAESKey {
private subtle: any;
private subtle: SubtleCrypto;
private key: ArrayBuffer;

constructor(subtle, key) {
constructor(subtle: SubtleCrypto, key: ArrayBuffer) {
this.subtle = subtle;
this.key = key;
}
Expand Down

0 comments on commit 3e6987b

Please sign in to comment.