mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2026-03-18 22:28:56 +00:00
Uniform casing of subkey(s): rename Key.subKeys to Key.subkeys (#1310)
Also, rename `SubKey` class to `Subkey`
This commit is contained in:
@@ -106,7 +106,7 @@ export async function reformat(options, config) {
|
||||
const secretKeyPacket = privateKey.keyPacket;
|
||||
|
||||
if (!options.subkeys) {
|
||||
options.subkeys = await Promise.all(privateKey.subKeys.map(async subkey => {
|
||||
options.subkeys = await Promise.all(privateKey.subkeys.map(async subkey => {
|
||||
const secretSubkeyPacket = subkey.keyPacket;
|
||||
const dataToVerify = { key: secretKeyPacket, bind: secretSubkeyPacket };
|
||||
const bindingSignature = await (
|
||||
@@ -118,7 +118,7 @@ export async function reformat(options, config) {
|
||||
}));
|
||||
}
|
||||
|
||||
const secretSubkeyPackets = privateKey.subKeys.map(subkey => subkey.keyPacket);
|
||||
const secretSubkeyPackets = privateKey.subkeys.map(subkey => subkey.keyPacket);
|
||||
if (options.subkeys.length !== secretSubkeyPackets.length) {
|
||||
throw new Error('Number of subkey options does not match number of subkeys');
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import defaultConfig from '../config';
|
||||
import enums from '../enums';
|
||||
import util from '../util';
|
||||
import User from './user';
|
||||
import SubKey from './subkey';
|
||||
import Subkey from './subkey';
|
||||
import * as helper from './helper';
|
||||
import PrivateKey from './private_key';
|
||||
import PublicKey from './public_key';
|
||||
@@ -50,7 +50,7 @@ class Key {
|
||||
packetListToStructure(packetlist, disallowedPackets = new Set()) {
|
||||
let user;
|
||||
let primaryKeyID;
|
||||
let subKey;
|
||||
let subkey;
|
||||
for (const packet of packetlist) {
|
||||
const tag = packet.constructor.tag;
|
||||
if (disallowedPackets.has(tag)) {
|
||||
@@ -76,8 +76,8 @@ class Key {
|
||||
case enums.packet.publicSubkey:
|
||||
case enums.packet.secretSubkey:
|
||||
user = null;
|
||||
subKey = new SubKey(packet, this);
|
||||
this.subKeys.push(subKey);
|
||||
subkey = new Subkey(packet, this);
|
||||
this.subkeys.push(subkey);
|
||||
break;
|
||||
case enums.packet.signature:
|
||||
switch (packet.signatureType) {
|
||||
@@ -106,21 +106,21 @@ class Key {
|
||||
this.directSignatures.push(packet);
|
||||
break;
|
||||
case enums.signature.subkeyBinding:
|
||||
if (!subKey) {
|
||||
if (!subkey) {
|
||||
util.printDebug('Dropping subkey binding signature without preceding subkey packet');
|
||||
continue;
|
||||
}
|
||||
subKey.bindingSignatures.push(packet);
|
||||
subkey.bindingSignatures.push(packet);
|
||||
break;
|
||||
case enums.signature.keyRevocation:
|
||||
this.revocationSignatures.push(packet);
|
||||
break;
|
||||
case enums.signature.subkeyRevocation:
|
||||
if (!subKey) {
|
||||
if (!subkey) {
|
||||
util.printDebug('Dropping subkey revocation signature without preceding subkey packet');
|
||||
continue;
|
||||
}
|
||||
subKey.revocationSignatures.push(packet);
|
||||
subkey.revocationSignatures.push(packet);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@@ -138,7 +138,7 @@ class Key {
|
||||
packetlist.push(...this.revocationSignatures);
|
||||
packetlist.push(...this.directSignatures);
|
||||
this.users.map(user => packetlist.push(...user.toPacketList()));
|
||||
this.subKeys.map(subKey => packetlist.push(...subKey.toPacketList()));
|
||||
this.subkeys.map(subkey => packetlist.push(...subkey.toPacketList()));
|
||||
return packetlist;
|
||||
}
|
||||
|
||||
@@ -172,20 +172,20 @@ class Key {
|
||||
* Returns an array containing all public or private subkeys matching keyID;
|
||||
* If no keyID is given, returns all subkeys.
|
||||
* @param {type/keyID} [keyID] - key ID to look for
|
||||
* @returns {Array<SubKey>} array of subkeys
|
||||
* @returns {Array<Subkey>} array of subkeys
|
||||
*/
|
||||
getSubkeys(keyID = null) {
|
||||
const subKeys = this.subKeys.filter(subKey => (
|
||||
!keyID || subKey.getKeyID().equals(keyID, true)
|
||||
const subkeys = this.subkeys.filter(subkey => (
|
||||
!keyID || subkey.getKeyID().equals(keyID, true)
|
||||
));
|
||||
return subKeys;
|
||||
return subkeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all public or private keys matching keyID.
|
||||
* If no keyID is given, returns all keys, starting with the primary key.
|
||||
* @param {type/keyid~KeyID} [keyID] - key ID to look for
|
||||
* @returns {Array<Key|SubKey>} array of keys
|
||||
* @returns {Array<Key|Subkey>} array of keys
|
||||
*/
|
||||
getKeys(keyID = null) {
|
||||
const keys = [];
|
||||
@@ -227,24 +227,24 @@ class Key {
|
||||
* @param {Date} [date] - use the fiven date date to to check key validity instead of the current date
|
||||
* @param {Object} [userID] - filter keys for the given user ID
|
||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||
* @returns {Promise<Key|SubKey>} signing key
|
||||
* @returns {Promise<Key|Subkey>} signing key
|
||||
* @throws if no valid signing key was found
|
||||
* @async
|
||||
*/
|
||||
async getSigningKey(keyID = null, date = new Date(), userID = {}, config = defaultConfig) {
|
||||
await this.verifyPrimaryKey(date, userID, config);
|
||||
const primaryKey = this.keyPacket;
|
||||
const subKeys = this.subKeys.slice().sort((a, b) => b.keyPacket.created - a.keyPacket.created);
|
||||
const subkeys = this.subkeys.slice().sort((a, b) => b.keyPacket.created - a.keyPacket.created);
|
||||
let exception;
|
||||
for (const subKey of subKeys) {
|
||||
if (!keyID || subKey.getKeyID().equals(keyID)) {
|
||||
for (const subkey of subkeys) {
|
||||
if (!keyID || subkey.getKeyID().equals(keyID)) {
|
||||
try {
|
||||
await subKey.verify(date, config);
|
||||
const dataToVerify = { key: primaryKey, bind: subKey.keyPacket };
|
||||
await subkey.verify(date, config);
|
||||
const dataToVerify = { key: primaryKey, bind: subkey.keyPacket };
|
||||
const bindingSignature = await helper.getLatestValidSignature(
|
||||
subKey.bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config
|
||||
subkey.bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config
|
||||
);
|
||||
if (!helper.isValidSigningKeyPacket(subKey.keyPacket, bindingSignature)) {
|
||||
if (!helper.isValidSigningKeyPacket(subkey.keyPacket, bindingSignature)) {
|
||||
continue;
|
||||
}
|
||||
if (!bindingSignature.embeddedSignature) {
|
||||
@@ -252,10 +252,10 @@ class Key {
|
||||
}
|
||||
// verify embedded signature
|
||||
await helper.getLatestValidSignature(
|
||||
[bindingSignature.embeddedSignature], subKey.keyPacket, enums.signature.keyBinding, dataToVerify, date, config
|
||||
[bindingSignature.embeddedSignature], subkey.keyPacket, enums.signature.keyBinding, dataToVerify, date, config
|
||||
);
|
||||
helper.checkKeyStrength(subKey.keyPacket, config);
|
||||
return subKey;
|
||||
helper.checkKeyStrength(subkey.keyPacket, config);
|
||||
return subkey;
|
||||
} catch (e) {
|
||||
exception = e;
|
||||
}
|
||||
@@ -281,7 +281,7 @@ class Key {
|
||||
* @param {Date} [date] - use the fiven date date to to check key validity instead of the current date
|
||||
* @param {Object} [userID] - filter keys for the given user ID
|
||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||
* @returns {Promise<Key|SubKey>} encryption key
|
||||
* @returns {Promise<Key|Subkey>} encryption key
|
||||
* @throws if no valid encryption key was found
|
||||
* @async
|
||||
*/
|
||||
@@ -289,17 +289,17 @@ class Key {
|
||||
await this.verifyPrimaryKey(date, userID, config);
|
||||
const primaryKey = this.keyPacket;
|
||||
// V4: by convention subkeys are preferred for encryption service
|
||||
const subKeys = this.subKeys.slice().sort((a, b) => b.keyPacket.created - a.keyPacket.created);
|
||||
const subkeys = this.subkeys.slice().sort((a, b) => b.keyPacket.created - a.keyPacket.created);
|
||||
let exception;
|
||||
for (const subKey of subKeys) {
|
||||
if (!keyID || subKey.getKeyID().equals(keyID)) {
|
||||
for (const subkey of subkeys) {
|
||||
if (!keyID || subkey.getKeyID().equals(keyID)) {
|
||||
try {
|
||||
await subKey.verify(date, config);
|
||||
const dataToVerify = { key: primaryKey, bind: subKey.keyPacket };
|
||||
const bindingSignature = await helper.getLatestValidSignature(subKey.bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
||||
if (helper.isValidEncryptionKeyPacket(subKey.keyPacket, bindingSignature)) {
|
||||
helper.checkKeyStrength(subKey.keyPacket, config);
|
||||
return subKey;
|
||||
await subkey.verify(date, config);
|
||||
const dataToVerify = { key: primaryKey, bind: subkey.keyPacket };
|
||||
const bindingSignature = await helper.getLatestValidSignature(subkey.bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
||||
if (helper.isValidEncryptionKeyPacket(subkey.keyPacket, bindingSignature)) {
|
||||
helper.checkKeyStrength(subkey.keyPacket, config);
|
||||
return subkey;
|
||||
}
|
||||
} catch (e) {
|
||||
exception = e;
|
||||
@@ -474,10 +474,10 @@ class Key {
|
||||
}
|
||||
if (this.isPublic() && sourceKey.isPrivate()) {
|
||||
// check for equal subkey packets
|
||||
const equal = (this.subKeys.length === sourceKey.subKeys.length) &&
|
||||
(this.subKeys.every(destSubKey => {
|
||||
return sourceKey.subKeys.some(srcSubKey => {
|
||||
return destSubKey.hasSameFingerprintAs(srcSubKey);
|
||||
const equal = (this.subkeys.length === sourceKey.subkeys.length) &&
|
||||
(this.subkeys.every(destSubkey => {
|
||||
return sourceKey.subkeys.some(srcSubkey => {
|
||||
return destSubkey.hasSameFingerprintAs(srcSubkey);
|
||||
});
|
||||
}));
|
||||
if (!equal) {
|
||||
@@ -514,9 +514,9 @@ class Key {
|
||||
}
|
||||
}));
|
||||
// update subkeys
|
||||
await Promise.all(sourceKey.subKeys.map(async srcSubkey => {
|
||||
await Promise.all(sourceKey.subkeys.map(async srcSubkey => {
|
||||
// multiple subkeys with same fingerprint might be preset
|
||||
const subkeysToUpdate = updatedKey.subKeys.filter(dstSubkey => (
|
||||
const subkeysToUpdate = updatedKey.subkeys.filter(dstSubkey => (
|
||||
dstSubkey.hasSameFingerprintAs(srcSubkey)
|
||||
));
|
||||
if (subkeysToUpdate.length > 0) {
|
||||
@@ -524,7 +524,7 @@ class Key {
|
||||
subkeysToUpdate.map(subkeyToUpdate => subkeyToUpdate.update(srcSubkey, date, config))
|
||||
);
|
||||
} else {
|
||||
updatedKey.subKeys.push(srcSubkey);
|
||||
updatedKey.subkeys.push(srcSubkey);
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -667,7 +667,7 @@ class Key {
|
||||
|
||||
['getKeyID', 'getFingerprint', 'getAlgorithmInfo', 'getCreationTime', 'hasSameFingerprintAs'].forEach(name => {
|
||||
Key.prototype[name] =
|
||||
SubKey.prototype[name];
|
||||
Subkey.prototype[name];
|
||||
});
|
||||
|
||||
export default Key;
|
||||
|
||||
@@ -84,19 +84,19 @@ class PrivateKey extends PublicKey {
|
||||
* @param {Date} date, optional
|
||||
* @param {String} userID, optional
|
||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||
* @returns {Promise<Array<Key|SubKey>>} Array of decryption keys.
|
||||
* @returns {Promise<Array<Key|Subkey>>} Array of decryption keys.
|
||||
* @async
|
||||
*/
|
||||
async getDecryptionKeys(keyID, date = new Date(), userID = {}, config = defaultConfig) {
|
||||
const primaryKey = this.keyPacket;
|
||||
const keys = [];
|
||||
for (let i = 0; i < this.subKeys.length; i++) {
|
||||
if (!keyID || this.subKeys[i].getKeyID().equals(keyID, true)) {
|
||||
for (let i = 0; i < this.subkeys.length; i++) {
|
||||
if (!keyID || this.subkeys[i].getKeyID().equals(keyID, true)) {
|
||||
try {
|
||||
const dataToVerify = { key: primaryKey, bind: this.subKeys[i].keyPacket };
|
||||
const bindingSignature = await helper.getLatestValidSignature(this.subKeys[i].bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
||||
const dataToVerify = { key: primaryKey, bind: this.subkeys[i].keyPacket };
|
||||
const bindingSignature = await helper.getLatestValidSignature(this.subkeys[i].bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
||||
if (helper.isValidDecryptionKeyPacket(bindingSignature, config)) {
|
||||
keys.push(this.subKeys[i]);
|
||||
keys.push(this.subkeys[i]);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class PublicKey extends Key {
|
||||
this.revocationSignatures = [];
|
||||
this.directSignatures = [];
|
||||
this.users = [];
|
||||
this.subKeys = [];
|
||||
this.subkeys = [];
|
||||
if (packetlist) {
|
||||
this.packetListToStructure(packetlist, new Set([enums.packet.secretKey, enums.packet.secretSubkey]));
|
||||
if (!this.keyPacket) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @module key/SubKey
|
||||
* @module key/Subkey
|
||||
* @private
|
||||
*/
|
||||
|
||||
@@ -10,20 +10,20 @@ import defaultConfig from '../config';
|
||||
|
||||
/**
|
||||
* Class that represents a subkey packet and the relevant signatures.
|
||||
* @borrows PublicSubkeyPacket#getKeyID as SubKey#getKeyID
|
||||
* @borrows PublicSubkeyPacket#getFingerprint as SubKey#getFingerprint
|
||||
* @borrows PublicSubkeyPacket#hasSameFingerprintAs as SubKey#hasSameFingerprintAs
|
||||
* @borrows PublicSubkeyPacket#getAlgorithmInfo as SubKey#getAlgorithmInfo
|
||||
* @borrows PublicSubkeyPacket#getCreationTime as SubKey#getCreationTime
|
||||
* @borrows PublicSubkeyPacket#isDecrypted as SubKey#isDecrypted
|
||||
* @borrows PublicSubkeyPacket#getKeyID as Subkey#getKeyID
|
||||
* @borrows PublicSubkeyPacket#getFingerprint as Subkey#getFingerprint
|
||||
* @borrows PublicSubkeyPacket#hasSameFingerprintAs as Subkey#hasSameFingerprintAs
|
||||
* @borrows PublicSubkeyPacket#getAlgorithmInfo as Subkey#getAlgorithmInfo
|
||||
* @borrows PublicSubkeyPacket#getCreationTime as Subkey#getCreationTime
|
||||
* @borrows PublicSubkeyPacket#isDecrypted as Subkey#isDecrypted
|
||||
*/
|
||||
class SubKey {
|
||||
class Subkey {
|
||||
/**
|
||||
* @param {SecretSubkeyPacket|PublicSubkeyPacket} subKeyPacket - subkey packet to hold in the Subkey
|
||||
* @param {SecretSubkeyPacket|PublicSubkeyPacket} subkeyPacket - subkey packet to hold in the Subkey
|
||||
* @param {Key} mainKey - reference to main Key object, containing the primary key packet corresponding to the subkey
|
||||
*/
|
||||
constructor(subKeyPacket, mainKey) {
|
||||
this.keyPacket = subKeyPacket;
|
||||
constructor(subkeyPacket, mainKey) {
|
||||
this.keyPacket = subkeyPacket;
|
||||
this.bindingSignatures = [];
|
||||
this.revocationSignatures = [];
|
||||
this.mainKey = mainKey;
|
||||
@@ -112,26 +112,26 @@ class SubKey {
|
||||
|
||||
/**
|
||||
* Update subkey with new components from specified subkey
|
||||
* @param {SubKey} subKey - Source subkey to merge
|
||||
* @param {Subkey} subkey - Source subkey to merge
|
||||
* @param {Date} [date] - Date to verify validity of signatures
|
||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||
* @throws {Error} if update failed
|
||||
* @async
|
||||
*/
|
||||
async update(subKey, date = new Date(), config = defaultConfig) {
|
||||
async update(subkey, date = new Date(), config = defaultConfig) {
|
||||
const primaryKey = this.mainKey.keyPacket;
|
||||
if (!this.hasSameFingerprintAs(subKey)) {
|
||||
throw new Error('SubKey update method: fingerprints of subkeys not equal');
|
||||
if (!this.hasSameFingerprintAs(subkey)) {
|
||||
throw new Error('Subkey update method: fingerprints of subkeys not equal');
|
||||
}
|
||||
// key packet
|
||||
if (this.keyPacket.constructor.tag === enums.packet.publicSubkey &&
|
||||
subKey.keyPacket.constructor.tag === enums.packet.secretSubkey) {
|
||||
this.keyPacket = subKey.keyPacket;
|
||||
subkey.keyPacket.constructor.tag === enums.packet.secretSubkey) {
|
||||
this.keyPacket = subkey.keyPacket;
|
||||
}
|
||||
// update missing binding signatures
|
||||
const that = this;
|
||||
const dataToVerify = { key: primaryKey, bind: that.keyPacket };
|
||||
await helper.mergeSignatures(subKey, this, 'bindingSignatures', date, async function(srcBindSig) {
|
||||
await helper.mergeSignatures(subkey, this, 'bindingSignatures', date, async function(srcBindSig) {
|
||||
for (let i = 0; i < that.bindingSignatures.length; i++) {
|
||||
if (that.bindingSignatures[i].issuerKeyID.equals(srcBindSig.issuerKeyID)) {
|
||||
if (srcBindSig.created > that.bindingSignatures[i].created) {
|
||||
@@ -148,7 +148,7 @@ class SubKey {
|
||||
}
|
||||
});
|
||||
// revocation signatures
|
||||
await helper.mergeSignatures(subKey, this, 'revocationSignatures', date, function(srcRevSig) {
|
||||
await helper.mergeSignatures(subkey, this, 'revocationSignatures', date, function(srcRevSig) {
|
||||
return helper.isDataRevoked(primaryKey, enums.signature.subkeyRevocation, dataToVerify, [srcRevSig], undefined, undefined, date, config);
|
||||
});
|
||||
}
|
||||
@@ -161,7 +161,7 @@ class SubKey {
|
||||
* @param {String} reasonForRevocation.string optional, string explaining the reason for revocation
|
||||
* @param {Date} date - optional, override the creationtime of the revocation signature
|
||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
||||
* @returns {Promise<SubKey>} New subkey with revocation signature.
|
||||
* @returns {Promise<Subkey>} New subkey with revocation signature.
|
||||
* @async
|
||||
*/
|
||||
async revoke(
|
||||
@@ -174,14 +174,14 @@ class SubKey {
|
||||
config = defaultConfig
|
||||
) {
|
||||
const dataToSign = { key: primaryKey, bind: this.keyPacket };
|
||||
const subKey = new SubKey(this.keyPacket, this.mainKey);
|
||||
subKey.revocationSignatures.push(await helper.createSignaturePacket(dataToSign, null, primaryKey, {
|
||||
const subkey = new Subkey(this.keyPacket, this.mainKey);
|
||||
subkey.revocationSignatures.push(await helper.createSignaturePacket(dataToSign, null, primaryKey, {
|
||||
signatureType: enums.signature.subkeyRevocation,
|
||||
reasonForRevocationFlag: enums.write(enums.reasonForRevocation, reasonForRevocationFlag),
|
||||
reasonForRevocationString
|
||||
}, date, undefined, false, config));
|
||||
await subKey.update(this);
|
||||
return subKey;
|
||||
await subkey.update(this);
|
||||
return subkey;
|
||||
}
|
||||
|
||||
hasSameFingerprintAs(other) {
|
||||
@@ -190,10 +190,10 @@ class SubKey {
|
||||
}
|
||||
|
||||
['getKeyID', 'getFingerprint', 'getAlgorithmInfo', 'getCreationTime', 'isDecrypted'].forEach(name => {
|
||||
SubKey.prototype[name] =
|
||||
Subkey.prototype[name] =
|
||||
function() {
|
||||
return this.keyPacket[name]();
|
||||
};
|
||||
});
|
||||
|
||||
export default SubKey;
|
||||
export default Subkey;
|
||||
|
||||
Reference in New Issue
Block a user