svn cleanup
[anytun.git] / Sockets / SSLInitializer.cpp
blob65218b13160347805976c7d9d598cdf3b10f142d
1 /**
2 ** \file SSLInitializer.cpp
3 ** \date 2007-04-30
4 ** \author grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2007 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifdef _WIN32
24 #ifdef _MSC_VER
25 #pragma warning(disable:4786)
26 #endif
27 #include <io.h>
28 #endif
29 #include "SSLInitializer.h"
30 #ifdef HAVE_OPENSSL
31 #include <map>
32 #include "Utility.h"
33 #include <openssl/rand.h>
34 #include "Mutex.h"
36 #ifdef _DEBUG
37 #define DEB(x) x
38 #else
39 #define DEB(x)
40 #endif
43 #ifdef SOCKETS_NAMESPACE
44 namespace SOCKETS_NAMESPACE {
45 #endif
49 SSLInitializer::SSLInitializer()
51 DEB( fprintf(stderr, "SSLInitializer()\n");)
53 bio_err = NULL;
54 m_rand_size = 1024;
56 /* An error write context */
57 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
59 /* Global system initialization*/
60 SSL_library_init();
61 SSL_load_error_strings();
62 OpenSSL_add_all_algorithms();
63 CRYPTO_set_locking_callback( SSL_locking_function );
64 CRYPTO_set_id_callback( SSL_id_function );
66 char *randfile = getenv("RANDFILE");
67 char *home = getenv("HOME");
68 if (!randfile && !home)
70 char *homepath = getenv("HOMEPATH");
71 if (homepath)
73 Utility::SetEnv("HOME", homepath);
76 char path[512];
77 *path = 0;
78 RAND_file_name(path, 512);
79 if (*path)
81 m_rand_file = path;
82 m_rand_size = 1024;
83 RAND_write_file(path);
85 else
87 DEB( fprintf(stderr, "SSLInitializer: no random file generated\n");)
90 /* Load randomness */
91 if (!m_rand_file.size() || !RAND_load_file(m_rand_file.c_str(), m_rand_size))
93 DEB( fprintf(stderr, "SSLInitializer: PRNG not initialized\n");)
99 SSLInitializer::~SSLInitializer()
101 DEB( fprintf(stderr, "~SSLInitializer()\n");)
102 DeleteRandFile();
103 // %! delete mutexes
107 void SSLInitializer::DeleteRandFile()
109 if (m_rand_file.size())
111 unlink(m_rand_file.c_str());
116 void SSLInitializer::SSL_locking_function(int mode, int n, const char *file, int line)
118 static std::map<int, Mutex *> mmap;
119 if (mmap.find(n) == mmap.end())
121 mmap[n] = new Mutex;
123 if (mode & CRYPTO_LOCK)
125 mmap[n] -> Lock();
127 else
129 mmap[n] -> Unlock();
134 unsigned long SSLInitializer::SSL_id_function()
136 return Utility::ThreadID();
140 #ifdef SOCKETS_NAMESPACE
141 } // namespace SOCKETS_NAMESPACE {
142 #endif
143 #endif // HAVE_OPENSSL