Hug*_*rse 6 java cryptography elliptic-curve java-security jwt-auth
嗨,我有一个使用 Elliptic Curve ES256 签名的 JWT,我正在尝试验证它:
eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMzQifQ.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MTU3NTY1NjY3NCwiZXhwIjoxOTI0OTkxOTk5LCJpYXQiOjE1NzU2NTY2NzQsImp0aSI6ImlkMTIzNDU2IiwidHlwIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9yZWdpc3RlciJ9.kcj0QQrERKIfny1TfHY-Z9iDFazr84xCssTDuXtV1n1dvY7CYXuP5ZBvpi9ArOQjsS8YCd0bKsWaQ-17VnF_1A
Run Code Online (Sandbox Code Playgroud)
使用此公钥:
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEoBUyo8CQAFPeYPvv78ylh5MwFZjT
CLQeb042TjiMJxG+9DLFmRSMlBQ9T/RsLLc+PmpB1+7yPAR+oR5gZn3kJQ==
-----END PUBLIC KEY-----
Run Code Online (Sandbox Code Playgroud)
这只是
eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMzQifQ.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MTU3NTY1NjY3NCwiZXhwIjoxOTI0OTkxOTk5LCJpYXQiOjE1NzU2NTY2NzQsImp0aSI6ImlkMTIzNDU2IiwidHlwIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9yZWdpc3RlciJ9.kcj0QQrERKIfny1TfHY-Z9iDFazr84xCssTDuXtV1n1dvY7CYXuP5ZBvpi9ArOQjsS8YCd0bKsWaQ-17VnF_1A
Run Code Online (Sandbox Code Playgroud)
它在https://jwt.io/ 上正确验证但是当我尝试使用本机 Java 验证它时,它引发了一个错误:
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEoBUyo8CQAFPeYPvv78ylh5MwFZjT
CLQeb042TjiMJxG+9DLFmRSMlBQ9T/RsLLc+PmpB1+7yPAR+oR5gZn3kJQ==
-----END PUBLIC KEY-----
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
{
"kty": "EC",
"use": "sig",
"crv": "P-256",
"kid": "1234",
"x": "oBUyo8CQAFPeYPvv78ylh5MwFZjTCLQeb042TjiMJxE=",
"y": "vvQyxZkUjJQUPU/0bCy3Pj5qQdfu8jwEfqEeYGZ95CU=",
"alg": "ES256"
}
Run Code Online (Sandbox Code Playgroud)
最后一行似乎引发了异常。
java.security.SignatureException: invalid encoding for signature: java.io.IOException: Sequence tag error
at com.ibm.crypto.provider.AbstractSHAwithECDSA.engineVerify(Unknown Source)
at java.security.Signature$Delegate.engineVerify(Signature.java:1219)
at java.security.Signature.verify(Signature.java:652)
at curam.platforminfrastructure.outbound.oauth2.impl.ValidateJWT.validateEllipticCurve(ValidateJWT.java:235)
at curam.platforminfrastructure.outbound.oauth2.impl.ValidateJWT.validateJWT(ValidateJWT.java:88)
at curam.platforminfrastructure.outbound.oauth2.impl.ValidateJWT.testJWT(ValidateJWT.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Run Code Online (Sandbox Code Playgroud)
我不确定我是否使用错误的曲线(secp256k1、secp256r1、secp256v1)导致了问题,或者可能是其他原因。任何提示将不胜感激。
问题在于,为 JWS 计算的签名(在椭圆曲线的情况下)是RFC7515中指定的R和值的原始串联。因此,这是 32 + 32 = 64 字节(在 256 位曲线的情况下)。S
然而,Java 实现期望此签名采用 DER 序列形式。这就是您遇到此异常的原因 - 只是您的签名(R和S值)不是 DER 编码的。您甚至可以检查负责解码签名 ( sun.security.ec.ECDSASignature::decodeSignature) 和编码签名的代码(用于签名操作 - sun.security.ec.ECDSASignature::encodeSignature)。据您在源代码中看到的,他们使用sun.ec包中的内部类进行 DER 编码。并且不鼓励您自己使用这些类。
您最好的选择是将原始签名转换为 DER 编码的签名。如果您愿意,可以使用BouncyCastle开箱即用的 DER 编码处理。以下是将 EC 原始签名转换为 DER 编码签名的方法:
private static byte[] toDerSignature(byte[] jwsSig) throws IOException {
byte[] rBytes = Arrays.copyOfRange(jwsSig, 0, 32);
byte[] sBytes = Arrays.copyOfRange(jwsSig, 32, 64);
BigInteger r = new BigInteger(1, rBytes);
BigInteger s = new BigInteger(1, sBytes);
DERSequence sequence = new DERSequence(new ASN1Encodable[] {
new ASN1Integer(r),
new ASN1Integer(s)
});
return sequence.getDEREncoded();
}
Run Code Online (Sandbox Code Playgroud)
并使用此签名进行验证。