diff options
Diffstat (limited to 'src/lib/pgp/summary.ts')
-rw-r--r-- | src/lib/pgp/summary.ts | 216 |
1 files changed, 111 insertions, 105 deletions
diff --git a/src/lib/pgp/summary.ts b/src/lib/pgp/summary.ts index 5c8a81c..bcd9bc8 100644 --- a/src/lib/pgp/summary.ts +++ b/src/lib/pgp/summary.ts @@ -57,7 +57,7 @@ export type Summary = { result: VerificationResult.MISSING_KEY; reason: Error; keyID: string; - created: Date; + created: Date | null; } | { result: | VerificationResult.SIGNATURE_CORRUPTED @@ -67,11 +67,11 @@ export type Summary = { } | { result: VerificationResult.TRUSTED_KEY; key: PublicKey | Subkey; - created: Date; + created: Date | null; } | { result: VerificationResult.UNTRUSTED_KEY; key: PublicKey | Subkey; - created: Date; + created: Date | null; } | { result: VerificationResult.EXPIRATION_AFTER_SIGNATURE; key: PublicKey | Subkey; @@ -99,7 +99,7 @@ export type Summary = { key: PublicKey | Subkey; }; -export async function createVerificationSummary( +export async function createVerificationsSummary( { dataCorrupted, verifications, signature }: Verification, ): Promise<[NonEmptyArray<Summary>, Map<string, NonEmptyArray<Summary>>]> { if (signature === undefined) { @@ -116,107 +116,7 @@ export async function createVerificationSummary( const summaries = await Promise.all< Promise<[Summary[], Map<string, Summary[]>]>[] - >( - (verifications ?? []).map( - async ({ signatureCorrupted, verified, packet, key }) => { - const errors: Summary[] = []; - const keys: Map<string, Summary[]> = new Map(); - - try { - await verified; - } catch (e) { - if (e instanceof Error) { - if ( - e.message.startsWith("Could not find signing key with key ID") - ) { - const keyID = e.message.slice(e.message.lastIndexOf(" ")); - const key = keys.get(keyID) ?? []; - key.push({ - result: VerificationResult.MISSING_KEY, - keyID, - reason: e, - }); - keys.set(keyID, key); - } else { - errors.push({ - result: VerificationResult.SIGNATURE_COULD_NOT_BE_CHECKED, - reason: e, - }); - } - } else { - throw e; - } - } - - const corrupted = await signatureCorrupted; - if (corrupted[0]) { - errors.push({ - result: VerificationResult.SIGNATURE_CORRUPTED, - reason: corrupted[1], - }); - } - - const sig = await packet; - const keyID = sig.issuerKeyID; - - sig.created; - - const keyAwaited = await key; - - if (keyAwaited === undefined) { - const key = keys.get(keyID.toHex()) ?? []; - key.push({ - result: VerificationResult.MISSING_KEY, - keyID: keyID.toHex(), - reason: new Error( - `Could not find signing key with key ID ${keyID.toHex()}`, - ), - }); - keys.set(keyID.toHex(), key); - - return [errors, keys] as [Summary[], Map<string, Summary[]>]; - } - - const keySummaries = keys.get(keyAwaited.getKeyID().toHex()) ?? []; - const expired = await isKeyExpired(keyAwaited); - - if (expired !== null && sig.created !== null) { - keySummaries.push({ - result: expired <= sig.created - ? VerificationResult.EXPIRATION_BEFORE_SIGNATURE - : VerificationResult.EXPIRATION_AFTER_SIGNATURE, - key: keyAwaited, - date: expired, - }); - } - - const revoked = isKeyRevoked(keyAwaited); - if (revoked?.date !== undefined && sig.created !== null) { - keySummaries.push({ - result: revoked?.date <= sig.created - ? VerificationResult.REVOCATION_BEFORE_SIGNATURE - : VerificationResult.REVOCATION_AFTER_SIGNATURE, - key: keyAwaited, - date: revoked.date, - revocationReason: revoked.reason, - }); - } - - const trust = sig.trustAmount ?? await keyTrust(keyAwaited as Key); - - keySummaries.push({ - result: trust > 0 - ? VerificationResult.TRUSTED_KEY - : VerificationResult.UNTRUSTED_KEY, - key: keyAwaited, - }); - - keys.set(keyAwaited.getKeyID().toHex(), keySummaries); - - return [errors, keys] as [Summary[], Map<string, Summary[]>]; - }, - ), - ); + >((verifications ?? []).map(createVerificationSummary)); const errors = summaries.flatMap(([x]) => x); const keys = new Map(summaries.flatMap(([, x]) => x.entries().toArray())); @@ -230,3 +130,109 @@ export async function createVerificationSummary( throw new Error("unreachable"); } + +export const createVerificationSummary = async ( + { signatureCorrupted, verified, packet, key }: NonNullable< + Verification["verifications"] + >[number], +): Promise<[Summary[], Map<string, Summary[]>]> => { + const errors: Summary[] = []; + const keys: Map<string, Summary[]> = new Map(); + + const sig = await packet; + + try { + await verified; + } catch (e) { + if (e instanceof Error) { + if ( + e.message.startsWith("Could not find signing key with key ID") + ) { + const keyID = e.message.slice(e.message.lastIndexOf(" ")); + const key = keys.get(keyID) ?? []; + key.push({ + result: VerificationResult.MISSING_KEY, + keyID, + reason: e, + created: sig.created, + }); + keys.set(keyID, key); + } else { + errors.push({ + result: VerificationResult.SIGNATURE_COULD_NOT_BE_CHECKED, + reason: e, + }); + } + } else { + throw e; + } + } + + const corrupted = await signatureCorrupted; + if (corrupted[0]) { + errors.push({ + result: VerificationResult.SIGNATURE_CORRUPTED, + reason: corrupted[1], + }); + } + + const keyID = sig.issuerKeyID; + + const keyAwaited = await key; + + if (keyAwaited === undefined) { + const key = keys.get(keyID.toHex()) ?? []; + key.push({ + result: VerificationResult.MISSING_KEY, + keyID: keyID.toHex(), + reason: new Error( + `Could not find signing key with key ID ${keyID.toHex()}`, + ), + created: sig.created, + }); + keys.set(keyID.toHex(), key); + + return [errors, keys] as [Summary[], Map<string, Summary[]>]; + } + + const keySummaries = keys.get(keyAwaited.getKeyID().toHex()) ?? []; + const expired = await isKeyExpired(keyAwaited); + + if (expired !== null && sig.created !== null) { + keySummaries.push({ + result: expired <= sig.created + ? VerificationResult.EXPIRATION_BEFORE_SIGNATURE + : VerificationResult.EXPIRATION_AFTER_SIGNATURE, + key: keyAwaited, + created: sig.created, + expired, + }); + } + + const revoked = isKeyRevoked(keyAwaited); + if (revoked?.date !== undefined && sig.created !== null) { + keySummaries.push({ + result: revoked?.date <= sig.created + ? VerificationResult.REVOCATION_BEFORE_SIGNATURE + : VerificationResult.REVOCATION_AFTER_SIGNATURE, + key: keyAwaited, + created: sig.created, + revoked: revoked.date, + revocationReason: revoked.reason, + }); + } + + const trust = sig.trustAmount ?? await keyTrust(keyAwaited as Key); + + keySummaries.push({ + result: trust > 0 + ? VerificationResult.TRUSTED_KEY + : VerificationResult.UNTRUSTED_KEY, + key: keyAwaited, + created: sig.created, + }); + + keys.set(keyAwaited.getKeyID().toHex(), keySummaries); + + return [errors, keys] as [Summary[], Map<string, Summary[]>]; +}; |