-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathwolfio.c
More file actions
3849 lines (3328 loc) · 115 KB
/
wolfio.c
File metadata and controls
3849 lines (3328 loc) · 115 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* wolfio.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFSSL_STRERROR_BUFFER_SIZE
#define WOLFSSL_STRERROR_BUFFER_SIZE 256
#endif
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifndef WOLFCRYPT_ONLY
#if defined(HAVE_ERRNO_H) && defined(WOLFSSL_NO_SOCK) && \
(defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT))
/* error codes are needed for TranslateIoReturnCode() and
* wolfIO_TcpConnect() even if defined(WOLFSSL_NO_SOCK), which inhibits
* inclusion of errno.h by wolfio.h.
*/
#include <errno.h>
#endif
#include <wolfssl/internal.h>
#include <wolfssl/error-ssl.h>
#include <wolfssl/wolfio.h>
#include <wolfssl/wolfcrypt/logging.h>
#ifdef NUCLEUS_PLUS_2_3
/* Holds last Nucleus networking error number */
int Nucleus_Net_Errno;
#endif
#if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT)
#ifdef USE_WINDOWS_API
#include <winsock2.h>
#else
#if defined(WOLFSSL_LWIP) && !defined(WOLFSSL_APACHE_MYNEWT)
#elif defined(ARDUINO)
#elif defined(FREESCALE_MQX)
#elif defined(FREESCALE_KSDK_MQX)
#elif (defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET))
#elif defined(WOLFSSL_CMSIS_RTOS)
#elif defined(WOLFSSL_CMSIS_RTOSv2)
#elif defined(WOLFSSL_TIRTOS)
#elif defined(FREERTOS_TCP)
#elif defined(WOLFSSL_IAR_ARM)
#elif defined(HAVE_NETX_BSD)
#elif defined(WOLFSSL_VXWORKS)
#elif defined(WOLFSSL_NUCLEUS_1_2)
#elif defined(WOLFSSL_LINUXKM)
/* the requisite linux/net.h is included in wc_port.h, with incompatible warnings masked out. */
#elif defined(WOLFSSL_ATMEL)
#elif defined(INTIME_RTOS)
#include <netdb.h>
#elif defined(WOLFSSL_PRCONNECT_PRO)
#include <netdb.h>
#include <sys/ioctl.h>
#elif defined(WOLFSSL_SGX)
#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
#elif defined(WOLFSSL_DEOS)
#elif defined(WOLFSSL_ZEPHYR)
#elif defined(MICROCHIP_PIC32)
#elif defined(HAVE_NETX)
#elif defined(FUSION_RTOS)
#elif !defined(WOLFSSL_NO_SOCK)
#if defined(HAVE_RTP_SYS)
#elif defined(EBSNET)
#elif defined(NETOS)
#elif !defined(DEVKITPRO) && !defined(WOLFSSL_PICOTCP) \
&& !defined(WOLFSSL_CONTIKI) && !defined(WOLFSSL_WICED) \
&& !defined(WOLFSSL_GNRC) && !defined(WOLFSSL_RIOT_OS)
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef __PPU
#include <netex/errno.h>
#else
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#endif
#endif
#endif
#endif /* USE_WINDOWS_API */
#endif /* defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT) */
#if defined(HAVE_HTTP_CLIENT)
#include <stdlib.h> /* strtol() */
#endif
/*
Possible IO enable options:
* WOLFSSL_USER_IO: Disables default Embed* callbacks and default: off
allows user to define their own using
wolfSSL_CTX_SetIORecv and wolfSSL_CTX_SetIOSend
* USE_WOLFSSL_IO: Enables the wolfSSL IO functions default: on
* HAVE_HTTP_CLIENT: Enables HTTP client API's default: off
(unless HAVE_OCSP or HAVE_CRL_IO defined)
* HAVE_IO_TIMEOUT: Enables support for connect timeout default: off
*
* DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER: This flag has effect only if
* ASN_NO_TIME is enabled. If enabled invalid peers messages are ignored
* indefinitely. If not enabled EmbedReceiveFrom will return timeout after
* DTLS_RECEIVEFROM_MAX_INVALID_PEER number of packets from invalid peers. When
* enabled, without a timer, EmbedReceivefrom can't check if the timeout is
* expired and it may never return under a continuous flow of invalid packets.
* default: off
*/
/* if user writes own I/O callbacks they can define WOLFSSL_USER_IO to remove
automatic setting of default I/O functions EmbedSend() and EmbedReceive()
but they'll still need SetCallback xxx() at end of file
*/
#if defined(NO_ASN_TIME) && !defined(DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER) \
&& !defined(DTLS_RECEIVEFROM_MAX_INVALID_PEER)
#define DTLS_RECEIVEFROM_MAX_INVALID_PEER 10
#endif
#if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT)
static WC_INLINE int wolfSSL_LastError(int err, SOCKET_T sd)
{
(void)sd;
if (err > 0)
return 0;
#ifdef USE_WINDOWS_API
return WSAGetLastError();
#elif defined(EBSNET)
return xn_getlasterror();
#elif defined(WOLFSSL_LINUXKM) || defined(WOLFSSL_EMNET)
return -err; /* Return provided error value with corrected sign. */
#elif defined(FUSION_RTOS)
#include <fclerrno.h>
return FCL_GET_ERRNO;
#elif defined(NUCLEUS_PLUS_2_3)
return Nucleus_Net_Errno;
#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
if ((err == 0) || (err == -SOCKET_EWOULDBLOCK)) {
return SOCKET_EWOULDBLOCK; /* convert to BSD style wouldblock */
} else {
err = RTCS_geterror(sd);
if ((err == RTCSERR_TCP_CONN_CLOSING) ||
(err == RTCSERR_TCP_CONN_RLSD))
{
err = SOCKET_ECONNRESET;
}
return err;
}
#elif defined(WOLFSSL_EMNET)
/* Get the real socket error */
IP_SOCK_getsockopt(sd, SOL_SOCKET, SO_ERROR, &err, (int)sizeof(old));
return err;
#else
return errno;
#endif
}
/* Translates return codes returned from
* send(), recv(), and other network I/O calls.
*/
static int TranslateIoReturnCode(int err, SOCKET_T sd, int direction)
{
#if defined(_WIN32) && !defined(__WATCOMC__) && !defined(_WIN32_WCE) && \
!defined(INTIME_RTOS)
size_t errstr_offset;
char errstr[WOLFSSL_STRERROR_BUFFER_SIZE];
#endif /* _WIN32 */
#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
if (err > 0)
return err;
#else
if (err >= 0)
return err;
#endif
err = wolfSSL_LastError(err, sd);
#if SOCKET_EWOULDBLOCK != SOCKET_EAGAIN
if ((err == SOCKET_EWOULDBLOCK) || (err == SOCKET_EAGAIN))
#else
if (err == SOCKET_EWOULDBLOCK)
#endif
{
WOLFSSL_MSG("\tWould block");
if (direction == SOCKET_SENDING)
return WOLFSSL_CBIO_ERR_WANT_WRITE;
else if (direction == SOCKET_RECEIVING)
return WOLFSSL_CBIO_ERR_WANT_READ;
else
return WOLFSSL_CBIO_ERR_GENERAL;
}
#ifdef SOCKET_ETIMEDOUT
else if (err == SOCKET_ETIMEDOUT) {
WOLFSSL_MSG("\tTimed out");
if (direction == SOCKET_SENDING)
return WOLFSSL_CBIO_ERR_WANT_WRITE;
else if (direction == SOCKET_RECEIVING)
return WOLFSSL_CBIO_ERR_WANT_READ;
else
return WOLFSSL_CBIO_ERR_TIMEOUT;
}
#endif /* SOCKET_ETIMEDOUT */
else if (err == SOCKET_ECONNRESET) {
WOLFSSL_MSG("\tConnection reset");
return WOLFSSL_CBIO_ERR_CONN_RST;
}
else if (err == SOCKET_EINTR) {
WOLFSSL_MSG("\tSocket interrupted");
return WOLFSSL_CBIO_ERR_ISR;
}
else if (err == SOCKET_EPIPE) {
WOLFSSL_MSG("\tBroken pipe");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
else if (err == SOCKET_ECONNABORTED) {
WOLFSSL_MSG("\tConnection aborted");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
#if defined(_WIN32) && !defined(__WATCOMC__) && !defined(_WIN32_WCE) && \
!defined(INTIME_RTOS)
strcpy_s(errstr, sizeof(errstr), "\tGeneral error: ");
errstr_offset = strlen(errstr);
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)(errstr + errstr_offset),
(DWORD)(sizeof(errstr) - errstr_offset),
NULL);
WOLFSSL_MSG(errstr);
#else
WOLFSSL_MSG_EX("\tGeneral error: %d", err);
#endif
return WOLFSSL_CBIO_ERR_GENERAL;
}
#endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */
#ifdef OPENSSL_EXTRA
#ifndef NO_BIO
int wolfSSL_BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
{
return SslBioSend(ssl, buf, sz, ctx);
}
int wolfSSL_BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx)
{
return SslBioReceive(ssl, buf, sz, ctx);
}
int BioReceiveInternal(WOLFSSL_BIO* biord, WOLFSSL_BIO* biowr, char* buf,
int sz)
{
int recvd = WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_GENERAL);
WOLFSSL_ENTER("SslBioReceive");
if (biord == NULL) {
WOLFSSL_MSG("WOLFSSL biord not set");
return WOLFSSL_CBIO_ERR_GENERAL;
}
recvd = wolfSSL_BIO_read(biord, buf, sz);
if (recvd <= 0) {
if (biowr != NULL &&
/* ssl->biowr->wrIdx is checked for Bind9 */
wolfSSL_BIO_method_type(biowr) == WOLFSSL_BIO_BIO &&
wolfSSL_BIO_wpending(biowr) != 0 &&
/* Not sure this pending check is necessary but let's double
* check that the read BIO is empty before we signal a write
* need */
wolfSSL_BIO_supports_pending(biord) &&
wolfSSL_BIO_ctrl_pending(biord) == 0) {
/* Let's signal to the app layer that we have
* data pending that needs to be sent. */
return WOLFSSL_CBIO_ERR_WANT_WRITE;
}
else if (biord->type == WOLFSSL_BIO_SOCKET) {
if (recvd == 0) {
WOLFSSL_MSG("SslBioReceive connection closed");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
#ifdef USE_WOLFSSL_IO
recvd = TranslateIoReturnCode(recvd, biord->num.fd,
SOCKET_RECEIVING);
#endif
return recvd;
}
/* If retry and read flags are set, return WANT_READ */
if ((biord->flags & WOLFSSL_BIO_FLAG_READ) &&
(biord->flags & WOLFSSL_BIO_FLAG_RETRY)) {
return WOLFSSL_CBIO_ERR_WANT_READ;
}
WOLFSSL_MSG("BIO general error");
return WOLFSSL_CBIO_ERR_GENERAL;
}
return recvd;
}
/* Use the WOLFSSL read BIO for receiving data. This is set by the function
* wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIORecv.
*
* ssl WOLFSSL struct passed in that has this function set as the receive
* callback.
* buf buffer to fill with data read
* sz size of buf buffer
* ctx a user set context
*
* returns the amount of data read or want read. See WOLFSSL_CBIO_ERR_* values.
*/
int SslBioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx)
{
WOLFSSL_ENTER("SslBioReceive");
(void)ctx;
return BioReceiveInternal(ssl->biord, ssl->biowr, buf, sz);
}
/* Use the WOLFSSL write BIO for sending data. This is set by the function
* wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIOSend.
*
* ssl WOLFSSL struct passed in that has this function set as the send callback.
* buf buffer with data to write out
* sz size of buf buffer
* ctx a user set context
*
* returns the amount of data sent or want send. See WOLFSSL_CBIO_ERR_* values.
*/
int SslBioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
{
int sent = WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_GENERAL);
WOLFSSL_ENTER("SslBioSend");
if (ssl->biowr == NULL) {
WOLFSSL_MSG("WOLFSSL biowr not set");
return WOLFSSL_CBIO_ERR_GENERAL;
}
sent = wolfSSL_BIO_write(ssl->biowr, buf, sz);
if (sent <= 0) {
if (ssl->biowr->type == WOLFSSL_BIO_SOCKET) {
#ifdef USE_WOLFSSL_IO
sent = TranslateIoReturnCode(sent, ssl->biowr->num.fd,
SOCKET_SENDING);
#endif
return sent;
}
else if (ssl->biowr->type == WOLFSSL_BIO_BIO) {
if (sent == WOLFSSL_BIO_ERROR) {
WOLFSSL_MSG("\tWould Block");
return WOLFSSL_CBIO_ERR_WANT_WRITE;
}
}
/* If retry and write flags are set, return WANT_WRITE */
if ((ssl->biowr->flags & WOLFSSL_BIO_FLAG_WRITE) &&
(ssl->biowr->flags & WOLFSSL_BIO_FLAG_RETRY)) {
return WOLFSSL_CBIO_ERR_WANT_WRITE;
}
return WOLFSSL_CBIO_ERR_GENERAL;
}
(void)ctx;
return sent;
}
#endif /* !NO_BIO */
#endif /* OPENSSL_EXTRA */
#ifdef USE_WOLFSSL_IO
/* The receive embedded callback
* return : nb bytes read, or error
*/
int EmbedReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
{
int recvd;
#ifndef WOLFSSL_LINUXKM
int sd = *(int*)ctx;
#else
struct socket *sd = (struct socket*)ctx;
#endif
recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags);
if (recvd < 0) {
WOLFSSL_MSG("Embed Receive error");
}
else if (recvd == 0) {
WOLFSSL_MSG("Embed receive connection closed");
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
return recvd;
}
/* The send embedded callback
* return : nb bytes sent, or error
*/
int EmbedSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
{
int sent;
#ifndef WOLFSSL_LINUXKM
int sd = *(int*)ctx;
#else
struct socket *sd = (struct socket*)ctx;
#endif
#ifdef WOLFSSL_MAX_SEND_SZ
if (sz > WOLFSSL_MAX_SEND_SZ)
sz = WOLFSSL_MAX_SEND_SZ;
#endif
sent = wolfIO_Send(sd, buf, sz, ssl->wflags);
if (sent < 0) {
WOLFSSL_MSG("Embed Send error");
}
return sent;
}
#ifdef WOLFSSL_DTLS
#include <wolfssl/wolfcrypt/sha.h>
#if defined(NUCLEUS_PLUS_2_3)
STATIC INT32 nucyassl_recv(INT sd, CHAR *buf, UINT16 sz, INT16 flags)
{
int recvd;
/* Read data from socket */
recvd = NU_Recv(sd, buf, sz, flags);
if (recvd < 0) {
if (recvd == NU_NOT_CONNECTED) {
recvd = 0;
} else {
Nucleus_Net_Errno = recvd;
recvd = WOLFSSL_FATAL_ERROR;
}
} else {
Nucleus_Net_Errno = 0;
}
return (recvd);
}
STATIC int nucyassl_send(INT sd, CHAR *buf, UINT16 sz, INT16 flags)
{
int sent;
/* Write data to socket */
sent = NU_Send(sd, buf, sz, flags);
if (sent < 0) {
Nucleus_Net_Errno = sent;
sent = WOLFSSL_FATAL_ERROR;
} else {
Nucleus_Net_Errno = 0;
}
return sent;
}
#define SELECT_FUNCTION nucyassl_select
int nucyassl_select(INT sd, UINT32 timeout)
{
FD_SET readfs;
STATUS status;
/* Init fs data for socket */
NU_FD_Init(&readfs);
NU_FD_Set(sd, &readfs);
/* Wait for data to arrive */
status = NU_Select((sd + 1), &readfs, NU_NULL, NU_NULL,
(timeout * NU_TICKS_PER_SECOND));
if (status < 0) {
Nucleus_Net_Errno = status;
status = WOLFSSL_FATAL_ERROR;
}
return status;
}
#define sockaddr_storage addr_struct
#define sockaddr addr_struct
STATIC INT32 nucyassl_recvfrom(INT sd, CHAR *buf, UINT16 sz, INT16 flags,
SOCKADDR *peer, XSOCKLENT *peersz)
{
int recvd;
memset(peer, 0, sizeof(struct addr_struct));
recvd = NU_Recv_From(sd, buf, sz, flags, (struct addr_struct *) peer,
(INT16*) peersz);
if (recvd < 0) {
Nucleus_Net_Errno = recvd;
recvd = WOLFSSL_FATAL_ERROR;
} else {
Nucleus_Net_Errno = 0;
}
return recvd;
}
STATIC int nucyassl_sendto(INT sd, CHAR *buf, UINT16 sz, INT16 flags,
const SOCKADDR *peer, INT16 peersz)
{
int sent;
sent = NU_Send_To(sd, buf, sz, flags, (const struct addr_struct *) peer,
peersz);
if (sent < 0) {
Nucleus_Net_Errno = sent;
sent = WOLFSSL_FATAL_ERROR;
} else {
Nucleus_Net_Errno = 0;
}
return sent;
}
#endif /* NUCLEUS_PLUS_2_3 */
#ifndef DTLS_SENDTO_FUNCTION
#define DTLS_SENDTO_FUNCTION sendto
#endif
#ifndef DTLS_RECVFROM_FUNCTION
#define DTLS_RECVFROM_FUNCTION recvfrom
#endif
int sockAddrEqual(
SOCKADDR_S *a, XSOCKLENT aLen, SOCKADDR_S *b, XSOCKLENT bLen)
{
if (aLen != bLen)
return 0;
if (a->ss_family != b->ss_family)
return 0;
if (a->ss_family == WOLFSSL_IP4) {
if (aLen < (XSOCKLENT)sizeof(SOCKADDR_IN))
return 0;
if (((SOCKADDR_IN*)a)->sin_port != ((SOCKADDR_IN*)b)->sin_port)
return 0;
if (((SOCKADDR_IN*)a)->sin_addr.s_addr !=
((SOCKADDR_IN*)b)->sin_addr.s_addr)
return 0;
return 1;
}
#ifdef WOLFSSL_IPV6
if (a->ss_family == WOLFSSL_IP6) {
SOCKADDR_IN6 *a6, *b6;
if (aLen < (XSOCKLENT)sizeof(SOCKADDR_IN6))
return 0;
a6 = (SOCKADDR_IN6*)a;
b6 = (SOCKADDR_IN6*)b;
if (((SOCKADDR_IN6*)a)->sin6_port != ((SOCKADDR_IN6*)b)->sin6_port)
return 0;
if (XMEMCMP((void*)&a6->sin6_addr, (void*)&b6->sin6_addr,
sizeof(a6->sin6_addr)) != 0)
return 0;
return 1;
}
#endif /* WOLFSSL_IPV6 */
return 0;
}
#ifndef WOLFSSL_IPV6
static int PeerIsIpv6(const SOCKADDR_S *peer, XSOCKLENT len)
{
if (len < (XSOCKLENT)sizeof(peer->ss_family))
return 0;
return peer->ss_family == WOLFSSL_IP6;
}
#endif /* !WOLFSSL_IPV6 */
static int isDGramSock(int sfd)
{
int type = 0;
/* optvalue 'type' is of size int */
XSOCKLENT length = (XSOCKLENT)sizeof(type);
if (getsockopt(sfd, SOL_SOCKET, SO_TYPE, (XSOCKOPT_TYPE_OPTVAL_TYPE)&type,
&length) == 0 && type != SOCK_DGRAM) {
return 0;
}
else {
return 1;
}
}
void wolfSSL_SetRecvFrom(WOLFSSL* ssl, WolfSSLRecvFrom recvFrom)
{
if (ssl != NULL)
ssl->buffers.dtlsCtx.recvfrom = recvFrom;
}
void wolfSSL_SetSendTo(WOLFSSL* ssl, WolfSSLSento sendTo)
{
if (ssl != NULL)
ssl->buffers.dtlsCtx.sendto = sendTo;
}
/* The receive embedded callback
* return : nb bytes read, or error
*/
int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
{
WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
int recvd;
int sd = dtlsCtx->rfd;
int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
byte doDtlsTimeout;
SOCKADDR_S lclPeer;
SOCKADDR_S* peer;
XSOCKLENT peerSz = 0;
#ifndef NO_ASN_TIME
word32 start = 0;
#elif !defined(DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER)
word32 invalidPeerPackets = 0;
#endif
int newPeer = 0;
int ret = 0;
WOLFSSL_ENTER("EmbedReceiveFrom");
(void)ret; /* possibly unused */
if (sz < 0)
return WOLFSSL_CBIO_ERR_GENERAL;
XMEMSET(&lclPeer, 0, sizeof(lclPeer));
#ifdef WOLFSSL_RW_THREADED
if (wc_LockRwLock_Rd(&ssl->buffers.dtlsCtx.peerLock) != 0)
return WOLFSSL_CBIO_ERR_GENERAL;
#endif
if (dtlsCtx->connected) {
peer = NULL;
}
else if (dtlsCtx->userSet) {
#ifndef WOLFSSL_IPV6
if (PeerIsIpv6((SOCKADDR_S*)dtlsCtx->peer.sa, dtlsCtx->peer.sz)) {
WOLFSSL_MSG("ipv6 dtls peer set but no ipv6 support compiled");
ret = WOLFSSL_CBIO_ERR_GENERAL;
}
#endif
peer = &lclPeer;
peerSz = sizeof(lclPeer);
}
else {
/* Store the peer address. It is used to calculate the DTLS cookie. */
newPeer = dtlsCtx->peer.sa == NULL || !ssl->options.dtlsStateful;
peer = &lclPeer;
peerSz = sizeof(lclPeer);
}
#ifdef WOLFSSL_RW_THREADED
/* We make a copy above to avoid holding the lock for the entire function */
if (wc_UnLockRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0)
return WOLFSSL_CBIO_ERR_GENERAL;
#endif
if (ret != 0)
return ret;
/* Don't use ssl->options.handShakeDone since it is true even if
* we are in the process of renegotiation */
doDtlsTimeout = ssl->options.handShakeState != HANDSHAKE_DONE;
#ifdef WOLFSSL_DTLS13
if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) {
doDtlsTimeout = doDtlsTimeout || ssl->dtls13Rtx.rtxRecords != NULL;
#ifdef WOLFSSL_RW_THREADED
ret = wc_LockMutex(&ssl->dtls13Rtx.mutex);
if (ret != 0)
return ret;
#endif
doDtlsTimeout = doDtlsTimeout ||
(ssl->dtls13FastTimeout && ssl->dtls13Rtx.seenRecords != NULL);
#ifdef WOLFSSL_RW_THREADED
wc_UnLockMutex(&ssl->dtls13Rtx.mutex);
#endif
}
#endif /* WOLFSSL_DTLS13 */
do {
if (!doDtlsTimeout) {
dtls_timeout = 0;
}
else {
#ifndef NO_ASN_TIME
if (start == 0) {
start = LowResTimer();
}
else {
dtls_timeout -= (int) (LowResTimer() - start);
start = LowResTimer();
if (dtls_timeout < 0 || dtls_timeout > DTLS_TIMEOUT_MAX)
return WOLFSSL_CBIO_ERR_TIMEOUT;
}
#endif
}
if (!wolfSSL_get_using_nonblock(ssl)) {
#ifdef USE_WINDOWS_API
DWORD timeout = dtls_timeout * 1000;
#ifdef WOLFSSL_DTLS13
if (wolfSSL_dtls13_use_quick_timeout(ssl) &&
IsAtLeastTLSv1_3(ssl->version))
timeout /= 4;
#endif /* WOLFSSL_DTLS13 */
#else
struct timeval timeout;
XMEMSET(&timeout, 0, sizeof(timeout));
#ifdef WOLFSSL_DTLS13
if (wolfSSL_dtls13_use_quick_timeout(ssl) &&
IsAtLeastTLSv1_3(ssl->version)) {
if (dtls_timeout >= 4)
timeout.tv_sec = dtls_timeout / 4;
else
timeout.tv_usec = dtls_timeout * 1000000 / 4;
}
else
#endif /* WOLFSSL_DTLS13 */
timeout.tv_sec = dtls_timeout;
#endif /* USE_WINDOWS_API */
if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
sizeof(timeout)) != 0) {
WOLFSSL_MSG("setsockopt rcvtimeo failed");
}
}
#ifndef NO_ASN_TIME
else if (IsSCR(ssl)) {
if (ssl->dtls_start_timeout &&
LowResTimer() - ssl->dtls_start_timeout >
(word32)dtls_timeout) {
ssl->dtls_start_timeout = 0;
return WOLFSSL_CBIO_ERR_TIMEOUT;
}
else if (!ssl->dtls_start_timeout) {
ssl->dtls_start_timeout = LowResTimer();
}
}
#endif /* !NO_ASN_TIME */
{
XSOCKLENT inPeerSz = peerSz;
if (dtlsCtx->recvfrom == NULL) {
recvd = (int)DTLS_RECVFROM_FUNCTION(sd, buf, (size_t)sz,
ssl->rflags, (SOCKADDR*)peer,
peer != NULL ? &inPeerSz : NULL);
}
else {
recvd = (int)dtlsCtx->recvfrom(sd, buf, (size_t) sz,
ssl->rflags, (SOCKADDR*) peer,
peer != NULL ? &inPeerSz : NULL);
}
/* Truncate peerSz. From the RECV(2) man page
* The returned address is truncated if the buffer provided is too
* small; in this case, addrlen will return a value greater than was
* supplied to the call.
*/
peerSz = MIN(peerSz, inPeerSz);
}
recvd = TranslateIoReturnCode(recvd, sd, SOCKET_RECEIVING);
if (recvd < 0) {
WOLFSSL_MSG("Embed Receive From error");
if (recvd == WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_WANT_READ) &&
!wolfSSL_dtls_get_using_nonblock(ssl)) {
recvd = WOLFSSL_CBIO_ERR_TIMEOUT;
}
return recvd;
}
else if (recvd == 0) {
if (!isDGramSock(sd)) {
/* Closed TCP connection */
recvd = WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
else {
WOLFSSL_MSG("Ignoring 0-length datagram");
continue;
}
return recvd;
}
else if (dtlsCtx->connected) {
/* Nothing to do */
}
else if (dtlsCtx->userSet) {
/* Check we received the packet from the correct peer */
int ignore = 0;
#ifdef WOLFSSL_RW_THREADED
if (wc_LockRwLock_Rd(&ssl->buffers.dtlsCtx.peerLock) != 0)
return WOLFSSL_CBIO_ERR_GENERAL;
#endif
if (dtlsCtx->peer.sz > 0 &&
(peerSz != (XSOCKLENT)dtlsCtx->peer.sz ||
!sockAddrEqual(peer, peerSz, (SOCKADDR_S*)dtlsCtx->peer.sa,
dtlsCtx->peer.sz))) {
WOLFSSL_MSG(" Ignored packet from invalid peer");
ignore = 1;
}
#ifdef WOLFSSL_RW_THREADED
if (wc_UnLockRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0)
return WOLFSSL_CBIO_ERR_GENERAL;
#endif
if (ignore) {
#if defined(NO_ASN_TIME) && \
!defined(DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER)
if (doDtlsTimeout) {
invalidPeerPackets++;
if (invalidPeerPackets > DTLS_RECEIVEFROM_MAX_INVALID_PEER)
return wolfSSL_dtls_get_using_nonblock(ssl)
? WOLFSSL_CBIO_ERR_WANT_READ
: WOLFSSL_CBIO_ERR_TIMEOUT;
}
#endif /* NO_ASN_TIME && !DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER */
continue;
}
}
else {
if (newPeer) {
/* Store size of saved address. Locking handled internally. */
if (wolfSSL_dtls_set_peer(ssl, peer, peerSz) != WOLFSSL_SUCCESS)
return WOLFSSL_CBIO_ERR_GENERAL;
dtlsCtx->userSet = 0;
}
#ifndef WOLFSSL_PEER_ADDRESS_CHANGES
else {
ret = 0;
#ifdef WOLFSSL_RW_THREADED
if (wc_LockRwLock_Rd(&ssl->buffers.dtlsCtx.peerLock) != 0)
return WOLFSSL_CBIO_ERR_GENERAL;
#endif /* WOLFSSL_RW_THREADED */
if (!sockAddrEqual(peer, peerSz, (SOCKADDR_S*)dtlsCtx->peer.sa,
dtlsCtx->peer.sz)) {
ret = WOLFSSL_CBIO_ERR_GENERAL;
}
#ifdef WOLFSSL_RW_THREADED
if (wc_UnLockRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0)
return WOLFSSL_CBIO_ERR_GENERAL;
#endif /* WOLFSSL_RW_THREADED */
if (ret != 0)
return ret;
}
#endif /* !WOLFSSL_PEER_ADDRESS_CHANGES */
}
#ifndef NO_ASN_TIME
ssl->dtls_start_timeout = 0;
#endif /* !NO_ASN_TIME */
break;
} while (1);
return recvd;
}
/* The send embedded callback
* return : nb bytes sent, or error
*/
int EmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
{
WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
int sd = dtlsCtx->wfd;
int sent;
const SOCKADDR_S* peer = NULL;
XSOCKLENT peerSz = 0;
WOLFSSL_ENTER("EmbedSendTo");
if (sz < 0)
return WOLFSSL_CBIO_ERR_GENERAL;
if (!isDGramSock(sd)) {
/* Probably a TCP socket. peer and peerSz MUST be NULL and 0 */
}
else if (!dtlsCtx->connected) {
peer = (const SOCKADDR_S*)dtlsCtx->peer.sa;
peerSz = dtlsCtx->peer.sz;
#ifndef WOLFSSL_IPV6
if (PeerIsIpv6(peer, peerSz)) {
WOLFSSL_MSG("ipv6 dtls peer set but no ipv6 support compiled");
return NOT_COMPILED_IN;
}
#endif
}
if (dtlsCtx->sendto == NULL) {
sent = (int)DTLS_SENDTO_FUNCTION(sd, buf, (size_t)sz, ssl->wflags,
(const SOCKADDR*)peer, peerSz);
}
else {
sent = (int)dtlsCtx->sendto(sd, buf, (size_t)sz, ssl->wflags,
(const SOCKADDR*)peer, peerSz);
}
sent = TranslateIoReturnCode(sent, sd, SOCKET_SENDING);
if (sent < 0) {
WOLFSSL_MSG("Embed Send To error");
}
return sent;
}
#ifdef WOLFSSL_MULTICAST
/* The alternate receive embedded callback for Multicast
* return : nb bytes read, or error
*/
int EmbedReceiveFromMcast(WOLFSSL *ssl, char *buf, int sz, void *ctx)
{
WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
int recvd;
int sd = dtlsCtx->rfd;
WOLFSSL_ENTER("EmbedReceiveFromMcast");
if (sz < 0)
return WOLFSSL_CBIO_ERR_GENERAL;
recvd = (int)DTLS_RECVFROM_FUNCTION(sd, buf, (size_t)sz, ssl->rflags, NULL, NULL);
recvd = TranslateIoReturnCode(recvd, sd, SOCKET_RECEIVING);
if (recvd < 0) {
WOLFSSL_MSG("Embed Receive From error");
if (recvd == WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_WANT_READ) &&
!wolfSSL_dtls_get_using_nonblock(ssl)) {
recvd = WOLFSSL_CBIO_ERR_TIMEOUT;
}
}
return recvd;
}
#endif /* WOLFSSL_MULTICAST */
/* The DTLS Generate Cookie callback
* return : number of bytes copied into buf, or error
*/
int EmbedGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx)
{
int sd = ssl->wfd;
SOCKADDR_S peer;
XSOCKLENT peerSz = sizeof(peer);
byte digest[WC_SHA256_DIGEST_SIZE];