tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AbstractCryptoEngine.ts (8688B)


      1 import { BitString, OctetString } from "asn1js";
      2 import * as pvutils from "pvutils";
      3 import { AlgorithmIdentifier } from "../AlgorithmIdentifier";
      4 import { EMPTY_STRING } from "../constants";
      5 import { EncryptedContentInfo } from "../EncryptedContentInfo";
      6 import { PublicKeyInfo } from "../PublicKeyInfo";
      7 import * as type from "./CryptoEngineInterface";
      8 
      9 export abstract class AbstractCryptoEngine implements type.ICryptoEngine {
     10  public name: string;
     11  public crypto: Crypto;
     12  public subtle: SubtleCrypto;
     13 
     14  /**
     15   * Constructor for CryptoEngine class
     16   * @param parameters
     17   */
     18  constructor(parameters: type.CryptoEngineParameters) {
     19    this.crypto = parameters.crypto;
     20    this.subtle = "webkitSubtle" in parameters.crypto
     21      ? (parameters.crypto as any).webkitSubtle
     22      : parameters.crypto.subtle;
     23    this.name = pvutils.getParametersValue(parameters, "name", EMPTY_STRING);
     24  }
     25 
     26  public abstract getOIDByAlgorithm(algorithm: Algorithm, safety?: boolean, target?: string): string;
     27  public abstract getAlgorithmParameters(algorithmName: string, operation: type.CryptoEngineAlgorithmOperation): type.CryptoEngineAlgorithmParams;
     28  public abstract getAlgorithmByOID<T extends Algorithm = Algorithm>(oid: string, safety?: boolean, target?: string): object | T;
     29  public abstract getAlgorithmByOID<T extends Algorithm = Algorithm>(oid: string, safety: true, target?: string): T;
     30  public abstract getAlgorithmByOID(oid: any, safety?: any, target?: any): object;
     31  public abstract getHashAlgorithm(signatureAlgorithm: AlgorithmIdentifier): string;
     32  public abstract getSignatureParameters(privateKey: CryptoKey, hashAlgorithm?: string): Promise<type.CryptoEngineSignatureParams>;
     33  public abstract signWithPrivateKey(data: BufferSource, privateKey: CryptoKey, parameters: type.CryptoEngineSignWithPrivateKeyParams): Promise<ArrayBuffer>;
     34  public abstract verifyWithPublicKey(data: BufferSource, signature: BitString | OctetString, publicKeyInfo: PublicKeyInfo, signatureAlgorithm: AlgorithmIdentifier, shaAlgorithm?: string): Promise<boolean>;
     35  public abstract getPublicKey(publicKeyInfo: PublicKeyInfo, signatureAlgorithm: AlgorithmIdentifier, parameters?: type.CryptoEnginePublicKeyParams): Promise<CryptoKey>;
     36  public abstract encryptEncryptedContentInfo(parameters: type.CryptoEngineEncryptParams): Promise<EncryptedContentInfo>;
     37  public abstract decryptEncryptedContentInfo(parameters: type.CryptoEngineDecryptParams): Promise<ArrayBuffer>;
     38  public abstract stampDataWithPassword(parameters: type.CryptoEngineStampDataWithPasswordParams): Promise<ArrayBuffer>;
     39  public abstract verifyDataStampedWithPassword(parameters: type.CryptoEngineVerifyDataStampedWithPasswordParams): Promise<boolean>;
     40  public async encrypt(algorithm: globalThis.AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
     41  public async encrypt(...args: any[]): Promise<ArrayBuffer> {
     42    return (this.subtle.encrypt as any)(...args);
     43  }
     44 
     45  public decrypt(algorithm: globalThis.AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
     46  public async decrypt(...args: any[]): Promise<ArrayBuffer> {
     47    return (this.subtle.decrypt as any)(...args);
     48  }
     49 
     50  public sign(algorithm: globalThis.AlgorithmIdentifier | RsaPssParams | EcdsaParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
     51  public sign(...args: any[]): Promise<ArrayBuffer> {
     52    return (this.subtle.sign as any)(...args);
     53  }
     54 
     55  public verify(algorithm: globalThis.AlgorithmIdentifier | RsaPssParams | EcdsaParams, key: CryptoKey, signature: BufferSource, data: BufferSource): Promise<boolean>;
     56  public async verify(...args: any[]): Promise<boolean> {
     57    return (this.subtle.verify as any)(...args);
     58  }
     59 
     60  public digest(algorithm: globalThis.AlgorithmIdentifier, data: BufferSource): Promise<ArrayBuffer>;
     61  public async digest(...args: any[]) {
     62    return (this.subtle.digest as any)(...args);
     63  }
     64 
     65  public generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise<CryptoKeyPair>;
     66  public generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair>;
     67  public generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
     68  public generateKey(algorithm: globalThis.AlgorithmIdentifier, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair | CryptoKey>;
     69  public async generateKey(...args: any[]): Promise<CryptoKey | CryptoKeyPair> {
     70    return (this.subtle.generateKey as any)(...args);
     71  }
     72 
     73  public deriveKey(algorithm: globalThis.AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey, derivedKeyType: globalThis.AlgorithmIdentifier | HkdfParams | Pbkdf2Params | AesDerivedKeyParams | HmacImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
     74  public deriveKey(algorithm: globalThis.AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey, derivedKeyType: globalThis.AlgorithmIdentifier | HkdfParams | Pbkdf2Params | AesDerivedKeyParams | HmacImportParams, extractable: boolean, keyUsages: Iterable<KeyUsage>): Promise<CryptoKey>;
     75  public async deriveKey(...args: any[]): Promise<CryptoKey> {
     76    return (this.subtle.deriveKey as any)(...args);
     77  }
     78 
     79  public deriveBits(algorithm: globalThis.AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey, length: number): Promise<ArrayBuffer>;
     80  public async deriveBits(...args: any[]): Promise<ArrayBuffer> {
     81    return (this.subtle.deriveBits as any)(...args);
     82  }
     83 
     84  public wrapKey(format: KeyFormat, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: globalThis.AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams): Promise<ArrayBuffer>;
     85  public async wrapKey(...args: any[]): Promise<ArrayBuffer> {
     86    return (this.subtle.wrapKey as any)(...args);
     87  }
     88 
     89  public unwrapKey(format: KeyFormat, wrappedKey: BufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: globalThis.AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, unwrappedKeyAlgorithm: globalThis.AlgorithmIdentifier | HmacImportParams | RsaHashedImportParams | EcKeyImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
     90  public unwrapKey(format: KeyFormat, wrappedKey: BufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: globalThis.AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, unwrappedKeyAlgorithm: globalThis.AlgorithmIdentifier | HmacImportParams | RsaHashedImportParams | EcKeyImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: Iterable<KeyUsage>): Promise<CryptoKey>;
     91  public async unwrapKey(...args: any[]): Promise<CryptoKey> {
     92    return (this.subtle.unwrapKey as any)(...args);
     93  }
     94 
     95  exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
     96  exportKey(format: "pkcs8" | "raw" | "spki", key: CryptoKey): Promise<ArrayBuffer>;
     97  exportKey(...args: any[]): Promise<ArrayBuffer> | Promise<JsonWebKey> {
     98    return (this.subtle.exportKey as any)(...args);
     99  }
    100  importKey(format: "jwk", keyData: JsonWebKey, algorithm: globalThis.AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
    101  importKey(format: "pkcs8" | "raw" | "spki", keyData: BufferSource, algorithm: globalThis.AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
    102  importKey(format: "jwk", keyData: JsonWebKey, algorithm: globalThis.AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
    103  importKey(format: "pkcs8" | "raw" | "spki", keyData: BufferSource, algorithm: globalThis.AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: Iterable<KeyUsage>): Promise<CryptoKey>;
    104  importKey(...args: any[]): Promise<CryptoKey> {
    105    return (this.subtle.importKey as any)(...args);
    106  }
    107 
    108  public getRandomValues<T extends ArrayBufferView | null>(array: T): T {
    109    if (array === null) {
    110      throw new Error("Argument \"array\" must not be null");
    111    }
    112 
    113    return this.crypto.getRandomValues(array) as T;
    114  }
    115 
    116 }