Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniform casing of subkey(s): rename Key.subKeys to Key.subkeys #1310

Merged
merged 2 commits into from Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions openpgp.d.ts
Expand Up @@ -25,7 +25,7 @@ export function reformatKey(options: { privateKey: PrivateKey; userIDs?: UserID|

export abstract class Key {
private keyPacket: PublicKeyPacket | SecretKeyPacket;
public subKeys: SubKey[];
public subkeys: Subkey[];
public users: User[];
public revocationSignatures: SignaturePacket[];
public write(): Uint8Array;
Expand All @@ -45,10 +45,10 @@ export abstract class Key {
public verifyAllUsers(publicKeys: PublicKey[], date?: Date, config?: Config): Promise<{ userID: string, keyID: KeyID, valid: boolean | null }[]>;
public isRevoked(signature: SignaturePacket, key?: AnyKeyPacket, date?: Date, config?: Config): Promise<boolean>;
public getRevocationCertificate(date?: Date, config?: Config): Promise<Stream<string> | string | undefined>;
public getEncryptionKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<PublicKey | SubKey>;
public getSigningKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<PublicKey | SubKey>;
public getKeys(keyID?: KeyID): (PublicKey | SubKey)[];
public getSubkeys(keyID?: KeyID): SubKey[];
public getEncryptionKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<PublicKey | Subkey>;
public getSigningKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<PublicKey | Subkey>;
public getKeys(keyID?: KeyID): (PublicKey | Subkey)[];
public getSubkeys(keyID?: KeyID): Subkey[];
public getFingerprint(): string;
public getCreationTime(): Date;
public getAlgorithmInfo(): AlgorithmInfo;
Expand All @@ -65,14 +65,14 @@ export class PrivateKey extends PublicKey {
constructor(packetlist: PacketList<AnyKeyPacket>);
public revoke(reason: { flag?: enums.reasonForRevocation; string?: string; }, date?: Date, config?: Config): Promise<PrivateKey>;
public isDecrypted(): boolean;
public addSubkey(options: SubKeyOptions): Promise<PrivateKey>;
public getDecryptionKeys(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<PrivateKey | SubKey>
public addSubkey(options: SubkeyOptions): Promise<PrivateKey>;
public getDecryptionKeys(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<PrivateKey | Subkey>
public update(sourceKey: PublicKey, date?: Date, config?: Config): Promise<PrivateKey>;
public getKeys(keyID?: KeyID): (PrivateKey | SubKey)[];
public getKeys(keyID?: KeyID): (PrivateKey | Subkey)[];
}

export class SubKey {
constructor(subKeyPacket: SecretSubkeyPacket | PublicSubkeyPacket, mainKey: PublicKey);
export class Subkey {
constructor(subkeyPacket: SecretSubkeyPacket | PublicSubkeyPacket, mainKey: PublicKey);
private keyPacket: SecretSubkeyPacket | PublicSubkeyPacket;
private mainKey: PublicKey;
public bindingSignatures: SignaturePacket[];
Expand Down Expand Up @@ -634,11 +634,11 @@ interface KeyOptions {
rsaBits?: number;
keyExpirationTime?: number;
date?: Date;
subkeys?: SubKeyOptions[];
subkeys?: SubkeyOptions[];
config?: PartialConfig;
}

interface SubKeyOptions {
interface SubkeyOptions {
type?: 'ecc' | 'rsa';
curve?: EllipticCurveName;
rsaBits?: number;
Expand Down
4 changes: 2 additions & 2 deletions src/key/factory.js
Expand Up @@ -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 (
Expand All @@ -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');
}
Expand Down
86 changes: 43 additions & 43 deletions src/key/key.js
Expand Up @@ -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';
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -227,35 +227,35 @@ 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) {
throw new Error('Missing embedded signature');
}
// 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;
}
Expand All @@ -281,25 +281,25 @@ 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
*/
async getEncryptionKey(keyID, date = new Date(), userID = {}, config = defaultConfig) {
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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -514,17 +514,17 @@ 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) {
await Promise.all(
subkeysToUpdate.map(subkeyToUpdate => subkeyToUpdate.update(srcSubkey, date, config))
);
} else {
updatedKey.subKeys.push(srcSubkey);
updatedKey.subkeys.push(srcSubkey);
}
}));

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/key/private_key.js
Expand Up @@ -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) {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/key/public_key.js
Expand Up @@ -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) {
Expand Down