1
0
mirror of https://github.com/moparisthebest/mail synced 2025-02-17 23:40:22 -05:00

[WO-74, WO-80] handle error when decrypting PGP messages

This commit is contained in:
Tankred Hase 2013-11-14 13:57:52 +01:00
parent 4bb271c406
commit 3a57172358
3 changed files with 58 additions and 11 deletions

View File

@ -153,7 +153,16 @@ define(function(require) {
receiverKeys[i] = openpgp.read_publicKey(receiverKeys[i])[0]; receiverKeys[i] = openpgp.read_publicKey(receiverKeys[i])[0];
} }
ct = openpgp.write_signed_and_encrypted_message(privateKey, receiverKeys, plaintext); // encrypt and sign the plaintext
try {
ct = openpgp.write_signed_and_encrypted_message(privateKey, receiverKeys, plaintext);
} catch (err) {
callback({
errMsg: 'Error encrypting plaintext!',
err: err
});
return;
}
callback(null, ct); callback(null, ct);
}; };
@ -162,12 +171,20 @@ 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 = openpgp.keyring.exportPrivateKey(0).obj; var privateKey, msg, keymat, sesskey, decrypted;
privateKey = openpgp.keyring.exportPrivateKey(0).obj;
senderKey = openpgp.read_publicKey(senderKey)[0]; senderKey = openpgp.read_publicKey(senderKey)[0];
var msg = openpgp.read_message(ciphertext)[0]; try {
var keymat = null; msg = openpgp.read_message(ciphertext)[0];
var sesskey = null; } catch (err) {
callback({
errMsg: 'Error decrypting PGP message!',
err: err
});
return;
}
// Find the private (sub)key for the session key of the message // Find the private (sub)key for the session key of the message
for (var i = 0; i < msg.sessionKeys.length; i++) { for (var i = 0; i < msg.sessionKeys.length; i++) {
@ -190,15 +207,26 @@ define(function(require) {
} }
} }
} }
if (keymat !== null) {
var decrypted = msg.decryptAndVerifySignature(keymat, sesskey, senderKey);
callback(null, decrypted.text);
} else { if (!keymat) {
callback({ callback({
errMsg: 'No private key found!' errMsg: 'No private key found!'
}); });
return;
} }
// decrypt and verify ciphertext
try {
decrypted = msg.decryptAndVerifySignature(keymat, sesskey, senderKey);
} catch (err) {
callback({
errMsg: 'Error decrypting PGP message!',
err: err
});
return;
}
callback(null, decrypted.text);
}; };
return PGP; return PGP;

View File

@ -296,8 +296,7 @@ define(function(require) {
// decrypt and verfiy signatures // decrypt and verfiy signatures
self._crypto.decrypt(email.body, senderPubkey.publicKey, function(err, decrypted) { self._crypto.decrypt(email.body, senderPubkey.publicKey, function(err, decrypted) {
if (err) { if (err) {
callback(err); decrypted = err.errMsg;
return;
} }
email.body = decrypted; email.body = decrypted;

View File

@ -147,6 +147,16 @@ define(function(require) {
}); });
describe('Encrypt', function() { describe('Encrypt', function() {
it('should fail', function(done) {
var input = null;
pgp.encrypt(input, [pubkey], function(err, ct) {
expect(err).to.exist;
expect(ct).to.not.exist;
done();
});
});
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;
@ -158,6 +168,16 @@ define(function(require) {
}); });
describe('Decrypt', function() { describe('Decrypt', function() {
it('should fail', function(done) {
var input = 'asdfa\rsdf';
pgp.decrypt(input, pubkey, function(err, pt) {
expect(err).to.exist;
expect(pt).to.not.exist;
done();
});
});
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;