Implement key.validate() (#1028)
This function checks whether the private and public key parameters of the primary key match. This check is necessary when using your own private key to encrypt data if the private key was stored on an untrusted medium, and trust is derived from being able to decrypt the private key.
This commit is contained in:
parent
26502e36cd
commit
8f355a75da
|
@ -392,6 +392,30 @@ Key.prototype.decrypt = async function(passphrases, keyId = null) {
|
|||
return results.every(result => result === true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check whether the private and public key parameters of the primary key match
|
||||
* @returns {Promise<Boolean>} true if the primary key parameters correspond
|
||||
* @async
|
||||
*/
|
||||
Key.prototype.validate = async function() {
|
||||
if (!this.isPrivate()) {
|
||||
throw new Error("Can't validate a public key");
|
||||
}
|
||||
const signingKeyPacket = this.primaryKey;
|
||||
if (!signingKeyPacket.isDecrypted()) {
|
||||
throw new Error("Key is not decrypted");
|
||||
}
|
||||
const data = new packet.Literal();
|
||||
data.setBytes(new Uint8Array(), 'binary');
|
||||
const signature = new packet.Signature();
|
||||
signature.publicKeyAlgorithm = signingKeyPacket.algorithm;
|
||||
signature.hashAlgorithm = enums.hash.sha256;
|
||||
const signatureType = enums.signature.binary;
|
||||
signature.signatureType = signatureType;
|
||||
await signature.sign(signingKeyPacket, data);
|
||||
return signature.verify(signingKeyPacket, signatureType, data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a signature on a key is revoked
|
||||
* @param {module:packet.SecretKey|
|
||||
|
|
|
@ -1739,6 +1739,71 @@ TcIYl5/Uyoi+FOvPLcNw4hOv2nwUzSSVAw==
|
|||
-----END PGP PRIVATE KEY BLOCK-----
|
||||
`;
|
||||
|
||||
const mismatchingKeyParams = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
||||
Version: OpenPGP.js v4.7.0
|
||||
Comment: https://openpgpjs.org
|
||||
|
||||
xcMGBF3ey50BCADaTsujZxXLCYBeaGd9qXqHc+oWtQF2BZdYWPvguljrYgrK
|
||||
WwyoFy8cHaQyi3OTccFXVhFNDG+TgYUG9nk/jvsgOKiu4HugJR5/UPXapBwp
|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BRsKqJdVvwf/UICpq9O09uuQ
|
||||
MFKYevMLfEGF896TCe6sKtwpvyU5QX0xlODI554uJhIxUew6HPzafCO9SWfP
|
||||
tas+15nI43pEc0VEnd31g3pqiKSd+PYolw4NfYI0jrcRabebGlGcprvoj2fD
|
||||
C/wSMmcnvJkjFzUoDkRX3bMV1C7birw9C1QYOpEj8c0KGIsiVI45sGwFlclD
|
||||
AxMSJy5Dv9gcVPq6V8fuPw05ODSpbieoIF3d3WuaI39lAZpfuhNaSNAQmzA7
|
||||
6os1UTIywR2rDFRWbh2IrviZ9BVkV6NXa9+gT+clr3PsE4XeADacVAa2MZNR
|
||||
0NubenKyljKtyHyoU+S+TqUyx7gf5A==
|
||||
=Lj9k
|
||||
-----END PGP PRIVATE KEY BLOCK-----
|
||||
`;
|
||||
|
||||
function versionSpecificTests() {
|
||||
it('Preferences of generated key', function() {
|
||||
const testPref = function(key) {
|
||||
|
@ -2522,6 +2587,17 @@ describe('Key', function() {
|
|||
expect(encryptExpirationTime).to.equal(Infinity);
|
||||
});
|
||||
|
||||
it('validate() - return true if key parameters correspond', async function() {
|
||||
const { key } = await openpgp.generateKey({ userIds: {}, curve: 'ed25519' });
|
||||
expect(await key.validate()).to.be.true;
|
||||
});
|
||||
|
||||
it('validate() - return false if key parameters do not correspond', async function() {
|
||||
const { keys: [key] } = await openpgp.key.readArmored(mismatchingKeyParams);
|
||||
await key.decrypt('userpass');
|
||||
expect(await key.validate()).to.be.false;
|
||||
});
|
||||
|
||||
it('update() - throw error if fingerprints not equal', async function() {
|
||||
const keys = (await openpgp.key.readArmored(twoKeys)).keys;
|
||||
await expect(keys[0].update.bind(
|
||||
|
|
Loading…
Reference in New Issue
Block a user