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-04-10 09:33:14 +00:00
|
|
|
/**
|
|
|
|
* A connection port will be opened for each communication between gpgmejs and
|
|
|
|
* gnupg. It should be alive as long as there are additional messages to be
|
|
|
|
* expected.
|
|
|
|
*/
|
2018-04-18 14:38:06 +00:00
|
|
|
import { permittedOperations } from './permittedOperations'
|
2018-04-25 13:59:36 +00:00
|
|
|
import { gpgme_error } from "./Errors"
|
2018-05-25 09:53:24 +00:00
|
|
|
import { GPGME_Message, createMessage } from "./Message";
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-04-23 15:18:46 +00:00
|
|
|
/**
|
|
|
|
* A Connection handles the nativeMessaging interaction.
|
|
|
|
*/
|
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-04-23 15:18:46 +00:00
|
|
|
this.connect();
|
2018-04-24 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-25 09:53:24 +00:00
|
|
|
* Retrieves the information about the backend.
|
|
|
|
* @param {Boolean} details (optional) If set to false, the promise will
|
|
|
|
* just return a connection status
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
* {String} The property 'gpgme': Version number of gpgme
|
|
|
|
* {Array<Object>} 'info' Further information about the backends.
|
|
|
|
* Example:
|
|
|
|
* "protocol": "OpenPGP",
|
|
|
|
* "fname": "/usr/bin/gpg",
|
|
|
|
* "version": "2.2.6",
|
|
|
|
* "req_version": "1.4.0",
|
|
|
|
* "homedir": "default"
|
2018-04-24 16:44:30 +00:00
|
|
|
*/
|
2018-05-25 09:53:24 +00:00
|
|
|
checkConnection(details = true){
|
|
|
|
if (details === true) {
|
|
|
|
return this.post(createMessage('version'));
|
|
|
|
} else {
|
|
|
|
let me = this;
|
|
|
|
return new Promise(function(resolve,reject) {
|
|
|
|
Promise.race([
|
|
|
|
me.post(createMessage('version')),
|
|
|
|
new Promise(function(resolve, reject){
|
|
|
|
setTimeout(function(){
|
|
|
|
reject(gpgme_error('CONN_TIMEOUT'));
|
|
|
|
}, 500);
|
|
|
|
})
|
|
|
|
]).then(function(result){
|
|
|
|
resolve(true);
|
|
|
|
}, function(reject){
|
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
2018-04-10 09:33:14 +00:00
|
|
|
|
|
|
|
/**
|
2018-04-23 15:18:46 +00:00
|
|
|
* Immediately closes the open port.
|
2018-04-10 09:33:14 +00:00
|
|
|
*/
|
2018-04-18 14:38:06 +00:00
|
|
|
disconnect() {
|
|
|
|
if (this._connection){
|
|
|
|
this._connection.disconnect();
|
2018-05-25 09:53:24 +00:00
|
|
|
this._connection = null;
|
2018-04-10 09:33:14 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-04-23 15:18:46 +00:00
|
|
|
/**
|
|
|
|
* Opens a nativeMessaging port.
|
|
|
|
*/
|
|
|
|
connect(){
|
2018-05-25 09:53:24 +00:00
|
|
|
if (!this._connection){
|
2018-04-25 08:54:24 +00:00
|
|
|
this._connection = chrome.runtime.connectNative('gpgmejson');
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:38:06 +00:00
|
|
|
/**
|
|
|
|
* Sends a message and resolves with the answer.
|
|
|
|
* @param {GPGME_Message} message
|
|
|
|
* @returns {Promise<Object>} the gnupg answer, or rejection with error
|
2018-04-23 15:18:46 +00:00
|
|
|
* information.
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
|
|
|
post(message){
|
2018-05-25 09:53:24 +00:00
|
|
|
if (!this._connection) {
|
|
|
|
|
2018-04-24 16:44:30 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
if (!message || !message instanceof GPGME_Message){
|
2018-05-28 14:52:50 +00:00
|
|
|
this.disconnect();
|
2018-06-06 09:57:41 +00:00
|
|
|
return Promise.reject(gpgme_error('PARAM_WRONG', 'Connection.post'));
|
2018-04-23 15:18:46 +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-04-18 14:38:06 +00:00
|
|
|
let me = this;
|
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-04-18 14:38:06 +00:00
|
|
|
me._connection.onMessage.removeListener(listener)
|
2018-05-28 14:52:50 +00:00
|
|
|
me._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 if (msg.type === "error"){
|
2018-05-07 16:27:25 +00:00
|
|
|
me._connection.onMessage.removeListener(listener);
|
2018-05-28 14:52:50 +00:00
|
|
|
me._connection.disconnect();
|
2018-04-27 18:03:09 +00:00
|
|
|
reject(gpgme_error('GNUPG_ERROR', msg.msg));
|
2018-04-18 14:38:06 +00:00
|
|
|
} else {
|
2018-04-25 08:54:24 +00:00
|
|
|
let answer_result = answer.add(msg);
|
|
|
|
if (answer_result !== true){
|
2018-05-07 16:27:25 +00:00
|
|
|
me._connection.onMessage.removeListener(listener);
|
2018-05-28 14:52:50 +00:00
|
|
|
me._connection.disconnect();
|
2018-04-25 08:54:24 +00:00
|
|
|
reject(answer_result);
|
2018-05-28 14:52:50 +00:00
|
|
|
} else if (msg.more === true){
|
2018-04-18 14:38:06 +00:00
|
|
|
me._connection.postMessage({'op': 'getmore'});
|
|
|
|
} else {
|
|
|
|
me._connection.onMessage.removeListener(listener)
|
2018-05-28 14:52:50 +00:00
|
|
|
me._connection.disconnect();
|
2018-04-18 14:38:06 +00:00
|
|
|
resolve(answer.message);
|
|
|
|
}
|
2018-04-10 09:33:14 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
};
|
|
|
|
me._connection.onMessage.addListener(listener);
|
2018-04-25 09:32:21 +00:00
|
|
|
if (permittedOperations[message.operation].pinentry){
|
|
|
|
return me._connection.postMessage(message.message);
|
|
|
|
} else {
|
2018-04-25 13:59:36 +00:00
|
|
|
return Promise.race([
|
|
|
|
me._connection.postMessage(message.message),
|
|
|
|
function(resolve, reject){
|
|
|
|
setTimeout(function(){
|
2018-05-28 14:52:50 +00:00
|
|
|
me._connection.disconnect();
|
2018-04-25 13:59:36 +00:00
|
|
|
reject(gpgme_error('CONN_TIMEOUT'));
|
|
|
|
}, 5000);
|
|
|
|
}]).then(function(result){
|
2018-05-28 14:52:50 +00:00
|
|
|
return result;
|
2018-05-07 16:27:25 +00:00
|
|
|
}, function(reject){
|
|
|
|
if(!reject instanceof Error) {
|
2018-05-28 14:52:50 +00:00
|
|
|
me._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-04-18 14:38:06 +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.
|
|
|
|
* @param {String} operation The operation, to look up validity of returning messages
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
|
|
|
class Answer{
|
2018-04-10 09:33:14 +00:00
|
|
|
|
2018-05-22 12:24:16 +00:00
|
|
|
constructor(message){
|
|
|
|
this.operation = message.operation;
|
|
|
|
this.expected = message.expected;
|
2018-04-10 09:33:14 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
|
|
|
|
/**
|
2018-04-23 15:18:46 +00:00
|
|
|
* Add the information to the answer
|
2018-04-18 14:38:06 +00:00
|
|
|
* @param {Object} msg The message as received with nativeMessaging
|
2018-04-25 13:59:36 +00:00
|
|
|
* returns true if successfull, gpgme_error otherwise
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
|
|
|
add(msg){
|
|
|
|
if (this._response === undefined){
|
|
|
|
this._response = {};
|
|
|
|
}
|
|
|
|
let messageKeys = Object.keys(msg);
|
|
|
|
let poa = permittedOperations[this.operation].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) {
|
|
|
|
case 'type':
|
|
|
|
if ( msg.type !== 'error' && poa.type.indexOf(msg.type) < 0){
|
2018-04-25 13:59:36 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'more':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//data should be concatenated
|
|
|
|
if (poa.data.indexOf(key) >= 0){
|
|
|
|
if (!this._response.hasOwnProperty(key)){
|
|
|
|
this._response[key] = '';
|
|
|
|
}
|
2018-05-09 17:40:57 +00:00
|
|
|
this._response[key] += msg[key];
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
//params should not change through the message
|
|
|
|
else if (poa.params.indexOf(key) >= 0){
|
|
|
|
if (!this._response.hasOwnProperty(key)){
|
|
|
|
this._response[key] = msg[key];
|
|
|
|
}
|
|
|
|
else if (this._response[key] !== msg[key]){
|
2018-04-25 13:59:36 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER',msg[key]);
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//infos may be json objects etc. Not yet defined.
|
|
|
|
// Pushing them into arrays for now
|
|
|
|
else if (poa.infos.indexOf(key) >= 0){
|
|
|
|
if (!this._response.hasOwnProperty(key)){
|
|
|
|
this._response[key] = [];
|
|
|
|
}
|
2018-06-06 09:57:41 +00:00
|
|
|
|
2018-05-25 17:02:18 +00:00
|
|
|
if (Array.isArray(msg[key])) {
|
|
|
|
for (let i=0; i< msg[key].length; i++) {
|
|
|
|
this._response[key].push(msg[key][i]);
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-06 09:57:41 +00:00
|
|
|
this._response[key].push(msg[key]);
|
2018-05-25 17:02:18 +00:00
|
|
|
}
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-05-07 16:27:25 +00:00
|
|
|
return gpgme_error('CONN_UNEXPECTED_ANSWER');
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 08:54:24 +00:00
|
|
|
return true;
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-22 12:24:16 +00:00
|
|
|
* @returns {Object} the assembled message, original data assumed to be
|
|
|
|
* (javascript-) strings
|
2018-04-18 14:38:06 +00:00
|
|
|
*/
|
|
|
|
get message(){
|
2018-05-09 17:40:57 +00:00
|
|
|
let keys = Object.keys(this._response);
|
2018-05-22 12:24:16 +00:00
|
|
|
let msg = {};
|
2018-05-09 17:40:57 +00:00
|
|
|
let poa = permittedOperations[this.operation].answer;
|
|
|
|
for (let i=0; i < keys.length; i++) {
|
2018-05-22 12:24:16 +00:00
|
|
|
if (poa.data.indexOf(keys[i]) >= 0
|
|
|
|
&& this._response.base64 === true
|
|
|
|
) {
|
|
|
|
msg[keys[i]] = atob(this._response[keys[i]]);
|
|
|
|
if (this.expected === 'base64'){
|
|
|
|
msg[keys[i]] = this._response[keys[i]];
|
|
|
|
} else {
|
|
|
|
msg[keys[i]] = decodeURIComponent(
|
|
|
|
atob(this._response[keys[i]]).split('').map(function(c) {
|
2018-05-09 17:40:57 +00:00
|
|
|
return '%' +
|
2018-05-22 12:24:16 +00:00
|
|
|
('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
2018-05-09 17:40:57 +00:00
|
|
|
}).join(''));
|
|
|
|
}
|
2018-05-22 12:24:16 +00:00
|
|
|
} else {
|
|
|
|
msg[keys[i]] = this._response[keys[i]];
|
2018-05-09 17:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-22 12:24:16 +00:00
|
|
|
return msg;
|
2018-04-18 14:38:06 +00:00
|
|
|
}
|
|
|
|
}
|