Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions extension/js/common/core/crypto/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,12 @@ export class KeyUtil {
return keys.map(k => ({ id: k.id, emails: k.users.map(u => u.email).filter((e): e is string => !!e), armored: KeyUtil.armor(k), family: k.family }));
}

public static validateChecksum(armoredText: string): boolean {
public static isChecksumMismatch(armoredText: string): boolean {
// Regex to capture any PGP armor block, e.g. SIGNATURE, MESSAGE, etc.
// It captures: the block type in group (1), and the content in group (2)
const pgpBlockRegex = /-----BEGIN PGP ([A-Z ]+)-----([\s\S]*?)-----END PGP \1-----/g;

let match: RegExpExecArray | null;
let validFound = false;

// Iterate over all PGP blocks in the text
while ((match = pgpBlockRegex.exec(armoredText))) {
Expand Down Expand Up @@ -553,12 +552,12 @@ export class KeyUtil {
const rawData = decodedChunks.join('');
// eslint-disable-next-line @typescript-eslint/no-misused-spread
const dataBytes = new Uint8Array([...rawData].map(c => c.charCodeAt(0)));
if (KeyUtil.crc24(dataBytes) === providedCRC) {
validFound = true;
if (KeyUtil.crc24(dataBytes) !== providedCRC) {
return true;
}
}

return validFound;
return false;
}

private static crc24 = (dataBytes: Uint8Array): number => {
Expand Down
2 changes: 1 addition & 1 deletion extension/js/common/message-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ export class MessageRenderer {
renderModule,
this.getRetryVerification(signerEmail, verificationPubs => MessageRenderer.decryptFunctionToVerifyRes(() => decrypt(verificationPubs))),
plainSubject,
!KeyUtil.validateChecksum(encryptedData.toString())
typeof encryptedData === 'string' && KeyUtil.isChecksumMismatch(encryptedData)
);
} else if (result.error.type === DecryptErrTypes.format) {
if (fallbackToPlainText) {
Expand Down
14 changes: 14 additions & 0 deletions test/source/tests/unit-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ Something wrong with this key`),
}
);
});
test(`[unit][KeyUtil.isChecksumMismatch] detects only present checksum mismatches`, t => {
const armoredWithValidChecksum = `-----BEGIN PGP MESSAGE-----

aGVsbG8=
=R/WK
-----END PGP MESSAGE-----`;
const armoredWithInvalidChecksum = armoredWithValidChecksum.replace('=R/WK', '=AAAA');
const armoredWithoutChecksum = armoredWithValidChecksum.replace('\n=R/WK', '');

expect(KeyUtil.isChecksumMismatch(armoredWithValidChecksum)).to.equal(false);
expect(KeyUtil.isChecksumMismatch(armoredWithInvalidChecksum)).to.equal(true);
expect(KeyUtil.isChecksumMismatch(armoredWithoutChecksum)).to.equal(false);
t.pass();
});
test(`[unit][OpenPGPKey.parse] throws on invalid input`, async t => {
await t.throwsAsync(
() =>
Expand Down
Loading