Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CSEv1Encryption.js « Encryption « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10e29a9f444d927a3871edea666fa44563b76c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sodium from 'libsodium-wrappers';

export default class CSEv1Encryption {

    /**
     * @param {BasicClassLoader} classLoader
     */
    constructor(classLoader) {
        this.fields = {
            password: ['url', 'label', 'notes', 'password', 'username', 'customFields'],
            folder  : ['label'],
            tag     : ['label', 'color']
        };
        this._enabled = classLoader.getClass('state.boolean', false);
        this._ready = classLoader.getClass('state.boolean', false);
        this._keychain = null;
        this._classLoader = classLoader;

        sodium.ready.then(() => {this._ready.set(true);});
    }

    /**
     *
     * @returns {Promise<Boolean>}
     */
    async ready() {
        await this._ready.awaitTrue() && await this._enabled.awaitTrue();
        return true;
    }

    /**
     * @returns {Boolean}
     */
    enabled() {
        return this._ready.get() && this._enabled.get();
    }

    /**
     * Encrypts an object
     *
     * @param {Object} object
     * @param {String} type
     * @returns {Object}
     */
    async encrypt(object, type) {
        if(!this.fields.hasOwnProperty(type)) throw this._classLoader.getClass('exception.encryption.object');
        if(!this.enabled()) throw this._classLoader.getClass('exception.encryption.enabled');

        let fields = this.fields[type],
            key    = this._keychain.getCurrentKey();

        for(let field of fields) {
            let data  = object[field];

            if(data === null || data === undefined || data.length === 0) continue;
            object[field] = this._encryptString(data, key);
        }

        object.cseType = 'CSEv1r1';
        object.cseKey = this._keychain.getCurrentKeyId();

        return object;
    }

    /**
     * Decrypts an object
     *
     * @param {Object} object
     * @param {String} type
     * @returns {Object}
     */
    async decrypt(object, type) {
        if(!this.fields.hasOwnProperty(type)) throw this._classLoader.getClass('exception.encryption.object');
        if(object.cseType !== 'CSEv1r1') throw this._classLoader.getClass('exception.encryption.unsupported', object, 'CSEv1r1');
        if(!this.enabled()) throw this._classLoader.getClass('exception.encryption.enabled');

        let fields = this.fields[type],
            key    = this._keychain.getKey(object.cseKey);

        for(let field of fields) {
            let data  = object[field];

            if(data === null || data.length === 0) continue;
            object[field] = this._decryptString(data, key);
        }

        return object;
    }

    /**
     * Encrypt the message with the given key and return a hex encoded string
     *
     * @param {String} message
     * @param {Uint8Array} key
     * @returns {String}
     * @private
     */
    _encryptString(message, key) {
        return sodium.to_hex(this._encrypt(message, key));
    }

    /**
     * Decrypt the hex or base64 encoded message with the given key
     *
     * @param {String} encodedString
     * @param {Uint8Array} key
     * @returns {String}
     * @private
     */
    _decryptString(encodedString, key) {
        try {
            let encryptedString = sodium.from_hex(encodedString);
            return sodium.to_string(this._decrypt(encryptedString, key));
        } catch(e) {
            let encryptedString = sodium.from_base64(encodedString);
            return sodium.to_string(this._decrypt(encryptedString, key));
        }
    }

    /**
     * Encrypt the message with the given key
     *
     * @param {Uint8Array} message
     * @param {Uint8Array} key
     * @returns {Uint8Array}
     * @private
     */
    _encrypt(message, key) {
        let nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);

        return new Uint8Array([...nonce, ...sodium.crypto_secretbox_easy(message, nonce, key)]);
    }

    /**
     * Decrypt the message with the given key
     *
     * @param {Uint8Array} encrypted
     * @param {Uint8Array} key
     * @returns {Uint8Array}
     * @private
     */
    _decrypt(encrypted, key) {
        if(encrypted.length < sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES) throw new Error('Invalid encrypted text length');

        let nonce      = encrypted.slice(0, sodium.crypto_secretbox_NONCEBYTES),
            ciphertext = encrypted.slice(sodium.crypto_secretbox_NONCEBYTES);

        return sodium.crypto_secretbox_open_easy(ciphertext, nonce, key);
    }

    /**
     * Decrypt and activate the keychain
     *
     * @param {CSEv1Keychain} keychain
     * @private
     */
    setKeychain(keychain) {
        this._keychain = keychain;

        keychain
            .ready()
            .then(() => {
                this._enabled.set(true);
            });
    }

    /**
     * Remove the current keychain
     */
    unsetKeychain() {
        this._enabled.set(false);
        this._keychain = null;
    }
}