2016-09-07 5 views
2

PKCS7コンテナを使用してPDFファイルに切り離された署名を作成したいと思います。データ(ハッシュ)は、秘密鍵を持つ別のデバイスであらかじめ署名されています。署名付きのデータを含むPKCS7を公開鍵を使って証明書と共に作成したいと思います。プライベートキーを提供せずにライブラリにデータを署名することなく、弾力のある城を持つPKCS7を作成することはできません。これは動作していないよう:弾力のある城を使用して、事前に設定されたデータでPKCS7を作成する

 InputStream inStream = new FileInputStream("1_public.pem"); 
     BufferedInputStream bis = new BufferedInputStream(inStream); 

     CertificateFactory cf = CertificateFactory.getInstance("X.509"); 

     List<Certificate> certList = new ArrayList<Certificate>(); 
     Certificate certificate = cf.generateCertificate(bis); 
     certList.add(certificate); 
     Store certs = new JcaCertStore(certList); 

     CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); 
     gen.addCertificates(certs); 
     CMSProcessableInputStream msg = new CMSProcessableInputStream(new ByteArrayInputStream("signedhash".getBytes())); 

     CMSSignedData signedData = gen.generate(msg, false); 
     byte[] pkcs7 = signedData.getEncoded())); 
+0

どのように動作しないかを指定してください。 – bmargulies

+0

署名されたデータがpkcs7にありません – rob2000

答えて

3

私は、実際には非常に単純でサインインしていませんContentSignerを提供することにより、これを行うために管理:

 InputStream inStream = new FileInputStream("1_public.pem"); 
     BufferedInputStream bis = new BufferedInputStream(inStream); 

     CertificateFactory cf = CertificateFactory.getInstance("X.509"); 

     List<Certificate> certList = new ArrayList<Certificate>(); 
     Certificate certificate = cf.generateCertificate(bis); 
     certList.add(certificate); 
     Store certs = new JcaCertStore(certList); 
     CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); 
     gen.addCertificates(certs); 

     final byte[] signedHash = "signedhash".getBytes(); 

     ContentSigner nonSigner = new ContentSigner() { 

      @Override 
      public byte[] getSignature() { 
       return signedHash; 
      } 

      @Override 
      public OutputStream getOutputStream() { 
       return new ByteArrayOutputStream(); 
      } 

      @Override 
      public AlgorithmIdentifier getAlgorithmIdentifier() { 
       return new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WithRSA"); 
      } 
     }; 

     org.bouncycastle.asn1.x509.Certificate cert = org.bouncycastle.asn1.x509.Certificate.getInstance(ASN1Primitive.fromByteArray(certificate.getEncoded())); 
     JcaSignerInfoGeneratorBuilder sigb = new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build()); 
     sigb.setDirectSignature(true); 
     gen.addSignerInfoGenerator(sigb.build(nonSigner, new X509CertificateHolder(cert))); 
     CMSProcessableInputStream msg = new CMSProcessableInputStream(new ByteArrayInputStream("not used".getBytes())); 

     CMSSignedData signedData = gen.generate(msg, false); 
     byte[] pkcs7 = signedData.getEncoded(); 
0

「外部の署名」を行う場合にはハードウェアデバイスによって、「署名された属性」も含む可能性があります。この場合、コードも含まれている必要があります

AttributeTable signedAttributes = signer.getSignedAttributes(); 
signerInfoBuilder.setSignedAttributeGenerator(new SimpleAttributeTableGenerator(signedAttributes));  
signatureGenerator.addSignerInfoGenerator(signerInfoBuilder.build(nonSigner, signCertificate)); 

あなたはまた、完全な例が、ここでhttps://www.len.ro/work/attach-payload-into-detached-pkcs7-signature/見つけることができます

signatureGenerator.setDirectSignature(true) 

を削除する必要があります。私は解決策を探すのに多くの時間を費やしているので、この投稿は重要な手がかりを与えてくれました。ありがとう。

+3

あなたのサイトであればリンクに所属を追加してください –

関連する問題