Fix key and signature tests

This commit is contained in:
Tankred Hase 2016-02-10 15:41:04 +07:00
parent 000c3b3686
commit a8fd179843
2 changed files with 36 additions and 37 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var openpgp = typeof window != 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp');
var chai = require('chai'), var chai = require('chai'),
expect = chai.expect; expect = chai.expect;
@ -391,7 +391,6 @@ var pgp_desktop_priv =
'=63Nq', '=63Nq',
'-----END PGP PRIVATE KEY BLOCK-----'].join('\n'); '-----END PGP PRIVATE KEY BLOCK-----'].join('\n');
it('Parsing armored text with two keys', function(done) { it('Parsing armored text with two keys', function(done) {
var pubKeys = openpgp.key.readArmored(twoKeys); var pubKeys = openpgp.key.readArmored(twoKeys);
expect(pubKeys).to.exist; expect(pubKeys).to.exist;
@ -411,7 +410,7 @@ var pgp_desktop_priv =
var pubKeyV4 = pubKeysV4.keys[0]; var pubKeyV4 = pubKeysV4.keys[0];
expect(pubKeyV4).to.exist; expect(pubKeyV4).to.exist;
var pubKeysV3 = openpgp.key.readArmored(pub_v3) var pubKeysV3 = openpgp.key.readArmored(pub_v3);
expect(pubKeysV3).to.exist; expect(pubKeysV3).to.exist;
expect(pubKeysV3.err).to.not.exist; expect(pubKeysV3.err).to.not.exist;
@ -428,7 +427,7 @@ var pgp_desktop_priv =
}); });
it('Testing key method getSubkeyPackets', function(done) { it('Testing key method getSubkeyPackets', function(done) {
var pubKeys = openpgp.key.readArmored(pub_sig_test) var pubKeys = openpgp.key.readArmored(pub_sig_test);
expect(pubKeys).to.exist; expect(pubKeys).to.exist;
expect(pubKeys.err).to.not.exist; expect(pubKeys.err).to.not.exist;
@ -634,9 +633,9 @@ var pgp_desktop_priv =
expect(key.users[0].selfCertifications[0].preferredCompressionAlgorithms).to.eql([compr.zlib, compr.zip]); expect(key.users[0].selfCertifications[0].preferredCompressionAlgorithms).to.eql([compr.zlib, compr.zip]);
expect(key.users[0].selfCertifications[0].features).to.eql(openpgp.config.integrity_protect ? [1] : null); // modification detection expect(key.users[0].selfCertifications[0].features).to.eql(openpgp.config.integrity_protect ? [1] : null); // modification detection
}; };
var opt = {numBits: 512, userId: 'test', passphrase: 'hello'}; var opt = {numBits: 512, userIds: 'test <a@b.com>', passphrase: 'hello'};
if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKeyPair(opt).then(function(key) { openpgp.generateKey(opt).then(function(key) {
testPref(key.key); testPref(key.key);
testPref(openpgp.key.readArmored(key.publicKeyArmored).keys[0]); testPref(openpgp.key.readArmored(key.publicKeyArmored).keys[0]);
done(); done();
@ -658,9 +657,9 @@ var pgp_desktop_priv =
}); });
it('Generated key is not unlocked by default', function(done) { it('Generated key is not unlocked by default', function(done) {
var opt = {numBits: 512, userId: 'test', passphrase: '123'}; var opt = {numBits: 512, userIds: 'test <a@b.com>', passphrase: '123'};
if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKeyPair(opt).then(function(key) { openpgp.generateKey(opt).then(function(key) {
var msg = openpgp.message.fromText('hello').encrypt([key.key]); var msg = openpgp.message.fromText('hello').encrypt([key.key]);
msg = msg.decrypt.bind(msg, key.key); msg = msg.decrypt.bind(msg, key.key);
expect(msg).to.throw('Private key is not decrypted.'); expect(msg).to.throw('Private key is not decrypted.');
@ -669,10 +668,10 @@ var pgp_desktop_priv =
}); });
it('Generate key - single userid', function(done) { it('Generate key - single userid', function(done) {
var userId = 'single user'; var userId = 'test <a@b.com>';
var opt = {numBits: 512, userId: userId, passphrase: '123'}; var opt = {numBits: 512, userIds: userId, passphrase: '123'};
if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKeyPair(opt).then(function(key) { openpgp.generateKey(opt).then(function(key) {
key = key.key; key = key.key;
expect(key.users.length).to.equal(1); expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal(userId); expect(key.users[0].userId.userid).to.equal(userId);
@ -681,11 +680,11 @@ var pgp_desktop_priv =
}); });
it('Generate key - multi userid', function(done) { it('Generate key - multi userid', function(done) {
var userId1 = 'first user'; var userId1 = 'test <a@b.com>';
var userId2 = 'second user'; var userId2 = 'test <b@c.com>';
var opt = {numBits: 512, userId: [userId1, userId2], passphrase: '123'}; var opt = {numBits: 512, userIds: [userId1, userId2], passphrase: '123'};
if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKeyPair(opt).then(function(key) { openpgp.generateKey(opt).then(function(key) {
key = key.key; key = key.key;
expect(key.users.length).to.equal(2); expect(key.users.length).to.equal(2);
expect(key.users[0].userId.userid).to.equal(userId1); expect(key.users[0].userId.userid).to.equal(userId1);

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var openpgp = typeof window != 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp');
var chai = require('chai'), var chai = require('chai'),
expect = chai.expect; expect = chai.expect;
@ -262,8 +262,8 @@ describe("Signature", function() {
var pub_key = openpgp.key.readArmored(pub_key_arm1).keys[0]; var pub_key = openpgp.key.readArmored(pub_key_arm1).keys[0];
var msg = openpgp.message.readArmored(msg_arm1); var msg = openpgp.message.readArmored(msg_arm1);
priv_key.decrypt("abcd"); priv_key.decrypt("abcd");
openpgp.decryptAndVerifyMessage(priv_key, [pub_key], msg).then(function(decrypted) { openpgp.decrypt({ privateKey: priv_key, publicKeys:[pub_key], message:msg }).then(function(decrypted) {
expect(decrypted).to.exist; expect(decrypted.data).to.exist;
expect(decrypted.signatures[0].valid).to.be.true; expect(decrypted.signatures[0].valid).to.be.true;
done(); done();
}); });
@ -358,7 +358,7 @@ describe("Signature", function() {
done(); done();
}); });
it('Verify signature of signed and encrypted message from GPG2 with openpgp.decryptAndVerifyMessage', function(done) { it('Verify signature of signed and encrypted message from GPG2 with openpgp.decrypt', function(done) {
var msg_armor = var msg_armor =
[ '-----BEGIN PGP MESSAGE-----', [ '-----BEGIN PGP MESSAGE-----',
'Version: GnuPG v2.0.19 (GNU/Linux)', 'Version: GnuPG v2.0.19 (GNU/Linux)',
@ -384,16 +384,16 @@ describe("Signature", function() {
var keyids = esMsg.getEncryptionKeyIds(); var keyids = esMsg.getEncryptionKeyIds();
privKey.decryptKeyPacket(keyids, 'hello world'); privKey.decryptKeyPacket(keyids, 'hello world');
openpgp.decryptAndVerifyMessage(privKey, [pubKey], esMsg).then(function(decrypted) { openpgp.decrypt({ privateKey: privKey, publicKeys:[pubKey], message:esMsg }).then(function(decrypted) {
expect(decrypted).to.exist; expect(decrypted.data).to.exist;
expect(decrypted.text).to.equal(plaintext); expect(decrypted.data).to.equal(plaintext);
expect(decrypted.signatures).to.have.length(1); expect(decrypted.signatures).to.have.length(1);
expect(decrypted.signatures[0].valid).to.be.true; expect(decrypted.signatures[0].valid).to.be.true;
done(); done();
}); });
}); });
it('Verify signature of signed and encrypted message from PGP 10.3.0 with openpgp.decryptAndVerifyMessage', function(done) { it('Verify signature of signed and encrypted message from PGP 10.3.0 with openpgp.decrypt', function(done) {
var msg_armor = var msg_armor =
[ '-----BEGIN PGP MESSAGE-----', [ '-----BEGIN PGP MESSAGE-----',
'Version: Encryption Desktop 10.3.0 (Build 9307)', 'Version: Encryption Desktop 10.3.0 (Build 9307)',
@ -420,9 +420,9 @@ describe("Signature", function() {
var keyids = esMsg.getEncryptionKeyIds(); var keyids = esMsg.getEncryptionKeyIds();
privKey.decryptKeyPacket(keyids, 'hello world'); privKey.decryptKeyPacket(keyids, 'hello world');
openpgp.decryptAndVerifyMessage(privKey, [pubKey], esMsg).then(function(decrypted) { openpgp.decrypt({ privateKey: privKey, publicKeys:[pubKey], message:esMsg }).then(function(decrypted) {
expect(decrypted).to.exist; expect(decrypted.data).to.exist;
expect(decrypted.text).to.equal(plaintext); expect(decrypted.data).to.equal(plaintext);
expect(decrypted.signatures).to.have.length(1); expect(decrypted.signatures).to.have.length(1);
expect(decrypted.signatures[0].valid).to.be.true; expect(decrypted.signatures[0].valid).to.be.true;
done(); done();
@ -471,7 +471,7 @@ describe("Signature", function() {
done(); done();
}); });
it('Verify cleartext signed message with two signatures with openpgp.verifyClearSignedMessage', function(done) { it('Verify cleartext signed message with two signatures with openpgp.verify', function(done) {
var msg_armor = var msg_armor =
[ '-----BEGIN PGP SIGNED MESSAGE-----', [ '-----BEGIN PGP SIGNED MESSAGE-----',
'Hash: SHA256', 'Hash: SHA256',
@ -506,9 +506,9 @@ describe("Signature", function() {
expect(pubKey2.getKeyPacket(keyids)).to.exist; expect(pubKey2.getKeyPacket(keyids)).to.exist;
expect(pubKey3.getKeyPacket(keyids)).to.exist; expect(pubKey3.getKeyPacket(keyids)).to.exist;
openpgp.verifyClearSignedMessage([pubKey2, pubKey3], csMsg).then(function(cleartextSig) { openpgp.verify({ publicKeys:[pubKey2, pubKey3], message:csMsg }).then(function(cleartextSig) {
expect(cleartextSig).to.exist; expect(cleartextSig).to.exist;
expect(cleartextSig.text).to.equal(plaintext); expect(cleartextSig.data).to.equal(plaintext);
expect(cleartextSig.signatures).to.have.length(2); expect(cleartextSig.signatures).to.have.length(2);
expect(cleartextSig.signatures[0].valid).to.be.true; expect(cleartextSig.signatures[0].valid).to.be.true;
expect(cleartextSig.signatures[1].valid).to.be.true; expect(cleartextSig.signatures[1].valid).to.be.true;
@ -516,20 +516,20 @@ describe("Signature", function() {
}); });
}); });
it('Sign text with openpgp.signClearMessage and verify with openpgp.verifyClearSignedMessage leads to same cleartext and valid signatures', function(done) { it('Sign text with openpgp.sign and verify with openpgp.verify leads to same cleartext and valid signatures', function(done) {
var plaintext = 'short message\nnext line\n한국어/조선말'; var plaintext = 'short message\nnext line\n한국어/조선말';
var pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0]; var pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0];
var privKey = openpgp.key.readArmored(priv_key_arm2).keys[0]; var privKey = openpgp.key.readArmored(priv_key_arm2).keys[0];
privKey.getSigningKeyPacket().decrypt('hello world'); privKey.getSigningKeyPacket().decrypt('hello world');
openpgp.signClearMessage([privKey], plaintext).then(function(clearSignedArmor) { openpgp.sign({ privateKeys:[privKey], data:plaintext }).then(function(signed) {
var csMsg = openpgp.cleartext.readArmored(clearSignedArmor); var csMsg = openpgp.cleartext.readArmored(signed.data);
return openpgp.verifyClearSignedMessage([pubKey], csMsg); return openpgp.verify({ publicKeys:[pubKey], message:csMsg });
}).then(function(cleartextSig) { }).then(function(cleartextSig) {
expect(cleartextSig).to.exist; expect(cleartextSig).to.exist;
expect(cleartextSig.text).to.equal(plaintext.replace(/\r/g,'')); expect(cleartextSig.data).to.equal(plaintext.replace(/\r/g,''));
expect(cleartextSig.signatures).to.have.length(1); expect(cleartextSig.signatures).to.have.length(1);
expect(cleartextSig.signatures[0].valid).to.be.true; expect(cleartextSig.signatures[0].valid).to.be.true;
done(); done();
@ -576,7 +576,7 @@ describe("Signature", function() {
it('Write unhashed subpackets', function() { it('Write unhashed subpackets', function() {
var pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0]; var pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0];
expect(pubKey.users[0].selfCertifications).to.exist; expect(pubKey.users[0].selfCertifications).to.exist;
pubKey = openpgp.key.readArmored(pubKey.armor()).keys[0] pubKey = openpgp.key.readArmored(pubKey.armor()).keys[0];
expect(pubKey.users[0].selfCertifications).to.exist; expect(pubKey.users[0].selfCertifications).to.exist;
}); });
@ -641,9 +641,9 @@ describe("Signature", function() {
}); });
it('Sign message with key without password', function(done) { it('Sign message with key without password', function(done) {
var opt = {numBits: 512, userId: 'ABC', passphrase: null}; var opt = {numBits: 512, userIds: { name:'test', email:'a@b.com' }, passphrase: null};
if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys if (openpgp.util.getWebCrypto()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
openpgp.generateKeyPair(opt).then(function(gen) { openpgp.generateKey(opt).then(function(gen) {
var key = gen.key; var key = gen.key;
var message = openpgp.message.fromText('hello world'); var message = openpgp.message.fromText('hello world');