2 // Copyright (C) 2011 Suguru Kawamoto
4 // socket関連関数をOpenSSL用に置換
5 // コンパイルにはOpenSSLのヘッダーファイルが必要
10 #include <openssl/ssl.h>
12 #include "socketwrapper.h"
14 typedef void (__cdecl* _SSL_load_error_strings)();
15 typedef int (__cdecl* _SSL_library_init)();
16 typedef SSL_METHOD* (__cdecl* _SSLv23_method)();
17 typedef SSL_CTX* (__cdecl* _SSL_CTX_new)(SSL_METHOD*);
18 typedef void (__cdecl* _SSL_CTX_free)(SSL_CTX*);
19 typedef SSL* (__cdecl* _SSL_new)(SSL_CTX*);
20 typedef void (__cdecl* _SSL_free)(SSL*);
21 typedef int (__cdecl* _SSL_shutdown)(SSL*);
22 typedef int (__cdecl* _SSL_get_fd)(SSL*);
23 typedef int (__cdecl* _SSL_set_fd)(SSL*, int);
24 typedef int (__cdecl* _SSL_accept)(SSL*);
25 typedef int (__cdecl* _SSL_connect)(SSL*);
26 typedef int (__cdecl* _SSL_write)(SSL*, const void*, int);
27 typedef int (__cdecl* _SSL_peek)(SSL*, void*, int);
28 typedef int (__cdecl* _SSL_read)(SSL*, void*, int);
29 typedef int (__cdecl* _SSL_get_error)(SSL*, int);
31 _SSL_load_error_strings pSSL_load_error_strings;
32 _SSL_library_init pSSL_library_init;
33 _SSLv23_method pSSLv23_method;
34 _SSL_CTX_new pSSL_CTX_new;
35 _SSL_CTX_free pSSL_CTX_free;
38 _SSL_shutdown pSSL_shutdown;
39 _SSL_get_fd pSSL_get_fd;
40 _SSL_set_fd pSSL_set_fd;
41 _SSL_accept pSSL_accept;
42 _SSL_connect pSSL_connect;
43 _SSL_write pSSL_write;
46 _SSL_get_error pSSL_get_error;
48 #define MAX_SSL_SOCKET 64
50 BOOL g_bOpenSSLLoaded;
52 CRITICAL_SECTION g_OpenSSLLock;
53 DWORD g_OpenSSLTimeout;
54 LPSSLTIMEOUTCALLBACK g_pOpenSSLTimeoutCallback;
55 SSL_CTX* g_pOpenSSLCTX;
56 SSL* g_pOpenSSLHandle[MAX_SSL_SOCKET];
58 BOOL __stdcall DefaultSSLTimeoutCallback()
68 g_hOpenSSL = LoadLibrary("ssleay32.dll");
70 g_hOpenSSL = LoadLibrary("libssl32.dll");
72 || !(pSSL_load_error_strings = (_SSL_load_error_strings)GetProcAddress(g_hOpenSSL, "SSL_load_error_strings"))
73 || !(pSSL_library_init = (_SSL_library_init)GetProcAddress(g_hOpenSSL, "SSL_library_init"))
74 || !(pSSLv23_method = (_SSLv23_method)GetProcAddress(g_hOpenSSL, "SSLv23_method"))
75 || !(pSSL_CTX_new = (_SSL_CTX_new)GetProcAddress(g_hOpenSSL, "SSL_CTX_new"))
76 || !(pSSL_CTX_free = (_SSL_CTX_free)GetProcAddress(g_hOpenSSL, "SSL_CTX_free"))
77 || !(pSSL_new = (_SSL_new)GetProcAddress(g_hOpenSSL, "SSL_new"))
78 || !(pSSL_free = (_SSL_free)GetProcAddress(g_hOpenSSL, "SSL_free"))
79 || !(pSSL_shutdown = (_SSL_shutdown)GetProcAddress(g_hOpenSSL, "SSL_shutdown"))
80 || !(pSSL_get_fd = (_SSL_get_fd)GetProcAddress(g_hOpenSSL, "SSL_get_fd"))
81 || !(pSSL_set_fd = (_SSL_set_fd)GetProcAddress(g_hOpenSSL, "SSL_set_fd"))
82 || !(pSSL_accept = (_SSL_accept)GetProcAddress(g_hOpenSSL, "SSL_accept"))
83 || !(pSSL_connect = (_SSL_connect)GetProcAddress(g_hOpenSSL, "SSL_connect"))
84 || !(pSSL_write = (_SSL_write)GetProcAddress(g_hOpenSSL, "SSL_write"))
85 || !(pSSL_peek = (_SSL_peek)GetProcAddress(g_hOpenSSL, "SSL_peek"))
86 || !(pSSL_read = (_SSL_read)GetProcAddress(g_hOpenSSL, "SSL_read"))
87 || !(pSSL_get_error = (_SSL_get_error)GetProcAddress(g_hOpenSSL, "SSL_get_error")))
90 FreeLibrary(g_hOpenSSL);
94 InitializeCriticalSection(&g_OpenSSLLock);
95 pSSL_load_error_strings();
97 SetSSLTimeoutCallback(60000, DefaultSSLTimeoutCallback);
98 g_bOpenSSLLoaded = TRUE;
105 if(!g_bOpenSSLLoaded)
107 EnterCriticalSection(&g_OpenSSLLock);
108 for(i = 0; i < MAX_SSL_SOCKET; i++)
110 if(g_pOpenSSLHandle[i])
112 pSSL_shutdown(g_pOpenSSLHandle[i]);
113 pSSL_free(g_pOpenSSLHandle[i]);
114 g_pOpenSSLHandle[i] = NULL;
118 pSSL_CTX_free(g_pOpenSSLCTX);
119 g_pOpenSSLCTX = NULL;
120 FreeLibrary(g_hOpenSSL);
122 LeaveCriticalSection(&g_OpenSSLLock);
123 DeleteCriticalSection(&g_OpenSSLLock);
124 g_bOpenSSLLoaded = FALSE;
127 BOOL IsOpenSSLLoaded()
129 return g_bOpenSSLLoaded;
132 SSL** GetUnusedSSLPointer()
135 for(i = 0; i < MAX_SSL_SOCKET; i++)
137 if(!g_pOpenSSLHandle[i])
138 return &g_pOpenSSLHandle[i];
143 SSL** FindSSLPointerFromSocket(SOCKET s)
146 for(i = 0; i < MAX_SSL_SOCKET; i++)
148 if(g_pOpenSSLHandle[i])
150 if(pSSL_get_fd(g_pOpenSSLHandle[i]) == s)
151 return &g_pOpenSSLHandle[i];
157 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
159 if(!g_bOpenSSLLoaded)
161 EnterCriticalSection(&g_OpenSSLLock);
162 g_OpenSSLTimeout = Timeout;
163 g_pOpenSSLTimeoutCallback = pCallback;
164 LeaveCriticalSection(&g_OpenSSLLock);
167 BOOL AttachSSL(SOCKET s)
172 if(!g_bOpenSSLLoaded)
175 Time = timeGetTime();
176 EnterCriticalSection(&g_OpenSSLLock);
178 g_pOpenSSLCTX = pSSL_CTX_new(pSSLv23_method());
181 if(ppSSL = GetUnusedSSLPointer())
183 if(*ppSSL = pSSL_new(g_pOpenSSLCTX))
185 if(pSSL_set_fd(*ppSSL, s) != 0)
188 // SSLのネゴシエーションには時間がかかる場合がある
189 while(pSSL_connect(*ppSSL) != 1)
191 LeaveCriticalSection(&g_OpenSSLLock);
192 if(g_pOpenSSLTimeoutCallback() || timeGetTime() - Time >= g_OpenSSLTimeout)
196 EnterCriticalSection(&g_OpenSSLLock);
199 EnterCriticalSection(&g_OpenSSLLock);
204 LeaveCriticalSection(&g_OpenSSLLock);
206 EnterCriticalSection(&g_OpenSSLLock);
211 LeaveCriticalSection(&g_OpenSSLLock);
215 BOOL DetachSSL(SOCKET s)
219 if(!g_bOpenSSLLoaded)
222 EnterCriticalSection(&g_OpenSSLLock);
223 if(ppSSL = FindSSLPointerFromSocket(s))
225 pSSL_shutdown(*ppSSL);
230 LeaveCriticalSection(&g_OpenSSLLock);
234 BOOL IsSSLAttached(SOCKET s)
237 if(!g_bOpenSSLLoaded)
239 EnterCriticalSection(&g_OpenSSLLock);
240 ppSSL = FindSSLPointerFromSocket(s);
241 LeaveCriticalSection(&g_OpenSSLLock);
247 SOCKET socketS(int af, int type, int protocol)
249 return socket(af, type, protocol);
252 int bindS(SOCKET s, const struct sockaddr *addr, int namelen)
254 return bind(s, addr, namelen);
257 int listenS(SOCKET s, int backlog)
259 return listen(s, backlog);
262 SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
265 r = accept(s, addr, addrlen);
269 return INVALID_SOCKET;
274 int connectS(SOCKET s, const struct sockaddr *name, int namelen)
277 r = connect(s, name, namelen);
283 int closesocketS(SOCKET s)
286 return closesocket(s);
289 int sendS(SOCKET s, const char * buf, int len, int flags)
292 if(!g_bOpenSSLLoaded)
293 return send(s, buf, len, flags);
294 EnterCriticalSection(&g_OpenSSLLock);
295 ppSSL = FindSSLPointerFromSocket(s);
296 LeaveCriticalSection(&g_OpenSSLLock);
298 return send(s, buf, len, flags);
299 return pSSL_write(*ppSSL, buf, len);
302 int recvS(SOCKET s, char * buf, int len, int flags)
305 if(!g_bOpenSSLLoaded)
306 return recv(s, buf, len, flags);
307 EnterCriticalSection(&g_OpenSSLLock);
308 ppSSL = FindSSLPointerFromSocket(s);
309 LeaveCriticalSection(&g_OpenSSLLock);
311 return recv(s, buf, len, flags);
313 return pSSL_peek(*ppSSL, buf, len);
314 return pSSL_read(*ppSSL, buf, len);