Add new exceptions for keychain uses

Signed-off-by: Marius David Wieschollek's avatarMarius David Wieschollek <passwords.public@mdns.eu>
parent d5b244b2
......@@ -61,6 +61,8 @@ import InvalidObjectTypeError from "../Exception/Encryption/InvalidObjectTypeErr
import EncryptionNotEnabledError from "../Exception/Encryption/EncryptionNotEnabledError";
import ChallengeTypeNotSupported from "../Exception/ChallengeTypeNotSupported";
import ConfigurationError from "../Exception/ConfigruationError";
import MissingEncryptionKeyError from "../Exception/Encryption/MissingEncryptionKeyError";
import InvalidEncryptedTextLength from "../Exception/Encryption/InvalidEncryptedTextLength";
export default class DefaultClassLoader extends BasicClassLoader {
......@@ -111,11 +113,11 @@ export default class DefaultClassLoader extends BasicClassLoader {
'token.user' : UserToken,
'token.request': RequestToken,
'encryption.none' : NoEncryption,
'encryption.none' : () => { return new NoEncryption(this.getInstance('classes')); },
'encryption.csev1': () => { return new CSEv1Encryption(this.getInstance('classes')); },
'encryption.expv1': () => { return new ExportV1Encryption(this.getInstance('classes')); },
'keychain.csev1': CSEv1Keychain,
'keychain.csev1': (k, p) => { return new CSEv1Keychain(this.getInstance('classes'), k, p); },
'service.model' : () => { return new ModelService(this.getInstance('classes')); },
'service.password': () => { return new PasswordService(this.getInstance('client')); },
......@@ -146,6 +148,8 @@ export default class DefaultClassLoader extends BasicClassLoader {
'exception.encryption.unsupported': UnsupportedEncryptionTypeError,
'exception.encryption.object' : InvalidObjectTypeError,
'exception.encryption.enabled' : EncryptionNotEnabledError,
'exception.encryption.key.missing': MissingEncryptionKeyError,
'exception.encryption.text.length': InvalidEncryptedTextLength,
'exception.configuration' : ConfigurationError,
......
......@@ -4,11 +4,18 @@ import BooleanState from '../../State/BooleanState';
export default class CSEv1Keychain {
constructor(keychain = null, password = null) {
/**
*
* @param {BasicClassLoader} classLoader
* @param {String} keychain
* @param {String} password
*/
constructor(classLoader, keychain = null, password = null) {
this._keys = {};
this._current = null;
this._enabled = new BooleanState(false);
this._enabled = classLoader.getClass('state.boolean', false);
this._password = password;
this._classLoader = classLoader;
if(keychain !== null) {
sodium.ready.then(() => {
......@@ -47,8 +54,7 @@ export default class CSEv1Keychain {
return this._keys[id];
}
// TODO custom error here
throw new Error('Unknown CSE key id');
throw this._classLoader.getClass('exception.encryption.key.missing', id);
}
/**
......@@ -154,7 +160,8 @@ export default class CSEv1Keychain {
* @returns {Uint8Array}
*/
_decrypt(encrypted, key) {
if(encrypted.length < sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES) throw new Error('Invalid encrypted text length');
let expectedLength = sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES;
if(encrypted.length < expectedLength) throw this._classLoader.getClass('exception.encryption.text.length', encrypted.length, expectedLength);
let nonce = encrypted.slice(0, sodium.crypto_secretbox_NONCEBYTES),
ciphertext = encrypted.slice(sodium.crypto_secretbox_NONCEBYTES);
......
export default class InvalidEncryptedTextLength extends Error {
/**
* @returns {String}
*/
get name() {
return 'InvalidEncryptedTextLength';
}
/**
* @param {Number} length
* @param {Number} expectedLength
*/
constructor(length, expectedLength) {
super(`Invalid encrypted text length. Expected ${expectedLength}, got ${length} instead.`);
}
}
\ No newline at end of file
export default class MissingEncryptionKeyError extends Error {
/**
* @returns {String}
*/
get name() {
return 'MissingEncryptionKeyError';
}
/**
* @param {String} id
*/
constructor(id) {
super(`Requested encryption key ${id} not found`);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment