pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1599281 - in /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature: CreateSignature.java CreateVisibleSignature.java
Date Mon, 02 Jun 2014 17:52:55 GMT
Author: lehmi
Date: Mon Jun  2 17:52:55 2014
New Revision: 1599281

URL: http://svn.apache.org/r1599281
Log:
PDFBOX-2088: removed the remaining deprecated bouncy castle calls to be compatible with bc
1.50

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java?rev=1599281&r1=1599280&r2=1599281&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
Mon Jun  2 17:52:55 2014
@@ -32,7 +32,7 @@ import java.security.NoSuchAlgorithmExce
 import java.security.PrivateKey;
 import java.security.UnrecoverableKeyException;
 import java.security.cert.Certificate;
-import java.security.cert.X509Certificate;
+import java.security.interfaces.RSAPrivateKey;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collection;
@@ -53,14 +53,21 @@ import org.bouncycastle.asn1.cms.Attribu
 import org.bouncycastle.asn1.cms.AttributeTable;
 import org.bouncycastle.asn1.cms.Attributes;
 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
+import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 import org.bouncycastle.cert.X509CertificateHolder;
 import org.bouncycastle.cms.CMSException;
 import org.bouncycastle.cms.CMSSignedData;
 import org.bouncycastle.cms.CMSSignedDataGenerator;
-import org.bouncycastle.cms.CMSSignedGenerator;
+import org.bouncycastle.cms.SignerInfoGeneratorBuilder;
 import org.bouncycastle.cms.SignerInformation;
 import org.bouncycastle.cms.SignerInformationStore;
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.crypto.params.RSAKeyParameters;
+import org.bouncycastle.operator.ContentSigner;
+import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
+import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
+import org.bouncycastle.operator.OperatorCreationException;
+import org.bouncycastle.operator.bc.BcDigestCalculatorProvider;
+import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder;
 import org.bouncycastle.tsp.TSPException;
 
 /**
@@ -264,13 +271,22 @@ public class CreateSignature implements 
     {
         try
         {
-            CMSProcessableInputStream processable = new CMSProcessableInputStream(content);
             org.bouncycastle.asn1.x509.Certificate certificate =
                     org.bouncycastle.asn1.x509.Certificate.getInstance(ASN1Primitive.fromByteArray(certificateChain[0].getEncoded()));
             CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
-            gen.addSigner(privateKey, (X509Certificate) certificateChain[0], CMSSignedGenerator.DIGEST_SHA256);
-            gen.addCertificate(new X509CertificateHolder(certificate));
-            CMSSignedData signedData = gen.generate(processable, false, new BouncyCastleProvider());
+
+            
+            AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WITHRSAENCRYPTION");
+            AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
+            RSAPrivateKey privateRSAKey = (RSAPrivateKey)privateKey; 
+            RSAKeyParameters keyParams = new RSAKeyParameters(true, privateRSAKey.getModulus(),
privateRSAKey.getPrivateExponent()); 
+            ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(keyParams);
+
+            gen.addSignerInfoGenerator(
+                    new SignerInfoGeneratorBuilder(new BcDigestCalculatorProvider())
+                        .build(sigGen, new X509CertificateHolder(certificate)));
+            CMSProcessableInputStream processable = new CMSProcessableInputStream(content);
+            CMSSignedData signedData = gen.generate(processable, false);
             if (tsaClient != null)
             {
                 signedData = signTimeStamps(signedData);
@@ -289,6 +305,10 @@ public class CreateSignature implements 
         {
             throw new IOException(e);
         }
+        catch (OperatorCreationException e)
+        {
+            throw new IOException(e);
+        }
     }
 
     public static void main(String[] args) throws IOException, GeneralSecurityException

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java?rev=1599281&r1=1599280&r2=1599281&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java
Mon Jun  2 17:52:55 2014
@@ -28,7 +28,7 @@ import java.security.PrivateKey;
 import java.security.UnrecoverableKeyException;
 import java.security.cert.Certificate;
 import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
+import java.security.interfaces.RSAPrivateKey;
 import java.util.Calendar;
 import java.util.Enumeration;
 
@@ -39,11 +39,18 @@ import org.apache.pdfbox.pdmodel.interac
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigProperties;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSignDesigner;
 import org.bouncycastle.asn1.ASN1Primitive;
+import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
 import org.bouncycastle.cert.X509CertificateHolder;
 import org.bouncycastle.cms.CMSSignedData;
 import org.bouncycastle.cms.CMSSignedDataGenerator;
-import org.bouncycastle.cms.CMSSignedGenerator;
+import org.bouncycastle.cms.SignerInfoGeneratorBuilder;
+import org.bouncycastle.crypto.params.RSAKeyParameters;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.operator.ContentSigner;
+import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
+import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
+import org.bouncycastle.operator.bc.BcDigestCalculatorProvider;
+import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder;
 
 /**
  * This is an example for visual signing a pdf with bouncy castle.
@@ -183,15 +190,22 @@ public class CreateVisibleSignature impl
     @Override
     public byte[] sign(InputStream content) throws IOException
     {
-        CMSProcessableInputStream input = new CMSProcessableInputStream(content);
-        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
         try
         {
             org.bouncycastle.asn1.x509.Certificate certificate = 
                     org.bouncycastle.asn1.x509.Certificate.getInstance(ASN1Primitive.fromByteArray(cert[0].getEncoded()));

-            gen.addSigner(privKey, (X509Certificate) cert[0], CMSSignedGenerator.DIGEST_SHA256);
-            gen.addCertificate(new X509CertificateHolder(certificate));
-            CMSSignedData signedData = gen.generate(input, false, new BouncyCastleProvider());
+
+            AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WITHRSAENCRYPTION");
+            AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
+            RSAPrivateKey privateRSAKey = (RSAPrivateKey)privKey; 
+            RSAKeyParameters keyParams = new RSAKeyParameters(true, privateRSAKey.getModulus(),
privateRSAKey.getPrivateExponent()); 
+            ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(keyParams);
+            CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
+            gen.addSignerInfoGenerator(
+                    new SignerInfoGeneratorBuilder(new BcDigestCalculatorProvider())
+                        .build(sigGen, new X509CertificateHolder(certificate)));
+            CMSProcessableInputStream processable = new CMSProcessableInputStream(content);
+            CMSSignedData signedData = gen.generate(processable, false);
             return signedData.getEncoded();
         }
         catch (Exception e)



Mime
View raw message