/*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <config.h>
#include "stream-ssl.h"
#include "dhparams.h"
-#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <openssl/err.h>
+#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <poll.h>
#include <unistd.h>
#include "coverage.h"
#include "dynamic-string.h"
+#include "entropy.h"
#include "leak-checker.h"
#include "ofpbuf.h"
#include "openflow/openflow.h"
VLOG_ERR("CA certificate must be configured to use SSL");
retval = ENOPROTOOPT;
}
- if (!SSL_CTX_check_private_key(ctx)) {
+ if (!retval && !SSL_CTX_check_private_key(ctx)) {
VLOG_ERR("Private key does not match certificate public key: %s",
ERR_error_string(ERR_get_error(), NULL));
retval = ENOPROTOOPT;
* certificate, but that's more trouble than it's worth. These
* connections will succeed the next time they retry, assuming that
* they have a certificate against the correct CA.) */
- VLOG_ERR("rejecting SSL connection during bootstrap race window");
+ VLOG_INFO("rejecting SSL connection during bootstrap race window");
return EPROTO;
} else {
return 0;
ssize_t ret;
/* Behavior of zero-byte SSL_read is poorly defined. */
- assert(n > 0);
+ ovs_assert(n > 0);
old_state = SSL_get_state(sslv->ssl);
ret = SSL_read(sslv->ssl, buffer, n);
return -fd;
}
sprintf(bound_name, "pssl:%"PRIu16":"IP_FMT,
- ntohs(sin.sin_port), IP_ARGS(&sin.sin_addr.s_addr));
+ ntohs(sin.sin_port), IP_ARGS(sin.sin_addr.s_addr));
pssl = xmalloc(sizeof *pssl);
pstream_init(&pssl->pstream, &pssl_pstream_class, bound_name);
return error;
}
- sprintf(name, "ssl:"IP_FMT, IP_ARGS(&sin.sin_addr));
+ sprintf(name, "ssl:"IP_FMT, IP_ARGS(sin.sin_addr.s_addr));
if (sin.sin_port != htons(OFP_SSL_PORT)) {
sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin.sin_port));
}
poll_fd_wait(pssl->fd, POLLIN);
}
+static int
+pssl_set_dscp(struct pstream *pstream, uint8_t dscp)
+{
+ struct pssl_pstream *pssl = pssl_pstream_cast(pstream);
+ return set_dscp(pssl->fd, dscp);
+}
+
const struct pstream_class pssl_pstream_class = {
"pssl",
true,
pssl_close,
pssl_accept,
pssl_wait,
+ pssl_set_dscp,
};
\f
/*
static int init_status = -1;
if (init_status < 0) {
init_status = do_ssl_init();
- assert(init_status >= 0);
+ ovs_assert(init_status >= 0);
}
return init_status;
}
SSL_library_init();
SSL_load_error_strings();
+ if (!RAND_status()) {
+ /* We occasionally see OpenSSL fail to seed its random number generator
+ * in heavily loaded hypervisors. I suspect the following scenario:
+ *
+ * 1. OpenSSL calls read() to get 32 bytes from /dev/urandom.
+ * 2. The kernel generates 10 bytes of randomness and copies it out.
+ * 3. A signal arrives (perhaps SIGALRM).
+ * 4. The kernel interrupts the system call to service the signal.
+ * 5. Userspace gets 10 bytes of entropy.
+ * 6. OpenSSL doesn't read again to get the final 22 bytes. Therefore
+ * OpenSSL doesn't have enough entropy to consider itself
+ * initialized.
+ *
+ * The only part I'm not entirely sure about is #6, because the OpenSSL
+ * code is so hard to read. */
+ uint8_t seed[32];
+ int retval;
+
+ VLOG_WARN("OpenSSL random seeding failed, reseeding ourselves");
+
+ retval = get_entropy(seed, sizeof seed);
+ if (retval) {
+ VLOG_ERR("failed to obtain entropy (%s)",
+ ovs_retval_to_string(retval));
+ return retval > 0 ? retval : ENOPROTOOPT;
+ }
+
+ RAND_seed(seed, sizeof seed);
+ }
+
/* New OpenSSL changed TLSv1_method() to return a "const" pointer, so the
* cast is needed to avoid a warning with those newer versions. */
- method = (SSL_METHOD *) TLSv1_method();
+ method = CONST_CAST(SSL_METHOD *, TLSv1_method());
if (method == NULL) {
VLOG_ERR("TLSv1_method: %s", ERR_error_string(ERR_get_error(), NULL));
return ENOPROTOOPT;