diff options
author | Marius David Wieschollek <passwords.public@mdns.eu> | 2020-12-23 13:43:06 +0300 |
---|---|---|
committer | Marius David Wieschollek <passwords.public@mdns.eu> | 2020-12-23 13:43:06 +0300 |
commit | 72e4bd842e755ef565351aa1f5d5a8475c931ec5 (patch) | |
tree | eb08b64258bcc629981ca15288e160c7cf5a2d6b | |
parent | d5b244b2aeec109273fdffe13f87c85e49709365 (diff) |
Add new exceptions for keychain uses
Signed-off-by: Marius David Wieschollek <passwords.public@mdns.eu>
-rw-r--r-- | src/ClassLoader/DefaultClassLoader.js | 8 | ||||
-rw-r--r-- | src/Encryption/Keychain/CSEv1Keychain.js | 17 | ||||
-rw-r--r-- | src/Exception/Encryption/InvalidEncryptedTextLength.js | 17 | ||||
-rw-r--r-- | src/Exception/Encryption/MissingEncryptionKeyError.js | 16 |
4 files changed, 51 insertions, 7 deletions
diff --git a/src/ClassLoader/DefaultClassLoader.js b/src/ClassLoader/DefaultClassLoader.js index bb87a07..5cc8bf0 100644 --- a/src/ClassLoader/DefaultClassLoader.js +++ b/src/ClassLoader/DefaultClassLoader.js @@ -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, diff --git a/src/Encryption/Keychain/CSEv1Keychain.js b/src/Encryption/Keychain/CSEv1Keychain.js index 22e6bab..b80ad63 100644 --- a/src/Encryption/Keychain/CSEv1Keychain.js +++ b/src/Encryption/Keychain/CSEv1Keychain.js @@ -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); diff --git a/src/Exception/Encryption/InvalidEncryptedTextLength.js b/src/Exception/Encryption/InvalidEncryptedTextLength.js new file mode 100644 index 0000000..8760a11 --- /dev/null +++ b/src/Exception/Encryption/InvalidEncryptedTextLength.js @@ -0,0 +1,17 @@ +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 diff --git a/src/Exception/Encryption/MissingEncryptionKeyError.js b/src/Exception/Encryption/MissingEncryptionKeyError.js new file mode 100644 index 0000000..d753b06 --- /dev/null +++ b/src/Exception/Encryption/MissingEncryptionKeyError.js @@ -0,0 +1,16 @@ +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 |