Skip to content

Commit 83add44

Browse files
committed
Bug fix for SSL/TLS streams terminating prematurely (with data available in pre buffer)
1 parent fba2aa2 commit 83add44

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

GCD/GCDAsyncSocket.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
138138
#endif
139139
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
140140
SSLContextRef sslContext;
141-
NSMutableData *sslReadBuffer;
141+
GCDAsyncSocketPreBuffer *sslPreBuffer;
142142
size_t sslWriteCachedLength;
143+
OSStatus sslErrCode;
143144
#endif
144145

145146
id userData;

GCD/GCDAsyncSocket.m

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,7 @@ - (void)closeWithError:(NSError *)error
26292629
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
26302630
{
26312631
[sslPreBuffer reset];
2632+
sslErrCode = noErr;
26322633

26332634
if (sslContext)
26342635
{
@@ -4517,8 +4518,19 @@ - (void)doReadData
45174518
if (result == errSSLWouldBlock)
45184519
waiting = YES;
45194520
else
4520-
error = [self sslError:result];
4521-
4521+
{
4522+
if (result == errSSLClosedGraceful || result == errSSLClosedAbort)
4523+
{
4524+
// We've reached the end of the stream.
4525+
// Handle this the same way we would an EOF from the socket.
4526+
socketEOF = YES;
4527+
sslErrCode = result;
4528+
}
4529+
else
4530+
{
4531+
error = [self sslError:result];
4532+
}
4533+
}
45224534
// It's possible that bytesRead > 0, even if the result was errSSLWouldBlock.
45234535
// This happens when the SSLRead function is able to read some data,
45244536
// but not the entire amount we requested.
@@ -4910,7 +4922,23 @@ - (void)doReadEOF
49104922
{
49114923
if (error == nil)
49124924
{
4913-
error = [self connectionClosedError];
4925+
if ([self usingSecureTransportForTLS])
4926+
{
4927+
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
4928+
if (sslErrCode != noErr && sslErrCode != errSSLClosedGraceful)
4929+
{
4930+
error = [self sslError:sslErrCode];
4931+
}
4932+
else
4933+
{
4934+
error = [self connectionClosedError];
4935+
}
4936+
#endif
4937+
}
4938+
else
4939+
{
4940+
error = [self connectionClosedError];
4941+
}
49144942
}
49154943
[self closeWithError:error];
49164944
}
@@ -6402,6 +6430,8 @@ - (void)ssl_startTLS
64026430
[sslPreBuffer didWrite:preBufferLength];
64036431
}
64046432

6433+
sslErrCode = noErr;
6434+
64056435
// Start the SSL Handshake process
64066436

64076437
[self ssl_continueSSLHandshake];

0 commit comments

Comments
 (0)