2018-04-18 14:38:06 +00:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1+
|
2018-06-06 11:05:53 +00:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Maximilian Krambach <mkrambach@intevation.de>
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-06 11:05:53 +00:00
|
|
|
/* global chrome */
|
|
|
|
|
|
|
|
import { permittedOperations } from './permittedOperations';
|
|
|
|
import { gpgme_error } from './Errors';
|
|
|
|
import { GPGME_Message, createMessage } from './Message';
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-04-23 15:18:46 +00:00
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* 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
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
2018-04-18 14:38:06 +00:00
|
|
|
export class Connection{
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-04-18 14:38:06 +00:00
|
|
|
constructor(){
|
2018-07-27 18:36:21 +00:00
|
|
|
let _connection = chrome.runtime.connectNative('gpgmejson');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Immediately closes an open port.
|
|
|
|
*/
|
|
|
|
this.disconnect = function () {
|
|
|
|
if (_connection){
|
|
|
|
_connection.disconnect();
|
|
|
|
_connection = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-24 16:44:30 +00:00
|
|
|
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* @typedef {Object} backEndDetails
|
|
|
|
* @property {String} gpgme Version number of gpgme
|
|
|
|
* @property {Array<Object>} info Further information about the backend
|
|
|
|
* and the used applications (Example:
|
|
|
|
* {
|
2018-05-25 09:53:24 +00:00
|
|
|
* "protocol": "OpenPGP",
|
|
|
|
* "fname": "/usr/bin/gpg",
|
|
|
|
* "version": "2.2.6",
|
|
|
|
* "req_version": "1.4.0",
|
|
|
|
* "homedir": "default"
|
2018-07-10 12:32:26 +00:00
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the information about the backend.
|
|
|
|
* @param {Boolean} details (optional) If set to false, the promise will
|
|
|
|
* just return if a connection was successful.
|
|
|
|
* @returns {Promise<backEndDetails>|Promise<Boolean>} Details from the
|
|
|
|
* backend
|
|
|
|
* @async
|
2018-04-24 16:44:30 +00:00
|
|
|
*/
|
2018-07-27 18:36:21 +00:00
|
|
|
this.checkConnection = function(details = true){
|
|
|
|
const msg = createMessage('version');
|
2018-05-25 09:53:24 +00:00
|
|
|
if (details === true) {
|
2018-07-27 18:36:21 +00:00
|
|
|
return this.post(msg);
|
2018-05-25 09:53:24 +00:00
|
|
|
} else {
|
|
|
|
let me = this;
|
2018-06-06 11:05:53 +00:00
|
|
|
return new Promise(function(resolve) {
|
2018-05-25 09:53:24 +00:00
|
|
|
Promise.race([
|
2018-07-27 18:36:21 +00:00
|
|
|
me.post(msg),
|
2018-05-25 09:53:24 +00:00
|
|
|
new Promise(function(resolve, reject){
|
|
|
|
setTimeout(function(){
|
|
|
|
reject(gpgme_error('CONN_TIMEOUT'));
|
|
|
|
}, 500);
|
|
|
|
})
|
2018-06-06 11:05:53 +00:00
|
|
|
]).then(function(){ // success
|
|
|
|
resolve(true);
|
|
|
|
}, function(){ // failure
|
2018-05-25 09:53:24 +00:00
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-07-27 18:36:21 +00:00
|
|
|
};
|
2018-04-23 15:18:46 +00:00
|
|
|
|
2018-04-18 14:38:06 +00:00
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Sends a {@link GPGME_Message} via tghe nativeMessaging port. It resolves
|
|
|
|
* with the completed answer after all parts have been received and
|
|
|
|
* reassembled, or rejects with an {@link GPGME_Error}.
|
|
|
|
*
|
2018-04-18 14:38:06 +00:00
|
|
|
* @param {GPGME_Message} message
|
2018-07-10 12:32:26 +00:00
|
|
|
* @returns {Promise<Object>} The collected answer
|
|
|
|
* @async
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
2018-07-27 18:36:21 +00:00
|
|
|
this.post = function (message){
|
2018-06-06 11:05:53 +00:00
|
|
|
if (!message || !(message instanceof GPGME_Message)){
|
2018-05-28 14:52:50 +00:00
|
|
|
this.disconnect();
|
2018-06-06 11:05:53 +00:00
|
|
|
return Promise.reject(gpgme_error(
|
|
|
|
'PARAM_WRONG', 'Connection.post'));
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
2018-07-27 18:36:21 +00:00
|
|
|
if (message.isComplete() !== true){
|
2018-05-28 14:52:50 +00:00
|
|
|
this.disconnect();
|
2018-04-25 13:59:36 +00:00
|
|
|
return Promise.reject(gpgme_error('MSG_INCOMPLETE'));
|
2018-04-10 09:33:14 +00:00
|
|
|
}
|
2018-06-08 15:54:58 +00:00
|
|
|
let chunksize = message.chunksize;
|
2018-04-10 09:33:14 +00:00
|
|
|
return new Promise(function(resolve, reject){
|
2018-05-22 12:24:16 +00:00
|
|
|
let answer = new Answer(message);
|
2018-04-18 14:38:06 +00:00
|
|
|
let listener = function(msg) {
|
2018-04-10 09:33:14 +00:00
|
|
|
if (!msg){
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.onMessage.removeListener(listener);
|
|
|
|
_connection.disconnect();
|
2018-04-25 13:59:36 +00:00
|
|
|
reject(gpgme_error('CONN_EMPTY_GPG_ANSWER'));
|
2018-04-18 14:38:06 +00:00
|
|
|
} else {
|
2018-06-08 15:54:58 +00:00
|
|
|
let answer_result = answer.collect(msg);
|
2018-04-25 08:54:24 +00:00
|
|
|
if (answer_result !== true){
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.onMessage.removeListener(listener);
|
|
|
|
_connection.disconnect();
|
2018-04-25 08:54:24 +00:00
|
|
|
reject(answer_result);
|
2018-04-18 14:38:06 +00:00
|
|
|
} else {
|
2018-06-08 15:54:58 +00:00
|
|
|
if (msg.more === true){
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.postMessage({
|
2018-06-08 15:54:58 +00:00
|
|
|
'op': 'getmore',
|
|
|
|
'chunksize': chunksize
|
|
|
|
});
|
|
|
|
} else {
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.onMessage.removeListener(listener);
|
|
|
|
_connection.disconnect();
|
|
|
|
const message = answer.getMessage();
|
|
|
|
if (message instanceof Error){
|
|
|
|
reject(message);
|
2018-06-08 15:54:58 +00:00
|
|
|
} else {
|
2018-07-27 18:36:21 +00:00
|
|
|
resolve(message);
|
2018-06-08 15:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
2018-04-10 09:33:14 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
};
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.onMessage.addListener(listener);
|
2018-04-25 09:32:21 +00:00
|
|
|
if (permittedOperations[message.operation].pinentry){
|
2018-07-27 18:36:21 +00:00
|
|
|
return _connection.postMessage(message.message);
|
2018-04-25 09:32:21 +00:00
|
|
|
} else {
|
2018-04-25 13:59:36 +00:00
|
|
|
return Promise.race([
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.postMessage(message.message),
|
2018-04-25 13:59:36 +00:00
|
|
|
function(resolve, reject){
|
|
|
|
setTimeout(function(){
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.disconnect();
|
2018-04-25 13:59:36 +00:00
|
|
|
reject(gpgme_error('CONN_TIMEOUT'));
|
|
|
|
}, 5000);
|
|
|
|
}]).then(function(result){
|
2018-06-06 11:05:53 +00:00
|
|
|
return result;
|
2018-05-07 16:27:25 +00:00
|
|
|
}, function(reject){
|
2018-06-06 11:05:53 +00:00
|
|
|
if(!(reject instanceof Error)) {
|
2018-07-27 18:36:21 +00:00
|
|
|
_connection.disconnect();
|
2018-05-07 16:27:25 +00:00
|
|
|
return gpgme_error('GNUPG_ERROR', reject);
|
|
|
|
} else {
|
|
|
|
return reject;
|
|
|
|
}
|
2018-04-25 13:59:36 +00:00
|
|
|
});
|
2018-04-25 09:32:21 +00:00
|
|
|
}
|
2018-04-10 09:33:14 +00:00
|
|
|
});
|
2018-07-27 18:36:21 +00:00
|
|
|
};
|
|
|
|
}
|
2018-06-06 11:05:53 +00:00
|
|
|
}
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-04-18 14:38:06 +00:00
|
|
|
/**
|
|
|
|
* A class for answer objects, checking and processing the return messages of
|
2018-04-23 15:18:46 +00:00
|
|
|
* the nativeMessaging communication.
|
2018-07-10 12:32:26 +00:00
|
|
|
* @protected
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
|
|
|
class Answer{
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* @param {GPGME_Message} message
|
|
|
|
*/
|
2018-05-22 12:24:16 +00:00
|
|
|
constructor(message){
|
2018-07-27 18:36:21 +00:00
|
|
|
const operation = message.operation;
|
|
|
|
const expect = message.expect;
|
|
|
|
let response_b64 = null;
|
2018-04-18 14:38:06 +00:00
|
|
|
|
2018-07-27 18:36:21 +00:00
|
|
|
this.getOperation = function(){
|
|
|
|
return operation;
|
|
|
|
};
|
|
|
|
this.getExpect = function(){
|
|
|
|
return expect;
|
|
|
|
};
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Adds incoming base64 encoded data to the existing response
|
|
|
|
* @param {*} msg base64 encoded data.
|
|
|
|
* @returns {Boolean}
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2018-07-27 18:36:21 +00:00
|
|
|
this.collect = function (msg){
|
2018-06-08 15:54:58 +00:00
|
|
|
if (typeof(msg) !== 'object' || !msg.hasOwnProperty('response')) {
|
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
|
|
|
}
|
2018-07-27 18:36:21 +00:00
|
|
|
if (response_b64 === null){
|
|
|
|
response_b64 = msg.response;
|
2018-06-08 15:54:58 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-07-27 18:36:21 +00:00
|
|
|
response_b64 += msg.response;
|
2018-06-08 15:54:58 +00:00
|
|
|
return true;
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
2018-07-27 18:36:21 +00:00
|
|
|
};
|
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* Returns the base64 encoded answer data with the content verified against
|
|
|
|
* {@link permittedOperations}.
|
|
|
|
*/
|
2018-07-27 18:36:21 +00:00
|
|
|
this.getMessage = function (){
|
|
|
|
if (response_b64 === undefined){
|
2018-06-08 15:54:58 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
|
|
|
}
|
2018-07-27 18:36:21 +00:00
|
|
|
let _decodedResponse = JSON.parse(atob(response_b64));
|
2018-06-08 15:54:58 +00:00
|
|
|
let _response = {};
|
|
|
|
let messageKeys = Object.keys(_decodedResponse);
|
2018-07-27 18:36:21 +00:00
|
|
|
let poa = permittedOperations[this.getOperation()].answer;
|
2018-04-25 08:54:24 +00:00
|
|
|
if (messageKeys.length === 0){
|
2018-04-25 13:59:36 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
2018-04-25 08:54:24 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
for (let i= 0; i < messageKeys.length; i++){
|
|
|
|
let key = messageKeys[i];
|
|
|
|
switch (key) {
|
2018-06-06 11:05:53 +00:00
|
|
|
case 'type':
|
2018-06-08 15:54:58 +00:00
|
|
|
if (_decodedResponse.type === 'error'){
|
2018-07-24 12:50:54 +00:00
|
|
|
return (gpgme_error('GNUPG_ERROR',
|
|
|
|
decodeURIComponent(escape(_decodedResponse.msg))));
|
2018-06-08 15:54:58 +00:00
|
|
|
} else if (poa.type.indexOf(_decodedResponse.type) < 0){
|
2018-06-06 11:05:53 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
|
|
|
}
|
|
|
|
break;
|
2018-06-08 15:54:58 +00:00
|
|
|
case 'base64':
|
2018-06-06 11:05:53 +00:00
|
|
|
break;
|
2018-06-08 15:54:58 +00:00
|
|
|
case 'msg':
|
|
|
|
if (_decodedResponse.type === 'error'){
|
|
|
|
return (gpgme_error('GNUPG_ERROR', _decodedResponse.msg));
|
2018-06-06 11:05:53 +00:00
|
|
|
}
|
2018-06-08 15:54:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!poa.data.hasOwnProperty(key)){
|
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
2018-06-06 11:05:53 +00:00
|
|
|
}
|
2018-06-08 15:54:58 +00:00
|
|
|
if( typeof(_decodedResponse[key]) !== poa.data[key] ){
|
2018-06-06 11:05:53 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
|
|
|
}
|
2018-06-08 15:54:58 +00:00
|
|
|
if (_decodedResponse.base64 === true
|
|
|
|
&& poa.data[key] === 'string'
|
2018-07-27 18:36:21 +00:00
|
|
|
&& this.getExpect() === undefined
|
2018-06-08 15:54:58 +00:00
|
|
|
){
|
|
|
|
_response[key] = decodeURIComponent(
|
|
|
|
atob(_decodedResponse[key]).split('').map(
|
2018-06-06 11:05:53 +00:00
|
|
|
function(c) {
|
|
|
|
return '%' +
|
2018-06-08 15:54:58 +00:00
|
|
|
('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
2018-06-06 11:05:53 +00:00
|
|
|
}).join(''));
|
2018-06-08 15:54:58 +00:00
|
|
|
} else {
|
|
|
|
_response[key] = _decodedResponse[key];
|
2018-05-09 17:40:57 +00:00
|
|
|
}
|
2018-06-08 15:54:58 +00:00
|
|
|
break;
|
2018-05-09 17:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 15:54:58 +00:00
|
|
|
return _response;
|
2018-07-27 18:36:21 +00:00
|
|
|
};
|
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|