ObjFW  Check-in [38e105d056]

Overview
Comment:Fix hanging blocking TLS connections with OpenSSL
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 38e105d056909d559e878e59851c425ee4272e237c4166b7156e9c69552b78ec
User & Date: js on 2023-08-27 09:23:56
Other Links: manifest | tags
References
2023-08-27
09:26 Fixed ticket [5ad1ca4257]: OFTLSStream with OpenSSL implementation sometimes hangs plus 4 other changes artifact: 22afd23f09 user: js
Context
2023-08-27
14:03
Don't require m68k-amigaos-g++ for exceptions check-in: 83aa78d1af user: js tags: trunk
09:23
Fix hanging blocking TLS connections with OpenSSL check-in: 38e105d056 user: js tags: trunk
2023-08-26
22:02
Correctly handle KAME's embedded scope ID check-in: 42acd2b94a user: js tags: trunk
Changes

Modified src/tls/OFOpenSSLTLSStream.m from [df13b43daa] to [d93e2e9a8c].

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

139



140


141









142

143


144

145



146


147
148
149
150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
{
	int ret;
	size_t bytesRead;

	if (!_handshakeDone)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (BIO_ctrl_pending(_readBIO) < 1) {
		@try {
			size_t tmp = [_underlyingStream
			    readIntoBuffer: _buffer
				    length: bufferSize];

			OFEnsure(tmp <= INT_MAX);
			/* Writing to a memory BIO must never fail. */
			OFEnsure(BIO_write(_readBIO, _buffer, (int)tmp) ==
			    (int)tmp);
		} @catch (OFReadFailedException *e) {
			if (e.errNo != EWOULDBLOCK && e.errNo != EAGAIN)
				@throw e;
		}
	}

	ret = SSL_read_ex(_SSL, buffer, length, &bytesRead);

	while (BIO_ctrl_pending(_writeBIO) > 0) {
		int tmp = BIO_read(_writeBIO, _buffer, bufferSize);

		OFEnsure(tmp >= 0);

		[_underlyingStream writeBuffer: _buffer length: tmp];
		[_underlyingStream flushWriteBuffer];
	}

	if (ret != 1) {

		/*



		 * The underlying stream might have had data ready, but not


		 * enough for OpenSSL to return decrypted data. This means the









		 * caller might have observed the TLS stream for reading, got a

		 * ready signal and read - and expects the read to succeed, not


		 * to fail with EWOULDBLOCK, as it was signaled ready.

		 * Therefore, return 0, as we could read 0 decrypted bytes, but



		 * cleared the ready signal of the underlying stream.


		 */
		if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ)
			return 0;


		/* FIXME: Translate error to errNo */
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: 0];
	}

	return bytesRead;
}

- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
{
	int ret;
	size_t bytesWritten;








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<











|
>
|
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
|
>
|
>
>
|
>
|
>
>
>
|
>
>
|


|
>
|
|
|
|
<
<
<







104
105
106
107
108
109
110
















111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163



164
165
166
167
168
169
170
{
	int ret;
	size_t bytesRead;

	if (!_handshakeDone)
		@throw [OFNotOpenException exceptionWithObject: self];

















	ret = SSL_read_ex(_SSL, buffer, length, &bytesRead);

	while (BIO_ctrl_pending(_writeBIO) > 0) {
		int tmp = BIO_read(_writeBIO, _buffer, bufferSize);

		OFEnsure(tmp >= 0);

		[_underlyingStream writeBuffer: _buffer length: tmp];
		[_underlyingStream flushWriteBuffer];
	}

	if (ret == 1)
		return bytesRead;

	if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ) {
		if (BIO_ctrl_pending(_readBIO) < 1) {
			@try {
				size_t tmp = [_underlyingStream
				    readIntoBuffer: _buffer
					    length: bufferSize];

				OFEnsure(tmp <= INT_MAX);
				/* Writing to a memory BIO must never fail. */
				OFEnsure(BIO_write(_readBIO, _buffer,
				    (int)tmp) == (int)tmp);
			} @catch (OFReadFailedException *e) {
				if (e.errNo == EWOULDBLOCK || e.errNo != EAGAIN)
					return 0;
			}
		}

		ret = SSL_read_ex(_SSL, buffer, length, &bytesRead);

		while (BIO_ctrl_pending(_writeBIO) > 0) {
			int tmp = BIO_read(_writeBIO, _buffer, bufferSize);

			OFEnsure(tmp >= 0);

			[_underlyingStream writeBuffer: _buffer length: tmp];
			[_underlyingStream flushWriteBuffer];
		}

		if (ret == 1)
			return bytesRead;

		if (SSL_get_error(_SSL, ret) == SSL_ERROR_WANT_READ)
			return 0;
	}

	/* FIXME: Translate error to errNo */
	@throw [OFReadFailedException exceptionWithObject: self
					  requestedLength: length
						    errNo: 0];



}

- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
{
	int ret;
	size_t bytesWritten;