Skip to content
/ src Public

Commit b5b7f1d

Browse files
committed
Add new OpenSSL api SSL_write_ex, SSL_read_ex and SSL_peek_ex
As these still meet the usual expectations for special, I will leave it up to ingo to decide to either document separately or in one man page like OpenSSL did. Will also need Symbols.list additions by tb@ when he starts the rapture ok tb@ jsing@
1 parent fa3025f commit b5b7f1d

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

lib/libssl/ssl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ssl.h,v 1.211 2021/10/23 11:41:51 beck Exp $ */
1+
/* $OpenBSD: ssl.h,v 1.212 2021/10/23 15:30:44 beck Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -1289,6 +1289,11 @@ int SSL_is_server(const SSL *s);
12891289
int SSL_read(SSL *ssl, void *buf, int num);
12901290
int SSL_peek(SSL *ssl, void *buf, int num);
12911291
int SSL_write(SSL *ssl, const void *buf, int num);
1292+
#if defined(LIBRESSL_NEW_API)
1293+
int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *bytes_read);
1294+
int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *bytes_peeked);
1295+
int SSL_write_ex(SSL *ssl, const void *buf, size_t num, size_t *bytes_written);
1296+
#endif
12921297

12931298
#if defined(LIBRESSL_HAS_TLS1_3) || defined(LIBRESSL_INTERNAL)
12941299
uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx);

lib/libssl/ssl_lib.c

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ssl_lib.c,v 1.271 2021/10/23 15:02:27 jsing Exp $ */
1+
/* $OpenBSD: ssl_lib.c,v 1.272 2021/10/23 15:30:44 beck Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -141,6 +141,7 @@
141141
*/
142142

143143
#include <arpa/inet.h>
144+
#include <sys/limits.h>
144145
#include <sys/socket.h>
145146
#include <netinet/in.h>
146147

@@ -995,6 +996,11 @@ SSL_get_default_timeout(const SSL *s)
995996
int
996997
SSL_read(SSL *s, void *buf, int num)
997998
{
999+
if (num < 0) {
1000+
SSLerror(s, SSL_R_BAD_LENGTH);
1001+
return -1;
1002+
}
1003+
9981004
if (s->internal->handshake_func == NULL) {
9991005
SSLerror(s, SSL_R_UNINITIALIZED);
10001006
return (-1);
@@ -1007,9 +1013,33 @@ SSL_read(SSL *s, void *buf, int num)
10071013
return ssl3_read(s, buf, num);
10081014
}
10091015

1016+
int
1017+
SSL_read_ex(SSL *s, void *buf, size_t num, size_t *bytes_read)
1018+
{
1019+
int ret;
1020+
1021+
/* We simply don't bother supporting enormous reads */
1022+
if (num > INT_MAX) {
1023+
SSLerror(s, SSL_R_BAD_LENGTH);
1024+
return 0;
1025+
}
1026+
1027+
ret = SSL_read(s, buf, (int)num);
1028+
if (ret < 0)
1029+
ret = 0;
1030+
*bytes_read = ret;
1031+
1032+
return ret > 0;
1033+
}
1034+
10101035
int
10111036
SSL_peek(SSL *s, void *buf, int num)
10121037
{
1038+
if (num < 0) {
1039+
SSLerror(s, SSL_R_BAD_LENGTH);
1040+
return -1;
1041+
}
1042+
10131043
if (s->internal->handshake_func == NULL) {
10141044
SSLerror(s, SSL_R_UNINITIALIZED);
10151045
return (-1);
@@ -1021,9 +1051,33 @@ SSL_peek(SSL *s, void *buf, int num)
10211051
return ssl3_peek(s, buf, num);
10221052
}
10231053

1054+
int
1055+
SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *bytes_peeked)
1056+
{
1057+
int ret;
1058+
1059+
/* We simply don't bother supporting enormous peeks */
1060+
if (num > INT_MAX) {
1061+
SSLerror(s, SSL_R_BAD_LENGTH);
1062+
return 0;
1063+
}
1064+
1065+
ret = SSL_peek(s, buf, (int)num);
1066+
if (ret < 0)
1067+
ret = 0;
1068+
*bytes_peeked = ret;
1069+
1070+
return ret > 0;
1071+
}
1072+
10241073
int
10251074
SSL_write(SSL *s, const void *buf, int num)
10261075
{
1076+
if (num < 0) {
1077+
SSLerror(s, SSL_R_BAD_LENGTH);
1078+
return -1;
1079+
}
1080+
10271081
if (s->internal->handshake_func == NULL) {
10281082
SSLerror(s, SSL_R_UNINITIALIZED);
10291083
return (-1);
@@ -1037,6 +1091,31 @@ SSL_write(SSL *s, const void *buf, int num)
10371091
return ssl3_write(s, buf, num);
10381092
}
10391093

1094+
int
1095+
SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *bytes_written)
1096+
{
1097+
int ret;
1098+
1099+
/* We simply don't bother supporting enormous writes */
1100+
if (num > INT_MAX) {
1101+
SSLerror(s, SSL_R_BAD_LENGTH);
1102+
return 0;
1103+
}
1104+
1105+
if (num == 0) {
1106+
/* This API is special */
1107+
bytes_written = 0;
1108+
return 1;
1109+
}
1110+
1111+
ret = SSL_write(s, buf, (int)num);
1112+
if (ret < 0)
1113+
ret = 0;
1114+
*bytes_written = ret;
1115+
1116+
return ret > 0;
1117+
}
1118+
10401119
uint32_t
10411120
SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
10421121
{

0 commit comments

Comments
 (0)