OSDN Git Service

c4c7603ef07151657384e94d2b7767125f059ed4
[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         // 同梱するOpenSSLのバージョンに合わせてSHA1ハッシュ値を変更すること
71         // ssleay32.dll 1.0.0e
72         // libssl32.dll 1.0.0e
73         RegisterTrustedModuleSHA1Hash("\x4E\xB7\xA0\x22\x14\x4B\x58\x6D\xBC\xF5\x21\x0D\x96\x78\x0D\x79\x7D\x66\xB2\xB0");
74         // libeay32.dll 1.0.0e
75         RegisterTrustedModuleSHA1Hash("\x01\x32\x7A\xAE\x69\x26\xE6\x58\xC7\x63\x22\x1E\x53\x5A\x78\xBC\x61\xC7\xB5\xC1");
76 #endif
77         g_hOpenSSL = LoadLibrary("ssleay32.dll");
78         if(!g_hOpenSSL)
79                 g_hOpenSSL = LoadLibrary("libssl32.dll");
80         if(!g_hOpenSSL
81                 || !(pSSL_load_error_strings = (_SSL_load_error_strings)GetProcAddress(g_hOpenSSL, "SSL_load_error_strings"))
82                 || !(pSSL_library_init = (_SSL_library_init)GetProcAddress(g_hOpenSSL, "SSL_library_init"))
83                 || !(pSSLv23_method = (_SSLv23_method)GetProcAddress(g_hOpenSSL, "SSLv23_method"))
84                 || !(pSSL_CTX_new = (_SSL_CTX_new)GetProcAddress(g_hOpenSSL, "SSL_CTX_new"))
85                 || !(pSSL_CTX_free = (_SSL_CTX_free)GetProcAddress(g_hOpenSSL, "SSL_CTX_free"))
86                 || !(pSSL_new = (_SSL_new)GetProcAddress(g_hOpenSSL, "SSL_new"))
87                 || !(pSSL_free = (_SSL_free)GetProcAddress(g_hOpenSSL, "SSL_free"))
88                 || !(pSSL_shutdown = (_SSL_shutdown)GetProcAddress(g_hOpenSSL, "SSL_shutdown"))
89                 || !(pSSL_get_fd = (_SSL_get_fd)GetProcAddress(g_hOpenSSL, "SSL_get_fd"))
90                 || !(pSSL_set_fd = (_SSL_set_fd)GetProcAddress(g_hOpenSSL, "SSL_set_fd"))
91                 || !(pSSL_accept = (_SSL_accept)GetProcAddress(g_hOpenSSL, "SSL_accept"))
92                 || !(pSSL_connect = (_SSL_connect)GetProcAddress(g_hOpenSSL, "SSL_connect"))
93                 || !(pSSL_write = (_SSL_write)GetProcAddress(g_hOpenSSL, "SSL_write"))
94                 || !(pSSL_peek = (_SSL_peek)GetProcAddress(g_hOpenSSL, "SSL_peek"))
95                 || !(pSSL_read = (_SSL_read)GetProcAddress(g_hOpenSSL, "SSL_read"))
96                 || !(pSSL_get_error = (_SSL_get_error)GetProcAddress(g_hOpenSSL, "SSL_get_error")))
97         {
98                 if(g_hOpenSSL)
99                         FreeLibrary(g_hOpenSSL);
100                 g_hOpenSSL = NULL;
101                 return FALSE;
102         }
103         InitializeCriticalSection(&g_OpenSSLLock);
104         pSSL_load_error_strings();
105         pSSL_library_init();
106         SetSSLTimeoutCallback(60000, DefaultSSLTimeoutCallback);
107         g_bOpenSSLLoaded = TRUE;
108         return TRUE;
109 }
110
111 void FreeOpenSSL()
112 {
113         int i;
114         if(!g_bOpenSSLLoaded)
115                 return;
116         EnterCriticalSection(&g_OpenSSLLock);
117         for(i = 0; i < MAX_SSL_SOCKET; i++)
118         {
119                 if(g_pOpenSSLHandle[i])
120                 {
121                         pSSL_shutdown(g_pOpenSSLHandle[i]);
122                         pSSL_free(g_pOpenSSLHandle[i]);
123                         g_pOpenSSLHandle[i] = NULL;
124                 }
125         }
126         if(g_pOpenSSLCTX)
127                 pSSL_CTX_free(g_pOpenSSLCTX);
128         g_pOpenSSLCTX = NULL;
129         FreeLibrary(g_hOpenSSL);
130         g_hOpenSSL = NULL;
131         LeaveCriticalSection(&g_OpenSSLLock);
132         DeleteCriticalSection(&g_OpenSSLLock);
133         g_bOpenSSLLoaded = FALSE;
134 }
135
136 BOOL IsOpenSSLLoaded()
137 {
138         return g_bOpenSSLLoaded;
139 }
140
141 SSL** GetUnusedSSLPointer()
142 {
143         int i;
144         for(i = 0; i < MAX_SSL_SOCKET; i++)
145         {
146                 if(!g_pOpenSSLHandle[i])
147                         return &g_pOpenSSLHandle[i];
148         }
149         return NULL;
150 }
151
152 SSL** FindSSLPointerFromSocket(SOCKET s)
153 {
154         int i;
155         for(i = 0; i < MAX_SSL_SOCKET; i++)
156         {
157                 if(g_pOpenSSLHandle[i])
158                 {
159                         if(pSSL_get_fd(g_pOpenSSLHandle[i]) == s)
160                                 return &g_pOpenSSLHandle[i];
161                 }
162         }
163         return NULL;
164 }
165
166 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
167 {
168         if(!g_bOpenSSLLoaded)
169                 return;
170         EnterCriticalSection(&g_OpenSSLLock);
171         g_OpenSSLTimeout = Timeout;
172         g_pOpenSSLTimeoutCallback = pCallback;
173         LeaveCriticalSection(&g_OpenSSLLock);
174 }
175
176 BOOL AttachSSL(SOCKET s)
177 {
178         BOOL r;
179         DWORD Time;
180         SSL** ppSSL;
181         if(!g_bOpenSSLLoaded)
182                 return FALSE;
183         r = FALSE;
184         Time = timeGetTime();
185         EnterCriticalSection(&g_OpenSSLLock);
186         if(!g_pOpenSSLCTX)
187                 g_pOpenSSLCTX = pSSL_CTX_new(pSSLv23_method());
188         if(g_pOpenSSLCTX)
189         {
190                 if(ppSSL = GetUnusedSSLPointer())
191                 {
192                         if(*ppSSL = pSSL_new(g_pOpenSSLCTX))
193                         {
194                                 if(pSSL_set_fd(*ppSSL, s) != 0)
195                                 {
196                                         r = TRUE;
197                                         // SSLのネゴシエーションには時間がかかる場合がある
198                                         while(pSSL_connect(*ppSSL) != 1)
199                                         {
200                                                 LeaveCriticalSection(&g_OpenSSLLock);
201                                                 if(g_pOpenSSLTimeoutCallback() || timeGetTime() - Time >= g_OpenSSLTimeout)
202                                                 {
203                                                         DetachSSL(s);
204                                                         r = FALSE;
205                                                         EnterCriticalSection(&g_OpenSSLLock);
206                                                         break;
207                                                 }
208                                                 EnterCriticalSection(&g_OpenSSLLock);
209                                         }
210                                 }
211                                 else
212                                 {
213                                         LeaveCriticalSection(&g_OpenSSLLock);
214                                         DetachSSL(s);
215                                         EnterCriticalSection(&g_OpenSSLLock);
216                                 }
217                         }
218                 }
219         }
220         LeaveCriticalSection(&g_OpenSSLLock);
221         return r;
222 }
223
224 BOOL DetachSSL(SOCKET s)
225 {
226         BOOL r;
227         SSL** ppSSL;
228         if(!g_bOpenSSLLoaded)
229                 return FALSE;
230         r = FALSE;
231         EnterCriticalSection(&g_OpenSSLLock);
232         if(ppSSL = FindSSLPointerFromSocket(s))
233         {
234                 pSSL_shutdown(*ppSSL);
235                 pSSL_free(*ppSSL);
236                 *ppSSL = NULL;
237                 r = TRUE;
238         }
239         LeaveCriticalSection(&g_OpenSSLLock);
240         return r;
241 }
242
243 BOOL IsSSLAttached(SOCKET s)
244 {
245         SSL** ppSSL;
246         if(!g_bOpenSSLLoaded)
247                 return FALSE;
248         EnterCriticalSection(&g_OpenSSLLock);
249         ppSSL = FindSSLPointerFromSocket(s);
250         LeaveCriticalSection(&g_OpenSSLLock);
251         if(!ppSSL)
252                 return TRUE;
253         return TRUE;
254 }
255
256 SOCKET socketS(int af, int type, int protocol)
257 {
258         return socket(af, type, protocol);
259 }
260
261 int bindS(SOCKET s, const struct sockaddr *addr, int namelen)
262 {
263         return bind(s, addr, namelen);
264 }
265
266 int listenS(SOCKET s, int backlog)
267 {
268         return listen(s, backlog);
269 }
270
271 SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
272 {
273         SOCKET r;
274         r = accept(s, addr, addrlen);
275         if(!AttachSSL(r))
276         {
277                 closesocket(r);
278                 return INVALID_SOCKET;
279         }
280         return r;
281 }
282
283 int connectS(SOCKET s, const struct sockaddr *name, int namelen)
284 {
285         int r;
286         r = connect(s, name, namelen);
287         if(!AttachSSL(r))
288                 return SOCKET_ERROR;
289         return r;
290 }
291
292 int closesocketS(SOCKET s)
293 {
294         DetachSSL(s);
295         return closesocket(s);
296 }
297
298 int sendS(SOCKET s, const char * buf, int len, int flags)
299 {
300         SSL** ppSSL;
301         if(!g_bOpenSSLLoaded)
302                 return send(s, buf, len, flags);
303         EnterCriticalSection(&g_OpenSSLLock);
304         ppSSL = FindSSLPointerFromSocket(s);
305         LeaveCriticalSection(&g_OpenSSLLock);
306         if(!ppSSL)
307                 return send(s, buf, len, flags);
308         return pSSL_write(*ppSSL, buf, len);
309 }
310
311 int recvS(SOCKET s, char * buf, int len, int flags)
312 {
313         SSL** ppSSL;
314         if(!g_bOpenSSLLoaded)
315                 return recv(s, buf, len, flags);
316         EnterCriticalSection(&g_OpenSSLLock);
317         ppSSL = FindSSLPointerFromSocket(s);
318         LeaveCriticalSection(&g_OpenSSLLock);
319         if(!ppSSL)
320                 return recv(s, buf, len, flags);
321         if(flags & MSG_PEEK)
322                 return pSSL_peek(*ppSSL, buf, len);
323         return pSSL_read(*ppSSL, buf, len);
324 }
325