4bf89e
Backported for 5.4.16 from PHP 5.5.23, 5.5.24 and 5.5.25
4bf89e
4bf89e
ext/openssl/xp_ssl.c is in sync with latest 5.5 (.38)
4bf89e
4bf89e
4bf89e
From 9ad97cd48903ea5454853960f2c14de326e0f624 Mon Sep 17 00:00:00 2001
4bf89e
From: Christopher Jones <sixd@php.net>
4bf89e
Date: Wed, 14 Aug 2013 20:36:50 -0700
4bf89e
Subject: [PATCH] Reduce (some) compile noise of 'unused variable' and 'may be
4bf89e
 used uninitialized' warnings.
4bf89e
4bf89e
---
4bf89e
 ext/date/php_date.c                            | 6 +++---
4bf89e
 ext/dba/dba.c                                  | 1 -
4bf89e
 ext/dba/libinifile/inifile.c                   | 2 +-
4bf89e
 ext/dom/xpath.c                                | 2 +-
4bf89e
 ext/gmp/gmp.c                                  | 2 +-
4bf89e
 ext/intl/grapheme/grapheme_util.c              | 2 +-
4bf89e
 ext/intl/resourcebundle/resourcebundle_class.c | 4 ++--
4bf89e
 ext/openssl/openssl.c                          | 2 +-
4bf89e
 ext/openssl/xp_ssl.c                           | 2 +-
4bf89e
 ext/session/session.c                          | 2 +-
4bf89e
 ext/simplexml/simplexml.c                      | 2 +-
4bf89e
 ext/snmp/snmp.c                                | 2 +-
4bf89e
 ext/spl/spl_array.c                            | 2 +-
4bf89e
 ext/spl/spl_dllist.c                           | 2 +-
4bf89e
 ext/standard/array.c                           | 9 ++++-----
4bf89e
 ext/standard/html.c                            | 6 +++---
4bf89e
 ext/standard/string.c                          | 4 ++--
4bf89e
 ext/standard/url_scanner_ex.re                 | 2 +-
4bf89e
 ext/xsl/xsltprocessor.c                        | 2 +-
4bf89e
 ext/zip/php_zip.c                              | 2 +-
4bf89e
 main/php_variables.c                           | 2 +-
4bf89e
 main/rfc1867.c                                 | 2 +-
4bf89e
 sapi/cli/php_cli_server.c                      | 2 +-
4bf89e
 23 files changed, 31 insertions(+), 33 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index e19e8f098d05..6e74d8024f5a 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -472,7 +472,7 @@ static inline int php_openssl_enable_crypto(php_stream *stream,
4bf89e
 		
4bf89e
 		do {
4bf89e
 			struct timeval	cur_time,
4bf89e
-							elapsed_time;
4bf89e
+							elapsed_time = {0};
4bf89e
 			
4bf89e
 			if (sslsock->is_client) {
4bf89e
 				n = SSL_connect(sslsock->ssl_handle);
4bf89e
4bf89e
From 32be79dcfa1bc5af8682d9f512da68c5b3e2cbf3 Mon Sep 17 00:00:00 2001
4bf89e
From: Chris Wright <github@daverandom.com>
4bf89e
Date: Sat, 23 Aug 2014 01:40:19 +0100
4bf89e
Subject: [PATCH] Fix stream_select() issue with OpenSSL buffer
4bf89e
4bf89e
Ensure data from OpenSSL internal buffer has been
4bf89e
transfered to PHP stream buffer before a select()
4bf89e
emulation operation is performed
4bf89e
4bf89e
Addresses bug #65137
4bf89e
https://bugs.php.net/bug.php?id=65137
4bf89e
4bf89e
Conflicts:
4bf89e
	ext/openssl/xp_ssl.c
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c   | 13 +++++++++++++
4bf89e
 main/php_streams.h     |  3 +++
4bf89e
 main/streams/streams.c |  8 ++++----
4bf89e
 3 files changed, 20 insertions(+), 4 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index b7b8690165e1..956ffd0547fe 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -825,6 +825,19 @@ static int php_openssl_sockop_cast(php_stream *stream, int castas, void **ret TS
4bf89e
 
4bf89e
 		case PHP_STREAM_AS_FD_FOR_SELECT:
4bf89e
 			if (ret) {
4bf89e
+				if (sslsock->ssl_active) {
4bf89e
+					/* OpenSSL has an internal buffer which select() cannot see. If we don't
4bf89e
+					   fetch it into the stream's buffer, no activity will be reported on the
4bf89e
+					   stream even though there is data waiting to be read - but we only fetch
4bf89e
+					   the number of bytes OpenSSL has ready to give us since we weren't asked
4bf89e
+					   for any data at this stage. This is only likely to cause issues with
4bf89e
+					   non-blocking streams, but it's harmless to always do it. */
4bf89e
+					int bytes;
4bf89e
+					while ((bytes = SSL_pending(sslsock->ssl_handle)) > 0) {
4bf89e
+						php_stream_fill_read_buffer(stream, (size_t)bytes);
4bf89e
+					}
4bf89e
+				}
4bf89e
+
4bf89e
 				*(int *)ret = sslsock->s.socket;
4bf89e
 			}
4bf89e
 			return SUCCESS;
4bf89e
diff --git a/main/php_streams.h b/main/php_streams.h
4bf89e
index 2e4a3a2a18c1..89b877fdb167 100644
4bf89e
--- a/main/php_streams.h
4bf89e
+++ b/main/php_streams.h
4bf89e
@@ -301,6 +301,9 @@ PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t coun
4bf89e
 #define php_stream_write_string(stream, str)	_php_stream_write(stream, str, strlen(str) TSRMLS_CC)
4bf89e
 #define php_stream_write(stream, buf, count)	_php_stream_write(stream, (buf), (count) TSRMLS_CC)
4bf89e
 
4bf89e
+PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC);
4bf89e
+#define php_stream_fill_read_buffer(stream, size)	_php_stream_fill_read_buffer((stream), (size) TSRMLS_CC)
4bf89e
+
4bf89e
 #ifdef ZTS
4bf89e
 PHPAPI size_t _php_stream_printf(php_stream *stream TSRMLS_DC, const char *fmt, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
4bf89e
 #else
4bf89e
diff --git a/main/streams/streams.c b/main/streams/streams.c
4bf89e
index 3fd4ab37968a..fbcc1ca4e365 100644
4bf89e
--- a/main/streams/streams.c
4bf89e
+++ b/main/streams/streams.c
4bf89e
@@ -573,7 +573,7 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov
4bf89e
 
4bf89e
 /* {{{ generic stream operations */
4bf89e
 
4bf89e
-static void php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC)
4bf89e
+PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC)
4bf89e
 {
4bf89e
 	/* allocate/fill the buffer */
4bf89e
 
4bf89e
@@ -737,7 +737,7 @@ PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t size TSRMLS
4bf89e
 		if (!stream->readfilters.head && (stream->flags & PHP_STREAM_FLAG_NO_BUFFER || stream->chunk_size == 1)) {
4bf89e
 			toread = stream->ops->read(stream, buf, size TSRMLS_CC);
4bf89e
 		} else {
4bf89e
-			php_stream_fill_read_buffer(stream, size TSRMLS_CC);
4bf89e
+			php_stream_fill_read_buffer(stream, size);
4bf89e
 
4bf89e
 			toread = stream->writepos - stream->readpos;
4bf89e
 			if (toread > size) {
4bf89e
@@ -973,7 +973,7 @@ PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
4bf89e
 				}
4bf89e
 			}
4bf89e
 
4bf89e
-			php_stream_fill_read_buffer(stream, toread TSRMLS_CC);
4bf89e
+			php_stream_fill_read_buffer(stream, toread);
4bf89e
 
4bf89e
 			if (stream->writepos - stream->readpos == 0) {
4bf89e
 				break;
4bf89e
@@ -1048,7 +1048,7 @@ PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *re
4bf89e
 
4bf89e
 		to_read_now = MIN(maxlen - buffered_len, stream->chunk_size);
4bf89e
 
4bf89e
-		php_stream_fill_read_buffer(stream, buffered_len + to_read_now TSRMLS_CC);
4bf89e
+		php_stream_fill_read_buffer(stream, buffered_len + to_read_now);
4bf89e
 
4bf89e
 		just_read = STREAM_BUFFERED_AMOUNT(stream) - buffered_len;
4bf89e
 
4bf89e
From f86b2193a483f56b0bd056570a0cdb57ebe66e2f Mon Sep 17 00:00:00 2001
4bf89e
From: Daniel Lowrey <rdlowrey@php.net>
4bf89e
Date: Tue, 9 Sep 2014 07:37:57 -0600
4bf89e
Subject: [PATCH] Bug #67965: Fix blocking behavior in non-blocking crypto
4bf89e
 streams
4bf89e
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 24 +++++++++++++-----------
4bf89e
 1 file changed, 13 insertions(+), 11 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index 956ffd0547fe..76095b4df2d7 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -825,17 +825,19 @@ static int php_openssl_sockop_cast(php_stream *stream, int castas, void **ret TS
4bf89e
 
4bf89e
 		case PHP_STREAM_AS_FD_FOR_SELECT:
4bf89e
 			if (ret) {
4bf89e
-				if (sslsock->ssl_active) {
4bf89e
-					/* OpenSSL has an internal buffer which select() cannot see. If we don't
4bf89e
-					   fetch it into the stream's buffer, no activity will be reported on the
4bf89e
-					   stream even though there is data waiting to be read - but we only fetch
4bf89e
-					   the number of bytes OpenSSL has ready to give us since we weren't asked
4bf89e
-					   for any data at this stage. This is only likely to cause issues with
4bf89e
-					   non-blocking streams, but it's harmless to always do it. */
4bf89e
-					int bytes;
4bf89e
-					while ((bytes = SSL_pending(sslsock->ssl_handle)) > 0) {
4bf89e
-						php_stream_fill_read_buffer(stream, (size_t)bytes);
4bf89e
-					}
4bf89e
+				/* OpenSSL has an internal buffer which select() cannot see. If we don't
4bf89e
+				 * fetch it into the stream's buffer, no activity will be reported on the
4bf89e
+				 * stream even though there is data waiting to be read - but we only fetch
4bf89e
+				 * the lower of bytes OpenSSL has ready to give us or chunk_size since we
4bf89e
+				 * weren't asked for any data at this stage. This is only likely to cause
4bf89e
+				 * issues with non-blocking streams, but it's harmless to always do it. */
4bf89e
+				size_t pending;
4bf89e
+				if (stream->writepos == stream->readpos
4bf89e
+					&& sslsock->ssl_active
4bf89e
+					&& (pending = (size_t)SSL_pending(sslsock->ssl_handle)) > 0) {
4bf89e
+						php_stream_fill_read_buffer(stream, pending < stream->chunk_size
4bf89e
+							? pending
4bf89e
+							: stream->chunk_size);
4bf89e
 				}
4bf89e
 
4bf89e
 				*(int *)ret = sslsock->s.socket;
4bf89e
From fd4641696cc67fedf494717b5e4d452019f04d6f Mon Sep 17 00:00:00 2001
4bf89e
From: Brad Broerman <bbroerman@bbroerman.net>
4bf89e
Date: Wed, 28 Jan 2015 00:04:20 -0500
4bf89e
Subject: [PATCH] Updated with SSL fixes (backported from trunk)
4bf89e
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 204 +++++++++++++++++++++++++++++++++++++++------------
4bf89e
 1 file changed, 159 insertions(+), 45 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index 6b74a2bb7362..53c48beca766 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -40,6 +40,9 @@
4bf89e
 int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC);
4bf89e
 SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC);
4bf89e
 int php_openssl_get_x509_list_id(void);
4bf89e
+static struct timeval subtract_timeval( struct timeval a, struct timeval b );
4bf89e
+static int compare_timeval( struct timeval a, struct timeval b );
4bf89e
+static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, size_t count TSRMLS_DC);
4bf89e
 
4bf89e
 /* This implementation is very closely tied to the that of the native
4bf89e
  * sockets implemented in the core.
4bf89e
@@ -171,69 +174,167 @@ static int handle_ssl_error(php_stream *stream, int nr_bytes, zend_bool is_init
4bf89e
 	return retry;
4bf89e
 }
4bf89e
 
4bf89e
-
4bf89e
 static size_t php_openssl_sockop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
4bf89e
 {
4bf89e
+	return php_openssl_sockop_io( 0, stream, buf, count );
4bf89e
+}
4bf89e
+
4bf89e
+static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
+{
4bf89e
+	return php_openssl_sockop_io( 1, stream, buf, count );
4bf89e
+}
4bf89e
+
4bf89e
+/**
4bf89e
+ * Factored out common functionality (blocking, timeout, loop management) for read and write.
4bf89e
+ * Perform IO (read or write) to an SSL socket. If we have a timeout, we switch to non-blocking mode
4bf89e
+ * for the duration of the operation, using select to do our waits. If we time out, or we have an error
4bf89e
+ * report that back to PHP
4bf89e
+ *
4bf89e
+ */
4bf89e
+static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
+{
4bf89e
 	php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract;
4bf89e
-	int didwrite;
4bf89e
+    int nr_bytes = 0;
4bf89e
 	
4bf89e
+    /* Only do this if SSL is active. */
4bf89e
 	if (sslsock->ssl_active) {
4bf89e
 		int retry = 1;
4bf89e
+        struct timeval  start_time,
4bf89e
+                        *timeout;
4bf89e
+        int             blocked   = sslsock->s.is_blocked,
4bf89e
+                        has_timeout = 0;
4bf89e
 
4bf89e
-		do {
4bf89e
-			didwrite = SSL_write(sslsock->ssl_handle, buf, count);
4bf89e
+		/* Begin by making the socket non-blocking. This allows us to check the timeout. */
4bf89e
+		if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0 TSRMLS_CC)) {
4bf89e
+			sslsock->s.is_blocked = 0;
4bf89e
+		}
4bf89e
 
4bf89e
-			if (didwrite <= 0) {
4bf89e
-				retry = handle_ssl_error(stream, didwrite, 0 TSRMLS_CC);
4bf89e
-			} else {
4bf89e
-				break;
4bf89e
-			}
4bf89e
-		} while(retry);
4bf89e
+		/* Get the timeout value (and make sure we are to check it. */
4bf89e
+		timeout = sslsock->is_client ? &sslsock->connect_timeout : &sslsock->s.timeout;
4bf89e
+		has_timeout = !sslsock->s.is_blocked && (timeout->tv_sec || timeout->tv_usec);
4bf89e
 
4bf89e
-		if (didwrite > 0) {
4bf89e
-			php_stream_notify_progress_increment(stream->context, didwrite, 0);
4bf89e
-		}
4bf89e
-	} else {
4bf89e
-		didwrite = php_stream_socket_ops.write(stream, buf, count TSRMLS_CC);
4bf89e
+		/* gettimeofday is not monotonic; using it here is not strictly correct */
4bf89e
+		if (has_timeout) {
4bf89e
+			gettimeofday(&start_time, NULL);
4bf89e
 	}
4bf89e
 
4bf89e
-	if (didwrite < 0) {
4bf89e
-		didwrite = 0;
4bf89e
-	}
4bf89e
+		/* Main IO loop. */
4bf89e
+		do {
4bf89e
+			struct timeval cur_time, elapsed_time, left_time;
4bf89e
 	
4bf89e
-	return didwrite;
4bf89e
-}
4bf89e
+			/* If we have a timeout to check, figure out how much time has elapsed since we started. */
4bf89e
+			if (has_timeout) {
4bf89e
+				gettimeofday(&cur_time, NULL);
4bf89e
 
4bf89e
-static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
-{
4bf89e
-	php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract;
4bf89e
-	int nr_bytes = 0;
4bf89e
+				/* Determine how much time we've taken so far. */
4bf89e
+				elapsed_time = subtract_timeval( cur_time, start_time );
4bf89e
 
4bf89e
-	if (sslsock->ssl_active) {
4bf89e
-		int retry = 1;
4bf89e
+				/* and return an error if we've taken too long. */
4bf89e
+				if (compare_timeval( elapsed_time, *timeout) > 0 ) {
4bf89e
+					/* If the socket was originally blocking, set it back. */
4bf89e
+					if (blocked) {
4bf89e
+						php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC);
4bf89e
+						sslsock->s.is_blocked = 1;
4bf89e
+					}
4bf89e
+					return -1;
4bf89e
+				}
4bf89e
+			}
4bf89e
 
4bf89e
-		do {
4bf89e
+			/* Now, do the IO operation. Don't block if we can't complete... */
4bf89e
+			if (read) {
4bf89e
 			nr_bytes = SSL_read(sslsock->ssl_handle, buf, count);
4bf89e
 
4bf89e
+			if (sslsock->reneg && sslsock->reneg->should_close) {
4bf89e
+				/* renegotiation rate limiting triggered */
4bf89e
+          php_stream_xport_shutdown(stream, (stream_shutdown_t)SHUT_RDWR);
4bf89e
+				nr_bytes = 0;
4bf89e
+				stream->eof = 1;
4bf89e
+				break;
4bf89e
+        }
4bf89e
+			} else {
4bf89e
+				nr_bytes = SSL_write(sslsock->ssl_handle, buf, count);
4bf89e
+			}
4bf89e
+
4bf89e
+			/* Now, how much time until we time out? */
4bf89e
+			if (has_timeout) {
4bf89e
+				left_time = subtract_timeval( *timeout, elapsed_time );
4bf89e
+			}
4bf89e
+
4bf89e
+			/* If we didn't do anything on the last loop (or an error) check to see if we should retry or exit. */
4bf89e
 			if (nr_bytes <= 0) {
4bf89e
+
4bf89e
+				/* Get the error code from SSL, and check to see if it's an error or not. */
4bf89e
+				int err = SSL_get_error(sslsock->ssl_handle, nr_bytes );
4bf89e
 				retry = handle_ssl_error(stream, nr_bytes, 0 TSRMLS_CC);
4bf89e
+
4bf89e
+				/* If we get this (the above doesn't check) then we'll retry as well. */
4bf89e
+				if (errno == EAGAIN && err == SSL_ERROR_WANT_READ && read) {
4bf89e
+					retry = 1;
4bf89e
+				}
4bf89e
+				if (errno == EAGAIN && SSL_ERROR_WANT_WRITE && read == 0) {          
4bf89e
+					retry = 1;
4bf89e
+				}
4bf89e
+
4bf89e
+				/* Also, on reads, we may get this condition on an EOF. We should check properly. */
4bf89e
+				if (read) {
4bf89e
 				stream->eof = (retry == 0 && errno != EAGAIN && !SSL_pending(sslsock->ssl_handle));
4bf89e
+				}
4bf89e
 				
4bf89e
+				/* Now, if we have to wait some time, and we're supposed to be blocking, wait for the socket to become
4bf89e
+				 * available. Now, php_pollfd_for uses select to wait up to our time_left value only...
4bf89e
+				 */
4bf89e
+				if (retry && blocked) {
4bf89e
+					if (read) {
4bf89e
+						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_WRITE) ?
4bf89e
+							(POLLOUT|POLLPRI) : (POLLIN|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
+					} else {
4bf89e
+						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_READ) ?
4bf89e
+							(POLLIN|POLLPRI) : (POLLOUT|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
+					}
4bf89e
+				}
4bf89e
 			} else {
4bf89e
-				/* we got the data */
4bf89e
+				/* Else, if we got bytes back, check for possible errors. */
4bf89e
+				int err = SSL_get_error(sslsock->ssl_handle, nr_bytes );
4bf89e
+
4bf89e
+				/* If we didn't get any error, then let's return it to PHP. */
4bf89e
+				if (err == SSL_ERROR_NONE)
4bf89e
 				break;
4bf89e
+
4bf89e
+				/* Otherwise, we need to wait again (up to time_left or we get an error) */
4bf89e
+				if (blocked)
4bf89e
+					if (read) {
4bf89e
+						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_WRITE) ?
4bf89e
+							(POLLOUT|POLLPRI) : (POLLIN|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
+					} else {
4bf89e
+						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_READ) ?
4bf89e
+							(POLLIN|POLLPRI) : (POLLOUT|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
+					}
4bf89e
 			}
4bf89e
+		/* Finally, we keep going until we got data, and an SSL_ERROR_NONE, unless we had an error. */
4bf89e
 		} while (retry);
4bf89e
 
4bf89e
+		/* Tell PHP if we read / wrote bytes. */
4bf89e
 		if (nr_bytes > 0) {
4bf89e
 			php_stream_notify_progress_increment(stream->context, nr_bytes, 0);
4bf89e
 		}
4bf89e
+
4bf89e
+		/* And if we were originally supposed to be blocking, let's reset the socket to that. */
4bf89e
+		if (blocked) {
4bf89e
+		  php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC);
4bf89e
+		  sslsock->s.is_blocked = 1;
4bf89e
 	}
4bf89e
-	else
4bf89e
-	{
4bf89e
+    } else {
4bf89e
+	    /*
4bf89e
+	     * This block is if we had no timeout... We will just sit and wait forever on the IO operation.
4bf89e
+	     */
4bf89e
+        if (read) {
4bf89e
 		nr_bytes = php_stream_socket_ops.read(stream, buf, count TSRMLS_CC);
4bf89e
+        } else {
4bf89e
+            nr_bytes = php_stream_socket_ops.write(stream, buf, count TSRMLS_CC);
4bf89e
 	}
4bf89e
+    }
4bf89e
 
4bf89e
+    /* PHP doesn't expect a negative return. */
4bf89e
 	if (nr_bytes < 0) {
4bf89e
 		nr_bytes = 0;
4bf89e
 	}
4bf89e
@@ -241,6 +342,31 @@ static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t coun
4bf89e
 	return nr_bytes;
4bf89e
 }
4bf89e
 
4bf89e
+struct timeval subtract_timeval( struct timeval a, struct timeval b )
4bf89e
+{
4bf89e
+	struct timeval difference;
4bf89e
+
4bf89e
+	difference.tv_sec  = a.tv_sec  - b.tv_sec;
4bf89e
+	difference.tv_usec = a.tv_usec - b.tv_usec;
4bf89e
+
4bf89e
+	if (a.tv_usec < b.tv_usec) {
4bf89e
+	  	b.tv_sec  -= 1L;
4bf89e
+	   	b.tv_usec += 1000000L;
4bf89e
+	}
4bf89e
+
4bf89e
+	return difference;
4bf89e
+}
4bf89e
+
4bf89e
+int compare_timeval( struct timeval a, struct timeval b )
4bf89e
+{
4bf89e
+	if (a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_usec > b.tv_usec) ) {
4bf89e
+		return 1;
4bf89e
+	} else if( a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec ) {
4bf89e
+		return 0;
4bf89e
+	} else {
4bf89e
+		return -1;
4bf89e
+	}
4bf89e
+}
4bf89e
 
4bf89e
 static int php_openssl_sockop_close(php_stream *stream, int close_handle TSRMLS_DC)
4bf89e
 {
4bf89e
@@ -482,16 +608,9 @@ static inline int php_openssl_enable_crypto(php_stream *stream,
4bf89e
 
4bf89e
 			if (has_timeout) {
4bf89e
 				gettimeofday(&cur_time, NULL);
4bf89e
-				elapsed_time.tv_sec  = cur_time.tv_sec  - start_time.tv_sec;
4bf89e
-				elapsed_time.tv_usec = cur_time.tv_usec - start_time.tv_usec;
4bf89e
-				if (cur_time.tv_usec < start_time.tv_usec) {
4bf89e
-					elapsed_time.tv_sec  -= 1L;
4bf89e
-					elapsed_time.tv_usec += 1000000L;
4bf89e
-				}
4bf89e
+				elapsed_time = subtract_timeval( cur_time, start_time );
4bf89e
 			
4bf89e
-				if (elapsed_time.tv_sec > timeout->tv_sec ||
4bf89e
-						(elapsed_time.tv_sec == timeout->tv_sec &&
4bf89e
-						elapsed_time.tv_usec > timeout->tv_usec)) {
4bf89e
+				if (compare_timeval( elapsed_time, *timeout) > 0) {
4bf89e
 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL: crypto enabling timeout");
4bf89e
 					return -1;
4bf89e
 				}
4bf89e
@@ -507,12 +626,7 @@ static inline int php_openssl_enable_crypto(php_stream *stream,
4bf89e
 					struct timeval left_time;
4bf89e
 					
4bf89e
 					if (has_timeout) {
4bf89e
-						left_time.tv_sec  = timeout->tv_sec  - elapsed_time.tv_sec;
4bf89e
-						left_time.tv_usec =	timeout->tv_usec - elapsed_time.tv_usec;
4bf89e
-						if (timeout->tv_usec < elapsed_time.tv_usec) {
4bf89e
-							left_time.tv_sec  -= 1L;
4bf89e
-							left_time.tv_usec += 1000000L;
4bf89e
-						}
4bf89e
+						left_time = subtract_timeval( *timeout, elapsed_time );
4bf89e
 					}
4bf89e
 					php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_READ) ?
4bf89e
 						(POLLIN|POLLPRI) : POLLOUT, has_timeout ? &left_time : NULL);
4bf89e
From 1482ed2d5660c3875add40706a18fe29e2b3ff70 Mon Sep 17 00:00:00 2001
4bf89e
From: Brad Broerman <bbroerman@bbroerman.net>
4bf89e
Date: Wed, 28 Jan 2015 22:36:41 -0500
4bf89e
Subject: [PATCH] reneg and should_close are not yet members of sslsock.
4bf89e
 Removing...
4bf89e
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 10 +---------
4bf89e
 1 file changed, 1 insertion(+), 9 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index 53c48beca766..842829f384a4 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -242,15 +242,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 
4bf89e
 			/* Now, do the IO operation. Don't block if we can't complete... */
4bf89e
 			if (read) {
4bf89e
-			nr_bytes = SSL_read(sslsock->ssl_handle, buf, count);
4bf89e
-
4bf89e
-			if (sslsock->reneg && sslsock->reneg->should_close) {
4bf89e
-				/* renegotiation rate limiting triggered */
4bf89e
-          php_stream_xport_shutdown(stream, (stream_shutdown_t)SHUT_RDWR);
4bf89e
-				nr_bytes = 0;
4bf89e
-				stream->eof = 1;
4bf89e
-				break;
4bf89e
-        }
4bf89e
+				nr_bytes = SSL_read(sslsock->ssl_handle, buf, count);
4bf89e
 			} else {
4bf89e
 				nr_bytes = SSL_write(sslsock->ssl_handle, buf, count);
4bf89e
 			}
4bf89e
From dddbe0fc338a0f01ba336e84755694fb9bfbeb53 Mon Sep 17 00:00:00 2001
4bf89e
From: Brad Broerman <bbroerman@bbroerman.net>
4bf89e
Date: Wed, 4 Feb 2015 10:13:36 -0500
4bf89e
Subject: [PATCH] Update xp_ssl.c
4bf89e
4bf89e
Added TSRMLS_CC to php_openssl_sockop_io calls.
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 4 ++--
4bf89e
 1 file changed, 2 insertions(+), 2 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index 842829f384a4..4b8a8955ae43 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -176,12 +176,12 @@ static int handle_ssl_error(php_stream *stream, int nr_bytes, zend_bool is_init
4bf89e
 
4bf89e
 static size_t php_openssl_sockop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
4bf89e
 {
4bf89e
-	return php_openssl_sockop_io( 0, stream, buf, count );
4bf89e
+	return php_openssl_sockop_io( 0, stream, buf, count TSRMLS_CC);
4bf89e
 }
4bf89e
 
4bf89e
 static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
 {
4bf89e
-	return php_openssl_sockop_io( 1, stream, buf, count );
4bf89e
+	return php_openssl_sockop_io( 1, stream, buf, count TSRMLS_CC);
4bf89e
 }
4bf89e
 
4bf89e
 /**
4bf89e
From 1eef4f2a0cf855b8f453e9fe8fb2499907754022 Mon Sep 17 00:00:00 2001
4bf89e
From: Daniel Lowrey <rdlowrey@php.net>
4bf89e
Date: Mon, 9 Feb 2015 11:42:17 -0500
4bf89e
Subject: [PATCH] Miscellaneous cleanup
4bf89e
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 56 ++++++++++++++++++++++++++--------------------------
4bf89e
 1 file changed, 28 insertions(+), 28 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index 4b8a8955ae43..5adff1f953fd 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -174,14 +174,14 @@ static int handle_ssl_error(php_stream *stream, int nr_bytes, zend_bool is_init
4bf89e
 	return retry;
4bf89e
 }
4bf89e
 
4bf89e
-static size_t php_openssl_sockop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
4bf89e
+static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
 {
4bf89e
-	return php_openssl_sockop_io( 0, stream, buf, count TSRMLS_CC);
4bf89e
+	return php_openssl_sockop_io(1, stream, buf, count TSRMLS_CC);
4bf89e
 }
4bf89e
 
4bf89e
-static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
+static size_t php_openssl_sockop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
4bf89e
 {
4bf89e
-	return php_openssl_sockop_io( 1, stream, buf, count TSRMLS_CC);
4bf89e
+	return php_openssl_sockop_io(0, stream, (char*)buf, count TSRMLS_CC);
4bf89e
 }
4bf89e
 
4bf89e
 /**
4bf89e
@@ -194,15 +194,15 @@ static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t coun
4bf89e
 static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, size_t count TSRMLS_DC)
4bf89e
 {
4bf89e
 	php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract;
4bf89e
-    int nr_bytes = 0;
4bf89e
+	int nr_bytes = 0;
4bf89e
 	
4bf89e
-    /* Only do this if SSL is active. */
4bf89e
+	/* Only do this if SSL is active. */
4bf89e
 	if (sslsock->ssl_active) {
4bf89e
 		int retry = 1;
4bf89e
-        struct timeval  start_time,
4bf89e
-                        *timeout;
4bf89e
-        int             blocked   = sslsock->s.is_blocked,
4bf89e
-                        has_timeout = 0;
4bf89e
+		struct timeval start_time;
4bf89e
+		struct timeval *timeout;
4bf89e
+		int blocked = sslsock->s.is_blocked;
4bf89e
+		int has_timeout = 0;
4bf89e
 
4bf89e
 		/* Begin by making the socket non-blocking. This allows us to check the timeout. */
4bf89e
 		if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0 TSRMLS_CC)) {
4bf89e
@@ -216,7 +216,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 		/* gettimeofday is not monotonic; using it here is not strictly correct */
4bf89e
 		if (has_timeout) {
4bf89e
 			gettimeofday(&start_time, NULL);
4bf89e
-	}
4bf89e
+		}
4bf89e
 
4bf89e
 		/* Main IO loop. */
4bf89e
 		do {
4bf89e
@@ -263,7 +263,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				if (errno == EAGAIN && err == SSL_ERROR_WANT_READ && read) {
4bf89e
 					retry = 1;
4bf89e
 				}
4bf89e
-				if (errno == EAGAIN && SSL_ERROR_WANT_WRITE && read == 0) {          
4bf89e
+				if (errno == EAGAIN && SSL_ERROR_WANT_WRITE && read == 0) {
4bf89e
 					retry = 1;
4bf89e
 				}
4bf89e
 
4bf89e
@@ -289,11 +289,12 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				int err = SSL_get_error(sslsock->ssl_handle, nr_bytes );
4bf89e
 
4bf89e
 				/* If we didn't get any error, then let's return it to PHP. */
4bf89e
-				if (err == SSL_ERROR_NONE)
4bf89e
-				break;
4bf89e
+				if (err == SSL_ERROR_NONE) {
4bf89e
+					break;
4bf89e
+				}
4bf89e
 
4bf89e
 				/* Otherwise, we need to wait again (up to time_left or we get an error) */
4bf89e
-				if (blocked)
4bf89e
+				if (blocked) {
4bf89e
 					if (read) {
4bf89e
 						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_WRITE) ?
4bf89e
 							(POLLOUT|POLLPRI) : (POLLIN|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
@@ -301,6 +302,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_READ) ?
4bf89e
 							(POLLIN|POLLPRI) : (POLLOUT|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
 					}
4bf89e
+				}
4bf89e
 			}
4bf89e
 		/* Finally, we keep going until we got data, and an SSL_ERROR_NONE, unless we had an error. */
4bf89e
 		} while (retry);
4bf89e
@@ -312,21 +314,19 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 
4bf89e
 		/* And if we were originally supposed to be blocking, let's reset the socket to that. */
4bf89e
 		if (blocked) {
4bf89e
-		  php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC);
4bf89e
-		  sslsock->s.is_blocked = 1;
4bf89e
-	}
4bf89e
-    } else {
4bf89e
-	    /*
4bf89e
-	     * This block is if we had no timeout... We will just sit and wait forever on the IO operation.
4bf89e
-	     */
4bf89e
-        if (read) {
4bf89e
-		nr_bytes = php_stream_socket_ops.read(stream, buf, count TSRMLS_CC);
4bf89e
-        } else {
4bf89e
-            nr_bytes = php_stream_socket_ops.write(stream, buf, count TSRMLS_CC);
4bf89e
+			php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC);
4bf89e
+			sslsock->s.is_blocked = 1;
4bf89e
+		}
4bf89e
+	} else {
4bf89e
+		/* This block is if we had no timeout... We will just sit and wait forever on the IO operation. */
4bf89e
+		if (read) {
4bf89e
+			nr_bytes = php_stream_socket_ops.read(stream, buf, count TSRMLS_CC);
4bf89e
+		} else {
4bf89e
+			nr_bytes = php_stream_socket_ops.write(stream, buf, count TSRMLS_CC);
4bf89e
+		}
4bf89e
 	}
4bf89e
-    }
4bf89e
 
4bf89e
-    /* PHP doesn't expect a negative return. */
4bf89e
+	/* PHP doesn't expect a negative return. */
4bf89e
 	if (nr_bytes < 0) {
4bf89e
 		nr_bytes = 0;
4bf89e
 	}
4bf89e
From 5ff77b005b646e1ae497640d9ddfa37f486f09a8 Mon Sep 17 00:00:00 2001
4bf89e
From: Anatol Belski <ab@php.net>
4bf89e
Date: Fri, 13 Feb 2015 13:39:46 +0100
4bf89e
Subject: [PATCH] fix condition
4bf89e
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 2 +-
4bf89e
 1 file changed, 1 insertion(+), 1 deletion(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index 5adff1f953fd..b8d747b5ec10 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -263,7 +263,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				if (errno == EAGAIN && err == SSL_ERROR_WANT_READ && read) {
4bf89e
 					retry = 1;
4bf89e
 				}
4bf89e
-				if (errno == EAGAIN && SSL_ERROR_WANT_WRITE && read == 0) {
4bf89e
+				if (errno == EAGAIN && err == SSL_ERROR_WANT_WRITE && read == 0) {
4bf89e
 					retry = 1;
4bf89e
 				}
4bf89e
 
4bf89e
From bbfd4a5e62cf058cb18d53a0817860edc01c371c Mon Sep 17 00:00:00 2001
4bf89e
From: Daniel Lowrey <rdlowrey@php.net>
4bf89e
Date: Mon, 9 Mar 2015 15:53:26 -0600
4bf89e
Subject: [PATCH] Fix crypto stream timeout regressions
4bf89e
4bf89e
---
4bf89e
 ext/openssl/xp_ssl.c | 37 +++++++++++++++++++------------------
4bf89e
 1 file changed, 19 insertions(+), 18 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index b8d747b5ec10..e8bc6ae39d35 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -200,21 +200,22 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 	if (sslsock->ssl_active) {
4bf89e
 		int retry = 1;
4bf89e
 		struct timeval start_time;
4bf89e
-		struct timeval *timeout;
4bf89e
-		int blocked = sslsock->s.is_blocked;
4bf89e
+		struct timeval *timeout = NULL;
4bf89e
+		int began_blocked = sslsock->s.is_blocked;
4bf89e
 		int has_timeout = 0;
4bf89e
 
4bf89e
-		/* Begin by making the socket non-blocking. This allows us to check the timeout. */
4bf89e
-		if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0 TSRMLS_CC)) {
4bf89e
-			sslsock->s.is_blocked = 0;
4bf89e
+		/* never use a timeout with non-blocking sockets */
4bf89e
+		if (began_blocked && &sslsock->s.timeout) {
4bf89e
+			timeout = &sslsock->s.timeout;
4bf89e
 		}
4bf89e
 
4bf89e
-		/* Get the timeout value (and make sure we are to check it. */
4bf89e
-		timeout = sslsock->is_client ? &sslsock->connect_timeout : &sslsock->s.timeout;
4bf89e
-		has_timeout = !sslsock->s.is_blocked && (timeout->tv_sec || timeout->tv_usec);
4bf89e
+		if (timeout && php_set_sock_blocking(sslsock->s.socket, 0 TSRMLS_CC) == SUCCESS) {
4bf89e
+			sslsock->s.is_blocked = 0;
4bf89e
+		}
4bf89e
 
4bf89e
-		/* gettimeofday is not monotonic; using it here is not strictly correct */
4bf89e
-		if (has_timeout) {
4bf89e
+		if (!sslsock->s.is_blocked && timeout && (timeout->tv_sec || timeout->tv_usec)) {
4bf89e
+			has_timeout = 1;
4bf89e
+			/* gettimeofday is not monotonic; using it here is not strictly correct */
4bf89e
 			gettimeofday(&start_time, NULL);
4bf89e
 		}
4bf89e
 
4bf89e
@@ -227,15 +228,16 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				gettimeofday(&cur_time, NULL);
4bf89e
 
4bf89e
 				/* Determine how much time we've taken so far. */
4bf89e
-				elapsed_time = subtract_timeval( cur_time, start_time );
4bf89e
+				elapsed_time = subtract_timeval(cur_time, start_time);
4bf89e
 
4bf89e
 				/* and return an error if we've taken too long. */
4bf89e
-				if (compare_timeval( elapsed_time, *timeout) > 0 ) {
4bf89e
+				if (compare_timeval(elapsed_time, *timeout) > 0 ) {
4bf89e
 					/* If the socket was originally blocking, set it back. */
4bf89e
-					if (blocked) {
4bf89e
+					if (began_blocked) {
4bf89e
 						php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC);
4bf89e
 						sslsock->s.is_blocked = 1;
4bf89e
 					}
4bf89e
+					sslsock->s.timeout_event = 1;
4bf89e
 					return -1;
4bf89e
 				}
4bf89e
 			}
4bf89e
@@ -275,7 +277,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				/* Now, if we have to wait some time, and we're supposed to be blocking, wait for the socket to become
4bf89e
 				 * available. Now, php_pollfd_for uses select to wait up to our time_left value only...
4bf89e
 				 */
4bf89e
-				if (retry && blocked) {
4bf89e
+				if (retry && began_blocked) {
4bf89e
 					if (read) {
4bf89e
 						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_WRITE) ?
4bf89e
 							(POLLOUT|POLLPRI) : (POLLIN|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
@@ -286,7 +288,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				}
4bf89e
 			} else {
4bf89e
 				/* Else, if we got bytes back, check for possible errors. */
4bf89e
-				int err = SSL_get_error(sslsock->ssl_handle, nr_bytes );
4bf89e
+				int err = SSL_get_error(sslsock->ssl_handle, nr_bytes);
4bf89e
 
4bf89e
 				/* If we didn't get any error, then let's return it to PHP. */
4bf89e
 				if (err == SSL_ERROR_NONE) {
4bf89e
@@ -294,7 +296,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 				}
4bf89e
 
4bf89e
 				/* Otherwise, we need to wait again (up to time_left or we get an error) */
4bf89e
-				if (blocked) {
4bf89e
+				if (began_blocked) {
4bf89e
 					if (read) {
4bf89e
 						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_WRITE) ?
4bf89e
 							(POLLOUT|POLLPRI) : (POLLIN|POLLPRI), has_timeout ? &left_time : NULL);
4bf89e
@@ -313,8 +315,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 		}
4bf89e
 
4bf89e
 		/* And if we were originally supposed to be blocking, let's reset the socket to that. */
4bf89e
-		if (blocked) {
4bf89e
-			php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC);
4bf89e
+		if (began_blocked && php_set_sock_blocking(sslsock->s.socket, 1 TSRMLS_CC) == SUCCESS) {
4bf89e
 			sslsock->s.is_blocked = 1;
4bf89e
 		}
4bf89e
 	} else {
4bf89e
From 601d60a978b9e053ab8e6dc0f12ff850fc642ced Mon Sep 17 00:00:00 2001
4bf89e
From: Daniel Lowrey <rdlowrey@php.net>
4bf89e
Date: Tue, 14 Apr 2015 09:12:28 -0600
4bf89e
Subject: [PATCH] Fix Bug #69402: Reading empty SSL stream hangs until timeout
4bf89e
4bf89e
---
4bf89e
 NEWS                 |  4 ++++
4bf89e
 ext/openssl/xp_ssl.c | 13 +++++++++----
4bf89e
 2 files changed, 13 insertions(+), 4 deletions(-)
4bf89e
4bf89e
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
4bf89e
index e8bc6ae39d35..6c80c2228733 100644
4bf89e
--- a/ext/openssl/xp_ssl.c
4bf89e
+++ b/ext/openssl/xp_ssl.c
4bf89e
@@ -195,7 +195,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 {
4bf89e
 	php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract;
4bf89e
 	int nr_bytes = 0;
4bf89e
-	
4bf89e
+
4bf89e
 	/* Only do this if SSL is active. */
4bf89e
 	if (sslsock->ssl_active) {
4bf89e
 		int retry = 1;
4bf89e
@@ -271,13 +271,18 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
4bf89e
 
4bf89e
 				/* Also, on reads, we may get this condition on an EOF. We should check properly. */
4bf89e
 				if (read) {
4bf89e
-				stream->eof = (retry == 0 && errno != EAGAIN && !SSL_pending(sslsock->ssl_handle));
4bf89e
+					stream->eof = (retry == 0 && errno != EAGAIN && !SSL_pending(sslsock->ssl_handle));
4bf89e
 				}
4bf89e
-				
4bf89e
+
4bf89e
+				/* Don't loop indefinitely in non-blocking mode if no data is available */
4bf89e
+				if (began_blocked == 0) {
4bf89e
+					break;
4bf89e
+				}
4bf89e
+
4bf89e
 				/* Now, if we have to wait some time, and we're supposed to be blocking, wait for the socket to become
4bf89e
 				 * available. Now, php_pollfd_for uses select to wait up to our time_left value only...
4bf89e
 				 */
4bf89e
-				if (retry && began_blocked) {
4bf89e
+				if (retry) {
4bf89e
 					if (read) {
4bf89e
 						php_pollfd_for(sslsock->s.socket, (err == SSL_ERROR_WANT_WRITE) ?
4bf89e
 							(POLLOUT|POLLPRI) : (POLLIN|POLLPRI), has_timeout ? &left_time : NULL);