/src/wolfssl/src/wolfio.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* wolfio.c |
2 | | * |
3 | | * Copyright (C) 2006-2023 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | |
23 | | #ifndef WOLFSSL_STRERROR_BUFFER_SIZE |
24 | | #define WOLFSSL_STRERROR_BUFFER_SIZE 256 |
25 | | #endif |
26 | | |
27 | | #ifdef HAVE_CONFIG_H |
28 | | #include <config.h> |
29 | | #endif |
30 | | |
31 | | #include <wolfssl/wolfcrypt/settings.h> |
32 | | |
33 | | #ifndef WOLFCRYPT_ONLY |
34 | | |
35 | | #ifdef _WIN32_WCE |
36 | | /* On WinCE winsock2.h must be included before windows.h for socket stuff */ |
37 | | #include <winsock2.h> |
38 | | #endif |
39 | | |
40 | | #include <wolfssl/internal.h> |
41 | | #include <wolfssl/error-ssl.h> |
42 | | #include <wolfssl/wolfio.h> |
43 | | |
44 | | #if defined(HAVE_HTTP_CLIENT) |
45 | | #include <stdlib.h> /* strtol() */ |
46 | | #endif |
47 | | |
48 | | /* |
49 | | Possible IO enable options: |
50 | | * WOLFSSL_USER_IO: Disables default Embed* callbacks and default: off |
51 | | allows user to define their own using |
52 | | wolfSSL_CTX_SetIORecv and wolfSSL_CTX_SetIOSend |
53 | | * USE_WOLFSSL_IO: Enables the wolfSSL IO functions default: on |
54 | | * HAVE_HTTP_CLIENT: Enables HTTP client API's default: off |
55 | | (unless HAVE_OCSP or HAVE_CRL_IO defined) |
56 | | * HAVE_IO_TIMEOUT: Enables support for connect timeout default: off |
57 | | * |
58 | | * DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER: This flag has effect only if |
59 | | * ASN_NO_TIME is enabled. If enabled invalid peers messages are ignored |
60 | | * indefinetely. If not enabled EmbedReceiveFrom will return timeout after |
61 | | * DTLS_RECEIVEFROM_MAX_INVALID_PEER number of packets from invalid peers. When |
62 | | * enabled, without a timer, EmbedReceivefrom can't check if the timeout is |
63 | | * expired and it may never return under a continuous flow of invalid packets. |
64 | | * default: off |
65 | | */ |
66 | | |
67 | | |
68 | | /* if user writes own I/O callbacks they can define WOLFSSL_USER_IO to remove |
69 | | automatic setting of default I/O functions EmbedSend() and EmbedReceive() |
70 | | but they'll still need SetCallback xxx() at end of file |
71 | | */ |
72 | | |
73 | | #if defined(NO_ASN_TIME) && !defined(DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER) \ |
74 | | && !defined(DTLS_RECEIVEFROM_MAX_INVALID_PEER) |
75 | | #define DTLS_RECEIVEFROM_MAX_INVALID_PEER 10 |
76 | | #endif |
77 | | |
78 | | #if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT) |
79 | | |
80 | | /* Translates return codes returned from |
81 | | * send() and recv() if need be. |
82 | | */ |
83 | | static WC_INLINE int TranslateReturnCode(int old, int sd) |
84 | 0 | { |
85 | 0 | (void)sd; |
86 | |
|
87 | | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
88 | | if (old == 0) { |
89 | | errno = SOCKET_EWOULDBLOCK; |
90 | | return -1; /* convert to BSD style wouldblock as error */ |
91 | | } |
92 | | |
93 | | if (old < 0) { |
94 | | errno = RTCS_geterror(sd); |
95 | | if (errno == RTCSERR_TCP_CONN_CLOSING) |
96 | | return 0; /* convert to BSD style closing */ |
97 | | if (errno == RTCSERR_TCP_CONN_RLSD) |
98 | | errno = SOCKET_ECONNRESET; |
99 | | if (errno == RTCSERR_TCP_TIMED_OUT) |
100 | | errno = SOCKET_EAGAIN; |
101 | | } |
102 | | #endif |
103 | |
|
104 | 0 | return old; |
105 | 0 | } |
106 | | |
107 | | static WC_INLINE int wolfSSL_LastError(int err) |
108 | 0 | { |
109 | 0 | (void)err; /* Suppress unused arg */ |
110 | |
|
111 | | #ifdef USE_WINDOWS_API |
112 | | return WSAGetLastError(); |
113 | | #elif defined(EBSNET) |
114 | | return xn_getlasterror(); |
115 | | #elif defined(WOLFSSL_LINUXKM) |
116 | | return err; /* Return provided error value */ |
117 | | #elif defined(FUSION_RTOS) |
118 | | #include <fclerrno.h> |
119 | | return FCL_GET_ERRNO; |
120 | | #else |
121 | 0 | return errno; |
122 | 0 | #endif |
123 | 0 | } |
124 | | |
125 | | static int TranslateIoError(int err) |
126 | 0 | { |
127 | | #ifdef _WIN32 |
128 | | size_t errstr_offset; |
129 | | char errstr[WOLFSSL_STRERROR_BUFFER_SIZE]; |
130 | | #endif /* _WIN32 */ |
131 | | |
132 | |
|
133 | 0 | if (err > 0) |
134 | 0 | return err; |
135 | | |
136 | 0 | err = wolfSSL_LastError(err); |
137 | | #if SOCKET_EWOULDBLOCK != SOCKET_EAGAIN |
138 | | if ((err == SOCKET_EWOULDBLOCK) || (err == SOCKET_EAGAIN)) |
139 | | #else |
140 | 0 | if (err == SOCKET_EWOULDBLOCK) |
141 | 0 | #endif |
142 | 0 | { |
143 | 0 | WOLFSSL_MSG("\tWould block"); |
144 | 0 | return WOLFSSL_CBIO_ERR_WANT_READ; |
145 | 0 | } |
146 | 0 | else if (err == SOCKET_ECONNRESET) { |
147 | 0 | WOLFSSL_MSG("\tConnection reset"); |
148 | 0 | return WOLFSSL_CBIO_ERR_CONN_RST; |
149 | 0 | } |
150 | 0 | else if (err == SOCKET_EINTR) { |
151 | 0 | WOLFSSL_MSG("\tSocket interrupted"); |
152 | 0 | return WOLFSSL_CBIO_ERR_ISR; |
153 | 0 | } |
154 | 0 | else if (err == SOCKET_EPIPE) { |
155 | 0 | WOLFSSL_MSG("\tBroken pipe"); |
156 | 0 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
157 | 0 | } |
158 | 0 | else if (err == SOCKET_ECONNABORTED) { |
159 | 0 | WOLFSSL_MSG("\tConnection aborted"); |
160 | 0 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
161 | 0 | } |
162 | | |
163 | | #if defined(_WIN32) |
164 | | strcpy_s(errstr, sizeof(errstr), "\tGeneral error: "); |
165 | | errstr_offset = strlen(errstr); |
166 | | FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
167 | | NULL, |
168 | | err, |
169 | | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
170 | | (LPSTR)(errstr + errstr_offset), |
171 | | (DWORD)(sizeof(errstr) - errstr_offset), |
172 | | NULL); |
173 | | WOLFSSL_MSG(errstr); |
174 | | #else |
175 | 0 | WOLFSSL_MSG("\tGeneral error"); |
176 | 0 | #endif |
177 | 0 | return WOLFSSL_CBIO_ERR_GENERAL; |
178 | 0 | } |
179 | | #endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */ |
180 | | |
181 | | #ifdef OPENSSL_EXTRA |
182 | | #ifndef NO_BIO |
183 | | /* Use the WOLFSSL read BIO for receiving data. This is set by the function |
184 | | * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIORecv. |
185 | | * |
186 | | * ssl WOLFSSL struct passed in that has this function set as the receive |
187 | | * callback. |
188 | | * buf buffer to fill with data read |
189 | | * sz size of buf buffer |
190 | | * ctx a user set context |
191 | | * |
192 | | * returns the amount of data read or want read. See WOLFSSL_CBIO_ERR_* values. |
193 | | */ |
194 | | int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
195 | | { |
196 | | int recvd = WOLFSSL_CBIO_ERR_GENERAL; |
197 | | |
198 | | WOLFSSL_ENTER("BioReceive"); |
199 | | |
200 | | if (ssl->biord == NULL) { |
201 | | WOLFSSL_MSG("WOLFSSL biord not set"); |
202 | | return WOLFSSL_CBIO_ERR_GENERAL; |
203 | | } |
204 | | |
205 | | recvd = wolfSSL_BIO_read(ssl->biord, buf, sz); |
206 | | if (recvd <= 0) { |
207 | | if (/* ssl->biowr->wrIdx is checked for Bind9 */ |
208 | | wolfSSL_BIO_method_type(ssl->biowr) == WOLFSSL_BIO_BIO && |
209 | | wolfSSL_BIO_wpending(ssl->biowr) != 0 && |
210 | | /* Not sure this pending check is necessary but let's double |
211 | | * check that the read BIO is empty before we signal a write |
212 | | * need */ |
213 | | wolfSSL_BIO_supports_pending(ssl->biord) && |
214 | | wolfSSL_BIO_ctrl_pending(ssl->biord) == 0) { |
215 | | /* Let's signal to the app layer that we have |
216 | | * data pending that needs to be sent. */ |
217 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
218 | | } |
219 | | else if (ssl->biord->type == WOLFSSL_BIO_SOCKET) { |
220 | | if (recvd == 0) { |
221 | | WOLFSSL_MSG("BioReceive connection closed"); |
222 | | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
223 | | } |
224 | | #ifdef USE_WOLFSSL_IO |
225 | | recvd = TranslateIoError(recvd); |
226 | | #endif |
227 | | return recvd; |
228 | | } |
229 | | |
230 | | /* If retry and read flags are set, return WANT_READ */ |
231 | | if ((ssl->biord->flags & WOLFSSL_BIO_FLAG_READ) && |
232 | | (ssl->biord->flags & WOLFSSL_BIO_FLAG_RETRY)) { |
233 | | return WOLFSSL_CBIO_ERR_WANT_READ; |
234 | | } |
235 | | |
236 | | WOLFSSL_MSG("BIO general error"); |
237 | | return WOLFSSL_CBIO_ERR_GENERAL; |
238 | | } |
239 | | |
240 | | (void)ctx; |
241 | | return recvd; |
242 | | } |
243 | | |
244 | | |
245 | | /* Use the WOLFSSL write BIO for sending data. This is set by the function |
246 | | * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIOSend. |
247 | | * |
248 | | * ssl WOLFSSL struct passed in that has this function set as the send callback. |
249 | | * buf buffer with data to write out |
250 | | * sz size of buf buffer |
251 | | * ctx a user set context |
252 | | * |
253 | | * returns the amount of data sent or want send. See WOLFSSL_CBIO_ERR_* values. |
254 | | */ |
255 | | int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
256 | | { |
257 | | int sent = WOLFSSL_CBIO_ERR_GENERAL; |
258 | | |
259 | | WOLFSSL_ENTER("BioSend"); |
260 | | |
261 | | if (ssl->biowr == NULL) { |
262 | | WOLFSSL_MSG("WOLFSSL biowr not set"); |
263 | | return WOLFSSL_CBIO_ERR_GENERAL; |
264 | | } |
265 | | |
266 | | sent = wolfSSL_BIO_write(ssl->biowr, buf, sz); |
267 | | if (sent <= 0) { |
268 | | if (ssl->biowr->type == WOLFSSL_BIO_SOCKET) { |
269 | | #ifdef USE_WOLFSSL_IO |
270 | | sent = TranslateIoError(sent); |
271 | | #endif |
272 | | return sent; |
273 | | } |
274 | | else if (ssl->biowr->type == WOLFSSL_BIO_BIO) { |
275 | | if (sent == WOLFSSL_BIO_ERROR) { |
276 | | WOLFSSL_MSG("\tWould Block"); |
277 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
278 | | } |
279 | | } |
280 | | |
281 | | /* If retry and write flags are set, return WANT_WRITE */ |
282 | | if ((ssl->biord->flags & WOLFSSL_BIO_FLAG_WRITE) && |
283 | | (ssl->biord->flags & WOLFSSL_BIO_FLAG_RETRY)) { |
284 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
285 | | } |
286 | | |
287 | | return WOLFSSL_CBIO_ERR_GENERAL; |
288 | | } |
289 | | (void)ctx; |
290 | | |
291 | | return sent; |
292 | | } |
293 | | #endif /* !NO_BIO */ |
294 | | #endif /* OPENSSL_EXTRA */ |
295 | | |
296 | | |
297 | | #ifdef USE_WOLFSSL_IO |
298 | | |
299 | | /* The receive embedded callback |
300 | | * return : nb bytes read, or error |
301 | | */ |
302 | | int EmbedReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
303 | 0 | { |
304 | 0 | int recvd; |
305 | 0 | #ifndef WOLFSSL_LINUXKM |
306 | 0 | int sd = *(int*)ctx; |
307 | | #else |
308 | | struct socket *sd = (struct socket*)ctx; |
309 | | #endif |
310 | |
|
311 | 0 | recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags); |
312 | 0 | if (recvd < 0) { |
313 | 0 | WOLFSSL_MSG("Embed Receive error"); |
314 | 0 | return TranslateIoError(recvd); |
315 | 0 | } |
316 | 0 | else if (recvd == 0) { |
317 | 0 | WOLFSSL_MSG("Embed receive connection closed"); |
318 | 0 | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
319 | 0 | } |
320 | | |
321 | 0 | return recvd; |
322 | 0 | } |
323 | | |
324 | | /* The send embedded callback |
325 | | * return : nb bytes sent, or error |
326 | | */ |
327 | | int EmbedSend(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
328 | 0 | { |
329 | 0 | int sent; |
330 | 0 | #ifndef WOLFSSL_LINUXKM |
331 | 0 | int sd = *(int*)ctx; |
332 | | #else |
333 | | struct socket *sd = (struct socket*)ctx; |
334 | | #endif |
335 | |
|
336 | | #ifdef WOLFSSL_MAX_SEND_SZ |
337 | | if (sz > WOLFSSL_MAX_SEND_SZ) |
338 | | sz = WOLFSSL_MAX_SEND_SZ; |
339 | | #endif |
340 | |
|
341 | 0 | sent = wolfIO_Send(sd, buf, sz, ssl->wflags); |
342 | 0 | if (sent < 0) { |
343 | 0 | WOLFSSL_MSG("Embed Send error"); |
344 | 0 | return TranslateIoError(sent); |
345 | 0 | } |
346 | | |
347 | 0 | return sent; |
348 | 0 | } |
349 | | |
350 | | |
351 | | #ifdef WOLFSSL_DTLS |
352 | | |
353 | | #include <wolfssl/wolfcrypt/sha.h> |
354 | | |
355 | | #ifndef DTLS_SENDTO_FUNCTION |
356 | | #define DTLS_SENDTO_FUNCTION sendto |
357 | | #endif |
358 | | #ifndef DTLS_RECVFROM_FUNCTION |
359 | | #define DTLS_RECVFROM_FUNCTION recvfrom |
360 | | #endif |
361 | | |
362 | | static int sockAddrEqual( |
363 | | SOCKADDR_S *a, XSOCKLENT aLen, SOCKADDR_S *b, XSOCKLENT bLen) |
364 | | { |
365 | | if (aLen != bLen) |
366 | | return 0; |
367 | | |
368 | | if (a->ss_family != b->ss_family) |
369 | | return 0; |
370 | | |
371 | | if (a->ss_family == WOLFSSL_IP4) { |
372 | | |
373 | | if (aLen < (XSOCKLENT)sizeof(SOCKADDR_IN)) |
374 | | return 0; |
375 | | |
376 | | if (((SOCKADDR_IN*)a)->sin_port != ((SOCKADDR_IN*)b)->sin_port) |
377 | | return 0; |
378 | | |
379 | | if (((SOCKADDR_IN*)a)->sin_addr.s_addr != |
380 | | ((SOCKADDR_IN*)b)->sin_addr.s_addr) |
381 | | return 0; |
382 | | |
383 | | return 1; |
384 | | } |
385 | | |
386 | | #ifdef WOLFSSL_IPV6 |
387 | | if (a->ss_family == WOLFSSL_IP6) { |
388 | | SOCKADDR_IN6 *a6, *b6; |
389 | | |
390 | | if (aLen < (XSOCKLENT)sizeof(SOCKADDR_IN6)) |
391 | | return 0; |
392 | | |
393 | | a6 = (SOCKADDR_IN6*)a; |
394 | | b6 = (SOCKADDR_IN6*)b; |
395 | | |
396 | | if (((SOCKADDR_IN6*)a)->sin6_port != ((SOCKADDR_IN6*)b)->sin6_port) |
397 | | return 0; |
398 | | |
399 | | if (XMEMCMP((void*)&a6->sin6_addr, (void*)&b6->sin6_addr, |
400 | | sizeof(a6->sin6_addr)) != 0) |
401 | | return 0; |
402 | | |
403 | | return 1; |
404 | | } |
405 | | #endif /* WOLFSSL_IPV6 */ |
406 | | |
407 | | return 0; |
408 | | } |
409 | | |
410 | | #ifndef WOLFSSL_IPV6 |
411 | | static int PeerIsIpv6(const SOCKADDR_S *peer, XSOCKLENT len) |
412 | | { |
413 | | if (len < (XSOCKLENT)sizeof(peer->ss_family)) |
414 | | return 0; |
415 | | return peer->ss_family == WOLFSSL_IP6; |
416 | | } |
417 | | #endif /* !WOLFSSL_IPV6 */ |
418 | | |
419 | | static int isDGramSock(int sfd) |
420 | | { |
421 | | int type = 0; |
422 | | /* optvalue 'type' is of size int */ |
423 | | XSOCKLENT length = (XSOCKLENT)sizeof(type); |
424 | | |
425 | | if (getsockopt(sfd, SOL_SOCKET, SO_TYPE, (XSOCKOPT_TYPE_OPTVAL_TYPE)&type, |
426 | | &length) == 0 && type != SOCK_DGRAM) { |
427 | | return 0; |
428 | | } |
429 | | else { |
430 | | return 1; |
431 | | } |
432 | | } |
433 | | |
434 | | /* The receive embedded callback |
435 | | * return : nb bytes read, or error |
436 | | */ |
437 | | int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
438 | | { |
439 | | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
440 | | int recvd; |
441 | | int sd = dtlsCtx->rfd; |
442 | | int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); |
443 | | byte doDtlsTimeout; |
444 | | SOCKADDR_S lclPeer; |
445 | | SOCKADDR_S* peer; |
446 | | XSOCKLENT peerSz = 0; |
447 | | #ifndef NO_ASN_TIME |
448 | | word32 start = 0; |
449 | | #elif !defined(DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER) |
450 | | word32 invalidPeerPackets = 0; |
451 | | #endif |
452 | | |
453 | | WOLFSSL_ENTER("EmbedReceiveFrom"); |
454 | | |
455 | | if (dtlsCtx->connected) { |
456 | | peer = NULL; |
457 | | } |
458 | | else if (dtlsCtx->userSet) { |
459 | | #ifndef WOLFSSL_IPV6 |
460 | | if (PeerIsIpv6((SOCKADDR_S*)dtlsCtx->peer.sa, dtlsCtx->peer.sz)) { |
461 | | WOLFSSL_MSG("ipv6 dtls peer set but no ipv6 support compiled"); |
462 | | return NOT_COMPILED_IN; |
463 | | } |
464 | | #endif |
465 | | peer = &lclPeer; |
466 | | XMEMSET(&lclPeer, 0, sizeof(lclPeer)); |
467 | | peerSz = sizeof(lclPeer); |
468 | | } |
469 | | else { |
470 | | /* Store the peer address. It is used to calculate the DTLS cookie. */ |
471 | | if (dtlsCtx->peer.sa == NULL) { |
472 | | dtlsCtx->peer.sa = (void*)XMALLOC(sizeof(SOCKADDR_S), |
473 | | ssl->heap, DYNAMIC_TYPE_SOCKADDR); |
474 | | dtlsCtx->peer.sz = 0; |
475 | | if (dtlsCtx->peer.sa != NULL) |
476 | | dtlsCtx->peer.bufSz = sizeof(SOCKADDR_S); |
477 | | else |
478 | | dtlsCtx->peer.bufSz = 0; |
479 | | } |
480 | | peer = (SOCKADDR_S*)dtlsCtx->peer.sa; |
481 | | peerSz = dtlsCtx->peer.bufSz; |
482 | | } |
483 | | |
484 | | /* Don't use ssl->options.handShakeDone since it is true even if |
485 | | * we are in the process of renegotiation */ |
486 | | doDtlsTimeout = ssl->options.handShakeState != HANDSHAKE_DONE; |
487 | | |
488 | | #ifdef WOLFSSL_DTLS13 |
489 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) { |
490 | | doDtlsTimeout = |
491 | | doDtlsTimeout || ssl->dtls13Rtx.rtxRecords != NULL || |
492 | | (ssl->dtls13FastTimeout && ssl->dtls13Rtx.seenRecords != NULL); |
493 | | } |
494 | | #endif /* WOLFSSL_DTLS13 */ |
495 | | |
496 | | do { |
497 | | |
498 | | if (!doDtlsTimeout) { |
499 | | dtls_timeout = 0; |
500 | | } |
501 | | else { |
502 | | #ifndef NO_ASN_TIME |
503 | | if (start == 0) { |
504 | | start = LowResTimer(); |
505 | | } |
506 | | else { |
507 | | dtls_timeout -= LowResTimer() - start; |
508 | | start = LowResTimer(); |
509 | | if (dtls_timeout < 0 || dtls_timeout > DTLS_TIMEOUT_MAX) |
510 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
511 | | } |
512 | | #endif |
513 | | } |
514 | | |
515 | | if (!wolfSSL_get_using_nonblock(ssl)) { |
516 | | #ifdef USE_WINDOWS_API |
517 | | DWORD timeout = dtls_timeout * 1000; |
518 | | #ifdef WOLFSSL_DTLS13 |
519 | | if (wolfSSL_dtls13_use_quick_timeout(ssl) && |
520 | | IsAtLeastTLSv1_3(ssl->version)) |
521 | | timeout /= 4; |
522 | | #endif /* WOLFSSL_DTLS13 */ |
523 | | #else |
524 | | struct timeval timeout; |
525 | | XMEMSET(&timeout, 0, sizeof(timeout)); |
526 | | #ifdef WOLFSSL_DTLS13 |
527 | | if (wolfSSL_dtls13_use_quick_timeout(ssl) && |
528 | | IsAtLeastTLSv1_3(ssl->version)) { |
529 | | if (dtls_timeout >= 4) |
530 | | timeout.tv_sec = dtls_timeout / 4; |
531 | | else |
532 | | timeout.tv_usec = dtls_timeout * 1000000 / 4; |
533 | | } |
534 | | else |
535 | | #endif /* WOLFSSL_DTLS13 */ |
536 | | timeout.tv_sec = dtls_timeout; |
537 | | #endif |
538 | | if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, |
539 | | sizeof(timeout)) != 0) { |
540 | | WOLFSSL_MSG("setsockopt rcvtimeo failed"); |
541 | | } |
542 | | } |
543 | | #ifndef NO_ASN_TIME |
544 | | else if (IsSCR(ssl)) { |
545 | | if (ssl->dtls_start_timeout && |
546 | | LowResTimer() - ssl->dtls_start_timeout > |
547 | | (word32)dtls_timeout) { |
548 | | ssl->dtls_start_timeout = 0; |
549 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
550 | | } |
551 | | else if (!ssl->dtls_start_timeout) { |
552 | | ssl->dtls_start_timeout = LowResTimer(); |
553 | | } |
554 | | } |
555 | | #endif /* !NO_ASN_TIME */ |
556 | | |
557 | | recvd = (int)DTLS_RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags, |
558 | | (SOCKADDR*)peer, peer != NULL ? &peerSz : NULL); |
559 | | |
560 | | /* From the RECV(2) man page |
561 | | * The returned address is truncated if the buffer provided is too |
562 | | * small; in this case, addrlen will return a value greater than was |
563 | | * supplied to the call. |
564 | | */ |
565 | | if (dtlsCtx->connected) { |
566 | | /* No need to sanitize the value of peerSz */ |
567 | | } |
568 | | else if (dtlsCtx->userSet) { |
569 | | /* Truncate peer size */ |
570 | | if (peerSz > (XSOCKLENT)sizeof(lclPeer)) |
571 | | peerSz = (XSOCKLENT)sizeof(lclPeer); |
572 | | } |
573 | | else { |
574 | | /* Truncate peer size */ |
575 | | if (peerSz > (XSOCKLENT)dtlsCtx->peer.bufSz) |
576 | | peerSz = (XSOCKLENT)dtlsCtx->peer.bufSz; |
577 | | } |
578 | | |
579 | | recvd = TranslateReturnCode(recvd, sd); |
580 | | |
581 | | if (recvd < 0) { |
582 | | WOLFSSL_MSG("Embed Receive From error"); |
583 | | recvd = TranslateIoError(recvd); |
584 | | if (recvd == WOLFSSL_CBIO_ERR_WANT_READ && |
585 | | !wolfSSL_dtls_get_using_nonblock(ssl)) { |
586 | | recvd = WOLFSSL_CBIO_ERR_TIMEOUT; |
587 | | } |
588 | | return recvd; |
589 | | } |
590 | | else if (recvd == 0) { |
591 | | if (!isDGramSock(sd)) { |
592 | | /* Closed TCP connection */ |
593 | | recvd = WOLFSSL_CBIO_ERR_CONN_CLOSE; |
594 | | } |
595 | | else { |
596 | | WOLFSSL_MSG("Ignoring 0-length datagram"); |
597 | | continue; |
598 | | } |
599 | | return recvd; |
600 | | } |
601 | | else if (dtlsCtx->connected) { |
602 | | /* Nothing to do */ |
603 | | } |
604 | | else if (dtlsCtx->userSet) { |
605 | | /* Check we received the packet from the correct peer */ |
606 | | if (dtlsCtx->peer.sz > 0 && |
607 | | (peerSz != (XSOCKLENT)dtlsCtx->peer.sz || |
608 | | !sockAddrEqual(peer, peerSz, (SOCKADDR_S*)dtlsCtx->peer.sa, |
609 | | dtlsCtx->peer.sz))) { |
610 | | WOLFSSL_MSG(" Ignored packet from invalid peer"); |
611 | | #if defined(NO_ASN_TIME) && \ |
612 | | !defined(DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER) |
613 | | if (doDtlsTimeout) { |
614 | | invalidPeerPackets++; |
615 | | if (invalidPeerPackets > DTLS_RECEIVEFROM_MAX_INVALID_PEER) |
616 | | return wolfSSL_dtls_get_using_nonblock(ssl) |
617 | | ? WOLFSSL_CBIO_ERR_WANT_READ |
618 | | : WOLFSSL_CBIO_ERR_TIMEOUT; |
619 | | } |
620 | | #endif /* NO_ASN_TIME && !DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER */ |
621 | | continue; |
622 | | } |
623 | | } |
624 | | else { |
625 | | /* Store size of saved address */ |
626 | | dtlsCtx->peer.sz = peerSz; |
627 | | } |
628 | | #ifndef NO_ASN_TIME |
629 | | ssl->dtls_start_timeout = 0; |
630 | | #endif /* !NO_ASN_TIME */ |
631 | | break; |
632 | | } while (1); |
633 | | |
634 | | return recvd; |
635 | | } |
636 | | |
637 | | |
638 | | /* The send embedded callback |
639 | | * return : nb bytes sent, or error |
640 | | */ |
641 | | int EmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
642 | | { |
643 | | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
644 | | int sd = dtlsCtx->wfd; |
645 | | int sent; |
646 | | const SOCKADDR_S* peer = NULL; |
647 | | XSOCKLENT peerSz = 0; |
648 | | |
649 | | WOLFSSL_ENTER("EmbedSendTo"); |
650 | | |
651 | | if (!isDGramSock(sd)) { |
652 | | /* Probably a TCP socket. peer and peerSz MUST be NULL and 0 */ |
653 | | } |
654 | | else if (!dtlsCtx->connected) { |
655 | | peer = (const SOCKADDR_S*)dtlsCtx->peer.sa; |
656 | | peerSz = dtlsCtx->peer.sz; |
657 | | #ifndef WOLFSSL_IPV6 |
658 | | if (PeerIsIpv6(peer, peerSz)) { |
659 | | WOLFSSL_MSG("ipv6 dtls peer set but no ipv6 support compiled"); |
660 | | return NOT_COMPILED_IN; |
661 | | } |
662 | | #endif |
663 | | } |
664 | | |
665 | | sent = (int)DTLS_SENDTO_FUNCTION(sd, buf, sz, ssl->wflags, |
666 | | (const SOCKADDR*)peer, peerSz); |
667 | | |
668 | | sent = TranslateReturnCode(sent, sd); |
669 | | |
670 | | if (sent < 0) { |
671 | | WOLFSSL_MSG("Embed Send To error"); |
672 | | return TranslateIoError(sent); |
673 | | } |
674 | | |
675 | | return sent; |
676 | | } |
677 | | |
678 | | |
679 | | #ifdef WOLFSSL_MULTICAST |
680 | | |
681 | | /* The alternate receive embedded callback for Multicast |
682 | | * return : nb bytes read, or error |
683 | | */ |
684 | | int EmbedReceiveFromMcast(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
685 | | { |
686 | | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
687 | | int recvd; |
688 | | int sd = dtlsCtx->rfd; |
689 | | |
690 | | WOLFSSL_ENTER("EmbedReceiveFromMcast"); |
691 | | |
692 | | recvd = (int)DTLS_RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags, NULL, NULL); |
693 | | |
694 | | recvd = TranslateReturnCode(recvd, sd); |
695 | | |
696 | | if (recvd < 0) { |
697 | | WOLFSSL_MSG("Embed Receive From error"); |
698 | | recvd = TranslateIoError(recvd); |
699 | | if (recvd == WOLFSSL_CBIO_ERR_WANT_READ && |
700 | | !wolfSSL_dtls_get_using_nonblock(ssl)) { |
701 | | recvd = WOLFSSL_CBIO_ERR_TIMEOUT; |
702 | | } |
703 | | return recvd; |
704 | | } |
705 | | |
706 | | return recvd; |
707 | | } |
708 | | #endif /* WOLFSSL_MULTICAST */ |
709 | | |
710 | | |
711 | | /* The DTLS Generate Cookie callback |
712 | | * return : number of bytes copied into buf, or error |
713 | | */ |
714 | | int EmbedGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx) |
715 | | { |
716 | | int sd = ssl->wfd; |
717 | | SOCKADDR_S peer; |
718 | | XSOCKLENT peerSz = sizeof(peer); |
719 | | byte digest[WC_SHA256_DIGEST_SIZE]; |
720 | | int ret = 0; |
721 | | |
722 | | (void)ctx; |
723 | | |
724 | | XMEMSET(&peer, 0, sizeof(peer)); |
725 | | if (getpeername(sd, (SOCKADDR*)&peer, &peerSz) != 0) { |
726 | | WOLFSSL_MSG("getpeername failed in EmbedGenerateCookie"); |
727 | | return GEN_COOKIE_E; |
728 | | } |
729 | | |
730 | | ret = wc_Sha256Hash((byte*)&peer, peerSz, digest); |
731 | | if (ret != 0) |
732 | | return ret; |
733 | | |
734 | | if (sz > WC_SHA256_DIGEST_SIZE) |
735 | | sz = WC_SHA256_DIGEST_SIZE; |
736 | | XMEMCPY(buf, digest, sz); |
737 | | |
738 | | return sz; |
739 | | } |
740 | | #endif /* WOLFSSL_DTLS */ |
741 | | |
742 | | #ifdef WOLFSSL_SESSION_EXPORT |
743 | | |
744 | | #ifdef WOLFSSL_DTLS |
745 | | static int EmbedGetPeerDTLS(WOLFSSL* ssl, char* ip, int* ipSz, |
746 | | unsigned short* port, int* fam) |
747 | | { |
748 | | SOCKADDR_S peer; |
749 | | word32 peerSz; |
750 | | int ret; |
751 | | |
752 | | /* get peer information stored in ssl struct */ |
753 | | peerSz = sizeof(SOCKADDR_S); |
754 | | if ((ret = wolfSSL_dtls_get_peer(ssl, (void*)&peer, &peerSz)) |
755 | | != WOLFSSL_SUCCESS) { |
756 | | return ret; |
757 | | } |
758 | | |
759 | | /* extract family, ip, and port */ |
760 | | *fam = ((SOCKADDR_S*)&peer)->ss_family; |
761 | | switch (*fam) { |
762 | | case WOLFSSL_IP4: |
763 | | if (XINET_NTOP(*fam, &(((SOCKADDR_IN*)&peer)->sin_addr), |
764 | | ip, *ipSz) == NULL) { |
765 | | WOLFSSL_MSG("XINET_NTOP error"); |
766 | | return SOCKET_ERROR_E; |
767 | | } |
768 | | *port = XNTOHS(((SOCKADDR_IN*)&peer)->sin_port); |
769 | | break; |
770 | | |
771 | | case WOLFSSL_IP6: |
772 | | #ifdef WOLFSSL_IPV6 |
773 | | if (XINET_NTOP(*fam, &(((SOCKADDR_IN6*)&peer)->sin6_addr), |
774 | | ip, *ipSz) == NULL) { |
775 | | WOLFSSL_MSG("XINET_NTOP error"); |
776 | | return SOCKET_ERROR_E; |
777 | | } |
778 | | *port = XNTOHS(((SOCKADDR_IN6*)&peer)->sin6_port); |
779 | | #endif /* WOLFSSL_IPV6 */ |
780 | | break; |
781 | | |
782 | | default: |
783 | | WOLFSSL_MSG("Unknown family type"); |
784 | | return SOCKET_ERROR_E; |
785 | | } |
786 | | ip[*ipSz - 1] = '\0'; /* make sure has terminator */ |
787 | | *ipSz = (word16)XSTRLEN(ip); |
788 | | |
789 | | return WOLFSSL_SUCCESS; |
790 | | } |
791 | | |
792 | | static int EmbedSetPeerDTLS(WOLFSSL* ssl, char* ip, int ipSz, |
793 | | unsigned short port, int fam) |
794 | | { |
795 | | int ret; |
796 | | SOCKADDR_S addr; |
797 | | |
798 | | /* sanity checks on arguments */ |
799 | | if (ssl == NULL || ip == NULL || ipSz < 0 || ipSz > MAX_EXPORT_IP) { |
800 | | return BAD_FUNC_ARG; |
801 | | } |
802 | | |
803 | | addr.ss_family = fam; |
804 | | switch (addr.ss_family) { |
805 | | case WOLFSSL_IP4: |
806 | | if (XINET_PTON(addr.ss_family, ip, |
807 | | &(((SOCKADDR_IN*)&addr)->sin_addr)) <= 0) { |
808 | | WOLFSSL_MSG("XINET_PTON error"); |
809 | | return SOCKET_ERROR_E; |
810 | | } |
811 | | ((SOCKADDR_IN*)&addr)->sin_port = XHTONS(port); |
812 | | |
813 | | /* peer sa is free'd in SSL_ResourceFree */ |
814 | | if ((ret = wolfSSL_dtls_set_peer(ssl, (SOCKADDR_IN*)&addr, |
815 | | sizeof(SOCKADDR_IN)))!= WOLFSSL_SUCCESS) { |
816 | | WOLFSSL_MSG("Import DTLS peer info error"); |
817 | | return ret; |
818 | | } |
819 | | break; |
820 | | |
821 | | case WOLFSSL_IP6: |
822 | | #ifdef WOLFSSL_IPV6 |
823 | | if (XINET_PTON(addr.ss_family, ip, |
824 | | &(((SOCKADDR_IN6*)&addr)->sin6_addr)) <= 0) { |
825 | | WOLFSSL_MSG("XINET_PTON error"); |
826 | | return SOCKET_ERROR_E; |
827 | | } |
828 | | ((SOCKADDR_IN6*)&addr)->sin6_port = XHTONS(port); |
829 | | |
830 | | /* peer sa is free'd in SSL_ResourceFree */ |
831 | | if ((ret = wolfSSL_dtls_set_peer(ssl, (SOCKADDR_IN6*)&addr, |
832 | | sizeof(SOCKADDR_IN6)))!= WOLFSSL_SUCCESS) { |
833 | | WOLFSSL_MSG("Import DTLS peer info error"); |
834 | | return ret; |
835 | | } |
836 | | #endif /* WOLFSSL_IPV6 */ |
837 | | break; |
838 | | |
839 | | default: |
840 | | WOLFSSL_MSG("Unknown address family"); |
841 | | return BUFFER_E; |
842 | | } |
843 | | |
844 | | return WOLFSSL_SUCCESS; |
845 | | } |
846 | | #endif |
847 | | |
848 | | /* get the peer information in human readable form (ip, port, family) |
849 | | * default function assumes BSD sockets |
850 | | * can be overridden with wolfSSL_CTX_SetIOGetPeer |
851 | | */ |
852 | | int EmbedGetPeer(WOLFSSL* ssl, char* ip, int* ipSz, |
853 | | unsigned short* port, int* fam) |
854 | | { |
855 | | if (ssl == NULL || ip == NULL || ipSz == NULL || |
856 | | port == NULL || fam == NULL) { |
857 | | return BAD_FUNC_ARG; |
858 | | } |
859 | | |
860 | | if (ssl->options.dtls) { |
861 | | #ifdef WOLFSSL_DTLS |
862 | | return EmbedGetPeerDTLS(ssl, ip, ipSz, port, fam); |
863 | | #else |
864 | | return NOT_COMPILED_IN; |
865 | | #endif |
866 | | } |
867 | | else { |
868 | | *port = wolfSSL_get_fd(ssl); |
869 | | ip[0] = '\0'; |
870 | | *ipSz = 0; |
871 | | *fam = 0; |
872 | | return WOLFSSL_SUCCESS; |
873 | | } |
874 | | } |
875 | | |
876 | | /* set the peer information in human readable form (ip, port, family) |
877 | | * default function assumes BSD sockets |
878 | | * can be overridden with wolfSSL_CTX_SetIOSetPeer |
879 | | */ |
880 | | int EmbedSetPeer(WOLFSSL* ssl, char* ip, int ipSz, |
881 | | unsigned short port, int fam) |
882 | | { |
883 | | /* sanity checks on arguments */ |
884 | | if (ssl == NULL || ip == NULL || ipSz < 0 || ipSz > MAX_EXPORT_IP) { |
885 | | return BAD_FUNC_ARG; |
886 | | } |
887 | | |
888 | | if (ssl->options.dtls) { |
889 | | #ifdef WOLFSSL_DTLS |
890 | | return EmbedSetPeerDTLS(ssl, ip, ipSz, port, fam); |
891 | | #else |
892 | | return NOT_COMPILED_IN; |
893 | | #endif |
894 | | } |
895 | | else { |
896 | | wolfSSL_set_fd(ssl, port); |
897 | | (void)fam; |
898 | | return WOLFSSL_SUCCESS; |
899 | | } |
900 | | } |
901 | | #endif /* WOLFSSL_SESSION_EXPORT */ |
902 | | |
903 | | #ifdef WOLFSSL_LINUXKM |
904 | | static int linuxkm_send(struct socket *socket, void *buf, int size, |
905 | | unsigned int flags) |
906 | | { |
907 | | int ret; |
908 | | struct kvec vec = { .iov_base = buf, .iov_len = size }; |
909 | | struct msghdr msg = { .msg_flags = flags }; |
910 | | ret = kernel_sendmsg(socket, &msg, &vec, 1, size); |
911 | | return ret; |
912 | | } |
913 | | |
914 | | static int linuxkm_recv(struct socket *socket, void *buf, int size, |
915 | | unsigned int flags) |
916 | | { |
917 | | int ret; |
918 | | struct kvec vec = { .iov_base = buf, .iov_len = size }; |
919 | | struct msghdr msg = { .msg_flags = flags }; |
920 | | ret = kernel_recvmsg(socket, &msg, &vec, 1, size, msg.msg_flags); |
921 | | return ret; |
922 | | } |
923 | | #endif /* WOLFSSL_LINUXKM */ |
924 | | |
925 | | |
926 | | int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags) |
927 | 0 | { |
928 | 0 | int recvd; |
929 | |
|
930 | 0 | recvd = (int)RECV_FUNCTION(sd, buf, sz, rdFlags); |
931 | 0 | recvd = TranslateReturnCode(recvd, (int)sd); |
932 | |
|
933 | 0 | return recvd; |
934 | 0 | } |
935 | | |
936 | | int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags) |
937 | 0 | { |
938 | 0 | int sent; |
939 | |
|
940 | 0 | sent = (int)SEND_FUNCTION(sd, buf, sz, wrFlags); |
941 | 0 | sent = TranslateReturnCode(sent, (int)sd); |
942 | |
|
943 | 0 | return sent; |
944 | 0 | } |
945 | | |
946 | | #endif /* USE_WOLFSSL_IO */ |
947 | | |
948 | | |
949 | | #ifdef HAVE_HTTP_CLIENT |
950 | | |
951 | | #ifndef HAVE_IO_TIMEOUT |
952 | | #define io_timeout_sec 0 |
953 | | #else |
954 | | |
955 | | #ifndef DEFAULT_TIMEOUT_SEC |
956 | | #define DEFAULT_TIMEOUT_SEC 0 /* no timeout */ |
957 | | #endif |
958 | | |
959 | | static int io_timeout_sec = DEFAULT_TIMEOUT_SEC; |
960 | | |
961 | | void wolfIO_SetTimeout(int to_sec) |
962 | | { |
963 | | io_timeout_sec = to_sec; |
964 | | } |
965 | | |
966 | | int wolfIO_SetBlockingMode(SOCKET_T sockfd, int non_blocking) |
967 | | { |
968 | | int ret = 0; |
969 | | |
970 | | #ifdef USE_WINDOWS_API |
971 | | unsigned long blocking = non_blocking; |
972 | | ret = ioctlsocket(sockfd, FIONBIO, &blocking); |
973 | | if (ret == SOCKET_ERROR) |
974 | | ret = -1; |
975 | | #else |
976 | | ret = fcntl(sockfd, F_GETFL, 0); |
977 | | if (ret >= 0) { |
978 | | if (non_blocking) |
979 | | ret |= O_NONBLOCK; |
980 | | else |
981 | | ret &= ~O_NONBLOCK; |
982 | | ret = fcntl(sockfd, F_SETFL, ret); |
983 | | } |
984 | | #endif |
985 | | if (ret < 0) { |
986 | | WOLFSSL_MSG("wolfIO_SetBlockingMode failed"); |
987 | | } |
988 | | |
989 | | return ret; |
990 | | } |
991 | | |
992 | | int wolfIO_Select(SOCKET_T sockfd, int to_sec) |
993 | | { |
994 | | fd_set rfds, wfds; |
995 | | int nfds = 0; |
996 | | struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0}; |
997 | | int ret; |
998 | | |
999 | | #ifndef USE_WINDOWS_API |
1000 | | nfds = (int)sockfd + 1; |
1001 | | |
1002 | | if ((sockfd < 0) || (sockfd >= FD_SETSIZE)) { |
1003 | | WOLFSSL_MSG("socket fd out of FDSET range"); |
1004 | | return -1; |
1005 | | } |
1006 | | #endif |
1007 | | |
1008 | | FD_ZERO(&rfds); |
1009 | | FD_SET(sockfd, &rfds); |
1010 | | wfds = rfds; |
1011 | | |
1012 | | ret = select(nfds, &rfds, &wfds, NULL, &timeout); |
1013 | | if (ret == 0) { |
1014 | | #ifdef DEBUG_HTTP |
1015 | | fprintf(stderr, "Timeout: %d\n", ret); |
1016 | | #endif |
1017 | | return HTTP_TIMEOUT; |
1018 | | } |
1019 | | else if (ret > 0) { |
1020 | | if (FD_ISSET(sockfd, &wfds)) { |
1021 | | if (!FD_ISSET(sockfd, &rfds)) { |
1022 | | return 0; |
1023 | | } |
1024 | | } |
1025 | | } |
1026 | | |
1027 | | WOLFSSL_MSG("Select error"); |
1028 | | return SOCKET_ERROR_E; |
1029 | | } |
1030 | | #endif /* HAVE_IO_TIMEOUT */ |
1031 | | |
1032 | | static int wolfIO_Word16ToString(char* d, word16 number) |
1033 | | { |
1034 | | int i = 0; |
1035 | | word16 order = 10000; |
1036 | | word16 digit; |
1037 | | |
1038 | | if (d == NULL) |
1039 | | return i; |
1040 | | |
1041 | | if (number == 0) |
1042 | | d[i++] = '0'; |
1043 | | else { |
1044 | | while (order) { |
1045 | | digit = number / order; |
1046 | | if (i > 0 || digit != 0) |
1047 | | d[i++] = (char)digit + '0'; |
1048 | | if (digit != 0) |
1049 | | number %= digit * order; |
1050 | | |
1051 | | order = (order > 1) ? order / 10 : 0; |
1052 | | } |
1053 | | } |
1054 | | d[i] = 0; /* null terminate */ |
1055 | | |
1056 | | return i; |
1057 | | } |
1058 | | |
1059 | | int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip, word16 port, int to_sec) |
1060 | | { |
1061 | | #ifdef HAVE_SOCKADDR |
1062 | | int ret = 0; |
1063 | | SOCKADDR_S addr; |
1064 | | int sockaddr_len; |
1065 | | #if defined(HAVE_GETADDRINFO) |
1066 | | /* use getaddrinfo */ |
1067 | | ADDRINFO hints; |
1068 | | ADDRINFO* answer = NULL; |
1069 | | char strPort[6]; |
1070 | | #else |
1071 | | /* use gethostbyname */ |
1072 | | #if !defined(WOLFSSL_USE_POPEN_HOST) |
1073 | | #if defined(__GLIBC__) && (__GLIBC__ >= 2) && defined(__USE_MISC) && \ |
1074 | | !defined(SINGLE_THREADED) |
1075 | | HOSTENT entry_buf, *entry = NULL; |
1076 | | char *ghbn_r_buf = NULL; |
1077 | | int ghbn_r_errno; |
1078 | | #else |
1079 | | HOSTENT *entry; |
1080 | | #endif |
1081 | | #endif |
1082 | | #ifdef WOLFSSL_IPV6 |
1083 | | SOCKADDR_IN6 *sin; |
1084 | | #else |
1085 | | SOCKADDR_IN *sin; |
1086 | | #endif |
1087 | | #endif /* HAVE_SOCKADDR */ |
1088 | | |
1089 | | if (sockfd == NULL || ip == NULL) { |
1090 | | return -1; |
1091 | | } |
1092 | | |
1093 | | #if !defined(HAVE_GETADDRINFO) |
1094 | | #ifdef WOLFSSL_IPV6 |
1095 | | sockaddr_len = sizeof(SOCKADDR_IN6); |
1096 | | #else |
1097 | | sockaddr_len = sizeof(SOCKADDR_IN); |
1098 | | #endif |
1099 | | #endif |
1100 | | XMEMSET(&addr, 0, sizeof(addr)); |
1101 | | |
1102 | | #ifdef WOLFIO_DEBUG |
1103 | | printf("TCP Connect: %s:%d\n", ip, port); |
1104 | | #endif |
1105 | | |
1106 | | /* use gethostbyname for c99 */ |
1107 | | #if defined(HAVE_GETADDRINFO) |
1108 | | XMEMSET(&hints, 0, sizeof(hints)); |
1109 | | #ifdef WOLFSSL_IPV6 |
1110 | | hints.ai_family = AF_UNSPEC; /* detect IPv4 or IPv6 */ |
1111 | | #else |
1112 | | hints.ai_family = AF_INET; /* detect only IPv4 */ |
1113 | | #endif |
1114 | | hints.ai_socktype = SOCK_STREAM; |
1115 | | hints.ai_protocol = IPPROTO_TCP; |
1116 | | |
1117 | | if (wolfIO_Word16ToString(strPort, port) == 0) { |
1118 | | WOLFSSL_MSG("invalid port number for responder"); |
1119 | | return -1; |
1120 | | } |
1121 | | |
1122 | | if (getaddrinfo(ip, strPort, &hints, &answer) < 0 || answer == NULL) { |
1123 | | WOLFSSL_MSG("no addr info for responder"); |
1124 | | return -1; |
1125 | | } |
1126 | | |
1127 | | sockaddr_len = answer->ai_addrlen; |
1128 | | XMEMCPY(&addr, answer->ai_addr, sockaddr_len); |
1129 | | freeaddrinfo(answer); |
1130 | | #elif defined(WOLFSSL_USE_POPEN_HOST) && !defined(WOLFSSL_IPV6) |
1131 | | { |
1132 | | char host_ipaddr[4] = { 127, 0, 0, 1 }; |
1133 | | int found = 1; |
1134 | | |
1135 | | if ((XSTRNCMP(ip, "localhost", 10) != 0) && |
1136 | | (XSTRNCMP(ip, "127.0.0.1", 10) != 0)) { |
1137 | | FILE* fp; |
1138 | | char host_out[100]; |
1139 | | char cmd[100]; |
1140 | | |
1141 | | XSTRNCPY(cmd, "host ", 6); |
1142 | | XSTRNCAT(cmd, ip, 99 - XSTRLEN(cmd)); |
1143 | | found = 0; |
1144 | | fp = popen(cmd, "r"); |
1145 | | if (fp != NULL) { |
1146 | | while (fgets(host_out, sizeof(host_out), fp) != NULL) { |
1147 | | int i; |
1148 | | int j = 0; |
1149 | | for (j = 0; host_out[j] != '\0'; j++) { |
1150 | | if ((host_out[j] >= '0') && (host_out[j] <= '9')) { |
1151 | | break; |
1152 | | } |
1153 | | } |
1154 | | found = (host_out[j] >= '0') && (host_out[j] <= '9'); |
1155 | | if (!found) { |
1156 | | continue; |
1157 | | } |
1158 | | |
1159 | | for (i = 0; i < 4; i++) { |
1160 | | host_ipaddr[i] = atoi(host_out + j); |
1161 | | while ((host_out[j] >= '0') && (host_out[j] <= '9')) { |
1162 | | j++; |
1163 | | } |
1164 | | if (host_out[j] == '.') { |
1165 | | j++; |
1166 | | found &= (i != 3); |
1167 | | } |
1168 | | else { |
1169 | | found &= (i == 3); |
1170 | | break; |
1171 | | } |
1172 | | } |
1173 | | if (found) { |
1174 | | break; |
1175 | | } |
1176 | | } |
1177 | | pclose(fp); |
1178 | | } |
1179 | | } |
1180 | | if (found) { |
1181 | | sin = (SOCKADDR_IN *)&addr; |
1182 | | sin->sin_family = AF_INET; |
1183 | | sin->sin_port = XHTONS(port); |
1184 | | XMEMCPY(&sin->sin_addr.s_addr, host_ipaddr, sizeof(host_ipaddr)); |
1185 | | } |
1186 | | else { |
1187 | | WOLFSSL_MSG("no addr info for responder"); |
1188 | | return -1; |
1189 | | } |
1190 | | } |
1191 | | #else |
1192 | | #if defined(__GLIBC__) && (__GLIBC__ >= 2) && defined(__USE_MISC) && \ |
1193 | | !defined(SINGLE_THREADED) |
1194 | | /* 2048 is a magic number that empirically works. the header and |
1195 | | * documentation provide no guidance on appropriate buffer size other than |
1196 | | * "if buf is too small, the functions will return ERANGE, and the call |
1197 | | * should be retried with a larger buffer." |
1198 | | */ |
1199 | | ghbn_r_buf = (char *)XMALLOC(2048, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1200 | | if (ghbn_r_buf != NULL) { |
1201 | | gethostbyname_r(ip, &entry_buf, ghbn_r_buf, 2048, &entry, &ghbn_r_errno); |
1202 | | } |
1203 | | #else |
1204 | | entry = gethostbyname(ip); |
1205 | | #endif |
1206 | | |
1207 | | if (entry) { |
1208 | | #ifdef WOLFSSL_IPV6 |
1209 | | sin = (SOCKADDR_IN6 *)&addr; |
1210 | | sin->sin6_family = AF_INET6; |
1211 | | sin->sin6_port = XHTONS(port); |
1212 | | XMEMCPY(&sin->sin6_addr, entry->h_addr_list[0], entry->h_length); |
1213 | | #else |
1214 | | sin = (SOCKADDR_IN *)&addr; |
1215 | | sin->sin_family = AF_INET; |
1216 | | sin->sin_port = XHTONS(port); |
1217 | | XMEMCPY(&sin->sin_addr.s_addr, entry->h_addr_list[0], entry->h_length); |
1218 | | #endif |
1219 | | } |
1220 | | |
1221 | | #if defined(__GLIBC__) && (__GLIBC__ >= 2) && defined(__USE_MISC) && \ |
1222 | | !defined(SINGLE_THREADED) |
1223 | | XFREE(ghbn_r_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1224 | | #endif |
1225 | | |
1226 | | if (entry == NULL) { |
1227 | | WOLFSSL_MSG("no addr info for responder"); |
1228 | | return -1; |
1229 | | } |
1230 | | #endif |
1231 | | |
1232 | | *sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM, 0); |
1233 | | #ifdef USE_WINDOWS_API |
1234 | | if (*sockfd == SOCKET_INVALID) |
1235 | | #else |
1236 | | if (*sockfd <= SOCKET_INVALID) |
1237 | | #endif |
1238 | | { |
1239 | | WOLFSSL_MSG("bad socket fd, out of fds?"); |
1240 | | *sockfd = SOCKET_INVALID; |
1241 | | return -1; |
1242 | | } |
1243 | | |
1244 | | #ifdef HAVE_IO_TIMEOUT |
1245 | | /* if timeout value provided then set socket non-blocking */ |
1246 | | if (to_sec > 0) { |
1247 | | wolfIO_SetBlockingMode(*sockfd, 1); |
1248 | | } |
1249 | | #else |
1250 | | (void)to_sec; |
1251 | | #endif |
1252 | | |
1253 | | ret = connect(*sockfd, (SOCKADDR *)&addr, sockaddr_len); |
1254 | | #ifdef HAVE_IO_TIMEOUT |
1255 | | if ((ret != 0) && (to_sec > 0)) { |
1256 | | #ifdef USE_WINDOWS_API |
1257 | | if ((ret == SOCKET_ERROR) && (wolfSSL_LastError(ret) == WSAEWOULDBLOCK)) |
1258 | | #else |
1259 | | if (errno == EINPROGRESS) |
1260 | | #endif |
1261 | | { |
1262 | | /* wait for connect to complete */ |
1263 | | ret = wolfIO_Select(*sockfd, to_sec); |
1264 | | |
1265 | | /* restore blocking mode */ |
1266 | | wolfIO_SetBlockingMode(*sockfd, 0); |
1267 | | } |
1268 | | } |
1269 | | #endif |
1270 | | if (ret != 0) { |
1271 | | WOLFSSL_MSG("Responder tcp connect failed"); |
1272 | | CloseSocket(*sockfd); |
1273 | | *sockfd = SOCKET_INVALID; |
1274 | | return -1; |
1275 | | } |
1276 | | return ret; |
1277 | | #else |
1278 | | (void)sockfd; |
1279 | | (void)ip; |
1280 | | (void)port; |
1281 | | (void)to_sec; |
1282 | | return -1; |
1283 | | #endif /* HAVE_SOCKADDR */ |
1284 | | } |
1285 | | |
1286 | | int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port) |
1287 | | { |
1288 | | #ifdef HAVE_SOCKADDR |
1289 | | int ret = 0; |
1290 | | SOCKADDR_S addr; |
1291 | | int sockaddr_len = sizeof(SOCKADDR_IN); |
1292 | | SOCKADDR_IN *sin = (SOCKADDR_IN *)&addr; |
1293 | | |
1294 | | if (sockfd == NULL || port < 1) { |
1295 | | return -1; |
1296 | | } |
1297 | | |
1298 | | XMEMSET(&addr, 0, sizeof(addr)); |
1299 | | |
1300 | | sin->sin_family = AF_INET; |
1301 | | sin->sin_addr.s_addr = INADDR_ANY; |
1302 | | sin->sin_port = XHTONS(port); |
1303 | | *sockfd = (SOCKET_T)socket(AF_INET, SOCK_STREAM, 0); |
1304 | | |
1305 | | #ifdef USE_WINDOWS_API |
1306 | | if (*sockfd == SOCKET_INVALID) |
1307 | | #else |
1308 | | if (*sockfd <= SOCKET_INVALID) |
1309 | | #endif |
1310 | | { |
1311 | | WOLFSSL_MSG("socket failed"); |
1312 | | *sockfd = SOCKET_INVALID; |
1313 | | return -1; |
1314 | | } |
1315 | | |
1316 | | #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\ |
1317 | | && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR) |
1318 | | { |
1319 | | int optval = 1; |
1320 | | XSOCKLENT optlen = sizeof(optval); |
1321 | | ret = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, optlen); |
1322 | | } |
1323 | | #endif |
1324 | | |
1325 | | if (ret == 0) |
1326 | | ret = bind(*sockfd, (SOCKADDR *)sin, sockaddr_len); |
1327 | | if (ret == 0) |
1328 | | ret = listen(*sockfd, SOMAXCONN); |
1329 | | |
1330 | | if (ret != 0) { |
1331 | | WOLFSSL_MSG("wolfIO_TcpBind failed"); |
1332 | | CloseSocket(*sockfd); |
1333 | | *sockfd = SOCKET_INVALID; |
1334 | | ret = -1; |
1335 | | } |
1336 | | |
1337 | | return ret; |
1338 | | #else |
1339 | | (void)sockfd; |
1340 | | (void)port; |
1341 | | return -1; |
1342 | | #endif /* HAVE_SOCKADDR */ |
1343 | | } |
1344 | | |
1345 | | #ifdef HAVE_SOCKADDR |
1346 | | int wolfIO_TcpAccept(SOCKET_T sockfd, SOCKADDR* peer_addr, XSOCKLENT* peer_len) |
1347 | | { |
1348 | | return (int)accept(sockfd, peer_addr, peer_len); |
1349 | | } |
1350 | | #endif /* HAVE_SOCKADDR */ |
1351 | | |
1352 | | #ifndef HTTP_SCRATCH_BUFFER_SIZE |
1353 | | #define HTTP_SCRATCH_BUFFER_SIZE 512 |
1354 | | #endif |
1355 | | #ifndef MAX_URL_ITEM_SIZE |
1356 | | #define MAX_URL_ITEM_SIZE 80 |
1357 | | #endif |
1358 | | |
1359 | | int wolfIO_DecodeUrl(const char* url, int urlSz, char* outName, char* outPath, |
1360 | | word16* outPort) |
1361 | | { |
1362 | | int result = -1; |
1363 | | |
1364 | | if (url == NULL || urlSz == 0) { |
1365 | | if (outName) |
1366 | | *outName = 0; |
1367 | | if (outPath) |
1368 | | *outPath = 0; |
1369 | | if (outPort) |
1370 | | *outPort = 0; |
1371 | | } |
1372 | | else { |
1373 | | int i, cur; |
1374 | | |
1375 | | /* need to break the url down into scheme, address, and port */ |
1376 | | /* "http://example.com:8080/" */ |
1377 | | /* "http://[::1]:443/" */ |
1378 | | if (XSTRNCMP(url, "http://", 7) == 0) { |
1379 | | cur = 7; |
1380 | | } else cur = 0; |
1381 | | |
1382 | | i = 0; |
1383 | | if (url[cur] == '[') { |
1384 | | cur++; |
1385 | | /* copy until ']' */ |
1386 | | while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0 && |
1387 | | url[cur] != ']') { |
1388 | | if (outName) |
1389 | | outName[i] = url[cur]; |
1390 | | i++; cur++; |
1391 | | } |
1392 | | cur++; /* skip ']' */ |
1393 | | } |
1394 | | else { |
1395 | | while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0 && |
1396 | | url[cur] != ':' && url[cur] != '/') { |
1397 | | if (outName) |
1398 | | outName[i] = url[cur]; |
1399 | | i++; cur++; |
1400 | | } |
1401 | | } |
1402 | | if (outName) |
1403 | | outName[i] = 0; |
1404 | | /* Need to pick out the path after the domain name */ |
1405 | | |
1406 | | if (cur < urlSz && url[cur] == ':') { |
1407 | | char port[6]; |
1408 | | int j; |
1409 | | word32 bigPort = 0; |
1410 | | i = 0; |
1411 | | cur++; |
1412 | | while (i < 6 && cur < urlSz && url[cur] != 0 && url[cur] != '/') { |
1413 | | port[i] = url[cur]; |
1414 | | i++; cur++; |
1415 | | } |
1416 | | |
1417 | | for (j = 0; j < i; j++) { |
1418 | | if (port[j] < '0' || port[j] > '9') return -1; |
1419 | | bigPort = (bigPort * 10) + (port[j] - '0'); |
1420 | | } |
1421 | | if (outPort) |
1422 | | *outPort = (word16)bigPort; |
1423 | | } |
1424 | | else if (outPort) |
1425 | | *outPort = 80; |
1426 | | |
1427 | | |
1428 | | if (cur < urlSz && url[cur] == '/') { |
1429 | | i = 0; |
1430 | | while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0) { |
1431 | | if (outPath) |
1432 | | outPath[i] = url[cur]; |
1433 | | i++; cur++; |
1434 | | } |
1435 | | if (outPath) |
1436 | | outPath[i] = 0; |
1437 | | } |
1438 | | else if (outPath) { |
1439 | | outPath[0] = '/'; |
1440 | | outPath[1] = 0; |
1441 | | } |
1442 | | |
1443 | | result = 0; |
1444 | | } |
1445 | | |
1446 | | return result; |
1447 | | } |
1448 | | |
1449 | | static int wolfIO_HttpProcessResponseBuf(int sfd, byte **recvBuf, |
1450 | | int* recvBufSz, int chunkSz, char* start, int len, int dynType, void* heap) |
1451 | | { |
1452 | | byte* newRecvBuf = NULL; |
1453 | | int newRecvSz = *recvBufSz + chunkSz; |
1454 | | int pos = 0; |
1455 | | |
1456 | | WOLFSSL_MSG("Processing HTTP response"); |
1457 | | #ifdef WOLFIO_DEBUG |
1458 | | printf("HTTP Chunk %d->%d\n", *recvBufSz, chunkSz); |
1459 | | #endif |
1460 | | |
1461 | | (void)heap; |
1462 | | (void)dynType; |
1463 | | |
1464 | | if (chunkSz < 0 || len < 0) { |
1465 | | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf invalid chunk or length size"); |
1466 | | return MEMORY_E; |
1467 | | } |
1468 | | |
1469 | | if (newRecvSz <= 0) { |
1470 | | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf new receive size overflow"); |
1471 | | return MEMORY_E; |
1472 | | } |
1473 | | |
1474 | | newRecvBuf = (byte*)XMALLOC(newRecvSz, heap, dynType); |
1475 | | if (newRecvBuf == NULL) { |
1476 | | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf malloc failed"); |
1477 | | return MEMORY_E; |
1478 | | } |
1479 | | |
1480 | | /* if buffer already exists, then we are growing it */ |
1481 | | if (*recvBuf) { |
1482 | | XMEMCPY(&newRecvBuf[pos], *recvBuf, *recvBufSz); |
1483 | | XFREE(*recvBuf, heap, dynType); |
1484 | | pos += *recvBufSz; |
1485 | | *recvBuf = NULL; |
1486 | | } |
1487 | | |
1488 | | /* copy the remainder of the httpBuf into the respBuf */ |
1489 | | if (len != 0) { |
1490 | | if (pos + len <= newRecvSz) { |
1491 | | XMEMCPY(&newRecvBuf[pos], start, len); |
1492 | | pos += len; |
1493 | | } |
1494 | | else { |
1495 | | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf bad size"); |
1496 | | XFREE(newRecvBuf, heap, dynType); |
1497 | | return -1; |
1498 | | } |
1499 | | } |
1500 | | |
1501 | | /* receive the remainder of chunk */ |
1502 | | while (len < chunkSz) { |
1503 | | int rxSz = wolfIO_Recv(sfd, (char*)&newRecvBuf[pos], chunkSz-len, 0); |
1504 | | if (rxSz > 0) { |
1505 | | len += rxSz; |
1506 | | pos += rxSz; |
1507 | | } |
1508 | | else { |
1509 | | WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf recv failed"); |
1510 | | XFREE(newRecvBuf, heap, dynType); |
1511 | | return -1; |
1512 | | } |
1513 | | } |
1514 | | |
1515 | | *recvBuf = newRecvBuf; |
1516 | | *recvBufSz = newRecvSz; |
1517 | | |
1518 | | return 0; |
1519 | | } |
1520 | | |
1521 | | int wolfIO_HttpProcessResponse(int sfd, const char** appStrList, |
1522 | | byte** respBuf, byte* httpBuf, int httpBufSz, int dynType, void* heap) |
1523 | | { |
1524 | | static const char HTTP_PROTO[] = "HTTP/1."; |
1525 | | static const char HTTP_STATUS_200[] = "200"; |
1526 | | int result = 0; |
1527 | | int len = 0; |
1528 | | char *start, *end; |
1529 | | int respBufSz = 0; |
1530 | | int isChunked = 0, chunkSz = 0; |
1531 | | enum phr_state { phr_init, phr_http_start, phr_have_length, phr_have_type, |
1532 | | phr_wait_end, phr_get_chunk_len, phr_get_chunk_data, |
1533 | | phr_http_end |
1534 | | } state = phr_init; |
1535 | | |
1536 | | WOLFSSL_ENTER("wolfIO_HttpProcessResponse"); |
1537 | | |
1538 | | *respBuf = NULL; |
1539 | | start = end = NULL; |
1540 | | do { |
1541 | | if (state == phr_get_chunk_data) { |
1542 | | /* get chunk of data */ |
1543 | | result = wolfIO_HttpProcessResponseBuf(sfd, respBuf, &respBufSz, |
1544 | | chunkSz, start, len, dynType, heap); |
1545 | | |
1546 | | state = (result != 0) ? phr_http_end : phr_get_chunk_len; |
1547 | | end = NULL; |
1548 | | len = 0; |
1549 | | } |
1550 | | |
1551 | | /* read data if no \r\n or first time */ |
1552 | | if ((start == NULL) || (end == NULL)) { |
1553 | | result = wolfIO_Recv(sfd, (char*)httpBuf+len, httpBufSz-len-1, 0); |
1554 | | if (result > 0) { |
1555 | | len += result; |
1556 | | start = (char*)httpBuf; |
1557 | | start[len] = 0; |
1558 | | } |
1559 | | else { |
1560 | | result = TranslateReturnCode(result, sfd); |
1561 | | result = wolfSSL_LastError(result); |
1562 | | if (result == SOCKET_EWOULDBLOCK || result == SOCKET_EAGAIN) { |
1563 | | return OCSP_WANT_READ; |
1564 | | } |
1565 | | |
1566 | | WOLFSSL_MSG("wolfIO_HttpProcessResponse recv http from peer failed"); |
1567 | | return HTTP_RECV_ERR; |
1568 | | } |
1569 | | } |
1570 | | end = XSTRSTR(start, "\r\n"); /* locate end */ |
1571 | | |
1572 | | /* handle incomplete rx */ |
1573 | | if (end == NULL) { |
1574 | | if (len != 0) |
1575 | | XMEMMOVE(httpBuf, start, len); |
1576 | | start = end = NULL; |
1577 | | } |
1578 | | /* when start is "\r\n" */ |
1579 | | else if (end == start) { |
1580 | | /* if waiting for end or need chunk len */ |
1581 | | if (state == phr_wait_end || state == phr_get_chunk_len) { |
1582 | | state = (isChunked) ? phr_get_chunk_len : phr_http_end; |
1583 | | len -= 2; start += 2; /* skip \r\n */ |
1584 | | } |
1585 | | else { |
1586 | | WOLFSSL_MSG("wolfIO_HttpProcessResponse header ended early"); |
1587 | | return HTTP_HEADER_ERR; |
1588 | | } |
1589 | | } |
1590 | | else { |
1591 | | *end = 0; /* null terminate */ |
1592 | | len -= (int)(end - start) + 2; |
1593 | | /* adjust len to remove the first line including the /r/n */ |
1594 | | |
1595 | | #ifdef WOLFIO_DEBUG |
1596 | | printf("HTTP Resp: %s\n", start); |
1597 | | #endif |
1598 | | |
1599 | | switch (state) { |
1600 | | case phr_init: |
1601 | | /* length of "HTTP/1.x 200" == 12*/ |
1602 | | if (XSTRLEN(start) < 12) { |
1603 | | WOLFSSL_MSG("wolfIO_HttpProcessResponse HTTP header " |
1604 | | "too short."); |
1605 | | return HTTP_HEADER_ERR; |
1606 | | } |
1607 | | if (XSTRNCASECMP(start, HTTP_PROTO, |
1608 | | sizeof(HTTP_PROTO) - 1) != 0) { |
1609 | | WOLFSSL_MSG("wolfIO_HttpProcessResponse HTTP header " |
1610 | | "doesn't start with HTTP/1."); |
1611 | | return HTTP_PROTO_ERR; |
1612 | | } |
1613 | | /* +2 for HTTP minor version and space between version and |
1614 | | * status code. */ |
1615 | | start += sizeof(HTTP_PROTO) - 1 + 2 ; |
1616 | | if (XSTRNCASECMP(start, HTTP_STATUS_200, |
1617 | | sizeof(HTTP_STATUS_200) - 1) != 0) { |
1618 | | WOLFSSL_MSG("wolfIO_HttpProcessResponse HTTP header " |
1619 | | "doesn't have status code 200."); |
1620 | | return HTTP_STATUS_ERR; |
1621 | | } |
1622 | | state = phr_http_start; |
1623 | | break; |
1624 | | case phr_http_start: |
1625 | | case phr_have_length: |
1626 | | case phr_have_type: |
1627 | | if (XSTRNCASECMP(start, "Content-Type:", 13) == 0) { |
1628 | | int i; |
1629 | | |
1630 | | start += 13; |
1631 | | while (*start == ' ') start++; |
1632 | | |
1633 | | /* try and match against appStrList */ |
1634 | | i = 0; |
1635 | | while (appStrList[i] != NULL) { |
1636 | | if (XSTRNCASECMP(start, appStrList[i], |
1637 | | XSTRLEN(appStrList[i])) == 0) { |
1638 | | break; |
1639 | | } |
1640 | | i++; |
1641 | | } |
1642 | | if (appStrList[i] == NULL) { |
1643 | | WOLFSSL_MSG("wolfIO_HttpProcessResponse appstr mismatch"); |
1644 | | return HTTP_APPSTR_ERR; |
1645 | | } |
1646 | | state = (state == phr_http_start) ? phr_have_type : phr_wait_end; |
1647 | | } |
1648 | | else if (XSTRNCASECMP(start, "Content-Length:", 15) == 0) { |
1649 | | start += 15; |
1650 | | while (*start == ' ') start++; |
1651 | | chunkSz = XATOI(start); |
1652 | | state = (state == phr_http_start) ? phr_have_length : phr_wait_end; |
1653 | | } |
1654 | | else if (XSTRNCASECMP(start, "Transfer-Encoding:", 18) == 0) { |
1655 | | start += 18; |
1656 | | while (*start == ' ') start++; |
1657 | | if (XSTRNCASECMP(start, "chunked", 7) == 0) { |
1658 | | isChunked = 1; |
1659 | | state = (state == phr_http_start) ? phr_have_length : phr_wait_end; |
1660 | | } |
1661 | | } |
1662 | | break; |
1663 | | case phr_get_chunk_len: |
1664 | | chunkSz = (int)strtol(start, NULL, 16); /* hex format */ |
1665 | | state = (chunkSz == 0) ? phr_http_end : phr_get_chunk_data; |
1666 | | break; |
1667 | | case phr_get_chunk_data: |
1668 | | /* processing for chunk data done above, since \r\n isn't required */ |
1669 | | case phr_wait_end: |
1670 | | case phr_http_end: |
1671 | | /* do nothing */ |
1672 | | break; |
1673 | | } /* switch (state) */ |
1674 | | |
1675 | | /* skip to end plus \r\n */ |
1676 | | start = end + 2; |
1677 | | } |
1678 | | } while (state != phr_http_end); |
1679 | | |
1680 | | if (!isChunked) { |
1681 | | result = wolfIO_HttpProcessResponseBuf(sfd, respBuf, &respBufSz, chunkSz, |
1682 | | start, len, dynType, heap); |
1683 | | } |
1684 | | |
1685 | | if (result >= 0) { |
1686 | | result = respBufSz; |
1687 | | } |
1688 | | else { |
1689 | | WOLFSSL_ERROR(result); |
1690 | | } |
1691 | | |
1692 | | return result; |
1693 | | } |
1694 | | int wolfIO_HttpBuildRequest(const char *reqType, const char *domainName, |
1695 | | const char *path, int pathLen, int reqSz, const char *contentType, |
1696 | | byte *buf, int bufSize) |
1697 | | { |
1698 | | return wolfIO_HttpBuildRequest_ex(reqType, domainName, path, pathLen, reqSz, contentType, "", buf, bufSize); |
1699 | | } |
1700 | | |
1701 | | int wolfIO_HttpBuildRequest_ex(const char *reqType, const char *domainName, |
1702 | | const char *path, int pathLen, int reqSz, const char *contentType, |
1703 | | const char *exHdrs, byte *buf, int bufSize) |
1704 | | { |
1705 | | word32 reqTypeLen, domainNameLen, reqSzStrLen, contentTypeLen, exHdrsLen, maxLen; |
1706 | | char reqSzStr[6]; |
1707 | | char* req = (char*)buf; |
1708 | | const char* blankStr = " "; |
1709 | | const char* http11Str = " HTTP/1.1"; |
1710 | | const char* hostStr = "\r\nHost: "; |
1711 | | const char* contentLenStr = "\r\nContent-Length: "; |
1712 | | const char* contentTypeStr = "\r\nContent-Type: "; |
1713 | | const char* singleCrLfStr = "\r\n"; |
1714 | | const char* doubleCrLfStr = "\r\n\r\n"; |
1715 | | word32 blankStrLen, http11StrLen, hostStrLen, contentLenStrLen, |
1716 | | contentTypeStrLen, singleCrLfStrLen, doubleCrLfStrLen; |
1717 | | |
1718 | | reqTypeLen = (word32)XSTRLEN(reqType); |
1719 | | domainNameLen = (word32)XSTRLEN(domainName); |
1720 | | reqSzStrLen = wolfIO_Word16ToString(reqSzStr, (word16)reqSz); |
1721 | | contentTypeLen = (word32)XSTRLEN(contentType); |
1722 | | |
1723 | | blankStrLen = (word32)XSTRLEN(blankStr); |
1724 | | http11StrLen = (word32)XSTRLEN(http11Str); |
1725 | | hostStrLen = (word32)XSTRLEN(hostStr); |
1726 | | contentLenStrLen = (word32)XSTRLEN(contentLenStr); |
1727 | | contentTypeStrLen = (word32)XSTRLEN(contentTypeStr); |
1728 | | |
1729 | | if(exHdrs){ |
1730 | | singleCrLfStrLen = (word32)XSTRLEN(singleCrLfStr); |
1731 | | exHdrsLen = (word32)XSTRLEN(exHdrs); |
1732 | | } else { |
1733 | | singleCrLfStrLen = 0; |
1734 | | exHdrsLen = 0; |
1735 | | } |
1736 | | |
1737 | | doubleCrLfStrLen = (word32)XSTRLEN(doubleCrLfStr); |
1738 | | |
1739 | | /* determine max length and check it */ |
1740 | | maxLen = |
1741 | | reqTypeLen + |
1742 | | blankStrLen + |
1743 | | pathLen + |
1744 | | http11StrLen + |
1745 | | hostStrLen + |
1746 | | domainNameLen + |
1747 | | contentLenStrLen + |
1748 | | reqSzStrLen + |
1749 | | contentTypeStrLen + |
1750 | | contentTypeLen + |
1751 | | singleCrLfStrLen + |
1752 | | exHdrsLen + |
1753 | | doubleCrLfStrLen + |
1754 | | 1 /* null term */; |
1755 | | if (maxLen > (word32)bufSize) |
1756 | | return 0; |
1757 | | |
1758 | | XSTRNCPY((char*)buf, reqType, bufSize); |
1759 | | buf += reqTypeLen; bufSize -= reqTypeLen; |
1760 | | XSTRNCPY((char*)buf, blankStr, bufSize); |
1761 | | buf += blankStrLen; bufSize -= blankStrLen; |
1762 | | XSTRNCPY((char*)buf, path, bufSize); |
1763 | | buf += pathLen; bufSize -= pathLen; |
1764 | | XSTRNCPY((char*)buf, http11Str, bufSize); |
1765 | | buf += http11StrLen; bufSize -= http11StrLen; |
1766 | | if (domainNameLen > 0) { |
1767 | | XSTRNCPY((char*)buf, hostStr, bufSize); |
1768 | | buf += hostStrLen; bufSize -= hostStrLen; |
1769 | | XSTRNCPY((char*)buf, domainName, bufSize); |
1770 | | buf += domainNameLen; bufSize -= domainNameLen; |
1771 | | } |
1772 | | if (reqSz > 0 && reqSzStrLen > 0) { |
1773 | | XSTRNCPY((char*)buf, contentLenStr, bufSize); |
1774 | | buf += contentLenStrLen; bufSize -= contentLenStrLen; |
1775 | | XSTRNCPY((char*)buf, reqSzStr, bufSize); |
1776 | | buf += reqSzStrLen; bufSize -= reqSzStrLen; |
1777 | | } |
1778 | | if (contentTypeLen > 0) { |
1779 | | XSTRNCPY((char*)buf, contentTypeStr, bufSize); |
1780 | | buf += contentTypeStrLen; bufSize -= contentTypeStrLen; |
1781 | | XSTRNCPY((char*)buf, contentType, bufSize); |
1782 | | buf += contentTypeLen; bufSize -= contentTypeLen; |
1783 | | } |
1784 | | if (exHdrsLen > 0) |
1785 | | { |
1786 | | XSTRNCPY((char *)buf, singleCrLfStr, bufSize); |
1787 | | buf += singleCrLfStrLen; |
1788 | | bufSize -= singleCrLfStrLen; |
1789 | | XSTRNCPY((char *)buf, exHdrs, bufSize); |
1790 | | buf += exHdrsLen; |
1791 | | bufSize -= exHdrsLen; |
1792 | | } |
1793 | | XSTRNCPY((char*)buf, doubleCrLfStr, bufSize); |
1794 | | buf += doubleCrLfStrLen; |
1795 | | |
1796 | | #ifdef WOLFIO_DEBUG |
1797 | | printf("HTTP %s: %s", reqType, req); |
1798 | | #endif |
1799 | | |
1800 | | /* calculate actual length based on original and new pointer */ |
1801 | | return (int)((char*)buf - req); |
1802 | | } |
1803 | | |
1804 | | |
1805 | | #ifdef HAVE_OCSP |
1806 | | |
1807 | | int wolfIO_HttpBuildRequestOcsp(const char* domainName, const char* path, |
1808 | | int ocspReqSz, byte* buf, int bufSize) |
1809 | | { |
1810 | | const char *cacheCtl = "Cache-Control: no-cache"; |
1811 | | return wolfIO_HttpBuildRequest_ex("POST", domainName, path, (int)XSTRLEN(path), |
1812 | | ocspReqSz, "application/ocsp-request", cacheCtl, buf, bufSize); |
1813 | | } |
1814 | | |
1815 | | /* return: >0 OCSP Response Size |
1816 | | * -1 error */ |
1817 | | int wolfIO_HttpProcessResponseOcsp(int sfd, byte** respBuf, |
1818 | | byte* httpBuf, int httpBufSz, void* heap) |
1819 | | { |
1820 | | const char* appStrList[] = { |
1821 | | "application/ocsp-response", |
1822 | | NULL |
1823 | | }; |
1824 | | |
1825 | | return wolfIO_HttpProcessResponse(sfd, appStrList, |
1826 | | respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_OCSP, heap); |
1827 | | } |
1828 | | |
1829 | | /* in default wolfSSL callback ctx is the heap pointer */ |
1830 | | int EmbedOcspLookup(void* ctx, const char* url, int urlSz, |
1831 | | byte* ocspReqBuf, int ocspReqSz, byte** ocspRespBuf) |
1832 | | { |
1833 | | SOCKET_T sfd = SOCKET_INVALID; |
1834 | | word16 port; |
1835 | | int ret = -1; |
1836 | | #ifdef WOLFSSL_SMALL_STACK |
1837 | | char* path; |
1838 | | char* domainName; |
1839 | | #else |
1840 | | char path[MAX_URL_ITEM_SIZE]; |
1841 | | char domainName[MAX_URL_ITEM_SIZE]; |
1842 | | #endif |
1843 | | |
1844 | | #ifdef WOLFSSL_SMALL_STACK |
1845 | | path = (char*)XMALLOC(MAX_URL_ITEM_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1846 | | if (path == NULL) |
1847 | | return MEMORY_E; |
1848 | | |
1849 | | domainName = (char*)XMALLOC(MAX_URL_ITEM_SIZE, NULL, |
1850 | | DYNAMIC_TYPE_TMP_BUFFER); |
1851 | | if (domainName == NULL) { |
1852 | | XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1853 | | return MEMORY_E; |
1854 | | } |
1855 | | #endif |
1856 | | |
1857 | | if (ocspReqBuf == NULL || ocspReqSz == 0) { |
1858 | | WOLFSSL_MSG("OCSP request is required for lookup"); |
1859 | | } |
1860 | | else if (ocspRespBuf == NULL) { |
1861 | | WOLFSSL_MSG("Cannot save OCSP response"); |
1862 | | } |
1863 | | else if (wolfIO_DecodeUrl(url, urlSz, domainName, path, &port) < 0) { |
1864 | | WOLFSSL_MSG("Unable to decode OCSP URL"); |
1865 | | } |
1866 | | else { |
1867 | | /* Note, the library uses the EmbedOcspRespFree() callback to |
1868 | | * free this buffer. */ |
1869 | | int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE; |
1870 | | byte* httpBuf = (byte*)XMALLOC(httpBufSz, ctx, DYNAMIC_TYPE_OCSP); |
1871 | | |
1872 | | if (httpBuf == NULL) { |
1873 | | WOLFSSL_MSG("Unable to create OCSP response buffer"); |
1874 | | } |
1875 | | else { |
1876 | | httpBufSz = wolfIO_HttpBuildRequestOcsp(domainName, path, ocspReqSz, |
1877 | | httpBuf, httpBufSz); |
1878 | | |
1879 | | ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec); |
1880 | | if (ret != 0) { |
1881 | | WOLFSSL_MSG("OCSP Responder connection failed"); |
1882 | | } |
1883 | | else if (wolfIO_Send(sfd, (char*)httpBuf, httpBufSz, 0) != |
1884 | | httpBufSz) { |
1885 | | WOLFSSL_MSG("OCSP http request failed"); |
1886 | | } |
1887 | | else if (wolfIO_Send(sfd, (char*)ocspReqBuf, ocspReqSz, 0) != |
1888 | | ocspReqSz) { |
1889 | | WOLFSSL_MSG("OCSP ocsp request failed"); |
1890 | | } |
1891 | | else { |
1892 | | ret = wolfIO_HttpProcessResponseOcsp((int)sfd, ocspRespBuf, httpBuf, |
1893 | | HTTP_SCRATCH_BUFFER_SIZE, ctx); |
1894 | | } |
1895 | | if (sfd != SOCKET_INVALID) |
1896 | | CloseSocket(sfd); |
1897 | | XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP); |
1898 | | } |
1899 | | } |
1900 | | |
1901 | | #ifdef WOLFSSL_SMALL_STACK |
1902 | | XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1903 | | XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1904 | | #endif |
1905 | | |
1906 | | return ret; |
1907 | | } |
1908 | | |
1909 | | /* in default callback ctx is heap hint */ |
1910 | | void EmbedOcspRespFree(void* ctx, byte *resp) |
1911 | | { |
1912 | | if (resp) |
1913 | | XFREE(resp, ctx, DYNAMIC_TYPE_OCSP); |
1914 | | |
1915 | | (void)ctx; |
1916 | | } |
1917 | | #endif /* HAVE_OCSP */ |
1918 | | |
1919 | | |
1920 | | #if defined(HAVE_CRL) && defined(HAVE_CRL_IO) |
1921 | | |
1922 | | int wolfIO_HttpBuildRequestCrl(const char* url, int urlSz, |
1923 | | const char* domainName, byte* buf, int bufSize) |
1924 | | { |
1925 | | const char *cacheCtl = "Cache-Control: no-cache"; |
1926 | | return wolfIO_HttpBuildRequest_ex("GET", domainName, url, urlSz, 0, "", |
1927 | | cacheCtl, buf, bufSize); |
1928 | | } |
1929 | | |
1930 | | int wolfIO_HttpProcessResponseCrl(WOLFSSL_CRL* crl, int sfd, byte* httpBuf, |
1931 | | int httpBufSz) |
1932 | | { |
1933 | | int ret; |
1934 | | byte *respBuf = NULL; |
1935 | | |
1936 | | const char* appStrList[] = { |
1937 | | "application/pkix-crl", |
1938 | | "application/x-pkcs7-crl", |
1939 | | NULL |
1940 | | }; |
1941 | | |
1942 | | |
1943 | | ret = wolfIO_HttpProcessResponse(sfd, appStrList, |
1944 | | &respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_CRL, crl->heap); |
1945 | | if (ret >= 0) { |
1946 | | ret = BufferLoadCRL(crl, respBuf, ret, WOLFSSL_FILETYPE_ASN1, 0); |
1947 | | } |
1948 | | XFREE(respBuf, crl->heap, DYNAMIC_TYPE_CRL); |
1949 | | |
1950 | | return ret; |
1951 | | } |
1952 | | |
1953 | | int EmbedCrlLookup(WOLFSSL_CRL* crl, const char* url, int urlSz) |
1954 | | { |
1955 | | SOCKET_T sfd = SOCKET_INVALID; |
1956 | | word16 port; |
1957 | | int ret = -1; |
1958 | | #ifdef WOLFSSL_SMALL_STACK |
1959 | | char* domainName; |
1960 | | #else |
1961 | | char domainName[MAX_URL_ITEM_SIZE]; |
1962 | | #endif |
1963 | | |
1964 | | #ifdef WOLFSSL_SMALL_STACK |
1965 | | domainName = (char*)XMALLOC(MAX_URL_ITEM_SIZE, crl->heap, |
1966 | | DYNAMIC_TYPE_TMP_BUFFER); |
1967 | | if (domainName == NULL) { |
1968 | | return MEMORY_E; |
1969 | | } |
1970 | | #endif |
1971 | | |
1972 | | if (wolfIO_DecodeUrl(url, urlSz, domainName, NULL, &port) < 0) { |
1973 | | WOLFSSL_MSG("Unable to decode CRL URL"); |
1974 | | } |
1975 | | else { |
1976 | | int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE; |
1977 | | byte* httpBuf = (byte*)XMALLOC(httpBufSz, crl->heap, |
1978 | | DYNAMIC_TYPE_CRL); |
1979 | | if (httpBuf == NULL) { |
1980 | | WOLFSSL_MSG("Unable to create CRL response buffer"); |
1981 | | } |
1982 | | else { |
1983 | | httpBufSz = wolfIO_HttpBuildRequestCrl(url, urlSz, domainName, |
1984 | | httpBuf, httpBufSz); |
1985 | | |
1986 | | ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec); |
1987 | | if (ret != 0) { |
1988 | | WOLFSSL_MSG("CRL connection failed"); |
1989 | | } |
1990 | | else if (wolfIO_Send(sfd, (char*)httpBuf, httpBufSz, 0) |
1991 | | != httpBufSz) { |
1992 | | WOLFSSL_MSG("CRL http get failed"); |
1993 | | } |
1994 | | else { |
1995 | | ret = wolfIO_HttpProcessResponseCrl(crl, sfd, httpBuf, |
1996 | | HTTP_SCRATCH_BUFFER_SIZE); |
1997 | | } |
1998 | | if (sfd != SOCKET_INVALID) |
1999 | | CloseSocket(sfd); |
2000 | | XFREE(httpBuf, crl->heap, DYNAMIC_TYPE_CRL); |
2001 | | } |
2002 | | } |
2003 | | |
2004 | | #ifdef WOLFSSL_SMALL_STACK |
2005 | | XFREE(domainName, crl->heap, DYNAMIC_TYPE_TMP_BUFFER); |
2006 | | #endif |
2007 | | |
2008 | | return ret; |
2009 | | } |
2010 | | #endif /* HAVE_CRL && HAVE_CRL_IO */ |
2011 | | |
2012 | | #endif /* HAVE_HTTP_CLIENT */ |
2013 | | |
2014 | | |
2015 | | |
2016 | | void wolfSSL_CTX_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv) |
2017 | 0 | { |
2018 | 0 | if (ctx) { |
2019 | 0 | ctx->CBIORecv = CBIORecv; |
2020 | | #ifdef OPENSSL_EXTRA |
2021 | | ctx->cbioFlag |= WOLFSSL_CBIO_RECV; |
2022 | | #endif |
2023 | 0 | } |
2024 | 0 | } |
2025 | | |
2026 | | |
2027 | | void wolfSSL_CTX_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend) |
2028 | 0 | { |
2029 | 0 | if (ctx) { |
2030 | 0 | ctx->CBIOSend = CBIOSend; |
2031 | | #ifdef OPENSSL_EXTRA |
2032 | | ctx->cbioFlag |= WOLFSSL_CBIO_SEND; |
2033 | | #endif |
2034 | 0 | } |
2035 | 0 | } |
2036 | | |
2037 | | |
2038 | | /* sets the IO callback to use for receives at WOLFSSL level */ |
2039 | | void wolfSSL_SSLSetIORecv(WOLFSSL *ssl, CallbackIORecv CBIORecv) |
2040 | 0 | { |
2041 | 0 | if (ssl) { |
2042 | 0 | ssl->CBIORecv = CBIORecv; |
2043 | | #ifdef OPENSSL_EXTRA |
2044 | | ssl->cbioFlag |= WOLFSSL_CBIO_RECV; |
2045 | | #endif |
2046 | 0 | } |
2047 | 0 | } |
2048 | | |
2049 | | |
2050 | | /* sets the IO callback to use for sends at WOLFSSL level */ |
2051 | | void wolfSSL_SSLSetIOSend(WOLFSSL *ssl, CallbackIOSend CBIOSend) |
2052 | 0 | { |
2053 | 0 | if (ssl) { |
2054 | 0 | ssl->CBIOSend = CBIOSend; |
2055 | | #ifdef OPENSSL_EXTRA |
2056 | | ssl->cbioFlag |= WOLFSSL_CBIO_SEND; |
2057 | | #endif |
2058 | 0 | } |
2059 | 0 | } |
2060 | | |
2061 | | |
2062 | | void wolfSSL_SetIOReadCtx(WOLFSSL* ssl, void *rctx) |
2063 | 0 | { |
2064 | 0 | if (ssl) |
2065 | 0 | ssl->IOCB_ReadCtx = rctx; |
2066 | 0 | } |
2067 | | |
2068 | | |
2069 | | void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *wctx) |
2070 | 0 | { |
2071 | 0 | if (ssl) |
2072 | 0 | ssl->IOCB_WriteCtx = wctx; |
2073 | 0 | } |
2074 | | |
2075 | | |
2076 | | void* wolfSSL_GetIOReadCtx(WOLFSSL* ssl) |
2077 | 0 | { |
2078 | 0 | if (ssl) |
2079 | 0 | return ssl->IOCB_ReadCtx; |
2080 | | |
2081 | 0 | return NULL; |
2082 | 0 | } |
2083 | | |
2084 | | |
2085 | | void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl) |
2086 | 0 | { |
2087 | 0 | if (ssl) |
2088 | 0 | return ssl->IOCB_WriteCtx; |
2089 | | |
2090 | 0 | return NULL; |
2091 | 0 | } |
2092 | | |
2093 | | |
2094 | | void wolfSSL_SetIOReadFlags(WOLFSSL* ssl, int flags) |
2095 | 0 | { |
2096 | 0 | if (ssl) |
2097 | 0 | ssl->rflags = flags; |
2098 | 0 | } |
2099 | | |
2100 | | |
2101 | | void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags) |
2102 | 0 | { |
2103 | 0 | if (ssl) |
2104 | 0 | ssl->wflags = flags; |
2105 | 0 | } |
2106 | | |
2107 | | |
2108 | | #ifdef WOLFSSL_DTLS |
2109 | | |
2110 | | void wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX* ctx, CallbackGenCookie cb) |
2111 | | { |
2112 | | if (ctx) |
2113 | | ctx->CBIOCookie = cb; |
2114 | | } |
2115 | | |
2116 | | |
2117 | | void wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx) |
2118 | | { |
2119 | | if (ssl) |
2120 | | ssl->IOCB_CookieCtx = ctx; |
2121 | | } |
2122 | | |
2123 | | |
2124 | | void* wolfSSL_GetCookieCtx(WOLFSSL* ssl) |
2125 | | { |
2126 | | if (ssl) |
2127 | | return ssl->IOCB_CookieCtx; |
2128 | | |
2129 | | return NULL; |
2130 | | } |
2131 | | #endif /* WOLFSSL_DTLS */ |
2132 | | |
2133 | | #ifdef WOLFSSL_SESSION_EXPORT |
2134 | | |
2135 | | void wolfSSL_CTX_SetIOGetPeer(WOLFSSL_CTX* ctx, CallbackGetPeer cb) |
2136 | | { |
2137 | | if (ctx) |
2138 | | ctx->CBGetPeer = cb; |
2139 | | } |
2140 | | |
2141 | | |
2142 | | void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX* ctx, CallbackSetPeer cb) |
2143 | | { |
2144 | | if (ctx) |
2145 | | ctx->CBSetPeer = cb; |
2146 | | } |
2147 | | |
2148 | | #endif /* WOLFSSL_SESSION_EXPORT */ |
2149 | | |
2150 | | |
2151 | | #ifdef HAVE_NETX |
2152 | | |
2153 | | /* The NetX receive callback |
2154 | | * return : bytes read, or error |
2155 | | */ |
2156 | | int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
2157 | | { |
2158 | | NetX_Ctx* nxCtx = (NetX_Ctx*)ctx; |
2159 | | ULONG left; |
2160 | | ULONG total; |
2161 | | ULONG copied = 0; |
2162 | | UINT status; |
2163 | | |
2164 | | (void)ssl; |
2165 | | |
2166 | | if (nxCtx == NULL || nxCtx->nxSocket == NULL) { |
2167 | | WOLFSSL_MSG("NetX Recv NULL parameters"); |
2168 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2169 | | } |
2170 | | |
2171 | | if (nxCtx->nxPacket == NULL) { |
2172 | | status = nx_tcp_socket_receive(nxCtx->nxSocket, &nxCtx->nxPacket, |
2173 | | nxCtx->nxWait); |
2174 | | if (status != NX_SUCCESS) { |
2175 | | WOLFSSL_MSG("NetX Recv receive error"); |
2176 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2177 | | } |
2178 | | } |
2179 | | |
2180 | | if (nxCtx->nxPacket) { |
2181 | | status = nx_packet_length_get(nxCtx->nxPacket, &total); |
2182 | | if (status != NX_SUCCESS) { |
2183 | | WOLFSSL_MSG("NetX Recv length get error"); |
2184 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2185 | | } |
2186 | | |
2187 | | left = total - nxCtx->nxOffset; |
2188 | | status = nx_packet_data_extract_offset(nxCtx->nxPacket, nxCtx->nxOffset, |
2189 | | buf, sz, &copied); |
2190 | | if (status != NX_SUCCESS) { |
2191 | | WOLFSSL_MSG("NetX Recv data extract offset error"); |
2192 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2193 | | } |
2194 | | |
2195 | | nxCtx->nxOffset += copied; |
2196 | | |
2197 | | if (copied == left) { |
2198 | | WOLFSSL_MSG("NetX Recv Drained packet"); |
2199 | | nx_packet_release(nxCtx->nxPacket); |
2200 | | nxCtx->nxPacket = NULL; |
2201 | | nxCtx->nxOffset = 0; |
2202 | | } |
2203 | | } |
2204 | | |
2205 | | return copied; |
2206 | | } |
2207 | | |
2208 | | |
2209 | | /* The NetX send callback |
2210 | | * return : bytes sent, or error |
2211 | | */ |
2212 | | int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
2213 | | { |
2214 | | NetX_Ctx* nxCtx = (NetX_Ctx*)ctx; |
2215 | | NX_PACKET* packet; |
2216 | | NX_PACKET_POOL* pool; /* shorthand */ |
2217 | | UINT status; |
2218 | | |
2219 | | (void)ssl; |
2220 | | |
2221 | | if (nxCtx == NULL || nxCtx->nxSocket == NULL) { |
2222 | | WOLFSSL_MSG("NetX Send NULL parameters"); |
2223 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2224 | | } |
2225 | | |
2226 | | pool = nxCtx->nxSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool; |
2227 | | status = nx_packet_allocate(pool, &packet, NX_TCP_PACKET, |
2228 | | nxCtx->nxWait); |
2229 | | if (status != NX_SUCCESS) { |
2230 | | WOLFSSL_MSG("NetX Send packet alloc error"); |
2231 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2232 | | } |
2233 | | |
2234 | | status = nx_packet_data_append(packet, buf, sz, pool, nxCtx->nxWait); |
2235 | | if (status != NX_SUCCESS) { |
2236 | | nx_packet_release(packet); |
2237 | | WOLFSSL_MSG("NetX Send data append error"); |
2238 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2239 | | } |
2240 | | |
2241 | | status = nx_tcp_socket_send(nxCtx->nxSocket, packet, nxCtx->nxWait); |
2242 | | if (status != NX_SUCCESS) { |
2243 | | nx_packet_release(packet); |
2244 | | WOLFSSL_MSG("NetX Send socket send error"); |
2245 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2246 | | } |
2247 | | |
2248 | | return sz; |
2249 | | } |
2250 | | |
2251 | | |
2252 | | /* like set_fd, but for default NetX context */ |
2253 | | void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption) |
2254 | | { |
2255 | | if (ssl) { |
2256 | | ssl->nxCtx.nxSocket = nxSocket; |
2257 | | ssl->nxCtx.nxWait = waitOption; |
2258 | | } |
2259 | | } |
2260 | | |
2261 | | #endif /* HAVE_NETX */ |
2262 | | |
2263 | | |
2264 | | #ifdef MICRIUM |
2265 | | |
2266 | | /* Micrium uTCP/IP port, using the NetSock API |
2267 | | * TCP and UDP are currently supported with the callbacks below. |
2268 | | * |
2269 | | * WOLFSSL_SESSION_EXPORT is not yet supported, would need EmbedGetPeer() |
2270 | | * and EmbedSetPeer() callbacks implemented. |
2271 | | * |
2272 | | * HAVE_CRL is not yet supported, would need an EmbedCrlLookup() |
2273 | | * callback implemented. |
2274 | | * |
2275 | | * HAVE_OCSP is not yet supported, would need an EmbedOCSPLookup() |
2276 | | * callback implemented. |
2277 | | */ |
2278 | | |
2279 | | /* The Micrium uTCP/IP send callback |
2280 | | * return : bytes sent, or error |
2281 | | */ |
2282 | | int MicriumSend(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
2283 | | { |
2284 | | NET_SOCK_ID sd = *(int*)ctx; |
2285 | | NET_SOCK_RTN_CODE ret; |
2286 | | NET_ERR err; |
2287 | | |
2288 | | ret = NetSock_TxData(sd, buf, sz, ssl->wflags, &err); |
2289 | | if (ret < 0) { |
2290 | | WOLFSSL_MSG("Embed Send error"); |
2291 | | |
2292 | | if (err == NET_ERR_TX) { |
2293 | | WOLFSSL_MSG("\tWould block"); |
2294 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
2295 | | |
2296 | | } else { |
2297 | | WOLFSSL_MSG("\tGeneral error"); |
2298 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2299 | | } |
2300 | | } |
2301 | | |
2302 | | return ret; |
2303 | | } |
2304 | | |
2305 | | /* The Micrium uTCP/IP receive callback |
2306 | | * return : nb bytes read, or error |
2307 | | */ |
2308 | | int MicriumReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
2309 | | { |
2310 | | NET_SOCK_ID sd = *(int*)ctx; |
2311 | | NET_SOCK_RTN_CODE ret; |
2312 | | NET_ERR err; |
2313 | | |
2314 | | #ifdef WOLFSSL_DTLS |
2315 | | { |
2316 | | int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); |
2317 | | /* Don't use ssl->options.handShakeDone since it is true even if |
2318 | | * we are in the process of renegotiation */ |
2319 | | byte doDtlsTimeout = ssl->options.handShakeState != HANDSHAKE_DONE; |
2320 | | #ifdef WOLFSSL_DTLS13 |
2321 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) { |
2322 | | doDtlsTimeout = |
2323 | | doDtlsTimeout || ssl->dtls13Rtx.rtxRecords != NULL || |
2324 | | (ssl->dtls13FastTimeout && ssl->dtls13Rtx.seenRecords != NULL); |
2325 | | } |
2326 | | #endif /* WOLFSSL_DTLS13 */ |
2327 | | |
2328 | | if (!doDtlsTimeout) |
2329 | | dtls_timeout = 0; |
2330 | | |
2331 | | if (!wolfSSL_dtls_get_using_nonblock(ssl)) { |
2332 | | /* needs timeout in milliseconds */ |
2333 | | #ifdef WOLFSSL_DTLS13 |
2334 | | if (wolfSSL_dtls13_use_quick_timeout(ssl) && |
2335 | | IsAtLeastTLSv1_3(ssl->version)) { |
2336 | | dtls_timeout = (1000 * dtls_timeout) / 4; |
2337 | | } else |
2338 | | #endif /* WOLFSSL_DTLS13 */ |
2339 | | dtls_timeout = 1000 * dtls_timeout; |
2340 | | NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout, &err); |
2341 | | if (err != NET_SOCK_ERR_NONE) { |
2342 | | WOLFSSL_MSG("NetSock_CfgTimeoutRxQ_Set failed"); |
2343 | | } |
2344 | | } |
2345 | | } |
2346 | | #endif |
2347 | | |
2348 | | ret = NetSock_RxData(sd, buf, sz, ssl->rflags, &err); |
2349 | | if (ret < 0) { |
2350 | | WOLFSSL_MSG("Embed Receive error"); |
2351 | | |
2352 | | if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY || |
2353 | | err == NET_ERR_FAULT_LOCK_ACQUIRE) { |
2354 | | if (!wolfSSL_dtls(ssl) || wolfSSL_dtls_get_using_nonblock(ssl)) { |
2355 | | WOLFSSL_MSG("\tWould block"); |
2356 | | return WOLFSSL_CBIO_ERR_WANT_READ; |
2357 | | } |
2358 | | else { |
2359 | | WOLFSSL_MSG("\tSocket timeout"); |
2360 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
2361 | | } |
2362 | | |
2363 | | } else if (err == NET_SOCK_ERR_CLOSED) { |
2364 | | WOLFSSL_MSG("Embed receive connection closed"); |
2365 | | return WOLFSSL_CBIO_ERR_CONN_CLOSE; |
2366 | | |
2367 | | } else { |
2368 | | WOLFSSL_MSG("\tGeneral error"); |
2369 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2370 | | } |
2371 | | } |
2372 | | |
2373 | | return ret; |
2374 | | } |
2375 | | |
2376 | | /* The Micrium uTCP/IP receivefrom callback |
2377 | | * return : nb bytes read, or error |
2378 | | */ |
2379 | | int MicriumReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
2380 | | { |
2381 | | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
2382 | | NET_SOCK_ID sd = dtlsCtx->rfd; |
2383 | | NET_SOCK_ADDR peer; |
2384 | | NET_SOCK_ADDR_LEN peerSz = sizeof(peer); |
2385 | | NET_SOCK_RTN_CODE ret; |
2386 | | NET_ERR err; |
2387 | | |
2388 | | WOLFSSL_ENTER("MicriumReceiveFrom"); |
2389 | | |
2390 | | #ifdef WOLFSSL_DTLS |
2391 | | { |
2392 | | int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); |
2393 | | /* Don't use ssl->options.handShakeDone since it is true even if |
2394 | | * we are in the process of renegotiation */ |
2395 | | byte doDtlsTimeout = ssl->options.handShakeState != HANDSHAKE_DONE; |
2396 | | |
2397 | | #ifdef WOLFSSL_DTLS13 |
2398 | | if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) { |
2399 | | doDtlsTimeout = |
2400 | | doDtlsTimeout || ssl->dtls13Rtx.rtxRecords != NULL || |
2401 | | (ssl->dtls13FastTimeout && ssl->dtls13Rtx.seenRecords != NULL); |
2402 | | } |
2403 | | #endif /* WOLFSSL_DTLS13 */ |
2404 | | |
2405 | | if (!doDtlsTimeout) |
2406 | | dtls_timeout = 0; |
2407 | | |
2408 | | if (!wolfSSL_dtls_get_using_nonblock(ssl)) { |
2409 | | /* needs timeout in milliseconds */ |
2410 | | #ifdef WOLFSSL_DTLS13 |
2411 | | if (wolfSSL_dtls13_use_quick_timeout(ssl) && |
2412 | | IsAtLeastTLSv1_3(ssl->version)) { |
2413 | | dtls_timeout = (1000 * dtls_timeout) / 4; |
2414 | | } else |
2415 | | #endif /* WOLFSSL_DTLS13 */ |
2416 | | dtls_timeout = 1000 * dtls_timeout; |
2417 | | NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout, &err); |
2418 | | if (err != NET_SOCK_ERR_NONE) { |
2419 | | WOLFSSL_MSG("NetSock_CfgTimeoutRxQ_Set failed"); |
2420 | | } |
2421 | | } |
2422 | | } |
2423 | | #endif /* WOLFSSL_DTLS */ |
2424 | | |
2425 | | ret = NetSock_RxDataFrom(sd, buf, sz, ssl->rflags, &peer, &peerSz, |
2426 | | 0, 0, 0, &err); |
2427 | | if (ret < 0) { |
2428 | | WOLFSSL_MSG("Embed Receive From error"); |
2429 | | |
2430 | | if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY || |
2431 | | err == NET_ERR_FAULT_LOCK_ACQUIRE) { |
2432 | | if (wolfSSL_dtls_get_using_nonblock(ssl)) { |
2433 | | WOLFSSL_MSG("\tWould block"); |
2434 | | return WOLFSSL_CBIO_ERR_WANT_READ; |
2435 | | } |
2436 | | else { |
2437 | | WOLFSSL_MSG("\tSocket timeout"); |
2438 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
2439 | | } |
2440 | | } else { |
2441 | | WOLFSSL_MSG("\tGeneral error"); |
2442 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2443 | | } |
2444 | | } |
2445 | | else { |
2446 | | if (dtlsCtx->peer.sz > 0 |
2447 | | && peerSz != (NET_SOCK_ADDR_LEN)dtlsCtx->peer.sz |
2448 | | && XMEMCMP(&peer, dtlsCtx->peer.sa, peerSz) != 0) { |
2449 | | WOLFSSL_MSG("\tIgnored packet from invalid peer"); |
2450 | | return WOLFSSL_CBIO_ERR_WANT_READ; |
2451 | | } |
2452 | | } |
2453 | | |
2454 | | return ret; |
2455 | | } |
2456 | | |
2457 | | /* The Micrium uTCP/IP sendto callback |
2458 | | * return : nb bytes sent, or error |
2459 | | */ |
2460 | | int MicriumSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
2461 | | { |
2462 | | WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx; |
2463 | | NET_SOCK_ID sd = dtlsCtx->wfd; |
2464 | | NET_SOCK_RTN_CODE ret; |
2465 | | NET_ERR err; |
2466 | | |
2467 | | WOLFSSL_ENTER("MicriumSendTo"); |
2468 | | |
2469 | | ret = NetSock_TxDataTo(sd, buf, sz, ssl->wflags, |
2470 | | (NET_SOCK_ADDR*)dtlsCtx->peer.sa, |
2471 | | (NET_SOCK_ADDR_LEN)dtlsCtx->peer.sz, |
2472 | | &err); |
2473 | | if (err < 0) { |
2474 | | WOLFSSL_MSG("Embed Send To error"); |
2475 | | |
2476 | | if (err == NET_ERR_TX) { |
2477 | | WOLFSSL_MSG("\tWould block"); |
2478 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
2479 | | |
2480 | | } else { |
2481 | | WOLFSSL_MSG("\tGeneral error"); |
2482 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2483 | | } |
2484 | | } |
2485 | | |
2486 | | return ret; |
2487 | | } |
2488 | | |
2489 | | /* Micrium DTLS Generate Cookie callback |
2490 | | * return : number of bytes copied into buf, or error |
2491 | | */ |
2492 | | #if defined(NO_SHA) && !defined(NO_SHA256) |
2493 | | #define MICRIUM_COOKIE_DIGEST_SIZE WC_SHA256_DIGEST_SIZE |
2494 | | #elif !defined(NO_SHA) |
2495 | | #define MICRIUM_COOKIE_DIGEST_SIZE WC_SHA_DIGEST_SIZE |
2496 | | #else |
2497 | | #error Must enable either SHA-1 or SHA256 (or both) for Micrium. |
2498 | | #endif |
2499 | | int MicriumGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx) |
2500 | | { |
2501 | | NET_SOCK_ADDR peer; |
2502 | | NET_SOCK_ADDR_LEN peerSz = sizeof(peer); |
2503 | | byte digest[MICRIUM_COOKIE_DIGEST_SIZE]; |
2504 | | int ret = 0; |
2505 | | |
2506 | | (void)ctx; |
2507 | | |
2508 | | XMEMSET(&peer, 0, sizeof(peer)); |
2509 | | if (wolfSSL_dtls_get_peer(ssl, (void*)&peer, |
2510 | | (unsigned int*)&peerSz) != WOLFSSL_SUCCESS) { |
2511 | | WOLFSSL_MSG("getpeername failed in MicriumGenerateCookie"); |
2512 | | return GEN_COOKIE_E; |
2513 | | } |
2514 | | |
2515 | | #if defined(NO_SHA) && !defined(NO_SHA256) |
2516 | | ret = wc_Sha256Hash((byte*)&peer, peerSz, digest); |
2517 | | #else |
2518 | | ret = wc_ShaHash((byte*)&peer, peerSz, digest); |
2519 | | #endif |
2520 | | if (ret != 0) |
2521 | | return ret; |
2522 | | |
2523 | | if (sz > MICRIUM_COOKIE_DIGEST_SIZE) |
2524 | | sz = MICRIUM_COOKIE_DIGEST_SIZE; |
2525 | | XMEMCPY(buf, digest, sz); |
2526 | | |
2527 | | return sz; |
2528 | | } |
2529 | | |
2530 | | #endif /* MICRIUM */ |
2531 | | |
2532 | | #if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) |
2533 | | |
2534 | | #include <os/os_error.h> |
2535 | | #include <os/os_mbuf.h> |
2536 | | #include <os/os_mempool.h> |
2537 | | |
2538 | | #define MB_NAME "wolfssl_mb" |
2539 | | |
2540 | | typedef struct Mynewt_Ctx { |
2541 | | struct mn_socket *mnSocket; /* send/recv socket handler */ |
2542 | | struct mn_sockaddr_in mnSockAddrIn; /* socket address */ |
2543 | | struct os_mbuf *mnPacket; /* incoming packet handle |
2544 | | for short reads */ |
2545 | | int reading; /* reading flag */ |
2546 | | |
2547 | | /* private */ |
2548 | | void *mnMemBuffer; /* memory buffer for mempool */ |
2549 | | struct os_mempool mnMempool; /* mempool */ |
2550 | | struct os_mbuf_pool mnMbufpool; /* mbuf pool */ |
2551 | | } Mynewt_Ctx; |
2552 | | |
2553 | | void mynewt_ctx_clear(void *ctx) { |
2554 | | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; |
2555 | | if(!mynewt_ctx) return; |
2556 | | |
2557 | | if(mynewt_ctx->mnPacket) { |
2558 | | os_mbuf_free_chain(mynewt_ctx->mnPacket); |
2559 | | mynewt_ctx->mnPacket = NULL; |
2560 | | } |
2561 | | os_mempool_clear(&mynewt_ctx->mnMempool); |
2562 | | XFREE(mynewt_ctx->mnMemBuffer, 0, 0); |
2563 | | XFREE(mynewt_ctx, 0, 0); |
2564 | | } |
2565 | | |
2566 | | /* return Mynewt_Ctx instance */ |
2567 | | void* mynewt_ctx_new() { |
2568 | | int rc = 0; |
2569 | | Mynewt_Ctx *mynewt_ctx; |
2570 | | int mem_buf_count = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_COUNT); |
2571 | | int mem_buf_size = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_SIZE); |
2572 | | int mempool_bytes = OS_MEMPOOL_BYTES(mem_buf_count, mem_buf_size); |
2573 | | |
2574 | | mynewt_ctx = (Mynewt_Ctx *)XMALLOC(sizeof(struct Mynewt_Ctx), |
2575 | | NULL, DYNAMIC_TYPE_TMP_BUFFER); |
2576 | | if(!mynewt_ctx) return NULL; |
2577 | | |
2578 | | XMEMSET(mynewt_ctx, 0, sizeof(Mynewt_Ctx)); |
2579 | | mynewt_ctx->mnMemBuffer = (void *)XMALLOC(mempool_bytes, 0, 0); |
2580 | | if(!mynewt_ctx->mnMemBuffer) { |
2581 | | mynewt_ctx_clear((void*)mynewt_ctx); |
2582 | | return NULL; |
2583 | | } |
2584 | | |
2585 | | rc = os_mempool_init(&mynewt_ctx->mnMempool, |
2586 | | mem_buf_count, mem_buf_size, |
2587 | | mynewt_ctx->mnMemBuffer, MB_NAME); |
2588 | | if(rc != 0) { |
2589 | | mynewt_ctx_clear((void*)mynewt_ctx); |
2590 | | return NULL; |
2591 | | } |
2592 | | rc = os_mbuf_pool_init(&mynewt_ctx->mnMbufpool, &mynewt_ctx->mnMempool, |
2593 | | mem_buf_count, mem_buf_size); |
2594 | | if(rc != 0) { |
2595 | | mynewt_ctx_clear((void*)mynewt_ctx); |
2596 | | return NULL; |
2597 | | } |
2598 | | |
2599 | | return mynewt_ctx; |
2600 | | } |
2601 | | |
2602 | | static void mynewt_sock_writable(void *arg, int err); |
2603 | | static void mynewt_sock_readable(void *arg, int err); |
2604 | | static const union mn_socket_cb mynewt_sock_cbs = { |
2605 | | .socket.writable = mynewt_sock_writable, |
2606 | | .socket.readable = mynewt_sock_readable, |
2607 | | }; |
2608 | | static void mynewt_sock_writable(void *arg, int err) |
2609 | | { |
2610 | | /* do nothing */ |
2611 | | } |
2612 | | static void mynewt_sock_readable(void *arg, int err) |
2613 | | { |
2614 | | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)arg; |
2615 | | if (err && mynewt_ctx->reading) { |
2616 | | mynewt_ctx->reading = 0; |
2617 | | } |
2618 | | } |
2619 | | |
2620 | | /* The Mynewt receive callback |
2621 | | * return : bytes read, or error |
2622 | | */ |
2623 | | int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx) |
2624 | | { |
2625 | | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; |
2626 | | int rc = 0; |
2627 | | struct mn_sockaddr_in from; |
2628 | | struct os_mbuf *m; |
2629 | | int read_sz = 0; |
2630 | | word16 total; |
2631 | | |
2632 | | if (mynewt_ctx == NULL || mynewt_ctx->mnSocket == NULL) { |
2633 | | WOLFSSL_MSG("Mynewt Recv NULL parameters"); |
2634 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2635 | | } |
2636 | | |
2637 | | if(mynewt_ctx->mnPacket == NULL) { |
2638 | | mynewt_ctx->mnPacket = os_mbuf_get_pkthdr(&mynewt_ctx->mnMbufpool, 0); |
2639 | | if(mynewt_ctx->mnPacket == NULL) { |
2640 | | return MEMORY_E; |
2641 | | } |
2642 | | |
2643 | | mynewt_ctx->reading = 1; |
2644 | | while(mynewt_ctx->reading && rc == 0) { |
2645 | | rc = mn_recvfrom(mynewt_ctx->mnSocket, &m, (struct mn_sockaddr *) &from); |
2646 | | if(rc == MN_ECONNABORTED) { |
2647 | | rc = 0; |
2648 | | mynewt_ctx->reading = 0; |
2649 | | break; |
2650 | | } |
2651 | | if (!(rc == 0 || rc == MN_EAGAIN)) { |
2652 | | WOLFSSL_MSG("Mynewt Recv receive error"); |
2653 | | mynewt_ctx->reading = 0; |
2654 | | break; |
2655 | | } |
2656 | | if(rc == 0) { |
2657 | | int len = OS_MBUF_PKTLEN(m); |
2658 | | if(len == 0) { |
2659 | | break; |
2660 | | } |
2661 | | rc = os_mbuf_appendfrom(mynewt_ctx->mnPacket, m, 0, len); |
2662 | | if(rc != 0) { |
2663 | | WOLFSSL_MSG("Mynewt Recv os_mbuf_appendfrom error"); |
2664 | | break; |
2665 | | } |
2666 | | os_mbuf_free_chain(m); |
2667 | | m = NULL; |
2668 | | } else if(rc == MN_EAGAIN) { |
2669 | | /* continue to until reading all of packet data. */ |
2670 | | rc = 0; |
2671 | | break; |
2672 | | } |
2673 | | } |
2674 | | if(rc != 0) { |
2675 | | mynewt_ctx->reading = 0; |
2676 | | os_mbuf_free_chain(mynewt_ctx->mnPacket); |
2677 | | mynewt_ctx->mnPacket = NULL; |
2678 | | return rc; |
2679 | | } |
2680 | | } |
2681 | | |
2682 | | if(mynewt_ctx->mnPacket) { |
2683 | | total = OS_MBUF_PKTLEN(mynewt_ctx->mnPacket); |
2684 | | read_sz = (total >= sz)? sz : total; |
2685 | | |
2686 | | os_mbuf_copydata(mynewt_ctx->mnPacket, 0, read_sz, (void*)buf); |
2687 | | os_mbuf_adj(mynewt_ctx->mnPacket, read_sz); |
2688 | | |
2689 | | if (read_sz == total) { |
2690 | | WOLFSSL_MSG("Mynewt Recv Drained packet"); |
2691 | | os_mbuf_free_chain(mynewt_ctx->mnPacket); |
2692 | | mynewt_ctx->mnPacket = NULL; |
2693 | | } |
2694 | | } |
2695 | | |
2696 | | return read_sz; |
2697 | | } |
2698 | | |
2699 | | /* The Mynewt send callback |
2700 | | * return : bytes sent, or error |
2701 | | */ |
2702 | | int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx) |
2703 | | { |
2704 | | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; |
2705 | | int rc = 0; |
2706 | | struct os_mbuf *m; |
2707 | | int write_sz = 0; |
2708 | | m = os_msys_get_pkthdr(sz, 0); |
2709 | | if (!m) { |
2710 | | WOLFSSL_MSG("Mynewt Send os_msys_get_pkthdr error"); |
2711 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2712 | | } |
2713 | | rc = os_mbuf_copyinto(m, 0, buf, sz); |
2714 | | if (rc != 0) { |
2715 | | WOLFSSL_MSG("Mynewt Send os_mbuf_copyinto error"); |
2716 | | os_mbuf_free_chain(m); |
2717 | | return rc; |
2718 | | } |
2719 | | rc = mn_sendto(mynewt_ctx->mnSocket, m, (struct mn_sockaddr *)&mynewt_ctx->mnSockAddrIn); |
2720 | | if(rc != 0) { |
2721 | | WOLFSSL_MSG("Mynewt Send mn_sendto error"); |
2722 | | os_mbuf_free_chain(m); |
2723 | | return rc; |
2724 | | } |
2725 | | write_sz = sz; |
2726 | | return write_sz; |
2727 | | } |
2728 | | |
2729 | | /* like set_fd, but for default NetX context */ |
2730 | | void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, struct mn_sockaddr_in* mnSockAddrIn) |
2731 | | { |
2732 | | if (ssl && ssl->mnCtx) { |
2733 | | Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)ssl->mnCtx; |
2734 | | mynewt_ctx->mnSocket = mnSocket; |
2735 | | XMEMCPY(&mynewt_ctx->mnSockAddrIn, mnSockAddrIn, sizeof(struct mn_sockaddr_in)); |
2736 | | mn_socket_set_cbs(mynewt_ctx->mnSocket, mnSocket, &mynewt_sock_cbs); |
2737 | | } |
2738 | | } |
2739 | | |
2740 | | #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ |
2741 | | |
2742 | | #ifdef WOLFSSL_UIP |
2743 | | #include <uip.h> |
2744 | | #include <stdio.h> |
2745 | | |
2746 | | /* uIP TCP/IP port, using the native tcp/udp socket api. |
2747 | | * TCP and UDP are currently supported with the callbacks below. |
2748 | | * |
2749 | | */ |
2750 | | /* The uIP tcp send callback |
2751 | | * return : bytes sent, or error |
2752 | | */ |
2753 | | int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* _ctx) |
2754 | | { |
2755 | | uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; |
2756 | | int total_written = 0; |
2757 | | (void)ssl; |
2758 | | do { |
2759 | | int ret; |
2760 | | unsigned int bytes_left = sz - total_written; |
2761 | | unsigned int max_sendlen = tcp_socket_max_sendlen(&ctx->conn.tcp); |
2762 | | if (bytes_left > max_sendlen) { |
2763 | | fprintf(stderr, "uIPSend: Send limited by buffer\r\n"); |
2764 | | bytes_left = max_sendlen; |
2765 | | } |
2766 | | if (bytes_left == 0) { |
2767 | | fprintf(stderr, "uIPSend: Buffer full!\r\n"); |
2768 | | break; |
2769 | | } |
2770 | | ret = tcp_socket_send(&ctx->conn.tcp, (unsigned char *)buf + total_written, bytes_left); |
2771 | | if (ret <= 0) |
2772 | | break; |
2773 | | total_written += ret; |
2774 | | } while(total_written < sz); |
2775 | | if (total_written == 0) |
2776 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
2777 | | return total_written; |
2778 | | } |
2779 | | |
2780 | | int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) |
2781 | | { |
2782 | | uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; |
2783 | | int ret = 0; |
2784 | | (void)ssl; |
2785 | | ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port ); |
2786 | | if (ret == 0) |
2787 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
2788 | | return ret; |
2789 | | } |
2790 | | |
2791 | | /* The uIP uTCP/IP receive callback |
2792 | | * return : nb bytes read, or error |
2793 | | */ |
2794 | | int uIPReceive(WOLFSSL *ssl, char *buf, int sz, void *_ctx) |
2795 | | { |
2796 | | uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx; |
2797 | | if (!ctx || !ctx->ssl_rx_databuf) |
2798 | | return -1; |
2799 | | (void)ssl; |
2800 | | if (ctx->ssl_rb_len > 0) { |
2801 | | if (sz > ctx->ssl_rb_len - ctx->ssl_rb_off) |
2802 | | sz = ctx->ssl_rb_len - ctx->ssl_rb_off; |
2803 | | XMEMCPY(buf, ctx->ssl_rx_databuf + ctx->ssl_rb_off, sz); |
2804 | | ctx->ssl_rb_off += sz; |
2805 | | if (ctx->ssl_rb_off >= ctx->ssl_rb_len) { |
2806 | | ctx->ssl_rb_len = 0; |
2807 | | ctx->ssl_rb_off = 0; |
2808 | | } |
2809 | | return sz; |
2810 | | } else { |
2811 | | return WOLFSSL_CBIO_ERR_WANT_READ; |
2812 | | } |
2813 | | } |
2814 | | |
2815 | | /* uIP DTLS Generate Cookie callback |
2816 | | * return : number of bytes copied into buf, or error |
2817 | | */ |
2818 | | #if defined(NO_SHA) && !defined(NO_SHA256) |
2819 | | #define UIP_COOKIE_DIGEST_SIZE WC_SHA256_DIGEST_SIZE |
2820 | | #elif !defined(NO_SHA) |
2821 | | #define UIP_COOKIE_DIGEST_SIZE WC_SHA_DIGEST_SIZE |
2822 | | #else |
2823 | | #error Must enable either SHA-1 or SHA256 (or both) for uIP. |
2824 | | #endif |
2825 | | int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) |
2826 | | { |
2827 | | uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx; |
2828 | | byte token[32]; |
2829 | | byte digest[UIP_COOKIE_DIGEST_SIZE]; |
2830 | | int ret = 0; |
2831 | | XMEMSET(token, 0, sizeof(token)); |
2832 | | XMEMCPY(token, &ctx->peer_addr, sizeof(uip_ipaddr_t)); |
2833 | | XMEMCPY(token + sizeof(uip_ipaddr_t), &ctx->peer_port, sizeof(word16)); |
2834 | | #if defined(NO_SHA) && !defined(NO_SHA256) |
2835 | | ret = wc_Sha256Hash(token, sizeof(uip_ipaddr_t) + sizeof(word16), digest); |
2836 | | #else |
2837 | | ret = wc_ShaHash(token, sizeof(uip_ipaddr_t) + sizeof(word16), digest); |
2838 | | #endif |
2839 | | if (ret != 0) |
2840 | | return ret; |
2841 | | if (sz > UIP_COOKIE_DIGEST_SIZE) |
2842 | | sz = UIP_COOKIE_DIGEST_SIZE; |
2843 | | XMEMCPY(buf, digest, sz); |
2844 | | return sz; |
2845 | | } |
2846 | | |
2847 | | #endif /* WOLFSSL_UIP */ |
2848 | | |
2849 | | #ifdef WOLFSSL_GNRC |
2850 | | |
2851 | | #include <net/sock.h> |
2852 | | #include <net/sock/tcp.h> |
2853 | | #include <stdio.h> |
2854 | | |
2855 | | /* GNRC TCP/IP port, using the native tcp/udp socket api. |
2856 | | * TCP and UDP are currently supported with the callbacks below. |
2857 | | * |
2858 | | */ |
2859 | | /* The GNRC tcp send callback |
2860 | | * return : bytes sent, or error |
2861 | | */ |
2862 | | |
2863 | | int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) |
2864 | | { |
2865 | | sock_tls_t *ctx = (sock_tls_t *)_ctx; |
2866 | | int ret = 0; |
2867 | | (void)ssl; |
2868 | | if (!ctx) |
2869 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2870 | | ret = sock_udp_send(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr); |
2871 | | if (ret == 0) |
2872 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
2873 | | return ret; |
2874 | | } |
2875 | | |
2876 | | /* The GNRC TCP/IP receive callback |
2877 | | * return : nb bytes read, or error |
2878 | | */ |
2879 | | int GNRC_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *_ctx) |
2880 | | { |
2881 | | sock_udp_ep_t ep; |
2882 | | int ret; |
2883 | | word32 timeout = wolfSSL_dtls_get_current_timeout(ssl) * 1000000; |
2884 | | sock_tls_t *ctx = (sock_tls_t *)_ctx; |
2885 | | if (!ctx) |
2886 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2887 | | (void)ssl; |
2888 | | if (wolfSSL_get_using_nonblock(ctx->ssl)) { |
2889 | | timeout = 0; |
2890 | | } |
2891 | | ret = sock_udp_recv(&ctx->conn.udp, buf, sz, timeout, &ep); |
2892 | | if (ret > 0) { |
2893 | | if (ctx->peer_addr.port == 0) |
2894 | | XMEMCPY(&ctx->peer_addr, &ep, sizeof(sock_udp_ep_t)); |
2895 | | } |
2896 | | if (ret == -ETIMEDOUT) { |
2897 | | return WOLFSSL_CBIO_ERR_WANT_READ; |
2898 | | } |
2899 | | return ret; |
2900 | | } |
2901 | | |
2902 | | /* GNRC DTLS Generate Cookie callback |
2903 | | * return : number of bytes copied into buf, or error |
2904 | | */ |
2905 | | #define GNRC_MAX_TOKEN_SIZE (32) |
2906 | | #if defined(NO_SHA) && !defined(NO_SHA256) |
2907 | | #define GNRC_COOKIE_DIGEST_SIZE WC_SHA256_DIGEST_SIZE |
2908 | | #elif !defined(NO_SHA) |
2909 | | #define GNRC_COOKIE_DIGEST_SIZE WC_SHA_DIGEST_SIZE |
2910 | | #else |
2911 | | #error Must enable either SHA-1 or SHA256 (or both) for GNRC. |
2912 | | #endif |
2913 | | int GNRC_GenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) |
2914 | | { |
2915 | | sock_tls_t *ctx = (sock_tls_t *)_ctx; |
2916 | | if (!ctx) |
2917 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2918 | | byte token[GNRC_MAX_TOKEN_SIZE]; |
2919 | | byte digest[GNRC_COOKIE_DIGEST_SIZE]; |
2920 | | int ret = 0; |
2921 | | size_t token_size = sizeof(sock_udp_ep_t); |
2922 | | (void)ssl; |
2923 | | if (token_size > GNRC_MAX_TOKEN_SIZE) |
2924 | | token_size = GNRC_MAX_TOKEN_SIZE; |
2925 | | XMEMSET(token, 0, GNRC_MAX_TOKEN_SIZE); |
2926 | | XMEMCPY(token, &ctx->peer_addr, token_size); |
2927 | | #if defined(NO_SHA) && !defined(NO_SHA256) |
2928 | | ret = wc_Sha256Hash(token, token_size, digest); |
2929 | | #else |
2930 | | ret = wc_ShaHash(token, token_size, digest); |
2931 | | #endif |
2932 | | if (ret != 0) |
2933 | | return ret; |
2934 | | if (sz > GNRC_COOKIE_DIGEST_SIZE) |
2935 | | sz = GNRC_COOKIE_DIGEST_SIZE; |
2936 | | XMEMCPY(buf, digest, sz); |
2937 | | return sz; |
2938 | | } |
2939 | | |
2940 | | #endif /* WOLFSSL_GNRC */ |
2941 | | |
2942 | | #ifdef WOLFSSL_LWIP_NATIVE |
2943 | | int LwIPNativeSend(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
2944 | | { |
2945 | | err_t ret; |
2946 | | WOLFSSL_LWIP_NATIVE_STATE* nlwip = (WOLFSSL_LWIP_NATIVE_STATE*)ctx; |
2947 | | |
2948 | | ret = tcp_write(nlwip->pcb, buf, sz, TCP_WRITE_FLAG_COPY); |
2949 | | if (ret != ERR_OK) { |
2950 | | sz = -1; |
2951 | | } |
2952 | | |
2953 | | return sz; |
2954 | | } |
2955 | | |
2956 | | |
2957 | | int LwIPNativeReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
2958 | | { |
2959 | | struct pbuf *current, *head; |
2960 | | WOLFSSL_LWIP_NATIVE_STATE* nlwip; |
2961 | | int ret = 0; |
2962 | | |
2963 | | if (ctx == NULL) { |
2964 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2965 | | } |
2966 | | nlwip = (WOLFSSL_LWIP_NATIVE_STATE*)ctx; |
2967 | | |
2968 | | current = nlwip->pbuf; |
2969 | | if (current == NULL || sz > current->tot_len) { |
2970 | | WOLFSSL_MSG("LwIP native pbuf list is null or not enough data, want read"); |
2971 | | ret = WOLFSSL_CBIO_ERR_WANT_READ; |
2972 | | } |
2973 | | else { |
2974 | | int read = 0; /* total amount read */ |
2975 | | head = nlwip->pbuf; /* save pointer to current head */ |
2976 | | |
2977 | | /* loop through buffers reading data */ |
2978 | | while (current != NULL) { |
2979 | | int len; /* current amount to be read */ |
2980 | | |
2981 | | len = (current->len - nlwip->pulled < sz) ? |
2982 | | (current->len - nlwip->pulled) : sz; |
2983 | | |
2984 | | if (read + len > sz) { |
2985 | | /* should never be hit but have sanity check before use */ |
2986 | | return WOLFSSL_CBIO_ERR_GENERAL; |
2987 | | } |
2988 | | |
2989 | | /* check if is a partial read from before */ |
2990 | | XMEMCPY(&buf[read], |
2991 | | (const char *)&(((char *)(current->payload))[nlwip->pulled]), |
2992 | | |
2993 | | len); |
2994 | | nlwip->pulled = nlwip->pulled + len; |
2995 | | if (nlwip->pulled >= current->len) { |
2996 | | WOLFSSL_MSG("Native LwIP read full pbuf"); |
2997 | | nlwip->pbuf = current->next; |
2998 | | current = nlwip->pbuf; |
2999 | | nlwip->pulled = 0; |
3000 | | } |
3001 | | read = read + len; |
3002 | | ret = read; |
3003 | | |
3004 | | /* read enough break out */ |
3005 | | if (read >= sz) { |
3006 | | /* if more pbuf's are left in the chain then increment the |
3007 | | * ref count for next in chain and free all from beginning till |
3008 | | * next */ |
3009 | | if (current != NULL) { |
3010 | | pbuf_ref(current); |
3011 | | } |
3012 | | |
3013 | | /* ack and start free'ing from the current head of the chain */ |
3014 | | pbuf_free(head); |
3015 | | break; |
3016 | | } |
3017 | | } |
3018 | | } |
3019 | | WOLFSSL_LEAVE("LwIPNativeReceive", ret); |
3020 | | return ret; |
3021 | | } |
3022 | | |
3023 | | |
3024 | | static err_t LwIPNativeReceiveCB(void* cb, struct tcp_pcb* pcb, |
3025 | | struct pbuf* pbuf, err_t err) |
3026 | | { |
3027 | | WOLFSSL_LWIP_NATIVE_STATE* nlwip; |
3028 | | |
3029 | | if (cb == NULL || pcb == NULL) { |
3030 | | WOLFSSL_MSG("Expected callback was null, abort"); |
3031 | | return ERR_ABRT; |
3032 | | } |
3033 | | |
3034 | | nlwip = (WOLFSSL_LWIP_NATIVE_STATE*)cb; |
3035 | | if (pbuf == NULL && err == ERR_OK) { |
3036 | | return ERR_OK; |
3037 | | } |
3038 | | |
3039 | | if (nlwip->pbuf == NULL) { |
3040 | | nlwip->pbuf = pbuf; |
3041 | | } |
3042 | | else { |
3043 | | if (nlwip->pbuf != pbuf) { |
3044 | | tcp_recved(nlwip->pcb, pbuf->tot_len); |
3045 | | pbuf_cat(nlwip->pbuf, pbuf); /* add chain to head */ |
3046 | | } |
3047 | | } |
3048 | | |
3049 | | if (nlwip->recv_fn) { |
3050 | | return nlwip->recv_fn(nlwip->arg, pcb, pbuf, err); |
3051 | | } |
3052 | | |
3053 | | WOLFSSL_LEAVE("LwIPNativeReceiveCB", nlwip->pbuf->tot_len); |
3054 | | return ERR_OK; |
3055 | | } |
3056 | | |
3057 | | |
3058 | | static err_t LwIPNativeSentCB(void* cb, struct tcp_pcb* pcb, u16_t len) |
3059 | | { |
3060 | | WOLFSSL_LWIP_NATIVE_STATE* nlwip; |
3061 | | |
3062 | | if (cb == NULL || pcb == NULL) { |
3063 | | WOLFSSL_MSG("Expected callback was null, abort"); |
3064 | | return ERR_ABRT; |
3065 | | } |
3066 | | |
3067 | | nlwip = (WOLFSSL_LWIP_NATIVE_STATE*)cb; |
3068 | | if (nlwip->sent_fn) { |
3069 | | return nlwip->sent_fn(nlwip->arg, pcb, len); |
3070 | | } |
3071 | | return ERR_OK; |
3072 | | } |
3073 | | |
3074 | | |
3075 | | int wolfSSL_SetIO_LwIP(WOLFSSL* ssl, void* pcb, |
3076 | | tcp_recv_fn recv_fn, tcp_sent_fn sent_fn, void *arg) |
3077 | | { |
3078 | | if (ssl == NULL || pcb == NULL) |
3079 | | return BAD_FUNC_ARG; |
3080 | | |
3081 | | ssl->lwipCtx.pcb = (struct tcp_pcb *)pcb; |
3082 | | ssl->lwipCtx.recv_fn = recv_fn; /* recv user callback */ |
3083 | | ssl->lwipCtx.sent_fn = sent_fn; /* sent user callback */ |
3084 | | ssl->lwipCtx.arg = arg; |
3085 | | ssl->lwipCtx.pbuf = 0; |
3086 | | ssl->lwipCtx.pulled = 0; |
3087 | | ssl->lwipCtx.wait = 0; |
3088 | | |
3089 | | /* wolfSSL_LwIP_recv/sent_cb invokes recv/sent user callback in them. */ |
3090 | | tcp_recv(pcb, LwIPNativeReceiveCB); |
3091 | | tcp_sent(pcb, LwIPNativeSentCB); |
3092 | | tcp_arg (pcb, (void *)&ssl->lwipCtx); |
3093 | | wolfSSL_SetIOReadCtx(ssl, &ssl->lwipCtx); |
3094 | | wolfSSL_SetIOWriteCtx(ssl, &ssl->lwipCtx); |
3095 | | |
3096 | | return ERR_OK; |
3097 | | } |
3098 | | #endif |
3099 | | |
3100 | | #ifdef WOLFSSL_ISOTP |
3101 | | static int isotp_send_single_frame(struct isotp_wolfssl_ctx *ctx, char *buf, |
3102 | | word16 length) |
3103 | | { |
3104 | | /* Length will be at most 7 bytes to get here. Packet is length and type |
3105 | | * for the first byte, then up to 7 bytes of data */ |
3106 | | ctx->frame.data[0] = ((byte)length) | (ISOTP_FRAME_TYPE_SINGLE << 4); |
3107 | | XMEMCPY(&ctx->frame.data[1], buf, length); |
3108 | | ctx->frame.length = length + 1; |
3109 | | return ctx->send_fn(&ctx->frame, ctx->arg); |
3110 | | } |
3111 | | |
3112 | | static int isotp_send_flow_control(struct isotp_wolfssl_ctx *ctx, |
3113 | | byte overflow) |
3114 | | { |
3115 | | int ret; |
3116 | | /* Overflow is set it if we have been asked to receive more data than the |
3117 | | * user allocated a buffer for */ |
3118 | | if (overflow) { |
3119 | | ctx->frame.data[0] = ISOTP_FLOW_CONTROL_ABORT | |
3120 | | (ISOTP_FRAME_TYPE_CONTROL << 4); |
3121 | | } else { |
3122 | | ctx->frame.data[0] = ISOTP_FLOW_CONTROL_CTS | |
3123 | | (ISOTP_FRAME_TYPE_CONTROL << 4); |
3124 | | } |
3125 | | /* Set the number of frames between flow control to infinite */ |
3126 | | ctx->frame.data[1] = ISOTP_FLOW_CONTROL_FRAMES; |
3127 | | /* User specified frame delay */ |
3128 | | ctx->frame.data[2] = ctx->receive_delay; |
3129 | | ctx->frame.length = ISOTP_FLOW_CONTROL_PACKET_SIZE; |
3130 | | ret = ctx->send_fn(&ctx->frame, ctx->arg); |
3131 | | return ret; |
3132 | | } |
3133 | | |
3134 | | static int isotp_receive_flow_control(struct isotp_wolfssl_ctx *ctx) |
3135 | | { |
3136 | | int ret; |
3137 | | enum isotp_frame_type type; |
3138 | | enum isotp_flow_control flow_control; |
3139 | | ret = ctx->recv_fn(&ctx->frame, ctx->arg, ISOTP_DEFAULT_TIMEOUT); |
3140 | | if (ret == 0) { |
3141 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
3142 | | } else if (ret < 0) { |
3143 | | WOLFSSL_MSG("ISO-TP error receiving flow control packet"); |
3144 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3145 | | } |
3146 | | /* Flow control is the frame type and flow response for the first byte, |
3147 | | * number of frames until the next flow control packet for the second |
3148 | | * byte, time between frames for the third byte */ |
3149 | | type = ctx->frame.data[0] >> 4; |
3150 | | |
3151 | | if (type != ISOTP_FRAME_TYPE_CONTROL) { |
3152 | | WOLFSSL_MSG("ISO-TP frames out of sequence"); |
3153 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3154 | | } |
3155 | | |
3156 | | flow_control = ctx->frame.data[0] & 0xf; |
3157 | | |
3158 | | ctx->flow_counter = 0; |
3159 | | ctx->flow_packets = ctx->frame.data[1]; |
3160 | | ctx->frame_delay = ctx->frame.data[2]; |
3161 | | |
3162 | | return flow_control; |
3163 | | } |
3164 | | |
3165 | | static int isotp_send_consecutive_frame(struct isotp_wolfssl_ctx *ctx) |
3166 | | { |
3167 | | /* Sequence is 0 - 15 and then starts again, the first frame has an |
3168 | | * implied sequence of '0' */ |
3169 | | ctx->sequence += 1; |
3170 | | if (ctx->sequence > ISOTP_MAX_SEQUENCE_COUNTER) { |
3171 | | ctx->sequence = 0; |
3172 | | } |
3173 | | ctx->flow_counter++; |
3174 | | /* First byte it type and sequence number, up to 7 bytes of data */ |
3175 | | ctx->frame.data[0] = ctx->sequence | (ISOTP_FRAME_TYPE_CONSECUTIVE << 4); |
3176 | | if (ctx->buf_length > ISOTP_MAX_CONSECUTIVE_FRAME_DATA_SIZE) { |
3177 | | XMEMCPY(&ctx->frame.data[1], ctx->buf_ptr, |
3178 | | ISOTP_MAX_CONSECUTIVE_FRAME_DATA_SIZE); |
3179 | | ctx->buf_ptr += ISOTP_MAX_CONSECUTIVE_FRAME_DATA_SIZE; |
3180 | | ctx->buf_length -= ISOTP_MAX_CONSECUTIVE_FRAME_DATA_SIZE; |
3181 | | ctx->frame.length = ISOTP_CAN_BUS_PAYLOAD_SIZE; |
3182 | | } else { |
3183 | | XMEMCPY(&ctx->frame.data[1], ctx->buf_ptr, ctx->buf_length); |
3184 | | ctx->frame.length = ctx->buf_length + 1; |
3185 | | ctx->buf_length = 0; |
3186 | | } |
3187 | | return ctx->send_fn(&ctx->frame, ctx->arg); |
3188 | | |
3189 | | } |
3190 | | |
3191 | | static int isotp_send_first_frame(struct isotp_wolfssl_ctx *ctx, char *buf, |
3192 | | word16 length) |
3193 | | { |
3194 | | int ret; |
3195 | | ctx->sequence = 0; |
3196 | | /* Set to 1 to trigger a flow control straight away, the flow control |
3197 | | * packet will set these properly */ |
3198 | | ctx->flow_packets = ctx->flow_counter = 1; |
3199 | | /* First frame has 1 nibble for type, 3 nibbles for length followed by |
3200 | | * 6 bytes for data*/ |
3201 | | ctx->frame.data[0] = (length >> 8) | (ISOTP_FRAME_TYPE_FIRST << 4); |
3202 | | ctx->frame.data[1] = length & 0xff; |
3203 | | XMEMCPY(&ctx->frame.data[2], buf, ISOTP_FIRST_FRAME_DATA_SIZE); |
3204 | | ctx->buf_ptr = buf + ISOTP_FIRST_FRAME_DATA_SIZE; |
3205 | | ctx->buf_length = length - ISOTP_FIRST_FRAME_DATA_SIZE; |
3206 | | ctx->frame.length = ISOTP_CAN_BUS_PAYLOAD_SIZE; |
3207 | | ret = ctx->send_fn(&ctx->frame, ctx->arg); |
3208 | | if (ret <= 0) { |
3209 | | WOLFSSL_MSG("ISO-TP error sending first frame"); |
3210 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3211 | | } |
3212 | | while(ctx->buf_length) { |
3213 | | /* The receiver can set how often to get a flow control packet. If it |
3214 | | * is time, then get the packet. Note that this will always happen |
3215 | | * after the first packet */ |
3216 | | if ((ctx->flow_packets > 0) && |
3217 | | (ctx->flow_counter == ctx->flow_packets)) { |
3218 | | ret = isotp_receive_flow_control(ctx); |
3219 | | } |
3220 | | /* Frame delay <= 0x7f is in ms, 0xfX is X * 100 us */ |
3221 | | if (ctx->frame_delay) { |
3222 | | if (ctx->frame_delay <= ISOTP_MAX_MS_FRAME_DELAY) { |
3223 | | ctx->delay_fn(ctx->frame_delay * 1000); |
3224 | | } else { |
3225 | | ctx->delay_fn((ctx->frame_delay & 0xf) * 100); |
3226 | | } |
3227 | | } |
3228 | | switch (ret) { |
3229 | | /* Clear to send */ |
3230 | | case ISOTP_FLOW_CONTROL_CTS: |
3231 | | if (isotp_send_consecutive_frame(ctx) < 0) { |
3232 | | WOLFSSL_MSG("ISO-TP error sending consecutive frame"); |
3233 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3234 | | } |
3235 | | break; |
3236 | | /* Receiver says "WAIT", so we wait for another flow control |
3237 | | * packet, or abort if we have waited too long */ |
3238 | | case ISOTP_FLOW_CONTROL_WAIT: |
3239 | | ctx->wait_counter += 1; |
3240 | | if (ctx->wait_counter > ISOTP_DEFAULT_WAIT_COUNT) { |
3241 | | WOLFSSL_MSG("ISO-TP receiver told us to wait too many" |
3242 | | " times"); |
3243 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
3244 | | } |
3245 | | break; |
3246 | | /* Receiver is not ready to receive packet, so abort */ |
3247 | | case ISOTP_FLOW_CONTROL_ABORT: |
3248 | | WOLFSSL_MSG("ISO-TP receiver aborted transmission"); |
3249 | | return WOLFSSL_CBIO_ERR_WANT_WRITE; |
3250 | | default: |
3251 | | WOLFSSL_MSG("ISO-TP got unexpected flow control packet"); |
3252 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3253 | | } |
3254 | | } |
3255 | | return 0; |
3256 | | } |
3257 | | |
3258 | | int ISOTP_Send(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
3259 | | { |
3260 | | int ret; |
3261 | | struct isotp_wolfssl_ctx *isotp_ctx; |
3262 | | (void) ssl; |
3263 | | |
3264 | | if (!ctx) { |
3265 | | WOLFSSL_MSG("ISO-TP requires wolfSSL_SetIO_ISOTP to be called first"); |
3266 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3267 | | } |
3268 | | isotp_ctx = (struct isotp_wolfssl_ctx*) ctx; |
3269 | | |
3270 | | /* ISO-TP cannot send more than 4095 bytes, this limits the packet size |
3271 | | * and wolfSSL will try again with the remaining data */ |
3272 | | if (sz > ISOTP_MAX_DATA_SIZE) { |
3273 | | sz = ISOTP_MAX_DATA_SIZE; |
3274 | | } |
3275 | | /* Can't send whilst we are receiving */ |
3276 | | if (isotp_ctx->state != ISOTP_CONN_STATE_IDLE) { |
3277 | | return WOLFSSL_ERROR_WANT_WRITE; |
3278 | | } |
3279 | | isotp_ctx->state = ISOTP_CONN_STATE_SENDING; |
3280 | | |
3281 | | /* Assuming normal addressing */ |
3282 | | if (sz <= ISOTP_SINGLE_FRAME_DATA_SIZE) { |
3283 | | ret = isotp_send_single_frame(isotp_ctx, buf, (word16)sz); |
3284 | | } else { |
3285 | | ret = isotp_send_first_frame(isotp_ctx, buf, (word16)sz); |
3286 | | } |
3287 | | isotp_ctx->state = ISOTP_CONN_STATE_IDLE; |
3288 | | |
3289 | | if (ret == 0) { |
3290 | | return sz; |
3291 | | } |
3292 | | return ret; |
3293 | | } |
3294 | | |
3295 | | static int isotp_receive_single_frame(struct isotp_wolfssl_ctx *ctx) |
3296 | | { |
3297 | | byte data_size; |
3298 | | |
3299 | | /* 1 nibble for data size which will be 1 - 7 in a regular 8 byte CAN |
3300 | | * packet */ |
3301 | | data_size = (byte)ctx->frame.data[0] & 0xf; |
3302 | | if (ctx->receive_buffer_size < (int)data_size) { |
3303 | | WOLFSSL_MSG("ISO-TP buffer is too small to receive data"); |
3304 | | return BUFFER_E; |
3305 | | } |
3306 | | XMEMCPY(ctx->receive_buffer, &ctx->frame.data[1], data_size); |
3307 | | return data_size; |
3308 | | } |
3309 | | |
3310 | | static int isotp_receive_multi_frame(struct isotp_wolfssl_ctx *ctx) |
3311 | | { |
3312 | | int ret; |
3313 | | word16 data_size; |
3314 | | byte delay = 0; |
3315 | | |
3316 | | /* Increase receive timeout for enforced ms delay */ |
3317 | | if (ctx->receive_delay <= ISOTP_MAX_MS_FRAME_DELAY) { |
3318 | | delay = ctx->receive_delay; |
3319 | | } |
3320 | | /* Still processing first frame. |
3321 | | * Full data size is lower nibble of first byte for the most significant |
3322 | | * followed by the second byte for the rest. Last 6 bytes are data */ |
3323 | | data_size = ((ctx->frame.data[0] & 0xf) << 8) + ctx->frame.data[1]; |
3324 | | XMEMCPY(ctx->receive_buffer, &ctx->frame.data[2], ISOTP_FIRST_FRAME_DATA_SIZE); |
3325 | | /* Need to send a flow control packet to either cancel or continue |
3326 | | * transmission of data */ |
3327 | | if (ctx->receive_buffer_size < data_size) { |
3328 | | isotp_send_flow_control(ctx, TRUE); |
3329 | | WOLFSSL_MSG("ISO-TP buffer is too small to receive data"); |
3330 | | return BUFFER_E; |
3331 | | } |
3332 | | isotp_send_flow_control(ctx, FALSE); |
3333 | | |
3334 | | ctx->buf_length = ISOTP_FIRST_FRAME_DATA_SIZE; |
3335 | | ctx->buf_ptr = ctx->receive_buffer + ISOTP_FIRST_FRAME_DATA_SIZE; |
3336 | | data_size -= ISOTP_FIRST_FRAME_DATA_SIZE; |
3337 | | ctx->sequence = 1; |
3338 | | |
3339 | | while(data_size) { |
3340 | | enum isotp_frame_type type; |
3341 | | byte sequence; |
3342 | | byte frame_len; |
3343 | | ret = ctx->recv_fn(&ctx->frame, ctx->arg, ISOTP_DEFAULT_TIMEOUT + |
3344 | | (delay / 1000)); |
3345 | | if (ret == 0) { |
3346 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
3347 | | } |
3348 | | type = ctx->frame.data[0] >> 4; |
3349 | | /* Consecutive frames have sequence number as lower nibble */ |
3350 | | sequence = ctx->frame.data[0] & 0xf; |
3351 | | if (type != ISOTP_FRAME_TYPE_CONSECUTIVE) { |
3352 | | WOLFSSL_MSG("ISO-TP frames out of sequence"); |
3353 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3354 | | } |
3355 | | if (sequence != ctx->sequence) { |
3356 | | WOLFSSL_MSG("ISO-TP frames out of sequence"); |
3357 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3358 | | } |
3359 | | /* Last 7 bytes or whatever we got after the first byte is data */ |
3360 | | frame_len = ctx->frame.length - 1; |
3361 | | XMEMCPY(ctx->buf_ptr, &ctx->frame.data[1], frame_len); |
3362 | | ctx->buf_ptr += frame_len; |
3363 | | ctx->buf_length += frame_len; |
3364 | | data_size -= frame_len; |
3365 | | |
3366 | | /* Sequence is 0 - 15 (first 0 is implied for first packet */ |
3367 | | ctx->sequence++; |
3368 | | if (ctx->sequence > ISOTP_MAX_SEQUENCE_COUNTER) { |
3369 | | ctx->sequence = 0; |
3370 | | } |
3371 | | } |
3372 | | return ctx->buf_length; |
3373 | | |
3374 | | } |
3375 | | |
3376 | | /* The wolfSSL receive callback, needs to buffer because we need to grab all |
3377 | | * incoming data, even if wolfSSL doesn't want it all yet */ |
3378 | | int ISOTP_Receive(WOLFSSL* ssl, char* buf, int sz, void* ctx) |
3379 | | { |
3380 | | enum isotp_frame_type type; |
3381 | | int ret; |
3382 | | struct isotp_wolfssl_ctx *isotp_ctx; |
3383 | | (void) ssl; |
3384 | | |
3385 | | if (!ctx) { |
3386 | | WOLFSSL_MSG("ISO-TP requires wolfSSL_SetIO_ISOTP to be called first"); |
3387 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
3388 | | } |
3389 | | isotp_ctx = (struct isotp_wolfssl_ctx*)ctx; |
3390 | | |
3391 | | /* Is buffer empty? If so, fill it */ |
3392 | | if (!isotp_ctx->receive_buffer_len) { |
3393 | | /* Can't send whilst we are receiving */ |
3394 | | if (isotp_ctx->state != ISOTP_CONN_STATE_IDLE) { |
3395 | | return WOLFSSL_ERROR_WANT_READ; |
3396 | | } |
3397 | | isotp_ctx->state = ISOTP_CONN_STATE_RECEIVING; |
3398 | | do { |
3399 | | ret = isotp_ctx->recv_fn(&isotp_ctx->frame, isotp_ctx->arg, |
3400 | | ISOTP_DEFAULT_TIMEOUT); |
3401 | | } while (ret == 0); |
3402 | | if (ret == 0) { |
3403 | | isotp_ctx->state = ISOTP_CONN_STATE_IDLE; |
3404 | | return WOLFSSL_CBIO_ERR_TIMEOUT; |
3405 | | } else if (ret < 0) { |
3406 | | isotp_ctx->state = ISOTP_CONN_STATE_IDLE; |
3407 | | WOLFSSL_MSG("ISO-TP receive error"); |
3408 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3409 | | } |
3410 | | |
3411 | | type = (enum isotp_frame_type) isotp_ctx->frame.data[0] >> 4; |
3412 | | |
3413 | | if (type == ISOTP_FRAME_TYPE_SINGLE) { |
3414 | | isotp_ctx->receive_buffer_len = |
3415 | | isotp_receive_single_frame(isotp_ctx); |
3416 | | } else if (type == ISOTP_FRAME_TYPE_FIRST) { |
3417 | | isotp_ctx->receive_buffer_len = |
3418 | | isotp_receive_multi_frame(isotp_ctx); |
3419 | | } else { |
3420 | | /* Should never get here */ |
3421 | | isotp_ctx->state = ISOTP_CONN_STATE_IDLE; |
3422 | | WOLFSSL_MSG("ISO-TP frames out of sequence"); |
3423 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3424 | | } |
3425 | | if (isotp_ctx->receive_buffer_len <= 1) { |
3426 | | isotp_ctx->state = ISOTP_CONN_STATE_IDLE; |
3427 | | return isotp_ctx->receive_buffer_len; |
3428 | | } else { |
3429 | | isotp_ctx->receive_buffer_ptr = isotp_ctx->receive_buffer; |
3430 | | } |
3431 | | isotp_ctx->state = ISOTP_CONN_STATE_IDLE; |
3432 | | } |
3433 | | |
3434 | | /* Return from the buffer */ |
3435 | | if (isotp_ctx->receive_buffer_len >= sz) { |
3436 | | XMEMCPY(buf, isotp_ctx->receive_buffer_ptr, sz); |
3437 | | isotp_ctx->receive_buffer_ptr+= sz; |
3438 | | isotp_ctx->receive_buffer_len-= sz; |
3439 | | return sz; |
3440 | | } else { |
3441 | | XMEMCPY(buf, isotp_ctx->receive_buffer_ptr, |
3442 | | isotp_ctx->receive_buffer_len); |
3443 | | sz = isotp_ctx->receive_buffer_len; |
3444 | | isotp_ctx->receive_buffer_len = 0; |
3445 | | return sz; |
3446 | | } |
3447 | | } |
3448 | | |
3449 | | int wolfSSL_SetIO_ISOTP(WOLFSSL *ssl, isotp_wolfssl_ctx *ctx, |
3450 | | can_recv_fn recv_fn, can_send_fn send_fn, can_delay_fn delay_fn, |
3451 | | word32 receive_delay, char *receive_buffer, int receive_buffer_size, |
3452 | | void *arg) |
3453 | | { |
3454 | | if (!ctx || !recv_fn || !send_fn || !delay_fn || !receive_buffer) { |
3455 | | WOLFSSL_MSG("ISO-TP has missing required parameter"); |
3456 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3457 | | } |
3458 | | ctx->recv_fn = recv_fn; |
3459 | | ctx->send_fn = send_fn; |
3460 | | ctx->arg = arg; |
3461 | | ctx->delay_fn = delay_fn; |
3462 | | ctx->frame_delay = 0; |
3463 | | ctx->receive_buffer = receive_buffer; |
3464 | | ctx->receive_buffer_size = receive_buffer_size; |
3465 | | ctx->receive_buffer_len = 0; |
3466 | | ctx->state = ISOTP_CONN_STATE_IDLE; |
3467 | | |
3468 | | wolfSSL_SetIOReadCtx(ssl, ctx); |
3469 | | wolfSSL_SetIOWriteCtx(ssl, ctx); |
3470 | | |
3471 | | /* Delay of 100 - 900us is 0xfX where X is value / 100. Delay of |
3472 | | * >= 1000 is divided by 1000. > 127ms is invalid */ |
3473 | | if (receive_delay < 1000) { |
3474 | | ctx->receive_delay = 0xf0 + (receive_delay / 100); |
3475 | | } else if (receive_delay <= ISOTP_MAX_MS_FRAME_DELAY * 1000) { |
3476 | | ctx->receive_delay = receive_delay / 1000; |
3477 | | } else { |
3478 | | WOLFSSL_MSG("ISO-TP delay parameter out of bounds"); |
3479 | | return WOLFSSL_CBIO_ERR_GENERAL; |
3480 | | } |
3481 | | return 0; |
3482 | | } |
3483 | | #endif |
3484 | | #endif /* WOLFCRYPT_ONLY */ |