diff --git a/lang/js/BrowserTestExtension/tests/encryptTest.js b/lang/js/BrowserTestExtension/tests/encryptTest.js
index 2cb4e58b..1114125e 100644
--- a/lang/js/BrowserTestExtension/tests/encryptTest.js
+++ b/lang/js/BrowserTestExtension/tests/encryptTest.js
@@ -1,155 +1,156 @@
/* 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 .
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach
*/
/* global describe, it, expect, Gpgmejs */
/* global inputvalues, fixedLengthString */
describe('Encryption', function () {
it('Successful encrypt', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
inputvalues.encrypt.good.data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
});
});
});
it('Successful encrypt 5 MB', function (done) {
let prm = Gpgmejs.init();
let data = fixedLengthString(5);
prm.then(function (context) {
context.encrypt(
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
});
});
}).timeout(10000);
it('Successful encrypt 20 MB', function (done) {
let prm = Gpgmejs.init();
let data = fixedLengthString(20);
prm.then(function (context) {
context.encrypt(
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
});
});
}).timeout(20000);
it('Successful encrypt 50 MB', function (done) {
let prm = Gpgmejs.init();
let data = fixedLengthString(50);
prm.then(function (context) {
context.encrypt(
data,
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty;
expect(answer.data).to.be.a('string');
expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE');
done();
});
});
}).timeout(20000);
it('Sending encryption without keys fails', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
inputvalues.encrypt.good.data,
null).then(function (answer) {
expect(answer).to.be.undefined;
}, function(error){
expect(error).to.be.an('Error');
expect(error.code).to.equal('MSG_INCOMPLETE');
done();
});
});
});
it('Sending encryption without data fails', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
null, inputvalues.encrypt.good.keyid).then(function (answer) {
expect(answer).to.be.undefined;
}, function (error) {
expect(error).to.be.an.instanceof(Error);
expect(error.code).to.equal('MSG_INCOMPLETE');
done();
});
});
});
it('Sending encryption with non existing keys fails', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
inputvalues.encrypt.good.data,
inputvalues.encrypt.bad.fingerprint).then(function (answer) {
expect(answer).to.be.undefined;
}, function(error){
expect(error).to.be.an('Error');
expect(error.code).to.not.be.undefined;
expect(error.code).to.equal('GNUPG_ERROR');
done();
});
});
}).timeout(5000);
- it('Overly large message ( > 65MB) is rejected', function (done) {
+ it('Overly large message ( > 64MB) is rejected', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
fixedLengthString(65),
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.be.undefined;
}, function(error){
expect(error).to.be.an.instanceof(Error);
- // expect(error.code).to.equal('GNUPG_ERROR');
// TODO: there is a 64 MB hard limit at least in chrome at:
// chromium//extensions/renderer/messaging_util.cc:
// kMaxMessageLength
+ // The error will be a browser error, not from gnupg or from
+ // this library
done();
});
});
}).timeout(8000);
// TODO check different valid parameter
});
diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js
index 358757b0..09c43f73 100644
--- a/lang/js/src/Keyring.js
+++ b/lang/js/src/Keyring.js
@@ -1,332 +1,350 @@
/* 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 .
* SPDX-License-Identifier: LGPL-2.1+
*
* Author(s):
* Maximilian Krambach
*/
import {createMessage} from './Message';
import {createKey} from './Key';
import { isFingerprint } from './Helpers';
import { gpgme_error } from './Errors';
export class GPGME_Keyring {
constructor(){
}
/**
* @param {String} pattern (optional) pattern A pattern to search for,
* in userIds or KeyIds
* @param {Boolean} prepare_sync (optional, default true) if set to true,
* Key.armor and Key.hasSecret will be called, so they can be used
* inmediately. This allows for full synchronous use. If set to false,
* these will initially only be available as Promises in getArmor() and
* getHasSecret()
* @param {Boolean} search (optional) retrieve the Keys from servers with
* the method(s) defined in gnupg (e.g. WKD/HKP lookup)
* @returns {Promise.>}
*
*/
getKeys(pattern, prepare_sync, search){
return new Promise(function(resolve, reject) {
let msg = createMessage('keylist');
if (pattern !== undefined){
msg.setParameter('keys', pattern);
}
msg.setParameter('sigs', true);
if (search === true){
msg.setParameter('locate', true);
}
msg.post().then(function(result){
let resultset = [];
- let promises = [];
if (result.keys.length === 0){
resolve([]);
} else {
- for (let i=0; i< result.keys.length; i++){
- let k = createKey(result.keys[i].fingerprint);
- k.setKeyData(result.keys[i]);
- if (prepare_sync === true){
- promises.push(k.getArmor());
- promises.push(k.getHasSecret());
- }
- resultset.push(k);
- }
- if (promises.length > 0) {
- Promise.all(promises).then(function() {
- resolve(resultset);
- }, function(error){
- reject(error);
- });
+ let secondrequest;
+ if (prepare_sync === true) {
+ secondrequest = function() {
+ msg.setParameter('secret', true);
+ return msg.post();
+ };
} else {
- resolve(resultset);
+ secondrequest = function() {
+ return Promise.resolve(true);
+ };
}
+ secondrequest().then(function(answer) {
+ for (let i=0; i < result.keys.length; i++){
+ if (prepare_sync === true){
+ result.keys[i].hasSecret = false;
+ if (answer && answer.keys) {
+ for (let j=0; j < answer.keys.length; j++ ){
+ if (result.keys[i].fingerprint ===
+ answer.keys[j].fingerprint
+ ) {
+ if (answer.keys[j].secret === true){
+ result.keys[i].hasSecret = true;
+ }
+ break;
+ }
+ }
+ // TODO getArmor() to be used in sync
+ }
+ }
+ let k = createKey(result.keys[i].fingerprint);
+ k.setKeyData(result.keys[i]);
+ resultset.push(k);
+ }
+ resolve(resultset);
+ }, function(error){
+ reject(error);
+ });
}
});
});
}
/**
* Fetches the armored public Key blocks for all Keys matchin the pattern
* (if no pattern is given, fetches all known to gnupg)
* @param {String|Array} pattern (optional)
* @returns {Promise} Armored Key blocks
*/
getKeysArmored(pattern) {
return new Promise(function(resolve, reject) {
let msg = createMessage('export');
msg.setParameter('armor', true);
if (pattern !== undefined){
msg.setParameter('keys', pattern);
}
msg.post().then(function(result){
resolve(result.data);
}, function(error){
reject(error);
});
});
}
/**
* Returns the Key to be used by default for signing operations,
* looking up the gpg configuration, or returning the first key that
* contains a secret key.
* @returns {Promise}
*
*
* TODO: getHasSecret always returns false at this moment, so this fucntion
* still does not fully work as intended.
* * @async
*/
getDefaultKey() {
let me = this;
return new Promise(function(resolve, reject){
let msg = createMessage('config_opt');
msg.setParameter('component', 'gpg');
msg.setParameter('option', 'default-key');
msg.post().then(function(response){
if (response.value !== undefined
&& response.value.hasOwnProperty('string')
&& typeof(response.value.string) === 'string'
){
me.getKeys(response.value.string,true).then(function(keys){
if(keys.length === 1){
resolve(keys[0]);
} else {
reject(gpgme_error('KEY_NO_DEFAULT'));
}
}, function(error){
reject(error);
});
} else {
// TODO: this is overly 'expensive' in communication
// and probably performance, too
me.getKeys(null,true).then(function(keys){
for (let i=0; i < keys.length; i++){
if (keys[i].get('hasSecret') === true){
resolve(keys[i]);
break;
}
if (i === keys.length -1){
reject(gpgme_error('KEY_NO_DEFAULT'));
}
}
}, function(error){
reject(error);
});
}
}, function(error){
reject(error);
});
});
}
/**
*
* @param {String} armored Armored Key block of the Key(s) to be imported
* into gnupg
* @param {Boolean} prepare_sync prepare the keys for synched use
* (see getKeys()).
*
* @returns {Promise