summaryrefslogtreecommitdiff
path: root/lang/js/src/Connection.js
blob: 923698a443095c7989ad8208d0f6f953031d16cc (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* gpgme.js - Javascript integration for gpgme
 * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1+
 *
 * Author(s):
 *     Maximilian Krambach <mkrambach@intevation.de>
 */

/* global chrome */

import { permittedOperations } from './permittedOperations';
import { gpgme_error } from './Errors';
import { GPGME_Message, createMessage } from './Message';
import { decode, atobArray, Utf8ArrayToStr } from './Helpers';

/**
 * A Connection handles the nativeMessaging interaction via a port. As the
 * protocol only allows up to 1MB of message sent from the nativeApp to the
 * browser, the connection will stay open until all parts of a communication
 * are finished. For a new request, a new port will open, to avoid mixing
 * contexts.
 * @class
 * @private
 */
export class Connection{

    constructor (){
        this._connectionError = null;
        this._connection = chrome.runtime.connectNative('gpgmejson');
        this._connection.onDisconnect.addListener(() => {
            if (chrome.runtime.lastError) {
                this._connectionError = chrome.runtime.lastError.message;
            } else {
                this._connectionError = 'Disconnected without error message';
            }
        });
    }

    /**
     * Immediately closes an open port.
     */
    disconnect () {
        if (this._connection){
            this._connection.disconnect();
            this._connection = null;
            this._connectionError = 'Disconnect requested by gpgmejs';
        }
    }

    /**
     * Checks if the connection terminated with an error state
     */
    get isDisconnected (){
        return this._connectionError !== null;
    }

    /**
    * @typedef {Object} backEndDetails
    * @property {String} gpgme Version number of gpgme
    * @property {Array<Object>} info Further information about the backend
    * and the used applications (Example:
    * <pre>
    * {
    *          "protocol":     "OpenPGP",
    *          "fname":        "/usr/bin/gpg",
    *          "version":      "2.2.6",
    *          "req_version":  "1.4.0",
    *          "homedir":      "default"
    * }
    * </pre>
    */

    /**
     * Retrieves the information about the backend.
     * @param {Boolean} details (optional) If set to false, the promise will
     *  just return if a connection was successful.
     * @param {Number} timeout (optional)
     * @returns {Promise<backEndDetails>|Promise<Boolean>} Details from the
     * backend
     * @async
     */
    checkConnection (details = true, timeout = 1000){
        if (typeof timeout !== 'number' && timeout <= 0) {
            timeout = 1000;
        }
        const msg = createMessage('version');
        if (details === true) {
            return this.post(msg);
        } else {
            let me = this;
            return new Promise(function (resolve) {
                Promise.race([
                    me.post(msg),
                    new Promise(function (resolve, reject){
                        setTimeout(function (){
                            reject(gpgme_error('CONN_TIMEOUT'));
                        }, timeout);
                    })
                ]).then(function (){ // success
                    resolve(true);
                }, function (){ // failure
                    resolve(false);
                });
            });
        }
    }

    /**
     * Sends a {@link GPGME_Message} via the nativeMessaging port. It
     * resolves with the completed answer after all parts have been
     * received and reassembled, or rejects with an {@link GPGME_Error}.
     *
     * @param {GPGME_Message} message
     * @returns {Promise<*>} The collected answer, depending on the messages'
     * operation
     * @private
     * @async
     */
    post (message){
        if (!message || !(message instanceof GPGME_Message)){
            this.disconnect();
            return Promise.reject(gpgme_error(
                'PARAM_WRONG', 'Connection.post'));
        }
        if (message.isComplete() !== true){
            this.disconnect();
            return Promise.reject(gpgme_error('MSG_INCOMPLETE'));
        }
        if (this.isDisconnected) {
            if ( this.isNativeHostUnknown === true) {
                return Promise.reject(gpgme_error('CONN_NO_CONFIG'));
            } else {
                return Promise.reject(gpgme_error(
                    'CONN_NO_CONNECT', this._connectionError));
            }
        }
        let chunksize = message.chunksize;
        const me = this;
        const nativeCommunication = new Promise(function (resolve, reject){
            let answer = new Answer(message);
            let listener = function (msg) {
                if (!msg){
                    me._connection.onMessage.removeListener(listener);
                    me._connection.disconnect();
                    reject(gpgme_error('CONN_EMPTY_GPG_ANSWER'));
                } else {
                    let answer_result = answer.collect(msg);
                    if (answer_result !== true){
                        me._connection.onMessage.removeListener(listener);
                        me._connection.disconnect();
                        reject(answer_result);
                    } else {
                        if (msg.more === true){
                            me._connection.postMessage({
                                'op': 'getmore',
                                'chunksize': chunksize
                            });
                        } else {
                            me._connection.onMessage.removeListener(listener);
                            me._connection.disconnect();
                            const message = answer.getMessage();
                            if (message instanceof Error){
                                reject(message);
                            } else {
                                resolve(message);
                            }
                        }
                    }
                }
            };
            me._connection.onMessage.addListener(listener);
            me._connection.postMessage(message.message);
        });
        if (permittedOperations[message.operation].pinentry === true) {
            return nativeCommunication;
        } else {
            return Promise.race([
                nativeCommunication,
                new Promise(function (resolve, reject){
                    setTimeout(function (){
                        me._connection.disconnect();
                        reject(gpgme_error('CONN_TIMEOUT'));
                    }, 5000);
                })
            ]);
        }
    }
}


/**
 * A class for answer objects, checking and processing the return messages of
 * the nativeMessaging communication.
 * @private
 */
class Answer{

    /**
     * @param {GPGME_Message} message
     */
    constructor (message){
        this._operation = message.operation;
        this._expected = message.expected;
        this._response_b64 = null;
    }

    get operation (){
        return this._operation;
    }

    get expected (){
        return this._expected;
    }

    /**
     * Checks if an error matching browsers 'host not known' messages occurred
     */
    get isNativeHostUnknown () {
        return this._connectionError === 'Specified native messaging host not found.';
    }

    /**
     * Adds incoming base64 encoded data to the existing response
     * @param {*} msg base64 encoded data.
     * @returns {Boolean}
     *
     * @private
     */
    collect (msg){
        if (typeof (msg) !== 'object' || !msg.hasOwnProperty('response')) {
            return gpgme_error('CONN_UNEXPECTED_ANSWER');
        }
        if (!this._response_b64){
            this._response_b64 = msg.response;
            return true;
        } else {
            this._response_b64 += msg.response;
            return true;
        }
    }
    /**
     * Decodes and verifies the base64 encoded answer data. Verified against
     * {@link permittedOperations}.
     * @returns {Object} The readable gpnupg answer
     */
    getMessage (){
        if (this._response_b64 === null){
            return gpgme_error('CONN_UNEXPECTED_ANSWER');
        }
        let _decodedResponse = JSON.parse(atob(this._response_b64));
        let _response = {
            format: 'ascii'
        };
        let messageKeys = Object.keys(_decodedResponse);
        let poa = permittedOperations[this.operation].answer;
        if (messageKeys.length === 0){
            return gpgme_error('CONN_UNEXPECTED_ANSWER');
        }
        for (let i= 0; i < messageKeys.length; i++){
            let key = messageKeys[i];
            switch (key) {
            case 'type': {
                if (_decodedResponse.type === 'error'){
                    return (gpgme_error('GNUPG_ERROR',
                        decode(_decodedResponse.msg)));
                } else if (poa.type.indexOf(_decodedResponse.type) < 0){
                    return gpgme_error('CONN_UNEXPECTED_ANSWER');
                }
                break;
            }
            case 'base64': {
                break;
            }
            case 'msg': {
                if (_decodedResponse.type === 'error'){
                    return (gpgme_error('GNUPG_ERROR', _decodedResponse.msg));
                }
                break;
            }
            default: {
                let answerType = null;
                if (poa.payload && poa.payload.hasOwnProperty(key)){
                    answerType = 'p';
                } else if (poa.info && poa.info.hasOwnProperty(key)){
                    answerType = 'i';
                }
                if (answerType !== 'p' && answerType !== 'i'){
                    return gpgme_error('CONN_UNEXPECTED_ANSWER');
                }

                if (answerType === 'i') {
                    if ( typeof (_decodedResponse[key]) !== poa.info[key] ){
                        return gpgme_error('CONN_UNEXPECTED_ANSWER');
                    }
                    _response[key] = decode(_decodedResponse[key]);

                } else if (answerType === 'p') {
                    if (_decodedResponse.base64 === true
                        && poa.payload[key] === 'string'
                    ) {
                        if (this.expected === 'uint8'){
                            _response[key] = atobArray(_decodedResponse[key]);
                            _response.format = 'uint8';

                        } else if (this.expected === 'base64'){
                            _response[key] = _decodedResponse[key];
                            _response.format = 'base64';

                        } else { // no 'expected'
                            _response[key] = Utf8ArrayToStr(
                                atobArray(_decodedResponse[key]));
                            _response.format = 'string';
                        }
                    } else if (poa.payload[key] === 'string') {
                        _response[key] = _decodedResponse[key];
                    } else {
                        // fallthrough, should not be reached
                        // (payload is always string)
                        return gpgme_error('CONN_UNEXPECTED_ANSWER');
                    }
                }
                break;
            } }
        }
        return _response;
    }
}