Skip to content

Commit 139914f

Browse files
committed
Add option for blocking reads
1 parent cd6c04a commit 139914f

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

ssl/ssl.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ extern "C" {
8383
#define SSL_DISPLAY_CERTS 0x00200000
8484
#define SSL_DISPLAY_RSA 0x00400000
8585
#define SSL_CONNECT_IN_PARTS 0x00800000
86+
#define SSL_READ_BLOCKING 0x01000000
8687

8788
/* errors that can be generated */
8889
#define SSL_OK 0

ssl/tls1.c

+25-12
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,23 @@ EXP_FUNC void STDCALL ssl_free(SSL *ssl)
260260
*/
261261
EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data)
262262
{
263-
int ret = basic_read(ssl, in_data);
263+
int ret = SSL_OK;
264+
do {
265+
ret= basic_read(ssl, in_data);
264266

265-
/* check for return code so we can send an alert */
266-
if (ret < SSL_OK && ret != SSL_CLOSE_NOTIFY)
267-
{
268-
if (ret != SSL_ERROR_CONN_LOST)
267+
/* check for return code so we can send an alert */
268+
if (ret < SSL_OK && ret != SSL_CLOSE_NOTIFY)
269269
{
270-
send_alert(ssl, ret);
271-
#ifndef CONFIG_SSL_SKELETON_MODE
272-
/* something nasty happened, so get rid of this session */
273-
kill_ssl_session(ssl->ssl_ctx->ssl_sessions, ssl);
274-
#endif
270+
if (ret != SSL_ERROR_CONN_LOST)
271+
{
272+
send_alert(ssl, ret);
273+
#ifndef CONFIG_SSL_SKELETON_MODE
274+
/* something nasty happened, so get rid of this session */
275+
kill_ssl_session(ssl->ssl_ctx->ssl_sessions, ssl);
276+
#endif
277+
}
275278
}
276-
}
277-
279+
} while (IS_SET_SSL_FLAG(SSL_READ_BLOCKING) && (ssl->got_bytes < ssl->need_bytes) && ret == 0 && !IS_SET_SSL_FLAG(SSL_NEED_RECORD));
278280
return ret;
279281
}
280282

@@ -558,6 +560,9 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
558560

559561
/* a bit hacky but saves a few bytes of memory */
560562
ssl->flag |= ssl_ctx->options;
563+
if (IS_SET_SSL_FLAG(SSL_CONNECT_IN_PARTS) && IS_SET_SSL_FLAG(SSL_READ_BLOCKING)) {
564+
CLR_SSL_FLAG(SSL_READ_BLOCKING);
565+
}
561566
SSL_CTX_LOCK(ssl_ctx->mutex);
562567

563568
if (ssl_ctx->head == NULL)
@@ -1293,6 +1298,14 @@ int basic_read(SSL *ssl, uint8_t **in_data)
12931298
ssl->need_bytes = (buf[3] << 8) + buf[4];
12941299

12951300
/* do we violate the spec with the message size? */
1301+
if (ssl->need_bytes > RT_MAX_PLAIN_LENGTH+RT_EXTRA-BM_RECORD_OFFSET)
1302+
{
1303+
printf("ssl->need_bytes=%d violates spec\r\n", ssl->need_bytes, RT_MAX_PLAIN_LENGTH+RT_EXTRA-BM_RECORD_OFFSET);
1304+
ret = SSL_ERROR_INVALID_PROT_MSG;
1305+
goto error;
1306+
}
1307+
1308+
/* is the allocated buffer large enough to handle all the data? if not, increase its size*/
12961309
if (ssl->need_bytes > ssl->max_plain_length+RT_EXTRA-BM_RECORD_OFFSET)
12971310
{
12981311
printf("ssl->need_bytes=%d > %d\r\n", ssl->need_bytes, ssl->max_plain_length+RT_EXTRA-BM_RECORD_OFFSET);

ssl/tls1_clnt.c

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
124124
case HS_FINISHED:
125125
ret = process_finished(ssl, buf, hs_len);
126126
disposable_free(ssl);
127+
if (ssl->ssl_ctx->options & SSL_READ_BLOCKING) {
128+
ssl->flag |= SSL_READ_BLOCKING;
129+
}
127130
/* note: client renegotiation is not allowed after this */
128131
break;
129132

0 commit comments

Comments
 (0)