OSDN Git Service

Fix bugs process protection.
[ffftp/ffftp.git] / socketwrapper.c
1 // socketwrapper.c
2 // Copyright (C) 2011 Suguru Kawamoto
3 // ソケットラッパー
4 // socket関連関数をOpenSSL用に置換
5 // コンパイルにはOpenSSLのヘッダーファイルが必要
6 // 実行にはOpenSSLのDLLが必要
7
8 #include <windows.h>
9 #include <mmsystem.h>
10 #include <openssl/ssl.h>
11
12 #include "socketwrapper.h"
13 #include "protectprocess.h"
14
15 typedef void (__cdecl* _SSL_load_error_strings)();
16 typedef int (__cdecl* _SSL_library_init)();
17 typedef SSL_METHOD* (__cdecl* _SSLv23_method)();
18 typedef SSL_CTX* (__cdecl* _SSL_CTX_new)(SSL_METHOD*);
19 typedef void (__cdecl* _SSL_CTX_free)(SSL_CTX*);
20 typedef SSL* (__cdecl* _SSL_new)(SSL_CTX*);
21 typedef void (__cdecl* _SSL_free)(SSL*);
22 typedef int (__cdecl* _SSL_shutdown)(SSL*);
23 typedef int (__cdecl* _SSL_get_fd)(SSL*);
24 typedef int (__cdecl* _SSL_set_fd)(SSL*, int);
25 typedef int (__cdecl* _SSL_accept)(SSL*);
26 typedef int (__cdecl* _SSL_connect)(SSL*);
27 typedef int (__cdecl* _SSL_write)(SSL*, const void*, int);
28 typedef int (__cdecl* _SSL_peek)(SSL*, void*, int);
29 typedef int (__cdecl* _SSL_read)(SSL*, void*, int);
30 typedef int (__cdecl* _SSL_get_error)(SSL*, int);
31
32 _SSL_load_error_strings pSSL_load_error_strings;
33 _SSL_library_init pSSL_library_init;
34 _SSLv23_method pSSLv23_method;
35 _SSL_CTX_new pSSL_CTX_new;
36 _SSL_CTX_free pSSL_CTX_free;
37 _SSL_new pSSL_new;
38 _SSL_free pSSL_free;
39 _SSL_shutdown pSSL_shutdown;
40 _SSL_get_fd pSSL_get_fd;
41 _SSL_set_fd pSSL_set_fd;
42 _SSL_accept pSSL_accept;
43 _SSL_connect pSSL_connect;
44 _SSL_write pSSL_write;
45 _SSL_peek pSSL_peek;
46 _SSL_read pSSL_read;
47 _SSL_get_error pSSL_get_error;
48
49 #define MAX_SSL_SOCKET 64
50
51 BOOL g_bOpenSSLLoaded;
52 HMODULE g_hOpenSSL;
53 CRITICAL_SECTION g_OpenSSLLock;
54 DWORD g_OpenSSLTimeout;
55 LPSSLTIMEOUTCALLBACK g_pOpenSSLTimeoutCallback;
56 SSL_CTX* g_pOpenSSLCTX;
57 SSL* g_pOpenSSLHandle[MAX_SSL_SOCKET];
58
59 BOOL __stdcall DefaultSSLTimeoutCallback()
60 {
61         Sleep(100);
62         return FALSE;
63 }
64
65 BOOL LoadOpenSSL()
66 {
67         if(g_bOpenSSLLoaded)
68                 return FALSE;
69 #ifdef ENABLE_PROCESS_PROTECTION
70         // ssleay32.dll 1.0.0e
71         // libssl32.dll 1.0.0e
72         RegisterTrustedModuleMD5Hash("\x8B\xA3\xB7\xB3\xCE\x2E\x4F\x07\x8C\xB8\x93\x7D\x77\xE1\x09\x3A");
73         // libeay32.dll 1.0.0e
74         RegisterTrustedModuleMD5Hash("\xA6\x4C\xAF\x9E\xF3\xDC\xFC\x68\xAE\xCA\xCC\x61\xD2\xF6\x70\x8B");
75 #endif
76         g_hOpenSSL = LoadLibrary("ssleay32.dll");
77         if(!g_hOpenSSL)
78                 g_hOpenSSL = LoadLibrary("libssl32.dll");
79         if(!g_hOpenSSL
80                 || !(pSSL_load_error_strings = (_SSL_load_error_strings)GetProcAddress(g_hOpenSSL, "SSL_load_error_strings"))
81                 || !(pSSL_library_init = (_SSL_library_init)GetProcAddress(g_hOpenSSL, "SSL_library_init"))
82                 || !(pSSLv23_method = (_SSLv23_method)GetProcAddress(g_hOpenSSL, "SSLv23_method"))
83                 || !(pSSL_CTX_new = (_SSL_CTX_new)GetProcAddress(g_hOpenSSL, "SSL_CTX_new"))
84                 || !(pSSL_CTX_free = (_SSL_CTX_free)GetProcAddress(g_hOpenSSL, "SSL_CTX_free"))
85                 || !(pSSL_new = (_SSL_new)GetProcAddress(g_hOpenSSL, "SSL_new"))
86                 || !(pSSL_free = (_SSL_free)GetProcAddress(g_hOpenSSL, "SSL_free"))
87                 || !(pSSL_shutdown = (_SSL_shutdown)GetProcAddress(g_hOpenSSL, "SSL_shutdown"))
88                 || !(pSSL_get_fd = (_SSL_get_fd)GetProcAddress(g_hOpenSSL, "SSL_get_fd"))
89                 || !(pSSL_set_fd = (_SSL_set_fd)GetProcAddress(g_hOpenSSL, "SSL_set_fd"))
90                 || !(pSSL_accept = (_SSL_accept)GetProcAddress(g_hOpenSSL, "SSL_accept"))
91                 || !(pSSL_connect = (_SSL_connect)GetProcAddress(g_hOpenSSL, "SSL_connect"))
92                 || !(pSSL_write = (_SSL_write)GetProcAddress(g_hOpenSSL, "SSL_write"))
93                 || !(pSSL_peek = (_SSL_peek)GetProcAddress(g_hOpenSSL, "SSL_peek"))
94                 || !(pSSL_read = (_SSL_read)GetProcAddress(g_hOpenSSL, "SSL_read"))
95                 || !(pSSL_get_error = (_SSL_get_error)GetProcAddress(g_hOpenSSL, "SSL_get_error")))
96         {
97                 if(g_hOpenSSL)
98                         FreeLibrary(g_hOpenSSL);
99                 g_hOpenSSL = NULL;
100                 return FALSE;
101         }
102         InitializeCriticalSection(&g_OpenSSLLock);
103         pSSL_load_error_strings();
104         pSSL_library_init();
105         SetSSLTimeoutCallback(60000, DefaultSSLTimeoutCallback);
106         g_bOpenSSLLoaded = TRUE;
107         return TRUE;
108 }
109
110 void FreeOpenSSL()
111 {
112         int i;
113         if(!g_bOpenSSLLoaded)
114                 return;
115         EnterCriticalSection(&g_OpenSSLLock);
116         for(i = 0; i < MAX_SSL_SOCKET; i++)
117         {
118                 if(g_pOpenSSLHandle[i])
119                 {
120                         pSSL_shutdown(g_pOpenSSLHandle[i]);
121                         pSSL_free(g_pOpenSSLHandle[i]);
122                         g_pOpenSSLHandle[i] = NULL;
123                 }
124         }
125         if(g_pOpenSSLCTX)
126                 pSSL_CTX_free(g_pOpenSSLCTX);
127         g_pOpenSSLCTX = NULL;
128         FreeLibrary(g_hOpenSSL);
129         g_hOpenSSL = NULL;
130         LeaveCriticalSection(&g_OpenSSLLock);
131         DeleteCriticalSection(&g_OpenSSLLock);
132         g_bOpenSSLLoaded = FALSE;
133 }
134
135 BOOL IsOpenSSLLoaded()
136 {
137         return g_bOpenSSLLoaded;
138 }
139
140 SSL** GetUnusedSSLPointer()
141 {
142         int i;
143         for(i = 0; i < MAX_SSL_SOCKET; i++)
144         {
145                 if(!g_pOpenSSLHandle[i])
146                         return &g_pOpenSSLHandle[i];
147         }
148         return NULL;
149 }
150
151 SSL** FindSSLPointerFromSocket(SOCKET s)
152 {
153         int i;
154         for(i = 0; i < MAX_SSL_SOCKET; i++)
155         {
156                 if(g_pOpenSSLHandle[i])
157                 {
158                         if(pSSL_get_fd(g_pOpenSSLHandle[i]) == s)
159                                 return &g_pOpenSSLHandle[i];
160                 }
161         }
162         return NULL;
163 }
164
165 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
166 {
167         if(!g_bOpenSSLLoaded)
168                 return;
169         EnterCriticalSection(&g_OpenSSLLock);
170         g_OpenSSLTimeout = Timeout;
171         g_pOpenSSLTimeoutCallback = pCallback;
172         LeaveCriticalSection(&g_OpenSSLLock);
173 }
174
175 BOOL AttachSSL(SOCKET s)
176 {
177         BOOL r;
178         DWORD Time;
179         SSL** ppSSL;
180         if(!g_bOpenSSLLoaded)
181                 return FALSE;
182         r = FALSE;
183         Time = timeGetTime();
184         EnterCriticalSection(&g_OpenSSLLock);
185         if(!g_pOpenSSLCTX)
186                 g_pOpenSSLCTX = pSSL_CTX_new(pSSLv23_method());
187         if(g_pOpenSSLCTX)
188         {
189                 if(ppSSL = GetUnusedSSLPointer())
190                 {
191                         if(*ppSSL = pSSL_new(g_pOpenSSLCTX))
192                         {
193                                 if(pSSL_set_fd(*ppSSL, s) != 0)
194                                 {
195                                         r = TRUE;
196                                         // SSLのネゴシエーションには時間がかかる場合がある
197                                         while(pSSL_connect(*ppSSL) != 1)
198                                         {
199                                                 LeaveCriticalSection(&g_OpenSSLLock);
200                                                 if(g_pOpenSSLTimeoutCallback() || timeGetTime() - Time >= g_OpenSSLTimeout)
201                                                 {
202                                                         DetachSSL(s);
203                                                         r = FALSE;
204                                                         EnterCriticalSection(&g_OpenSSLLock);
205                                                         break;
206                                                 }
207                                                 EnterCriticalSection(&g_OpenSSLLock);
208                                         }
209                                 }
210                                 else
211                                 {
212                                         LeaveCriticalSection(&g_OpenSSLLock);
213                                         DetachSSL(s);
214                                         EnterCriticalSection(&g_OpenSSLLock);
215                                 }
216                         }
217                 }
218         }
219         LeaveCriticalSection(&g_OpenSSLLock);
220         return r;
221 }
222
223 BOOL DetachSSL(SOCKET s)
224 {
225         BOOL r;
226         SSL** ppSSL;
227         if(!g_bOpenSSLLoaded)
228                 return FALSE;
229         r = FALSE;
230         EnterCriticalSection(&g_OpenSSLLock);
231         if(ppSSL = FindSSLPointerFromSocket(s))
232         {
233                 pSSL_shutdown(*ppSSL);
234                 pSSL_free(*ppSSL);
235                 *ppSSL = NULL;
236                 r = TRUE;
237         }
238         LeaveCriticalSection(&g_OpenSSLLock);
239         return r;
240 }
241
242 BOOL IsSSLAttached(SOCKET s)
243 {
244         SSL** ppSSL;
245         if(!g_bOpenSSLLoaded)
246                 return FALSE;
247         EnterCriticalSection(&g_OpenSSLLock);
248         ppSSL = FindSSLPointerFromSocket(s);
249         LeaveCriticalSection(&g_OpenSSLLock);
250         if(!ppSSL)
251                 return TRUE;
252         return TRUE;
253 }
254
255 SOCKET socketS(int af, int type, int protocol)
256 {
257         return socket(af, type, protocol);
258 }
259
260 int bindS(SOCKET s, const struct sockaddr *addr, int namelen)
261 {
262         return bind(s, addr, namelen);
263 }
264
265 int listenS(SOCKET s, int backlog)
266 {
267         return listen(s, backlog);
268 }
269
270 SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
271 {
272         SOCKET r;
273         r = accept(s, addr, addrlen);
274         if(!AttachSSL(r))
275         {
276                 closesocket(r);
277                 return INVALID_SOCKET;
278         }
279         return r;
280 }
281
282 int connectS(SOCKET s, const struct sockaddr *name, int namelen)
283 {
284         int r;
285         r = connect(s, name, namelen);
286         if(!AttachSSL(r))
287                 return SOCKET_ERROR;
288         return r;
289 }
290
291 int closesocketS(SOCKET s)
292 {
293         DetachSSL(s);
294         return closesocket(s);
295 }
296
297 int sendS(SOCKET s, const char * buf, int len, int flags)
298 {
299         SSL** ppSSL;
300         if(!g_bOpenSSLLoaded)
301                 return send(s, buf, len, flags);
302         EnterCriticalSection(&g_OpenSSLLock);
303         ppSSL = FindSSLPointerFromSocket(s);
304         LeaveCriticalSection(&g_OpenSSLLock);
305         if(!ppSSL)
306                 return send(s, buf, len, flags);
307         return pSSL_write(*ppSSL, buf, len);
308 }
309
310 int recvS(SOCKET s, char * buf, int len, int flags)
311 {
312         SSL** ppSSL;
313         if(!g_bOpenSSLLoaded)
314                 return recv(s, buf, len, flags);
315         EnterCriticalSection(&g_OpenSSLLock);
316         ppSSL = FindSSLPointerFromSocket(s);
317         LeaveCriticalSection(&g_OpenSSLLock);
318         if(!ppSSL)
319                 return recv(s, buf, len, flags);
320         if(flags & MSG_PEEK)
321                 return pSSL_peek(*ppSSL, buf, len);
322         return pSSL_read(*ppSSL, buf, len);
323 }
324