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
|
/*
Jabber Protocol Plugin for Miranda IM
Copyright ( C ) 2002-04 Santithorn Bunchua
Copyright ( C ) 2005-06 George Hazan
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or ( at your option ) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
File name : $Source: /cvsroot/miranda/miranda/protocols/JabberG/jabber_ssl.cpp,v $
Revision : $Revision: 2866 $
Last change on : $Date: 2006-05-16 20:39:40 +0400 (Втр, 16 Май 2006) $
Last change by : $Author: ghazan $
*/
#define _JABBER_SSL_C_
#include "jabber.h"
#include "jabber_ssl.h"
PFN_SSL_int_void pfn_SSL_library_init; // int SSL_library_init()
PFN_SSL_pvoid_void pfn_SSLv23_client_method; // SSL_METHOD *SSLv23_client_method()
PFN_SSL_pvoid_pvoid pfn_SSL_CTX_new; // SSL_CTX *SSL_CTX_new( SSL_METHOD *method )
PFN_SSL_void_pvoid pfn_SSL_CTX_free; // void SSL_CTX_free( SSL_CTX *ctx );
PFN_SSL_pvoid_pvoid pfn_SSL_new; // SSL *SSL_new( SSL_CTX *ctx )
PFN_SSL_void_pvoid pfn_SSL_free; // void SSL_free( SSL *ssl );
PFN_SSL_int_pvoid_int pfn_SSL_set_fd; // int SSL_set_fd( SSL *ssl, int fd );
PFN_SSL_int_pvoid pfn_SSL_connect; // int SSL_connect( SSL *ssl );
PFN_SSL_int_pvoid_pvoid_int pfn_SSL_read; // int SSL_read( SSL *ssl, void *buffer, int bufsize )
PFN_SSL_int_pvoid_pvoid_int pfn_SSL_write; // int SSL_write( SSL *ssl, void *buffer, int bufsize )
static CRITICAL_SECTION sslHandleMutex;
static JABBER_SSL_MAPPING *sslHandleList = NULL;
static int sslHandleCount = 0;
BOOL JabberSslInit()
{
BOOL error = FALSE;
sslHandleList = NULL;
sslHandleCount = 0;
InitializeCriticalSection( &sslHandleMutex );
hLibSSL = LoadLibraryA( "SSLEAY32.DLL" );
if ( !hLibSSL )
hLibSSL = LoadLibraryA( "LIBSSL32.DLL" );
if ( hLibSSL ) {
if (( pfn_SSL_library_init=( PFN_SSL_int_void )GetProcAddress( hLibSSL, "SSL_library_init" )) == NULL )
error = TRUE;
if (( pfn_SSLv23_client_method=( PFN_SSL_pvoid_void )GetProcAddress( hLibSSL, "SSLv23_client_method" )) == NULL )
error = TRUE;
if (( pfn_SSL_CTX_new=( PFN_SSL_pvoid_pvoid )GetProcAddress( hLibSSL, "SSL_CTX_new" )) == NULL )
error = TRUE;
if (( pfn_SSL_CTX_free=( PFN_SSL_void_pvoid )GetProcAddress( hLibSSL, "SSL_CTX_free" )) == NULL )
error = TRUE;
if (( pfn_SSL_new=( PFN_SSL_pvoid_pvoid )GetProcAddress( hLibSSL, "SSL_new" )) == NULL )
error = TRUE;
if (( pfn_SSL_free=( PFN_SSL_void_pvoid )GetProcAddress( hLibSSL, "SSL_free" )) == NULL )
error = TRUE;
if (( pfn_SSL_set_fd=( PFN_SSL_int_pvoid_int )GetProcAddress( hLibSSL, "SSL_set_fd" )) == NULL )
error = TRUE;
if (( pfn_SSL_connect=( PFN_SSL_int_pvoid )GetProcAddress( hLibSSL, "SSL_connect" )) == NULL )
error = TRUE;
if (( pfn_SSL_read=( PFN_SSL_int_pvoid_pvoid_int )GetProcAddress( hLibSSL, "SSL_read" )) == NULL )
error = TRUE;
if (( pfn_SSL_write=( PFN_SSL_int_pvoid_pvoid_int )GetProcAddress( hLibSSL, "SSL_write" )) == NULL )
error = TRUE;
if ( error == TRUE ) {
FreeLibrary( hLibSSL );
hLibSSL = NULL;
}
}
#ifdef _DEBUG
if ( hLibSSL )
JabberLog( "SSL library load successful" );
else
JabberLog( "SSL library cannot load" );
#endif
if ( hLibSSL ) {
pfn_SSL_library_init();
jabberSslCtx = pfn_SSL_CTX_new( pfn_SSLv23_client_method());
return TRUE;
}
else
return FALSE;
}
void JabberSslUninit()
{
if ( hLibSSL ) {
pfn_SSL_CTX_free( jabberSslCtx );
JabberLog( "Free SSL library" );
FreeLibrary( hLibSSL );
hLibSSL = NULL;
}
if ( sslHandleList ) mir_free( sslHandleList );
sslHandleCount = 0;
DeleteCriticalSection( &sslHandleMutex );
}
int JabberSslFindHandle( HANDLE hConn )
{
int i;
EnterCriticalSection( &sslHandleMutex );
for ( i=0; i<sslHandleCount; i++ ) {
if ( sslHandleList[i].h == hConn ) {
LeaveCriticalSection( &sslHandleMutex );
return i;
}
}
LeaveCriticalSection( &sslHandleMutex );
return -1;
}
PVOID JabberSslHandleToSsl( HANDLE hConn )
{
int i;
EnterCriticalSection( &sslHandleMutex );
for ( i=0; i<sslHandleCount; i++ ) {
if ( sslHandleList[i].h == hConn ) {
LeaveCriticalSection( &sslHandleMutex );
return sslHandleList[i].ssl;
}
}
LeaveCriticalSection( &sslHandleMutex );
return NULL;
}
void JabberSslAddHandle( HANDLE hConn, PVOID ssl )
{
EnterCriticalSection( &sslHandleMutex );
if ( JabberSslFindHandle( hConn ) >= 0 ) {
LeaveCriticalSection( &sslHandleMutex );
return;
}
sslHandleList = ( JABBER_SSL_MAPPING * ) mir_realloc( sslHandleList, ( sslHandleCount+1 )*sizeof( JABBER_SSL_MAPPING ));
sslHandleList[sslHandleCount].h = hConn;
sslHandleList[sslHandleCount].ssl = ssl;
sslHandleCount++;
LeaveCriticalSection( &sslHandleMutex );
}
void JabberSslRemoveHandle( HANDLE hConn )
{
int i;
EnterCriticalSection( &sslHandleMutex );
if (( i=JabberSslFindHandle( hConn )) < 0 ) {
LeaveCriticalSection( &sslHandleMutex );
return;
}
sslHandleCount--;
memmove( sslHandleList+i, sslHandleList+i+1, ( sslHandleCount-i )*sizeof( JABBER_SSL_MAPPING ));
sslHandleList = ( JABBER_SSL_MAPPING * ) mir_realloc( sslHandleList, sslHandleCount*sizeof( JABBER_SSL_MAPPING ));
LeaveCriticalSection( &sslHandleMutex );
}
|