Add test of verification of a clearsign message generated by gpg

This commit is contained in:
Robert Nelson 2013-12-04 19:33:44 -08:00
parent e9b8b0efb9
commit f8d8d471a9
2 changed files with 175 additions and 109 deletions

View File

@ -38,123 +38,176 @@ define(function(require) {
'=ULta\n' + '=ULta\n' +
'-----END PGP PRIVATE KEY BLOCK-----'; '-----END PGP PRIVATE KEY BLOCK-----';
beforeEach(function() { beforeEach(function() {
pgp = new PGP(); pgp = new PGP();
}); });
afterEach(function() {}); afterEach(function() {});
describe('Generate key pair', function() { describe('Generate key pair', function() {
it('should fail', function(done) { it('should fail', function(done) {
pgp.generateKeys({ pgp.generateKeys({
emailAddress: 'test@t-onlinede', emailAddress: 'test@t-onlinede',
keySize: keySize, keySize: keySize,
passphrase: passphrase passphrase: passphrase
}, function(err, keys) { }, function(err, keys) {
expect(err).to.exist; expect(err).to.exist;
expect(keys).to.not.exist; expect(keys).to.not.exist;
done(); done();
}); });
}); });
it('should fail', function(done) { it('should fail', function(done) {
pgp.generateKeys({ pgp.generateKeys({
emailAddress: 'testt-online.de', emailAddress: 'testt-online.de',
keySize: keySize, keySize: keySize,
passphrase: passphrase passphrase: passphrase
}, function(err, keys) { }, function(err, keys) {
expect(err).to.exist; expect(err).to.exist;
expect(keys).to.not.exist; expect(keys).to.not.exist;
done(); done();
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
pgp.generateKeys({ pgp.generateKeys({
emailAddress: user, emailAddress: user,
keySize: keySize, keySize: keySize,
passphrase: passphrase passphrase: passphrase
}, function(err, keys) { }, function(err, keys) {
expect(err).to.not.exist; expect(err).to.not.exist;
expect(keys.keyId).to.exist; expect(keys.keyId).to.exist;
expect(keys.privateKeyArmored).to.exist; expect(keys.privateKeyArmored).to.exist;
expect(keys.publicKeyArmored).to.exist; expect(keys.publicKeyArmored).to.exist;
done(); done();
}); });
}); });
}); });
describe('Import/Export key pair', function() { describe('Import/Export key pair', function() {
it('should fail', function(done) { it('should fail', function(done) {
pgp.importKeys({ pgp.importKeys({
passphrase: 'asd', passphrase: 'asd',
privateKeyArmored: privkey, privateKeyArmored: privkey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) { }, function(err) {
expect(err).to.exist; expect(err).to.exist;
pgp.exportKeys(function(err, keys) { pgp.exportKeys(function(err, keys) {
expect(err).to.exist; expect(err).to.exist;
expect(keys).to.not.exist; expect(keys).to.not.exist;
done(); done();
}); });
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
pgp.importKeys({ pgp.importKeys({
passphrase: passphrase, passphrase: passphrase,
privateKeyArmored: privkey, privateKeyArmored: privkey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) { }, function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
pgp.exportKeys(function(err, keys) { pgp.exportKeys(function(err, keys) {
expect(err).to.not.exist; expect(err).to.not.exist;
expect(keys.keyId).to.equal(keyId); expect(keys.keyId).to.equal(keyId);
expect(keys.privateKeyArmored).to.equal(privkey); expect(keys.privateKeyArmored).to.equal(privkey);
expect(keys.publicKeyArmored).to.equal(pubkey); expect(keys.publicKeyArmored).to.equal(pubkey);
done(); done();
}); });
}); });
}); });
}); });
describe('Encryption', function() { describe('Encryption', function() {
var message = 'Hello, World!', var message = 'Hello, World!',
ciphertext; ciphertext;
beforeEach(function(done) { beforeEach(function(done) {
pgp.importKeys({ pgp.importKeys({
passphrase: passphrase, passphrase: passphrase,
privateKeyArmored: privkey, privateKeyArmored: privkey,
publicKeyArmored: pubkey publicKeyArmored: pubkey
}, function(err) { }, function(err) {
expect(err).to.not.exist; expect(err).to.not.exist;
done(); done();
}); });
}); });
describe('Encrypt and Sign', function() { describe('Encrypt and Sign', function() {
it('should work', function(done) { it('should work', function(done) {
pgp.encrypt(message, [pubkey], function(err, ct) { pgp.encrypt(message, [pubkey], function(err, ct) {
expect(err).to.not.exist; expect(err).to.not.exist;
expect(ct).to.exist; expect(ct).to.exist;
ciphertext = ct; ciphertext = ct;
done(); done();
}); });
}); });
}); });
describe('Decrypt and Verify', function() { describe('Decrypt and Verify', function() {
it('should work', function(done) { it('should work', function(done) {
pgp.decrypt(ciphertext, pubkey, function(err, pt) { pgp.decrypt(ciphertext, pubkey, function(err, pt) {
expect(err).to.not.exist; expect(err).to.not.exist;
expect(pt.text).to.equal(message); expect(pt.text).to.equal(message);
expect(pt.validSignatures[0]).to.be.true; expect(pt.validSignatures[0]).to.be.true;
done(); done();
}); });
}); });
}); });
}); });
}); describe('Clearsign from gpg verification', function() {
}); var clearsign_msg = '-----BEGIN PGP SIGNED MESSAGE-----\r\n' +
'Hash: SHA1\r\n' +
'\r\n' +
'This is a test message.\r\n' +
'\r\n' +
'This paragraph is separated form the next by a line of dashes.\r\n' +
'\r\n' +
'- --------------------------------------------------------------------------\r\n' +
'\r\n' +
'The next paragraph has a number of blank lines between this one and it.\r\n' +
'\r\n' +
'\r\n' +
'\r\n' +
'\r\n' +
'\r\n' +
'\r\n' +
'This is the last paragraph.\r\n' +
'\r\n' +
'- --\r\n' +
'\r\n' +
'Joe Test\r\n' +
'-----BEGIN PGP SIGNATURE-----\r\n' +
'Version: GnuPG v1.4.15 (GNU/Linux)\r\n' +
'\r\n' +
'iFwEAQECAAYFAlKf5LcACgkQ9vYOm0LN/0ybVwH8CItdDh4kWKVcyUx3Q3hWZnWd\r\n' +
'zP9CUbIa9uToIPABjV3GOTDM3ZgiP0/SE6Al5vG8hlx+/u2piVojoLovk/4LnA==\r\n' +
'=i6ew\r\n' +
'-----END PGP SIGNATURE-----\r\n';
beforeEach(function(done) {
pgp.importKeys({
passphrase: passphrase,
privateKeyArmored: privkey,
publicKeyArmored: pubkey
}, function(err) {
expect(err).to.not.exist;
done();
});
});
describe('Verify', function() {
it('should work', function(done) {
pgp.verify(clearsign_msg, pubkey, function(err, pt) {
expect(err).to.not.exist;
expect(pt).to.be.true;
done();
});
});
});
});
});
});

View File

@ -174,5 +174,18 @@ define(function(require) {
} }
}; };
/**
* Verify a clearsign message for a single sender
*/
PGP.prototype.verify = function(message, senderKey, callback) {
var publicKey = openpgp.read_publicKey(senderKey)[0];
var pubKeys = [ { armored: senderKey, obj: publicKey, keyId: publicKey.getKeyId() } ];
var msg = openpgp.read_message(message)[0];
var verified = msg.verifySignature(pubKeys);
callback(null, verified);
};
return PGP; return PGP;
}); });