c260e0
From 355f7594877a62f9aa657e8a72d3f92b3c887d73 Mon Sep 17 00:00:00 2001
c260e0
From: Kamil Dudka <kdudka@redhat.com>
c260e0
Date: Thu, 17 Apr 2014 13:12:59 +0200
c260e0
Subject: [PATCH 1/4] nss: split Curl_nss_connect() into 4 functions
c260e0
c260e0
Upstream-commit: a43bba3a34ed8912c4ca10f213590d1998ba0d29
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/nss.c | 134 +++++++++++++++++++++++++++++++++++++++++++-------------------
c260e0
 1 file changed, 94 insertions(+), 40 deletions(-)
c260e0
c260e0
diff --git a/lib/nss.c b/lib/nss.c
c260e0
index 1381dc4..4d57a24 100644
c260e0
--- a/lib/nss.c
c260e0
+++ b/lib/nss.c
c260e0
@@ -1216,9 +1216,62 @@ static CURLcode nss_init_sslver(SSLVersionRange *sslver,
c260e0
   return CURLE_SSL_CONNECT_ERROR;
c260e0
 }
c260e0
 
c260e0
-CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
+static CURLcode nss_fail_connect(struct ssl_connect_data *connssl,
c260e0
+                                 struct SessionHandle *data,
c260e0
+                                 CURLcode curlerr)
c260e0
 {
c260e0
+  SSLVersionRange sslver;
c260e0
   PRErrorCode err = 0;
c260e0
+
c260e0
+  /* reset the flag to avoid an infinite loop */
c260e0
+  data->state.ssl_connect_retry = FALSE;
c260e0
+
c260e0
+  if(is_nss_error(curlerr)) {
c260e0
+    /* read NSPR error code */
c260e0
+    err = PR_GetError();
c260e0
+    if(is_cc_error(err))
c260e0
+      curlerr = CURLE_SSL_CERTPROBLEM;
c260e0
+
c260e0
+    /* print the error number and error string */
c260e0
+    infof(data, "NSS error %d (%s)\n", err, nss_error_to_name(err));
c260e0
+
c260e0
+    /* print a human-readable message describing the error if available */
c260e0
+    nss_print_error_message(data, err);
c260e0
+  }
c260e0
+
c260e0
+  /* cleanup on connection failure */
c260e0
+  Curl_llist_destroy(connssl->obj_list, NULL);
c260e0
+  connssl->obj_list = NULL;
c260e0
+
c260e0
+  if((SSL_VersionRangeGet(connssl->handle, &sslver) == SECSuccess)
c260e0
+      && (sslver.min == SSL_LIBRARY_VERSION_3_0)
c260e0
+      && (sslver.max == SSL_LIBRARY_VERSION_TLS_1_0)
c260e0
+      && isTLSIntoleranceError(err)) {
c260e0
+    /* schedule reconnect through Curl_retry_request() */
c260e0
+    data->state.ssl_connect_retry = TRUE;
c260e0
+    infof(data, "Error in TLS handshake, trying SSLv3...\n");
c260e0
+    return CURLE_OK;
c260e0
+  }
c260e0
+
c260e0
+  return curlerr;
c260e0
+}
c260e0
+
c260e0
+/* Switch the SSL socket into non-blocking mode. */
c260e0
+static CURLcode nss_set_nonblock(struct ssl_connect_data *connssl,
c260e0
+                                 struct SessionHandle *data)
c260e0
+{
c260e0
+  static PRSocketOptionData sock_opt;
c260e0
+  sock_opt.option = PR_SockOpt_Nonblocking;
c260e0
+  sock_opt.value.non_blocking = PR_TRUE;
c260e0
+
c260e0
+  if(PR_SetSocketOption(connssl->handle, &sock_opt) != PR_SUCCESS)
c260e0
+    return nss_fail_connect(connssl, data, CURLE_SSL_CONNECT_ERROR);
c260e0
+
c260e0
+  return CURLE_OK;
c260e0
+}
c260e0
+
c260e0
+static CURLcode nss_setup_connect(struct connectdata *conn, int sockindex)
c260e0
+{
c260e0
   PRFileDesc *model = NULL;
c260e0
   PRBool ssl_no_cache;
c260e0
   PRBool ssl_cbc_random_iv;
c260e0
@@ -1226,9 +1279,6 @@ CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
   curl_socket_t sockfd = conn->sock[sockindex];
c260e0
   struct ssl_connect_data *connssl = &conn->ssl[sockindex];
c260e0
   CURLcode curlerr;
c260e0
-  PRSocketOptionData sock_opt;
c260e0
-  long time_left;
c260e0
-  PRUint32 timeout;
c260e0
 
c260e0
   SSLVersionRange sslver = {
c260e0
     SSL_LIBRARY_VERSION_3_0,      /* min */
c260e0
@@ -1402,16 +1452,32 @@ CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
 
c260e0
   SSL_SetURL(connssl->handle, conn->host.name);
c260e0
 
c260e0
+  return CURLE_OK;
c260e0
+
c260e0
+error:
c260e0
+  if(model)
c260e0
+    PR_Close(model);
c260e0
+
c260e0
+  return nss_fail_connect(connssl, data, curlerr);
c260e0
+}
c260e0
+
c260e0
+static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
c260e0
+{
c260e0
+  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
c260e0
+  struct SessionHandle *data = conn->data;
c260e0
+  CURLcode curlerr = CURLE_SSL_CONNECT_ERROR;
c260e0
+  PRUint32 timeout;
c260e0
+
c260e0
   /* check timeout situation */
c260e0
-  time_left = Curl_timeleft(data, NULL, TRUE);
c260e0
+  const long time_left = Curl_timeleft(data, NULL, TRUE);
c260e0
   if(time_left < 0L) {
c260e0
     failf(data, "timed out before SSL handshake");
c260e0
     curlerr = CURLE_OPERATION_TIMEDOUT;
c260e0
     goto error;
c260e0
   }
c260e0
-  timeout = PR_MillisecondsToInterval((PRUint32) time_left);
c260e0
 
c260e0
   /* Force the handshake now */
c260e0
+  timeout = PR_MillisecondsToInterval((PRUint32) time_left);
c260e0
   if(SSL_ForceHandshakeWithTimeout(connssl->handle, timeout) != SECSuccess) {
c260e0
     if(conn->data->set.ssl.certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
c260e0
       curlerr = CURLE_PEER_FAILED_VERIFICATION;
c260e0
@@ -1420,12 +1486,6 @@ CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
     goto error;
c260e0
   }
c260e0
 
c260e0
-  /* switch the SSL socket into non-blocking mode */
c260e0
-  sock_opt.option = PR_SockOpt_Nonblocking;
c260e0
-  sock_opt.value.non_blocking = PR_TRUE;
c260e0
-  if(PR_SetSocketOption(connssl->handle, &sock_opt) != PR_SUCCESS)
c260e0
-    goto error;
c260e0
-
c260e0
   connssl->state = ssl_connection_complete;
c260e0
   conn->recv[sockindex] = nss_recv;
c260e0
   conn->send[sockindex] = nss_send;
c260e0
@@ -1453,40 +1513,34 @@ CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
 
c260e0
   return CURLE_OK;
c260e0
 
c260e0
-  error:
c260e0
-  /* reset the flag to avoid an infinite loop */
c260e0
-  data->state.ssl_connect_retry = FALSE;
c260e0
+error:
c260e0
+  return nss_fail_connect(connssl, data, curlerr);
c260e0
+}
c260e0
 
c260e0
-  if(is_nss_error(curlerr)) {
c260e0
-    /* read NSPR error code */
c260e0
-    err = PR_GetError();
c260e0
-    if(is_cc_error(err))
c260e0
-      curlerr = CURLE_SSL_CERTPROBLEM;
c260e0
+CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
+{
c260e0
+  struct ssl_connect_data *connssl = &conn->ssl[sockindex];
c260e0
+  struct SessionHandle *data = conn->data;
c260e0
+  CURLcode rv;
c260e0
 
c260e0
-    /* print the error number and error string */
c260e0
-    infof(data, "NSS error %d (%s)\n", err, nss_error_to_name(err));
c260e0
+  rv = nss_setup_connect(conn, sockindex);
c260e0
+  if(rv)
c260e0
+    return rv;
c260e0
 
c260e0
-    /* print a human-readable message describing the error if available */
c260e0
-    nss_print_error_message(data, err);
c260e0
+  rv = nss_do_connect(conn, sockindex);
c260e0
+  switch(rv) {
c260e0
+  case CURLE_OK:
c260e0
+    break;
c260e0
+  default:
c260e0
+    return rv;
c260e0
   }
c260e0
 
c260e0
-  if(model)
c260e0
-    PR_Close(model);
c260e0
-
c260e0
-  /* cleanup on connection failure */
c260e0
-  Curl_llist_destroy(connssl->obj_list, NULL);
c260e0
-  connssl->obj_list = NULL;
c260e0
-
c260e0
-  if((sslver.min == SSL_LIBRARY_VERSION_3_0)
c260e0
-      && (sslver.max == SSL_LIBRARY_VERSION_TLS_1_0)
c260e0
-      && isTLSIntoleranceError(err)) {
c260e0
-    /* schedule reconnect through Curl_retry_request() */
c260e0
-    data->state.ssl_connect_retry = TRUE;
c260e0
-    infof(data, "Error in TLS handshake, trying SSLv3...\n");
c260e0
-    return CURLE_OK;
c260e0
-  }
c260e0
+  /* switch the SSL socket into non-blocking mode */
c260e0
+  rv = nss_set_nonblock(connssl, data);
c260e0
+  if(rv)
c260e0
+    return rv;
c260e0
 
c260e0
-  return curlerr;
c260e0
+  return CURLE_OK;
c260e0
 }
c260e0
 
c260e0
 static ssize_t nss_send(struct connectdata *conn,  /* connection data */
c260e0
-- 
c260e0
2.1.0
c260e0
c260e0
c260e0
From b5132ce96009510656e5f719c8805647c246685b Mon Sep 17 00:00:00 2001
c260e0
From: Kamil Dudka <kdudka@redhat.com>
c260e0
Date: Thu, 17 Apr 2014 13:27:39 +0200
c260e0
Subject: [PATCH 2/4] nss: implement non-blocking SSL handshake
c260e0
c260e0
Upstream-commit: 8868a226cdad66a9a07d6e3f168884817592a1df
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/nss.c     | 57 ++++++++++++++++++++++++++++++++++++++++++++++++---------
c260e0
 lib/nssg.h    |  1 +
c260e0
 lib/urldata.h |  1 +
c260e0
 3 files changed, 50 insertions(+), 9 deletions(-)
c260e0
c260e0
diff --git a/lib/nss.c b/lib/nss.c
c260e0
index 4d57a24..5be1058 100644
c260e0
--- a/lib/nss.c
c260e0
+++ b/lib/nss.c
c260e0
@@ -1479,7 +1479,10 @@ static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
c260e0
   /* Force the handshake now */
c260e0
   timeout = PR_MillisecondsToInterval((PRUint32) time_left);
c260e0
   if(SSL_ForceHandshakeWithTimeout(connssl->handle, timeout) != SECSuccess) {
c260e0
-    if(conn->data->set.ssl.certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
c260e0
+    if(PR_GetError() == PR_WOULD_BLOCK_ERROR)
c260e0
+      /* TODO: propagate the blocking direction from the NSPR layer */
c260e0
+      return CURLE_AGAIN;
c260e0
+    else if(conn->data->set.ssl.certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
c260e0
       curlerr = CURLE_PEER_FAILED_VERIFICATION;
c260e0
     else if(conn->data->set.ssl.certverifyresult!=0)
c260e0
       curlerr = CURLE_SSL_CACERT;
c260e0
@@ -1517,32 +1520,68 @@ error:
c260e0
   return nss_fail_connect(connssl, data, curlerr);
c260e0
 }
c260e0
 
c260e0
-CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
+static CURLcode nss_connect_common(struct connectdata *conn, int sockindex,
c260e0
+                                   bool *done)
c260e0
 {
c260e0
   struct ssl_connect_data *connssl = &conn->ssl[sockindex];
c260e0
   struct SessionHandle *data = conn->data;
c260e0
+  const bool blocking = (done == NULL);
c260e0
   CURLcode rv;
c260e0
 
c260e0
-  rv = nss_setup_connect(conn, sockindex);
c260e0
-  if(rv)
c260e0
-    return rv;
c260e0
+  if(connssl->connecting_state == ssl_connect_1) {
c260e0
+    rv = nss_setup_connect(conn, sockindex);
c260e0
+    if(rv)
c260e0
+      /* we do not expect CURLE_AGAIN from nss_setup_connect() */
c260e0
+      return rv;
c260e0
+
c260e0
+    if(!blocking) {
c260e0
+      /* in non-blocking mode, set NSS non-blocking mode before handshake */
c260e0
+      rv = nss_set_nonblock(connssl, data);
c260e0
+      if(rv)
c260e0
+        return rv;
c260e0
+    }
c260e0
+
c260e0
+    connssl->connecting_state = ssl_connect_2;
c260e0
+  }
c260e0
 
c260e0
   rv = nss_do_connect(conn, sockindex);
c260e0
   switch(rv) {
c260e0
   case CURLE_OK:
c260e0
     break;
c260e0
+  case CURLE_AGAIN:
c260e0
+    if(!blocking)
c260e0
+      /* CURLE_AGAIN in non-blocking mode is not an error */
c260e0
+      return CURLE_OK;
c260e0
+    /* fall through */
c260e0
   default:
c260e0
     return rv;
c260e0
   }
c260e0
 
c260e0
-  /* switch the SSL socket into non-blocking mode */
c260e0
-  rv = nss_set_nonblock(connssl, data);
c260e0
-  if(rv)
c260e0
-    return rv;
c260e0
+  if(blocking) {
c260e0
+    /* in blocking mode, set NSS non-blocking mode _after_ SSL handshake */
c260e0
+    rv = nss_set_nonblock(connssl, data);
c260e0
+    if(rv)
c260e0
+      return rv;
c260e0
+  }
c260e0
+  else
c260e0
+    /* signal completed SSL handshake */
c260e0
+    *done = TRUE;
c260e0
 
c260e0
+  connssl->connecting_state = ssl_connect_done;
c260e0
   return CURLE_OK;
c260e0
 }
c260e0
 
c260e0
+CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
c260e0
+{
c260e0
+  return nss_connect_common(conn, sockindex, /* blocking */ NULL);
c260e0
+}
c260e0
+
c260e0
+CURLcode Curl_nss_connect_nonblocking(struct connectdata *conn,
c260e0
+                                      int sockindex, bool *done)
c260e0
+{
c260e0
+  return nss_connect_common(conn, sockindex, done);
c260e0
+}
c260e0
+
c260e0
 static ssize_t nss_send(struct connectdata *conn,  /* connection data */
c260e0
                         int sockindex,             /* socketindex */
c260e0
                         const void *mem,           /* send this data */
c260e0
diff --git a/lib/nssg.h b/lib/nssg.h
c260e0
index a881a9a..6d9aea6 100644
c260e0
--- a/lib/nssg.h
c260e0
+++ b/lib/nssg.h
c260e0
@@ -64,6 +64,7 @@ void Curl_nss_md5sum(unsigned char *tmp, /* input */
c260e0
 #define curlssl_init Curl_nss_init
c260e0
 #define curlssl_cleanup Curl_nss_cleanup
c260e0
 #define curlssl_connect Curl_nss_connect
c260e0
+#define curlssl_connect_nonblocking Curl_nss_connect_nonblocking
c260e0
 
c260e0
 /* NSS has its own session ID cache */
c260e0
 #define curlssl_session_free(x) Curl_nop_stmt
c260e0
diff --git a/lib/urldata.h b/lib/urldata.h
c260e0
index e5d85ff..c91bcff 100644
c260e0
--- a/lib/urldata.h
c260e0
+++ b/lib/urldata.h
c260e0
@@ -303,6 +303,7 @@ struct ssl_connect_data {
c260e0
   struct SessionHandle *data;
c260e0
   struct curl_llist *obj_list;
c260e0
   PK11GenericObject *obj_clicert;
c260e0
+  ssl_connect_state connecting_state;
c260e0
 #endif /* USE_NSS */
c260e0
 #ifdef USE_QSOSSL
c260e0
   SSLHandle *handle;
c260e0
-- 
c260e0
2.1.0
c260e0
c260e0
c260e0
From 2f1f1b1ca2d9c60c5fca5d73303ae2ec4c3d94b2 Mon Sep 17 00:00:00 2001
c260e0
From: Kamil Dudka <kdudka@redhat.com>
c260e0
Date: Wed, 23 Apr 2014 15:37:26 +0200
c260e0
Subject: [PATCH 3/4] nss: propagate blocking direction from NSPR I/O
c260e0
c260e0
... during the non-blocking SSL handshake
c260e0
c260e0
Upstream-commit: 9c941e92c4bd3d2a5dbe243f7517b6a6029afc6e
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/http.c |   2 +-
c260e0
 lib/nss.c  | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
c260e0
 2 files changed, 104 insertions(+), 6 deletions(-)
c260e0
c260e0
diff --git a/lib/http.c b/lib/http.c
c260e0
index d1b0405..c007226 100644
c260e0
--- a/lib/http.c
c260e0
+++ b/lib/http.c
c260e0
@@ -1351,7 +1351,7 @@ static CURLcode https_connecting(struct connectdata *conn, bool *done)
c260e0
 #endif
c260e0
 
c260e0
 #if defined(USE_SSLEAY) || defined(USE_GNUTLS) || defined(USE_SCHANNEL) || \
c260e0
-    defined(USE_DARWINSSL)
c260e0
+    defined(USE_DARWINSSL) || defined(USE_NSS)
c260e0
 /* This function is for OpenSSL, GnuTLS, darwinssl, and schannel only.
c260e0
    It should be made to query the generic SSL layer instead. */
c260e0
 static int https_getsock(struct connectdata *conn,
c260e0
diff --git a/lib/nss.c b/lib/nss.c
c260e0
index 5be1058..dadeb58 100644
c260e0
--- a/lib/nss.c
c260e0
+++ b/lib/nss.c
c260e0
@@ -179,6 +179,10 @@ static const cipher_s cipherlist[] = {
c260e0
 static const char* pem_library = "libnsspem.so";
c260e0
 SECMODModule* mod = NULL;
c260e0
 
c260e0
+/* NSPR I/O layer we use to detect blocking direction during SSL handshake */
c260e0
+static PRDescIdentity nspr_io_identity = PR_INVALID_IO_LAYER;
c260e0
+static PRIOMethods nspr_io_methods;
c260e0
+
c260e0
 static const char* nss_error_to_name(PRErrorCode code)
c260e0
 {
c260e0
   const char *name = PR_ErrorToName(code);
c260e0
@@ -861,6 +865,60 @@ isTLSIntoleranceError(PRInt32 err)
c260e0
   }
c260e0
 }
c260e0
 
c260e0
+/* update blocking direction in case of PR_WOULD_BLOCK_ERROR */
c260e0
+static void nss_update_connecting_state(ssl_connect_state state, void *secret)
c260e0
+{
c260e0
+  struct ssl_connect_data *connssl = (struct ssl_connect_data *)secret;
c260e0
+  if(PR_GetError() != PR_WOULD_BLOCK_ERROR)
c260e0
+    /* an unrelated error is passing by */
c260e0
+    return;
c260e0
+
c260e0
+  switch(connssl->connecting_state) {
c260e0
+  case ssl_connect_2:
c260e0
+  case ssl_connect_2_reading:
c260e0
+  case ssl_connect_2_writing:
c260e0
+    break;
c260e0
+  default:
c260e0
+    /* we are not called from an SSL handshake */
c260e0
+    return;
c260e0
+  }
c260e0
+
c260e0
+  /* update the state accordingly */
c260e0
+  connssl->connecting_state = state;
c260e0
+}
c260e0
+
c260e0
+/* recv() wrapper we use to detect blocking direction during SSL handshake */
c260e0
+static PRInt32 nspr_io_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
c260e0
+                            PRIntn flags, PRIntervalTime timeout)
c260e0
+{
c260e0
+  const PRRecvFN recv_fn = fd->lower->methods->recv;
c260e0
+  const PRInt32 rv = recv_fn(fd->lower, buf, amount, flags, timeout);
c260e0
+  if(rv < 0)
c260e0
+    /* check for PR_WOULD_BLOCK_ERROR and update blocking direction */
c260e0
+    nss_update_connecting_state(ssl_connect_2_reading, fd->secret);
c260e0
+  return rv;
c260e0
+}
c260e0
+
c260e0
+/* send() wrapper we use to detect blocking direction during SSL handshake */
c260e0
+static PRInt32 nspr_io_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
c260e0
+                            PRIntn flags, PRIntervalTime timeout)
c260e0
+{
c260e0
+  const PRSendFN send_fn = fd->lower->methods->send;
c260e0
+  const PRInt32 rv = send_fn(fd->lower, buf, amount, flags, timeout);
c260e0
+  if(rv < 0)
c260e0
+    /* check for PR_WOULD_BLOCK_ERROR and update blocking direction */
c260e0
+    nss_update_connecting_state(ssl_connect_2_writing, fd->secret);
c260e0
+  return rv;
c260e0
+}
c260e0
+
c260e0
+/* close() wrapper to avoid assertion failure due to fd->secret != NULL */
c260e0
+static PRStatus nspr_io_close(PRFileDesc *fd)
c260e0
+{
c260e0
+  const PRCloseFN close_fn = PR_GetDefaultIOMethods()->close;
c260e0
+  fd->secret = NULL;
c260e0
+  return close_fn(fd);
c260e0
+}
c260e0
+
c260e0
 static CURLcode nss_init_core(struct SessionHandle *data, const char *cert_dir)
c260e0
 {
c260e0
   NSSInitParameters initparams;
c260e0
@@ -925,6 +983,21 @@ static CURLcode nss_init(struct SessionHandle *data)
c260e0
     }
c260e0
   }
c260e0
 
c260e0
+  if(nspr_io_identity == PR_INVALID_IO_LAYER) {
c260e0
+    /* allocate an identity for our own NSPR I/O layer */
c260e0
+    nspr_io_identity = PR_GetUniqueIdentity("libcurl");
c260e0
+    if(nspr_io_identity == PR_INVALID_IO_LAYER)
c260e0
+      return CURLE_OUT_OF_MEMORY;
c260e0
+
c260e0
+    /* the default methods just call down to the lower I/O layer */
c260e0
+    memcpy(&nspr_io_methods, PR_GetDefaultIOMethods(), sizeof nspr_io_methods);
c260e0
+
c260e0
+    /* override certain methods in the table by our wrappers */
c260e0
+    nspr_io_methods.recv  = nspr_io_recv;
c260e0
+    nspr_io_methods.send  = nspr_io_send;
c260e0
+    nspr_io_methods.close = nspr_io_close;
c260e0
+  }
c260e0
+
c260e0
   rv = nss_init_core(data, cert_dir);
c260e0
   if(rv)
c260e0
     return rv;
c260e0
@@ -1273,6 +1346,8 @@ static CURLcode nss_set_nonblock(struct ssl_connect_data *connssl,
c260e0
 static CURLcode nss_setup_connect(struct connectdata *conn, int sockindex)
c260e0
 {
c260e0
   PRFileDesc *model = NULL;
c260e0
+  PRFileDesc *nspr_io = NULL;
c260e0
+  PRFileDesc *nspr_io_stub = NULL;
c260e0
   PRBool ssl_no_cache;
c260e0
   PRBool ssl_cbc_random_iv;
c260e0
   struct SessionHandle *data = conn->data;
c260e0
@@ -1433,11 +1508,34 @@ static CURLcode nss_setup_connect(struct connectdata *conn, int sockindex)
c260e0
     goto error;
c260e0
   }
c260e0
 
c260e0
-  /* Import our model socket  onto the existing file descriptor */
c260e0
-  connssl->handle = PR_ImportTCPSocket(sockfd);
c260e0
-  connssl->handle = SSL_ImportFD(model, connssl->handle);
c260e0
-  if(!connssl->handle)
c260e0
+  /* wrap OS file descriptor by NSPR's file descriptor abstraction */
c260e0
+  nspr_io = PR_ImportTCPSocket(sockfd);
c260e0
+  if(!nspr_io)
c260e0
+    goto error;
c260e0
+
c260e0
+  /* create our own NSPR I/O layer */
c260e0
+  nspr_io_stub = PR_CreateIOLayerStub(nspr_io_identity, &nspr_io_methods);
c260e0
+  if(!nspr_io_stub) {
c260e0
+    PR_Close(nspr_io);
c260e0
     goto error;
c260e0
+  }
c260e0
+
c260e0
+  /* make the per-connection data accessible from NSPR I/O callbacks */
c260e0
+  nspr_io_stub->secret = (void *)connssl;
c260e0
+
c260e0
+  /* push our new layer to the NSPR I/O stack */
c260e0
+  if(PR_PushIOLayer(nspr_io, PR_TOP_IO_LAYER, nspr_io_stub) != PR_SUCCESS) {
c260e0
+    PR_Close(nspr_io);
c260e0
+    PR_Close(nspr_io_stub);
c260e0
+    goto error;
c260e0
+  }
c260e0
+
c260e0
+  /* import our model socket onto the current I/O stack */
c260e0
+  connssl->handle = SSL_ImportFD(model, nspr_io);
c260e0
+  if(!connssl->handle) {
c260e0
+    PR_Close(nspr_io);
c260e0
+    goto error;
c260e0
+  }
c260e0
 
c260e0
   PR_Close(model); /* We don't need this any more */
c260e0
   model = NULL;
c260e0
@@ -1480,7 +1578,7 @@ static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
c260e0
   timeout = PR_MillisecondsToInterval((PRUint32) time_left);
c260e0
   if(SSL_ForceHandshakeWithTimeout(connssl->handle, timeout) != SECSuccess) {
c260e0
     if(PR_GetError() == PR_WOULD_BLOCK_ERROR)
c260e0
-      /* TODO: propagate the blocking direction from the NSPR layer */
c260e0
+      /* blocking direction is updated by nss_update_connecting_state() */
c260e0
       return CURLE_AGAIN;
c260e0
     else if(conn->data->set.ssl.certverifyresult == SSL_ERROR_BAD_CERT_DOMAIN)
c260e0
       curlerr = CURLE_PEER_FAILED_VERIFICATION;
c260e0
-- 
c260e0
2.1.0
c260e0
c260e0
c260e0
From 813f39b34ecc2634aa8ff332709ddde9235f6891 Mon Sep 17 00:00:00 2001
c260e0
From: Kamil Dudka <kdudka@redhat.com>
c260e0
Date: Mon, 20 Oct 2014 18:18:57 +0200
c260e0
Subject: [PATCH 4/4] nss: reset SSL handshake state machine
c260e0
c260e0
... when the handshake succeeds
c260e0
c260e0
This fixes a connection failure when FTPS handle is reused.
c260e0
c260e0
Upstream-commit: 0aecdf682895b42c25b232e91529f48bdf7738b3
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/nss.c | 17 +++++++++--------
c260e0
 1 file changed, 9 insertions(+), 8 deletions(-)
c260e0
c260e0
diff --git a/lib/nss.c b/lib/nss.c
c260e0
index dadeb58..36fa097 100644
c260e0
--- a/lib/nss.c
c260e0
+++ b/lib/nss.c
c260e0
@@ -1360,9 +1360,6 @@ static CURLcode nss_setup_connect(struct connectdata *conn, int sockindex)
c260e0
     SSL_LIBRARY_VERSION_TLS_1_0   /* max */
c260e0
   };
c260e0
 
c260e0
-  if(connssl->state == ssl_connection_complete)
c260e0
-    return CURLE_OK;
c260e0
-
c260e0
   connssl->data = data;
c260e0
 
c260e0
   /* list of all NSS objects we need to destroy in Curl_nss_close() */
c260e0
@@ -1587,10 +1584,6 @@ static CURLcode nss_do_connect(struct connectdata *conn, int sockindex)
c260e0
     goto error;
c260e0
   }
c260e0
 
c260e0
-  connssl->state = ssl_connection_complete;
c260e0
-  conn->recv[sockindex] = nss_recv;
c260e0
-  conn->send[sockindex] = nss_send;
c260e0
-
c260e0
   display_conn_info(conn, connssl->handle);
c260e0
 
c260e0
   if(data->set.str[STRING_SSL_ISSUERCERT]) {
c260e0
@@ -1626,6 +1619,9 @@ static CURLcode nss_connect_common(struct connectdata *conn, int sockindex,
c260e0
   const bool blocking = (done == NULL);
c260e0
   CURLcode rv;
c260e0
 
c260e0
+  if(connssl->state == ssl_connection_complete)
c260e0
+    return CURLE_OK;
c260e0
+
c260e0
   if(connssl->connecting_state == ssl_connect_1) {
c260e0
     rv = nss_setup_connect(conn, sockindex);
c260e0
     if(rv)
c260e0
@@ -1665,7 +1661,12 @@ static CURLcode nss_connect_common(struct connectdata *conn, int sockindex,
c260e0
     /* signal completed SSL handshake */
c260e0
     *done = TRUE;
c260e0
 
c260e0
-  connssl->connecting_state = ssl_connect_done;
c260e0
+  connssl->state = ssl_connection_complete;
c260e0
+  conn->recv[sockindex] = nss_recv;
c260e0
+  conn->send[sockindex] = nss_send;
c260e0
+
c260e0
+  /* ssl_connect_done is never used outside, go back to the initial state */
c260e0
+  connssl->connecting_state = ssl_connect_1;
c260e0
   return CURLE_OK;
c260e0
 }
c260e0
 
c260e0
-- 
c260e0
2.1.0
c260e0