2016-07-26 2 views
1

CMS 서명 데이터 (또는 PKCS7 서명 데이터)를 만들기 위해 현재 Java Bouncy Castle 라이브러리를 사용하고 있습니다. 나는 인증서 서명자가 제대로 추가 되었더라도 인증서를 추가하는 데 어려움을 겪고있는 것처럼 보입니다.CMS 서명 된 데이터에 인증서 추가

올바르게 데이터에 서명하는 것에 대해 this question을 체크 아웃했지만 SCEP 서버의 요구에 응답하지 않았습니다. 내가 사용한 코드는 EJBCA에서 나온 것이지만 PKCS7 서명 데이터에 인증서를 추가하지 않는 것 같습니다.

openssl cms 도구로 서명 된 데이터를 구문 분석 할 때 '인증서'입력란에 '비어 있음'이 표시됩니다. 또한 openssl pkcs7 [...] -print_certs으로 certs를 인쇄하려고하면 아무 것도 얻을 수 없습니다. 위의 코드의이 조각은 일반적으로 인증서를 추가해야합니다

CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator(); 
CMSTypedData msg; 
List<X509Certificate> certList = new ArrayList<>(); 
// Make sure the certificate is not null 
if (this.certificate != null) { 
    certList.add((X509Certificate) this.certificate); 
} 

/** 
* Create the signed CMS message to be contained inside the envelope 
* this message does not contain any message, and no signerInfo 
**/ 
CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); 
Collection<JcaX509CertificateHolder> x509CertificateHolder = new ArrayList<>(); 
try { 
    for (X509Certificate certificate : certList) { 
     x509CertificateHolder.add(new JcaX509CertificateHolder(certificate)); 
    } 
    CollectionStore<JcaX509CertificateHolder> store = new CollectionStore<>(x509CertificateHolder); 
    gen.addCertificates(store); 
} catch (Handle all exceptions) {} 

: 여기

내가 (이 많은 코드 만 문제를 재현하기 충분) 탄력 성 내 데이터에 서명하는 방법입니다. EJBCA에서 가져 왔습니다.

CMSSignedDataGenerator gen1 = new CMSSignedDataGenerator(); 
// I add ALL of my attributes here 
// Once they're added... 
Certificate caCert = this.caCertificate; 
try { 
    String provider = BouncyCastleProvider.PROVIDER_NAME; 
    ContentSigner contentSigner = new JcaContentSignerBuilder(signatureAlgorithmName). 
      setProvider(provider). 
      build(signerKey); 
    JcaDigestCalculatorProviderBuilder calculatorProviderBuilder = new JcaDigestCalculatorProviderBuilder(). 
      setProvider(provider); 
    JcaSignerInfoGeneratorBuilder builder = new JcaSignerInfoGeneratorBuilder(calculatorProviderBuilder.build()); 
    builder.setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(attributes))); 
    gen1.addSignerInfoGenerator(builder.build(contentSigner, (X509Certificate) ca)); 
} catch (Handle all exceptions) {} 

// Create the signed data 
CMSSignedData sd = gen1.generate(msg, true); 
byte[] results = sd.getEncoded(); 

바이트 배열 결과 데이터를 서명 PKCS7 포맷 된 DER입니다 ...하지만 인증서가 추가되지 않습니다 : 여기에

내가 서명 된 데이터를 작성하는 방법입니다.

내가 누락 된 항목이 있습니까? 도와 줘서 고마워!

답변

0

CMSSignedDataGenerator gen1에 명시 적으로 인증서를 추가해야합니다.

그것은 간단하게 수행 할 수 있습니다

  • ListX509Certificates의에 인증서를 추가;
  • ListCollectionJcaX509CertificateHolder으로 변환합니다.
  • CollectionStore에이 컬렉션을 추가하면 JcaX509CertificateHolder입니다.
  • 상점을 추가하면 CMSSignedDataGenerator입니다.

코드 샘플 :

CMSSignedDataGenerator gen1 = new CMSSignedDataGenerator(); 
List<X509Certificate> certificates = new ArrayList<>(); 

// I chose to add the CA certificate 
certificates.add((X509Certificate) this.caCertificate); 

// In this case, this is a certificate that I need to add 
if (this.certificate != null) 
    certificates.add((X509Certificate) this.certificate); 

// This is the recipient certificate 
if (this.recipientCert != null) 
    certificates.add((X509Certificate) this.recipientCert); 
Collection<JcaX509CertificateHolder> x509CertificateHolder = new ArrayList<>(); 

// Of course, we need to handle the exceptions... 
for (X509Certificate certificate : certificates) { 
    x509CertificateHolder.add(new JcaX509CertificateHolder(certificate)); 
} 
CollectionStore<JcaX509CertificateHolder> store = new CollectionStore<>(x509CertificateHolder); 

// The final stage. 
gen1.addCertificates(store); 

희망이 미래에 누군가를 도와줍니다.

+0

안녕하세요, 위의 해결책을 시도했지만 불행히도 최종 디지털 서명에 인증서가 없습니다. 디지털 서명에 인증서가 있는지 확인 했습니까? –

+0

안녕하세요, 늦게 답장을 드려 죄송합니다. 불행히도 더 이상 소스 코드에 액세스 할 수 없지만 여전히 도움을 드릴 수는 있습니다. 나는 디지털 서명 ('CSMSignedDataGenerator')을 생성 한 후 인증서 ('this.certificate')를 얻는 것을 상기합니다. CA가 없으므로 CA를 추가하지 않았습니다. 'certificates'리스트에 인증서를 추가했는지 확인 했습니까? –