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
{
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*/
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");
74 Utility::SetEnv("HOME", homepath
);
79 RAND_file_name(path
, 512);
84 RAND_write_file(path
);
88 DEB( fprintf(stderr
, "SSLInitializer: no random file generated\n");)
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");)
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())
124 if (mode
& CRYPTO_LOCK
)
135 unsigned long SSLInitializer::SSL_id_function()
137 return Utility::ThreadID();
141 #ifdef SOCKETS_NAMESPACE
142 } // namespace SOCKETS_NAMESPACE {
144 #endif // HAVE_OPENSSL