js: fixing errors found by testing: encrypt/decrypt

--

* Key.js: Error code for wrong parameter in createKey should be
  "PARAM_WRONG"
* Helpers.js: The property openpgpjs-like Objects were checked for in
  toKeyIdArray was not defined.
* src/permittedOperations.js: updated more expectations and assumptions
  for the native API

* new Problems:
  - There seems to be a message size limit of about 21 MB for
    nativeMessaging, much lower than the documented 4GB.
  - Some bytes are lost with random data in an encrypt-decrypt
    roundtrip. The culprit is unclear.
This commit is contained in:
Maximilian Krambach 2018-05-07 18:27:25 +02:00
parent cf075846fb
commit 8f3d83e5f0
7 changed files with 219 additions and 23 deletions

View File

@ -18,6 +18,7 @@
<!-- insert tests here--> <!-- insert tests here-->
<script src="tests/startup.js"></script> <script src="tests/startup.js"></script>
<script src="tests/encryptTest.js"></script> <script src="tests/encryptTest.js"></script>
<script src="tests/encryptDecryptTest.js"></script>
<!-- run tests --> <!-- run tests -->
<script src="runbrowsertest.js"></script> <script src="runbrowsertest.js"></script>

View File

@ -0,0 +1,72 @@
/* 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+
*/
describe('Encryption and Decryption', function () {
it('Successful encrypt and decrypt', 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');
context.decrypt(answer.data).then(function(result){
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
expect(result.data).to.equal(inputvalues.encrypt.good.data);
done();
});
});
});
});
/**
* Fails with random data! Some bytes (up to 100) of the original are missing in
* the result
*/
/**
for (let i=0; i< 20; i++) {
it('Successful encrypt 1 MB '+ i+ '/20', function (done) {
let prm = Gpgmejs.init();
let data = bigString(0.1);
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');
context.decrypt(answer.data).then(
function(result){
expect(result).to.not.be.empty;
expect(result.data).to.be.a('string');
expect(result.data).to.equal(data);
done();
});
});
});
}).timeout(10000);
};*/
});

View File

@ -17,47 +17,133 @@
* License along with this program; if not, see <http://www.gnu.org/licenses/>. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1+ * SPDX-License-Identifier: LGPL-2.1+
*/ */
describe('Encryption', function(){ describe('Encryption', function () {
it('Successfull encrypt', function(){ it('Successful encrypt', function (done) {
let prm = Gpgmejs.init(); let prm = Gpgmejs.init();
prm.then(function(context){ prm.then(function (context) {
context.encrypt( context.encrypt(
inputvalues.encrypt.good.data, inputvalues.encrypt.good.data,
inputvalues.encrypt.good.fingerprint).then(function(answer){ inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.not.be.empty; expect(answer).to.not.be.empty;
expect(answer.data).to.be.a("string"); expect(answer.data).to.be.a("string");
expect(answer.data).to.include('BEGIN PGP MESSAGE'); expect(answer.data).to.include('BEGIN PGP MESSAGE');
expect(answer.data).to.include('END PGP MESSAGE'); expect(answer.data).to.include('END PGP MESSAGE');
done();
}); });
}); });
}); });
it('Sending encryption without keys fails', function(){ it('Successful encrypt 5 MB', function (done) {
let prm = Gpgmejs.init(); let prm = Gpgmejs.init();
prm.then(function(context){ let data = bigString(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(5000);
it('Successful encrypt 20 MB', function (done) {
let prm = Gpgmejs.init();
let data = bigString(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 30 MB', function (done) {
// TODO: There seems to be a limit imposed at least by chrome at about 21 MB
let prm = Gpgmejs.init();
let data = bigString(30);
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( context.encrypt(
inputvalues.encrypt.good.data, inputvalues.encrypt.good.data,
null).then(function(answer){ null).then(function (answer) {
expect(answer).to.be.undefined; expect(answer).to.be.undefined;
}, function(error){ }, function(error){
expect(error).to.be.an('Error'); expect(error).to.be.an('Error');
expect(error.code).to.equal('MSG_INCOMPLETE'); expect(error.code).to.equal('MSG_INCOMPLETE');
//TODO: MSG_INCOMPLETE desired, GNUPG_ERROR coming done();
}); });
}); });
}); });
it('Sending encryption without data fails', function(){ it('Sending encryption without data fails', function (done) {
let prm = Gpgmejs.init(); let prm = Gpgmejs.init();
prm.then(function(context){ prm.then(function (context) {
context.encrypt( context.encrypt(
null,inputvalues.encrypt.good.keyid).then(function(answer){ 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();
});
});
});
it('Overly large message ( >= 48MB) is rejected', function (done) {
let prm = Gpgmejs.init();
prm.then(function (context) {
context.encrypt(
bigString(48),
inputvalues.encrypt.good.fingerprint).then(function (answer) {
expect(answer).to.be.undefined; expect(answer).to.be.undefined;
}, function(error){ }, function(error){
expect(error).to.be.an.instanceof(Error); expect(error).to.be.an.instanceof(Error);
expect(error.code).to.equal('PARAM_WRONG'); // TODO who is throwing the error here?
}); // It is not a GPGME_Error!
done();
}); });
}); });
}).timeout(8000);
// TODO check different valid parameter // TODO check different valid parameter
}); });

View File

@ -23,6 +23,9 @@ var inputvalues = {
good:{ good:{
data : 'Hello World.', data : 'Hello World.',
fingerprint : 'CDC3A2B2860625CCBFC5A5A9FC6D1B604967FC40' fingerprint : 'CDC3A2B2860625CCBFC5A5A9FC6D1B604967FC40'
},
bad: {
fingerprint: 'CDC3A2B2860625CCBFC5AAAAAC6D1B604967FC4A'
} }
}, },
init: { init: {
@ -30,3 +33,12 @@ var inputvalues = {
} }
}; };
function bigString(megabytes){
let maxlength = 1024 * 1024 * megabytes;
let uint = new Uint8Array(maxlength);
for (let i= 0; i < maxlength; i++){
uint[i] = Math.random() * Math.floor(256);
}
return new TextDecoder('utf-8').decode(uint);
}

View File

@ -99,11 +99,12 @@ export class Connection{
me._connection.onMessage.removeListener(listener) me._connection.onMessage.removeListener(listener)
reject(gpgme_error('CONN_EMPTY_GPG_ANSWER')); reject(gpgme_error('CONN_EMPTY_GPG_ANSWER'));
} else if (msg.type === "error"){ } else if (msg.type === "error"){
me._connection.onMessage.removeListener(listener) me._connection.onMessage.removeListener(listener);
reject(gpgme_error('GNUPG_ERROR', msg.msg)); reject(gpgme_error('GNUPG_ERROR', msg.msg));
} else { } else {
let answer_result = answer.add(msg); let answer_result = answer.add(msg);
if (answer_result !== true){ if (answer_result !== true){
me._connection.onMessage.removeListener(listener);
reject(answer_result); reject(answer_result);
} }
if (msg.more === true){ if (msg.more === true){
@ -127,8 +128,12 @@ export class Connection{
}, 5000); }, 5000);
}]).then(function(result){ }]).then(function(result){
return result; return result;
}, function(error){ }, function(reject){
return error; if(!reject instanceof Error) {
return gpgme_error('GNUPG_ERROR', reject);
} else {
return reject;
}
}); });
} }
}); });
@ -196,7 +201,7 @@ class Answer{
this._response.push(msg[key]); this._response.push(msg[key]);
} }
else { else {
return gpgme_error('CONN_UNEXPECTED_ANSWER', key); return gpgme_error('CONN_UNEXPECTED_ANSWER');
} }
break; break;
} }

View File

@ -84,9 +84,22 @@ export class GPGME_Message {
let checktype = function(val){ let checktype = function(val){
switch(typeof(val)){ switch(typeof(val)){
case 'string': case 'string':
if (poparam.allowed.indexOf(typeof(val)) >= 0
&& val.length > 0) {
return true;
}
return gpgme_error('PARAM_WRONG');
break;
case 'number': case 'number':
if (
poparam.allowed.indexOf('number') >= 0
&& isNaN(value) === false){
return true;
}
return gpgme_error('PARAM_WRONG');
break;
case 'boolean': case 'boolean':
if (poparam.allowed.indexOf(typeof(val)) >= 0){ if (poparam.allowed.indexOf('boolean') >= 0){
return true; return true;
} }
return gpgme_error('PARAM_WRONG'); return gpgme_error('PARAM_WRONG');
@ -102,7 +115,9 @@ export class GPGME_Message {
return res; return res;
} }
} }
if (val.length > 0) {
return true; return true;
}
} else if (val instanceof Uint8Array){ } else if (val instanceof Uint8Array){
if (poparam.allowed.indexOf('Uint8Array') >= 0){ if (poparam.allowed.indexOf('Uint8Array') >= 0){
return true; return true;

View File

@ -130,7 +130,6 @@ function unittests (){
let test0 = toKeyIdArray(hp.valid_openpgplike); let test0 = toKeyIdArray(hp.valid_openpgplike);
expect(test0).to.be.an('array').with.lengthOf(1); expect(test0).to.be.an('array').with.lengthOf(1);
console.log(test0);
expect(test0).to.include( expect(test0).to.include(
hp.valid_openpgplike.primaryKey.getFingerprint()); hp.valid_openpgplike.primaryKey.getFingerprint());
}); });
@ -255,7 +254,7 @@ function unittests (){
expect(test0.isComplete).to.be.false; expect(test0.isComplete).to.be.false;
}); });
it('Message is complete after setting mandatoy data', function(){ it('Message is complete after setting mandatory data', function(){
let test0 = createMessage('encrypt'); let test0 = createMessage('encrypt');
test0.setParameter('data', mp.valid_encrypt_data); test0.setParameter('data', mp.valid_encrypt_data);
test0.setParameter('keys', hp.validFingerprints); test0.setParameter('keys', hp.validFingerprints);
@ -263,6 +262,13 @@ function unittests (){
expect(test0.isComplete).to.be.true; expect(test0.isComplete).to.be.true;
}); });
it('Message is not complete after mandatory data is empty', function(){
let test0 = createMessage('encrypt');
test0.setParameter('data', '');
test0.setParameter('keys', hp.validFingerprints);
expect(test0.isComplete).to.be.false;
});
it('Complete Message contains the data that was set', function(){ it('Complete Message contains the data that was set', function(){
let test0 = createMessage('encrypt'); let test0 = createMessage('encrypt');
test0.setParameter('data', mp.valid_encrypt_data); test0.setParameter('data', mp.valid_encrypt_data);
@ -315,7 +321,6 @@ function unittests (){
}); });
}); });
mocha.run();
} }
export default {unittests}; export default {unittests};