diff --git a/lib/validateSignature.ts b/lib/validateSignature.ts index fb9a6ab4..36dd2b6c 100644 --- a/lib/validateSignature.ts +++ b/lib/validateSignature.ts @@ -219,7 +219,8 @@ const hasValidSignature = ( break; } } - if (!valid) { + // A digest mismatch returns null, as with a single certificate, so validate() can retry the encrypted XML. + if (valid === undefined) { throw new Error('invalid signature: Failed to verify signature against all the certificates provided.'); } } else { diff --git a/test/lib/response.spec.ts b/test/lib/response.spec.ts index b3eb2b9e..e9131783 100644 --- a/test/lib/response.spec.ts +++ b/test/lib/response.spec.ts @@ -596,6 +596,49 @@ describe('validate() with an assertion encrypted before the Response was signed' await assert.rejects(validate(signedEncrypted, options({ publicKey: certificate })), /Invalid assertion/); }); + for (const [label, publicKey] of [ + ['active certificate first', [testIdpCert, certificate].join(',')], + ['active certificate last', [certificate, testIdpCert].join(',')], + ['duplicate certificates', [testIdpCert, testIdpCert].join(',')], + ]) { + it(`accepts the encrypted Response with ${label}`, async function () { + const profile = await validate(signedEncrypted, options({ publicKey })); + assert.deepStrictEqual(profile, await validate(signedEncrypted, options())); + }); + + it(`rejects a tampered encrypted Response with ${label}`, async function () { + const tampered = signedEncrypted.replace( + `Destination="${ACS}"`, + 'Destination="https://evil.example.com/acs"' + ); + assert.notStrictEqual(tampered, signedEncrypted); + await assert.rejects(validate(tampered, options({ publicKey })), { + message: 'Invalid assertion signature.', + }); + }); + + it(`still checks the audience with ${label}`, async function () { + await assert.rejects( + validate(signedEncrypted, options({ publicKey, audience: 'https://wrong.example.com' })), + /Invalid audience/ + ); + }); + + it(`still checks request correlation with ${label}`, async function () { + await assert.rejects( + validate(signedEncrypted, options({ publicKey, inResponseTo: '_wrong-request' })), + /Invalid InResponseTo/ + ); + }); + } + + it('rejects the encrypted Response when none of the certificates is trusted', async function () { + await assert.rejects( + validate(signedEncrypted, options({ publicKey: [certificate, certificate].join(',') })), + /Invalid assertion/ + ); + }); + it('rejects it when no decryption key is supplied', async function () { await assert.rejects(validate(signedEncrypted, options({ privateKey: undefined })), { message: 'Exception of Assertion Decryption.', diff --git a/test/lib/validateSignature.spec.ts b/test/lib/validateSignature.spec.ts index 8c06f139..fd3fae35 100644 --- a/test/lib/validateSignature.spec.ts +++ b/test/lib/validateSignature.spec.ts @@ -416,6 +416,13 @@ describe('validateSignature() failure mode', function () { message: 'invalid signature: the signature does not verify', }); }); + + it('reports a digest mismatch the same way with multiple certificates', function () { + const tampered = signed().replace('jo@example.com', 'eve@example.com'); + assert.throws(() => validateSignature(tampered, [testIdpCert, testIdpCert].join(','), null), { + message: 'invalid signature: the signature does not verify', + }); + }); }); describe('validateSignature.ts - algorithm allowlists', function () {