mirror of
https://github.com/moparisthebest/mail
synced 2025-02-17 23:40:22 -05:00
check PGP signature and throw an error if verification fails
This commit is contained in:
parent
f6b15ac151
commit
76b2b90e0e
@ -177,10 +177,15 @@ define(function(require) {
|
|||||||
* Decrypt and verify a pgp message for a single sender
|
* Decrypt and verify a pgp message for a single sender
|
||||||
*/
|
*/
|
||||||
PGP.prototype.decrypt = function(ciphertext, senderKey, callback) {
|
PGP.prototype.decrypt = function(ciphertext, senderKey, callback) {
|
||||||
var privateKey, msg, keymat, sesskey, decrypted;
|
var privateKey, publicKey, pubKeys, msg, keymat, sesskey, decrypted;
|
||||||
|
|
||||||
privateKey = openpgp.keyring.exportPrivateKey(0).obj;
|
privateKey = openpgp.keyring.exportPrivateKey(0).obj;
|
||||||
senderKey = openpgp.read_publicKey(senderKey)[0];
|
publicKey = openpgp.read_publicKey(senderKey)[0];
|
||||||
|
pubKeys = [{
|
||||||
|
armored: senderKey,
|
||||||
|
obj: publicKey,
|
||||||
|
keyId: publicKey.getKeyId()
|
||||||
|
}];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
msg = openpgp.read_message(ciphertext)[0];
|
msg = openpgp.read_message(ciphertext)[0];
|
||||||
@ -223,15 +228,26 @@ define(function(require) {
|
|||||||
|
|
||||||
// decrypt and verify ciphertext
|
// decrypt and verify ciphertext
|
||||||
try {
|
try {
|
||||||
decrypted = msg.decryptAndVerifySignature(keymat, sesskey, senderKey);
|
decrypted = msg.decryptAndVerifySignature(keymat, sesskey, pubKeys);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
callback({
|
callback({
|
||||||
errMsg: 'Error reading PGP message!',
|
errMsg: 'Error decrypting PGP message!',
|
||||||
err: err
|
err: err
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if signatures are ok
|
||||||
|
for (var k = 0; k < decrypted.validSignatures.length; k++) {
|
||||||
|
if (!decrypted.validSignatures[k]) {
|
||||||
|
callback({
|
||||||
|
errMsg: 'Error verifying PGP signature!'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return decrypted plaintext
|
||||||
callback(null, decrypted.text);
|
callback(null, decrypted.text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -118,8 +118,7 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Encryption', function() {
|
describe('Encryption', function() {
|
||||||
var message = 'Hello, World!',
|
var message = 'Hello, World!';
|
||||||
ciphertext;
|
|
||||||
|
|
||||||
beforeEach(function(done) {
|
beforeEach(function(done) {
|
||||||
pgp.importKeys({
|
pgp.importKeys({
|
||||||
@ -151,7 +150,7 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Encrypt', function() {
|
describe('Encrypt and sign', function() {
|
||||||
it('should fail', function(done) {
|
it('should fail', function(done) {
|
||||||
var input = null;
|
var input = null;
|
||||||
|
|
||||||
@ -166,13 +165,23 @@ define(function(require) {
|
|||||||
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;
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Decrypt', function() {
|
describe('Decrypt and verify', function() {
|
||||||
|
var ciphertext;
|
||||||
|
|
||||||
|
beforeEach(function(done) {
|
||||||
|
pgp.encrypt(message, [pubkey], function(err, ct) {
|
||||||
|
expect(err).to.not.exist;
|
||||||
|
expect(ct).to.exist;
|
||||||
|
ciphertext = ct;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should fail', function(done) {
|
it('should fail', function(done) {
|
||||||
var input = 'asdfa\rsdf';
|
var input = 'asdfa\rsdf';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user