Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Core
import * as core from "webcrypto-core";
import { ID_DIGEST } from "./const";
import { Crypto } from "./crypto";
import { CryptoKey as P11CryptoKey } from "./key";
import {
AesCbcProvider, AesEcbProvider, AesGcmProvider,
EcdhProvider, EcdsaProvider,
HmacProvider,
RsaOaepProvider, RsaPssProvider, RsaSsaProvider,
Sha1Provider, Sha256Provider, Sha384Provider, Sha512Provider,
} from "./mechs";
import * as utils from "./utils";
export class SubtleCrypto extends core.SubtleCrypto {
constructor(private crypto: Crypto) {
super();
//#region AES
this.providers.set(new AesCbcProvider(this.crypto));
this.providers.set(new AesEcbProvider(this.crypto));
this.providers.set(new AesGcmProvider(this.crypto));
//#endregion
// //#region RSA
this.providers.set(new RsaSsaProvider(this.crypto));
this.providers.set(new RsaPssProvider(this.crypto));
this.providers.set(new RsaOaepProvider(this.crypto));
// //#endregion
import * as core from "webcrypto-core";
import {
AesCbcProvider, AesCmacProvider, AesCtrProvider, AesEcbProvider, AesGcmProvider, AesKwProvider,
DesCbcProvider,
DesEde3CbcProvider, EcdhProvider,
EcdsaProvider, HkdfProvider,
HmacProvider,
Pbkdf2Provider,
RsaEsProvider, RsaOaepProvider, RsaPssProvider, RsaSsaProvider,
Sha1Provider, Sha256Provider, Sha384Provider, Sha512Provider,
} from "./mechs";
export class SubtleCrypto extends core.SubtleCrypto {
constructor() {
super();
//#region AES
this.providers.set(new AesCbcProvider());
this.providers.set(new AesCtrProvider());
this.providers.set(new AesGcmProvider());
this.providers.set(new AesCmacProvider());
this.providers.set(new AesKwProvider());
this.providers.set(new AesEcbProvider());
//#endregion
//#region DES
this.providers.set(new DesCbcProvider());
this.providers.set(new DesEde3CbcProvider());
//#endregion
private wrapNativeKey(key: core.NativeCryptoKey, algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: KeyUsage[]): core.NativeCryptoKey {
if (this.browserInfo.name === Browser.IE) {
const algs = ["RSASSA-PKCS1-v1_5", "RSA-PSS", "RSA-OAEP"];
const index = algs.map((o) => o.toLowerCase()).indexOf(key.algorithm.name.toLowerCase());
if (index !== -1) {
const alg = this.prepareAlgorithm(algorithm);
if (core.SubtleCrypto.isHashedAlgorithm(alg)) {
const newAlg = {
...key.algorithm,
...alg,
name: algs[index],
};
return new WrappedNativeCryptoKey(newAlg, extractable, key.type, keyUsages, key);
}
}
}
return key;
}
AesCbcProvider, AesCtrProvider, AesEcbProvider, AesGcmProvider, AesKwProvider,
DesCbcProvider, DesEde3CbcProvider,
EcCrypto, EcdhProvider,
EcdsaProvider,
HmacProvider,
Pbkdf2Provider,
RsaEsProvider, RsaOaepProvider, RsaPssProvider, RsaSsaProvider,
Sha1Provider, Sha256Provider, Sha512Provider,
} from "./mechs";
import { getOidByNamedCurve } from "./mechs/ec/helper";
import { nativeSubtle } from "./native";
import { WrappedNativeCryptoKey } from "./wrapped_native_key";
type SubtleMethods = keyof core.SubtleCrypto;
export class SubtleCrypto extends core.SubtleCrypto {
private static readonly methods: SubtleMethods[] = ["digest", "importKey", "exportKey", "sign", "verify", "generateKey", "encrypt", "decrypt", "deriveBits", "deriveKey", "wrapKey", "unwrapKey"];
/**
* Returns true if key is CryptoKey and is not liner key
* > WARN Some browsers doesn't have CryptKey class in `self`.
* @param key
*/
private static isAnotherKey(key: any): key is core.NativeCryptoKey {
if (typeof key === "object"
&& typeof key.type === "string"
&& typeof key.extractable === "boolean"
&& typeof key.algorithm === "object") {
return !(key instanceof CryptoKey);
}
return false;