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

Wrong decryption key for ECDH with keywrap #1951

Closed
max4t opened this issue Oct 20, 2022 · 6 comments · Fixed by #2120
Closed

Wrong decryption key for ECDH with keywrap #1951

max4t opened this issue Oct 20, 2022 · 6 comments · Fixed by #2120
Assignees
Labels
Bug Product is not functioning as expected Customer reported Indicates issue was opened by customer IdentityModel8x Future breaking issues/features for IdentityModel 8x P1 More important, prioritize highly
Milestone

Comments

@max4t
Copy link

max4t commented Oct 20, 2022

Hello,

I believe there's an error in the implementation.

// on decryption we get the public key from the EPK value see: https://datatracker.ietf.org/doc/html/rfc7518#appendix-C
var ecdhKeyExchangeProvider = new EcdhKeyExchangeProvider(
key as ECDsaSecurityKey,
validationParameters.TokenDecryptionKey as ECDsaSecurityKey,
jwtToken.Alg,

The second argument of EcdhKeyExchangeProvider should be the public key taken from the epk in the token's header (as the comment said).

@max4t max4t changed the title wrong decryption key for ECDH with keywrap Wrong decryption key for ECDH with keywrap Oct 20, 2022
@brentschmaltz brentschmaltz added Customer reported Indicates issue was opened by customer Bug Product is not functioning as expected labels Oct 20, 2022
@brentschmaltz
Copy link
Member

@max4t thanks for reporting this issue.
We will check it out.

@GregDomzalski
Copy link
Contributor

I'm also running into this issue and agree with @max4t

In ECDH - the TokenDecryptionKey should be your private key. The public key needs to resolve to EPK.

There are other issues in this code as well. If there are multiple keys resolved and we drop to the "brute force" approach of trying them all, the exception messages are appended exceptionStrings. But, if we find a valid key that results in unwrappedKeys.Count > 0 we still fail.

It's hard to guess the original intent here. On the one hand, brute forcing multiple keys was definitely noted in the comments. However, the if statement checking the exceptionStrings.Length also seems deliberate, albeit incorrect.

FWIW, I was able to hack around these issues and get ECDH unwrap working using the following:

var jwt = new JsonWebToken(encodedToken);

// Work around EPK not being resolved
var epk = new JsonWebKey(jwt.GetHeaderValue<string>(JwtHeaderParameterNames.Epk));

// Code expects both keys to be ECDsaSecurityKeys and not JsonWebKeys
var ecdsa = ECDsa.Create(new ECParameters()
{
    Curve = Constants.Curve,
    Q = new ECPoint()
    {
        X = WebEncoders.Base64UrlDecode(epk.X),
        Y = WebEncoders.Base64UrlDecode(epk.Y)
    }
});

var ecdsaSK = new ECDsaSecurityKey(ecdsa);

var tokenValidationParameters = new TokenValidationParameters
{
    // Setting a resolver will force the key to be the private key
    TokenDecryptionKeyResolver = (_, _, _, _) => new [] { new ECDsaSecurityKey(decryptKey) },
    // Decryption key is used as the public key
    TokenDecryptionKey = ecdsaSK
};

var result = new JsonWebTokenHandler().ValidateToken(request, tokenValidationParameters);

@GregDomzalski
Copy link
Contributor

It would also really be nice if on the flip side, if KeyExchangePublicKey is set on the EncryptingCredentials that CreateToken would automatically add epk to the header. It seems that right now we need to add epk ourselves.

@inf9144
Copy link

inf9144 commented Dec 11, 2023

Hey, just ran into this issue and worked around it by manually setting EPK in AdditionalHeaderClaims and reading it in TokenDecryptionKeyResolver. Will there be an update? Saw the already open pull request.
Are you planning to add functionality so that the user does not need to provide the ephemeral key hisself/herself? Most other libs i saw are just generating the ephemeral under the hood and the dev only need sto supply the ECDsa public / private key on each side.

@brentschmaltz
Copy link
Member

@GregDomzalski @inf9144 looking into this now.

@brentschmaltz
Copy link
Member

@GregDomzalski the PR 2120 looks good one comments about back compat.

@jennyf19 jennyf19 assigned GregDomzalski and unassigned cesarpayan Mar 28, 2024
@jennyf19 jennyf19 added the P1 More important, prioritize highly label Mar 28, 2024
@brentschmaltz brentschmaltz added the IdentityModel8x Future breaking issues/features for IdentityModel 8x label Mar 30, 2024
@jennyf19 jennyf19 added this to the 8.0.1 milestone Jul 22, 2024
@pmaytak pmaytak linked a pull request Jul 26, 2024 that will close this issue
@pmaytak pmaytak closed this as completed Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Product is not functioning as expected Customer reported Indicates issue was opened by customer IdentityModel8x Future breaking issues/features for IdentityModel 8x P1 More important, prioritize highly
Projects
None yet
7 participants