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

Accept oid in assertValid #589

Merged
merged 5 commits into from
Jun 6, 2020

Conversation

fibsifan
Copy link
Contributor

@fibsifan fibsifan commented May 2, 2020

Hello,

This is a first draft for #588

I've added the OIDs for Hmac-Keys and extended the check in the assertValid method. There are multiple ways of doing this, I decided to extend the SignatureAlgorithm's constructor with a varargs parameter to avoid having to modify all enum values.

For the test, an actual pkcs12 KeyStore instance is used to store and load the key. I suppose, one could mock the key. Would that be necessary?

The coverage checks failed to upload in my cloned repository, I suppose they will successfully do so, when the pr is built.

@coveralls
Copy link

coveralls commented May 2, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling e079ca8 on fibsifan:accept-oid-in-assert-valid into 403e189 on jwtk:master.

Copy link
Contributor

@lhazlewood lhazlewood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! (and sorry for the late review - the pandemic and other things have made it a bit tough).

Please see the comments in code for changes. Thanks!

api/src/main/java/io/jsonwebtoken/SignatureAlgorithm.java Outdated Show resolved Hide resolved
@@ -122,16 +122,18 @@
private final boolean jdkStandard;
private final int digestLength;
private final int minKeyLength;
private final List<String> alternativeNames;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anything, I'd want to introduce a single field oid and check that if the jcaName check fails. Because it is all private, we don't have any risk of refactoring this later to a collection if we ever find that there is more than one.

I suspect all the other signature algorithms have OIDs as well, and if so (we should check), they should have a non-null value as well.

Also, let's mark this field as @Deprecated with a comment pointing to https://bugs.openjdk.java.net/browse/JDK-8243551. Having deprecated fields in internal implementations are fine because users never see/experience them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have extended the test I used to demonstrate this error to check all signature-algorithms against the JCA and Bouncycastle-Keystores. Only HmacSha-Keys loaded from a JCA-pkcs12-Keystore have shown the mentioned behaviour, so I went with checking the OID only for the HmacSha SignatureAlgorithms for now.

The following table lists Jca Names for each (private) Key when loaded from each keystore:

SignatureAlgorithm / Keystore jca pkcs12 jks jceks bks uber bcpkcs12
HS256 HmacSHA256 1.2.840.113549.2.9 HmacSHA256 HmacSHA256 HmacSHA256
HS384 HmacSHA384 1.2.840.113549.2.10 HmacSHA384 HmacSHA384 HmacSHA384
HS512 HmacSHA512 1.2.840.113549.2.11 HmacSHA512 HmacSHA512 HmacSHA512
RS256 RSA RSA RSA RSA RSA RSA RSA
RS384 RSA RSA RSA RSA RSA RSA RSA
RS512 RSA RSA RSA RSA RSA RSA RSA
ES256 EC EC EC EC EC EC EC
ES384 EC EC EC EC EC EC EC
ES512 EC EC EC EC EC EC EC
PS256 RSA RSA RSA RSA RSA RSA RSA
PS384 RSA RSA RSA RSA RSA RSA RSA
PS512 RSA RSA RSA RSA RSA RSA RSA


SignatureAlgorithm(String value, String description, String familyName, String jcaName, boolean jdkStandard,
int digestLength, int minKeyLength) {
this(value, description,familyName, jcaName, jdkStandard, digestLength, minKeyLength, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing null, is it possible to use the OIDs listed in the OpenJDK link I provided for the other algorithms? Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I took a while: #589 (comment)

Seems, that it's not necessary to have oids for every Signature-Algorithm. Should we have them anyways? Or could we leave them out, since the field is going to be removed anyways?

Copy link
Contributor

@lhazlewood lhazlewood Jun 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see! Instead of oid then, we should have a pkcs12Name field that captures the names represented in the table above. This guarantees that everything is non-null as well as ensures a parallel variable (jcaName and pkcs12Name make more sense now).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. I renamed the field and made it a copy of jcaName for non-HmacSHA algorithms.

@fibsifan
Copy link
Contributor Author

fibsifan commented Jun 6, 2020

Now the field pkcs12Name has always a value (except for SignatureAlgorithm.NONE), but the fallback namecheck is only performed for hmac-algirithms.

Copy link
Contributor

@lhazlewood lhazlewood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - thank you!

@lhazlewood
Copy link
Contributor

Resolves #588

@lhazlewood lhazlewood added this to the 0.11.2 milestone Jun 6, 2020
@lhazlewood lhazlewood merged commit 6b02041 into jwtk:master Jun 6, 2020
@fibsifan fibsifan deleted the accept-oid-in-assert-valid branch June 7, 2020 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants