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.
35 #include "vconn-ssl.h"
41 #include <netinet/tcp.h>
42 #include <openssl/err.h>
43 #include <openssl/ssl.h>
47 #include "socket-util.h"
51 #include "poll-loop.h"
52 #include "ofp-print.h"
53 #include "socket-util.h"
57 #define THIS_MODULE VLM_vconn_ssl
76 enum session_type type;
81 struct poll_waiter *tx_waiter;
83 /* rx_want and tx_want record the result of the last call to SSL_read()
84 * and SSL_write(), respectively:
86 * - If the call reported that data needed to be read from the file
87 * descriptor, the corresponding member is set to SSL_READING.
89 * - If the call reported that data needed to be written to the file
90 * descriptor, the corresponding member is set to SSL_WRITING.
92 * - Otherwise, the member is set to SSL_NOTHING, indicating that the
93 * call completed successfully (or with an error) and that there is no
96 * These are needed because there is no way to ask OpenSSL what a data read
97 * or write would require without giving it a buffer to receive into or
98 * data to send, respectively. (Note that the SSL_want() status is
99 * overwritten by each SSL_read() or SSL_write() call, so we can't rely on
102 * A single call to SSL_read() or SSL_write() can perform both reading
103 * and writing and thus invalidate not one of these values but actually
104 * both. Consider this situation, for example:
106 * - SSL_write() blocks on a read, so tx_want gets SSL_READING.
108 * - SSL_read() laters succeeds reading from 'fd' and clears out the
109 * whole receive buffer, so rx_want gets SSL_READING.
111 * - Client calls vconn_wait(WAIT_RECV) and vconn_wait(WAIT_SEND) and
114 * - Now we're stuck blocking until the peer sends us data, even though
115 * SSL_write() could now succeed, which could easily be a deadlock
118 * On the other hand, we can't reset both tx_want and rx_want on every call
119 * to SSL_read() or SSL_write(), because that would produce livelock,
120 * e.g. in this situation:
122 * - SSL_write() blocks, so tx_want gets SSL_READING or SSL_WRITING.
124 * - SSL_read() blocks, so rx_want gets SSL_READING or SSL_WRITING,
125 * but tx_want gets reset to SSL_NOTHING.
127 * - Client calls vconn_wait(WAIT_RECV) and vconn_wait(WAIT_SEND) and
130 * - Client wakes up immediately since SSL_NOTHING in tx_want indicates
131 * that no blocking is necessary.
133 * The solution we adopt here is to set tx_want to SSL_NOTHING after
134 * calling SSL_read() only if the SSL state of the connection changed,
135 * which indicates that an SSL-level renegotiation made some progress, and
136 * similarly for rx_want and SSL_write(). This prevents both the
137 * deadlock and livelock situations above.
139 int rx_want, tx_want;
142 /* SSL context created by ssl_init(). */
145 /* Required configuration. */
146 static bool has_private_key, has_certificate, has_ca_cert;
148 static int ssl_init(void);
149 static int do_ssl_init(void);
150 static bool ssl_wants_io(int ssl_error);
151 static void ssl_close(struct vconn *);
152 static int interpret_ssl_error(const char *function, int ret, int error,
154 static void ssl_tx_poll_callback(int fd, short int revents, void *vconn_);
155 static DH *tmp_dh_callback(SSL *ssl, int is_export UNUSED, int keylength);
158 want_to_poll_events(int want)
176 new_ssl_vconn(const char *name, int fd, enum session_type type,
177 enum ssl_state state, const struct sockaddr_in *sin,
178 struct vconn **vconnp)
180 struct ssl_vconn *sslv;
185 /* Check for all the needful configuration. */
186 if (!has_private_key) {
187 VLOG_ERR("Private key must be configured to use SSL");
190 if (!has_certificate) {
191 VLOG_ERR("Certificate must be configured to use SSL");
195 VLOG_ERR("CA certificate must be configured to use SSL");
198 if (!SSL_CTX_check_private_key(ctx)) {
199 VLOG_ERR("Private key does not match certificate public key");
204 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
206 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
211 /* Create and configure OpenSSL stream. */
214 VLOG_ERR("SSL_new: %s", ERR_error_string(ERR_get_error(), NULL));
218 if (SSL_set_fd(ssl, fd) == 0) {
219 VLOG_ERR("SSL_set_fd: %s", ERR_error_string(ERR_get_error(), NULL));
223 /* Create and return the ssl_vconn. */
224 sslv = xmalloc(sizeof *sslv);
225 sslv->vconn.class = &ssl_vconn_class;
226 sslv->vconn.connect_status = EAGAIN;
227 sslv->vconn.ip = sin->sin_addr.s_addr;
234 sslv->tx_waiter = NULL;
235 sslv->rx_want = sslv->tx_want = SSL_NOTHING;
236 *vconnp = &sslv->vconn;
247 static struct ssl_vconn *
248 ssl_vconn_cast(struct vconn *vconn)
250 assert(vconn->class == &ssl_vconn_class);
251 return CONTAINER_OF(vconn, struct ssl_vconn, vconn);
255 ssl_open(const char *name, char *suffix, struct vconn **vconnp)
257 char *save_ptr, *host_name, *port_string;
258 struct sockaddr_in sin;
267 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
268 * can cause segfaults here:
269 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
270 * Using "::" instead of the obvious ":" works around it. */
271 host_name = strtok_r(suffix, "::", &save_ptr);
272 port_string = strtok_r(NULL, "::", &save_ptr);
274 error(0, "%s: bad peer name format", name);
278 memset(&sin, 0, sizeof sin);
279 sin.sin_family = AF_INET;
280 if (lookup_ip(host_name, &sin.sin_addr)) {
283 sin.sin_port = htons(port_string && *port_string ? atoi(port_string)
287 fd = socket(AF_INET, SOCK_STREAM, 0);
289 VLOG_ERR("%s: socket: %s", name, strerror(errno));
292 retval = set_nonblocking(fd);
298 /* Connect socket. */
299 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
301 if (errno == EINPROGRESS) {
302 return new_ssl_vconn(name, fd, CLIENT, STATE_TCP_CONNECTING,
306 VLOG_ERR("%s: connect: %s", name, strerror(error));
311 return new_ssl_vconn(name, fd, CLIENT, STATE_SSL_CONNECTING,
317 ssl_connect(struct vconn *vconn)
319 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
322 switch (sslv->state) {
323 case STATE_TCP_CONNECTING:
324 retval = check_connection_completion(sslv->fd);
328 sslv->state = STATE_SSL_CONNECTING;
331 case STATE_SSL_CONNECTING:
332 retval = (sslv->type == CLIENT
333 ? SSL_connect(sslv->ssl) : SSL_accept(sslv->ssl));
335 int error = SSL_get_error(sslv->ssl, retval);
336 if (retval < 0 && ssl_wants_io(error)) {
340 interpret_ssl_error((sslv->type == CLIENT ? "SSL_connect"
341 : "SSL_accept"), retval, error, &unused);
342 shutdown(sslv->fd, SHUT_RDWR);
354 ssl_close(struct vconn *vconn)
356 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
357 poll_cancel(sslv->tx_waiter);
364 interpret_ssl_error(const char *function, int ret, int error,
371 VLOG_ERR("%s: unexpected SSL_ERROR_NONE", function);
374 case SSL_ERROR_ZERO_RETURN:
375 VLOG_ERR("%s: unexpected SSL_ERROR_ZERO_RETURN", function);
378 case SSL_ERROR_WANT_READ:
382 case SSL_ERROR_WANT_WRITE:
386 case SSL_ERROR_WANT_CONNECT:
387 VLOG_ERR("%s: unexpected SSL_ERROR_WANT_CONNECT", function);
390 case SSL_ERROR_WANT_ACCEPT:
391 VLOG_ERR("%s: unexpected SSL_ERROR_WANT_ACCEPT", function);
394 case SSL_ERROR_WANT_X509_LOOKUP:
395 VLOG_ERR("%s: unexpected SSL_ERROR_WANT_X509_LOOKUP", function);
398 case SSL_ERROR_SYSCALL: {
399 int queued_error = ERR_get_error();
400 if (queued_error == 0) {
403 VLOG_WARN("%s: system error (%s)", function, strerror(status));
406 VLOG_WARN("%s: unexpected SSL connection close", function);
410 VLOG_DBG("%s: %s", function, ERR_error_string(queued_error, NULL));
415 case SSL_ERROR_SSL: {
416 int queued_error = ERR_get_error();
417 if (queued_error != 0) {
418 VLOG_DBG("%s: %s", function, ERR_error_string(queued_error, NULL));
420 VLOG_ERR("%s: SSL_ERROR_SSL without queued error", function);
426 VLOG_ERR("%s: bad SSL error code %d", function, error);
433 ssl_recv(struct vconn *vconn, struct buffer **bufferp)
435 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
441 if (sslv->rxbuf == NULL) {
442 sslv->rxbuf = buffer_new(1564);
447 if (sizeof(struct ofp_header) > rx->size) {
448 want_bytes = sizeof(struct ofp_header) - rx->size;
450 struct ofp_header *oh = rx->data;
451 size_t length = ntohs(oh->length);
452 if (length < sizeof(struct ofp_header)) {
453 VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
456 want_bytes = length - rx->size;
463 buffer_prealloc_tailroom(rx, want_bytes);
465 /* Behavior of zero-byte SSL_read is poorly defined. */
466 assert(want_bytes > 0);
468 old_state = SSL_get_state(sslv->ssl);
469 ret = SSL_read(sslv->ssl, buffer_tail(rx), want_bytes);
470 if (old_state != SSL_get_state(sslv->ssl)) {
471 sslv->tx_want = SSL_NOTHING;
472 if (sslv->tx_waiter) {
473 poll_cancel(sslv->tx_waiter);
474 ssl_tx_poll_callback(sslv->fd, POLLIN, vconn);
477 sslv->rx_want = SSL_NOTHING;
481 if (ret == want_bytes) {
482 if (rx->size > sizeof(struct ofp_header)) {
492 int error = SSL_get_error(sslv->ssl, ret);
493 if (error == SSL_ERROR_ZERO_RETURN) {
494 /* Connection closed (EOF). */
496 VLOG_WARN("SSL_read: unexpected connection close");
502 return interpret_ssl_error("SSL_read", ret, error, &sslv->rx_want);
508 ssl_clear_txbuf(struct ssl_vconn *sslv)
510 buffer_delete(sslv->txbuf);
512 sslv->tx_waiter = NULL;
516 ssl_register_tx_waiter(struct vconn *vconn)
518 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
519 sslv->tx_waiter = poll_fd_callback(sslv->fd,
520 want_to_poll_events(sslv->tx_want),
521 ssl_tx_poll_callback, vconn);
525 ssl_do_tx(struct vconn *vconn)
527 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
530 int old_state = SSL_get_state(sslv->ssl);
531 int ret = SSL_write(sslv->ssl, sslv->txbuf->data, sslv->txbuf->size);
532 if (old_state != SSL_get_state(sslv->ssl)) {
533 sslv->rx_want = SSL_NOTHING;
535 sslv->tx_want = SSL_NOTHING;
537 buffer_pull(sslv->txbuf, ret);
538 if (sslv->txbuf->size == 0) {
542 int ssl_error = SSL_get_error(sslv->ssl, ret);
543 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
544 VLOG_WARN("SSL_write: connection closed");
547 return interpret_ssl_error("SSL_write", ret, ssl_error,
555 ssl_tx_poll_callback(int fd UNUSED, short int revents UNUSED, void *vconn_)
557 struct vconn *vconn = vconn_;
558 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
559 int error = ssl_do_tx(vconn);
560 if (error != EAGAIN) {
561 ssl_clear_txbuf(sslv);
563 ssl_register_tx_waiter(vconn);
568 ssl_send(struct vconn *vconn, struct buffer *buffer)
570 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
577 sslv->txbuf = buffer;
578 error = ssl_do_tx(vconn);
581 ssl_clear_txbuf(sslv);
584 ssl_register_tx_waiter(vconn);
594 ssl_wait(struct vconn *vconn, enum vconn_wait_type wait)
596 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
600 if (vconn_connect(vconn) != EAGAIN) {
601 poll_immediate_wake();
603 switch (sslv->state) {
604 case STATE_TCP_CONNECTING:
605 poll_fd_wait(sslv->fd, POLLOUT);
608 case STATE_SSL_CONNECTING:
609 /* ssl_connect() called SSL_accept() or SSL_connect(), which
610 * set up the status that we test here. */
611 poll_fd_wait(sslv->fd,
612 want_to_poll_events(SSL_want(sslv->ssl)));
622 if (sslv->rx_want != SSL_NOTHING) {
623 poll_fd_wait(sslv->fd, want_to_poll_events(sslv->rx_want));
625 poll_immediate_wake();
631 /* We have room in our tx queue. */
632 poll_immediate_wake();
634 /* The call to ssl_tx_poll_callback() will wake us up. */
643 struct vconn_class ssl_vconn_class = {
647 .connect = ssl_connect,
661 static struct pssl_vconn *
662 pssl_vconn_cast(struct vconn *vconn)
664 assert(vconn->class == &pssl_vconn_class);
665 return CONTAINER_OF(vconn, struct pssl_vconn, vconn);
669 pssl_open(const char *name, char *suffix, struct vconn **vconnp)
671 struct sockaddr_in sin;
672 struct pssl_vconn *pssl;
675 unsigned int yes = 1;
683 fd = socket(AF_INET, SOCK_STREAM, 0);
686 VLOG_ERR("%s: socket: %s", name, strerror(error));
690 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
692 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
696 memset(&sin, 0, sizeof sin);
697 sin.sin_family = AF_INET;
698 sin.sin_addr.s_addr = htonl(INADDR_ANY);
699 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_SSL_PORT);
700 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
703 VLOG_ERR("%s: bind: %s", name, strerror(error));
708 retval = listen(fd, 10);
711 VLOG_ERR("%s: listen: %s", name, strerror(error));
716 retval = set_nonblocking(fd);
722 pssl = xmalloc(sizeof *pssl);
723 pssl->vconn.class = &pssl_vconn_class;
724 pssl->vconn.connect_status = 0;
726 *vconnp = &pssl->vconn;
731 pssl_close(struct vconn *vconn)
733 struct pssl_vconn *pssl = pssl_vconn_cast(vconn);
739 pssl_accept(struct vconn *vconn, struct vconn **new_vconnp)
741 struct pssl_vconn *pssl = pssl_vconn_cast(vconn);
742 struct sockaddr_in sin;
743 socklen_t sin_len = sizeof sin;
748 new_fd = accept(pssl->fd, &sin, &sin_len);
751 if (error != EAGAIN) {
752 VLOG_DBG("accept: %s", strerror(error));
757 error = set_nonblocking(new_fd);
763 sprintf(name, "ssl:"IP_FMT, IP_ARGS(&sin.sin_addr));
764 if (sin.sin_port != htons(OFP_SSL_PORT)) {
765 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin.sin_port));
767 return new_ssl_vconn(name, new_fd, SERVER, STATE_SSL_CONNECTING, &sin,
772 pssl_wait(struct vconn *vconn, enum vconn_wait_type wait)
774 struct pssl_vconn *pssl = pssl_vconn_cast(vconn);
775 assert(wait == WAIT_ACCEPT);
776 poll_fd_wait(pssl->fd, POLLIN);
779 struct vconn_class pssl_vconn_class = {
783 .accept = pssl_accept,
788 * Returns true if OpenSSL error is WANT_READ or WANT_WRITE, indicating that
789 * OpenSSL is requesting that we call it back when the socket is ready for read
790 * or writing, respectively.
793 ssl_wants_io(int ssl_error)
795 return (ssl_error == SSL_ERROR_WANT_WRITE
796 || ssl_error == SSL_ERROR_WANT_READ);
802 static int init_status = -1;
803 if (init_status < 0) {
804 init_status = do_ssl_init();
805 assert(init_status >= 0);
816 SSL_load_error_strings();
818 method = TLSv1_method();
819 if (method == NULL) {
820 VLOG_ERR("TLSv1_method: %s", ERR_error_string(ERR_get_error(), NULL));
824 ctx = SSL_CTX_new(method);
826 VLOG_ERR("SSL_CTX_new: %s", ERR_error_string(ERR_get_error(), NULL));
829 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
830 SSL_CTX_set_tmp_dh_callback(ctx, tmp_dh_callback);
831 SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
832 SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
833 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
840 tmp_dh_callback(SSL *ssl, int is_export UNUSED, int keylength)
845 DH *(*constructor)(void);
848 static struct dh dh_table[] = {
849 {1024, NULL, get_dh1024},
850 {2048, NULL, get_dh2048},
851 {4096, NULL, get_dh4096},
856 for (dh = dh_table; dh < &dh_table[ARRAY_SIZE(dh_table)]; dh++) {
857 if (dh->keylength == keylength) {
859 dh->dh = dh->constructor();
861 fatal(ENOMEM, "out of memory constructing "
862 "Diffie-Hellman parameters");
868 VLOG_ERR("no Diffie-Hellman parameters for key length %d", keylength);
872 /* Returns true if SSL is at least partially configured. */
874 vconn_ssl_is_configured(void)
876 return has_private_key || has_certificate || has_ca_cert;
880 vconn_ssl_set_private_key_file(const char *file_name)
885 if (SSL_CTX_use_PrivateKey_file(ctx, file_name, SSL_FILETYPE_PEM) != 1) {
886 VLOG_ERR("SSL_use_PrivateKey_file: %s",
887 ERR_error_string(ERR_get_error(), NULL));
890 has_private_key = true;
894 vconn_ssl_set_certificate_file(const char *file_name)
899 if (SSL_CTX_use_certificate_chain_file(ctx, file_name) != 1) {
900 VLOG_ERR("SSL_use_certificate_file: %s",
901 ERR_error_string(ERR_get_error(), NULL));
904 has_certificate = true;
908 vconn_ssl_set_ca_cert_file(const char *file_name)
910 STACK_OF(X509_NAME) *ca_list;
916 /* Set up list of CAs that the server will accept from the client. */
917 ca_list = SSL_load_client_CA_file(file_name);
918 if (ca_list == NULL) {
919 VLOG_ERR("SSL_load_client_CA_file: %s",
920 ERR_error_string(ERR_get_error(), NULL));
923 SSL_CTX_set_client_CA_list(ctx, ca_list);
925 /* Set up CAs for OpenSSL to trust in verifying the peer's certificate. */
926 if (SSL_CTX_load_verify_locations(ctx, file_name, NULL) != 1) {
927 VLOG_ERR("SSL_CTX_load_verify_locations: %s",
928 ERR_error_string(ERR_get_error(), NULL));