Closed cmcdowell-red closed 6 months ago
Hi there!
DefaultJwsHeader
is not a publicly-available API - it's internal to the JJWT impl
module which should not be used with compile
scope within a project:
https://github.com/jwtk/jjwt?tab=readme-ov-file#understanding-jjwt-dependencies
So it's quite possible that your usage of that internal API is not supported and/or does not work as attempted.
Have you tried using the JWT or header builders? For example:
List<X509Certificate> chain = getMyChain();
Jwts.header().x509Chain(chain)...
We do have test cases for x5c
list/array parsing, for example:
While that tests a JWK, the X509 logic is shared between JJWT's JWK and JWT header implementations.
Anyway, I'm not saying that we don't have something that needs to be fixed, just that I can't tell yet.
Can you try using the public API and see if there's still an issue? That would help me hone in on what might need to be done.
I just threw a quick test together to see if the x5c
value you pasted above could be parsed. I just threw that same value in the JWK used from https://github.com/jwtk/jjwt/blob/master/impl/src/test/groovy/io/jsonwebtoken/impl/security/RFC7517AppendixBTest.groovy just to see if it could parse.
Here is my test json String:
var json = """
{
"kty": "RSA",
"use": "sig",
"kid": "1b94c",
"n" : "vrjOfz9Ccdgx5nQudyhdoR17V-IubWMeOZCwX_jj0hgAsz2J_pqYW08\
PLbK_PdiVGKPrqzmDIsLI7sA25VEnHU1uCLNwBuUiCO11_-7dYbsr4iJmG0Q\
u2j8DsVyT1azpJC_NG84Ty5KKthuCaPod7iI7w0LK9orSMhBEwwZDCxTWq4a\
YWAchc8t-emd9qOvWtVMDC2BXksRngh6X5bUYLy6AyHKvj-nUy1wgzjYQDwH\
MTplCoLtU-o-8SNnZ1tmRoGE9uJkBLdh5gFENabWnU5m1ZqZPdwS-qo-meMv\
VfJb6jJVWRpl2SUtCnYG2C32qvbWbjZ_jBPD5eunqsIo1vQ",
"e" : "AQAB",
"x5c": [
"MIIE3jCCA8agAwIBAgICAwEwDQYJKoZIhvcNAQEFBQAwYzELMAkGA1UEBhMCVVM\
xITAfBgNVBAoTGFRoZSBHbyBEYWRkeSBHcm91cCwgSW5jLjExMC8GA1UECxMoR2\
8gRGFkZHkgQ2xhc3MgMiBDZXJ0aWZpY2F0aW9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mNr0HZDzTuIYEZoDJJKPTEjlbVUjP9UNV+mWwD5MlM/Mtsq2azSiGM5bUMM\
j4QssxsodyamEwCW/POuZ6lcg5Ktz885hZo+L7tdEy8W9ViH0Pd"]
}
""";
var jwk = Jwks.parser().build().parse(json);
Parsing failed with the following:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid RSA JWK 'x5c' (X.509 Certificate Chain) value: '<redacted_for_brevity>' to X509Certificate instance. Cause: CertificateFactory callback execution failed: Unable to initialize, java.io.IOException: Invalid lenByte
at io.jsonwebtoken.impl.ParameterMap.apply(ParameterMap.java:193)
at io.jsonwebtoken.impl.ParameterMap.put(ParameterMap.java:139)
at io.jsonwebtoken.impl.ParameterMap.put(ParameterMap.java:149)
at io.jsonwebtoken.impl.ParameterMap.put(ParameterMap.java:36)
at io.jsonwebtoken.impl.lang.DelegatingMap.put(DelegatingMap.java:81)
at io.jsonwebtoken.impl.lang.DelegatingMapMutator.add(DelegatingMapMutator.java:45)
at io.jsonwebtoken.impl.security.JwkConverter.applyFrom(JwkConverter.java:170)
at io.jsonwebtoken.impl.security.JwkConverter.applyFrom(JwkConverter.java:42)
at io.jsonwebtoken.impl.io.ConvertingParser.parse(ConvertingParser.java:39)
at io.jsonwebtoken.impl.io.AbstractParser.parse(AbstractParser.java:36)
at io.jsonwebtoken.impl.io.AbstractParser.parse(AbstractParser.java:29)
at JJWT.Standalone.Test/test.Main.testMultiX5c(Main.java:136)
at JJWT.Standalone.Test/test.Main.run(Main.java:16)
at JJWT.Standalone.Test/test.Main.main(Main.java:9)
Caused by: java.lang.IllegalArgumentException: Unable to convert Base64 String '<redacted_for_brevity>' to X509Certificate instance. Cause: CertificateFactory callback execution failed: Unable to initialize, java.io.IOException: Invalid lenByte
at io.jsonwebtoken.impl.security.JwtX509StringConverter.applyFrom(JwtX509StringConverter.java:67)
at io.jsonwebtoken.impl.security.JwtX509StringConverter.applyFrom(JwtX509StringConverter.java:28)
at io.jsonwebtoken.impl.lang.EncodedObjectConverter.applyFrom(EncodedObjectConverter.java:42)
at io.jsonwebtoken.impl.lang.CollectionConverter.toElementList(CollectionConverter.java:63)
at io.jsonwebtoken.impl.lang.CollectionConverter.applyFrom(CollectionConverter.java:86)
at io.jsonwebtoken.impl.lang.CollectionConverter.applyFrom(CollectionConverter.java:27)
at io.jsonwebtoken.impl.lang.DefaultParameter.applyFrom(DefaultParameter.java:124)
at io.jsonwebtoken.impl.ParameterMap.apply(ParameterMap.java:176)
... 13 more
Caused by: io.jsonwebtoken.security.SecurityException: CertificateFactory callback execution failed: Unable to initialize, java.io.IOException: Invalid lenByte
at io.jsonwebtoken.impl.security.JcaTemplate.execute(JcaTemplate.java:130)
at io.jsonwebtoken.impl.security.JcaTemplate.fallback(JcaTemplate.java:144)
at io.jsonwebtoken.impl.security.JcaTemplate.generateX509Certificate(JcaTemplate.java:332)
at io.jsonwebtoken.impl.security.JwtX509StringConverter.toCert(JwtX509StringConverter.java:56)
at io.jsonwebtoken.impl.security.JwtX509StringConverter.applyFrom(JwtX509StringConverter.java:64)
... 20 more
Caused by: java.security.cert.CertificateException: Unable to initialize, java.io.IOException: Invalid lenByte
at java.base/sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:145)
at java.base/sun.security.provider.X509Factory.cachedGetX509Cert(X509Factory.java:113)
at java.base/sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:97)
at java.base/java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:355)
at io.jsonwebtoken.impl.security.JcaTemplate$10.apply(JcaTemplate.java:336)
at io.jsonwebtoken.impl.security.JcaTemplate$10.apply(JcaTemplate.java:332)
at io.jsonwebtoken.impl.security.JcaTemplate.execute(JcaTemplate.java:120)
at io.jsonwebtoken.impl.security.JcaTemplate.access$100(JcaTemplate.java:65)
at io.jsonwebtoken.impl.security.JcaTemplate$3.get(JcaTemplate.java:148)
at io.jsonwebtoken.impl.security.JcaTemplate.execute(JcaTemplate.java:125)
... 24 more
Caused by: java.io.IOException: Invalid lenByte
at java.base/sun.security.util.DerValue.<init>(DerValue.java:406)
at java.base/sun.security.util.DerInputStream.getDerValue(DerInputStream.java:110)
at java.base/sun.security.x509.X509CertImpl.parse(X509CertImpl.java:1435)
at java.base/sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:143)
... 33 more
This means the parser is successfully interpreting the value as an array of Base64 strings. But if you look at where it failed in the JwtX509StringConverter
, here's the code:
It fails specifically on line 56, which means that Base64 decoding (line 63) succeeded.
This means the decoded bytes appear not to be DER bytes as required by https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.6 (which is the same requirement in https://datatracker.ietf.org/doc/html/rfc7517#section-4.7 for JWKs).
So it seems that your test value pasted in this ticket is an invalid JSON array of Base64-encoded DER-formatted X509 Certificates.
That said, I'm happy to help keep digging if you have any other information and/or tests to share - please let me know!
Here's a test class I wrote demonstrating failure for the 2nd value in the x5c array, eliminating any JJWT certificate-related code:
import io.jsonwebtoken.io.Decoders;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
public class Test {
private static X509Certificate readCert(String b64) throws CertificateException {
byte[] derBytes = Decoders.BASE64.decode(b64);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
InputStream is = new ByteArrayInputStream(derBytes);
return (X509Certificate) factory.generateCertificate(is);
}
public static void main(String[] args) throws Exception {
var x5c1 = """
MIIE3jCCA8agAwIBAgICAwEwDQYJKoZIhvcNAQEFBQAwYzELMAkGA1UEBhMCVVM\
xITAfBgNVBAoTGFRoZSBHbyBEYWRkeSBHcm91cCwgSW5jLjExMC8GA1UECxMoR2\
8gRGFkZHkgQ2xhc3MgMiBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw0wNjExM\
TYwMTU0MzdaFw0yNjExMTYwMTU0MzdaMIHKMQswCQYDVQQGEwJVUzEQMA4GA1UE\
CBMHQXJpem9uYTETMBEGA1UEBxMKU2NvdHRzZGFsZTEaMBgGA1U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cg5Ktz885hZo+L7tdEy8W9ViH0Pd
""";
readCert(x5c3);
}
}
Interestingly, the 1st and 3rd Base64 values (index 0 and 2 in the array) parse successfully, but the 2nd one (index 1) does not.
But, this tells me something important about the quick JWK parsing test case above: the JJWT code parsing the JSON array is successfully parsing more than one element. It succeeded on the first, and failed on the 2nd one due to the invalid DER format of the bytes.
When you get a chance, can you please ensure all 3 String values in your JSON array successfully parse to X509Certificates using the readCert
helper method? Once that's guaranteed, then we can try to parse in the full context of a JWK or JW(S|E).
When I edit the readCert
helper method to use the JDK's Base64 decoder, the second value (x5c2
) fails during Base64-decoding:
private static X509Certificate readCert(String b64) throws CertificateException {
byte[] derBytes = Base64.getDecoder().decode(b64); // IllegalArgumentException fails with 'Input byte array has incorrect ending byte at 1668'
CertificateFactory factory = CertificateFactory.getInstance("X.509");
InputStream is = new ByteArrayInputStream(derBytes);
return (X509Certificate) factory.generateCertificate(is);
}
So something is amiss with that 2nd JSON array value.
Thank you for the quick reply. You are right, it was an error in the certificate. Thanks for the help.
@cmcdowell-red closing the issue as a result. Thanks for the update!
Describe the bug The x5c parameter value does not correctly get mapped to
List<X509Certificate>
if the value of the parameter contains more than one bse64 encoded certificate.The x5c parameter is supposed to allow a chain of multiple base64 encoded certs to be passed in the parameter, the implementation in jjwt does not support that. https://www.rfc-editor.org/rfc/rfc7515#section-4.1.6
To Reproduce
Using the latest version of jjwt,
io.jsonwebtoken:jjwt-api:0.12.5
,io.jsonwebtoken:jjwt-impl:0.12.5
, andio.jsonwebtoken:jjwt-jackson:0.12.5
I can try to construct a DefaultJwsHeader where the x5c parameter has a value which has more than one certificate. The value of the x5c header here is taken from Appendix B of RFC 7515 https://www.rfc-editor.org/rfc/rfc7515#appendix-B.
Expected behavior I expect this code to print out the size of the certificate chain.
Actual behavior
It looks like to me that
JwtX509StringConverter
is trying to base 64 decode the entire json array, rather than each individual certificate within the array.The same error occurs when using
Jwts.parser()
to parse a JWT with an x5c header that has mulitple certificates in the array.I noticed this when upgrading from an older version of jjwt that did not try to convert the x5c parameter into
List<X509Certificate>
but instead returned a string. This allowed me to parse the json in the parameter myself correctly, but with the latest version of jjwt I can no longer do that.