/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "safe-memset.h"
#include "iostream-openssl.h"
#include "dovecot-openssl-common.h"
# define HAVE_ECDH
#endif
struct ssl_iostream_password_context {
const char *password;
const char *error;
};
{
#ifdef HAVE_RSA_GENERATE_KEY_EX
return rsa;
}
return NULL;
#else
#endif
}
{
/* 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. */
else
}
static int
void *userdata)
{
"but password isn't given";
return 0;
}
return 0;
}
}
{
char *key;
return -1;
}
}
}
static
{
char *dhvalue;
return -1;
}
}
}
static int
const struct ssl_iostream_cert *set,
const char **error_r)
{
int ret = 0;
return -1;
"Can't load SSL private key: %s",
ret = -1;
}
return ret;
}
static int
const struct ssl_iostream_settings *set,
const char **error_r)
{
int ret = 0;
return -1;
"Can't load DH parameters: %s",
ret = -1;
}
return ret;
}
{
/* mostly just copy&pasted from SSL_CTX_use_certificate_chain_file() */
X509 *x;
int ret = 0;
i_fatal("BIO_new_mem_buf() failed");
if (x == NULL)
goto end;
if (ERR_peek_error() != 0)
ret = 0;
if (ret != 0) {
/* If we could set up our certificate, now proceed to
* the CA certificates.
*/
int r;
unsigned long err;
if (r == 0) {
ret = 0;
goto end;
}
}
/* When the while loop ends, it's usually just EOF. */
err = ERR_peek_last_error();
else
ret = 0; /* some real error */
}
end:
return ret;
}
{
/* mostly just copy&pasted from X509_load_cert_crl_file() */
int i;
i_fatal("BIO_new_mem_buf() failed");
return -1;
i_fatal("sk_X509_NAME_new_null() failed");
for(i = 0; i < sk_X509_INFO_num(inf); i++) {
}
}
return 0;
}
static void
{
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
}
#endif
}
#ifdef HAVE_SSL_GET_SERVERNAME
void *context ATTR_UNUSED)
{
i_debug("SSL_get_servername() failed");
}
ssl_io->sni_context) < 0) {
return SSL_TLSEXT_ERR_ALERT_FATAL;
}
}
return SSL_TLSEXT_ERR_OK;
}
#endif
static int
const struct ssl_iostream_settings *set,
const char **error_r)
{
return -1;
}
}
"Can't load CA certs from directory %s: %s",
return -1;
}
}
"Can't verify remote client certs without CA (ssl_ca setting)" :
"Can't verify remote server certs without trusted CAs (ssl_client_ca_* settings)";
return -1;
}
return 0;
}
static int
const struct ssl_iostream_settings *set,
const char **error_r)
{
return -1;
}
set->curve_list);
return -1;
}
#endif
if (set->prefer_server_ciphers) {
}
long opts;
int min_protocol;
&opts, &min_protocol) < 0) {
"Unknown ssl_min_protocol setting '%s'",
set->min_protocol);
return -1;
}
#else
#endif
}
return -1;
}
return -1;
}
return -1;
}
return -1;
}
return -1;
}
/* set trusted CA certs */
if (set->verify_remote_cert) {
return -1;
}
"Invalid cert_username_field: %s",
return -1;
}
}
#ifdef HAVE_SSL_GET_SERVERNAME
if (!ctx->client_ctx) {
ssl_servername_callback) != 1) {
i_debug("OpenSSL library doesn't support SNI");
}
}
#endif
return 0;
}
#if defined(HAVE_ECDH) && !defined(SSL_CTX_set_ecdh_auto)
static int
{
int nid = 0;
return -1;
else {
/* clear errors added by the above calls */
}
}
return -1;
else {
/* clear errors added by the above calls */
}
}
return 0;
}
#endif
static int
const char **error_r ATTR_UNUSED)
{
#if defined(HAVE_ECDH) && !defined(SSL_CTX_set_ecdh_auto)
int nid;
const char *curve_name;
#endif
if (SSL_CTX_need_tmp_RSA(ssl_ctx) != 0)
#ifdef HAVE_ECDH
/* In the non-recommended situation where ECDH cipher suites are being
used instead of ECDHE, do not reuse the same ECDH key pair for
different sessions. This option improves forward secrecy. */
#ifdef SSL_CTX_set_ecdh_auto
/* OpenSSL >= 1.0.2 automatically handles ECDH temporary key parameter
selection. */
/* shouldn't happen */
return -1;
}
#else
/* For OpenSSL < 1.0.2, ECDH temporary key parameter selection must be
performed manually. Attempt to select the same curve as that used
in the server's private EC key file. Otherwise fall back to the
NIST P-384 (secp384r1) curve to be compliant with RFC 6460 when
AES-256 TLS cipher suites are in use. This fall back option does
however make Dovecot non-compliant with RFC 6460 which requires
curve NIST P-256 (prime256v1) be used when AES-128 TLS cipher
suites are in use. At least the non-compliance is in the form of
providing too much security rather than too little. */
return -1;
/* Fall back option */
nid = NID_secp384r1;
}
i_debug("SSL: elliptic curve %s will be used for ECDH and"
" ECDHE key exchanges", curve_name);
}
}
#endif
#endif
return 0;
}
static int
const struct ssl_iostream_settings *set,
const char **error_r)
{
/* enable all SSL workarounds, except empty fragments as it
makes SSL more vulnerable against attacks */
#ifdef SSL_OP_NO_COMPRESSION
if (!set->compression)
#endif
#ifdef SSL_OP_NO_TICKET
#endif
#ifdef SSL_MODE_RELEASE_BUFFERS
#endif
return -1;
}
struct ssl_iostream_context **ctx_r,
const char **error_r)
{
return -1;
}
return -1;
}
return 0;
}
struct ssl_iostream_context **ctx_r,
const char **error_r)
{
return -1;
}
return -1;
}
return 0;
}
{
}
{
return;
}
void openssl_iostream_global_deinit(void)
{
if (!ssl_global_initialized)
return;
}
const char **error_r)
{
const char *error;
return 0;
case 0:
"Unknown ssl_crypto_device: %s",
set->crypto_device);
/* fall through */
case -1:
/* we'll deinit at exit in any case */
return -1;
}
}
return 0;
}