2018-11-08 04:54:56 +01:00
|
|
|
import * as bigInt from 'big-integer';
|
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { EncryptionType } from '../enums/encryptionType';
|
2021-06-09 23:24:31 +02:00
|
|
|
import { HashPurpose } from '../enums/hashPurpose';
|
2018-08-14 21:12:10 +02:00
|
|
|
import { KdfType } from '../enums/kdfType';
|
2018-01-08 20:11:28 +01:00
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
import { EncArrayBuffer } from '../models/domain/encArrayBuffer';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { EncryptedObject } from '../models/domain/encryptedObject';
|
2021-04-21 02:16:19 +02:00
|
|
|
import { EncString } from '../models/domain/encString';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
|
|
|
|
import { ProfileOrganizationResponse } from '../models/response/profileOrganizationResponse';
|
2018-01-08 20:11:28 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CryptoService as CryptoServiceAbstraction } from '../abstractions/crypto.service';
|
2018-04-22 05:14:04 +02:00
|
|
|
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
|
2020-12-11 17:44:57 +01:00
|
|
|
import { LogService } from '../abstractions/log.service';
|
2020-11-18 22:10:57 +01:00
|
|
|
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
2021-06-09 22:53:54 +02:00
|
|
|
import {
|
|
|
|
KeySuffixOptions,
|
|
|
|
StorageService,
|
|
|
|
} from '../abstractions/storage.service';
|
2018-01-08 20:11:28 +01:00
|
|
|
|
|
|
|
import { ConstantsService } from './constants.service';
|
2018-04-22 05:14:04 +02:00
|
|
|
|
2018-07-23 20:23:30 +02:00
|
|
|
import { sequentialize } from '../misc/sequentialize';
|
2018-04-22 05:14:04 +02:00
|
|
|
import { Utils } from '../misc/utils';
|
2018-11-08 04:54:56 +01:00
|
|
|
import { EEFLongWordList } from '../misc/wordlist';
|
2021-07-15 15:07:38 +02:00
|
|
|
import { ProfileProviderOrganizationResponse } from '../models/response/profileProviderOrganizationResponse';
|
|
|
|
import { ProfileProviderResponse } from '../models/response/profileProviderResponse';
|
2018-01-08 20:11:28 +01:00
|
|
|
|
2021-06-15 16:55:57 +02:00
|
|
|
export const Keys = {
|
2020-03-27 15:03:27 +01:00
|
|
|
key: 'key', // Master Key
|
2018-01-08 20:11:28 +01:00
|
|
|
encOrgKeys: 'encOrgKeys',
|
2021-07-15 15:07:38 +02:00
|
|
|
encProviderKeys: 'encProviderKeys',
|
2018-01-08 20:11:28 +01:00
|
|
|
encPrivateKey: 'encPrivateKey',
|
2020-03-27 15:03:27 +01:00
|
|
|
encKey: 'encKey', // Generated Symmetric Key
|
2018-01-08 20:11:28 +01:00
|
|
|
keyHash: 'keyHash',
|
|
|
|
};
|
|
|
|
|
2018-01-25 20:26:09 +01:00
|
|
|
export class CryptoService implements CryptoServiceAbstraction {
|
2018-01-08 20:11:28 +01:00
|
|
|
private key: SymmetricCryptoKey;
|
|
|
|
private encKey: SymmetricCryptoKey;
|
|
|
|
private legacyEtmKey: SymmetricCryptoKey;
|
|
|
|
private keyHash: string;
|
2018-07-02 23:09:45 +02:00
|
|
|
private publicKey: ArrayBuffer;
|
2018-01-08 20:11:28 +01:00
|
|
|
private privateKey: ArrayBuffer;
|
|
|
|
private orgKeys: Map<string, SymmetricCryptoKey>;
|
2021-07-15 15:07:38 +02:00
|
|
|
private providerKeys: Map<string, SymmetricCryptoKey>;
|
2018-01-08 20:11:28 +01:00
|
|
|
|
2021-06-15 16:55:57 +02:00
|
|
|
constructor(private storageService: StorageService, protected secureStorageService: StorageService,
|
|
|
|
private cryptoFunctionService: CryptoFunctionService, protected platformUtilService: PlatformUtilsService,
|
|
|
|
protected logService: LogService) {
|
2020-12-11 17:44:57 +01:00
|
|
|
}
|
2018-01-08 20:11:28 +01:00
|
|
|
|
|
|
|
async setKey(key: SymmetricCryptoKey): Promise<any> {
|
|
|
|
this.key = key;
|
|
|
|
|
2021-06-15 16:55:57 +02:00
|
|
|
await this.storeKey(key);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setKeyHash(keyHash: string): Promise<{}> {
|
|
|
|
this.keyHash = keyHash;
|
|
|
|
return this.storageService.save(Keys.keyHash, keyHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setEncKey(encKey: string): Promise<{}> {
|
|
|
|
if (encKey == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-22 05:14:04 +02:00
|
|
|
|
2018-01-08 20:11:28 +01:00
|
|
|
await this.storageService.save(Keys.encKey, encKey);
|
|
|
|
this.encKey = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setEncPrivateKey(encPrivateKey: string): Promise<{}> {
|
|
|
|
if (encPrivateKey == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.encPrivateKey, encPrivateKey);
|
|
|
|
this.privateKey = null;
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:07:38 +02:00
|
|
|
async setOrgKeys(orgs: ProfileOrganizationResponse[], providerOrgs: ProfileProviderOrganizationResponse[]): Promise<{}> {
|
2018-01-08 20:11:28 +01:00
|
|
|
const orgKeys: any = {};
|
2021-02-04 16:49:23 +01:00
|
|
|
orgs.forEach(org => {
|
2018-01-08 20:11:28 +01:00
|
|
|
orgKeys[org.id] = org.key;
|
|
|
|
});
|
|
|
|
|
2021-07-15 15:07:38 +02:00
|
|
|
for (const providerOrg of providerOrgs) {
|
|
|
|
// Convert provider encrypted keys to user encrypted.
|
|
|
|
const providerKey = await this.getProviderKey(providerOrg.providerId);
|
|
|
|
const decValue = await this.decryptToBytes(new EncString(providerOrg.key), providerKey);
|
|
|
|
orgKeys[providerOrg.id] = await (await this.rsaEncrypt(decValue)).encryptedString;
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:41:53 +02:00
|
|
|
this.orgKeys = null;
|
2018-01-08 20:11:28 +01:00
|
|
|
return this.storageService.save(Keys.encOrgKeys, orgKeys);
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:07:38 +02:00
|
|
|
setProviderKeys(providers: ProfileProviderResponse[]): Promise<{}> {
|
|
|
|
const providerKeys: any = {};
|
|
|
|
providers.forEach(provider => {
|
|
|
|
providerKeys[provider.id] = provider.key;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.providerKeys = null;
|
|
|
|
return this.storageService.save(Keys.encProviderKeys, providerKeys);
|
|
|
|
}
|
|
|
|
|
2021-06-09 22:53:54 +02:00
|
|
|
async getKey(keySuffix?: KeySuffixOptions): Promise<SymmetricCryptoKey> {
|
2018-01-08 20:11:28 +01:00
|
|
|
if (this.key != null) {
|
|
|
|
return this.key;
|
|
|
|
}
|
2021-06-14 21:03:13 +02:00
|
|
|
|
2021-06-09 22:53:54 +02:00
|
|
|
keySuffix ||= 'auto';
|
2021-06-14 21:03:13 +02:00
|
|
|
const symmetricKey = await this.getKeyFromStorage(keySuffix);
|
|
|
|
|
|
|
|
if (symmetricKey != null) {
|
|
|
|
this.setKey(symmetricKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return symmetricKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getKeyFromStorage(keySuffix: KeySuffixOptions): Promise<SymmetricCryptoKey> {
|
2021-06-09 22:53:54 +02:00
|
|
|
const key = await this.retrieveKeyFromStorage(keySuffix);
|
2018-04-22 05:14:04 +02:00
|
|
|
if (key != null) {
|
2021-04-28 22:02:43 +02:00
|
|
|
|
|
|
|
const symmetricKey = new SymmetricCryptoKey(Utils.fromB64ToArray(key).buffer);
|
|
|
|
|
|
|
|
if (!await this.validateKey(symmetricKey)) {
|
|
|
|
this.logService.warning('Wrong key, throwing away stored key');
|
2021-06-09 22:53:54 +02:00
|
|
|
this.secureStorageService.remove(Keys.key, { keySuffix: keySuffix });
|
2021-04-28 22:02:43 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:03:13 +02:00
|
|
|
return symmetricKey;
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
2021-06-14 21:03:13 +02:00
|
|
|
return null;
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:18:55 +02:00
|
|
|
async getKeyHash(): Promise<string> {
|
2018-01-08 20:11:28 +01:00
|
|
|
if (this.keyHash != null) {
|
2019-04-03 20:18:55 +02:00
|
|
|
return this.keyHash;
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 03:26:16 +02:00
|
|
|
const keyHash = await this.storageService.get<string>(Keys.keyHash);
|
2019-04-03 20:18:55 +02:00
|
|
|
if (keyHash != null) {
|
|
|
|
this.keyHash = keyHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyHash == null ? null : this.keyHash;
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 23:35:58 +02:00
|
|
|
async compareAndUpdateKeyHash(masterPassword: string, key: SymmetricCryptoKey): Promise<boolean> {
|
|
|
|
const storedKeyHash = await this.getKeyHash();
|
|
|
|
if (masterPassword != null && storedKeyHash != null) {
|
|
|
|
const localKeyHash = await this.hashPassword(masterPassword, key, HashPurpose.LocalAuthorization);
|
|
|
|
if (localKeyHash != null && storedKeyHash === localKeyHash) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove serverKeyHash check in 1-2 releases after everyone's keyHash has been updated
|
|
|
|
const serverKeyHash = await this.hashPassword(masterPassword, key, HashPurpose.ServerAuthorization);
|
|
|
|
if (serverKeyHash != null && storedKeyHash === serverKeyHash) {
|
|
|
|
await this.setKeyHash(localKeyHash);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-23 21:12:32 +02:00
|
|
|
@sequentialize(() => 'getEncKey')
|
2019-08-29 15:40:50 +02:00
|
|
|
async getEncKey(key: SymmetricCryptoKey = null): Promise<SymmetricCryptoKey> {
|
2018-01-08 20:11:28 +01:00
|
|
|
if (this.encKey != null) {
|
|
|
|
return this.encKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encKey = await this.storageService.get<string>(Keys.encKey);
|
|
|
|
if (encKey == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-29 15:40:50 +02:00
|
|
|
if (key == null) {
|
|
|
|
key = await this.getKey();
|
|
|
|
}
|
2018-01-08 20:11:28 +01:00
|
|
|
if (key == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:48:00 +02:00
|
|
|
let decEncKey: ArrayBuffer;
|
2021-04-21 02:16:19 +02:00
|
|
|
const encKeyCipher = new EncString(encKey);
|
2018-06-13 22:48:00 +02:00
|
|
|
if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_B64) {
|
2018-09-10 18:13:30 +02:00
|
|
|
decEncKey = await this.decryptToBytes(encKeyCipher, key);
|
2018-06-13 22:48:00 +02:00
|
|
|
} else if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_HmacSha256_B64) {
|
|
|
|
const newKey = await this.stretchKey(key);
|
2018-09-10 18:13:30 +02:00
|
|
|
decEncKey = await this.decryptToBytes(encKeyCipher, newKey);
|
2018-06-13 22:48:00 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported encKey type.');
|
|
|
|
}
|
|
|
|
|
2018-01-08 20:11:28 +01:00
|
|
|
if (decEncKey == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this.encKey = new SymmetricCryptoKey(decEncKey);
|
|
|
|
return this.encKey;
|
|
|
|
}
|
|
|
|
|
2018-07-02 23:09:45 +02:00
|
|
|
async getPublicKey(): Promise<ArrayBuffer> {
|
|
|
|
if (this.publicKey != null) {
|
|
|
|
return this.publicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
const privateKey = await this.getPrivateKey();
|
|
|
|
if (privateKey == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-03 05:57:22 +02:00
|
|
|
this.publicKey = await this.cryptoFunctionService.rsaExtractPublicKey(privateKey);
|
2018-07-02 23:09:45 +02:00
|
|
|
return this.publicKey;
|
|
|
|
}
|
|
|
|
|
2018-01-08 20:11:28 +01:00
|
|
|
async getPrivateKey(): Promise<ArrayBuffer> {
|
|
|
|
if (this.privateKey != null) {
|
|
|
|
return this.privateKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encPrivateKey = await this.storageService.get<string>(Keys.encPrivateKey);
|
|
|
|
if (encPrivateKey == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
this.privateKey = await this.decryptToBytes(new EncString(encPrivateKey), null);
|
2018-01-08 20:11:28 +01:00
|
|
|
return this.privateKey;
|
|
|
|
}
|
|
|
|
|
2018-11-08 05:12:45 +01:00
|
|
|
async getFingerprint(userId: string, publicKey?: ArrayBuffer): Promise<string[]> {
|
|
|
|
if (publicKey == null) {
|
|
|
|
publicKey = await this.getPublicKey();
|
|
|
|
}
|
2018-11-08 04:54:56 +01:00
|
|
|
if (publicKey === null) {
|
|
|
|
throw new Error('No public key available.');
|
|
|
|
}
|
|
|
|
const keyFingerprint = await this.cryptoFunctionService.hash(publicKey, 'sha256');
|
2020-10-29 20:52:12 +01:00
|
|
|
const userFingerprint = await this.cryptoFunctionService.hkdfExpand(keyFingerprint, userId, 32, 'sha256');
|
|
|
|
return this.hashPhrase(userFingerprint);
|
2018-11-08 04:54:56 +01:00
|
|
|
}
|
|
|
|
|
2018-07-23 21:12:32 +02:00
|
|
|
@sequentialize(() => 'getOrgKeys')
|
2018-01-08 20:11:28 +01:00
|
|
|
async getOrgKeys(): Promise<Map<string, SymmetricCryptoKey>> {
|
|
|
|
if (this.orgKeys != null && this.orgKeys.size > 0) {
|
|
|
|
return this.orgKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encOrgKeys = await this.storageService.get<any>(Keys.encOrgKeys);
|
2018-04-22 05:14:04 +02:00
|
|
|
if (encOrgKeys == null) {
|
2018-01-08 20:11:28 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const orgKeys: Map<string, SymmetricCryptoKey> = new Map<string, SymmetricCryptoKey>();
|
|
|
|
let setKey = false;
|
|
|
|
|
|
|
|
for (const orgId in encOrgKeys) {
|
|
|
|
if (!encOrgKeys.hasOwnProperty(orgId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-22 05:14:04 +02:00
|
|
|
const decValue = await this.rsaDecrypt(encOrgKeys[orgId]);
|
|
|
|
orgKeys.set(orgId, new SymmetricCryptoKey(decValue));
|
2018-01-08 20:11:28 +01:00
|
|
|
setKey = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setKey) {
|
|
|
|
this.orgKeys = orgKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.orgKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getOrgKey(orgId: string): Promise<SymmetricCryptoKey> {
|
|
|
|
if (orgId == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const orgKeys = await this.getOrgKeys();
|
|
|
|
if (orgKeys == null || !orgKeys.has(orgId)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return orgKeys.get(orgId);
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:07:38 +02:00
|
|
|
@sequentialize(() => 'getProviderKeys')
|
|
|
|
async getProviderKeys(): Promise<Map<string, SymmetricCryptoKey>> {
|
|
|
|
if (this.providerKeys != null && this.providerKeys.size > 0) {
|
|
|
|
return this.providerKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encProviderKeys = await this.storageService.get<any>(Keys.encProviderKeys);
|
|
|
|
if (encProviderKeys == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const providerKeys: Map<string, SymmetricCryptoKey> = new Map<string, SymmetricCryptoKey>();
|
|
|
|
let setKey = false;
|
|
|
|
|
|
|
|
for (const orgId in encProviderKeys) {
|
|
|
|
if (!encProviderKeys.hasOwnProperty(orgId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const decValue = await this.rsaDecrypt(encProviderKeys[orgId]);
|
|
|
|
providerKeys.set(orgId, new SymmetricCryptoKey(decValue));
|
|
|
|
setKey = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setKey) {
|
|
|
|
this.providerKeys = providerKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.providerKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProviderKey(providerId: string): Promise<SymmetricCryptoKey> {
|
|
|
|
if (providerId == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const providerKeys = await this.getProviderKeys();
|
|
|
|
if (providerKeys == null || !providerKeys.has(providerId)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return providerKeys.get(providerId);
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:10:52 +02:00
|
|
|
async hasKey(): Promise<boolean> {
|
2021-06-09 22:53:54 +02:00
|
|
|
return this.hasKeyInMemory() || await this.hasKeyStored('auto') || await this.hasKeyStored('biometric');
|
|
|
|
}
|
|
|
|
|
|
|
|
hasKeyInMemory(): boolean {
|
|
|
|
return this.key != null;
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:55:57 +02:00
|
|
|
hasKeyStored(keySuffix: KeySuffixOptions): Promise<boolean> {
|
|
|
|
return this.secureStorageService.has(Keys.key, { keySuffix: keySuffix });
|
2018-06-13 23:10:52 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:07:06 +02:00
|
|
|
async hasEncKey(): Promise<boolean> {
|
|
|
|
const encKey = await this.storageService.get<string>(Keys.encKey);
|
|
|
|
return encKey != null;
|
|
|
|
}
|
|
|
|
|
2021-06-09 22:53:54 +02:00
|
|
|
async clearKey(clearSecretStorage: boolean = true): Promise<any> {
|
2018-01-08 20:11:28 +01:00
|
|
|
this.key = this.legacyEtmKey = null;
|
2021-06-09 22:53:54 +02:00
|
|
|
if (clearSecretStorage) {
|
|
|
|
this.clearStoredKey('auto');
|
|
|
|
this.clearStoredKey('biometric');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearStoredKey(keySuffix: KeySuffixOptions) {
|
|
|
|
await this.secureStorageService.remove(Keys.key, { keySuffix: keySuffix });
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clearKeyHash(): Promise<any> {
|
|
|
|
this.keyHash = null;
|
|
|
|
return this.storageService.remove(Keys.keyHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearEncKey(memoryOnly?: boolean): Promise<any> {
|
|
|
|
this.encKey = null;
|
|
|
|
if (memoryOnly) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return this.storageService.remove(Keys.encKey);
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:41:55 +02:00
|
|
|
clearKeyPair(memoryOnly?: boolean): Promise<any> {
|
2018-01-08 20:11:28 +01:00
|
|
|
this.privateKey = null;
|
2018-07-03 17:41:55 +02:00
|
|
|
this.publicKey = null;
|
2018-01-08 20:11:28 +01:00
|
|
|
if (memoryOnly) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return this.storageService.remove(Keys.encPrivateKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearOrgKeys(memoryOnly?: boolean): Promise<any> {
|
|
|
|
this.orgKeys = null;
|
|
|
|
if (memoryOnly) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return this.storageService.remove(Keys.encOrgKeys);
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:07:38 +02:00
|
|
|
clearProviderKeys(memoryOnly?: boolean): Promise<any> {
|
|
|
|
this.providerKeys = null;
|
|
|
|
if (memoryOnly) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return this.storageService.remove(Keys.encOrgKeys);
|
|
|
|
}
|
|
|
|
|
2019-02-13 05:52:50 +01:00
|
|
|
clearPinProtectedKey(): Promise<any> {
|
|
|
|
return this.storageService.remove(ConstantsService.pinProtectedKey);
|
|
|
|
}
|
|
|
|
|
2021-05-19 21:12:08 +02:00
|
|
|
async clearKeys(): Promise<any> {
|
2021-06-22 14:10:47 +02:00
|
|
|
await this.clearKey();
|
2021-05-19 21:12:08 +02:00
|
|
|
await this.clearKeyHash();
|
|
|
|
await this.clearOrgKeys();
|
2021-07-15 15:07:38 +02:00
|
|
|
await this.clearProviderKeys();
|
2021-05-19 21:12:08 +02:00
|
|
|
await this.clearEncKey();
|
|
|
|
await this.clearKeyPair();
|
|
|
|
await this.clearPinProtectedKey();
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async toggleKey(): Promise<any> {
|
|
|
|
const key = await this.getKey();
|
|
|
|
|
|
|
|
await this.setKey(key);
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:12:10 +02:00
|
|
|
async makeKey(password: string, salt: string, kdf: KdfType, kdfIterations: number):
|
|
|
|
Promise<SymmetricCryptoKey> {
|
|
|
|
let key: ArrayBuffer = null;
|
2018-08-28 01:58:49 +02:00
|
|
|
if (kdf == null || kdf === KdfType.PBKDF2_SHA256) {
|
2018-08-14 21:12:10 +02:00
|
|
|
if (kdfIterations == null) {
|
|
|
|
kdfIterations = 5000;
|
|
|
|
} else if (kdfIterations < 5000) {
|
|
|
|
throw new Error('PBKDF2 iteration minimum is 5000.');
|
|
|
|
}
|
|
|
|
key = await this.cryptoFunctionService.pbkdf2(password, salt, 'sha256', kdfIterations);
|
|
|
|
} else {
|
|
|
|
throw new Error('Unknown Kdf.');
|
|
|
|
}
|
2018-04-22 05:14:04 +02:00
|
|
|
return new SymmetricCryptoKey(key);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2019-08-29 15:40:50 +02:00
|
|
|
async makeKeyFromPin(pin: string, salt: string, kdf: KdfType, kdfIterations: number,
|
2021-04-21 02:16:19 +02:00
|
|
|
protectedKeyCs: EncString = null):
|
2019-03-05 22:36:11 +01:00
|
|
|
Promise<SymmetricCryptoKey> {
|
2019-08-29 15:40:50 +02:00
|
|
|
if (protectedKeyCs == null) {
|
|
|
|
const pinProtectedKey = await this.storageService.get<string>(ConstantsService.pinProtectedKey);
|
|
|
|
if (pinProtectedKey == null) {
|
|
|
|
throw new Error('No PIN protected key found.');
|
|
|
|
}
|
2021-04-21 02:16:19 +02:00
|
|
|
protectedKeyCs = new EncString(pinProtectedKey);
|
2019-03-05 22:36:11 +01:00
|
|
|
}
|
|
|
|
const pinKey = await this.makePinKey(pin, salt, kdf, kdfIterations);
|
|
|
|
const decKey = await this.decryptToBytes(protectedKeyCs, pinKey);
|
|
|
|
return new SymmetricCryptoKey(decKey);
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async makeShareKey(): Promise<[EncString, SymmetricCryptoKey]> {
|
2018-07-02 23:09:45 +02:00
|
|
|
const shareKey = await this.cryptoFunctionService.randomBytes(64);
|
|
|
|
const publicKey = await this.getPublicKey();
|
2018-11-26 21:29:54 +01:00
|
|
|
const encShareKey = await this.rsaEncrypt(shareKey, publicKey);
|
2018-07-02 23:09:45 +02:00
|
|
|
return [encShareKey, new SymmetricCryptoKey(shareKey)];
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async makeKeyPair(key?: SymmetricCryptoKey): Promise<[string, EncString]> {
|
2018-07-03 17:41:55 +02:00
|
|
|
const keyPair = await this.cryptoFunctionService.rsaGenerateKeyPair(2048);
|
|
|
|
const publicB64 = Utils.fromBufferToB64(keyPair[0]);
|
|
|
|
const privateEnc = await this.encrypt(keyPair[1], key);
|
|
|
|
return [publicB64, privateEnc];
|
|
|
|
}
|
|
|
|
|
2019-02-13 06:04:31 +01:00
|
|
|
async makePinKey(pin: string, salt: string, kdf: KdfType, kdfIterations: number): Promise<SymmetricCryptoKey> {
|
|
|
|
const pinKey = await this.makeKey(pin, salt, kdf, kdfIterations);
|
2019-02-13 05:52:50 +01:00
|
|
|
return await this.stretchKey(pinKey);
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:58:18 +01:00
|
|
|
async makeSendKey(keyMaterial: ArrayBuffer): Promise<SymmetricCryptoKey> {
|
|
|
|
const sendKey = await this.cryptoFunctionService.hkdf(keyMaterial, 'bitwarden-send', 'send', 64, 'sha256');
|
|
|
|
return new SymmetricCryptoKey(sendKey);
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:24:31 +02:00
|
|
|
async hashPassword(password: string, key: SymmetricCryptoKey, hashPurpose?: HashPurpose): Promise<string> {
|
2018-05-16 03:11:20 +02:00
|
|
|
if (key == null) {
|
|
|
|
key = await this.getKey();
|
|
|
|
}
|
2018-04-22 05:14:04 +02:00
|
|
|
if (password == null || key == null) {
|
2018-01-08 20:11:28 +01:00
|
|
|
throw new Error('Invalid parameters.');
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:24:31 +02:00
|
|
|
const iterations = hashPurpose === HashPurpose.LocalAuthorization ? 2 : 1;
|
|
|
|
const hash = await this.cryptoFunctionService.pbkdf2(key.key, password, 'sha256', iterations);
|
2018-04-22 05:14:04 +02:00
|
|
|
return Utils.fromBufferToB64(hash);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async makeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, EncString]> {
|
2018-11-14 02:43:45 +01:00
|
|
|
const theKey = await this.getKeyForEncryption(key);
|
2018-06-13 22:48:00 +02:00
|
|
|
const encKey = await this.cryptoFunctionService.randomBytes(64);
|
2018-11-14 02:43:45 +01:00
|
|
|
return this.buildEncKey(theKey, encKey);
|
2018-08-28 01:06:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async remakeEncKey(key: SymmetricCryptoKey, encKey?: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, EncString]> {
|
2020-12-22 16:53:48 +01:00
|
|
|
if (encKey == null) {
|
|
|
|
encKey = await this.getEncKey();
|
|
|
|
}
|
2018-08-28 01:06:36 +02:00
|
|
|
return this.buildEncKey(key, encKey.key);
|
2018-06-13 22:48:00 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncString> {
|
2018-04-22 05:14:04 +02:00
|
|
|
if (plainValue == null) {
|
2018-01-08 20:11:28 +01:00
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
|
2018-04-22 05:14:04 +02:00
|
|
|
let plainBuf: ArrayBuffer;
|
|
|
|
if (typeof (plainValue) === 'string') {
|
|
|
|
plainBuf = Utils.fromUtf8ToArray(plainValue).buffer;
|
2018-01-08 20:11:28 +01:00
|
|
|
} else {
|
2018-04-22 05:14:04 +02:00
|
|
|
plainBuf = plainValue;
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2018-04-22 05:14:04 +02:00
|
|
|
const encObj = await this.aesEncrypt(plainBuf, key);
|
|
|
|
const iv = Utils.fromBufferToB64(encObj.iv);
|
2018-05-09 22:00:15 +02:00
|
|
|
const data = Utils.fromBufferToB64(encObj.data);
|
2018-04-22 05:14:04 +02:00
|
|
|
const mac = encObj.mac != null ? Utils.fromBufferToB64(encObj.mac) : null;
|
2021-04-21 02:16:19 +02:00
|
|
|
return new EncString(encObj.key.encType, data, iv, mac);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncArrayBuffer> {
|
2018-01-08 20:11:28 +01:00
|
|
|
const encValue = await this.aesEncrypt(plainValue, key);
|
|
|
|
let macLen = 0;
|
2018-04-22 05:14:04 +02:00
|
|
|
if (encValue.mac != null) {
|
|
|
|
macLen = encValue.mac.byteLength;
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
const encBytes = new Uint8Array(1 + encValue.iv.byteLength + macLen + encValue.data.byteLength);
|
2018-01-08 20:11:28 +01:00
|
|
|
encBytes.set([encValue.key.encType]);
|
2018-04-22 05:14:04 +02:00
|
|
|
encBytes.set(new Uint8Array(encValue.iv), 1);
|
|
|
|
if (encValue.mac != null) {
|
|
|
|
encBytes.set(new Uint8Array(encValue.mac), 1 + encValue.iv.byteLength);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
encBytes.set(new Uint8Array(encValue.data), 1 + encValue.iv.byteLength + macLen);
|
2021-04-21 02:16:19 +02:00
|
|
|
return new EncArrayBuffer(encBytes.buffer);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async rsaEncrypt(data: ArrayBuffer, publicKey?: ArrayBuffer): Promise<EncString> {
|
2018-07-11 19:30:06 +02:00
|
|
|
if (publicKey == null) {
|
|
|
|
publicKey = await this.getPublicKey();
|
|
|
|
}
|
|
|
|
if (publicKey == null) {
|
|
|
|
throw new Error('Public key unavailable.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const encBytes = await this.cryptoFunctionService.rsaEncrypt(data, publicKey, 'sha1');
|
2021-04-21 02:16:19 +02:00
|
|
|
return new EncString(EncryptionType.Rsa2048_OaepSha1_B64, Utils.fromBufferToB64(encBytes));
|
2018-07-11 19:30:06 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 20:29:50 +02:00
|
|
|
async rsaDecrypt(encValue: string, privateKeyValue?: ArrayBuffer): Promise<ArrayBuffer> {
|
2020-12-22 16:53:48 +01:00
|
|
|
const headerPieces = encValue.split('.');
|
|
|
|
let encType: EncryptionType = null;
|
|
|
|
let encPieces: string[];
|
|
|
|
|
|
|
|
if (headerPieces.length === 1) {
|
|
|
|
encType = EncryptionType.Rsa2048_OaepSha256_B64;
|
|
|
|
encPieces = [headerPieces[0]];
|
|
|
|
} else if (headerPieces.length === 2) {
|
|
|
|
try {
|
|
|
|
encType = parseInt(headerPieces[0], null);
|
|
|
|
encPieces = headerPieces[1].split('|');
|
2021-10-19 10:32:14 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2020-12-22 16:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (encType) {
|
|
|
|
case EncryptionType.Rsa2048_OaepSha256_B64:
|
|
|
|
case EncryptionType.Rsa2048_OaepSha1_B64:
|
|
|
|
// HmacSha256 types are deprecated
|
|
|
|
case EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64:
|
|
|
|
case EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('encType unavailable.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encPieces == null || encPieces.length <= 0) {
|
|
|
|
throw new Error('encPieces unavailable.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = Utils.fromB64ToArray(encPieces[0]).buffer;
|
2021-05-24 20:29:50 +02:00
|
|
|
const privateKey = privateKeyValue ?? await this.getPrivateKey();
|
2020-12-22 16:53:48 +01:00
|
|
|
if (privateKey == null) {
|
|
|
|
throw new Error('No private key.');
|
|
|
|
}
|
|
|
|
|
|
|
|
let alg: 'sha1' | 'sha256' = 'sha1';
|
|
|
|
switch (encType) {
|
|
|
|
case EncryptionType.Rsa2048_OaepSha256_B64:
|
|
|
|
case EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64:
|
|
|
|
alg = 'sha256';
|
|
|
|
break;
|
|
|
|
case EncryptionType.Rsa2048_OaepSha1_B64:
|
|
|
|
case EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('encType unavailable.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.cryptoFunctionService.rsaDecrypt(data, privateKey, alg);
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async decryptToBytes(encString: EncString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
|
|
|
const iv = Utils.fromB64ToArray(encString.iv).buffer;
|
|
|
|
const data = Utils.fromB64ToArray(encString.data).buffer;
|
|
|
|
const mac = encString.mac ? Utils.fromB64ToArray(encString.mac).buffer : null;
|
|
|
|
const decipher = await this.aesDecryptToBytes(encString.encryptionType, data, iv, mac, key);
|
2018-04-22 05:14:04 +02:00
|
|
|
if (decipher == null) {
|
2018-01-08 20:11:28 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-22 05:14:04 +02:00
|
|
|
return decipher;
|
|
|
|
}
|
2018-01-18 06:08:10 +01:00
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async decryptToUtf8(encString: EncString, key?: SymmetricCryptoKey): Promise<string> {
|
|
|
|
return await this.aesDecryptToUtf8(encString.encryptionType, encString.data,
|
|
|
|
encString.iv, encString.mac, key);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
2018-04-22 05:14:04 +02:00
|
|
|
if (encBuf == null) {
|
2018-01-08 20:11:28 +01:00
|
|
|
throw new Error('no encBuf.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const encBytes = new Uint8Array(encBuf);
|
|
|
|
const encType = encBytes[0];
|
|
|
|
let ctBytes: Uint8Array = null;
|
|
|
|
let ivBytes: Uint8Array = null;
|
|
|
|
let macBytes: Uint8Array = null;
|
|
|
|
|
|
|
|
switch (encType) {
|
|
|
|
case EncryptionType.AesCbc128_HmacSha256_B64:
|
|
|
|
case EncryptionType.AesCbc256_HmacSha256_B64:
|
|
|
|
if (encBytes.length <= 49) { // 1 + 16 + 32 + ctLength
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ivBytes = encBytes.slice(1, 17);
|
|
|
|
macBytes = encBytes.slice(17, 49);
|
|
|
|
ctBytes = encBytes.slice(49);
|
|
|
|
break;
|
|
|
|
case EncryptionType.AesCbc256_B64:
|
|
|
|
if (encBytes.length <= 17) { // 1 + 16 + ctLength
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ivBytes = encBytes.slice(1, 17);
|
|
|
|
ctBytes = encBytes.slice(17);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:00:49 +02:00
|
|
|
return await this.aesDecryptToBytes(encType, ctBytes.buffer, ivBytes.buffer,
|
2018-04-22 05:14:04 +02:00
|
|
|
macBytes != null ? macBytes.buffer : null, key);
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 19:03:47 +02:00
|
|
|
// EFForg/OpenWireless
|
|
|
|
// ref https://github.com/EFForg/OpenWireless/blob/master/app/js/diceware.js
|
|
|
|
async randomNumber(min: number, max: number): Promise<number> {
|
|
|
|
let rval = 0;
|
|
|
|
const range = max - min + 1;
|
|
|
|
const bitsNeeded = Math.ceil(Math.log2(range));
|
|
|
|
if (bitsNeeded > 53) {
|
|
|
|
throw new Error('We cannot generate numbers larger than 53 bits.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const bytesNeeded = Math.ceil(bitsNeeded / 8);
|
|
|
|
const mask = Math.pow(2, bitsNeeded) - 1;
|
|
|
|
// 7776 -> (2^13 = 8192) -1 == 8191 or 0x00001111 11111111
|
|
|
|
|
|
|
|
// Fill a byte array with N random numbers
|
|
|
|
const byteArray = new Uint8Array(await this.cryptoFunctionService.randomBytes(bytesNeeded));
|
|
|
|
|
|
|
|
let p = (bytesNeeded - 1) * 8;
|
|
|
|
for (let i = 0; i < bytesNeeded; i++) {
|
|
|
|
rval += byteArray[i] * Math.pow(2, p);
|
|
|
|
p -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use & to apply the mask and reduce the number of recursive lookups
|
|
|
|
// tslint:disable-next-line
|
|
|
|
rval = rval & mask;
|
|
|
|
|
|
|
|
if (rval >= range) {
|
|
|
|
// Integer out of acceptable range
|
|
|
|
return this.randomNumber(min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an integer that falls within the range
|
|
|
|
return min + rval;
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:02:43 +02:00
|
|
|
async validateKey(key: SymmetricCryptoKey) {
|
|
|
|
try {
|
|
|
|
const encPrivateKey = await this.storageService.get<string>(Keys.encPrivateKey);
|
2021-06-09 22:53:54 +02:00
|
|
|
const encKey = await this.getEncKey(key);
|
|
|
|
if (encPrivateKey == null || encKey == null) {
|
2021-04-28 22:02:43 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const privateKey = await this.decryptToBytes(new EncString(encPrivateKey), encKey);
|
|
|
|
await this.cryptoFunctionService.rsaExtractPublicKey(privateKey);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-22 05:14:04 +02:00
|
|
|
// Helpers
|
|
|
|
|
2021-06-15 16:55:57 +02:00
|
|
|
protected async storeKey(key: SymmetricCryptoKey) {
|
|
|
|
if (await this.shouldStoreKey('auto') || await this.shouldStoreKey('biometric')) {
|
|
|
|
this.secureStorageService.save(Keys.key, key.keyB64);
|
|
|
|
} else {
|
|
|
|
this.secureStorageService.remove(Keys.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async shouldStoreKey(keySuffix: KeySuffixOptions) {
|
2021-06-09 22:53:54 +02:00
|
|
|
let shouldStoreKey = false;
|
|
|
|
if (keySuffix === 'auto') {
|
|
|
|
const vaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
|
|
|
|
shouldStoreKey = vaultTimeout == null;
|
|
|
|
} else if (keySuffix === 'biometric') {
|
|
|
|
const biometricUnlock = await this.storageService.get<boolean>(ConstantsService.biometricUnlockKey);
|
|
|
|
shouldStoreKey = biometricUnlock && this.platformUtilService.supportsSecureStorage();
|
|
|
|
}
|
|
|
|
return shouldStoreKey;
|
|
|
|
}
|
2021-04-28 22:02:43 +02:00
|
|
|
|
2021-06-15 16:55:57 +02:00
|
|
|
protected retrieveKeyFromStorage(keySuffix: KeySuffixOptions) {
|
|
|
|
return this.secureStorageService.get<string>(Keys.key, { keySuffix: keySuffix });
|
2021-04-28 22:02:43 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
private async aesEncrypt(data: ArrayBuffer, key: SymmetricCryptoKey): Promise<EncryptedObject> {
|
2018-04-22 05:14:04 +02:00
|
|
|
const obj = new EncryptedObject();
|
|
|
|
obj.key = await this.getKeyForEncryption(key);
|
|
|
|
obj.iv = await this.cryptoFunctionService.randomBytes(16);
|
2018-05-09 22:00:15 +02:00
|
|
|
obj.data = await this.cryptoFunctionService.aesEncrypt(data, obj.iv, obj.key.encKey);
|
2018-04-22 05:14:04 +02:00
|
|
|
|
|
|
|
if (obj.key.macKey != null) {
|
2018-05-09 22:00:15 +02:00
|
|
|
const macData = new Uint8Array(obj.iv.byteLength + obj.data.byteLength);
|
2018-04-22 05:14:04 +02:00
|
|
|
macData.set(new Uint8Array(obj.iv), 0);
|
2018-05-09 22:00:15 +02:00
|
|
|
macData.set(new Uint8Array(obj.data), obj.iv.byteLength);
|
2018-04-22 05:14:04 +02:00
|
|
|
obj.mac = await this.cryptoFunctionService.hmac(macData.buffer, obj.key.macKey, 'sha256');
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
private async aesDecryptToUtf8(encType: EncryptionType, data: string, iv: string, mac: string,
|
2018-05-07 15:00:49 +02:00
|
|
|
key: SymmetricCryptoKey): Promise<string> {
|
2018-04-22 05:14:04 +02:00
|
|
|
const keyForEnc = await this.getKeyForEncryption(key);
|
|
|
|
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
|
|
|
|
|
|
|
if (theKey.macKey != null && mac == null) {
|
2020-12-11 17:44:57 +01:00
|
|
|
this.logService.error('mac required.');
|
2018-04-22 05:14:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:00:49 +02:00
|
|
|
if (theKey.encType !== encType) {
|
2020-12-11 17:44:57 +01:00
|
|
|
this.logService.error('encType unavailable.');
|
2018-04-22 05:14:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
const fastParams = this.cryptoFunctionService.aesDecryptFastParameters(data, iv, mac, theKey);
|
2018-05-07 15:00:49 +02:00
|
|
|
if (fastParams.macKey != null && fastParams.mac != null) {
|
|
|
|
const computedMac = await this.cryptoFunctionService.hmacFast(fastParams.macData,
|
|
|
|
fastParams.macKey, 'sha256');
|
2018-05-07 18:14:40 +02:00
|
|
|
const macsEqual = await this.cryptoFunctionService.compareFast(fastParams.mac, computedMac);
|
2018-05-07 15:00:49 +02:00
|
|
|
if (!macsEqual) {
|
2020-12-11 17:44:57 +01:00
|
|
|
this.logService.error('mac failed.');
|
2018-04-22 05:14:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:00:49 +02:00
|
|
|
return this.cryptoFunctionService.aesDecryptFast(fastParams);
|
2018-04-22 05:14:04 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
private async aesDecryptToBytes(encType: EncryptionType, data: ArrayBuffer, iv: ArrayBuffer,
|
2018-04-22 05:14:04 +02:00
|
|
|
mac: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
2018-05-07 15:00:49 +02:00
|
|
|
const keyForEnc = await this.getKeyForEncryption(key);
|
|
|
|
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
|
|
|
|
|
|
|
if (theKey.macKey != null && mac == null) {
|
2018-04-22 05:14:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:00:49 +02:00
|
|
|
if (theKey.encType !== encType) {
|
2018-04-22 05:14:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:00:49 +02:00
|
|
|
if (theKey.macKey != null && mac != null) {
|
2018-05-09 22:00:15 +02:00
|
|
|
const macData = new Uint8Array(iv.byteLength + data.byteLength);
|
2018-05-07 15:00:49 +02:00
|
|
|
macData.set(new Uint8Array(iv), 0);
|
2018-05-09 22:00:15 +02:00
|
|
|
macData.set(new Uint8Array(data), iv.byteLength);
|
2018-05-07 15:00:49 +02:00
|
|
|
const computedMac = await this.cryptoFunctionService.hmac(macData.buffer, theKey.macKey, 'sha256');
|
|
|
|
if (computedMac === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-07 18:14:40 +02:00
|
|
|
const macsMatch = await this.cryptoFunctionService.compare(mac, computedMac);
|
2018-05-07 15:00:49 +02:00
|
|
|
if (!macsMatch) {
|
2020-12-11 17:44:57 +01:00
|
|
|
this.logService.error('mac failed.');
|
2018-05-07 15:00:49 +02:00
|
|
|
return null;
|
|
|
|
}
|
2018-04-22 05:14:04 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:15 +02:00
|
|
|
return await this.cryptoFunctionService.aesDecrypt(data, iv, theKey.encKey);
|
2018-04-22 05:14:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 20:11:28 +01:00
|
|
|
private async getKeyForEncryption(key?: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
|
2018-04-22 05:14:04 +02:00
|
|
|
if (key != null) {
|
2018-01-08 20:11:28 +01:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encKey = await this.getEncKey();
|
2018-04-22 05:14:04 +02:00
|
|
|
if (encKey != null) {
|
|
|
|
return encKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.getKey();
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private resolveLegacyKey(encType: EncryptionType, key: SymmetricCryptoKey): SymmetricCryptoKey {
|
|
|
|
if (encType === EncryptionType.AesCbc128_HmacSha256_B64 &&
|
|
|
|
key.encType === EncryptionType.AesCbc256_B64) {
|
|
|
|
// Old encrypt-then-mac scheme, make a new key
|
2018-04-22 05:14:04 +02:00
|
|
|
if (this.legacyEtmKey == null) {
|
|
|
|
this.legacyEtmKey = new SymmetricCryptoKey(key.key, EncryptionType.AesCbc128_HmacSha256_B64);
|
|
|
|
}
|
2018-01-08 20:11:28 +01:00
|
|
|
return this.legacyEtmKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
2018-06-13 22:48:00 +02:00
|
|
|
|
2018-06-13 23:10:52 +02:00
|
|
|
private async stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
|
|
|
|
const newKey = new Uint8Array(64);
|
2020-10-29 20:52:12 +01:00
|
|
|
const encKey = await this.cryptoFunctionService.hkdfExpand(key.key, 'enc', 32, 'sha256');
|
|
|
|
const macKey = await this.cryptoFunctionService.hkdfExpand(key.key, 'mac', 32, 'sha256');
|
|
|
|
newKey.set(new Uint8Array(encKey));
|
|
|
|
newKey.set(new Uint8Array(macKey), 32);
|
2018-06-13 23:10:52 +02:00
|
|
|
return new SymmetricCryptoKey(newKey.buffer);
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:20:54 +01:00
|
|
|
private async hashPhrase(hash: ArrayBuffer, minimumEntropy: number = 64) {
|
2018-11-08 05:12:45 +01:00
|
|
|
const entropyPerWord = Math.log(EEFLongWordList.length) / Math.log(2);
|
2018-11-08 04:54:56 +01:00
|
|
|
let numWords = Math.ceil(minimumEntropy / entropyPerWord);
|
|
|
|
|
2018-11-09 14:20:54 +01:00
|
|
|
const hashArr = Array.from(new Uint8Array(hash));
|
|
|
|
const entropyAvailable = hashArr.length * 4;
|
2018-11-08 04:54:56 +01:00
|
|
|
if (numWords * entropyPerWord > entropyAvailable) {
|
|
|
|
throw new Error('Output entropy of hash function is too small');
|
|
|
|
}
|
|
|
|
|
|
|
|
const phrase: string[] = [];
|
2018-11-09 14:20:54 +01:00
|
|
|
let hashNumber = bigInt.fromArray(hashArr, 256);
|
2018-11-08 04:54:56 +01:00
|
|
|
while (numWords--) {
|
2018-11-08 05:12:45 +01:00
|
|
|
const remainder = hashNumber.mod(EEFLongWordList.length);
|
|
|
|
hashNumber = hashNumber.divide(EEFLongWordList.length);
|
2018-11-08 04:54:56 +01:00
|
|
|
phrase.push(EEFLongWordList[remainder as any]);
|
|
|
|
}
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:06:05 +02:00
|
|
|
private async buildEncKey(key: SymmetricCryptoKey, encKey: ArrayBuffer)
|
2021-04-21 02:16:19 +02:00
|
|
|
: Promise<[SymmetricCryptoKey, EncString]> {
|
|
|
|
let encKeyEnc: EncString = null;
|
2018-08-28 01:06:36 +02:00
|
|
|
if (key.key.byteLength === 32) {
|
|
|
|
const newKey = await this.stretchKey(key);
|
|
|
|
encKeyEnc = await this.encrypt(encKey, newKey);
|
|
|
|
} else if (key.key.byteLength === 64) {
|
|
|
|
encKeyEnc = await this.encrypt(encKey, key);
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid key size.');
|
|
|
|
}
|
|
|
|
return [new SymmetricCryptoKey(encKey), encKeyEnc];
|
|
|
|
}
|
2018-01-08 20:11:28 +01:00
|
|
|
}
|