2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "vconn-ssl.h"
25 #include <netinet/tcp.h>
26 #include <openssl/err.h>
27 #include <openssl/ssl.h>
28 #include <openssl/x509v3.h>
30 #include <sys/fcntl.h>
33 #include "dynamic-string.h"
34 #include "leak-checker.h"
36 #include "openflow/openflow.h"
38 #include "poll-loop.h"
39 #include "socket-util.h"
40 #include "socket-util.h"
42 #include "vconn-provider.h"
46 #define THIS_MODULE VLM_vconn_ssl
65 enum session_type type;
70 struct poll_waiter *tx_waiter;
72 /* rx_want and tx_want record the result of the last call to SSL_read()
73 * and SSL_write(), respectively:
75 * - If the call reported that data needed to be read from the file
76 * descriptor, the corresponding member is set to SSL_READING.
78 * - If the call reported that data needed to be written to the file
79 * descriptor, the corresponding member is set to SSL_WRITING.
81 * - Otherwise, the member is set to SSL_NOTHING, indicating that the
82 * call completed successfully (or with an error) and that there is no
85 * These are needed because there is no way to ask OpenSSL what a data read
86 * or write would require without giving it a buffer to receive into or
87 * data to send, respectively. (Note that the SSL_want() status is
88 * overwritten by each SSL_read() or SSL_write() call, so we can't rely on
91 * A single call to SSL_read() or SSL_write() can perform both reading
92 * and writing and thus invalidate not one of these values but actually
93 * both. Consider this situation, for example:
95 * - SSL_write() blocks on a read, so tx_want gets SSL_READING.
97 * - SSL_read() laters succeeds reading from 'fd' and clears out the
98 * whole receive buffer, so rx_want gets SSL_READING.
100 * - Client calls vconn_wait(WAIT_RECV) and vconn_wait(WAIT_SEND) and
103 * - Now we're stuck blocking until the peer sends us data, even though
104 * SSL_write() could now succeed, which could easily be a deadlock
107 * On the other hand, we can't reset both tx_want and rx_want on every call
108 * to SSL_read() or SSL_write(), because that would produce livelock,
109 * e.g. in this situation:
111 * - SSL_write() blocks, so tx_want gets SSL_READING or SSL_WRITING.
113 * - SSL_read() blocks, so rx_want gets SSL_READING or SSL_WRITING,
114 * but tx_want gets reset to SSL_NOTHING.
116 * - Client calls vconn_wait(WAIT_RECV) and vconn_wait(WAIT_SEND) and
119 * - Client wakes up immediately since SSL_NOTHING in tx_want indicates
120 * that no blocking is necessary.
122 * The solution we adopt here is to set tx_want to SSL_NOTHING after
123 * calling SSL_read() only if the SSL state of the connection changed,
124 * which indicates that an SSL-level renegotiation made some progress, and
125 * similarly for rx_want and SSL_write(). This prevents both the
126 * deadlock and livelock situations above.
128 int rx_want, tx_want;
131 /* SSL context created by ssl_init(). */
134 /* Required configuration. */
135 static bool has_private_key, has_certificate, has_ca_cert;
137 /* Ordinarily, we require a CA certificate for the peer to be locally
138 * available. 'has_ca_cert' is true when this is the case, and neither of the
139 * following variables matter.
141 * We can, however, bootstrap the CA certificate from the peer at the beginning
142 * of our first connection then use that certificate on all subsequent
143 * connections, saving it to a file for use in future runs also. In this case,
144 * 'has_ca_cert' is false, 'bootstrap_ca_cert' is true, and 'ca_cert_file'
145 * names the file to be saved. */
146 static bool bootstrap_ca_cert;
147 static char *ca_cert_file;
149 /* Who knows what can trigger various SSL errors, so let's throttle them down
151 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
153 static int ssl_init(void);
154 static int do_ssl_init(void);
155 static bool ssl_wants_io(int ssl_error);
156 static void ssl_close(struct vconn *);
157 static void ssl_clear_txbuf(struct ssl_vconn *);
158 static int interpret_ssl_error(const char *function, int ret, int error,
160 static void ssl_tx_poll_callback(int fd, short int revents, void *vconn_);
161 static DH *tmp_dh_callback(SSL *ssl, int is_export OVS_UNUSED, int keylength);
162 static void log_ca_cert(const char *file_name, X509 *cert);
165 want_to_poll_events(int want)
183 new_ssl_vconn(const char *name, int fd, enum session_type type,
184 enum ssl_state state, const struct sockaddr_in *remote,
185 struct vconn **vconnp)
187 struct sockaddr_in local;
188 socklen_t local_len = sizeof local;
189 struct ssl_vconn *sslv;
194 /* Check for all the needful configuration. */
196 if (!has_private_key) {
197 VLOG_ERR("Private key must be configured to use SSL");
198 retval = ENOPROTOOPT;
200 if (!has_certificate) {
201 VLOG_ERR("Certificate must be configured to use SSL");
202 retval = ENOPROTOOPT;
204 if (!has_ca_cert && !bootstrap_ca_cert) {
205 VLOG_ERR("CA certificate must be configured to use SSL");
206 retval = ENOPROTOOPT;
208 if (!SSL_CTX_check_private_key(ctx)) {
209 VLOG_ERR("Private key does not match certificate public key: %s",
210 ERR_error_string(ERR_get_error(), NULL));
211 retval = ENOPROTOOPT;
217 /* Get the local IP and port information */
218 retval = getsockname(fd, (struct sockaddr *) &local, &local_len);
220 memset(&local, 0, sizeof local);
224 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
226 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
231 /* Create and configure OpenSSL stream. */
234 VLOG_ERR("SSL_new: %s", ERR_error_string(ERR_get_error(), NULL));
235 retval = ENOPROTOOPT;
238 if (SSL_set_fd(ssl, fd) == 0) {
239 VLOG_ERR("SSL_set_fd: %s", ERR_error_string(ERR_get_error(), NULL));
240 retval = ENOPROTOOPT;
243 if (bootstrap_ca_cert && type == CLIENT) {
244 SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
247 /* Create and return the ssl_vconn. */
248 sslv = xmalloc(sizeof *sslv);
249 vconn_init(&sslv->vconn, &ssl_vconn_class, EAGAIN, name);
250 vconn_set_remote_ip(&sslv->vconn, remote->sin_addr.s_addr);
251 vconn_set_remote_port(&sslv->vconn, remote->sin_port);
252 vconn_set_local_ip(&sslv->vconn, local.sin_addr.s_addr);
253 vconn_set_local_port(&sslv->vconn, local.sin_port);
260 sslv->tx_waiter = NULL;
261 sslv->rx_want = sslv->tx_want = SSL_NOTHING;
262 *vconnp = &sslv->vconn;
273 static struct ssl_vconn *
274 ssl_vconn_cast(struct vconn *vconn)
276 vconn_assert_class(vconn, &ssl_vconn_class);
277 return CONTAINER_OF(vconn, struct ssl_vconn, vconn);
281 ssl_open(const char *name, char *suffix, struct vconn **vconnp)
283 struct sockaddr_in sin;
291 error = inet_open_active(SOCK_STREAM, suffix, OFP_SSL_PORT, &sin, &fd);
293 int state = error ? STATE_TCP_CONNECTING : STATE_SSL_CONNECTING;
294 return new_ssl_vconn(name, fd, CLIENT, state, &sin, vconnp);
296 VLOG_ERR("%s: connect: %s", name, strerror(error));
302 do_ca_cert_bootstrap(struct vconn *vconn)
304 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
305 STACK_OF(X509) *chain;
311 chain = SSL_get_peer_cert_chain(sslv->ssl);
312 if (!chain || !sk_X509_num(chain)) {
313 VLOG_ERR("could not bootstrap CA cert: no certificate presented by "
317 ca_cert = sk_X509_value(chain, sk_X509_num(chain) - 1);
319 /* Check that 'ca_cert' is self-signed. Otherwise it is not a CA
320 * certificate and we should not attempt to use it as one. */
321 error = X509_check_issued(ca_cert, ca_cert);
323 VLOG_ERR("could not bootstrap CA cert: obtained certificate is "
324 "not self-signed (%s)",
325 X509_verify_cert_error_string(error));
326 if (sk_X509_num(chain) < 2) {
327 VLOG_ERR("only one certificate was received, so probably the peer "
328 "is not configured to send its CA certificate");
333 fd = open(ca_cert_file, O_CREAT | O_EXCL | O_WRONLY, 0444);
335 VLOG_ERR("could not bootstrap CA cert: creating %s failed: %s",
336 ca_cert_file, strerror(errno));
340 file = fdopen(fd, "w");
343 VLOG_ERR("could not bootstrap CA cert: fdopen failed: %s",
345 unlink(ca_cert_file);
349 if (!PEM_write_X509(file, ca_cert)) {
350 VLOG_ERR("could not bootstrap CA cert: PEM_write_X509 to %s failed: "
351 "%s", ca_cert_file, ERR_error_string(ERR_get_error(), NULL));
353 unlink(ca_cert_file);
359 VLOG_ERR("could not bootstrap CA cert: writing %s failed: %s",
360 ca_cert_file, strerror(error));
361 unlink(ca_cert_file);
365 VLOG_INFO("successfully bootstrapped CA cert to %s", ca_cert_file);
366 log_ca_cert(ca_cert_file, ca_cert);
367 bootstrap_ca_cert = false;
370 /* SSL_CTX_add_client_CA makes a copy of ca_cert's relevant data. */
371 SSL_CTX_add_client_CA(ctx, ca_cert);
373 /* SSL_CTX_use_certificate() takes ownership of the certificate passed in.
374 * 'ca_cert' is owned by sslv->ssl, so we need to duplicate it. */
375 ca_cert = X509_dup(ca_cert);
379 if (SSL_CTX_load_verify_locations(ctx, ca_cert_file, NULL) != 1) {
380 VLOG_ERR("SSL_CTX_load_verify_locations: %s",
381 ERR_error_string(ERR_get_error(), NULL));
384 VLOG_INFO("killing successful connection to retry using CA cert");
389 ssl_connect(struct vconn *vconn)
391 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
394 switch (sslv->state) {
395 case STATE_TCP_CONNECTING:
396 retval = check_connection_completion(sslv->fd);
400 sslv->state = STATE_SSL_CONNECTING;
403 case STATE_SSL_CONNECTING:
404 retval = (sslv->type == CLIENT
405 ? SSL_connect(sslv->ssl) : SSL_accept(sslv->ssl));
407 int error = SSL_get_error(sslv->ssl, retval);
408 if (retval < 0 && ssl_wants_io(error)) {
412 interpret_ssl_error((sslv->type == CLIENT ? "SSL_connect"
413 : "SSL_accept"), retval, error, &unused);
414 shutdown(sslv->fd, SHUT_RDWR);
417 } else if (bootstrap_ca_cert) {
418 return do_ca_cert_bootstrap(vconn);
419 } else if ((SSL_get_verify_mode(sslv->ssl)
420 & (SSL_VERIFY_NONE | SSL_VERIFY_PEER))
421 != SSL_VERIFY_PEER) {
422 /* Two or more SSL connections completed at the same time while we
423 * were in bootstrap mode. Only one of these can finish the
424 * bootstrap successfully. The other one(s) must be rejected
425 * because they were not verified against the bootstrapped CA
426 * certificate. (Alternatively we could verify them against the CA
427 * certificate, but that's more trouble than it's worth. These
428 * connections will succeed the next time they retry, assuming that
429 * they have a certificate against the correct CA.) */
430 VLOG_ERR("rejecting SSL connection during bootstrap race window");
441 ssl_close(struct vconn *vconn)
443 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
444 poll_cancel(sslv->tx_waiter);
445 ssl_clear_txbuf(sslv);
446 ofpbuf_delete(sslv->rxbuf);
453 interpret_ssl_error(const char *function, int ret, int error,
460 VLOG_ERR_RL(&rl, "%s: unexpected SSL_ERROR_NONE", function);
463 case SSL_ERROR_ZERO_RETURN:
464 VLOG_ERR_RL(&rl, "%s: unexpected SSL_ERROR_ZERO_RETURN", function);
467 case SSL_ERROR_WANT_READ:
471 case SSL_ERROR_WANT_WRITE:
475 case SSL_ERROR_WANT_CONNECT:
476 VLOG_ERR_RL(&rl, "%s: unexpected SSL_ERROR_WANT_CONNECT", function);
479 case SSL_ERROR_WANT_ACCEPT:
480 VLOG_ERR_RL(&rl, "%s: unexpected SSL_ERROR_WANT_ACCEPT", function);
483 case SSL_ERROR_WANT_X509_LOOKUP:
484 VLOG_ERR_RL(&rl, "%s: unexpected SSL_ERROR_WANT_X509_LOOKUP",
488 case SSL_ERROR_SYSCALL: {
489 int queued_error = ERR_get_error();
490 if (queued_error == 0) {
493 VLOG_WARN_RL(&rl, "%s: system error (%s)",
494 function, strerror(status));
497 VLOG_WARN_RL(&rl, "%s: unexpected SSL connection close",
502 VLOG_WARN_RL(&rl, "%s: %s",
503 function, ERR_error_string(queued_error, NULL));
508 case SSL_ERROR_SSL: {
509 int queued_error = ERR_get_error();
510 if (queued_error != 0) {
511 VLOG_WARN_RL(&rl, "%s: %s",
512 function, ERR_error_string(queued_error, NULL));
514 VLOG_ERR_RL(&rl, "%s: SSL_ERROR_SSL without queued error",
521 VLOG_ERR_RL(&rl, "%s: bad SSL error code %d", function, error);
528 ssl_recv(struct vconn *vconn, struct ofpbuf **bufferp)
530 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
536 if (sslv->rxbuf == NULL) {
537 sslv->rxbuf = ofpbuf_new(1564);
542 if (sizeof(struct ofp_header) > rx->size) {
543 want_bytes = sizeof(struct ofp_header) - rx->size;
545 struct ofp_header *oh = rx->data;
546 size_t length = ntohs(oh->length);
547 if (length < sizeof(struct ofp_header)) {
548 VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
552 want_bytes = length - rx->size;
559 ofpbuf_prealloc_tailroom(rx, want_bytes);
561 /* Behavior of zero-byte SSL_read is poorly defined. */
562 assert(want_bytes > 0);
564 old_state = SSL_get_state(sslv->ssl);
565 ret = SSL_read(sslv->ssl, ofpbuf_tail(rx), want_bytes);
566 if (old_state != SSL_get_state(sslv->ssl)) {
567 sslv->tx_want = SSL_NOTHING;
568 if (sslv->tx_waiter) {
569 poll_cancel(sslv->tx_waiter);
570 ssl_tx_poll_callback(sslv->fd, POLLIN, vconn);
573 sslv->rx_want = SSL_NOTHING;
577 if (ret == want_bytes) {
578 if (rx->size > sizeof(struct ofp_header)) {
588 int error = SSL_get_error(sslv->ssl, ret);
589 if (error == SSL_ERROR_ZERO_RETURN) {
590 /* Connection closed (EOF). */
592 VLOG_WARN_RL(&rl, "SSL_read: unexpected connection close");
598 return interpret_ssl_error("SSL_read", ret, error, &sslv->rx_want);
604 ssl_clear_txbuf(struct ssl_vconn *sslv)
606 ofpbuf_delete(sslv->txbuf);
608 sslv->tx_waiter = NULL;
612 ssl_register_tx_waiter(struct vconn *vconn)
614 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
615 sslv->tx_waiter = poll_fd_callback(sslv->fd,
616 want_to_poll_events(sslv->tx_want),
617 ssl_tx_poll_callback, vconn);
621 ssl_do_tx(struct vconn *vconn)
623 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
626 int old_state = SSL_get_state(sslv->ssl);
627 int ret = SSL_write(sslv->ssl, sslv->txbuf->data, sslv->txbuf->size);
628 if (old_state != SSL_get_state(sslv->ssl)) {
629 sslv->rx_want = SSL_NOTHING;
631 sslv->tx_want = SSL_NOTHING;
633 ofpbuf_pull(sslv->txbuf, ret);
634 if (sslv->txbuf->size == 0) {
638 int ssl_error = SSL_get_error(sslv->ssl, ret);
639 if (ssl_error == SSL_ERROR_ZERO_RETURN) {
640 VLOG_WARN_RL(&rl, "SSL_write: connection closed");
643 return interpret_ssl_error("SSL_write", ret, ssl_error,
651 ssl_tx_poll_callback(int fd OVS_UNUSED, short int revents OVS_UNUSED,
654 struct vconn *vconn = vconn_;
655 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
656 int error = ssl_do_tx(vconn);
657 if (error != EAGAIN) {
658 ssl_clear_txbuf(sslv);
660 ssl_register_tx_waiter(vconn);
665 ssl_send(struct vconn *vconn, struct ofpbuf *buffer)
667 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
674 sslv->txbuf = buffer;
675 error = ssl_do_tx(vconn);
678 ssl_clear_txbuf(sslv);
681 leak_checker_claim(buffer);
682 ssl_register_tx_waiter(vconn);
692 ssl_wait(struct vconn *vconn, enum vconn_wait_type wait)
694 struct ssl_vconn *sslv = ssl_vconn_cast(vconn);
698 if (vconn_connect(vconn) != EAGAIN) {
699 poll_immediate_wake();
701 switch (sslv->state) {
702 case STATE_TCP_CONNECTING:
703 poll_fd_wait(sslv->fd, POLLOUT);
706 case STATE_SSL_CONNECTING:
707 /* ssl_connect() called SSL_accept() or SSL_connect(), which
708 * set up the status that we test here. */
709 poll_fd_wait(sslv->fd,
710 want_to_poll_events(SSL_want(sslv->ssl)));
720 if (sslv->rx_want != SSL_NOTHING) {
721 poll_fd_wait(sslv->fd, want_to_poll_events(sslv->rx_want));
723 poll_immediate_wake();
729 /* We have room in our tx queue. */
730 poll_immediate_wake();
732 /* The call to ssl_tx_poll_callback() will wake us up. */
741 struct vconn_class ssl_vconn_class = {
744 ssl_close, /* close */
745 ssl_connect, /* connect */
755 struct pvconn pvconn;
759 struct pvconn_class pssl_pvconn_class;
761 static struct pssl_pvconn *
762 pssl_pvconn_cast(struct pvconn *pvconn)
764 pvconn_assert_class(pvconn, &pssl_pvconn_class);
765 return CONTAINER_OF(pvconn, struct pssl_pvconn, pvconn);
769 pssl_open(const char *name, char *suffix, struct pvconn **pvconnp)
771 struct pssl_pvconn *pssl;
780 fd = inet_open_passive(SOCK_STREAM, suffix, OFP_SSL_PORT);
785 pssl = xmalloc(sizeof *pssl);
786 pvconn_init(&pssl->pvconn, &pssl_pvconn_class, name);
788 *pvconnp = &pssl->pvconn;
793 pssl_close(struct pvconn *pvconn)
795 struct pssl_pvconn *pssl = pssl_pvconn_cast(pvconn);
801 pssl_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
803 struct pssl_pvconn *pssl = pssl_pvconn_cast(pvconn);
804 struct sockaddr_in sin;
805 socklen_t sin_len = sizeof sin;
810 new_fd = accept(pssl->fd, &sin, &sin_len);
813 if (error != EAGAIN) {
814 VLOG_DBG_RL(&rl, "accept: %s", strerror(error));
819 error = set_nonblocking(new_fd);
825 sprintf(name, "ssl:"IP_FMT, IP_ARGS(&sin.sin_addr));
826 if (sin.sin_port != htons(OFP_SSL_PORT)) {
827 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin.sin_port));
829 return new_ssl_vconn(name, new_fd, SERVER, STATE_SSL_CONNECTING, &sin,
834 pssl_wait(struct pvconn *pvconn)
836 struct pssl_pvconn *pssl = pssl_pvconn_cast(pvconn);
837 poll_fd_wait(pssl->fd, POLLIN);
840 struct pvconn_class pssl_pvconn_class = {
849 * Returns true if OpenSSL error is WANT_READ or WANT_WRITE, indicating that
850 * OpenSSL is requesting that we call it back when the socket is ready for read
851 * or writing, respectively.
854 ssl_wants_io(int ssl_error)
856 return (ssl_error == SSL_ERROR_WANT_WRITE
857 || ssl_error == SSL_ERROR_WANT_READ);
863 static int init_status = -1;
864 if (init_status < 0) {
865 init_status = do_ssl_init();
866 assert(init_status >= 0);
877 SSL_load_error_strings();
879 method = TLSv1_method();
880 if (method == NULL) {
881 VLOG_ERR("TLSv1_method: %s", ERR_error_string(ERR_get_error(), NULL));
885 ctx = SSL_CTX_new(method);
887 VLOG_ERR("SSL_CTX_new: %s", ERR_error_string(ERR_get_error(), NULL));
890 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
891 SSL_CTX_set_tmp_dh_callback(ctx, tmp_dh_callback);
892 SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
893 SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
894 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
901 tmp_dh_callback(SSL *ssl OVS_UNUSED, int is_export OVS_UNUSED, int keylength)
906 DH *(*constructor)(void);
909 static struct dh dh_table[] = {
910 {1024, NULL, get_dh1024},
911 {2048, NULL, get_dh2048},
912 {4096, NULL, get_dh4096},
917 for (dh = dh_table; dh < &dh_table[ARRAY_SIZE(dh_table)]; dh++) {
918 if (dh->keylength == keylength) {
920 dh->dh = dh->constructor();
922 ovs_fatal(ENOMEM, "out of memory constructing "
923 "Diffie-Hellman parameters");
929 VLOG_ERR_RL(&rl, "no Diffie-Hellman parameters for key length %d",
934 /* Returns true if SSL is at least partially configured. */
936 vconn_ssl_is_configured(void)
938 return has_private_key || has_certificate || has_ca_cert;
942 vconn_ssl_set_private_key_file(const char *file_name)
947 if (SSL_CTX_use_PrivateKey_file(ctx, file_name, SSL_FILETYPE_PEM) != 1) {
948 VLOG_ERR("SSL_use_PrivateKey_file: %s",
949 ERR_error_string(ERR_get_error(), NULL));
952 has_private_key = true;
956 vconn_ssl_set_certificate_file(const char *file_name)
961 if (SSL_CTX_use_certificate_chain_file(ctx, file_name) != 1) {
962 VLOG_ERR("SSL_use_certificate_file: %s",
963 ERR_error_string(ERR_get_error(), NULL));
966 has_certificate = true;
969 /* Reads the X509 certificate or certificates in file 'file_name'. On success,
970 * stores the address of the first element in an array of pointers to
971 * certificates in '*certs' and the number of certificates in the array in
972 * '*n_certs', and returns 0. On failure, stores a null pointer in '*certs', 0
973 * in '*n_certs', and returns a positive errno value.
975 * The caller is responsible for freeing '*certs'. */
977 read_cert_file(const char *file_name, X509 ***certs, size_t *n_certs)
980 size_t allocated_certs = 0;
985 file = fopen(file_name, "r");
987 VLOG_ERR("failed to open %s for reading: %s",
988 file_name, strerror(errno));
996 /* Read certificate from file. */
997 certificate = PEM_read_X509(file, NULL, NULL, NULL);
1001 VLOG_ERR("PEM_read_X509 failed reading %s: %s",
1002 file_name, ERR_error_string(ERR_get_error(), NULL));
1003 for (i = 0; i < *n_certs; i++) {
1004 X509_free((*certs)[i]);
1012 /* Add certificate to array. */
1013 if (*n_certs >= allocated_certs) {
1014 *certs = x2nrealloc(*certs, &allocated_certs, sizeof **certs);
1016 (*certs)[(*n_certs)++] = certificate;
1018 /* Are there additional certificates in the file? */
1021 } while (isspace(c));
1032 /* Sets 'file_name' as the name of a file containing one or more X509
1033 * certificates to send to the peer. Typical use in OpenFlow is to send the CA
1034 * certificate to the peer, which enables a switch to pick up the controller's
1035 * CA certificate on its first connection. */
1037 vconn_ssl_set_peer_ca_cert_file(const char *file_name)
1047 if (!read_cert_file(file_name, &certs, &n_certs)) {
1048 for (i = 0; i < n_certs; i++) {
1049 if (SSL_CTX_add_extra_chain_cert(ctx, certs[i]) != 1) {
1050 VLOG_ERR("SSL_CTX_add_extra_chain_cert: %s",
1051 ERR_error_string(ERR_get_error(), NULL));
1058 /* Logs fingerprint of CA certificate 'cert' obtained from 'file_name'. */
1060 log_ca_cert(const char *file_name, X509 *cert)
1062 unsigned char digest[EVP_MAX_MD_SIZE];
1063 unsigned int n_bytes;
1068 if (!X509_digest(cert, EVP_sha1(), digest, &n_bytes)) {
1069 ds_put_cstr(&fp, "<out of memory>");
1072 for (i = 0; i < n_bytes; i++) {
1074 ds_put_char(&fp, ':');
1076 ds_put_format(&fp, "%02hhx", digest[i]);
1079 subject = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
1080 VLOG_INFO("Trusting CA cert from %s (%s) (fingerprint %s)", file_name,
1081 subject ? subject : "<out of memory>", ds_cstr(&fp));
1086 /* Sets 'file_name' as the name of the file from which to read the CA
1087 * certificate used to verify the peer within SSL connections. If 'bootstrap'
1088 * is false, the file must exist. If 'bootstrap' is false, then the file is
1089 * read if it is exists; if it does not, then it will be created from the CA
1090 * certificate received from the peer on the first SSL connection. */
1092 vconn_ssl_set_ca_cert_file(const char *file_name, bool bootstrap)
1102 if (bootstrap && stat(file_name, &s) && errno == ENOENT) {
1103 bootstrap_ca_cert = true;
1104 ca_cert_file = xstrdup(file_name);
1105 } else if (!read_cert_file(file_name, &certs, &n_certs)) {
1108 /* Set up list of CAs that the server will accept from the client. */
1109 for (i = 0; i < n_certs; i++) {
1110 /* SSL_CTX_add_client_CA makes a copy of the relevant data. */
1111 if (SSL_CTX_add_client_CA(ctx, certs[i]) != 1) {
1112 VLOG_ERR("failed to add client certificate %d from %s: %s",
1114 ERR_error_string(ERR_get_error(), NULL));
1116 log_ca_cert(file_name, certs[i]);
1118 X509_free(certs[i]);
1121 /* Set up CAs for OpenSSL to trust in verifying the peer's
1123 if (SSL_CTX_load_verify_locations(ctx, file_name, NULL) != 1) {
1124 VLOG_ERR("SSL_CTX_load_verify_locations: %s",
1125 ERR_error_string(ERR_get_error(), NULL));