summaryrefslogtreecommitdiff
path: root/src/lib/pgp/index.ts
blob: 8142732deb42394c0fbad1e3143aa2de6af204a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;