deal with empty encrypted data correctly
This commit is contained in:
parent
70fff1e268
commit
1682787152
|
@ -219,21 +219,21 @@ Message.prototype.getText = function() {
|
|||
Message.prototype.encrypt = function(keys, passwords, sessionKey) {
|
||||
let symAlgo, msg, symEncryptedPacket;
|
||||
return Promise.resolve().then(() => {
|
||||
if (keys) {
|
||||
symAlgo = enums.read(enums.symmetric, keyModule.getPreferredSymAlgo(keys));
|
||||
} else if (passwords) {
|
||||
symAlgo = enums.read(enums.symmetric, config.encryption_cipher);
|
||||
} else {
|
||||
throw new Error('No keys or passwords');
|
||||
}
|
||||
|
||||
if (sessionKey) {
|
||||
if (!util.isUint8Array(sessionKey.data) || !util.isString(sessionKey.algorithm)) {
|
||||
throw new Error('Invalid session key for encryption.');
|
||||
}
|
||||
symAlgo = sessionKey.algorithm;
|
||||
sessionKey = sessionKey.data;
|
||||
} else if (keys && keys.length) {
|
||||
symAlgo = enums.read(enums.symmetric, keyModule.getPreferredSymAlgo(keys));
|
||||
} else if (passwords && passwords.length) {
|
||||
symAlgo = enums.read(enums.symmetric, config.encryption_cipher);
|
||||
} else {
|
||||
throw new Error('No keys, passwords, or session key provided.');
|
||||
}
|
||||
|
||||
if (!sessionKey) {
|
||||
sessionKey = crypto.generateSessionKey(symAlgo);
|
||||
}
|
||||
|
||||
|
|
|
@ -252,17 +252,17 @@ export function decrypt({ message, privateKey, publicKeys, sessionKey, password,
|
|||
return message.decrypt(privateKey, sessionKey, password).then(message => {
|
||||
|
||||
const result = parseMessage(message, format);
|
||||
if (result.data) { // verify
|
||||
if (!publicKeys) {
|
||||
publicKeys = [];
|
||||
}
|
||||
if (signature) {
|
||||
//detached signature
|
||||
result.signatures = message.verifyDetached(signature, publicKeys);
|
||||
} else {
|
||||
result.signatures = message.verify(publicKeys);
|
||||
}
|
||||
|
||||
if (!publicKeys) {
|
||||
publicKeys = [];
|
||||
}
|
||||
if (signature) {
|
||||
//detached signature
|
||||
result.signatures = message.verifyDetached(signature, publicKeys);
|
||||
} else {
|
||||
result.signatures = message.verify(publicKeys);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}).catch(onError.bind(null, 'Error decrypting message'));
|
||||
|
|
|
@ -715,6 +715,27 @@ describe('OpenPGP.js public api tests', function() {
|
|||
});
|
||||
});
|
||||
|
||||
it('should encrypt/sign and decrypt/verify with null string input', function() {
|
||||
var encOpt = {
|
||||
data: '',
|
||||
publicKeys: publicKey.keys,
|
||||
privateKeys: privateKey.keys
|
||||
};
|
||||
var decOpt = {
|
||||
privateKey: privateKey.keys[0],
|
||||
publicKeys: publicKey.keys
|
||||
};
|
||||
return openpgp.encrypt(encOpt).then(function(encrypted) {
|
||||
decOpt.message = openpgp.message.readArmored(encrypted.data);
|
||||
return openpgp.decrypt(decOpt);
|
||||
}).then(function(decrypted) {
|
||||
expect(decrypted.data).to.equal('');
|
||||
expect(decrypted.signatures[0].valid).to.be.true;
|
||||
expect(decrypted.signatures[0].keyid.toHex()).to.equal(privateKey.keys[0].getSigningKeyPacket().getKeyId().toHex());
|
||||
expect(decrypted.signatures[0].signature.packets.length).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('should encrypt/sign and decrypt/verify with detached signatures', function() {
|
||||
var encOpt = {
|
||||
data: plaintext,
|
||||
|
@ -1242,7 +1263,7 @@ describe('OpenPGP.js public api tests', function() {
|
|||
throw new Error('Error expected.');
|
||||
})
|
||||
.catch(function(error) {
|
||||
expect(error.message).to.match(/No keys or passwords/);
|
||||
expect(error.message).to.match(/No keys, passwords, or session key provided/);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user