1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00

interface cryptoservice

This commit is contained in:
Kyle Spearrin 2017-11-13 16:12:23 -05:00
parent 11f392b036
commit bed28aebaa
8 changed files with 52 additions and 19 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -1,3 +1,4 @@
import { CryptoService } from '../../../services/abstractions/crypto.service';
import { UtilsService } from '../../../services/abstractions/utils.service';
function getBackgroundService<T>(service: string) {
@ -12,7 +13,7 @@ export const cryptoService = getBackgroundService<any>('cryptoService');
export const userService = getBackgroundService<any>('userService');
export const apiService = getBackgroundService<any>('apiService');
export const folderService = getBackgroundService<any>('folderService');
export const cipherService = getBackgroundService<any>('cipherService');
export const cipherService = getBackgroundService<CryptoService>('cipherService');
export const syncService = getBackgroundService<any>('syncService');
export const autofillService = getBackgroundService<any>('autofillService');
export const passwordGenerationService = getBackgroundService<any>('passwordGenerationService');

View File

@ -1,7 +1,6 @@
import * as angular from 'angular';
import * as angular from 'angular';
class ValidationService {
constructor(private toastr: any, private i18nService: any) {
}

View File

@ -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<any>;
setKeyHash(keyHash: string): Promise<{}>;
setEncKey(encKey: string): Promise<{}>;
setEncPrivateKey(encPrivateKey: string): Promise<{}>;
setOrgKeys(orgs: ProfileOrganizationResponse[]): Promise<{}>;
getKey(): Promise<SymmetricCryptoKey>;
getKeyHash(): Promise<string>;
getEncKey(): Promise<SymmetricCryptoKey>;
getPrivateKey(): Promise<ArrayBuffer>;
getOrgKeys(): Promise<Map<string, SymmetricCryptoKey>>;
getOrgKey(orgId: string): Promise<SymmetricCryptoKey>;
clearKeys(): Promise<any>;
toggleKey(): Promise<any>;
makeKey(password: string, salt: string): SymmetricCryptoKey;
hashPassword(password: string, key: SymmetricCryptoKey): Promise<string>;
makeEncKey(key: SymmetricCryptoKey): Promise<CipherString>;
encrypt(plainValue: string | Uint8Array, key?: SymmetricCryptoKey, plainValueEncoding?: string): Promise<CipherString>;
encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<ArrayBuffer>;
decrypt(cipherString: CipherString, key?: SymmetricCryptoKey, outputEncoding?: string): Promise<string>;
decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer>;
rsaDecrypt(encValue: string): Promise<string>;
}

View File

@ -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<string, SymmetricCryptoKey>;
async setKey(key: SymmetricCryptoKey) {
async setKey(key: SymmetricCryptoKey): Promise<any> {
this.key = key;
const option = await UtilsService.getObjFromStorage<number>(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<CipherString> {
plainValueEncoding: string = 'utf8'): Promise<CipherString> {
if (!plainValue) {
return Promise.resolve(null);
}
@ -308,7 +310,7 @@ export default class CryptoService {
}
async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey,
outputEncoding: string = 'utf8'): Promise<string> {
outputEncoding: string = 'utf8'): Promise<string> {
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<string> {
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<any> {
key: SymmetricCryptoKey): Promise<any> {
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<ArrayBuffer> {
macBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
const theKey = await this.getKeyForEncryption(key);
const keyBuf = theKey.getBuffers();
const encKey = await Subtle.importKey('raw', keyBuf.encKey, AesAlgorithm, false, ['decrypt']);

View File

@ -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`?" ],