summaryrefslogtreecommitdiff
path: root/plugins/OpenSSL/src/ssl_openssl.cpp
blob: 78ed29bd6900dd34e56eb3cc5b0c45e772bf844d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-14 Miranda NG project (http://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "commonheaders.h"
#include <m_popup.h>

#define SECURITY_WIN32
#include <security.h>

#include <openssl\ssl.h>

static HMODULE g_hOpenSSL;
static HMODULE g_hOpenSSLCrypto;
static HANDLE g_hSslMutex;
static bool bSslInitDone;

/* OpenSSL dynamic imports */

typedef void ( *pfnRAND_screen)(void);
typedef int ( *pfnSSL_library_init ) (void );
typedef int ( *pfnCRYPTO_set_mem_functions )(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *));
typedef void ( *pfnSSL_load_error_strings )(void );
typedef void ( *pfnSSL_free )(SSL *ssl);
typedef int ( *pfnSSL_connect )(SSL *ssl);
typedef int ( *pfnSSL_read )(SSL *ssl,void *buf,int num);
typedef int ( *pfnSSL_set_fd )(SSL *s, int fd);
typedef int ( *pfnSSL_peek )(SSL *ssl,void *buf,int num);
typedef int ( *pfnSSL_pending )(const SSL *s);
typedef int ( *pfnSSL_write )(SSL *ssl,const void *buf,int num);
typedef SSL * ( *pfnSSL_new )(SSL_CTX *ctx);
typedef int ( *pfnSSL_shutdown )(SSL *s);
typedef void ( *pfnSSL_CTX_free )(SSL_CTX *);
typedef SSL_CTX * ( *pfnSSL_CTX_new )(const SSL_METHOD *meth);
typedef long ( *pfnSSL_CTX_ctrl )(SSL_CTX *ctx,int cmd, long larg, void *parg);
typedef int ( *pfnSSL_get_error )(const SSL *s,int ret_code);

typedef const SSL_CIPHER * ( *pfnSSL_get_current_cipher )(const SSL *s);
typedef const char * ( *pfnSSL_CIPHER_get_name )(const SSL_CIPHER *c);

typedef STACK_OF(X509) * ( *pfnSSL_get_peer_cert_chain )(const SSL *s);
typedef X509 * ( *pfnSSL_get_peer_certificate )(const SSL *s);
typedef void ( *pfnCRYPTO_free )(void *ptr);
typedef void ( *pfnX509_free )(X509 *x509);
typedef int ( *pfni2d_X509 )(X509 *x, unsigned char **out);

typedef const SSL_METHOD * ( *pfnSSLv23_client_method )(void);

static struct LIBOPENSSL {
	pfnRAND_screen				RAND_screen;
	pfnSSL_connect				SSL_connect;
	pfnSSL_free					SSL_free;
	pfnSSL_get_error			SSL_get_error;
	pfnSSL_library_init			SSL_library_init;
	pfnSSL_load_error_strings	SSL_load_error_strings;
	pfnSSL_new					SSL_new;
	pfnSSL_peek					SSL_peek;
	pfnSSL_pending				SSL_pending;
	pfnSSL_read					SSL_read;
	pfnSSL_write				SSL_write;
	pfnSSL_set_fd				SSL_set_fd;
	pfnSSL_shutdown				SSL_shutdown;
	pfnSSL_CTX_free				SSL_CTX_free;
	pfnSSL_CTX_new				SSL_CTX_new;
	pfnSSL_CTX_ctrl				SSL_CTX_ctrl;
	pfnCRYPTO_set_mem_functions	CRYPTO_set_mem_functions;

	pfnSSLv23_client_method		SSLv23_client_method;

	pfnSSL_get_current_cipher	SSL_get_current_cipher;
	pfnSSL_CIPHER_get_name		SSL_CIPHER_get_name;

	pfnSSL_get_peer_certificate	SSL_get_peer_certificate;
	pfnSSL_get_peer_cert_chain	SSL_get_peer_cert_chain;
	pfnCRYPTO_free				CRYPTO_free;
	pfnX509_free				X509_free;
	pfni2d_X509					i2d_X509;
} g_OpenSSL;


/* Crypt32 dynamic imports */

typedef BOOL (WINAPI *pfnCertGetCertificateChain)(HCERTCHAINENGINE, PCCERT_CONTEXT, LPFILETIME, HCERTSTORE, PCERT_CHAIN_PARA, DWORD, LPVOID, PCCERT_CHAIN_CONTEXT*);
typedef VOID (WINAPI *pfnCertFreeCertificateChain)(PCCERT_CHAIN_CONTEXT);
typedef BOOL (WINAPI *pfnCertFreeCertificateContext)(PCCERT_CONTEXT);
typedef BOOL (WINAPI *pfnCertVerifyCertificateChainPolicy)(LPCSTR, PCCERT_CHAIN_CONTEXT, PCERT_CHAIN_POLICY_PARA, PCERT_CHAIN_POLICY_STATUS);
typedef HCERTSTORE (WINAPI *pfnCertOpenStore)(LPCSTR, DWORD, HCRYPTPROV_LEGACY, DWORD, const void *);

typedef BOOL (WINAPI *pfnCertCloseStore)(HCERTSTORE, DWORD);
typedef BOOL (WINAPI *pfnCertAddCertificateContextToStore)(HCERTSTORE, PCCERT_CONTEXT, DWORD, PCCERT_CONTEXT*);
typedef PCCERT_CONTEXT (WINAPI *pfnCertCreateCertificateContext)(DWORD, const BYTE *,DWORD);

static struct LIBCRYPT {
	pfnCertGetCertificateChain		CertGetCertificateChain;
	pfnCertFreeCertificateChain		CertFreeCertificateChain;
	pfnCertFreeCertificateContext	CertFreeCertificateContext;
	pfnCertVerifyCertificateChainPolicy	CertVerifyCertificateChainPolicy;
	pfnCertOpenStore				CertOpenStore;
	pfnCertCloseStore				CertCloseStore;
	pfnCertAddCertificateContextToStore	CertAddCertificateContextToStore;
	pfnCertCreateCertificateContext	CertCreateCertificateContext;
} g_Crypt;


typedef enum
{
	sockOpen,
	sockClosed,
	sockError
} SocketState;

struct SslHandle
{
	SOCKET s;

	SSL_CTX *ctx;
	SSL	*session;

	SocketState state;
};


void SslLog(const char *fmt, ...)
{
	va_list va;
	char szText[1024];

	va_start(va, fmt);
	mir_vsnprintf(szText, sizeof(szText), fmt, va);
	va_end(va);

	CallServiceSync(MS_NETLIB_LOG, (WPARAM)NULL, (LPARAM)szText);
}

static void SSL_library_unload(void) {
	/* Load Library Pointers */
	if (!bSslInitDone) return;

	WaitForSingleObject(g_hSslMutex, INFINITE);

	FreeLibrary(g_hOpenSSL);
	g_hOpenSSL = NULL;
	FreeLibrary(g_hOpenSSLCrypto);
	g_hOpenSSLCrypto = NULL;
	bSslInitDone = false;

	ReleaseMutex(g_hSslMutex);
}

static bool SSL_library_load(void)
{
	/* Load Library Pointers */
	if (bSslInitDone) return true;

	WaitForSingleObject(g_hSslMutex, INFINITE);

	if (!bSslInitDone)
	{
		g_hOpenSSLCrypto = LoadLibraryA("libeay32.dll");
		g_hOpenSSL = LoadLibraryA("ssleay32.dll");
		if (g_hOpenSSL && g_hOpenSSLCrypto)
		{
			// load function pointers
			#define LOAD_FN(struc,lib, name) struc.##name = (pfn##name)GetProcAddress(lib, #name);
			LOAD_FN(g_OpenSSL, g_hOpenSSLCrypto, RAND_screen);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_connect);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_free);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_get_error);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_library_init);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_load_error_strings);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_new);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_peek);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_pending);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_read);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_write);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_set_fd);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_shutdown);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_CTX_free);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_CTX_new);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_CTX_ctrl);
			LOAD_FN(g_OpenSSL, g_hOpenSSLCrypto, CRYPTO_set_mem_functions);

			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSLv23_client_method);

			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_get_current_cipher);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_CIPHER_get_name);

			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_get_peer_certificate);
			LOAD_FN(g_OpenSSL, g_hOpenSSL, SSL_get_peer_cert_chain);
			LOAD_FN(g_OpenSSL, g_hOpenSSLCrypto, CRYPTO_free);
			LOAD_FN(g_OpenSSL, g_hOpenSSLCrypto, X509_free);
			LOAD_FN(g_OpenSSL, g_hOpenSSLCrypto, i2d_X509);

			HINSTANCE hCrypt = LoadLibraryA("crypt32.dll");
			if (hCrypt)
			{
				LOAD_FN(g_Crypt, hCrypt, CertGetCertificateChain);
				LOAD_FN(g_Crypt, hCrypt, CertFreeCertificateChain);
				LOAD_FN(g_Crypt, hCrypt, CertFreeCertificateContext);
				LOAD_FN(g_Crypt, hCrypt, CertVerifyCertificateChainPolicy);
				LOAD_FN(g_Crypt, hCrypt, CertOpenStore);
				LOAD_FN(g_Crypt, hCrypt, CertCloseStore);
				LOAD_FN(g_Crypt, hCrypt, CertAddCertificateContextToStore);
				LOAD_FN(g_Crypt, hCrypt, CertCreateCertificateContext);
			}

			// init OpenSSL
			g_OpenSSL.SSL_library_init();
			g_OpenSSL.SSL_load_error_strings();
			g_OpenSSL.CRYPTO_set_mem_functions(mir_calloc, mir_realloc, mir_free);
			// FIXME check errors

			bSslInitDone = true;
		} else {
			SSL_library_unload();
		}
	}

	ReleaseMutex(g_hSslMutex);
	return bSslInitDone;
}

const char* SSL_GetCipherName(SslHandle *ssl)
{
	if (!ssl || !ssl->session)
		return NULL;

	return g_OpenSSL.SSL_CIPHER_get_name(g_OpenSSL.SSL_get_current_cipher(ssl->session));
}

static void ReportSslError(SECURITY_STATUS scRet, int line, bool showPopup = false)
{
	TCHAR szMsgBuf[256];
	switch (scRet)
	{
	case 0:
	case ERROR_NOT_READY:
		return;

	case SEC_E_INVALID_TOKEN:
		_tcscpy(szMsgBuf, TranslateT("Client cannot decode host message. Possible causes: host does not support SSL or requires not existing security package"));
		break;

	case CERT_E_CN_NO_MATCH:
	case SEC_E_WRONG_PRINCIPAL:
		_tcscpy(szMsgBuf, TranslateT("Host we are connecting to is not the one certificate was issued for"));
		break;

	default:
		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL, scRet, LANG_USER_DEFAULT, szMsgBuf, SIZEOF(szMsgBuf), NULL);
	}

	TCHAR szMsgBuf2[512];
	mir_sntprintf(szMsgBuf2, SIZEOF(szMsgBuf2), _T("SSL connection failure (%x %u): %s"), scRet, line, szMsgBuf);

	char* szMsg = Utf8EncodeT(szMsgBuf2);
	SslLog(szMsg);
	mir_free(szMsg);

	SetLastError(scRet);
	PUShowMessageT(szMsgBuf2, SM_WARNING);
}

void NetlibSslFree(SslHandle *ssl)
{
	if (ssl == NULL) return;

	/* Delete Context */
	if (ssl->session) g_OpenSSL.SSL_free (ssl->session);
	if (ssl->ctx) g_OpenSSL.SSL_CTX_free (ssl->ctx);
	ssl->session = NULL;
	ssl->ctx = NULL;

	memset(ssl, 0, sizeof(SslHandle));
	mir_free(ssl);
}

BOOL NetlibSslPending(SslHandle *ssl)
{
	/* return true if there is either unsend or buffered received data (ie. after peek) */
	return ssl && ssl->session && (g_OpenSSL.SSL_pending(ssl->session) > 0);
}

static bool ClientConnect(SslHandle *ssl, const char *host)
{
	SSL_METHOD * meth;

	// contrary to what it's named, SSLv23 announces all supported ciphers/versions,
	// generally TLS1.2 in a TLS1.0 Client Hello
	meth = (SSL_METHOD*)g_OpenSSL.SSLv23_client_method();
	if (!meth) {
		SslLog("SSL setup failure: client method");
		return false;
	}
	ssl->ctx = g_OpenSSL.SSL_CTX_new(meth);
	if (!ssl->ctx) {
		SslLog("SSL setup failure: context");
		return false;
	}
	// disable dangerous cipher suites
	g_OpenSSL.SSL_CTX_ctrl(ssl->ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3, NULL);
	// SSL_read/write should transparently handle renegotiations
	g_OpenSSL.SSL_CTX_ctrl(ssl->ctx, SSL_CTRL_MODE, SSL_MODE_AUTO_RETRY, NULL);

	g_OpenSSL.RAND_screen();
	ssl->session = g_OpenSSL.SSL_new(ssl->ctx);
	if (!ssl->session) {
		SslLog("SSL setup failure: session");
		return false;
	}
	g_OpenSSL.SSL_set_fd(ssl->session, ssl->s);

	int err = g_OpenSSL.SSL_connect(ssl->session);

	if (err != 1) {
		err = g_OpenSSL.SSL_get_error(ssl->session, err);
		SslLog("SSL negotiation failure (%d)", err);
		return false;
	}

	const char* suite = SSL_GetCipherName(ssl);
	if (suite != NULL)
		SslLog("SSL established with %s", suite);
	return true;
}

static PCCERT_CONTEXT SSL_X509ToCryptCert(X509 * x509) {
	int len;
	unsigned char * buf = NULL;
	PCCERT_CONTEXT pCertContext = NULL;

	len = g_OpenSSL.i2d_X509(x509, &buf);
	if ((len >= 0) && buf) {
		pCertContext = g_Crypt.CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, buf, len);

		g_OpenSSL.CRYPTO_free(buf);
	}
	return pCertContext;
}

static PCCERT_CONTEXT SSL_CertChainToCryptAnchor(SSL* session)
{
	/* convert the active certificate chain provided in the handshake of 'session' into
	   the format used by CryptAPI.
	*/
	PCCERT_CONTEXT anchor = NULL;
	// create cert store
	HCERTSTORE store = g_Crypt.CertOpenStore(CERT_STORE_PROV_MEMORY, 0, NULL, CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG, NULL);

	if (store) {
		X509 *server_cert = g_OpenSSL.SSL_get_peer_certificate(session);
		if (server_cert) {
			// add the server's cert first, to make sure CryptAPI builds the correct chain
			PCCERT_CONTEXT primary_cert;
			BOOL ok = g_Crypt.CertAddCertificateContextToStore(store, SSL_X509ToCryptCert(server_cert), CERT_STORE_ADD_ALWAYS, &primary_cert);
			if (ok && primary_cert) {
				// add all remaining certs to store (note: stack needs not be freed, it is not a copy)
				STACK_OF(X509) *server_chain = g_OpenSSL.SSL_get_peer_cert_chain(session);
				if (server_chain) {
					X509 *next_cert;
					int i;
					for (i = 0; i < server_chain->stack.num; i++) {
						next_cert = (X509 *)server_chain->stack.data[i];
						g_Crypt.CertAddCertificateContextToStore(store, SSL_X509ToCryptCert(next_cert), CERT_STORE_ADD_USE_EXISTING, NULL);
					}
				}

				// return primary cert; MUST be freed by caller which will free the associated store
				anchor = primary_cert;
			} else {
				if (primary_cert) g_Crypt.CertFreeCertificateContext(primary_cert);
			}

			g_OpenSSL.X509_free(server_cert);
		}

		g_Crypt.CertCloseStore(store, 0);
	}

	return anchor;
}

static bool VerifyCertificate(SslHandle *ssl, PCSTR pszServerName, DWORD dwCertFlags)
{
	if (!g_Crypt.CertGetCertificateChain)
		return true;

	static LPSTR rgszUsages[] =
	{
		szOID_PKIX_KP_SERVER_AUTH,
		szOID_SERVER_GATED_CRYPTO,
		szOID_SGC_NETSCAPE
	};

	CERT_CHAIN_PARA          ChainPara = {0};
	HTTPSPolicyCallbackData  polHttps = {0};
	CERT_CHAIN_POLICY_PARA   PolicyPara = {0};
	CERT_CHAIN_POLICY_STATUS PolicyStatus = {0};
	PCCERT_CHAIN_CONTEXT     pChainContext = NULL;
	PCCERT_CONTEXT           pServerCert = NULL;
	DWORD scRet;

	PWSTR pwszServerName = mir_a2u(pszServerName);

	pServerCert = SSL_CertChainToCryptAnchor(ssl->session);

	if (pServerCert == NULL)
	{
		scRet = SEC_E_WRONG_PRINCIPAL;
		goto cleanup;
	}

	ChainPara.cbSize = sizeof(ChainPara);
	ChainPara.RequestedUsage.dwType = USAGE_MATCH_TYPE_OR;
	ChainPara.RequestedUsage.Usage.cUsageIdentifier = SIZEOF(rgszUsages);
	ChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = rgszUsages;
	if (!g_Crypt.CertGetCertificateChain(NULL, pServerCert, NULL, pServerCert->hCertStore,
	     &ChainPara, 0, NULL, &pChainContext))
	{
		scRet = GetLastError();
		goto cleanup;
	}

	polHttps.cbStruct = sizeof(HTTPSPolicyCallbackData);
	polHttps.dwAuthType = AUTHTYPE_SERVER;
	polHttps.fdwChecks = dwCertFlags;
	polHttps.pwszServerName = pwszServerName;

	PolicyPara.cbSize = sizeof(PolicyPara);
	PolicyPara.pvExtraPolicyPara = &polHttps;

	PolicyStatus.cbSize = sizeof(PolicyStatus);

	if (!g_Crypt.CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, pChainContext,
		&PolicyPara, &PolicyStatus))
	{
		scRet = GetLastError();
		goto cleanup;
	}

	if (PolicyStatus.dwError)
	{
		scRet = PolicyStatus.dwError;
		goto cleanup;
	}

	scRet = SEC_E_OK;

cleanup:
	if (pChainContext)
		g_Crypt.CertFreeCertificateChain(pChainContext);
	if (pServerCert)
		g_Crypt.CertFreeCertificateContext(pServerCert);
	mir_free(pwszServerName);

	ReportSslError(scRet, __LINE__, true);
	return scRet == SEC_E_OK;
}

SslHandle *NetlibSslConnect(SOCKET s, const char* host, int verify)
{
	SslHandle *ssl = (SslHandle*)mir_calloc(sizeof(SslHandle));
	ssl->s = s;
	/* negotiate SSL session, verify cert, return NULL if failed */

	DWORD dwFlags = 0;
	if (!host || inet_addr(host) != INADDR_NONE)
		dwFlags |= 0x00001000;

	bool res = SSL_library_load();
	if (!res) {
		return NULL;
	}

	if (res) res = ClientConnect(ssl, host);
	if (res && verify) res = VerifyCertificate(ssl, host, dwFlags);

	if (!res)
	{
		NetlibSslFree(ssl);
		ssl = NULL;
	}
	return ssl;
}

void NetlibSslShutdown(SslHandle *ssl)
{
	/* Close SSL session, but keep socket open */
	if (ssl==NULL || ssl->session == NULL)
		return;

	g_OpenSSL.SSL_shutdown(ssl->session);
}

int NetlibSslRead(SslHandle *ssl, char *buf, int num, int peek)
{
	/* read number of bytes, keep in buffer if peek!=0 */
	if (!ssl || !ssl->session) return SOCKET_ERROR;
	if (num <= 0) return 0;

	int err = 0;
	if (peek)
		err = g_OpenSSL.SSL_peek(ssl->session, buf, num);
	else
		err = g_OpenSSL.SSL_read(ssl->session, buf, num);

	if (err <= 0) {
		int err2 = g_OpenSSL.SSL_get_error(ssl->session, err);
		switch (err2) {
			case SSL_ERROR_ZERO_RETURN:
				SslLog("SSL connection gracefully closed");
				ssl->state = sockClosed;
				break;
			default:
				SslLog("SSL failure recieving data (%d, %d, %d)", err, err2, WSAGetLastError());
				ssl->state = sockError;
				return SOCKET_ERROR;
		}
		return 0;
	}

	return err;
}

int NetlibSslWrite(SslHandle *ssl, const char *buf, int num)
{
	/* write number of bytes */
	if (!ssl || !ssl->session) return SOCKET_ERROR;
	if (num <= 0) return 0;

	int err = g_OpenSSL.SSL_write(ssl->session, buf, num);
	if (err <= 0) {
		int err2 = g_OpenSSL.SSL_get_error(ssl->session, err);
		switch (err2) {
			case SSL_ERROR_ZERO_RETURN:
				SslLog("SSL connection gracefully closed");
				ssl->state = sockClosed;
				break;
			default:
				SslLog("SSL failure sending data (%d, %d, %d)", err, err2, WSAGetLastError());
				ssl->state = sockError;
				return SOCKET_ERROR;
		}
		return 0;
	}
	return err;
}

static INT_PTR GetSslApi(WPARAM, LPARAM lParam)
{
	SSL_API* si = (SSL_API*)lParam;
	if (si == NULL) return FALSE;

	if (si->cbSize != sizeof(SSL_API))
		return FALSE;

	si->connect = (HSSL (__cdecl *)(SOCKET, const char *, int))NetlibSslConnect;
	si->pending = (BOOL (__cdecl *)(HSSL))NetlibSslPending;
	si->read = (int  (__cdecl *)(HSSL, char *, int, int))NetlibSslRead;
	si->write = (int  (__cdecl *)(HSSL, const char *, int))NetlibSslWrite;
	si->shutdown = (void (__cdecl *)(HSSL))NetlibSslShutdown;
	si->sfree = (void (__cdecl *)(HSSL))NetlibSslFree;

	return TRUE;
}

int LoadSslModule(void)
{
	CreateServiceFunction(MS_SYSTEM_GET_SI, GetSslApi);
	g_hSslMutex = CreateMutex(NULL, FALSE, NULL);

	return 0;
}

void UnloadSslModule(void)
{
	CloseHandle(g_hSslMutex);
	SSL_library_unload();
}