{"version":3,"file":"BrowserCrypto.js","sources":["../../src/crypto/BrowserCrypto.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { BrowserStringUtils } from \"../utils/BrowserStringUtils\";\nimport { BrowserAuthError } from \"../error/BrowserAuthError\";\nimport { ISubtleCrypto } from \"./ISubtleCrypto\";\nimport { ModernBrowserCrypto } from \"./ModernBrowserCrypto\";\nimport { MsrBrowserCrypto } from \"./MsrBrowserCrypto\";\nimport { MsBrowserCrypto } from \"./MsBrowserCrypto\";\nimport { Logger } from \"@azure/msal-common\";\nimport { BrowserConfigurationAuthError } from \"../error/BrowserConfigurationAuthError\";\nimport { CryptoOptions } from \"../config/Configuration\";\n/**\n * See here for more info on RsaHashedKeyGenParams: https://developer.mozilla.org/en-US/docs/Web/API/RsaHashedKeyGenParams\n */\n// RSA KeyGen Algorithm\nconst PKCS1_V15_KEYGEN_ALG = \"RSASSA-PKCS1-v1_5\";\n// SHA-256 hashing algorithm\nconst S256_HASH_ALG = \"SHA-256\";\n// MOD length for PoP tokens\nconst MODULUS_LENGTH = 2048;\n// Public Exponent\nconst PUBLIC_EXPONENT: Uint8Array = new Uint8Array([0x01, 0x00, 0x01]);\n\n/**\n * This class implements functions used by the browser library to perform cryptography operations such as\n * hashing and encoding. It also has helper functions to validate the availability of specific APIs.\n */\nexport class BrowserCrypto {\n\n private keygenAlgorithmOptions: RsaHashedKeyGenParams;\n private subtleCrypto: ISubtleCrypto;\n private logger: Logger;\n private cryptoOptions?: CryptoOptions;\n\n constructor(logger: Logger, cryptoOptions?: CryptoOptions) {\n this.logger = logger;\n this.cryptoOptions = cryptoOptions;\n\n if (this.hasBrowserCrypto()) {\n // Use standard modern web crypto if available\n this.logger.verbose(\"BrowserCrypto: modern crypto interface available\");\n this.subtleCrypto = new ModernBrowserCrypto();\n } else if (this.hasIECrypto()) {\n // For IE11, use msCrypto interface\n this.logger.verbose(\"BrowserCrypto: MS crypto interface available\");\n this.subtleCrypto = new MsBrowserCrypto();\n } else if (this.hasMsrCrypto() && this.cryptoOptions?.useMsrCrypto) {\n // For other browsers, use MSR Crypto if found\n this.logger.verbose(\"BrowserCrypto: MSR crypto interface available\");\n this.subtleCrypto = new MsrBrowserCrypto();\n } else {\n if (this.hasMsrCrypto()) {\n this.logger.info(\"BrowserCrypto: MSR Crypto interface available but system.cryptoOptions.useMsrCrypto not enabled\");\n }\n this.logger.error(\"BrowserCrypto: No crypto interfaces available.\");\n throw BrowserAuthError.createCryptoNotAvailableError(\"Browser crypto, msCrypto, or msrCrypto interfaces not available.\");\n }\n\n // Mainly needed for MSR Crypto: https://github.com/microsoft/MSR-JavaScript-Crypto#random-number-generator-prng\n if (this.subtleCrypto.initPrng) {\n this.logger.verbose(\"BrowserCrypto: Interface requires entropy\");\n\n if (!this.cryptoOptions?.entropy) {\n this.logger.error(\"BrowserCrypto: Interface requires entropy but none provided.\");\n throw BrowserConfigurationAuthError.createEntropyNotProvided();\n }\n\n this.logger.verbose(\"BrowserCrypto: Entropy provided\");\n this.subtleCrypto.initPrng(this.cryptoOptions.entropy);\n }\n\n this.keygenAlgorithmOptions = {\n name: PKCS1_V15_KEYGEN_ALG,\n hash: S256_HASH_ALG,\n modulusLength: MODULUS_LENGTH,\n publicExponent: PUBLIC_EXPONENT\n };\n }\n\n /**\n * Check whether IE crypto or other browser cryptography is available.\n */\n private hasIECrypto(): boolean {\n return \"msCrypto\" in window;\n }\n\n /**\n * Check whether browser crypto is available.\n */\n private hasBrowserCrypto(): boolean {\n return \"crypto\" in window;\n }\n\n /**\n * Check whether MSR crypto polyfill is available\n */\n private hasMsrCrypto(): boolean {\n return \"msrCrypto\" in window;\n }\n\n /**\n * Returns a sha-256 hash of the given dataString as an ArrayBuffer.\n * @param dataString \n */\n async sha256Digest(dataString: string): Promise {\n const data = BrowserStringUtils.stringToUtf8Arr(dataString);\n // MSR Crypto wants object with name property, instead of string\n return this.subtleCrypto.digest({ name: S256_HASH_ALG }, data);\n }\n\n /**\n * Populates buffer with cryptographically random values.\n * @param dataBuffer \n */\n getRandomValues(dataBuffer: Uint8Array): Uint8Array {\n return this.subtleCrypto.getRandomValues(dataBuffer);\n }\n\n /**\n * Generates a keypair based on current keygen algorithm config.\n * @param extractable \n * @param usages \n */\n async generateKeyPair(extractable: boolean, usages: Array): Promise {\n return this.subtleCrypto.generateKey(this.keygenAlgorithmOptions, extractable, usages);\n }\n\n /**\n * Export key as Json Web Key (JWK)\n * @param key \n * @param format \n */\n async exportJwk(key: CryptoKey): Promise {\n return this.subtleCrypto.exportKey(key);\n }\n\n /**\n * Imports key as Json Web Key (JWK), can set extractable and usages.\n * @param key \n * @param format \n * @param extractable \n * @param usages \n */\n async importJwk(key: JsonWebKey, extractable: boolean, usages: Array): Promise {\n return this.subtleCrypto.importKey(key, this.keygenAlgorithmOptions, extractable, usages);\n }\n\n /**\n * Signs given data with given key\n * @param key \n * @param data \n */\n async sign(key: CryptoKey, data: ArrayBuffer): Promise {\n return this.subtleCrypto.sign(this.keygenAlgorithmOptions, key, data);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;AAAA;;;;AAcA;;;AAGA;AACA,IAAM,oBAAoB,GAAG,mBAAmB,CAAC;AACjD;AACA,IAAM,aAAa,GAAG,SAAS,CAAC;AAChC;AACA,IAAM,cAAc,GAAG,IAAI,CAAC;AAC5B;AACA,IAAM,eAAe,GAAe,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAEvE;;;;;IAWI,uBAAY,MAAc,EAAE,aAA6B;;QACrD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QAEnC,IAAI,IAAI,CAAC,gBAAgB,EAAE,EAAE;;YAEzB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,kDAAkD,CAAC,CAAC;YACxE,IAAI,CAAC,YAAY,GAAG,IAAI,mBAAmB,EAA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