#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/dh.h>
+#include <openssl/x509v3.h>
+
+#define MAX_CERT_CHAIN_LENGTH 10 /* XXX: do not hardcode */
static SSL_CTX * ssl_ctx;
static DH *dh_params;
static bool ConnSSL_LoadServerKey_openssl PARAMS(( SSL_CTX *c ));
+static bool ConnSSL_SetVerifyProperties_openssl PARAMS((SSL_CTX * c));
#endif
#ifdef HAVE_LIBGNUTLS
typedef struct {
int refcnt;
gnutls_certificate_credentials_t x509_cred;
+ gnutls_dh_params_t dh_params;
} x509_cred_slot;
static array x509_creds = INIT_ARRAY;
static size_t x509_cred_idx;
static gnutls_dh_params_t dh_params;
-static gnutls_priority_t priorities_cache;
+static gnutls_priority_t priorities_cache = NULL;
static bool ConnSSL_LoadServerKey_gnutls PARAMS(( void ));
+static bool ConnSSL_SetVerifyProperties_gnutls PARAMS((void));
#endif
#define SHA256_STRING_LEN (32 * 2 + 1)
/**
* Log OpenSSL error message.
*
+ * @param level The log level
* @param msg The error message.
* @param info Additional information text or NULL.
*/
+static void
+LogOpenSSL_CertInfo(int level, X509 * cert, const char *msg)
+{
+ BIO *mem;
+ char *memptr;
+ long len;
+
+ assert(cert);
+ assert(msg);
+
+ if (!cert)
+ return;
+ mem = BIO_new(BIO_s_mem());
+ if (!mem)
+ return;
+ X509_NAME_print_ex(mem, X509_get_subject_name(cert), 0,
+ XN_FLAG_ONELINE);
+ X509_NAME_print_ex(mem, X509_get_issuer_name(cert), 2, XN_FLAG_ONELINE);
+ if (BIO_write(mem, "", 1) == 1) {
+ len = BIO_get_mem_data(mem, &memptr);
+ if (memptr && len > 0)
+ Log(level, "%s: \"%s\".", msg, memptr);
+ }
+ (void)BIO_set_close(mem, BIO_CLOSE);
+ BIO_free(mem);
+}
+
static void
LogOpenSSLError(const char *error, const char *info)
{
static int
-Verify_openssl(UNUSED int preverify_ok, UNUSED X509_STORE_CTX *x509_ctx)
+Verify_openssl(int preverify_ok, X509_STORE_CTX * ctx)
{
+#ifdef DEBUG
+ if (!preverify_ok) {
+ int err = X509_STORE_CTX_get_error(ctx);
+ LogDebug("Certificate validation failed: %s",
+ X509_verify_cert_error_string(err));
+ }
+#else
+ (void)preverify_ok;
+ (void)ctx;
+#endif
+
+ /* Always(!) return success as we have to deal with invalid
+ * (self-signed, expired, ...) client certificates and with invalid
+ * server certificates when "SSLVerify" is disabled, which we don't
+ * know at this stage. Therefore we postpone this check, it will be
+ * (and has to be!) handled in cb_connserver_login_ssl(). */
return 1;
}
#endif
assert(slot->x509_cred != NULL);
slot->refcnt--;
if ((c->ssl_state.x509_cred_idx != x509_cred_idx) && (slot->refcnt <= 0)) {
- Log(LOG_INFO, "Discarding X509 certificate credentials from slot %zd.",
- c->ssl_state.x509_cred_idx);
- /* TODO/FIXME: DH parameters will still leak memory. */
+ LogDebug("Discarding X509 certificate credentials from slot %zd.",
+ c->ssl_state.x509_cred_idx);
gnutls_certificate_free_keys(slot->x509_cred);
gnutls_certificate_free_credentials(slot->x509_cred);
slot->x509_cred = NULL;
+ gnutls_dh_params_deinit(slot->dh_params);
+ slot->dh_params = NULL;
slot->refcnt = 0;
}
#endif
}
SSL_CTX_set_session_id_context(newctx, (unsigned char *)"ngircd", 6);
- SSL_CTX_set_options(newctx, SSL_OP_SINGLE_DH_USE|SSL_OP_NO_SSLv2);
+ if (!ConnSSL_SetVerifyProperties_openssl(newctx))
+ goto out;
+ SSL_CTX_set_options(newctx,
+ SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv2 |
+ SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 |
+ SSL_OP_NO_COMPRESSION);
SSL_CTX_set_mode(newctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
- SSL_CTX_set_verify(newctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
- Verify_openssl);
SSL_CTX_free(ssl_ctx);
ssl_ctx = newctx;
Log(LOG_INFO, "%s initialized.", OpenSSL_version(OPENSSL_VERSION));
if (!ConnSSL_LoadServerKey_gnutls())
goto out;
+ if (priorities_cache != NULL) {
+ gnutls_priority_deinit(priorities_cache);
+ }
if (gnutls_priority_init(&priorities_cache, Conf_SSLOptions.CipherList,
NULL) != GNUTLS_E_SUCCESS) {
Log(LOG_ERR,
goto out;
}
+ if (!ConnSSL_SetVerifyProperties_gnutls())
+ goto out;
+
Log(LOG_INFO, "GnuTLS %s initialized.", gnutls_check_version(NULL));
initialized = true;
return true;
#ifdef HAVE_LIBGNUTLS
+static bool
+ConnSSL_SetVerifyProperties_gnutls(void)
+{
+ int err;
+
+ if (!Conf_SSLOptions.CAFile)
+ return true;
+
+ x509_cred_slot *slot = array_get(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
+ gnutls_certificate_credentials_t x509_cred = slot->x509_cred;
+
+ err = gnutls_certificate_set_x509_trust_file(x509_cred,
+ Conf_SSLOptions.CAFile,
+ GNUTLS_X509_FMT_PEM);
+ if (err < 0) {
+ Log(LOG_ERR, "Failed to load x509 trust file %s: %s",
+ Conf_SSLOptions.CAFile, gnutls_strerror(err));
+ return false;
+ }
+ if (Conf_SSLOptions.CRLFile) {
+ err =
+ gnutls_certificate_set_x509_crl_file(x509_cred,
+ Conf_SSLOptions.CRLFile,
+ GNUTLS_X509_FMT_PEM);
+ if (err < 0) {
+ Log(LOG_ERR, "Failed to load x509 crl file %s: %s",
+ Conf_SSLOptions.CRLFile, gnutls_strerror(err));
+ return false;
+ }
+ }
+ return true;
+}
+
+
static bool
ConnSSL_LoadServerKey_gnutls(void)
{
return false;
}
- cert_file = Conf_SSLOptions.CertFile ? Conf_SSLOptions.CertFile:Conf_SSLOptions.KeyFile;
- if (!cert_file) {
- Log(LOG_ERR, "No SSL server key configured!");
- return false;
- }
-
if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
Log(LOG_WARNING,
"Ignoring SSL \"KeyFilePassword\": Not supported by GnuTLS.");
return false;
gnutls_certificate_set_dh_params(x509_cred, dh_params);
- err = gnutls_certificate_set_x509_key_file(x509_cred, cert_file, Conf_SSLOptions.KeyFile, GNUTLS_X509_FMT_PEM);
- if (err < 0) {
- Log(LOG_ERR,
- "Failed to set certificate key file (cert %s, key %s): %s",
- cert_file,
- Conf_SSLOptions.KeyFile ? Conf_SSLOptions.KeyFile : "(NULL)",
- gnutls_strerror(err));
- return false;
+
+ cert_file = Conf_SSLOptions.CertFile ?
+ Conf_SSLOptions.CertFile : Conf_SSLOptions.KeyFile;
+ if (Conf_SSLOptions.KeyFile) {
+ err = gnutls_certificate_set_x509_key_file(x509_cred, cert_file,
+ Conf_SSLOptions.KeyFile,
+ GNUTLS_X509_FMT_PEM);
+ if (err < 0) {
+ Log(LOG_ERR,
+ "Failed to set certificate key file (cert %s, key %s): %s",
+ cert_file,
+ Conf_SSLOptions.KeyFile ? Conf_SSLOptions.KeyFile : "(NULL)",
+ gnutls_strerror(err));
+ return false;
+ }
}
/* Free currently active x509 context (if any) unless it is still in use */
slot = array_get(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
if ((slot != NULL) && (slot->refcnt <= 0) && (slot->x509_cred != NULL)) {
- Log(LOG_INFO, "Discarding X509 certificate credentials from slot %zd.", x509_cred_idx);
- /* TODO/FIXME: DH parameters will still leak memory. */
+ LogDebug("Discarding X509 certificate credentials from slot %zd.",
+ x509_cred_idx);
gnutls_certificate_free_keys(slot->x509_cred);
gnutls_certificate_free_credentials(slot->x509_cred);
slot->x509_cred = NULL;
+ gnutls_dh_params_deinit(slot->dh_params);
+ slot->dh_params = NULL;
slot->refcnt = 0;
}
char *cert_key;
assert(ctx);
- if (!Conf_SSLOptions.KeyFile) {
- Log(LOG_ERR, "No SSL server key configured!");
- return false;
- }
-
SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
SSL_CTX_set_default_passwd_cb_userdata(ctx, &Conf_SSLOptions.KeyFilePassword);
+ if (!Conf_SSLOptions.KeyFile)
+ return true;
+
if (SSL_CTX_use_PrivateKey_file(ctx, Conf_SSLOptions.KeyFile, SSL_FILETYPE_PEM) != 1) {
array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
LogOpenSSLError("Failed to add private key", Conf_SSLOptions.KeyFile);
}
+static bool
+ConnSSL_SetVerifyProperties_openssl(SSL_CTX * ctx)
+{
+ X509_STORE *store = NULL;
+ X509_LOOKUP *lookup;
+ bool ret = false;
+
+ if (!Conf_SSLOptions.CAFile)
+ return true;
+
+ if (SSL_CTX_load_verify_locations(ctx, Conf_SSLOptions.CAFile, NULL) !=
+ 1) {
+ LogOpenSSLError("SSL_CTX_load_verify_locations", NULL);
+ goto out;
+ }
+
+ if (Conf_SSLOptions.CRLFile) {
+ X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
+ X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
+ SSL_CTX_set1_param(ctx, param);
+
+ store = SSL_CTX_get_cert_store(ctx);
+ assert(store);
+ lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
+ if (!lookup) {
+ LogOpenSSLError("X509_STORE_add_lookup",
+ Conf_SSLOptions.CRLFile);
+ goto out;
+ }
+
+ if (X509_load_crl_file
+ (lookup, Conf_SSLOptions.CRLFile, X509_FILETYPE_PEM) != 1) {
+ LogOpenSSLError("X509_load_crl_file",
+ Conf_SSLOptions.CRLFile);
+ goto out;
+ }
+ }
+
+ SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
+ Verify_openssl);
+ SSL_CTX_set_verify_depth(ctx, MAX_CERT_CHAIN_LENGTH);
+ ret = true;
+out:
+ if (Conf_SSLOptions.CRLFile)
+ free(Conf_SSLOptions.CRLFile);
+ Conf_SSLOptions.CRLFile = NULL;
+ return ret;
+}
+
+
#endif
static bool
ConnSSL_Init_SSL(CONNECTION *c)
gnutls_certificate_server_set_request(c->ssl_state.gnutls_session,
GNUTLS_CERT_REQUEST);
- Log(LOG_INFO, "Using X509 credentials from slot %zd", x509_cred_idx);
+ LogDebug("Using X509 credentials from slot %zd.", x509_cred_idx);
c->ssl_state.x509_cred_idx = x509_cred_idx;
x509_cred_slot *slot = array_get(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
slot->refcnt++;
bool
-ConnSSL_PrepareConnect(CONNECTION *c, UNUSED CONF_SERVER *s)
+ConnSSL_PrepareConnect(CONNECTION * c, CONF_SERVER * s)
{
bool ret;
#ifdef HAVE_LIBGNUTLS
int err;
+ (void)s;
err = gnutls_init(&c->ssl_state.gnutls_session, GNUTLS_CLIENT);
if (err) {
Log(LOG_ERR, "Failed to initialize new SSL session: %s",
gnutls_strerror(err));
return false;
- }
+ }
#endif
ret = ConnSSL_Init_SSL(c);
if (!ret)
return false;
Conn_OPTION_ADD(c, CONN_SSL_CONNECT);
+
#ifdef HAVE_LIBSSL
assert(c->ssl_state.ssl);
- SSL_set_verify(c->ssl_state.ssl, SSL_VERIFY_NONE, NULL);
+
+ X509_VERIFY_PARAM *param = SSL_get0_param(c->ssl_state.ssl);
+ X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
+ int err = X509_VERIFY_PARAM_set1_host(param, s->host, 0);
+ if (err != 1) {
+ Log(LOG_ERR,
+ "Cannot set up hostname verification for '%s': %u",
+ s->host, err);
+ return false;
+ }
+
+ if (s->SSLVerify)
+ SSL_set_verify(c->ssl_state.ssl, SSL_VERIFY_PEER,
+ Verify_openssl);
+ else
+ SSL_set_verify(c->ssl_state.ssl, SSL_VERIFY_NONE, NULL);
#endif
+
return true;
}
"SSL error, client disconnected [in %s()]!",
fname);
break;
- case -1: /* low level socket I/O error, check errno */
- Log(LOG_ERR, "SSL error: %s [in %s()]!",
- strerror(real_errno), fname);
+ case -1:
+ /* Low level socket I/O error, check errno. But
+ * we don't need to log this here, the generic
+ * connection layer will take care of it. */
+ LogDebug("SSL error: %s [in %s()]!",
+ strerror(real_errno), fname);
}
}
break;
default:
assert(code < 0);
if (gnutls_error_is_fatal(code)) {
- Log(LOG_ERR, "SSL error: %s [%s].",
- gnutls_strerror(code), fname);
+ /* We don't need to log this here, the generic
+ * connection layer will take care of it. */
+ LogDebug("SSL error: %s [%s].",
+ gnutls_strerror(code), fname);
ConnSSL_Free(c);
return -1;
}
}
+#ifdef HAVE_LIBGNUTLS
+static void *
+LogMalloc(size_t s)
+{
+ void *mem = malloc(s);
+ if (!mem)
+ Log(LOG_ERR, "Out of memory: Could not allocate %lu byte",
+ (unsigned long)s);
+ return mem;
+}
+
+
+static void
+LogGnuTLS_CertInfo(int level, gnutls_x509_crt_t cert, const char *msg)
+{
+ char *dn, *issuer_dn;
+ size_t size = 0;
+ int err = gnutls_x509_crt_get_dn(cert, NULL, &size);
+ if (size == 0) {
+ Log(LOG_ERR, "gnutls_x509_crt_get_dn: size == 0");
+ return;
+ }
+ if (err && err != GNUTLS_E_SHORT_MEMORY_BUFFER)
+ goto err_crt_get;
+ dn = LogMalloc(size);
+ if (!dn)
+ return;
+ err = gnutls_x509_crt_get_dn(cert, dn, &size);
+ if (err)
+ goto err_crt_get;
+ gnutls_x509_crt_get_issuer_dn(cert, NULL, &size);
+ assert(size);
+ issuer_dn = LogMalloc(size);
+ if (!issuer_dn) {
+ Log(level, "%s: Distinguished Name \"%s\".", msg, dn);
+ free(dn);
+ return;
+ }
+ gnutls_x509_crt_get_issuer_dn(cert, issuer_dn, &size);
+ Log(level, "%s: Distinguished Name \"%s\", Issuer \"%s\".", msg, dn,
+ issuer_dn);
+ free(dn);
+ free(issuer_dn);
+ return;
+
+ err_crt_get:
+ Log(LOG_ERR, "gnutls_x509_crt_get_dn: %s", gnutls_strerror(err));
+ return;
+}
+#endif
+
+
static void
-ConnSSL_LogCertInfo( CONNECTION *c )
+ConnSSL_LogCertInfo( CONNECTION * c, bool connect)
{
+ bool cert_seen = false, cert_ok = false;
+ char msg[128];
#ifdef HAVE_LIBSSL
+ const char *comp_alg = "no compression";
+ const void *comp;
+ X509 *peer_cert = NULL;
SSL *ssl = c->ssl_state.ssl;
assert(ssl);
- Log(LOG_INFO, "Connection %d: initialized %s using cipher %s.",
- c->sock, SSL_get_version(ssl), SSL_get_cipher(ssl));
+ comp = SSL_get_current_compression(ssl);
+ if (comp)
+ comp_alg = SSL_COMP_get_name(comp);
+ Log(LOG_INFO, "Connection %d: initialized %s using cipher %s, %s.",
+ c->sock, SSL_get_version(ssl), SSL_get_cipher(ssl), comp_alg);
+ peer_cert = SSL_get_peer_certificate(ssl);
+ if (peer_cert) {
+ cert_seen = true;
+
+ if (connect) {
+ /* Outgoing connection. Verify the remote server! */
+ int err = SSL_get_verify_result(ssl);
+ if (err == X509_V_OK) {
+ const char *peername = SSL_get0_peername(ssl);
+ if (peername != NULL)
+ cert_ok = true;
+ LogDebug("X509_V_OK, peername = '%s'", peername);
+ } else
+ Log(LOG_WARNING, "Certificate validation failed: %s!",
+ X509_verify_cert_error_string(err));
+
+ snprintf(msg, sizeof(msg), "Got %svalid server certificate",
+ cert_ok ? "" : "in");
+ LogOpenSSL_CertInfo(LOG_INFO, peer_cert, msg);
+ } else {
+ /* Incoming connection.
+ * Accept all certificates, don't depend on their
+ * validity: for example, we don't know the hostname
+ * to check, because we not yet even know if this is a
+ * server connection at all and if so, which one, so we
+ * don't know a host name to look for. On the other
+ * hand we want client certificates, for example for
+ * "CertFP" authentication with services ... */
+ LogOpenSSL_CertInfo(LOG_INFO, peer_cert,
+ "Got unchecked peer certificate");
+ }
+
+ X509_free(peer_cert);
+ }
#endif
#ifdef HAVE_LIBGNUTLS
+ unsigned int status;
+ gnutls_credentials_type_t cred;
gnutls_session_t sess = c->ssl_state.gnutls_session;
gnutls_cipher_algorithm_t cipher = gnutls_cipher_get(sess);
gnutls_protocol_get_name(gnutls_protocol_get_version(sess)),
gnutls_cipher_get_name(cipher),
gnutls_mac_get_name(gnutls_mac_get(sess)));
+ cred = gnutls_auth_get_type(c->ssl_state.gnutls_session);
+ if (cred == GNUTLS_CRD_CERTIFICATE) {
+ cert_seen = true;
+
+ gnutls_x509_crt_t cert;
+ unsigned cert_list_size;
+ const gnutls_datum_t *cert_list =
+ gnutls_certificate_get_peers(sess, &cert_list_size);
+ if (!cert_list || cert_list_size == 0) {
+ Log(LOG_ERR, "No certificates found");
+ goto done_cn_validation;
+ }
+ int err = gnutls_x509_crt_init(&cert);
+ if (err < 0) {
+ Log(LOG_ERR,
+ "Failed to initialize x509 certificate: %s",
+ gnutls_strerror(err));
+ goto done_cn_validation;
+ }
+ err = gnutls_x509_crt_import(cert, cert_list,
+ GNUTLS_X509_FMT_DER);
+ if (err < 0) {
+ Log(LOG_ERR, "Failed to parse the certificate: %s",
+ gnutls_strerror(err));
+ goto done_cn_validation;
+ }
+
+ if (connect) {
+ int verify =
+ gnutls_certificate_verify_peers2(c->
+ ssl_state.gnutls_session,
+ &status);
+ if (verify < 0) {
+ Log(LOG_ERR,
+ "gnutls_certificate_verify_peers2 failed: %s",
+ gnutls_strerror(verify));
+ goto done_cn_validation;
+ } else if (status) {
+ gnutls_datum_t out;
+
+ if (gnutls_certificate_verification_status_print
+ (status, gnutls_certificate_type_get(sess), &out,
+ 0) == GNUTLS_E_SUCCESS) {
+ Log(LOG_ERR,
+ "Certificate validation failed: %s",
+ out.data);
+ gnutls_free(out.data);
+ }
+ }
+
+ err = gnutls_x509_crt_check_hostname(cert, c->host);
+ if (err == 0)
+ Log(LOG_ERR,
+ "Failed to verify the hostname, expected \"%s\"",
+ c->host);
+ else
+ cert_ok = verify == 0 && status == 0;
+
+ snprintf(msg, sizeof(msg), "Got %svalid server certificate",
+ cert_ok ? "" : "in");
+ LogGnuTLS_CertInfo(LOG_INFO, cert, msg);
+ } else {
+ /* Incoming connection. Please see comments for OpenSSL! */
+ LogGnuTLS_CertInfo(LOG_INFO, cert,
+ "Got unchecked peer certificate");
+ }
+
+ gnutls_x509_crt_deinit(cert);
+done_cn_validation:
+ ;
+ }
#endif
+ /*
+ * can be used later to check if connection was authenticated, e.g.
+ * if inbound connection tries to register itself as server.
+ * Could also restrict /OPER to authenticated connections, etc.
+ */
+ if (cert_ok)
+ Conn_OPTION_ADD(c, CONN_SSL_PEERCERT_OK);
+ if (!cert_seen)
+ Log(LOG_INFO, "Peer did not present a certificate.");
}
(void)ConnSSL_InitCertFp(c);
Conn_OPTION_DEL(c, (CONN_SSL_WANT_WRITE|CONN_SSL_WANT_READ|CONN_SSL_CONNECT));
- ConnSSL_LogCertInfo(c);
+ ConnSSL_LogCertInfo(c, connect);
Conn_StartLogin(CONNECTION2ID(c));
return 1;