2 ** \file SSLInitializer.cpp
4 ** \author grymse@alhem.net
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.
25 #pragma warning(disable:4786)
29 #include "SSLInitializer.h"
33 #include <openssl/rand.h>
43 #ifdef SOCKETS_NAMESPACE
44 namespace SOCKETS_NAMESPACE
{
49 SSLInitializer::SSLInitializer()
51 DEB( fprintf(stderr
, "SSLInitializer()\n");)
56 /* An error write context */
57 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
59 /* Global system initialization*/
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");
73 Utility::SetEnv("HOME", homepath
);
78 RAND_file_name(path
, 512);
83 RAND_write_file(path
);
87 DEB( fprintf(stderr
, "SSLInitializer: no random file generated\n");)
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");)
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())
123 if (mode
& CRYPTO_LOCK
)
134 unsigned long SSLInitializer::SSL_id_function()
136 return Utility::ThreadID();
140 #ifdef SOCKETS_NAMESPACE
141 } // namespace SOCKETS_NAMESPACE {
143 #endif // HAVE_OPENSSL