commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1166616 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ssl/ native/include/acr/ native/modules/openssl/ test/org/apache/commons/runtime/
Date Thu, 08 Sep 2011 10:45:06 GMT
Author: mturk
Date: Thu Sep  8 10:45:05 2011
New Revision: 1166616

URL: http://svn.apache.org/viewvc?rev=1166616&view=rev
Log:
Split init to two additional classes

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Engine.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Library.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
    commons/sandbox/runtime/trunk/src/main/native/modules/openssl/init.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Engine.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Engine.java?rev=1166616&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Engine.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Engine.java
Thu Sep  8 10:45:05 2011
@@ -0,0 +1,66 @@
+/* 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.commons.runtime.ssl;
+
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
+
+import java.io.File;
+
+/**
+ * OpenSSL Engine
+ */
+public final class Engine
+{
+    private static boolean inited = false;
+    private static Object  lock;
+
+    private Engine()
+    {
+        // No instance
+    }
+
+    static {
+        lock = new Object();
+    }
+
+
+    private static native int           init0(String name);
+
+    public static void initialize(String name)
+        throws SystemException
+    {
+        synchronized(lock) {
+            if (!inited) {
+                int rc = init0(name);
+                if (rc != 0)
+                    throw new SystemException(Status.describe(rc));
+                inited = true;
+            }
+        }
+    }
+
+    public static boolean initialized()
+    {
+        synchronized(lock) {
+            return inited;
+        }
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Engine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Library.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Library.java?rev=1166616&r1=1166615&r2=1166616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Library.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Library.java
Thu Sep  8 10:45:05 2011
@@ -41,18 +41,16 @@ public final class Library
     }
 
 
-    private static native int           init0(String engine);
+    private static native int           init0();
     private static native void          fipsmode0(boolean enable)
         throws UnsupportedOperationException;
 
-    private static native void          rndfile0(String path);
-
-    public static void initialize(String engine)
+    public static void initialize()
         throws SystemException
     {
         synchronized(lock) {
             if (!inited) {
-                int rc = init0(engine);
+                int rc = init0();
                 if (rc != 0)
                     throw new SystemException(Status.describe(rc));
                 inited = true;
@@ -80,14 +78,5 @@ public final class Library
         fipsmode0(enable);
     }
 
-    public static void setRandomFile(File path)
-        throws IllegalArgumentException
-    {
-        String fpath = path.getPath();
-        if (fpath == null || fpath.length() < 1)
-            throw new IllegalArgumentException();
-        rndfile0(fpath);
-    }
-
 }
 

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java?rev=1166616&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
Thu Sep  8 10:45:05 2011
@@ -0,0 +1,68 @@
+/* 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.commons.runtime.ssl;
+
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
+
+import java.io.File;
+
+/**
+ * Random number gererator
+ */
+public final class Random
+{
+
+    /**
+     * Creates a new random number generator.
+     */
+    public Random()
+    {
+    }
+
+    private static native boolean       seed0(String path);
+    private static native boolean       seed1();
+    private static native String        getdef0();
+    private static native void          setdef0(String path);
+
+    public static boolean seed(String path)
+    {
+        return seed0(path);
+    }
+
+    public static boolean seed()
+    {
+        return seed1();
+    }
+
+    public static void setSeedFile(File path)
+        throws SystemException
+    {
+        setdef0(path.getPath());
+    }
+
+    public static File getSeedFile()
+        throws SystemException
+    {
+        String path = getdef0();
+        return new File(path);
+    }
+
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h?rev=1166616&r1=1166615&r2=1166616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h Thu Sep  8 10:45:05
2011
@@ -51,6 +51,12 @@
     if (_s##V == 0 && V != 0) goto _e##V;
 #endif
 
+#define WITH_DSTR(V)                                                        \
+    do {                                                                    \
+    char *_b##V = 0;                                                        \
+    char *_s##V = AcrGetJavaStringA(env, V, _b##V);                         \
+    if (_s##V == 0 && V != 0) goto _e##V;
+    
 #define WITH_DWCS(V)                                                        \
     do {                                                                    \
     wchar_t *_b##V = 0;                                                     \

Modified: commons/sandbox/runtime/trunk/src/main/native/modules/openssl/init.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/modules/openssl/init.c?rev=1166616&r1=1166615&r2=1166616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/modules/openssl/init.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/modules/openssl/init.c Thu Sep  8 10:45:05
2011
@@ -188,34 +188,6 @@ static void ssl_dynlock_destroy(struct C
     AcrFree(l);
 }
 
-static int ssl_thread_setup(void)
-{
-    int i;
-
-    ssl_num_mutexes = CRYPTO_num_locks();
-    if (ssl_num_mutexes < 1)
-        return ACR_ENOSYS;
-    ssl_lock_mutex = malloc(ssl_num_mutexes * sizeof(acr_mutex_t *));
-    if (ssl_lock_mutex == 0)
-        return ACR_ENOMEM;
-    for (i = 0; i < ssl_num_mutexes; i++) {
-        int rc = AcrThreadMutexCreate(&ssl_lock_mutex[i]);
-        if (rc != 0)
-            return rc;
-    }
-
-    CRYPTO_set_id_callback(ssl_thread_id);
-    CRYPTO_set_locking_callback(ssl_thread_lock);
-
-    /* Set up dynamic locking scaffolding for OpenSSL to use at its
-     * convenience.
-     */
-    CRYPTO_set_dynlock_create_callback(ssl_dynlock_create);
-    CRYPTO_set_dynlock_lock_callback(ssl_dynlock_lock);
-    CRYPTO_set_dynlock_destroy_callback(ssl_dynlock_destroy);
-    return 0;
-}
-
 static int ssl_rand_choosenum(int l, int h)
 {
     int i;
@@ -239,7 +211,7 @@ static int ssl_rand_load_file(const char
         return -1;
     if (*file == '\0')
         file = RAND_file_name(buffer, sizeof(buffer));
-    if (file != 0) {
+    if (file != 0 && *file != '\0') {
         if (strncmp(file, "egd:", 4) == 0) {
             if ((n = RAND_egd(file + 4)) > 0)
                 return n;
@@ -287,12 +259,13 @@ int ACRSSL_rand_seed(const char *file)
 
 static int ssl_initialized = 0;
 
-ACR_SSL_EXPORT(jint, Library, init0)(JNI_STDARGS, jstring engine)
+ACR_SSL_EXPORT(jint, Library, init0)(JNI_STDARGS)
 {
-    int rc;
+    int i, rc = 0;
 
     if (ssl_initialized != 0)
         return 0;
+    memset(&ACRSSL_password_cb, 0, sizeof(ssl_pass_cb_t));
     /* We must register the library in full, to ensure our configuration
      * code can successfully test the SSL environment.
      */
@@ -304,47 +277,31 @@ ACR_SSL_EXPORT(jint, Library, init0)(JNI
     ENGINE_load_builtin_engines();
 #endif
     OPENSSL_load_builtin_modules();
-    rc = ssl_thread_setup();
-    if (rc != 0)
-        return rc;
 
-#ifndef OPENSSL_NO_ENGINE
-    if (engine != 0) {
-        WITH_CSTR(engine) {
-            ENGINE *ee = 0;
-            if (strcmp(J2S(engine), "auto") == 0) {
-                ENGINE_register_all_complete();
-            }
-            else {
-                if ((ee = ENGINE_by_id(J2S(engine))) == 0 &&
-                    (ee = ssl_try_load_engine(J2S(engine))) == 0)
-                    rc = ACR_ENOTIMPL;
-                else {
-                    if (strcmp(J2S(engine), "chil") == 0)
-                        ENGINE_ctrl(ee, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
-                    if (!ENGINE_set_default(ee, ENGINE_METHOD_ALL))
-                        rc = ACR_ENOTIMPL;
-                }
-                /* Free our "structural" reference. */
-                if (ee != 0)
-                    ENGINE_free(ee);
-            }
-        } DONE_WITH_STR(engine);
+    ssl_num_mutexes = CRYPTO_num_locks();
+    if (ssl_num_mutexes < 1)
+        return ACR_ENOSYS;
+    ssl_lock_mutex = malloc(ssl_num_mutexes * sizeof(acr_mutex_t *));
+    if (ssl_lock_mutex == 0)
+        return ACR_ENOMEM;
+    for (i = 0; i < ssl_num_mutexes; i++) {
+        rc = AcrThreadMutexCreate(&ssl_lock_mutex[i]);
+        if (rc != 0)
+            return rc;
     }
-#endif
-    if (rc != 0) {
 
-        return rc;
-    }
-    memset(&ACRSSL_password_cb, 0, sizeof(ssl_pass_cb_t));
-    /* Initialize PRNG
-     * This will in most cases call the builtin
-     * low entropy seed.
+    CRYPTO_set_id_callback(ssl_thread_id);
+    CRYPTO_set_locking_callback(ssl_thread_lock);
+
+    /* Set up dynamic locking scaffolding for OpenSSL to use at its
+     * convenience.
      */
-    ACRSSL_rand_seed(0);
+    CRYPTO_set_dynlock_create_callback(ssl_dynlock_create);
+    CRYPTO_set_dynlock_lock_callback(ssl_dynlock_lock);
+    CRYPTO_set_dynlock_destroy_callback(ssl_dynlock_destroy);
+
     /* For SSL_get_app_data2() at request time */
     ACRSSL_init_app_data2_idx();
-
     SSL_TMP_KEYS_INIT(rc);
     if (rc != 0) {
         /* One of the inits failed
@@ -355,13 +312,6 @@ ACR_SSL_EXPORT(jint, Library, init0)(JNI
     return 0;
 }
 
-ACR_SSL_EXPORT(void, Library, rndfile0)(JNI_STDARGS, jstring path)
-{
-    WITH_CSTR(path) {
-        strlcpy(ssl_global_rand_file, J2S(path), PATH_MAX);
-    } DONE_WITH_STR(path);
-}
-
 ACR_SSL_EXPORT(jboolean, Library, hasFipsMode)(JNI_STDARGS)
 {
 #if defined(OPENSSL_FIPS)
@@ -385,3 +335,70 @@ ACR_SSL_EXPORT(void, Library, fipsmode0)
     ACR_THROW_MSG(ACR_EX_ENOSYS, "FIPS was not available at build time. You will need an
OpenSSL with FIPS support.");
 #endif
 }
+
+ACR_SSL_EXPORT(jint, Engine, init0)(JNI_STDARGS, jstring name)
+{
+#ifndef OPENSSL_NO_ENGINE
+    int rc = 0;
+    WITH_CSTR(name) {
+        ENGINE *ee = 0;
+        rc = 0;
+        if (strcmp(J2S(name), "auto") == 0) {
+            ENGINE_register_all_complete();
+        }
+        else {
+            if ((ee = ENGINE_by_id(J2S(name))) == 0 &&
+                (ee = ssl_try_load_engine(J2S(name))) == 0)
+                rc = ACR_ENOTIMPL;
+            else {
+                if (strcmp(J2S(name), "chil") == 0)
+                    ENGINE_ctrl(ee, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
+                if (!ENGINE_set_default(ee, ENGINE_METHOD_ALL))
+                    rc = ACR_ENOTIMPL;
+            }
+            /* Free our "structural" reference. */
+            if (ee != 0)
+                ENGINE_free(ee);
+        }
+    } DONE_WITH_STR(name);
+    return rc;
+#else
+    return ACR_ENOTIMPL;
+#endif
+}
+
+ACR_SSL_EXPORT(jboolean, Random, seed0)(JNI_STDARGS, jstring file)
+{
+    int rc = 0;
+    /* Initialize PRNG
+     * This will in most cases call the builtin
+     * low entropy seed.
+     */
+    WITH_CSTR(file) {
+        rc = ACRSSL_rand_seed(J2S(file));
+    } DONE_WITH_STR(file);
+    return rc == 0 ? JNI_FALSE : JNI_TRUE;
+}
+
+ACR_SSL_EXPORT(jboolean, Random, seed1)(JNI_STDARGS)
+{
+    return ACRSSL_rand_seed(0) == 0 ? JNI_FALSE : JNI_TRUE;
+}
+
+ACR_SSL_EXPORT(jstring, Random, getdef0)(JNI_STDARGS)
+{
+    char buffer[PATH_MAX];
+    if (ssl_global_rand_file[0] != '\0')
+        return AcrNewJavaStringA(env, ssl_global_rand_file);
+    if (RAND_file_name(buffer, sizeof(buffer)) != 0)
+        return AcrNewJavaStringA(env, buffer);
+    else
+        return 0;
+}
+
+ACR_SSL_EXPORT(void, Random, setdef0)(JNI_STDARGS, jstring path)
+{
+    WITH_CSTR(path) {
+        strlcpy(ssl_global_rand_file, J2S(path), PATH_MAX);
+    } DONE_WITH_STR(path);
+}

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java?rev=1166616&r1=1166615&r2=1166616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
Thu Sep  8 10:45:05 2011
@@ -36,8 +36,10 @@ public class TestOpenSSL extends Assert
     @Test(groups = { "openssl" })
     public void sslInit()
     {
-        Library.initialize(null);
+        Library.initialize();
         assertTrue(Library.initialized());
+        System.out.println("PRNG file: " + Random.getSeedFile());
+        System.out.println("PRNG seed: " + Random.seed());
     }
 
 



Mime
View raw message