Used a more uniform of logging and added a commandline parser.
[UnsignedByte.git] / src / Sockets / SSLInitializer.cpp
bloba4e075f1d3fa020e8ce12c50daa73260c49508cf
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 #ifndef __MINGW32__
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
48 BIO *SSLInitializer::bio_err = NULL;
49 std::string SSLInitializer::m_rand_file;
50 long SSLInitializer::m_rand_size = 1024;
53 SSLInitializer::SSLInitializer()
55 DEB( fprintf(stderr, "SSLInitializer()\n");)
57 /* An error write context */
58 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
60 /* Global system initialization*/
61 SSL_library_init();
62 SSL_load_error_strings();
63 OpenSSL_add_all_algorithms();
64 CRYPTO_set_locking_callback( SSL_locking_function );
65 CRYPTO_set_id_callback( SSL_id_function );
67 char *randfile = getenv("RANDFILE");
68 char *home = getenv("HOME");
69 if (!randfile && !home)
71 char *homepath = getenv("HOMEPATH");
72 if (homepath)
74 Utility::SetEnv("HOME", homepath);
77 char path[512];
78 *path = 0;
79 RAND_file_name(path, 512);
80 if (*path)
82 m_rand_file = path;
83 m_rand_size = 1024;
84 RAND_write_file(path);
86 else
88 DEB( fprintf(stderr, "SSLInitializer: no random file generated\n");)
91 /* Load randomness */
92 if (!m_rand_file.size() || !RAND_load_file(m_rand_file.c_str(), m_rand_size))
94 DEB( fprintf(stderr, "SSLInitializer: PRNG not initialized\n");)
100 SSLInitializer::~SSLInitializer()
102 DEB( fprintf(stderr, "~SSLInitializer()\n");)
103 DeleteRandFile();
104 // %! delete mutexes
108 void SSLInitializer::DeleteRandFile()
110 if (m_rand_file.size())
112 unlink(m_rand_file.c_str());
117 void SSLInitializer::SSL_locking_function(int mode, int n, const char *file, int line)
119 static std::map<int, Mutex *> mmap;
120 if (mmap.find(n) == mmap.end())
122 mmap[n] = new Mutex;
124 if (mode & CRYPTO_LOCK)
126 mmap[n] -> Lock();
128 else
130 mmap[n] -> Unlock();
135 unsigned long SSLInitializer::SSL_id_function()
137 return Utility::ThreadID();
141 #ifdef SOCKETS_NAMESPACE
142 } // namespace SOCKETS_NAMESPACE {
143 #endif
144 #endif // HAVE_OPENSSL