2014-01-16 61 views
1

TimeStampToken類中有兩個驗證方法(bctsp-jdk16-1.46.jar),其中一個已棄用。如何使用文件系統中的.cer文件驗證TimeStampToken?

不推薦使用的方法使用X509Certificate作爲參數,這很容易創建。

InputStream inPFX = getClass().getClassLoader().getResourceAsStream("tsp.cer"); 
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); 
X509Certificate cert = (X509Certificate) cf.generateCertificate(inPFX); 

// The validate method just takes the X509Certificate object 
token.validate(cert, "BC"); 

新方法使用SignerInformationVerifier對象。我找到了一種創建SignerInformationVerifier的方法(不確定它是否正確),但我仍然需要一個X509CertificateHolder對象。

  • 我如何在文件系統中創建該文件中的X509CertificateHolder(* .CER文件)
  • 這是正確的方式來創建一個SignerInformationVerifier驗證時間戳標記?

我當前的代碼如下所示:

TimeStampToken token = new TimeStampToken(new CMSSignedData(response)); 

X509CertificateHolder x = // HOW TODO THIS? 

// create the SignerInformationVerifier object 
DigestAlgorithmIdentifierFinder daif = new DefaultDigestAlgorithmIdentifierFinder(); 
DigestCalculatorProvider dcp = new BcDigestCalculatorProvider(); 
SignerInformationVerifier siv = new BcRSASignerInfoVerifierBuilder(daif, dcp).build(x509ch); 

// use the new validate method 
token.validate(siv); 

回答

1

試試這個

TimeStampToken token = new TimeStampToken(new CMSSignedData(response)); 

InputStream in = new FileInputStream("tsp.cer"); 
CertificateFactory factory = CertificateFactory.getInstance("X.509"); 

X509Certificate cert = (X509Certificate) factory.generateCertificate(in); 

//RSA Signature processing with BC 
X509CertificateHolder holder = new X509CertificateHolder(cert.getEncoded()); 
SignerInformationVerifier siv = new BcRSASignerInfoVerifierBuilder(new DefaultDigestAlgorithmIdentifierFinder(), new BcDigestCalculatorProvider()).build(holder); 

//Signature processing with JCA and other provider 
//X509CertificateHolder holderJca = new JcaX509CertificateHolder(cert); 
//SignerInformationVerifier sivJca = new JcaSimpleSignerInfoVerifierBuilder().setProvider("anotherprovider").build(holderJca); 

token.validate(siv); 

看看驗證SignerInformation對象BC Version 2 APIs文檔部分,瞭解與簽名驗證的其他信息BC API。

您正在以正確的方式創建SignerInformationVerifier,您可以在示例代碼中找到另一種方法來爲基於JCA/JCE提供程序的解決方案創建SignerInformationVerifier

相關問題