2016-10-25 04:16:47 +02:00
|
|
|
function CryptoService(constantsService) {
|
|
|
|
this.constantsService = constantsService;
|
2017-04-22 16:58:32 +02:00
|
|
|
initCryptoService(constantsService);
|
2016-09-03 06:03:13 +02:00
|
|
|
};
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
function initCryptoService(constantsService) {
|
2016-09-03 06:03:13 +02:00
|
|
|
var _key,
|
2017-04-22 16:58:32 +02:00
|
|
|
_legacyEtmKey,
|
2016-10-25 04:16:47 +02:00
|
|
|
_keyHash,
|
2017-04-22 16:58:32 +02:00
|
|
|
_b64KeyHash;
|
2016-09-05 07:49:44 +02:00
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
CryptoService.prototype.setKey = function (key, callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
var self = this;
|
2016-09-02 06:50:57 +02:00
|
|
|
_key = key;
|
2016-10-25 04:16:47 +02:00
|
|
|
|
|
|
|
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
|
|
|
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
|
|
|
// if we have a lock option set, we do not store the key
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.storage.local.set({
|
2017-04-22 16:58:32 +02:00
|
|
|
'key': key.keyB64
|
2016-10-25 04:16:47 +02:00
|
|
|
}, function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptoService.prototype.setKeyHash = function (keyHash, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
_keyHash = keyHash;
|
2016-10-25 04:16:47 +02:00
|
|
|
|
2016-09-02 06:50:57 +02:00
|
|
|
chrome.storage.local.set({
|
2017-04-22 16:58:32 +02:00
|
|
|
'keyHash': _keyHash
|
2016-09-02 06:50:57 +02:00
|
|
|
}, function () {
|
|
|
|
callback();
|
|
|
|
});
|
2016-09-03 06:03:13 +02:00
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
CryptoService.prototype.getKey = function (callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
if (_key) {
|
2016-10-25 04:16:47 +02:00
|
|
|
callback(_key);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
var self = this;
|
|
|
|
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
|
|
|
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
|
|
|
// if we have a lock option set, we do not try to fetch the storage key since it should not even be there
|
|
|
|
callback(null);
|
|
|
|
return;
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
chrome.storage.local.get('key', function (obj) {
|
|
|
|
if (obj && obj.key) {
|
2017-04-22 16:58:32 +02:00
|
|
|
_key = new CryptoKey(obj.key, true);
|
2016-10-25 04:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(_key);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
CryptoService.prototype.getKeyHash = function (callback) {
|
2016-10-25 04:16:47 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
if (_keyHash) {
|
2016-10-25 04:16:47 +02:00
|
|
|
callback(_keyHash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.storage.local.get('keyHash', function (obj) {
|
|
|
|
if (obj && obj.keyHash) {
|
2017-04-22 16:58:32 +02:00
|
|
|
_keyHash = obj.keyHash;
|
2016-09-02 06:50:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
callback(_keyHash);
|
2016-09-02 06:50:57 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
CryptoService.prototype.clearKey = function (callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
_key = _legacyEtmKey = null;
|
2016-09-02 06:50:57 +02:00
|
|
|
chrome.storage.local.remove('key', function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-25 04:16:47 +02:00
|
|
|
CryptoService.prototype.clearKeyHash = function (callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
_keyHash = null;
|
2016-10-25 04:16:47 +02:00
|
|
|
chrome.storage.local.remove('keyHash', function () {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
CryptoService.prototype.toggleKey = function (callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
2017-04-22 16:58:32 +02:00
|
|
|
self.getKey(function (key) {
|
2016-10-25 04:16:47 +02:00
|
|
|
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
|
|
|
|
if (obj && (obj[self.constantsService.lockOptionKey] || obj[self.constantsService.lockOptionKey] === 0)) {
|
|
|
|
// if we have a lock option set, clear the key
|
|
|
|
self.clearKey(function () {
|
|
|
|
_key = key;
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no lock option, so store the current key
|
|
|
|
self.setKey(key, function () {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
CryptoService.prototype.makeKey = function (password, salt) {
|
|
|
|
var keyBytes = forge.pbkdf2(forge.util.encodeUtf8(password), forge.util.encodeUtf8(salt),
|
2017-02-16 01:02:55 +01:00
|
|
|
5000, 256 / 8, 'sha256');
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
return new CryptoKey(keyBytes);
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
|
|
|
|
2016-09-05 06:03:49 +02:00
|
|
|
CryptoService.prototype.hashPassword = function (password, key, callback) {
|
2017-04-22 16:58:32 +02:00
|
|
|
this.getKey(function (storedKey) {
|
|
|
|
key = key || storedKey;
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-05 06:03:49 +02:00
|
|
|
if (!password || !key) {
|
|
|
|
throw 'Invalid parameters.';
|
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
var hashBits = forge.pbkdf2(key.key, forge.util.encodeUtf8(password), 1, 256 / 8, 'sha256');
|
2017-01-13 05:57:56 +01:00
|
|
|
callback(forge.util.encode64(hashBits));
|
2016-12-10 16:33:58 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
CryptoService.prototype.encrypt = function (plainValue, key, plainValueEncoding) {
|
2016-12-10 16:33:58 +01:00
|
|
|
var self = this;
|
2016-09-17 05:47:50 +02:00
|
|
|
var deferred = Q.defer();
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
if (plainValue === null || plainValue === undefined) {
|
2016-09-17 05:47:50 +02:00
|
|
|
deferred.resolve(null);
|
|
|
|
}
|
|
|
|
else {
|
2017-04-22 16:58:32 +02:00
|
|
|
self.getKey(function (localKey) {
|
|
|
|
key = key || localKey;
|
|
|
|
if (!key) {
|
|
|
|
throw 'Encryption key unavailable.';
|
|
|
|
}
|
|
|
|
|
|
|
|
plainValueEncoding = plainValueEncoding || 'utf8';
|
|
|
|
var buffer = forge.util.createBuffer(plainValue, plainValueEncoding);
|
|
|
|
var ivBytes = forge.random.getBytesSync(16);
|
|
|
|
var cipher = forge.cipher.createCipher('AES-CBC', key.encKey);
|
|
|
|
cipher.start({ iv: ivBytes });
|
|
|
|
cipher.update(buffer);
|
|
|
|
cipher.finish();
|
|
|
|
|
|
|
|
var iv = forge.util.encode64(ivBytes);
|
|
|
|
var ctBytes = cipher.output.getBytes();
|
|
|
|
var ct = forge.util.encode64(ctBytes);
|
|
|
|
var mac = !key.macKey ? null : computeMac(ctBytes, ivBytes, key.macKey);
|
|
|
|
|
|
|
|
var cs = new CipherString(key.encType, iv, ct, mac);
|
|
|
|
deferred.resolve(cs);
|
2016-09-17 05:47:50 +02:00
|
|
|
});
|
2016-09-05 07:49:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 05:47:50 +02:00
|
|
|
return deferred.promise;
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
CryptoService.prototype.decrypt = function (cipherString, key, outputEncoding) {
|
2016-09-17 05:47:50 +02:00
|
|
|
var deferred = Q.defer();
|
2016-12-10 16:33:58 +01:00
|
|
|
var self = this;
|
2016-09-02 06:50:57 +02:00
|
|
|
|
2016-09-05 17:05:27 +02:00
|
|
|
if (cipherString === null || cipherString === undefined || !cipherString.encryptedString) {
|
2016-09-05 07:49:44 +02:00
|
|
|
throw 'cannot decrypt nothing';
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:58:32 +02:00
|
|
|
self.getKey(function (localKey) {
|
|
|
|
key = key || localKey;
|
|
|
|
if (!key) {
|
|
|
|
throw 'Encryption key unavailable.';
|
|
|
|
}
|
|
|
|
|
|
|
|
outputEncoding = outputEncoding || 'utf8';
|
|
|
|
|
|
|
|
if (cipherString.encryptionType === constantsService.encType.AesCbc128_HmacSha256_B64 &&
|
|
|
|
key.encType === constantsService.encType.AesCbc256_B64) {
|
|
|
|
// Old encrypt-then-mac scheme, swap out the key
|
|
|
|
_legacyEtmKey = _legacyEtmKey ||
|
|
|
|
new CryptoKey(key.key, false, constantsService.encType.AesCbc128_HmacSha256_B64);
|
|
|
|
key = _legacyEtmKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cipherString.encryptionType !== key.encType) {
|
|
|
|
throw 'encType unavailable.';
|
|
|
|
}
|
|
|
|
|
|
|
|
var ivBytes = forge.util.decode64(cipherString.initializationVector);
|
|
|
|
var ctBytes = forge.util.decode64(cipherString.cipherText);
|
|
|
|
|
|
|
|
if (key.macKey && cipherString.mac) {
|
|
|
|
var computedMac = computeMac(ctBytes, ivBytes, key.macKey);
|
|
|
|
if (computedMac !== cipherString.mac) {
|
|
|
|
console.error('MAC failed.');
|
|
|
|
deferred.reject('MAC failed.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var ctBuffer = forge.util.createBuffer(ctBytes);
|
|
|
|
var decipher = forge.cipher.createDecipher('AES-CBC', key.encKey);
|
|
|
|
decipher.start({ iv: ivBytes });
|
|
|
|
decipher.update(ctBuffer);
|
|
|
|
decipher.finish();
|
|
|
|
|
|
|
|
var decValue;
|
|
|
|
if (outputEncoding === 'utf8') {
|
|
|
|
decValue = decipher.output.toString('utf8');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
decValue = decipher.output.getBytes();
|
|
|
|
}
|
|
|
|
deferred.resolve(decValue);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
deferred.reject('Decryption failed.');
|
|
|
|
}
|
2016-09-04 06:34:24 +02:00
|
|
|
});
|
2016-09-17 05:47:50 +02:00
|
|
|
|
|
|
|
return deferred.promise;
|
2016-09-02 06:50:57 +02:00
|
|
|
};
|
2016-12-10 16:33:58 +01:00
|
|
|
|
|
|
|
function computeMac(ct, iv, macKey) {
|
2017-01-13 05:57:56 +01:00
|
|
|
var hmac = forge.hmac.create();
|
|
|
|
hmac.start('sha256', macKey);
|
2017-01-13 06:22:42 +01:00
|
|
|
hmac.update(iv + ct);
|
2017-01-13 05:57:56 +01:00
|
|
|
var mac = hmac.digest();
|
2017-01-13 06:22:42 +01:00
|
|
|
return forge.util.encode64(mac.getBytes());
|
2016-12-10 16:33:58 +01:00
|
|
|
}
|
2017-04-22 16:58:32 +02:00
|
|
|
|
|
|
|
function CryptoKey(keyBytes, b64KeyBytes, encType) {
|
|
|
|
if (b64KeyBytes) {
|
|
|
|
keyBytes = forge.util.decode64(keyBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keyBytes) {
|
|
|
|
throw 'Must provide keyBytes';
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer = forge.util.createBuffer(keyBytes);
|
|
|
|
if (!buffer || buffer.length() === 0) {
|
|
|
|
throw 'Couldn\'t make buffer';
|
|
|
|
}
|
|
|
|
var bufferLength = buffer.length();
|
|
|
|
|
|
|
|
if (encType === null || encType === undefined) {
|
|
|
|
if (bufferLength === 32) {
|
|
|
|
encType = constantsService.encType.AesCbc256_B64;
|
|
|
|
}
|
|
|
|
else if (bufferLength === 64) {
|
|
|
|
encType = constantsService.encType.AesCbc256_HmacSha256_B64;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw 'Unable to determine encType.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.key = keyBytes;
|
|
|
|
this.keyB64 = forge.util.encode64(keyBytes);
|
|
|
|
this.encType = encType;
|
|
|
|
|
|
|
|
if (encType === constantsService.encType.AesCbc256_B64 && bufferLength === 32) {
|
|
|
|
this.encKey = keyBytes;
|
|
|
|
this.macKey = null;
|
|
|
|
}
|
|
|
|
else if (encType === constantsService.encType.AesCbc128_HmacSha256_B64 && bufferLength === 32) {
|
|
|
|
this.encKey = buffer.getBytes(16); // first half
|
|
|
|
this.macKey = buffer.getBytes(16); // second half
|
|
|
|
}
|
|
|
|
else if (encType === constantsService.encType.AesCbc256_HmacSha256_B64 && bufferLength === 64) {
|
|
|
|
this.encKey = buffer.getBytes(32); // first half
|
|
|
|
this.macKey = buffer.getBytes(32); // second half
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw 'Unsupported encType/key length.';
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 06:34:24 +02:00
|
|
|
};
|