@@ -50,7 +50,6 @@ extern "C"
50
50
#define SSL_DEBUG_OPTS 0
51
51
#endif
52
52
53
- #define SSL_RX_BUF_SIZE 4096
54
53
55
54
class SSLContext {
56
55
public:
@@ -59,8 +58,6 @@ class SSLContext {
59
58
_ssl_ctx = ssl_ctx_new (SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0 );
60
59
}
61
60
++_ssl_ctx_refcnt;
62
-
63
- _rxbuf = new cbuf (SSL_RX_BUF_SIZE);
64
61
}
65
62
66
63
~SSLContext () {
@@ -73,8 +70,6 @@ class SSLContext {
73
70
if (_ssl_ctx_refcnt == 0 ) {
74
71
ssl_ctx_free (_ssl_ctx);
75
72
}
76
-
77
- delete _rxbuf;
78
73
}
79
74
80
75
void ref () {
@@ -92,38 +87,50 @@ class SSLContext {
92
87
}
93
88
94
89
int read (uint8_t * dst, size_t size) {
95
- if (!_rxbuf->getSize ()) {
96
- _readAll ();
90
+ if (!_available) {
91
+ if (!_readAll ())
92
+ return 0 ;
97
93
}
98
- size_t available = _rxbuf->getSize ();
99
- size_t will_read = (available < size) ? available : size;
100
- return _rxbuf->read (reinterpret_cast <char *>(dst), will_read);
94
+ size_t will_copy = (_available < size) ? _available : size;
95
+ memcpy (dst, _read_ptr, will_copy);
96
+ _read_ptr += will_copy;
97
+ _available -= will_copy;
98
+ if (_available == 0 ) {
99
+ _read_ptr = nullptr ;
100
+ }
101
+ return will_copy;
101
102
}
102
103
103
104
int read () {
104
- optimistic_yield ( 100 );
105
- if (!_rxbuf-> getSize ()) {
106
- _readAll () ;
105
+ if (!_available) {
106
+ if (!_readAll ())
107
+ return - 1 ;
107
108
}
108
- return _rxbuf->read ();
109
+ int result = _read_ptr[0 ];
110
+ ++_read_ptr;
111
+ --_available;
112
+ if (_available == 0 ) {
113
+ _read_ptr = nullptr ;
114
+ }
115
+ return result;
109
116
}
110
117
111
118
int peek () {
112
- if (!_rxbuf->getSize ()) {
113
- _readAll ();
119
+ if (!_available) {
120
+ if (!_readAll ())
121
+ return -1 ;
114
122
}
115
- return _rxbuf-> peek () ;
123
+ return _read_ptr[ 0 ] ;
116
124
}
117
125
118
126
int available () {
119
- auto rc = _rxbuf->getSize ();
120
- if (rc == 0 ) {
121
- _readAll ();
122
- rc = _rxbuf->getSize ();
127
+ auto cb = _available;
128
+ if (cb == 0 ) {
129
+ cb = _readAll ();
123
130
} else {
124
131
optimistic_yield (100 );
125
132
}
126
- return rc ;
133
+ return cb ;
127
134
}
128
135
129
136
operator SSL*() {
@@ -135,6 +142,8 @@ class SSLContext {
135
142
if (!_ssl)
136
143
return 0 ;
137
144
145
+ optimistic_yield (100 );
146
+
138
147
uint8_t * data;
139
148
int rc = ssl_read (_ssl, &data);
140
149
if (rc <= 0 ) {
@@ -144,25 +153,18 @@ class SSLContext {
144
153
}
145
154
return 0 ;
146
155
}
147
-
148
-
149
- if (rc > _rxbuf->room ()) {
150
- DEBUGV (" WiFiClientSecure rx overflow" );
151
- rc = _rxbuf->room ();
152
- }
153
- int result = 0 ;
154
- size_t sizeBefore = _rxbuf->getSize ();
155
- if (rc)
156
- result = _rxbuf->write (reinterpret_cast <const char *>(data), rc);
157
- DEBUGV (" *** rb: %d + %d = %d\r\n " , sizeBefore, rc, _rxbuf->getSize ());
158
- return result;
156
+ DEBUGV (" :wcs ra %d" , rc);
157
+ _read_ptr = data;
158
+ _available = rc;
159
+ return _available;
159
160
}
160
161
161
162
static SSL_CTX* _ssl_ctx;
162
163
static int _ssl_ctx_refcnt;
163
164
SSL* _ssl = nullptr ;
164
165
int _refcnt = 0 ;
165
- cbuf* _rxbuf;
166
+ const uint8_t * _read_ptr = nullptr ;
167
+ size_t _available = 0 ;
166
168
};
167
169
168
170
SSL_CTX* SSLContext::_ssl_ctx = nullptr ;
@@ -313,14 +315,13 @@ bool WiFiClientSecure::verify(const char* fp, const char* url) {
313
315
while (pos < len && fp[pos] == ' ' ) {
314
316
++pos;
315
317
}
316
- DEBUGV (" pos:%d " , pos);
317
318
if (pos > len - 2 ) {
318
- DEBUGV (" fingerprint too short\r\n " );
319
+ DEBUGV (" pos:%d len:%d fingerprint too short\r\n " , pos, len );
319
320
return false ;
320
321
}
321
322
uint8_t high, low;
322
323
if (!parseHexNibble (fp[pos], &high) || !parseHexNibble (fp[pos+1 ], &low)) {
323
- DEBUGV (" invalid hex sequence: %c%c\r\n " , fp[pos], fp[pos+1 ]);
324
+ DEBUGV (" pos:%d len:%d invalid hex sequence: %c%c\r\n " , pos, len , fp[pos], fp[pos+1 ]);
324
325
return false ;
325
326
}
326
327
pos += 2 ;
0 commit comments