Replace get(Sub)KeyPackets with get(Sub)Keys
This commit is contained in:
parent
91b7165b78
commit
15e6f0d654
38
src/key.js
38
src/key.js
|
@ -155,31 +155,30 @@ Key.prototype.toPacketlist = function() {
|
||||||
* Returns packetlist containing all public or private subkey packets matching keyId;
|
* Returns packetlist containing all public or private subkey packets matching keyId;
|
||||||
* If keyId is not present, returns all subkey packets.
|
* If keyId is not present, returns all subkey packets.
|
||||||
* @param {type/keyid} keyId
|
* @param {type/keyid} keyId
|
||||||
* @returns {module:packet.List}
|
* @returns {Array<SubKey>}
|
||||||
*/
|
*/
|
||||||
Key.prototype.getSubkeyPackets = function(keyId=null) {
|
Key.prototype.getSubkeys = function(keyId=null) {
|
||||||
const packets = new packet.List();
|
const subKeys = [];
|
||||||
this.subKeys.forEach(subKey => {
|
this.subKeys.forEach(subKey => {
|
||||||
if (!keyId || subKey.getKeyId().equals(keyId, true)) {
|
if (!keyId || subKey.getKeyId().equals(keyId, true)) {
|
||||||
packets.push(subKey.keyPacket);
|
subKeys.push(subKey);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return packets;
|
return subKeys;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a packetlist containing all public or private key packets matching keyId.
|
* Returns a packetlist containing all public or private key packets matching keyId.
|
||||||
* If keyId is not present, returns all key packets starting with the primary key.
|
* If keyId is not present, returns all key packets starting with the primary key.
|
||||||
* @param {type/keyid} keyId
|
* @param {type/keyid} keyId
|
||||||
* @returns {module:packet.List}
|
* @returns {Array<Key|SubKey>}
|
||||||
*/
|
*/
|
||||||
Key.prototype.getKeyPackets = function(keyId=null) {
|
Key.prototype.getKeys = function(keyId=null) {
|
||||||
const packets = new packet.List();
|
const keys = [];
|
||||||
if (!keyId || this.getKeyId().equals(keyId, true)) {
|
if (!keyId || this.getKeyId().equals(keyId, true)) {
|
||||||
packets.push(this.keyPacket);
|
keys.push(this);
|
||||||
}
|
}
|
||||||
packets.concat(this.getSubkeyPackets(keyId));
|
return keys.concat(this.getSubkeys(keyId));
|
||||||
return packets;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -187,7 +186,7 @@ Key.prototype.getKeyPackets = function(keyId=null) {
|
||||||
* @returns {Array<module:type/keyid>}
|
* @returns {Array<module:type/keyid>}
|
||||||
*/
|
*/
|
||||||
Key.prototype.getKeyIds = function() {
|
Key.prototype.getKeyIds = function() {
|
||||||
return this.getKeyPackets().map(keyPacket => keyPacket.getKeyId());
|
return this.getKeys().map(key => key.getKeyId());
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -374,13 +373,14 @@ Key.prototype.encrypt = async function(passphrases, keyId=null) {
|
||||||
throw new Error("Nothing to encrypt in a public key");
|
throw new Error("Nothing to encrypt in a public key");
|
||||||
}
|
}
|
||||||
|
|
||||||
const keyPackets = this.getKeyPackets(keyId);
|
const keys = this.getKeys(keyId);
|
||||||
passphrases = util.isArray(passphrases) ? passphrases : new Array(keyPackets.length).fill(passphrases);
|
passphrases = util.isArray(passphrases) ? passphrases : new Array(keys.length).fill(passphrases);
|
||||||
if (passphrases.length !== keyPackets.length) {
|
if (passphrases.length !== keys.length) {
|
||||||
throw new Error("Invalid number of passphrases for key");
|
throw new Error("Invalid number of passphrases for key");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(keyPackets.map(async function(keyPacket, i) {
|
return Promise.all(keys.map(async function(key, i) {
|
||||||
|
const { keyPacket } = key;
|
||||||
await keyPacket.encrypt(passphrases[i]);
|
await keyPacket.encrypt(passphrases[i]);
|
||||||
keyPacket.clearPrivateParams();
|
keyPacket.clearPrivateParams();
|
||||||
return keyPacket;
|
return keyPacket;
|
||||||
|
@ -400,12 +400,12 @@ Key.prototype.decrypt = async function(passphrases, keyId=null) {
|
||||||
}
|
}
|
||||||
passphrases = util.isArray(passphrases) ? passphrases : [passphrases];
|
passphrases = util.isArray(passphrases) ? passphrases : [passphrases];
|
||||||
|
|
||||||
const results = await Promise.all(this.getKeyPackets(keyId).map(async function(keyPacket) {
|
const results = await Promise.all(this.getKeys(keyId).map(async function(key) {
|
||||||
let decrypted = false;
|
let decrypted = false;
|
||||||
let error = null;
|
let error = null;
|
||||||
await Promise.all(passphrases.map(async function(passphrase) {
|
await Promise.all(passphrases.map(async function(passphrase) {
|
||||||
try {
|
try {
|
||||||
await keyPacket.decrypt(passphrase);
|
await key.keyPacket.decrypt(passphrase);
|
||||||
decrypted = true;
|
decrypted = true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e;
|
error = e;
|
||||||
|
@ -1326,7 +1326,7 @@ export async function reformat(options) {
|
||||||
options = sanitizeKeyOptions(options);
|
options = sanitizeKeyOptions(options);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const isDecrypted = options.privateKey.getKeyPackets().every(keyPacket => keyPacket.isDecrypted());
|
const isDecrypted = options.privateKey.getKeys().every(key => key.isDecrypted());
|
||||||
if (!isDecrypted) {
|
if (!isDecrypted) {
|
||||||
await options.privateKey.decrypt();
|
await options.privateKey.decrypt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,10 +168,10 @@ Message.prototype.decryptSessionKeys = async function(privateKeys, passwords) {
|
||||||
throw new Error('No public key encrypted session key packet found.');
|
throw new Error('No public key encrypted session key packet found.');
|
||||||
}
|
}
|
||||||
await Promise.all(pkESKeyPacketlist.map(async function(keyPacket) {
|
await Promise.all(pkESKeyPacketlist.map(async function(keyPacket) {
|
||||||
// TODO improve this
|
const privateKeyPackets = new packet.List();
|
||||||
const privateKeyPackets = privateKeys.reduce(function(acc, privateKey) {
|
privateKeys.forEach(privateKey => {
|
||||||
return acc.concat(privateKey.getKeyPackets(keyPacket.publicKeyId));
|
privateKeyPackets.concat(privateKey.getKeys(keyPacket.publicKeyId).map(key => key.keyPacket));
|
||||||
}, new packet.List());
|
});
|
||||||
await Promise.all(privateKeyPackets.map(async function(privateKeyPacket) {
|
await Promise.all(privateKeyPackets.map(async function(privateKeyPacket) {
|
||||||
if (!privateKeyPacket) {
|
if (!privateKeyPacket) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1362,7 +1362,7 @@ t/ia1kMpSEiOVLlX5dfHZzhR3WNtBqU=
|
||||||
expect(newKeyId.toHex()).to.equal(keyId.toHex());
|
expect(newKeyId.toHex()).to.equal(keyId.toHex());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Testing key method getSubkeyPackets', function(done) {
|
it('Testing key method getSubkeys', function(done) {
|
||||||
const pubKeys = openpgp.key.readArmored(pub_sig_test);
|
const pubKeys = openpgp.key.readArmored(pub_sig_test);
|
||||||
|
|
||||||
expect(pubKeys).to.exist;
|
expect(pubKeys).to.exist;
|
||||||
|
@ -1376,7 +1376,7 @@ t/ia1kMpSEiOVLlX5dfHZzhR3WNtBqU=
|
||||||
|
|
||||||
packetlist.read(openpgp.armor.decode(pub_sig_test).data);
|
packetlist.read(openpgp.armor.decode(pub_sig_test).data);
|
||||||
|
|
||||||
const subkeys = pubKey.getSubkeyPackets();
|
const subkeys = pubKey.getSubkeys();
|
||||||
expect(subkeys).to.exist;
|
expect(subkeys).to.exist;
|
||||||
expect(subkeys).to.have.length(2);
|
expect(subkeys).to.have.length(2);
|
||||||
expect(subkeys[0].getKeyId().equals(packetlist[8].getKeyId())).to.be.true;
|
expect(subkeys[0].getKeyId().equals(packetlist[8].getKeyId())).to.be.true;
|
||||||
|
|
|
@ -520,8 +520,8 @@ describe("Signature", function() {
|
||||||
|
|
||||||
const keyids = sMsg.getSigningKeyIds();
|
const keyids = sMsg.getSigningKeyIds();
|
||||||
|
|
||||||
expect(pubKey2.getKeyPackets(keyids[1])).to.not.be.empty;
|
expect(pubKey2.getKeys(keyids[1])).to.not.be.empty;
|
||||||
expect(pubKey3.getKeyPackets(keyids[0])).to.not.be.empty;
|
expect(pubKey3.getKeys(keyids[0])).to.not.be.empty;
|
||||||
|
|
||||||
expect(sMsg.getText()).to.equal(plaintext);
|
expect(sMsg.getText()).to.equal(plaintext);
|
||||||
|
|
||||||
|
@ -566,8 +566,8 @@ describe("Signature", function() {
|
||||||
|
|
||||||
const keyids = csMsg.getSigningKeyIds();
|
const keyids = csMsg.getSigningKeyIds();
|
||||||
|
|
||||||
expect(pubKey2.getKeyPackets(keyids[0])).to.not.be.empty;
|
expect(pubKey2.getKeys(keyids[0])).to.not.be.empty;
|
||||||
expect(pubKey3.getKeyPackets(keyids[1])).to.not.be.empty;
|
expect(pubKey3.getKeys(keyids[1])).to.not.be.empty;
|
||||||
|
|
||||||
return openpgp.verify({ publicKeys:[pubKey2, pubKey3], message:csMsg }).then(function(cleartextSig) {
|
return openpgp.verify({ publicKeys:[pubKey2, pubKey3], message:csMsg }).then(function(cleartextSig) {
|
||||||
expect(cleartextSig).to.exist;
|
expect(cleartextSig).to.exist;
|
||||||
|
@ -607,7 +607,7 @@ zmuVOdNuWQqxT9Sqa84=
|
||||||
|
|
||||||
const keyids = csMsg.getSigningKeyIds();
|
const keyids = csMsg.getSigningKeyIds();
|
||||||
|
|
||||||
expect(pubKey.getKeyPackets(keyids[0])).to.not.be.empty;
|
expect(pubKey.getKeys(keyids[0])).to.not.be.empty;
|
||||||
|
|
||||||
return openpgp.verify({ publicKeys:[pubKey], message:csMsg }).then(function(cleartextSig) {
|
return openpgp.verify({ publicKeys:[pubKey], message:csMsg }).then(function(cleartextSig) {
|
||||||
expect(cleartextSig).to.exist;
|
expect(cleartextSig).to.exist;
|
||||||
|
@ -638,7 +638,7 @@ yYDnCgA=
|
||||||
|
|
||||||
const keyids = sMsg.getSigningKeyIds();
|
const keyids = sMsg.getSigningKeyIds();
|
||||||
|
|
||||||
expect(pubKey.getKeyPackets(keyids[0])).to.not.be.empty;
|
expect(pubKey.getKeys(keyids[0])).to.not.be.empty;
|
||||||
|
|
||||||
return openpgp.verify({ publicKeys:[pubKey], message:sMsg }).then(function(cleartextSig) {
|
return openpgp.verify({ publicKeys:[pubKey], message:sMsg }).then(function(cleartextSig) {
|
||||||
expect(cleartextSig).to.exist;
|
expect(cleartextSig).to.exist;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user