1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
34 #include "vconn-ssl.h"
40 #include <netinet/tcp.h>
41 #include <openssl/err.h>
42 #include <openssl/ssl.h>
46 #include "socket-util.h"
50 #include "poll-loop.h"
51 #include "ofp-print.h"
52 #include "socket-util.h"
56 #define THIS_MODULE VLM_vconn_ssl
75 enum session_type type;
80 struct poll_waiter *tx_waiter;
82 /* rx_want and tx_want record the result of the last call to SSL_read()
83 * and SSL_write(), respectively:
85 * - If the call reported that data needed to be read from the file
86 * descriptor, the corresponding member is set to SSL_READING.
88 * - If the call reported that data needed to be written to the file
89 * descriptor, the corresponding member is set to SSL_WRITING.
91 * - Otherwise, the member is set to SSL_NOTHING, indicating that the
92 * call completed successfully (or with an error) and that there is no
95 * These are needed because there is no way to ask OpenSSL what a data read
96 * or write would require without giving it a buffer to receive into or
97 * data to send, respectively. (Note that the SSL_want() status is
98 * overwritten by each SSL_read() or SSL_write() call, so we can't rely on
101 * A single call to SSL_read() or SSL_write() can perform both reading
102 * and writing and thus invalidate not one of these values but actually
103 * both. Consider this situation, for example:
105 * - SSL_write() blocks on a read, so tx_want gets SSL_READING.
107 * - SSL_read() laters succeeds reading from 'fd' and clears out the
108 * whole receive buffer, so rx_want gets SSL_READING.
110 * - Client calls vconn_wait(WAIT_RECV) and vconn_wait(WAIT_SEND) and
113 * - Now we're stuck blocking until the peer sends us data, even though
114 * SSL_write() could now succeed, which could easily be a deadlock
117 * On the other hand, we can't reset both tx_want and rx_want on every call
118 * to SSL_read() or SSL_write(), because that would produce livelock,
119 * e.g. in this situation:
121 * - SSL_write() blocks, so tx_want gets SSL_READING or SSL_WRITING.
123 * - SSL_read() blocks, so rx_want gets SSL_READING or SSL_WRITING,
124 * but tx_want gets reset to SSL_NOTHING.
126 * - Client calls vconn_wait(WAIT_RECV) and vconn_wait(WAIT_SEND) and
129 * - Client wakes up immediately since SSL_NOTHING in tx_want indicates
130 * that no blocking is necessary.
132 * The solution we adopt here is to set tx_want to SSL_NOTHING after
133 * calling SSL_read() only if the SSL state of the connection changed,
134 * which indicates that an SSL-level renegotiation made some progress, and
135 * similarly for rx_want and SSL_write(). This prevents both the
136 * deadlock and livelock situations above.
138 int rx_want, tx_want;
141 /* SSL context created by ssl_init(). */
144 /* Required configuration. */
145 static bool has_private_key, has_certificate, has_ca_cert;
147 static int ssl_init(void);
148 static int do_ssl_init(void);
149 static bool ssl_wants_io(int ssl_error);
150 static void ssl_close(struct vconn *);
151 static int interpret_ssl_error(const char *function, int ret, int error,
153 static void ssl_tx_poll_callback(int fd, short int revents, void *vconn_);
154 static DH *tmp_dh_callback(SSL *ssl, int is_export UNUSED, int keylength);
157 want_to_poll_events(int want)
175 new_ssl_vconn(const char *name, int fd, enum session_type type,
176 enum ssl_state state, const struct sockaddr_in *sin,
177 struct vconn **vconnp)
179 struct ssl_vconn *sslv;
184 /* Check for all the needful configuration. */
185 if (!has_private_key) {
186 VLOG_ERR("Private key must be configured to use SSL");
189 if (!has_certificate) {
190 VLOG_ERR("Certificate must be configured to use SSL");
194 VLOG_ERR("CA certificate must be configured to use SSL");
197 if (!SSL_CTX_check_private_key(ctx)) {
198 VLOG_ERR("Private key does not match certificate public key");
203 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
205 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
210 /* Create and configure OpenSSL stream. */
213 VLOG_ERR("SSL_new: %s", ERR_error_string(ERR_get_error(), NULL));
217 if (SSL_set_fd(ssl, fd) == 0) {
218 VLOG_ERR("SSL_set_fd: %s", ERR_error_string(ERR_get_error(), NULL));
222 /* Create and return the ssl_vconn. */
223 sslv = xmalloc(sizeof *sslv);
224 sslv->vconn.class = &ssl_vconn_class;
225 sslv->vconn.connect_status = EAGAIN;
226 sslv->vconn.ip = sin->sin_addr.s_addr;
233 sslv->tx_waiter = NULL;
234 sslv->rx_want = sslv->tx_want = SSL_NOTHING;
235 *vconnp = &sslv->vconn;
246 static struct ssl_vconn *
247 ssl_vconn_cast(struct vconn *vconn)
249 assert(vconn->class == &ssl_vconn_class);
250 return CONTAINER_OF(vconn, struct ssl_vconn, vconn);
254 ssl_open(const char *name, char *suffix, struct vconn **vconnp)
256 char *save_ptr, *host_name, *port_string;
257 struct sockaddr_in sin;
266 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
267 * can cause segfaults here:
268 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
269 * Using "::" instead of the obvious ":" works around it. */
270 host_name = strtok_r(suffix, "::", &save_ptr);
271 port_string = strtok_r(NULL, "::", &save_ptr);
273 error(0, "%s: bad peer name format", name);
277 memset(&sin, 0, sizeof sin);
278 sin.sin_family = AF_INET;
279 if (lookup_ip(host_name, &sin.sin_addr)) {
282 sin.sin_port = htons(port_string && *port_string ? atoi(port_string)
286 fd = socket(AF_INET, SOCK_STREAM, 0);
288 VLOG_ERR("%s: socket: %s", name, strerror(errno));
291 retval = set_nonblocking(fd);
297 /* Connect socket. */
298 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
300 if (errno == EINPROGRESS) {
301 return new_ssl_vconn(name, fd, CLIENT, STATE_TCP_CONNECTING,
305 VLOG_ERR("%s: connect: %s", name, strerror(error));
310 return new_ssl_vconn(name, fd, CLIENT, STATE_SSL_CONNECTING,
316 ssl_connect(struct vconn *vconn)
318 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
321 switch (sslv->state) {
322 case STATE_TCP_CONNECTING:
323 retval = check_connection_completion(sslv->fd);
327 sslv->state = STATE_SSL_CONNECTING;
330 case STATE_SSL_CONNECTING:
331 retval = (sslv->type == CLIENT
332 ? SSL_connect(sslv->ssl) : SSL_accept(sslv->ssl));
334 int error = SSL_get_error(sslv->ssl, retval);
335 if (retval < 0 && ssl_wants_io(error)) {
339 interpret_ssl_error((sslv->type == CLIENT ? "SSL_connect"
340 : "SSL_accept"), retval, error, &unused);
341 shutdown(sslv->fd, SHUT_RDWR);
353 ssl_close(struct vconn *vconn)
355 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
356 poll_cancel(sslv->tx_waiter);
363 interpret_ssl_error(const char *function, int ret, int error,
370 VLOG_ERR("%s: unexpected SSL_ERROR_NONE", function);
373 case SSL_ERROR_ZERO_RETURN:
374 VLOG_ERR("%s: unexpected SSL_ERROR_ZERO_RETURN", function);
377 case SSL_ERROR_WANT_READ:
381 case SSL_ERROR_WANT_WRITE:
385 case SSL_ERROR_WANT_CONNECT:
386 VLOG_ERR("%s: unexpected SSL_ERROR_WANT_CONNECT", function);
389 case SSL_ERROR_WANT_ACCEPT:
390 VLOG_ERR("%s: unexpected SSL_ERROR_WANT_ACCEPT", function);
393 case SSL_ERROR_WANT_X509_LOOKUP:
394 VLOG_ERR("%s: unexpected SSL_ERROR_WANT_X509_LOOKUP", function);
397 case SSL_ERROR_SYSCALL: {
398 int queued_error = ERR_get_error();
399 if (queued_error == 0) {
402 VLOG_WARN("%s: system error (%s)", function, strerror(status));
405 VLOG_WARN("%s: unexpected SSL connection close", function);
409 VLOG_DBG("%s: %s", function, ERR_error_string(queued_error, NULL));
414 case SSL_ERROR_SSL: {
415 int queued_error = ERR_get_error();
416 if (queued_error != 0) {
417 VLOG_DBG("%s: %s", function, ERR_error_string(queued_error, NULL));
419 VLOG_ERR("%s: SSL_ERROR_SSL without queued error", function);
425 VLOG_ERR("%s: bad SSL error code %d", function, error);
432 ssl_recv(struct vconn *vconn, struct buffer **bufferp)
434 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
440 if (sslv->rxbuf == NULL) {
441 sslv->rxbuf = buffer_new(1564);
446 if (sizeof(struct ofp_header) > rx->size) {
447 want_bytes = sizeof(struct ofp_header) - rx->size;
449 struct ofp_header *oh = rx->data;
450 size_t length = ntohs(oh->length);
451 if (length < sizeof(struct ofp_header)) {
452 VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
455 want_bytes = length - rx->size;
462 buffer_prealloc_tailroom(rx, want_bytes);
464 /* Behavior of zero-byte SSL_read is poorly defined. */
465 assert(want_bytes > 0);
467 old_state = SSL_get_state(sslv->ssl);
468 ret = SSL_read(sslv->ssl, buffer_tail(rx), want_bytes);
469 if (old_state != SSL_get_state(sslv->ssl)) {
470 sslv->tx_want = SSL_NOTHING;
471 if (sslv->tx_waiter) {
472 poll_cancel(sslv->tx_waiter);
473 ssl_tx_poll_callback(sslv->fd, POLLIN, vconn);
476 sslv->rx_want = SSL_NOTHING;
480 if (ret == want_bytes) {
481 if (rx->size > sizeof(struct ofp_header)) {
491 int error = SSL_get_error(sslv->ssl, ret);
492 if (error == SSL_ERROR_ZERO_RETURN) {
493 /* Connection closed (EOF). */
495 VLOG_WARN("SSL_read: unexpected connection close");
501 return interpret_ssl_error("SSL_read", ret, error, &sslv->rx_want);
507 ssl_clear_txbuf(struct ssl_vconn *sslv)
509 buffer_delete(sslv->txbuf);
511 sslv->tx_waiter = NULL;
515 ssl_register_tx_waiter(struct vconn *vconn)
517 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
518 sslv->tx_waiter = poll_fd_callback(sslv->fd,
519 want_to_poll_events(sslv->tx_want),
520 ssl_tx_poll_callback, vconn);
524 ssl_do_tx(struct vconn *vconn)
526 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
529 int old_state = SSL_get_state(sslv->ssl);
530 int ret = SSL_write(sslv->ssl, sslv->txbuf->data, sslv->txbuf->size);
531 if (old_state != SSL_get_state(sslv->ssl)) {
532 sslv->rx_want = SSL_NOTHING;
534 sslv->tx_want = SSL_NOTHING;
536 buffer_pull(sslv->txbuf, ret);
537 if (sslv->txbuf->size == 0) {
541 int ssl_error = SSL_get_error(sslv->ssl, ret);
542 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
543 VLOG_WARN("SSL_write: connection closed");
546 return interpret_ssl_error("SSL_write", ret, ssl_error,
554 ssl_tx_poll_callback(int fd UNUSED, short int revents UNUSED, void *vconn_)
556 struct vconn *vconn = vconn_;
557 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
558 int error = ssl_do_tx(vconn);
559 if (error != EAGAIN) {
560 ssl_clear_txbuf(sslv);
562 ssl_register_tx_waiter(vconn);
567 ssl_send(struct vconn *vconn, struct buffer *buffer)
569 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
576 sslv->txbuf = buffer;
577 error = ssl_do_tx(vconn);
580 ssl_clear_txbuf(sslv);
583 ssl_register_tx_waiter(vconn);
593 ssl_wait(struct vconn *vconn, enum vconn_wait_type wait)
595 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
599 if (vconn_connect(vconn) != EAGAIN) {
600 poll_immediate_wake();
602 switch (sslv->state) {
603 case STATE_TCP_CONNECTING:
604 poll_fd_wait(sslv->fd, POLLOUT);
607 case STATE_SSL_CONNECTING:
608 /* ssl_connect() called SSL_accept() or SSL_connect(), which
609 * set up the status that we test here. */
610 poll_fd_wait(sslv->fd,
611 want_to_poll_events(SSL_want(sslv->ssl)));
621 if (sslv->rx_want != SSL_NOTHING) {
622 poll_fd_wait(sslv->fd, want_to_poll_events(sslv->rx_want));
624 poll_immediate_wake();
630 /* We have room in our tx queue. */
631 poll_immediate_wake();
633 /* The call to ssl_tx_poll_callback() will wake us up. */
642 struct vconn_class ssl_vconn_class = {
646 .connect = ssl_connect,
660 static struct pssl_vconn *
661 pssl_vconn_cast(struct vconn *vconn)
663 assert(vconn->class == &pssl_vconn_class);
664 return CONTAINER_OF(vconn, struct pssl_vconn, vconn);
668 pssl_open(const char *name, char *suffix, struct vconn **vconnp)
670 struct sockaddr_in sin;
671 struct pssl_vconn *pssl;
674 unsigned int yes = 1;
682 fd = socket(AF_INET, SOCK_STREAM, 0);
685 VLOG_ERR("%s: socket: %s", name, strerror(error));
689 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
691 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
695 memset(&sin, 0, sizeof sin);
696 sin.sin_family = AF_INET;
697 sin.sin_addr.s_addr = htonl(INADDR_ANY);
698 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_SSL_PORT);
699 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
702 VLOG_ERR("%s: bind: %s", name, strerror(error));
707 retval = listen(fd, 10);
710 VLOG_ERR("%s: listen: %s", name, strerror(error));
715 retval = set_nonblocking(fd);
721 pssl = xmalloc(sizeof *pssl);
722 pssl->vconn.class = &pssl_vconn_class;
723 pssl->vconn.connect_status = 0;
725 *vconnp = &pssl->vconn;
730 pssl_close(struct vconn *vconn)
732 struct pssl_vconn *pssl = pssl_vconn_cast(vconn);
738 pssl_accept(struct vconn *vconn, struct vconn **new_vconnp)
740 struct pssl_vconn *pssl = pssl_vconn_cast(vconn);
741 struct sockaddr_in sin;
742 socklen_t sin_len = sizeof sin;
747 new_fd = accept(pssl->fd, &sin, &sin_len);
750 if (error != EAGAIN) {
751 VLOG_DBG("accept: %s", strerror(error));
756 error = set_nonblocking(new_fd);
762 sprintf(name, "ssl:"IP_FMT, IP_ARGS(&sin.sin_addr));
763 if (sin.sin_port != htons(OFP_SSL_PORT)) {
764 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin.sin_port));
766 return new_ssl_vconn(name, new_fd, SERVER, STATE_SSL_CONNECTING, &sin,
771 pssl_wait(struct vconn *vconn, enum vconn_wait_type wait)
773 struct pssl_vconn *pssl = pssl_vconn_cast(vconn);
774 assert(wait == WAIT_ACCEPT);
775 poll_fd_wait(pssl->fd, POLLIN);
778 struct vconn_class pssl_vconn_class = {
782 .accept = pssl_accept,
787 * Returns true if OpenSSL error is WANT_READ or WANT_WRITE, indicating that
788 * OpenSSL is requesting that we call it back when the socket is ready for read
789 * or writing, respectively.
792 ssl_wants_io(int ssl_error)
794 return (ssl_error == SSL_ERROR_WANT_WRITE
795 || ssl_error == SSL_ERROR_WANT_READ);
801 static int init_status = -1;
802 if (init_status < 0) {
803 init_status = do_ssl_init();
804 assert(init_status >= 0);
815 SSL_load_error_strings();
817 method = TLSv1_method();
818 if (method == NULL) {
819 VLOG_ERR("TLSv1_method: %s", ERR_error_string(ERR_get_error(), NULL));
823 ctx = SSL_CTX_new(method);
825 VLOG_ERR("SSL_CTX_new: %s", ERR_error_string(ERR_get_error(), NULL));
828 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
829 SSL_CTX_set_tmp_dh_callback(ctx, tmp_dh_callback);
830 SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
831 SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
832 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
839 tmp_dh_callback(SSL *ssl, int is_export UNUSED, int keylength)
844 DH *(*constructor)(void);
847 static struct dh dh_table[] = {
848 {1024, NULL, get_dh1024},
849 {2048, NULL, get_dh2048},
850 {4096, NULL, get_dh4096},
855 for (dh = dh_table; dh < &dh_table[ARRAY_SIZE(dh_table)]; dh++) {
856 if (dh->keylength == keylength) {
858 dh->dh = dh->constructor();
860 fatal(ENOMEM, "out of memory constructing "
861 "Diffie-Hellman parameters");
867 VLOG_ERR("no Diffie-Hellman parameters for key length %d", keylength);
871 /* Returns true if SSL is at least partially configured. */
873 vconn_ssl_is_configured(void)
875 return has_private_key || has_certificate || has_ca_cert;
879 vconn_ssl_set_private_key_file(const char *file_name)
884 if (SSL_CTX_use_PrivateKey_file(ctx, file_name, SSL_FILETYPE_PEM) != 1) {
885 VLOG_ERR("SSL_use_PrivateKey_file: %s",
886 ERR_error_string(ERR_get_error(), NULL));
889 has_private_key = true;
893 vconn_ssl_set_certificate_file(const char *file_name)
898 if (SSL_CTX_use_certificate_chain_file(ctx, file_name) != 1) {
899 VLOG_ERR("SSL_use_certificate_file: %s",
900 ERR_error_string(ERR_get_error(), NULL));
903 has_certificate = true;
907 vconn_ssl_set_ca_cert_file(const char *file_name)
909 STACK_OF(X509_NAME) *ca_list;
915 /* Set up list of CAs that the server will accept from the client. */
916 ca_list = SSL_load_client_CA_file(file_name);
917 if (ca_list == NULL) {
918 VLOG_ERR("SSL_load_client_CA_file: %s",
919 ERR_error_string(ERR_get_error(), NULL));
922 SSL_CTX_set_client_CA_list(ctx, ca_list);
924 /* Set up CAs for OpenSSL to trust in verifying the peer's certificate. */
925 if (SSL_CTX_load_verify_locations(ctx, file_name, NULL) != 1) {
926 VLOG_ERR("SSL_CTX_load_verify_locations: %s",
927 ERR_error_string(ERR_get_error(), NULL));