summaryrefslogtreecommitdiff
path: root/src/lib/pgp/index.ts
diff options
context:
space:
mode:
authorJoão Augusto Costa Branco Marado Torres <torres.dev@disroot.org>2025-06-24 12:08:41 -0300
committerJoão Augusto Costa Branco Marado Torres <torres.dev@disroot.org>2025-06-24 12:50:43 -0300
commitf9a77c5c27aede4e5978eb55d9b7af781b680a1d (patch)
treed545e325ba1ae756fc2eac66fac1001b6753c40d /src/lib/pgp/index.ts
feat!: initial commit
Signed-off-by: João Augusto Costa Branco Marado Torres <torres.dev@disroot.org>
Diffstat (limited to 'src/lib/pgp/index.ts')
-rw-r--r--src/lib/pgp/index.ts63
1 files changed, 63 insertions, 0 deletions
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<Date | null> {
+ 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;