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 // 同梱する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");
77 g_hOpenSSL = LoadLibrary("ssleay32.dll");
79 g_hOpenSSL = LoadLibrary("libssl32.dll");
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")))
99 FreeLibrary(g_hOpenSSL);
103 InitializeCriticalSection(&g_OpenSSLLock);
104 pSSL_load_error_strings();
106 SetSSLTimeoutCallback(60000, DefaultSSLTimeoutCallback);
107 g_bOpenSSLLoaded = TRUE;
114 if(!g_bOpenSSLLoaded)
116 EnterCriticalSection(&g_OpenSSLLock);
117 for(i = 0; i < MAX_SSL_SOCKET; i++)
119 if(g_pOpenSSLHandle[i])
121 pSSL_shutdown(g_pOpenSSLHandle[i]);
122 pSSL_free(g_pOpenSSLHandle[i]);
123 g_pOpenSSLHandle[i] = NULL;
127 pSSL_CTX_free(g_pOpenSSLCTX);
128 g_pOpenSSLCTX = NULL;
129 FreeLibrary(g_hOpenSSL);
131 LeaveCriticalSection(&g_OpenSSLLock);
132 DeleteCriticalSection(&g_OpenSSLLock);
133 g_bOpenSSLLoaded = FALSE;
136 BOOL IsOpenSSLLoaded()
138 return g_bOpenSSLLoaded;
141 SSL** GetUnusedSSLPointer()
144 for(i = 0; i < MAX_SSL_SOCKET; i++)
146 if(!g_pOpenSSLHandle[i])
147 return &g_pOpenSSLHandle[i];
152 SSL** FindSSLPointerFromSocket(SOCKET s)
155 for(i = 0; i < MAX_SSL_SOCKET; i++)
157 if(g_pOpenSSLHandle[i])
159 if(pSSL_get_fd(g_pOpenSSLHandle[i]) == s)
160 return &g_pOpenSSLHandle[i];
166 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
168 if(!g_bOpenSSLLoaded)
170 EnterCriticalSection(&g_OpenSSLLock);
171 g_OpenSSLTimeout = Timeout;
172 g_pOpenSSLTimeoutCallback = pCallback;
173 LeaveCriticalSection(&g_OpenSSLLock);
176 BOOL AttachSSL(SOCKET s)
181 if(!g_bOpenSSLLoaded)
184 Time = timeGetTime();
185 EnterCriticalSection(&g_OpenSSLLock);
187 g_pOpenSSLCTX = pSSL_CTX_new(pSSLv23_method());
190 if(ppSSL = GetUnusedSSLPointer())
192 if(*ppSSL = pSSL_new(g_pOpenSSLCTX))
194 if(pSSL_set_fd(*ppSSL, s) != 0)
197 // SSLのネゴシエーションには時間がかかる場合がある
198 while(pSSL_connect(*ppSSL) != 1)
200 LeaveCriticalSection(&g_OpenSSLLock);
201 if(g_pOpenSSLTimeoutCallback() || timeGetTime() - Time >= g_OpenSSLTimeout)
205 EnterCriticalSection(&g_OpenSSLLock);
208 EnterCriticalSection(&g_OpenSSLLock);
213 LeaveCriticalSection(&g_OpenSSLLock);
215 EnterCriticalSection(&g_OpenSSLLock);
220 LeaveCriticalSection(&g_OpenSSLLock);
224 BOOL DetachSSL(SOCKET s)
228 if(!g_bOpenSSLLoaded)
231 EnterCriticalSection(&g_OpenSSLLock);
232 if(ppSSL = FindSSLPointerFromSocket(s))
234 pSSL_shutdown(*ppSSL);
239 LeaveCriticalSection(&g_OpenSSLLock);
243 BOOL IsSSLAttached(SOCKET s)
246 if(!g_bOpenSSLLoaded)
248 EnterCriticalSection(&g_OpenSSLLock);
249 ppSSL = FindSSLPointerFromSocket(s);
250 LeaveCriticalSection(&g_OpenSSLLock);
256 SOCKET socketS(int af, int type, int protocol)
258 return socket(af, type, protocol);
261 int bindS(SOCKET s, const struct sockaddr *addr, int namelen)
263 return bind(s, addr, namelen);
266 int listenS(SOCKET s, int backlog)
268 return listen(s, backlog);
271 SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
274 r = accept(s, addr, addrlen);
278 return INVALID_SOCKET;
283 int connectS(SOCKET s, const struct sockaddr *name, int namelen)
286 r = connect(s, name, namelen);
292 int closesocketS(SOCKET s)
295 return closesocket(s);
298 int sendS(SOCKET s, const char * buf, int len, int flags)
301 if(!g_bOpenSSLLoaded)
302 return send(s, buf, len, flags);
303 EnterCriticalSection(&g_OpenSSLLock);
304 ppSSL = FindSSLPointerFromSocket(s);
305 LeaveCriticalSection(&g_OpenSSLLock);
307 return send(s, buf, len, flags);
308 return pSSL_write(*ppSSL, buf, len);
311 int recvS(SOCKET s, char * buf, int len, int flags)
314 if(!g_bOpenSSLLoaded)
315 return recv(s, buf, len, flags);
316 EnterCriticalSection(&g_OpenSSLLock);
317 ppSSL = FindSSLPointerFromSocket(s);
318 LeaveCriticalSection(&g_OpenSSLLock);
320 return recv(s, buf, len, flags);
322 return pSSL_peek(*ppSSL, buf, len);
323 return pSSL_read(*ppSSL, buf, len);