Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ndi): fix incorrect key selection for ecdh-es #607

Merged
merged 1 commit into from
Oct 9, 2023
Merged
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
80 changes: 58 additions & 22 deletions lib/express/oidc/v2-ndi.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,47 +59,80 @@ const id_token_encryption_alg_values_supported = {
corpPass: corppass_id_token_encryption_alg_values_supported,
}

function findEncryptionKey(jwks) {
function findEcdhEsEncryptionKey(jwks, crv, algs) {
let encryptionKey = jwks.keys.find(
(item) =>
item.use === 'enc' &&
item.kty === 'EC' &&
item.crv === 'P-521' &&
(item.alg === 'ECDH-ES+A256KW' || !item.alg),
item.crv === crv &&
(!item.alg ||
(item.alg === 'ECDH-ES+A256KW' &&
algs.some((alg) => alg === item.alg))),
)
if (encryptionKey) {
return { ...encryptionKey, alg: 'ECDH-ES+A256KW' }
return {
...encryptionKey,
...(!encryptionKey.alg ? { alg: 'ECDH-ES+A256KW' } : {}),
}
}
encryptionKey = jwks.keys.find(
(item) =>
item.use === 'enc' &&
item.kty === 'EC' &&
item.crv === crv &&
(!item.alg ||
(item.alg === 'ECDH-ES+A192KW' &&
algs.some((alg) => alg === item.alg))),
)
if (encryptionKey) {
return {
...encryptionKey,
...(!encryptionKey.alg ? { alg: 'ECDH-ES+A256KW' } : {}),
}
}
encryptionKey = jwks.keys.find(
(item) =>
item.use === 'enc' &&
item.kty === 'EC' &&
item.crv === crv &&
(!item.alg ||
(item.alg === 'ECDH-ES+A128KW' &&
algs.some((alg) => alg === item.alg))),
)
if (encryptionKey) {
return {
...encryptionKey,
...(!encryptionKey.alg ? { alg: 'ECDH-ES+A256KW' } : {}),
}
}
return null
}

function findEncryptionKey(jwks, algs) {
let encryptionKey = findEcdhEsEncryptionKey(jwks, 'P-521', algs)
if (encryptionKey) {
return encryptionKey
}
if (!encryptionKey) {
encryptionKey = jwks.keys.find(
(item) =>
item.use === 'enc' &&
item.kty === 'EC' &&
item.crv === 'P-384' &&
(item.alg === 'ECDH-ES+A192KW' || !item.alg),
)
encryptionKey = findEcdhEsEncryptionKey(jwks, 'P-384', algs)
}
if (encryptionKey) {
return { ...encryptionKey, alg: 'ECDH-ES+A192KW' }
return encryptionKey
}
if (!encryptionKey) {
encryptionKey = jwks.keys.find(
(item) =>
item.use === 'enc' &&
item.kty === 'EC' &&
item.crv === 'P-256' &&
(item.alg === 'ECDH-ES+A128KW' || !item.alg),
)
encryptionKey = findEcdhEsEncryptionKey(jwks, 'P-256', algs)
}
if (encryptionKey) {
return { ...encryptionKey, alg: 'ECDH-ES+A128KW' }
return encryptionKey
}
if (!encryptionKey) {
encryptionKey = jwks.keys.find(
(item) =>
item.use === 'enc' &&
item.kty === 'RSA' &&
(item.alg === 'RSA-OAEP-256' || !item.alg),
(!item.alg ||
(item.alg === 'RSA-OAEP-256' &&
algs.some((alg) => alg === item.alg))),
)
}
if (encryptionKey) {
Expand Down Expand Up @@ -441,7 +474,10 @@ function config(app, { showLoginPage }) {
.sign(signingKey)

// Step 4: Encrypt ID token with RP encryption key
const rpEncryptionKey = findEncryptionKey(rpKeysetJson)
const rpEncryptionKey = findEncryptionKey(
rpKeysetJson,
id_token_encryption_alg_values_supported[idp],
)
if (!rpEncryptionKey) {
console.error('No suitable encryption key found', rpKeysetJson.keys)
return res.status(400).send({
Expand Down