@@ -29,6 +29,7 @@ extern "C"
29
29
}
30
30
#include < errno.h>
31
31
#include " debug.h"
32
+ #include " cbuf.h"
32
33
#include " ESP8266WiFi.h"
33
34
#include " WiFiClientSecure.h"
34
35
#include " WiFiClient.h"
@@ -41,15 +42,23 @@ extern "C"
41
42
#include " include/ClientContext.h"
42
43
#include " c_types.h"
43
44
45
+ // #define DEBUG_SSL
46
+
47
+ #ifdef DEBUG_SSL
48
+ #define SSL_DEBUG_OPTS SSL_DISPLAY_STATES
49
+ #else
50
+ #define SSL_DEBUG_OPTS 0
51
+ #endif
44
52
45
53
class SSLContext {
46
54
public:
47
55
SSLContext () {
48
56
if (_ssl_ctx_refcnt == 0 ) {
49
- _ssl_ctx = ssl_ctx_new (SSL_SERVER_VERIFY_LATER | SSL_DISPLAY_STATES , 0 );
57
+ _ssl_ctx = ssl_ctx_new (SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS , 0 );
50
58
}
51
59
++_ssl_ctx_refcnt;
52
60
61
+ _rxbuf = new cbuf (1536 );
53
62
}
54
63
55
64
~SSLContext () {
@@ -62,6 +71,8 @@ class SSLContext {
62
71
if (_ssl_ctx_refcnt == 0 ) {
63
72
ssl_ctx_free (_ssl_ctx);
64
73
}
74
+
75
+ delete _rxbuf;
65
76
}
66
77
67
78
void ref () {
@@ -78,27 +89,71 @@ class SSLContext {
78
89
_ssl = ssl_client_new (_ssl_ctx, reinterpret_cast <int >(ctx), nullptr , 0 );
79
90
}
80
91
92
+ int read (uint8_t * dst, size_t size) {
93
+ if (size > _rxbuf->getSize ()) {
94
+ _readAll ();
95
+ }
96
+ return _rxbuf->read (reinterpret_cast <char *>(dst), size);
97
+ }
98
+
99
+ int read () {
100
+ optimistic_yield (100 );
101
+ if (!_rxbuf->getSize ()) {
102
+ _readAll ();
103
+ }
104
+ return _rxbuf->read ();
105
+ }
106
+
107
+ int peek () {
108
+ if (!_rxbuf->getSize ()) {
109
+ _readAll ();
110
+ }
111
+ return _rxbuf->peek ();
112
+ }
113
+
114
+ int available () {
115
+ optimistic_yield (100 );
116
+ return _rxbuf->getSize ();
117
+ }
118
+
81
119
operator SSL*() {
82
120
return _ssl;
83
121
}
84
122
85
123
protected:
124
+ int _readAll () {
125
+ uint8_t * data;
126
+ int rc = ssl_read (_ssl, &data);
127
+ if (rc <= 0 )
128
+ return 0 ;
129
+
130
+ if (rc > _rxbuf->room ()) {
131
+ DEBUGV (" WiFiClientSecure rx overflow" );
132
+ rc = _rxbuf->room ();
133
+ }
134
+ int result = 0 ;
135
+ size_t sizeBefore = _rxbuf->getSize ();
136
+ if (rc)
137
+ result = _rxbuf->write (reinterpret_cast <const char *>(data), rc);
138
+ DEBUGV (" *** rb: %d + %d = %d\r\n " , sizeBefore, rc, _rxbuf->getSize ());
139
+ return result;
140
+ }
141
+
86
142
static SSL_CTX* _ssl_ctx;
87
143
static int _ssl_ctx_refcnt;
88
144
SSL* _ssl = nullptr ;
89
145
int _refcnt = 0 ;
146
+ cbuf* _rxbuf;
90
147
};
91
148
92
149
SSL_CTX* SSLContext::_ssl_ctx = nullptr ;
93
150
int SSLContext::_ssl_ctx_refcnt = 0 ;
94
151
95
152
96
- WiFiClientSecure::WiFiClientSecure ()
97
- {
153
+ WiFiClientSecure::WiFiClientSecure () {
98
154
}
99
155
100
- WiFiClientSecure::~WiFiClientSecure ()
101
- {
156
+ WiFiClientSecure::~WiFiClientSecure () {
102
157
if (_ssl) {
103
158
_ssl->unref ();
104
159
}
@@ -164,14 +219,19 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
164
219
}
165
220
166
221
int WiFiClientSecure::read (uint8_t *buf, size_t size) {
222
+ return _ssl->read (buf, size);
223
+ }
167
224
168
- uint8_t * data;
169
- int rc = ssl_read (*_ssl, &data);
170
- if (rc <= 0 )
171
- return 0 ;
225
+ int WiFiClientSecure::read () {
226
+ return _ssl->read ();
227
+ }
228
+
229
+ int WiFiClientSecure::peek () {
230
+ return _ssl->peek ();
231
+ }
172
232
173
- memcpy (buf, data, rc);
174
- return rc ;
233
+ int WiFiClientSecure::available () {
234
+ return _ssl-> available () ;
175
235
}
176
236
177
237
void WiFiClientSecure::stop () {
@@ -217,13 +277,13 @@ extern "C" int ax_get_file(const char *filename, uint8_t **buf) {
217
277
return 0 ;
218
278
}
219
279
280
+
220
281
#ifdef DEBUG_TLS_MEM
221
282
#define DEBUG_TLS_MEM_PRINT (...) DEBUGV(__VA_ARGS__)
222
283
#else
223
284
#define DEBUG_TLS_MEM_PRINT (...)
224
285
#endif
225
286
226
-
227
287
extern " C" void * ax_port_malloc (size_t size, const char * file, int line) {
228
288
void * result = malloc (size);
229
289
@@ -254,7 +314,6 @@ extern "C" void* ax_port_realloc(void* ptr, size_t size, const char* file, int l
254
314
return result;
255
315
}
256
316
257
-
258
317
extern " C" void ax_port_free (void * ptr) {
259
318
free (ptr);
260
319
uint32_t *p = (uint32_t *) ptr;
0 commit comments