我想表明,如果我从给定的 X509 证书修改一个位或一个字节,签名验证结果为 false(因为此修改导致与证书不同的哈希值)。我被困在如何使用 getTBSCertificate() 方法对证书进行修改的情况下。我的以下代码完美地完成了验证过程,但我试图使用位或字节修改的想法使其失败,但它不起作用。请注意,我提出的这个想法是证明对证书的任何修改都会导致签名验证失败
public class VerifyX509 {
private static Certificate getCACert;
private static Certificate[] getCert;
public static void main(String[] args) throws CertificateEncodingException {
setURLConnection("https://www.google.com");
X509Certificate x509cert= (X509Certificate) getCert[0];
byte[] b= x509cert.getTBSCertificate();
b[0] = (byte) ~b[0];
// HOW TO UPDATE getTBSCertificate() after flipping the b[0] to make Verify() in my method verifySign() return false!
verifySign();
}
public static void setURLConnection(String link){
try{
int i=1;
URL destinationURL = new URL(link);
HttpsURLConnection con = (HttpsURLConnection) destinationURL.openConnection();
con.connect();
getCert = con.getServerCertificates();
for (Certificate c : getCert)
{
if (i==2)
{
getCACert= c;
return;
}
i+=1;
}
}catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Error while connection! Check your Internet Connection.");
e1.printStackTrace();
}
}
public static boolean verifySign()
{
try
{
getCert[0].verify(getCACert.getPublicKey());
return true;
} catch (GeneralSecurityException e2)
{
return false;
}
}
}
如何设置概念验证代码以显示验证失败?
最佳答案
Note that this idea that I proposed is to proof that any modification on the certificate will make a failure while signature verification.
您可以通过简单地翻转有效证书中的随机位然后尝试验证它们来证明这一点(以一定的正确概率)。
但是,您不能证明这样的事情。适当的证明需要:
数学证明正确实现的 X509 证书具有更改证书使其无效的属性(概率非常接近 11)。
正式方法证明加载证书和执行验证的代码已正确实现。
1 - 其实很容易看出概率不可能恰好为一。应用鸽巢原理。
关于java - 修改X509证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21917034/