You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2017/03/29 20:57:41 UTC

svn commit: r1789414 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption: PublicKeySecurityHandler.java SecurityHandlerFactory.java SecurityProvider.java

Author: lehmi
Date: Wed Mar 29 20:57:40 2017
New Revision: 1789414

URL: http://svn.apache.org/viewvc?rev=1789414&view=rev
Log:
PDFBOX-2963: introduced a singleton holding the security provider to used for encryption/decryption

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java?rev=1789414&r1=1789413&r2=1789414&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java Wed Mar 29 20:57:40 2017
@@ -412,9 +412,10 @@ public final class PublicKeySecurityHand
         Cipher cipher;
         try
         {
-            apg = AlgorithmParameterGenerator.getInstance(algorithm);
-            keygen = KeyGenerator.getInstance(algorithm);
-            cipher = Cipher.getInstance(algorithm);
+            apg = AlgorithmParameterGenerator.getInstance(algorithm,
+                    SecurityProvider.getProvider());
+            keygen = KeyGenerator.getInstance(algorithm, SecurityProvider.getProvider());
+            cipher = Cipher.getInstance(algorithm, SecurityProvider.getProvider());
         }
         catch (NoSuchAlgorithmException e)
         {
@@ -469,7 +470,8 @@ public final class PublicKeySecurityHand
         Cipher cipher;
         try
         {
-            cipher = Cipher.getInstance(algorithmId.getAlgorithm().getId());
+            cipher = Cipher.getInstance(algorithmId.getAlgorithm().getId(),
+                    SecurityProvider.getProvider());
         }
         catch (NoSuchAlgorithmException e)
         {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java?rev=1789414&r1=1789413&r2=1789414&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandlerFactory.java Wed Mar 29 20:57:40 2017
@@ -23,8 +23,6 @@ import java.security.Security;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-
 /**
  * Manages security handlers for the application.
  * It follows the singleton pattern.
@@ -39,11 +37,6 @@ public final class SecurityHandlerFactor
     /** Singleton instance */
     public static final SecurityHandlerFactory INSTANCE = new SecurityHandlerFactory();
 
-    static
-    {
-        Security.addProvider(new BouncyCastleProvider());
-    }
-
     private final Map<String, Class<? extends SecurityHandler>> nameToHandler =
             new HashMap<String, Class<? extends SecurityHandler>>();
 

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java?rev=1789414&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java Wed Mar 29 20:57:40 2017
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.encryption;
+
+import java.io.IOException;
+import java.security.Provider;
+
+/**
+ * Singleton which provides a security provider.
+ * 
+ */
+public class SecurityProvider
+{
+    private static Provider provider = null;
+
+    private SecurityProvider()
+    {
+    }
+
+    /**
+     * Returns the provider to be used for advanced encrypting/decrypting. Default is the BouncyCastleProvider.
+     * 
+     * @return the security provider
+     * 
+     * @throws IOException if the default provider can't be instantiated
+     */
+    public static Provider getProvider() throws IOException
+    {
+        // TODO synchronize access
+        if (provider == null)
+        {
+            try
+            {
+                Class<Provider> providerClass = (Class<Provider>) Class
+                        .forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
+                provider = providerClass.newInstance();
+            }
+            catch (ClassNotFoundException ex)
+            {
+                throw new IOException(ex);
+            }
+            catch (InstantiationException ex)
+            {
+                throw new IOException(ex);
+            }
+            catch (IllegalAccessException ex)
+            {
+                throw new IOException(ex);
+            }
+        }
+        return provider;
+    }
+
+    /**
+     * Set the provider to be used for advanced encrypting/decrypting.
+     * 
+     * @param provider the security provider
+     */
+    public static void setProvider(Provider provider)
+    {
+        SecurityProvider.provider = provider;
+    }
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native