Commits

Kaya Kupferschmidt  committed 6c61bc6

Added new OpenSSL init library

  • Participants
  • Parent commits 8580aa1

Comments (0)

Files changed (23)

File source/libs/SConscript

         'magnum_io_text',
         'magnum_io_serialise',
         #'magnum_io_archive',
+		'magnum_openssl',
         'magnum_crypto',
         'magnum_util_zip',
         'magnum_util_prefs',

File source/libs/magnum_crypto/magnum_crypto.files

 unittest/Test_MD5Digester.h
 unittest/Test_NullCipher.h
 unittest/Test_SHADigester.h
+source/SConscript

File source/libs/magnum_crypto/source/SConscript

 env = DefaultEnvironment().Clone()		   
 env.Append(CPPDEFINES = ['COMPILE_MAGNUM_CRYPTO'])
 env.Append(CPPPATH = includes)
-env.ImportModules(['magnum_core', 'openssl'])
+env.ImportModules(['magnum_core', 'magnum_openssl'])
 lib = env.SharedLibrary('magnum_crypto', sources, PDB='magnum_crypto.pdb')
 env.Install(dir = '#/installed/lib', source = lib)
 env.Install(dir = '#/installed/bin', source = lib[0])
 
 module = Module('magnum_crypto')
-module.Import(['magnum_core', 'openssl'])
+module.Import(['magnum_core', 'magnum_openssl'])
 module.Append(LIBS = 'magnum_crypto')
 module.Append(LIBPATH = '#/installed/lib')
 module.Append(CPPPATH = includes)

File source/libs/magnum_crypto/source/magnum/crypto/BlowfishCipher.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/Exception.h"
 #include "magnum/crypto/BlowfishKey.h"
 #include "magnum/crypto/BlowfishCipher.h"
 
 namespace magnum { namespace crypto {
 using magnum::object::IllegalArgumentException;
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 , m_Encrypt(0)
 , m_INum(0)
 {
+    OpenSSL::startup();
 }
 
 

File source/libs/magnum_crypto/source/magnum/crypto/MD4Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/MD4Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 MD4Digester::MD4Digester()
 : AbstractDigester("MD4")
 {
+    OpenSSL::startup();
     MD4_Init(&m_Context);
 }
 

File source/libs/magnum_crypto/source/magnum/crypto/MD5Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/MD5Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 MD5Digester::MD5Digester()
 : AbstractDigester("MD5")
 {
+    OpenSSL::startup();
     MD5_Init(&m_Context);
 }
 

File source/libs/magnum_crypto/source/magnum/crypto/SHA1Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/SHA1Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 SHA1Digester::SHA1Digester()
 : AbstractDigester("SHA1")
 {
+    OpenSSL::startup();
     SHA1_Init(&m_Context);
 }
 

File source/libs/magnum_crypto/source/magnum/crypto/SHA224Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/SHA224Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 SHA224Digester::SHA224Digester()
 : AbstractDigester("SHA224")
 {
+    OpenSSL::startup();
     SHA224_Init(&m_Context);
 }
 

File source/libs/magnum_crypto/source/magnum/crypto/SHA256Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/SHA256Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 SHA256Digester::SHA256Digester()
 : AbstractDigester("SHA256")
 {
+    OpenSSL::startup();
     SHA256_Init(&m_Context);
 }
 

File source/libs/magnum_crypto/source/magnum/crypto/SHA384Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/SHA384Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 SHA384Digester::SHA384Digester()
 : AbstractDigester("SHA384")
 {
+    OpenSSL::startup();
     SHA384_Init(&m_Context);
 }
 

File source/libs/magnum_crypto/source/magnum/crypto/SHA512Digester.cpp

   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/crypto/SHA512Digester.h"
 
 
 namespace magnum { namespace crypto {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
 SHA512Digester::SHA512Digester()
 : AbstractDigester("SHA512")
 {
+    OpenSSL::startup();
     SHA512_Init(&m_Context);
 }
 

File source/libs/magnum_net/source/SConscript

 env = DefaultEnvironment().Clone()
 env.Append(CPPDEFINES = ['COMPILE_MAGNUM_NET'])
 env.Append(CPPPATH = includes)
-env.ImportModules(['magnum_core','openssl'])
+env.ImportModules(['magnum_core','magnum_openssl'])
 lib = env.SharedLibrary('magnum_net', sources, PDB='magnum_net.pdb')
 env.Install(dir = '#/installed/lib', source = lib)
 env.Install(dir = '#/installed/bin', source = lib[0])
 
 module = Module('magnum_net')
-module.Import(['magnum_core','openssl'])
+module.Import(['magnum_core','magnum_openssl'])
 module.Append(LIBS = 'magnum_net')
 module.Append(LIBPATH = '#/installed/lib')
 module.Append(CPPPATH = includes)

File source/libs/magnum_net/source/magnum/net/SslSocket.cpp

-/* 
+/*
   Magnum 
   Copyright (C) 2002-2012 Kaya Kupferschmidt. All Rights Reserved.
 
   Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
 */
 
+#include "magnum/openssl/OpenSSL.h"
 #include "magnum/net/Exception.h"
 #include "magnum/net/SslSocket.h"
 
 
 namespace magnum { namespace net {
+using magnum::openssl::OpenSSL;
 
 
 /*--------------------------------------------------------------------------*/
     , m_SslMethod(SSLv23_client_method())
     , m_Method(SslSocket::SSLv23)
 {
-    SSL_library_init();
-    SSL_load_error_strings();
+    OpenSSL::startup();
 }
 
 
     , m_SslMethod(SSLv23_client_method())
     , m_Method(SslSocket::SSLv23)
 {
-    SSL_library_init();
-	SSL_load_error_strings();
+    OpenSSL::startup();
 }
 
 
     , m_SslMethod(SSLv23_client_method())
     , m_Method(SslSocket::SSLv23)
 {
-    SSL_library_init();
-	SSL_load_error_strings();
+    OpenSSL::startup();
 }
 
 
     , m_SslMethod(SSLv23_client_method())
     , m_Method(SslSocket::SSLv23)
 {
-    SSL_library_init();
-	SSL_load_error_strings();
+    OpenSSL::startup();
 }
 
 

File source/libs/magnum_openssl/SConscript

+import SCons.Util
+Import('Sources', 'Module', 'SymLink')
+
+subdirs = ['source', 'unittest']
+
+SConscript(dirs=subdirs)

File source/libs/magnum_openssl/magnum_openssl.config

+// ADD PREDEFINED MACROS HERE!

File source/libs/magnum_openssl/magnum_openssl.creator

+[General]

File source/libs/magnum_openssl/magnum_openssl.files

+SConscript
+source/SConscript
+source/magnum/openssl/OpenSSL.h
+source/magnum/openssl/DllExport.h
+source/magnum/openssl/OpenSSL.cpp
+unittest/SConscript

File source/libs/magnum_openssl/magnum_openssl.includes

Empty file added.

File source/libs/magnum_openssl/source/SConscript

+Import('Module')
+Import('Sources')
+Import('IncludeDir')
+
+srcdirs = ['magnum/openssl']
+sources = Sources(srcdirs,['*.cpp'])
+includes = IncludeDir('.')
+
+env = DefaultEnvironment().Clone()
+env.Append(CPPDEFINES = ['COMPILE_MAGNUM_OPENSSL'])
+env.Append(CPPPATH = includes)
+env.ImportModules(['magnum_core','openssl'])
+lib = env.SharedLibrary('magnum_openssl', sources, PDB='magnum_openssl.pdb')
+env.Install(dir = '#/installed/lib', source = lib)
+env.Install(dir = '#/installed/bin', source = lib[0])
+
+module = Module('magnum_openssl')
+module.Import(['magnum_core','openssl'])
+module.Append(LIBS = 'magnum_openssl')
+module.Append(LIBPATH = '#/installed/lib')
+module.Append(CPPPATH = includes)

File source/libs/magnum_openssl/source/magnum/openssl/DllExport.h

+/*
+  Magnum
+  Copyright (C) 2002-2012 Kaya Kupferschmidt. All Rights Reserved.
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial nets, and to alter it and redistribute it
+  freely, under the terms in LICENSE.TXT.
+
+  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
+*/
+
+#ifndef __MAGNUM_OPENSSL_DLLEXPORT_H
+#define __MAGNUM_OPENSSL_DLLEXPORT_H
+
+#include "magnum/object/config/Config.h"
+
+
+#if defined(COMPILE_MAGNUM_OPENSSL)
+#    define MAGNUM_OPENSSL_API     MAGNUM_API_EXPORT
+#else
+#    define MAGNUM_OPENSSL_API     MAGNUM_API_IMPORT
+#endif
+
+#endif

File source/libs/magnum_openssl/source/magnum/openssl/OpenSSL.cpp

+/*
+  Magnum
+  Copyright (C) 2002-2012 Kaya Kupferschmidt. All Rights Reserved.
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial nets, and to alter it and redistribute it
+  freely, under the terms in LICENSE.TXT.
+
+  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
+*/
+
+#include "magnum/openssl/OpenSSL.h"
+
+#if defined(__FORWINDOWS__)
+#	include <winsock2.h>
+#	include <ws2tcpip.h>
+#else
+#	include <unistd.h>
+#	include <sys/time.h>
+#endif
+
+#include "openssl/ssl.h"
+#include "openssl/crypto.h"
+
+
+#if defined(__FORWINDOWS__)
+    #define MUTEX_TYPE HANDLE
+    #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
+    #define MUTEX_CLEANUP(x) CloseHandle(x)
+    #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE)
+    #define MUTEX_UNLOCK(x) ReleaseMutex(x)
+    #define THREAD_ID GetCurrentThreadId( )
+#elif defined(__FORPOSIX__)
+    /* _POSIX_THREADS is normally defined in unistd.h if pthreads are available
+       on your platform. */
+    #define MUTEX_TYPE pthread_mutex_t
+    #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
+    #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
+    #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
+    #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
+    #define THREAD_ID pthread_self( )
+#else
+    #error You must define mutex operations appropriate for your platform!
+#endif
+
+/* This array will store all of the mutexes available to OpenSSL. */
+static MUTEX_TYPE *mutex_buf = NULL;
+
+static void locking_function(int mode, int n, const char * file, int line)
+{
+    if (mode & CRYPTO_LOCK)
+        MUTEX_LOCK(mutex_buf[n]);
+    else
+        MUTEX_UNLOCK(mutex_buf[n]);
+}
+
+static unsigned long id_function(void)
+{
+    return ((unsigned long)THREAD_ID);
+}
+
+struct CRYPTO_dynlock_value
+{
+    MUTEX_TYPE mutex;
+};
+
+static struct CRYPTO_dynlock_value * dyn_create_function(const char *file,
+                             int line)
+{
+    struct CRYPTO_dynlock_value *value;
+    value = (struct CRYPTO_dynlock_value *)malloc(sizeof(struct CRYPTO_dynlock_value));
+    if (!value)
+        return NULL;
+    MUTEX_SETUP(value->mutex);
+    return value;
+}
+
+static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l,
+                  const char *file, int line)
+{
+    if (mode & CRYPTO_LOCK)
+        MUTEX_LOCK(l->mutex);
+    else
+        MUTEX_UNLOCK(l->mutex);
+}
+
+static void dyn_destroy_function(struct CRYPTO_dynlock_value *l,
+                 const char *file, int line)
+{
+    MUTEX_CLEANUP(l->mutex);
+    free(l);
+}
+
+
+namespace magnum { namespace openssl {
+static int startupCounter = 0;
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
+void OpenSSL::startup()
+{
+    if (startupCounter > 0)
+        return;
+    startupCounter++;
+
+    mutex_buf = (MUTEX_TYPE *)malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE));
+    for (int i = 0; i < CRYPTO_num_locks( ); i++)
+        MUTEX_SETUP(mutex_buf[i]);
+    CRYPTO_set_id_callback(id_function);
+    CRYPTO_set_locking_callback(locking_function);
+      /* The following three CRYPTO_... functions are the OpenSSL functions
+         for registering the callbacks we implemented above */
+    CRYPTO_set_dynlock_create_callback(dyn_create_function);
+    CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
+    CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
+
+    SSL_library_init();
+    SSL_load_error_strings();
+}
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
+void OpenSSL::shutdown()
+{
+    if (startupCounter == 0)
+        return;
+    --startupCounter;
+
+    if (!mutex_buf)
+        return;
+
+    CRYPTO_set_id_callback(NULL);
+    CRYPTO_set_locking_callback(NULL);
+    CRYPTO_set_dynlock_create_callback(NULL);
+    CRYPTO_set_dynlock_lock_callback(NULL);
+    CRYPTO_set_dynlock_destroy_callback(NULL);
+    for (int i = 0; i < CRYPTO_num_locks( ); i++)
+        MUTEX_CLEANUP(mutex_buf[i]);
+    free(mutex_buf);
+    mutex_buf = NULL;
+}
+
+
+} }

File source/libs/magnum_openssl/source/magnum/openssl/OpenSSL.h

+/*
+  Magnum
+  Copyright (C) 2002-2012 Kaya Kupferschmidt. All Rights Reserved.
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial nets, and to alter it and redistribute it
+  freely, under the terms in LICENSE.TXT.
+
+  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
+*/
+
+#ifndef __MAGNUM_OPENSSL_OPENSSL_H
+#define __MAGNUM_OPENSSL_OPENSSL_H
+
+#include "magnum/object/Object.h"
+#include "magnum/openssl/DllExport.h"
+
+#include "magnum/object/Object.inl"
+
+namespace magnum { namespace openssl {
+using magnum::object::Object;
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
+class MAGNUM_OPENSSL_API OpenSSL : public Object {
+OBJECT_METAINFO(OpenSSL);
+public:
+    static void startup();
+    static void shutdown();
+};
+
+} }
+
+#endif

File source/libs/magnum_openssl/unittest/SConscript

+Import('Sources')
+Import('installer')
+
+sources = Sources('*.cpp')
+
+env = DefaultEnvironment().Clone()
+env.ImportModules(['magnum_openssl','magnum_test'])
+#prg = env.Program('Test_OpenSSL',sources, PDB = 'Test_OpenSSL.pdb')
+libs = [env.SharedLibraryFile('#/installed/bin/magnum_openssl')]
+
+#exe = installer.Install(dir = '#/installed/bin/unittest', source = prg)
+installer.Install(dir = '#/installed/bin/unittest', source = libs)
+
+#env.RunTest("Test_Net", exe)