openpgp.reformatKey: Fix key preferences for signing subkeys

This commit is contained in:
Daniel Huigens 2019-05-23 17:25:01 +02:00
parent 72e3b491d7
commit 43441bfe0d
3 changed files with 28 additions and 6 deletions

View File

@ -1426,7 +1426,10 @@ export async function reformat(options) {
}
if (!options.subkeys) {
options.subkeys = secretSubkeyPackets.map(() => ({}));
options.subkeys = await Promise.all(secretSubkeyPackets.map(async secretSubkeyPacket => ({
sign: await options.privateKey.getSigningKey(secretSubkeyPacket.getKeyId(), null) &&
!await options.privateKey.getEncryptionKey(secretSubkeyPacket.getKeyId(), null)
})));
}
if (options.subkeys.length !== secretSubkeyPackets.length) {

View File

@ -1827,7 +1827,7 @@ function versionSpecificTests() {
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal(userId);
expect(key.users[0].selfCertifications[0].isPrimaryUserID).to.be.true;
expect(key.subKeys).to.have.lengthOf(2);
expect(key.subKeys).to.have.length(2);
expect(key.subKeys[0].getAlgorithmInfo().algorithm).to.equal('ecdh');
expect(key.subKeys[1].getAlgorithmInfo().algorithm).to.equal('ecdh');
});
@ -1836,14 +1836,33 @@ function versionSpecificTests() {
it('Generate key - one signing subkey', function() {
const userId = 'test <a@b.com>';
const opt = {curve: 'curve25519', userIds: [userId], passphrase: '123', subkeys:[{}, {sign: true}]};
return openpgp.generateKey(opt).then(function(key) {
key = key.key;
return openpgp.generateKey(opt).then(async function({ key }) {
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal(userId);
expect(key.users[0].selfCertifications[0].isPrimaryUserID).to.be.true;
expect(key.subKeys).to.have.lengthOf(2);
expect(key.subKeys).to.have.length(2);
expect(key.subKeys[0].getAlgorithmInfo().algorithm).to.equal('ecdh');
expect(await key.getEncryptionKey()).to.equal(key.subKeys[0]);
expect(key.subKeys[1].getAlgorithmInfo().algorithm).to.equal('eddsa');
expect(await key.getSigningKey()).to.equal(key.subKeys[1]);
});
});
it('Reformat key - one signing subkey', function() {
const userId = 'test <a@b.com>';
const opt = {curve: 'curve25519', userIds: [userId], passphrase: '123', subkeys:[{}, {sign: true}]};
return openpgp.generateKey(opt).then(async function({ key }) {
await key.decrypt('123');
return openpgp.reformatKey({ privateKey: key, userIds: [userId] });
}).then(async function({ key }) {
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal(userId);
expect(key.users[0].selfCertifications[0].isPrimaryUserID).to.be.true;
expect(key.subKeys).to.have.length(2);
expect(key.subKeys[0].getAlgorithmInfo().algorithm).to.equal('ecdh');
expect(await key.getEncryptionKey()).to.equal(key.subKeys[0]);
expect(key.subKeys[1].getAlgorithmInfo().algorithm).to.equal('eddsa');
expect(await key.getSigningKey()).to.equal(key.subKeys[1]);
});
});

View File

@ -32,7 +32,7 @@ describe('WKD unit tests', function() {
}).then(function(key) {
expect(key).to.exist;
expect(key).to.have.property('keys');
expect(key.keys).to.have.lengthOf(1);
expect(key.keys).to.have.length(1);
});
});