使用Signature.verify验证签名时,出现“签名无效编码”异常。 使用Azure服务验证同一签名时,签名也会得到验证。
我有一个哈希数据(SHA-256),一个公共密钥和一个我要验证的签名。 使用com.microsoft.azure.keyvault.KeyVaultClient.sign方法和签名算法“ ES256”来接收签名。
这有效(使用ES256算法):
com.microsoft.azure.keyvault.KeyVaultClient keyVaultClient;
String keyPairIdentifier;
boolean verify(byte[] hashData, byte[] signature, JsonWebKeySignatureAlgorithm signingAlgorithm) {
com.microsoft.azure.keyvault.models.KeyVerifyResult result = keyVaultClient.verify(keyPairIdentifier, signingAlgorithm, hashData, signature);
return result.value().booleanValue();
}
此操作失败(证书拥有与Azure密钥库中存储的相同的公钥):
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initVerify(certificate.getPublicKey());
ecdsaSign.update(hashData);
ecdsaSign.verify(signature)
预期结果-正确(签名已验证)
实际结果:
java.security.SignatureException: Could not verify signature
at sun.security.ec.ECDSASignature.engineVerify(ECDSASignature.java:325)
at java.security.Signature$Delegate.engineVerify(Signature.java:1222)
at java.security.Signature.verify(Signature.java:655)
at TestKV.KeyVault.VerifyDPSignature.verifySignatureUsingCertificate(VerifyDPSignature.java:143)
at TestKV.KeyVault.VerifyDPSignature.main(VerifyDPSignature.java:104)
Caused by: java.security.SignatureException: Invalid encoding for signature
at sun.security.ec.ECDSASignature.decodeSignature(ECDSASignature.java:400)
at sun.security.ec.ECDSASignature.engineVerify(ECDSASignature.java:322)
... 4 more
Caused by: java.io.IOException: Sequence tag error
at sun.security.util.DerInputStream.getSequence(DerInputStream.java:330)
at sun.security.ec.ECDSASignature.decodeSignature (ECDSASignature.java:376)
答案 0 :(得分:0)
Azure使用JWS which simply concatenates fixed-size I2OSP of r and s,但是Java JCE像大多数但不是全部标准一样,使用ASN.1 DER编码e.g. rfc3279(注意:现在有ECDSA的OID和其他哈希)。
要将JWS /普通代码转换为DER,请参阅我的https://security.stackexchange.com/questions/174095/convert-ecdsa-signature-from-plain-to-der-format(使用C语言),但是Java使其更容易,因为BigInteger
为您完成了一半的工作:
// byte[64] plain contains the JWS-style r,s (de-base64-ed if necessary)
byte[] r = new BigInteger(1,Arrays.copyOfRange(plain,0,32)).toByteArray();
byte[] s = new BigInteger(1,Arrays.copyOfRange(plain,32,64)).toByteArray();
byte[] der = new byte[6+r.length+s.length]; der[0] = 0x30; der[1] = der.length-2; int o = 2;
der[o++] = 0x30; der[o++] = (byte)r.length; System.arraycopy (r,0, der,o, r.length); t+=r.length;
der[o++] = 0x30; der[o++] = (byte)s.length; System.arraycopy (s,0, der,o, s.length); //t+=s.length;
答案 1 :(得分:0)
dave_thompson_085-谢谢! 您所附加的代码中存在一些错误,签名部分的标签应为0x02,而不是0x30,并且在复制第一部分后没有增加o。 这是更改后的代码:
byte[] r = new BigInteger(1,Arrays.copyOfRange(signature,0,32)).toByteArray();
byte[] s = new BigInteger(1,Arrays.copyOfRange(signature,32,64)).toByteArray();
byte[] der = new byte[6+r.length+s.length];
der[0] = 0x30; // Tag of signature object
der[1] = (byte)(der.length-2); // Length of signature object
int o = 2;
der[o++] = 0x02; // Tag of ASN1 Integer
der[o++] = (byte)r.length; // Length of first signature part
System.arraycopy (r,0, der,o, r.length);
o += r.length;
der[o++] = 0x02; // Tag of ASN1 Integer
der[o++] = (byte)s.length; // Length of second signature part
System.arraycopy (s,0, der,o, s.length);
更改格式后,我没有收到“序列标签错误”异常。但是验证仍然失败。
谢谢!