-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Impossible to build a JWK with alg: HS512
and a k
that is larger than 64 bytes
#905
Comments
@louis-jaris thank you so much for the kind words, and also thank you for the extremely well-written issue! It really helped me immediately understand what you're experiencing and how to address it.
Great catch! And thank you so much for reporting this! I believe this was a 'carryover' from similar logic validating EllipticCurve keys (which have to be exactly a specific length, not >= a specific length). That said, it's not exactly as simple as that, but it's not much more difficult. That method is called from below in the jjwt/impl/src/main/java/io/jsonwebtoken/impl/security/SecretJwkFactory.java Lines 78 to 95 in f61cfa8
And that logic (currently) uses the byte array size (in bits) to set it's JCA algorithm name (, e.g. This is a minor fix, but it also elucidates another issue I'd like to clean up: Any length/size validation(i.e. This should be simple enough - and thanks for the sample test cases! That'll help. This should be a relatively quick fix 😃 |
Fixed via #909 |
- Ensured Secret JWK 'k' byte arrays for HMAC-SHA algorithms can be larger than the identified HS* algorithm. This is allowed per https://datatracker.ietf.org/doc/html/rfc7518#section-3.2: "A key of the same size as the hash output ... _or larger_ MUST be used with this algorithm" - Ensured that, when using the JwkBuilder, Secret JWK 'alg' values would automatically be set to 'HS256', 'HS384', or 'HS512' if the specified Java SecretKey algorithm name equals a JCA standard name (HmacSHA256, HmacSHA384, etc) or JCA standard HMAC-SHA OID. - Updated CHANGELOG.md accordingly. Fixes #905
First of all, thank you for your work! 🙌
I've loved the recent refactoring that took place, as well as the phenomal work done in your
README.md
to explains various JW* concept, I'm so grateful for all of your work!Describe the bug
When trying to load JWK Set (also applicable to single JWK loading), it is impossible to load a key with
alg: HS512
with ak
that is larger than 64 bytes.While it makes complete sense that keys that are shorter than the requirement should be rejected, it is less evident that secrets that are larger should be rejected as well.
Additionally, this behaviour is inconsistant: when we are creating
SecretKey
withKeys.hmacShaKeyFor(...)
with more than 64 bytes, it works, while we cannot load HS512 JWK with more than 64 bytes.As you are citing in the DefaultMacAlgorithm, the RFC 7518 is indicating that secrets that are larger than the required minimal size should work...
This is the exact line that is producing this behaviour. A (simple) fix could be to use
bitLen < requiredBitLen
instead ofbitLen != requiredBitLen
.The stack trace is:
To Reproduce
Minimal code to reproduce (and that highlight different scenarios):
Expected behavior
We should be able to load JWK with
k
that are doing more than 64 bytes.We should be able to load the following JWK:
Screenshots
Screenshot of the result of the code above:
The text was updated successfully, but these errors were encountered: