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)
  • Loading branch information
robwalch committed May 24, 2024
1 parent ce84b46 commit d0832c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/crypt/decrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Decrypter {
/* no-op */
}
}
this.useSoftware = this.subtle === null;
this.useSoftware = !this.subtle;
}

destroy() {
Expand Down Expand Up @@ -155,20 +155,22 @@ export default class Decrypter {
iv: ArrayBuffer,
aesMode: DecrypterAesMode,
): Promise<ArrayBuffer> {
const subtle = this.subtle;
if (this.key !== key || !this.fastAesKey) {
if (!this.subtle) {
return Promise.resolve(this.onWebCryptoError(data, key, iv, aesMode));
}
this.key = key;
this.fastAesKey = new FastAESKey(subtle, key, aesMode);
this.fastAesKey = new FastAESKey(this.subtle, key, aesMode);
}
return this.fastAesKey
.expandKey()
.then((aesKey: CryptoKey) => {
// 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), aesMode);
const crypto = new AESCrypto(this.subtle, new Uint8Array(iv), aesMode);
return crypto.decrypt(data.buffer, aesKey);
})
.catch((err) => {
Expand All @@ -180,7 +182,12 @@ export default class Decrypter {
});
}

private onWebCryptoError(data, key, iv, aesMode): ArrayBuffer | never {
private onWebCryptoError(
data: Uint8Array,
key: ArrayBuffer,
iv: ArrayBuffer,
aesMode: DecrypterAesMode,
): ArrayBuffer | never {
const enableSoftwareAES = this.enableSoftwareAES;
if (enableSoftwareAES) {
this.useSoftware = true;
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,11 +1,11 @@
import { DecrypterAesMode } from './decrypter-aes-mode';

export default class FastAESKey {
private subtle: any;
private subtle: SubtleCrypto;
private key: ArrayBuffer;
private aesMode: DecrypterAesMode;

constructor(subtle, key, aesMode: DecrypterAesMode) {
constructor(subtle: SubtleCrypto, key, aesMode: DecrypterAesMode) {
this.subtle = subtle;
this.key = key;
this.aesMode = aesMode;
Expand Down

0 comments on commit d0832c6

Please sign in to comment.