You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2014/07/26 22:27:29 UTC

[2/3] git commit: Add ability to take control of OpenSSL initialization

Add ability to take control of OpenSSL initialization

Signed-off-by: Alan Dunn <am...@gmail.com>
Signed-off-by: Roger Meier <ro...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/c0ff5561
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/c0ff5561
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/c0ff5561

Branch: refs/heads/master
Commit: c0ff5561fefa4b690d6c72ac7d792f9a1e480bda
Parents: 8953e70
Author: Alan Dunn <am...@gmail.com>
Authored: Sat Jul 26 13:44:24 2014 -0500
Committer: Roger Meier <ro...@apache.org>
Committed: Sat Jul 26 22:13:55 2014 +0200

----------------------------------------------------------------------
 lib/cpp/src/thrift/transport/TSSLSocket.cpp |  7 +++++--
 lib/cpp/src/thrift/transport/TSSLSocket.h   | 16 +++++++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/c0ff5561/lib/cpp/src/thrift/transport/TSSLSocket.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/src/thrift/transport/TSSLSocket.cpp b/lib/cpp/src/thrift/transport/TSSLSocket.cpp
index 4b36f8c..fd285db 100644
--- a/lib/cpp/src/thrift/transport/TSSLSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TSSLSocket.cpp
@@ -460,11 +460,14 @@ void TSSLSocket::authorize() {
 // TSSLSocketFactory implementation
 uint64_t TSSLSocketFactory::count_ = 0;
 Mutex    TSSLSocketFactory::mutex_;
+bool     TSSLSocketFactory::manualOpenSSLInitialization_ = false;
 
 TSSLSocketFactory::TSSLSocketFactory(const SSLProtocol& protocol): server_(false) {
   Guard guard(mutex_);
   if (count_ == 0) {
-    initializeOpenSSL();
+    if (!manualOpenSSLInitialization_) {
+      initializeOpenSSL();
+    }
     randomize();
   }
   count_++;
@@ -475,7 +478,7 @@ TSSLSocketFactory::~TSSLSocketFactory() {
   Guard guard(mutex_);
   ctx_.reset();
   count_--;
-  if (count_ == 0) {
+  if (count_ == 0 && !manualOpenSSLInitialization_) {
     cleanupOpenSSL();
   }
 }

http://git-wip-us.apache.org/repos/asf/thrift/blob/c0ff5561/lib/cpp/src/thrift/transport/TSSLSocket.h
----------------------------------------------------------------------
diff --git a/lib/cpp/src/thrift/transport/TSSLSocket.h b/lib/cpp/src/thrift/transport/TSSLSocket.h
index eca9591..a4b805b 100644
--- a/lib/cpp/src/thrift/transport/TSSLSocket.h
+++ b/lib/cpp/src/thrift/transport/TSSLSocket.h
@@ -44,15 +44,17 @@ enum SSLProtocol {
 /**
  * Initialize OpenSSL library.  This function, or some other
  * equivalent function to initialize OpenSSL, must be called before
- * TSSLSocket is used.  Currently TSSLSocketFactory automatically
- * calls this function, so you should not.
+ * TSSLSocket is used.  If you set TSSLSocketFactory to use manual
+ * OpenSSL initialization, you should call this function or otherwise
+ * ensure OpenSSL is initialized yourself.
  */
 void initializeOpenSSL();
 /**
  * Cleanup OpenSSL library.  This function should be called to clean
- * up OpenSSL after use of OpenSSL functionality is finished.
- * Currently TSSLSocketFactory automatically calls this function, so
- * you should not.
+ * up OpenSSL after use of OpenSSL functionality is finished.  If you
+ * set TSSLSocketFactory to use manual OpenSSL initialization, you
+ * should call this function yourself or ensure that whatever
+ * initialized OpenSSL cleans it up too.
  */
 void cleanupOpenSSL();
 
@@ -216,6 +218,9 @@ class TSSLSocketFactory {
   virtual void access(boost::shared_ptr<AccessManager> manager) {
     access_ = manager;
   }
+  static void setManualOpenSSLInitialization(bool manualOpenSSLInitialization) {
+    manualOpenSSLInitialization_ = manualOpenSSLInitialization;
+  }
  protected:
   boost::shared_ptr<SSLContext> ctx_;
 
@@ -232,6 +237,7 @@ class TSSLSocketFactory {
   boost::shared_ptr<AccessManager> access_;
   static concurrency::Mutex mutex_;
   static uint64_t count_;
+  static bool manualOpenSSLInitialization_;
   void setup(boost::shared_ptr<TSSLSocket> ssl);
   static int passwordCallback(char* password, int size, int, void* data);
 };