From bed28aebaa613798b173ac545dd99e5270961258 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 13 Nov 2017 16:12:23 -0500 Subject: [PATCH] interface cryptoservice --- src/models/domain/cipherString.ts | 2 +- src/popup/app/lock/lock.component.ts | 4 ++- src/popup/app/services/auth.service.ts | 8 +++--- src/popup/app/services/background.service.ts | 3 ++- src/popup/app/services/validation.service.ts | 3 +-- src/services/abstractions/crypto.service.ts | 28 ++++++++++++++++++++ src/services/crypto.service.ts | 22 ++++++++------- tslint.json | 1 + 8 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 src/services/abstractions/crypto.service.ts diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index 9166229778..26d557e7ec 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -1,5 +1,5 @@ import { EncryptionType } from '../../enums/encryptionType.enum'; -import CryptoService from '../../services/crypto.service'; +import { CryptoService } from '../../services/abstractions/crypto.service'; class CipherString { encryptedString?: string; diff --git a/src/popup/app/lock/lock.component.ts b/src/popup/app/lock/lock.component.ts index 251989081e..c8c1a9bd52 100644 --- a/src/popup/app/lock/lock.component.ts +++ b/src/popup/app/lock/lock.component.ts @@ -1,10 +1,12 @@ import * as template from './lock.component.html'; +import { CryptoService } from '../../../services/abstractions/crypto.service'; + class LockController { i18n: any; constructor(public $scope: any, public $state: any, public i18nService: any, - public cryptoService: any, public toastr: any, public userService: any, + public cryptoService: CryptoService, public toastr: any, public userService: any, public SweetAlert: any, public $timeout: any) { this.i18n = i18nService; diff --git a/src/popup/app/services/auth.service.ts b/src/popup/app/services/auth.service.ts index 8f05040d1f..24dfad7868 100644 --- a/src/popup/app/services/auth.service.ts +++ b/src/popup/app/services/auth.service.ts @@ -1,13 +1,13 @@ import { DeviceRequest } from '../../../models/request/deviceRequest'; import { TokenRequest } from '../../../models/request/tokenRequest'; +import { CryptoService } from '../../../services/abstractions/crypto.service'; import { UtilsService } from '../../../services/abstractions/utils.service'; class AuthService { - constructor(public cryptoService: any, public apiService: any, public userService: any, public tokenService: any, - public $rootScope: any, public appIdService: any, public utilsService: UtilsService, - public constantsService: any) { - + constructor(public cryptoService: CryptoService, public apiService: any, public userService: any, + public tokenService: any, public $rootScope: any, public appIdService: any, public utilsService: UtilsService, + public constantsService: any) { } async logIn(email: string, masterPassword: string, twoFactorProvider?: number, diff --git a/src/popup/app/services/background.service.ts b/src/popup/app/services/background.service.ts index e654570724..ccf72ebc97 100644 --- a/src/popup/app/services/background.service.ts +++ b/src/popup/app/services/background.service.ts @@ -1,3 +1,4 @@ +import { CryptoService } from '../../../services/abstractions/crypto.service'; import { UtilsService } from '../../../services/abstractions/utils.service'; function getBackgroundService(service: string) { @@ -12,7 +13,7 @@ export const cryptoService = getBackgroundService('cryptoService'); export const userService = getBackgroundService('userService'); export const apiService = getBackgroundService('apiService'); export const folderService = getBackgroundService('folderService'); -export const cipherService = getBackgroundService('cipherService'); +export const cipherService = getBackgroundService('cipherService'); export const syncService = getBackgroundService('syncService'); export const autofillService = getBackgroundService('autofillService'); export const passwordGenerationService = getBackgroundService('passwordGenerationService'); diff --git a/src/popup/app/services/validation.service.ts b/src/popup/app/services/validation.service.ts index 4f82767102..a31829837e 100644 --- a/src/popup/app/services/validation.service.ts +++ b/src/popup/app/services/validation.service.ts @@ -1,7 +1,6 @@ -import * as angular from 'angular'; +import * as angular from 'angular'; class ValidationService { - constructor(private toastr: any, private i18nService: any) { } diff --git a/src/services/abstractions/crypto.service.ts b/src/services/abstractions/crypto.service.ts new file mode 100644 index 0000000000..1fd413d34f --- /dev/null +++ b/src/services/abstractions/crypto.service.ts @@ -0,0 +1,28 @@ +import { CipherString } from '../../models/domain/cipherString'; +import SymmetricCryptoKey from '../../models/domain/symmetricCryptoKey'; + +import { ProfileOrganizationResponse } from '../../models/response/profileOrganizationResponse'; + +export interface CryptoService { + setKey(key: SymmetricCryptoKey): Promise; + setKeyHash(keyHash: string): Promise<{}>; + setEncKey(encKey: string): Promise<{}>; + setEncPrivateKey(encPrivateKey: string): Promise<{}>; + setOrgKeys(orgs: ProfileOrganizationResponse[]): Promise<{}>; + getKey(): Promise; + getKeyHash(): Promise; + getEncKey(): Promise; + getPrivateKey(): Promise; + getOrgKeys(): Promise>; + getOrgKey(orgId: string): Promise; + clearKeys(): Promise; + toggleKey(): Promise; + makeKey(password: string, salt: string): SymmetricCryptoKey; + hashPassword(password: string, key: SymmetricCryptoKey): Promise; + makeEncKey(key: SymmetricCryptoKey): Promise; + encrypt(plainValue: string | Uint8Array, key?: SymmetricCryptoKey, plainValueEncoding?: string): Promise; + encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise; + decrypt(cipherString: CipherString, key?: SymmetricCryptoKey, outputEncoding?: string): Promise; + decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise; + rsaDecrypt(encValue: string): Promise; +} diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index 384193a9cb..4943451bca 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -8,6 +8,8 @@ import { ProfileOrganizationResponse } from '../models/response/profileOrganizat import ConstantsService from './constants.service'; import UtilsService from './utils.service'; +import { CryptoService as CryptoServiceInterface } from './abstractions/crypto.service'; + const Keys = { key: 'key', encOrgKeys: 'encOrgKeys', @@ -28,7 +30,7 @@ const AesAlgorithm = { const Crypto = window.crypto; const Subtle = Crypto.subtle; -export default class CryptoService { +export default class CryptoService implements CryptoServiceInterface { private key: SymmetricCryptoKey; private encKey: SymmetricCryptoKey; private legacyEtmKey: SymmetricCryptoKey; @@ -36,7 +38,7 @@ export default class CryptoService { private privateKey: ArrayBuffer; private orgKeys: Map; - async setKey(key: SymmetricCryptoKey) { + async setKey(key: SymmetricCryptoKey): Promise { this.key = key; const option = await UtilsService.getObjFromStorage(ConstantsService.lockOptionKey); @@ -53,7 +55,7 @@ export default class CryptoService { return UtilsService.saveObjToStorage(Keys.keyHash, keyHash); } - async setEncKey(encKey: string) { + async setEncKey(encKey: string): Promise<{}> { if (encKey == null) { return; } @@ -61,7 +63,7 @@ export default class CryptoService { this.encKey = null; } - async setEncPrivateKey(encPrivateKey: string) { + async setEncPrivateKey(encPrivateKey: string): Promise<{}> { if (encPrivateKey == null) { return; } @@ -246,7 +248,7 @@ export default class CryptoService { await this.setKey(key); } - makeKey(password: string, salt: string) { + makeKey(password: string, salt: string): SymmetricCryptoKey { const keyBytes: string = forge.pbkdf2(forge.util.encodeUtf8(password), forge.util.encodeUtf8(salt), 5000, 256 / 8, 'sha256'); return new SymmetricCryptoKey(keyBytes); @@ -270,7 +272,7 @@ export default class CryptoService { } async encrypt(plainValue: string | Uint8Array, key?: SymmetricCryptoKey, - plainValueEncoding: string = 'utf8'): Promise { + plainValueEncoding: string = 'utf8'): Promise { if (!plainValue) { return Promise.resolve(null); } @@ -308,7 +310,7 @@ export default class CryptoService { } async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey, - outputEncoding: string = 'utf8'): Promise { + outputEncoding: string = 'utf8'): Promise { const ivBytes: string = forge.util.decode64(cipherString.initializationVector); const ctBytes: string = forge.util.decode64(cipherString.cipherText); const macBytes: string = cipherString.mac ? forge.util.decode64(cipherString.mac) : null; @@ -361,7 +363,7 @@ export default class CryptoService { return await this.aesDecryptWC(encType, ctBytes.buffer, ivBytes.buffer, macBytes ? macBytes.buffer : null, key); } - async rsaDecrypt(encValue: string) { + async rsaDecrypt(encValue: string): Promise { const headerPieces = encValue.split('.'); let encType: EncryptionType = null; let encPieces: string[]; @@ -466,7 +468,7 @@ export default class CryptoService { } private async aesDecrypt(encType: EncryptionType, ctBytes: string, ivBytes: string, macBytes: string, - key: SymmetricCryptoKey): Promise { + key: SymmetricCryptoKey): Promise { const keyForEnc = await this.getKeyForEncryption(key); const theKey = this.resolveLegacyKey(encType, keyForEnc); @@ -495,7 +497,7 @@ export default class CryptoService { } private async aesDecryptWC(encType: EncryptionType, ctBuf: ArrayBuffer, ivBuf: ArrayBuffer, - macBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise { + macBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise { const theKey = await this.getKeyForEncryption(key); const keyBuf = theKey.getBuffers(); const encKey = await Subtle.importKey('raw', keyBuf.encKey, AesAlgorithm, false, ['decrypt']); diff --git a/tslint.json b/tslint.json index a87ee4e509..e23699d1d5 100644 --- a/tslint.json +++ b/tslint.json @@ -1,6 +1,7 @@ { "extends": "tslint:recommended", "rules": { + "align": [ true, "statements", "members" ], "ban-types": { "options": [ [ "Object", "Avoid using the `Object` type. Did you mean `object`?" ],