2 // Copyright (C) 2011 Suguru Kawamoto
4 // socket関連関数をOpenSSL用に置換
5 // コンパイルにはOpenSSLのヘッダーファイルが必要
10 #include <openssl/ssl.h>
12 #include "socketwrapper.h"
13 #include "protectprocess.h"
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);
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;
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;
47 _SSL_get_error pSSL_get_error;
49 #define MAX_SSL_SOCKET 64
51 BOOL g_bOpenSSLLoaded;
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];
59 BOOL __stdcall DefaultSSLTimeoutCallback()
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");
76 g_hOpenSSL = LoadLibrary("ssleay32.dll");
78 g_hOpenSSL = LoadLibrary("libssl32.dll");
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")))
98 FreeLibrary(g_hOpenSSL);
102 InitializeCriticalSection(&g_OpenSSLLock);
103 pSSL_load_error_strings();
105 SetSSLTimeoutCallback(60000, DefaultSSLTimeoutCallback);
106 g_bOpenSSLLoaded = TRUE;
113 if(!g_bOpenSSLLoaded)
115 EnterCriticalSection(&g_OpenSSLLock);
116 for(i = 0; i < MAX_SSL_SOCKET; i++)
118 if(g_pOpenSSLHandle[i])
120 pSSL_shutdown(g_pOpenSSLHandle[i]);
121 pSSL_free(g_pOpenSSLHandle[i]);
122 g_pOpenSSLHandle[i] = NULL;
126 pSSL_CTX_free(g_pOpenSSLCTX);
127 g_pOpenSSLCTX = NULL;
128 FreeLibrary(g_hOpenSSL);
130 LeaveCriticalSection(&g_OpenSSLLock);
131 DeleteCriticalSection(&g_OpenSSLLock);
132 g_bOpenSSLLoaded = FALSE;
135 BOOL IsOpenSSLLoaded()
137 return g_bOpenSSLLoaded;
140 SSL** GetUnusedSSLPointer()
143 for(i = 0; i < MAX_SSL_SOCKET; i++)
145 if(!g_pOpenSSLHandle[i])
146 return &g_pOpenSSLHandle[i];
151 SSL** FindSSLPointerFromSocket(SOCKET s)
154 for(i = 0; i < MAX_SSL_SOCKET; i++)
156 if(g_pOpenSSLHandle[i])
158 if(pSSL_get_fd(g_pOpenSSLHandle[i]) == s)
159 return &g_pOpenSSLHandle[i];
165 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
167 if(!g_bOpenSSLLoaded)
169 EnterCriticalSection(&g_OpenSSLLock);
170 g_OpenSSLTimeout = Timeout;
171 g_pOpenSSLTimeoutCallback = pCallback;
172 LeaveCriticalSection(&g_OpenSSLLock);
175 BOOL AttachSSL(SOCKET s)
180 if(!g_bOpenSSLLoaded)
183 Time = timeGetTime();
184 EnterCriticalSection(&g_OpenSSLLock);
186 g_pOpenSSLCTX = pSSL_CTX_new(pSSLv23_method());
189 if(ppSSL = GetUnusedSSLPointer())
191 if(*ppSSL = pSSL_new(g_pOpenSSLCTX))
193 if(pSSL_set_fd(*ppSSL, s) != 0)
196 // SSLのネゴシエーションには時間がかかる場合がある
197 while(pSSL_connect(*ppSSL) != 1)
199 LeaveCriticalSection(&g_OpenSSLLock);
200 if(g_pOpenSSLTimeoutCallback() || timeGetTime() - Time >= g_OpenSSLTimeout)
204 EnterCriticalSection(&g_OpenSSLLock);
207 EnterCriticalSection(&g_OpenSSLLock);
212 LeaveCriticalSection(&g_OpenSSLLock);
214 EnterCriticalSection(&g_OpenSSLLock);
219 LeaveCriticalSection(&g_OpenSSLLock);
223 BOOL DetachSSL(SOCKET s)
227 if(!g_bOpenSSLLoaded)
230 EnterCriticalSection(&g_OpenSSLLock);
231 if(ppSSL = FindSSLPointerFromSocket(s))
233 pSSL_shutdown(*ppSSL);
238 LeaveCriticalSection(&g_OpenSSLLock);
242 BOOL IsSSLAttached(SOCKET s)
245 if(!g_bOpenSSLLoaded)
247 EnterCriticalSection(&g_OpenSSLLock);
248 ppSSL = FindSSLPointerFromSocket(s);
249 LeaveCriticalSection(&g_OpenSSLLock);
255 SOCKET socketS(int af, int type, int protocol)
257 return socket(af, type, protocol);
260 int bindS(SOCKET s, const struct sockaddr *addr, int namelen)
262 return bind(s, addr, namelen);
265 int listenS(SOCKET s, int backlog)
267 return listen(s, backlog);
270 SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
273 r = accept(s, addr, addrlen);
277 return INVALID_SOCKET;
282 int connectS(SOCKET s, const struct sockaddr *name, int namelen)
285 r = connect(s, name, namelen);
291 int closesocketS(SOCKET s)
294 return closesocket(s);
297 int sendS(SOCKET s, const char * buf, int len, int flags)
300 if(!g_bOpenSSLLoaded)
301 return send(s, buf, len, flags);
302 EnterCriticalSection(&g_OpenSSLLock);
303 ppSSL = FindSSLPointerFromSocket(s);
304 LeaveCriticalSection(&g_OpenSSLLock);
306 return send(s, buf, len, flags);
307 return pSSL_write(*ppSSL, buf, len);
310 int recvS(SOCKET s, char * buf, int len, int flags)
313 if(!g_bOpenSSLLoaded)
314 return recv(s, buf, len, flags);
315 EnterCriticalSection(&g_OpenSSLLock);
316 ppSSL = FindSSLPointerFromSocket(s);
317 LeaveCriticalSection(&g_OpenSSLLock);
319 return recv(s, buf, len, flags);
321 return pSSL_peek(*ppSSL, buf, len);
322 return pSSL_read(*ppSSL, buf, len);