ssl-proxy-openssl.c revision e95dba8921087afebb8a92c592af3b8ca22ae796
/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
#include "common.h"
#include "array.h"
#include "ioloop.h"
#include "network.h"
#include "ostream.h"
#include "read-full.h"
#include "safe-memset.h"
#include "llist.h"
#include "master-service.h"
#include "ssl-proxy.h"
#include <fcntl.h>
#include <unistd.h>
#ifdef HAVE_OPENSSL
/* Check every 30 minutes if parameters file has been updated */
enum ssl_io_action {
};
struct ssl_proxy {
int refcount;
unsigned char plainout_buf[1024];
unsigned int plainout_size;
unsigned char sslout_buf[1024];
unsigned int sslout_size;
void *handshake_callback_context;
char *last_error;
unsigned int handshaked:1;
unsigned int destroyed:1;
unsigned int cert_received:1;
unsigned int cert_broken:1;
unsigned int client:1;
};
struct ssl_parameters {
const char *fname;
int fd;
};
static int extdata_index;
static SSL_CTX *ssl_server_ctx;
static SSL_CTX *ssl_client_ctx;
static unsigned int ssl_proxy_count;
static struct ssl_proxy *ssl_proxies;
static struct ssl_parameters ssl_params;
static int ssl_username_nid;
static void ssl_params_corrupted(const char *path)
{
i_fatal("Corrupted SSL parameters file: %s/%s "
"(delete it and also the one in %s)",
}
{
int ret;
if (ret == 0)
}
{
unsigned char *buf;
const unsigned char *cbuf;
unsigned int len;
int bits;
/* read bit size. 0 ends the DH parameters list. */
if (bits == 0)
return FALSE;
/* read data size. */
switch (bits) {
case 512:
break;
case 1024:
break;
default:
}
return TRUE;
}
{
}
}
}
{
char c;
/* we'll wait until parameter file exists */
for (;;) {
break;
i_fatal("Can't open SSL parameter file %s: %m",
}
if (!warned) {
i_warning("Waiting for SSL parameter file %s",
}
sleep(1);
}
else
while (read_dh_parameters_next(params)) ;
else if (ret != 0) {
/* more data than expected */
}
i_error("close() failed: %m");
}
{
return;
if (params->last_mtime == 0)
else {
}
}
{
switch (action) {
case SSL_ADD_INPUT:
break;
break;
case SSL_REMOVE_INPUT:
break;
case SSL_ADD_OUTPUT:
break;
break;
case SSL_REMOVE_OUTPUT:
break;
}
}
{
if (block) {
} else {
plain_read, proxy);
}
}
}
{
/* buffer full, block input until it's written */
return;
}
sizeof(proxy->sslout_buf) -
proxy->sslout_size);
if (ret <= 0) {
if (ret < 0)
break;
} else {
if (!corked) {
}
}
}
if (corked)
}
{
if (ret < 0)
else {
if (proxy->plainout_size > 0) {
plain_write, proxy);
}
} else {
}
}
}
static const char *ssl_last_error(void)
{
unsigned long err;
char *buf;
err = ERR_get_error();
if (err == 0) {
if (errno != 0)
return "Unknown error";
}
return buf;
}
const char *func_name)
{
int err;
switch (err) {
case SSL_ERROR_WANT_READ:
break;
case SSL_ERROR_WANT_WRITE:
break;
case SSL_ERROR_SYSCALL:
/* eat up the error queue */
if (ERR_peek_error() != 0)
errstr = ssl_last_error();
else if (ret != 0)
else {
/* EOF. */
errstr = "Disconnected";
break;
}
break;
case SSL_ERROR_ZERO_RETURN:
/* clean connection closing */
break;
case SSL_ERROR_SSL:
func_name, ssl_last_error());
break;
default:
break;
}
}
}
{
int ret;
if (ret != 1) {
return;
}
} else {
if (ret != 1) {
return;
}
}
}
{
int ret;
sizeof(proxy->plainout_buf) -
if (ret <= 0) {
break;
} else {
}
}
}
{
int ret;
if (ret <= 0)
else {
proxy->sslout_size);
}
}
{
if (!proxy->handshaked)
if (proxy->handshaked) {
else
if (proxy->sslout_size == 0)
else {
}
}
}
static int
{
int sfd[2];
if (!ssl_initialized) {
i_error("SSL support not enabled in configuration");
return -1;
}
return -1;
}
return -1;
}
i_error("socketpair() failed: %m");
return -1;
}
return sfd[1];
}
{
int ret;
return -1;
return ret;
}
{
int ret;
return -1;
return ret;
}
{
}
{
}
{
char buf[1024];
const char *name;
return NULL;
return NULL; /* we should have had it.. */
name = "";
else
}
{
return proxy->handshaked;
}
{
return proxy->last_error;
}
{
if (!proxy->handshaked)
return "";
return t_strdup_printf("%s with cipher %s (%d/%d bits)",
}
{
}
{
return;
}
{
return;
}
{
}
{
/* Well, I'm not exactly sure why the logic in here is this.
It's the same as in Postfix, so it can't be too wrong. */
return ssl_params.dh_512;
return ssl_params.dh_1024;
}
{
if ((where & SSL_CB_ALERT) != 0) {
i_warning("SSL alert: where=0x%x, ret=%d: %s %s [%s]",
} else {
i_warning("SSL BIO failed: where=0x%x, ret=%d: %s [%s]",
}
}
{
if (login_settings->verbose_ssl ||
char buf[1024];
if (!preverify_ok)
else
}
if (!preverify_ok)
/* Return success anyway, because if ssl_require_client_cert=no we
could still allow authentication. */
return 1;
}
static int
void *userdata)
{
i_error("SSL private key file is password protected, "
"but password isn't given");
return 0;
}
return 0;
}
unsigned int ssl_proxy_get_count(void)
{
return ssl_proxy_count;
}
static bool is_pem_key_file(const char *path)
{
char buf[4096];
/* this code is used only for giving a better error message,
so it needs to catch only the normal key files */
if (fd == -1)
return FALSE;
if (ret <= 0)
return FALSE;
}
static void
{
NULL) != 1) {
i_fatal("Can't load CA file %s: %s",
}
}
if (set->verbose_ssl)
if (SSL_CTX_need_tmp_RSA(ssl_ctx))
}
static void
{
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
#endif
}
{
char *password;
unsigned long err;
i_fatal("SSL_CTX_new() failed");
i_fatal("Can't set cipher list to '%s': %s",
}
err = ERR_peek_error();
i_fatal("Can't load certificate file %s: %s",
i_fatal("Can't load certificate file %s: "
"The file contains a private key "
"(you've mixed ssl_cert_file and ssl_key_file settings)",
set->ssl_cert_file);
} else {
i_fatal("Can't load certificate file %s: "
"The file doesn't contain a certificate.",
set->ssl_cert_file);
}
}
SSL_FILETYPE_PEM) != 1) {
i_fatal("Can't load private key file %s: %s",
}
if (set->verbose_ssl)
if (set->ssl_verify_client_cert)
if (ssl_username_nid == NID_undef) {
i_fatal("Invalid ssl_cert_username_field: %s",
}
}
{
i_fatal("SSL_CTX_new() failed");
}
void ssl_proxy_init(void)
{
static char dovecot[] = "dovecot";
unsigned char buf;
return;
does it before chrooting. We might not have enough entropy at
the first try, so this function may fail. It's still been
initialized though. */
ssl_proxy_count = 0;
ssl_proxies = NULL;
}
void ssl_proxy_deinit(void)
{
if (!ssl_initialized)
return;
while (ssl_proxies != NULL)
EVP_cleanup();
}
#endif