From f9a77c5c27aede4e5978eb55d9b7af781b680a1d Mon Sep 17 00:00:00 2001 From: João Augusto Costa Branco Marado Torres Date: Tue, 24 Jun 2025 12:08:41 -0300 Subject: feat!: initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Augusto Costa Branco Marado Torres --- src/lib/pgp/index.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/lib/pgp/index.ts (limited to 'src/lib/pgp/index.ts') diff --git a/src/lib/pgp/index.ts b/src/lib/pgp/index.ts new file mode 100644 index 0000000..8142732 --- /dev/null +++ b/src/lib/pgp/index.ts @@ -0,0 +1,63 @@ +import { enums, PublicKey, type Subkey } from "openpgp"; + +export async function isKeyExpired( + key: PublicKey | Subkey, +): Promise { + const keyExpiration = await key.getExpirationTime(); + + return typeof keyExpiration === "number" + ? new Date(keyExpiration) + : keyExpiration; +} + +export type RevocationReason = { flag?: string; msg?: string }; +export type Revocation = { date: Date; reason: RevocationReason }; +export function isKeyRevoked( + key: PublicKey | Subkey, +): Revocation | undefined { + const revokes = key.revocationSignatures.map(( + { created, reasonForRevocationFlag, reasonForRevocationString }, + ) => ({ created, reasonForRevocationFlag, reasonForRevocationString })); + let keyRevocation: Revocation | undefined = undefined; + for (const i of revokes) { + const unix = i.created?.getTime(); + if (unix === undefined) { + continue; + } + const date = new Date(unix); + if (keyRevocation === undefined || unix < keyRevocation.date.getTime()) { + let flag = undefined; + switch (i.reasonForRevocationFlag) { + case enums.reasonForRevocation.noReason: { + flag = "No reason specified (key revocations or cert revocations)"; + break; + } + case enums.reasonForRevocation.keySuperseded: { + flag = "Key is superseded (key revocations)"; + break; + } + case enums.reasonForRevocation.keyCompromised: { + flag = "Key material has been compromised (key revocations)"; + break; + } + case enums.reasonForRevocation.keyRetired: { + flag = "Key is retired and no longer used (key revocations)"; + break; + } + case enums.reasonForRevocation.userIDInvalid: { + flag = "User ID information is no longer valid (cert revocations)"; + break; + } + } + keyRevocation = { + date, + reason: { msg: i.reasonForRevocationString ?? undefined, flag }, + }; + } + } + + return keyRevocation; +} + +export const toPK = (key: PublicKey | Subkey): PublicKey => + key instanceof PublicKey ? key : key.mainKey; -- cgit v1.2.3