summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/SslFactory.cpp
blob: 91b31a69d7daac05bc72105883cc66a4f9fe0d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

// Note: This pragma is used to disable spurious warning messages having
//       to do with the length of debug symbols exceeding 255 characters.
//       This is due to STL template identifiers expansion.
//       The MSDN Library recommends that you put this pragma directive
//       in place to avoid the warnings.
#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <Ice/SslFactory.h>
#include <Ice/SslSystemOpenSSL.h>
#include <Ice/Security.h>

#define OPENSSL_THREAD_DEFINES
#include <openssl/opensslconf.h>
#if defined(THREADS)
#else
#error "Thread support not enabled"
#endif


namespace IceSecurity
{

namespace Ssl
{

// Static member instantiations.
IceUtil::Mutex Factory::_systemRepositoryMutex;
SystemMap Factory::_systemRepository;
SslHandleSystemMap Factory::_sslHandleSystemRepository;
int Factory::_evict = 0;

extern "C"
{
    void lockingCallback(int, int, const char*, int);
}

class SslLockKeeper
{

public:
    SslLockKeeper()
    {
        CRYPTO_set_locking_callback((void (*)(int, int, const char*, int))lockingCallback);
    }

    ~SslLockKeeper()
    {
        CRYPTO_set_locking_callback(NULL);
    }

    IceUtil::Mutex sslLocks[CRYPTO_NUM_LOCKS];

};

SslLockKeeper lockKeeper;

}

}

void IceSecurity::Ssl::lockingCallback(int mode, int type, const char *file, int line)
{
    if (mode & CRYPTO_LOCK)
    {
        lockKeeper.sslLocks[type].lock();
    }
    else
    {
        lockKeeper.sslLocks[type].unlock();
    }
}

IceSecurity::Ssl::SystemPtr
IceSecurity::Ssl::Factory::getSystem(const string& systemIdentifier)
{
    IceUtil::Mutex::Lock sync(_systemRepositoryMutex);

    SystemPtr system = _systemRepository[systemIdentifier];

    // Don't have that System.
    if (!system)
    {
        // In our case, the systemIdentifier happens to be the
        // SSL Configuration file.

        // This line would change based on the flavor of System that we're
        // creating for the caller.
        system = new OpenSSL::System();

        if (system)
        {
            _systemRepository[systemIdentifier] = system;
        }
    }

    assert(system);

    reapSystems();

    return system;
}

void
IceSecurity::Ssl::Factory::addSystemHandle(void* sslHandle, const SystemPtr& system)
{
    assert(system);
    assert(sslHandle);
    _sslHandleSystemRepository[sslHandle] = system;
}

void
IceSecurity::Ssl::Factory::removeSystemHandle(void* sslHandle)
{
    assert(sslHandle);
    _sslHandleSystemRepository.erase(sslHandle);
}

IceSecurity::Ssl::SystemPtr
IceSecurity::Ssl::Factory::getSystemFromHandle(void* sslHandle)
{
    IceUtil::Mutex::Lock sync(_systemRepositoryMutex);

    assert(sslHandle);

    SystemPtr& system = _sslHandleSystemRepository[sslHandle];

    assert(system);

    reapSystems();

    return system;
}

void
IceSecurity::Ssl::Factory::reapSystems()
{
    if (++_evict >= 10)
    {
        // Note: Double Eviction!  We keep two maps, one keyed on the config file
        //       name that the System is based on (_systemRepository), the other
        //       keyed on a SSL* that was generated by the system in question
        //       (_sslHandleSystemRepository).

        _evict = 0;

	SystemMap::iterator p = _systemRepository.begin();

	while (p != _systemRepository.end())
	{
            // Check the reference count on each System
	    if ((*p).second->__getRef() == 1)
	    {
                // If the Factory System Repository is the only one with a ref to it, erase
		_systemRepository.erase(p++);
	    }
	    else
	    {
		++p;
	    }
	}

/*
	SslHandleSystemMap::iterator r = _sslHandleSystemRepository.begin();

	while (r != _sslHandleSystemRepository.end())
	{
            // Check the reference count on each System
	    if ((*r).second->__getRef() == 1)
	    {
                // If the SSL Handle System Repository is the only one with a ref to it, erase
		_sslHandleSystemRepository.erase(r++);
	    }
	    else
	    {
		++r;
	    }
	}
*/
    }
}

void
IceSecurity::Ssl::setSystemCertificateVerifier(const string& systemIdentifier,
                                               CertificateVerifierType verifierType,
                                               const CertificateVerifierPtr& certificateVerifier)
{
    SystemPtr sslSystem = Factory::getSystem(systemIdentifier);

    if ((verifierType == Client) || (verifierType == ClientServer))
    {
        sslSystem->setClientCertificateVerifier(certificateVerifier);
    }

    if ((verifierType == Server) || (verifierType == ClientServer))
    {
        sslSystem->setServerCertificateVerifier(certificateVerifier);
    }
}