commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject commons-crypto git commit: If JNA cannot find the correct library, ensure the details are caught
Date Thu, 07 Jul 2016 16:31:44 GMT
Repository: commons-crypto
Updated Branches:
  refs/heads/master 088b2b7e8 -> 5f1cc557f


If JNA cannot find the correct library, ensure the details are caught

Don't try to create classes if the JNA libray is not enabled

Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/5f1cc557
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/5f1cc557
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/5f1cc557

Branch: refs/heads/master
Commit: 5f1cc557f5d24008dccb12abf38335aa4d6b6e38
Parents: 088b2b7
Author: Sebb <sebb@apache.org>
Authored: Thu Jul 7 17:31:39 2016 +0100
Committer: Sebb <sebb@apache.org>
Committed: Thu Jul 7 17:31:39 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/crypto/jna/OpenSslJna.java   |  9 +++++++++
 .../commons/crypto/jna/OpenSslJnaCipher.java    |  3 +++
 .../crypto/jna/OpenSslJnaCryptoRandom.java      |  9 ++++++---
 .../commons/crypto/jna/OpenSslNativeJna.java    | 20 ++++++++++++++++++--
 4 files changed, 36 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/5f1cc557/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java
index cfdff2e..b5c90a1 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java
@@ -32,4 +32,13 @@ public final class OpenSslJna {
     public static Class<? extends CryptoRandom> getRandomClass() {
         return OpenSslJnaCryptoRandom.class;
     }
+
+    public static boolean isEnabled() {
+        return OpenSslNativeJna.INIT_OK;
+    }
+
+    public static Throwable initialisationError() {
+        return OpenSslNativeJna.INIT_ERROR;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/5f1cc557/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
index 6824230..285fbe7 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java
@@ -61,6 +61,9 @@ class OpenSslJnaCipher implements CryptoCipher {
      */
     public OpenSslJnaCipher(Properties props, String transformation)
             throws GeneralSecurityException {
+        if (!OpenSslJna.isEnabled()) {
+            throw new GeneralSecurityException("Could not enable JNA access", OpenSslJna.initialisationError());
+        }
         this.transformation = transformation;
         Transform transform = tokenizeTransformation(transformation);
         algMode = AlgorithmMode.get(transform.algorithm, transform.mode);

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/5f1cc557/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
index d2e4406..aa245e6 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandom.java
@@ -18,6 +18,7 @@
 package org.apache.commons.crypto.jna;
 
 import java.nio.ByteBuffer;
+import java.security.GeneralSecurityException;
 import java.security.NoSuchAlgorithmException;
 import java.util.Properties;
 import java.util.Random;
@@ -54,11 +55,13 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom {
      * Constructs a {@link OpenSslJnaCryptoRandom}.
      *
      * @param props the configuration properties (not used)
-     * @throws NoSuchAlgorithmException if no Provider supports a
-     *         SecureRandomSpi implementation for the specified algorithm.
+     * @throws GeneralSecurityException  if could not enable JNA access
      */
     public OpenSslJnaCryptoRandom(Properties props)
-            throws NoSuchAlgorithmException {
+            throws GeneralSecurityException {
+        if (!OpenSslJna.isEnabled()) {
+            throw new GeneralSecurityException("Could not enable JNA access", OpenSslJna.initialisationError());
+        }
 
         boolean rdrandLoaded = false;
         try {

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/5f1cc557/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
index 6adddb2..f75566e 100644
--- a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
+++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java
@@ -31,9 +31,25 @@ class OpenSslNativeJna {
     static final int OOSL_JNA_ENCRYPT_MODE = 1;
     static final int OOSL_JNA_DECRYPT_MODE = 0;
 
+    static final boolean INIT_OK;
+ 
+    static final Throwable INIT_ERROR;
+
     static {
-        Native.register("crypto");
-        ERR_load_crypto_strings();
+        boolean ok = false;
+        Throwable thrown = null;
+        try {
+            Native.register("crypto");
+            ERR_load_crypto_strings();
+            ok = true;
+        } catch (Exception e) {
+            thrown = e;
+        } catch (UnsatisfiedLinkError e) {
+            thrown = e;
+        } finally {
+            INIT_OK = ok;
+            INIT_ERROR = thrown;
+        }
     }
 
     //misc


Mime
View raw message