![](/img/trans.png)
[英]Can communicate a certificate issued by a CA (client) with a certificate Self Signed (server)?
[英]My code can verify self signed certificate but is failing when VeriSign issued certificate is used
我的應用程序使用Java安全性API對文件進行簽名並進行驗證。 簽名時,我使用PFX文件和密碼作為輸入,簽名后,我使用字節生成簽名文件。 在驗證過程中,我使用簽名文件,證書文件和簽名文件作為輸入。 請在下面找到我在驗證中使用的代碼:
// KeyFilePath= path of certificate file
// fileToVerify = path of signed file
// signatureFilePath = path of signature file
InputStream inputStream = new FileInputStream(KeyFilePath);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream);
// input the signature bytes
String sigFile = signatureFilePath;
FileInputStream sigFileInputStream = new FileInputStream(sigFile);
byte[] sigToVerify = new byte[sigFileInputStream.available()];
sigFileInputStream.read(sigToVerify);
sigFileInputStream.close();
PublicKey pubKey = x509Certificate.getPublicKey();
Signature signature = Signature.getInstance(signAlgorithm);
signature.initVerify(pubKey);
// Update and verify the data
try {
FileInputStream dataFileInputStream = new FileInputStream(fileToVerify);
BufferedInputStream bufferedInputStream = new BufferedInputStream(dataFileInputStream);
byte[] buffer = new byte[IVerifyDigitalSignature.BYTE_SIZE];
int bufferedInputStreamLength;
while (bufferedInputStream.available() != IVerifyDigitalSignature.ZERO_LENGTH) {
bufferedInputStreamLength = bufferedInputStream.read(buffer);
signature.update(buffer, IVerifyDigitalSignature.ZERO_LENGTH, bufferedInputStreamLength);
}
bufferedInputStream.close();
// Verify the Signature
x509Certificate.verify(pubKey);
verifyDigitalSignature = signature.verify(sigToVerify);
請幫助我解決尚未解決的問題。
如果您想自己執行此操作,是的,您必須遍歷鏈中的證書,從信任錨到所需的證書,無論證書有多長(可能因不同的CA,類和在不同的時間而異)。 使用來自“父”(下一個)證書的公鑰來驗證每個“子”(下級)證書的簽名只是其中的一小部分; 還有許多其他步驟。 通常,僅僅找到合適的證書可能是一個問題。 如果您已經有了正確的鏈條,那么您將擁有一個良好的開端。 但是,您確定您擁有“正確的”鏈條嗎? 通常,給定證書有幾種可能的鏈,有時其中一些是有效的,但另一些已過期或變得不可驗證。 我相信Verisign特別發布了,我相信所有最新的證書都在其G5根目錄下,但是為不是最新的(有時無法更新)的標准版提供了(有效)回到G1的替代途徑。
在大多數情況下,該算法在“ PKIX” RFC5280中進行了定義,但用於撤銷的OCSP RFC6960而非CRL變得更加普遍。 您可能會忽略交叉層次結構和NameConstraints,而像Verisign這樣的公共CA並沒有真正使用AFAIK,而CA確實使用了但用戶/用戶並不關心的Policy東西。 https://security.stackexchange.com/questions/37409/certificate-chain-checking有一個不錯的但不完整的介紹。
但是您最好使用Java(實際上是JCE)的CertPathValidator作為“ PKIX” ,如果需要,還可以使用CertPathBuilder(我已經提到過)。 這已經由專家編寫和測試。 僅僅調用它仍然有點復雜,但是沒有什么比重寫它所做的所有事情復雜的了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.