/*
 * $Id: d9d30ece0361e26e2e414f05213890d6d0fa6ad2 $
 *
 * This file is part of nsmtp.
 *
 * Copyright (c) 2007, 2008 Niklas Olmes <niklas@noxa.de>
 *                          http://www.nevm.de/nsmtp/
 *                          NEVM-RIPE
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2
 * as published by the Free Software Foundation.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <stdio.h>

#include <openssl/ssl.h>
#include <openssl/err.h>

#include "ssl.h"

static RSA *cb_tmp_rsa(SSL *, int, int);
static int cb_dummy_verify(int, X509_STORE_CTX *);

const char *
ssl_error(void)
{
	static char buf[512];
	char str[512];
	const char *s;
	unsigned long e;

	e = ERR_get_error();
	ERR_error_string_n(e, str, sizeof(str));
	s = ERR_reason_error_string(e);
	(void)snprintf(buf, sizeof(buf), "error string: \"%s\", reason given: \"%s\"", str, s);

	return (const char *)buf;
}

const char *
ssl_info(SSL *ssl)
{
	static char buf[128];
	int usedbits, algbits;

	usedbits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ssl), &algbits);
	(void)snprintf(buf, sizeof(buf), "encrypted SMTP using %s with cipher %s (%d/%d bits)", SSL_get_version(ssl), SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)), usedbits, algbits);

	return (const char *)buf;
}

static RSA *
cb_tmp_rsa(SSL *ssl, int is_export, int keylen)
{

	(void)is_export;
	(void)ssl;

	return RSA_generate_key(keylen, 17UL, NULL, NULL);
}

static int
cb_dummy_verify(int pre, X509_STORE_CTX *x509ctx)
{

	(void)pre;
	(void)x509ctx;

	return 1;
}

void
ssl_callbacks(SSL_CTX *sslctx)
{

	SSL_CTX_set_tmp_rsa_callback(sslctx, cb_tmp_rsa);
	SSL_CTX_set_verify(sslctx, SSL_VERIFY_NONE, cb_dummy_verify);
}
