Signature.verify在SAML/XML响应时总是返回false



我试图使用java signature类验证XML上的签名,但它总是返回false。XML由SAML服务返回。

这是我的代码:

public static boolean test() throws SignatureException, InvalidKeyException, NoSuchAlgorithmException, CertificateException {

String rawCert = "MIIE4zCCA8ugAwIBAgIFAIunqocwDQYJKoZIhvcNAQELBQAwfjEYMBYGCSqGSIb3DQEJARMJaXRAYW1hLnB0MQswCQYDVQQGEwJQVDEPMA0GA1UEBxMGTGlzYm9hMQwwCgYDVQQKEwNBTUExCzAJBgNVBAsTAklUMSkwJwYDVQQDEyBzYW1sLnByZXByb2QuYXV0ZW50aWNhY2FvLmdvdi5wdDAeFw0yMjAyMDIxNTIyMzFaFw0yNjA0MjIxNTIyMzFaMIGmMQswCQYDVQQLEwJJVDE5MDcGA1UEChMwQUdFTkNJQSBQQVJBIEEgTU9ERVJOSVpBQ0FPIEFETUlOSVNUUkFUSVZBLCBJLlAuMQ8wDQYDVQQHEwZMaXNib24xDzANBgNVBAgTBkxpc2JvbjELMAkGA1UEBhMCUFQxLTArBgNVBAMTJGlkcC5zYW1sLnByZXByb2QuYXV0ZW50aWNhY2FvLmdvdi5wdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALYNe0uVeWxQenErd8CndyBJakERq+ZfFLGhIiCIG03Kk3P7ZXacKcj7KQk6h6xNDRccrgJjERMb0jhQPxNc1YfGs9gugYJjMVqdyhbPrOLw1gC6jOuZYCFKqpK3KvNLt5uFL+svJrlbCme04InkJGI+tWpMn18FZCNabgvr96MPdEx1UkiUd8/LvfNwrvdIcRzjncS8rDNN/aoN+LOrMBciWup9IfclcTQu21QnJu5bW1m6a5RarB+2VExGhBuO05V9Hlu1GgR+ISOT93OxW8w8Y297FJcbOtGn1DS+BmEP4nXw1Z5tLp1TglLJqJyjPUbQH/RTHdEwwghE7t2LriUCAwEAAaOCAT0wggE5MIGiBgNVHSMEgZowgZeAFIqEGWsGURditfEV0gDuoBPL+ns5oXekdTBzMRgwFgYJKoZIhvcNAQkBEwlpdEBhbWEucHQxCzAJBgNVBAYTAlBUMQ8wDQYDVQQHEwZMaXNib2ExDDAKBgNVBAoTA0FNQTELMAkGA1UECxMCSVQxHjAcBgNVBAMTFUF1dGVudGljYWNhby5Hb3YgUmFpeoIGAWHCrFBGMB0GA1UdDgQWBBQPx8QxxuPf/9Rahh21vZWGYa9+wDAJBgNVHRMEAjAAMAsGA1UdDwQEAwIF4DAvBgNVHREEKDAmgiRpZHAuc2FtbC5wcmVwcm9kLmF1dGVudGljYWNhby5nb3YucHQwFgYDVR0lAQH/BAwwCgYIKwYBBQUHAwEwEgYKKwYBBAGC9DQBAQQEAwICBDANBgkqhkiG9w0BAQsFAAOCAQEA1T3ZLmK9ZdITIUjy0EzGF8+art8NoESs4JCNmzSKcuEqWZpWIWoMmo5mhsCwgKo4DmtL42rFqDbYghJYCaq/ENZ+6dgRJmnZNTJqT2bqa5F4kQM4yNMZfjBwF1qRWUMF/mRay5rQVOe81zqmUCNguiGCWFAnTtxTED6HmO+/ci60fuW6BcU06HI8hmB8km/wwYfQe8gsQ9X/7INHe5XWZ1ZqT3X+yz6sNiWiKIk18b2VJ+Ml7ZMZr202YU5i8VjKCzTEA7e/2eEWLVZMIvKRm+FHNbKqpcmVxysGa9kDanv1id4CIv+u7H4WBMu3Dsi1GdI1WpFxuIQcblZfgM8odw==";
byte [] decoded = Base64.getDecoder().decode(rawCert);
Certificate cert = CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded));
PublicKey publicKey = cert.getPublicKey();

String sigValue = "muuNQW0vr/YE31Mjls4UCBZLlavnsAHp3EarT2p5NLYIU6C32Zb0jeQ/c+zvK5CsNG/ld836R78Ji4NFJiuWEPvSIqB7MKeoqpNolmOZdVbu6ZRRJLCa/awwLizfbPAWv+JQkT+aZX6pkVgS71q1LsF3NtAgIqOdJyBHtBFoFvuTRKZx7JmDwRttPtSL8lRMO3v+h0LyTIPNB0KFGfCuzrfUFOR3RrzZauiWOjJChN1uhKa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idXRlVmFsdWU+DQogICAgICA8L0F0dHJpYnV0ZT4NCiAgICA8L0F0dHJpYnV0ZVN0YXRlbWVudD4NCiAgPC9Bc3NlcnRpb24+DQo8L1Jlc3BvbnNlPg==";
byte[] contentBytes = Base64.getDecoder().decode(content);

Signature sig = Signature.getInstance(signatureType);
sig.initVerify(publicKey);
sig.update(contentBytes);
return sig.verify(sigBytes);

}

是我做错了什么,还是XML真的签名不好?我无法控制发送响应的服务器,显然一切都很好。

我不确定你从哪里得到的sigValue

XML签名验证使用XML中的信息进行验证,其中的signature值与您的值不匹配。

我使用了perl的XMLSig,并使用rawcert验证了XML,而内容则使用XML中的SignatureValue验证了XML

相关内容

  • 没有找到相关文章

最新更新