/*
	ZazouModSSL

	Copyright (C) 2003-2009 Xavier Garreau <xavier@xgarreau.org>

	This file is part of ZazouModSSL.

    ZazouMiniWebServer 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.

    ZazouModSSL 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 ZazouModSSL; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

	In addition, as a special exception, Xavier Garreau gives permission
	to link the code of this program with the OpenSSL library
	(or with modified versions of OpenSSL that use the same license as OpenSSL),
	and distribute linked combinations including the two.
	You must obey the GNU General Public License in all respects for all
	of the code used other than OpenSSL. If you modify this file, you may
	extend this exception to your version of the file, but you are not
	obligated to do so. If you do not wish to do so, delete this exception
	statement from your version. 
*/

#include "stdafx.h"
#include "ZazouModSsl.h"
#include <openssl/ssl.h>
#include <openssl/err.h>

/* *** *** */

SSL_METHOD *method;
SSL_CTX *ctx;

#define CHAIN_FILE "_certs.zmwsc\\chain.file"
#define PRIV_KEY "_certs.zmwsc\\priv.key"

int ZazouModSsl_Initialize ();
SSL* ZazouModSsl_Accept (int fd);
int ZazouModSsl_Close (SSL* ssl);
int ZazouModSsl_Read (SSL* ssl, LPBYTE data, DWORD datalen);
int ZazouModSsl_Write (SSL* ssl, LPBYTE data, DWORD datalen);

ZazouModSsl_Functions functions = {
	(fnInitialize_t)ZazouModSsl_Initialize,
	(fnAccept_t)ZazouModSsl_Accept,
	(fnClose_t)ZazouModSsl_Close,
	(fnRead_t)ZazouModSsl_Read,
	(fnWrite_t)ZazouModSsl_Write
};


/* *** *** */

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

int ZazouModSsl_Initialize () {
	OpenSSL_add_all_algorithms();
	SSL_load_error_strings();

	method = SSLv23_server_method();
	ctx = SSL_CTX_new(method);
	if (!ctx) {
		ERR_print_errors_fp(stderr);
		return 1;
	}

	SSL_CTX_use_certificate_chain_file(ctx, CHAIN_FILE);
	SSL_CTX_use_PrivateKey_file (ctx, PRIV_KEY, SSL_FILETYPE_PEM);
	if (!SSL_CTX_check_private_key(ctx)) {
		return 2;
	}

	return 0;
}

int ZazouModSsl_Close(SSL* ssl)
{
	if (!ssl) return 1;

	SSL_shutdown(ssl);
	SSL_free(ssl);
	return 0;
}

SSL* ZazouModSsl_Accept (int fd) {
	SSL* ssl = NULL;
	ssl = SSL_new(ctx);
	if (ssl) {
		if (SSL_set_fd (ssl, fd)) {
			if (!SSL_accept(ssl)) {
				SSL_free(ssl);
				ssl = NULL;
			}
		} else {
				SSL_free(ssl);
				ssl = NULL;
		}
	}
	return ssl;
}

int ZazouModSsl_Read(SSL* ssl, LPBYTE data, DWORD datalen)
{
	if (!ssl) return 0;
	if (!datalen) datalen = 16*1024;
	return SSL_read (ssl, data, datalen);
}

int ZazouModSsl_Write(SSL* ssl, LPBYTE data, DWORD datalen)
{
	if (!ssl) return 0;
	if (!datalen) return 0;
	return SSL_write (ssl, data, datalen);
}

// Il s'agit d'un exemple de fonction exportée.
ZazouModSsl_Functions ZazouModSsl(void)
{
	return functions;
}
