-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathssl_api_rw.c
More file actions
1383 lines (1234 loc) · 46.2 KB
/
Copy pathssl_api_rw.c
File metadata and controls
1383 lines (1234 loc) · 46.2 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
/* ssl_api_rw.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
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#if !defined(WOLFSSL_SSL_API_RW_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning ssl_api_rw.c does not need to be compiled separately from ssl.c
#endif
#else
#ifndef WOLFCRYPT_ONLY
#ifndef NO_TLS
#if defined(HAVE_WRITE_DUP) && defined(WOLFSSL_TLS13)
/* Take over the TLS 1.3 work delegated by the read side.
*
* The read side of a write duplicate cannot send, so it records what needs to
* be sent in the write duplicate object. Move that state across to the SSL
* object of the write side.
*
* Must be called with ssl->dupWrite->dupMutex held.
*
* @param [in, out] ssl SSL/TLS object of the write side.
* @return 0 on success.
* @return Negative on error.
*/
static int wolfssl_write_dup_take_tls13_work(WOLFSSL* ssl)
{
int ret = 0;
if (IsAtLeastTLSv1_3(ssl->version)) {
/* TLS 1.3: if the read side received a KeyUpdate(update_requested)
* it cannot respond; send the response from here. */
ssl->keys.keyUpdateRespond |= ssl->dupWrite->keyUpdateRespond;
ssl->dupWrite->keyUpdateRespond = 0;
#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
ssl->postHandshakeAuthPending |=
ssl->dupWrite->postHandshakeAuthPending;
ssl->dupWrite->postHandshakeAuthPending = 0;
if (ssl->postHandshakeAuthPending) {
/* Take ownership of the delegated auth state. */
CertReqCtx** tail = &ssl->dupWrite->postHandshakeCertReqCtx;
while (*tail != NULL) {
tail = &(*tail)->next;
}
*tail = ssl->certReqCtx;
ssl->certReqCtx = ssl->dupWrite->postHandshakeCertReqCtx;
ssl->dupWrite->postHandshakeCertReqCtx = NULL;
FreeHandshakeHashes(ssl);
ssl->hsHashes = ssl->dupWrite->postHandshakeHashState;
ssl->dupWrite->postHandshakeHashState = NULL;
ssl->options.sendVerify = ssl->dupWrite->postHandshakeSendVerify;
ssl->options.sigAlgo = ssl->dupWrite->postHandshakeSigAlgo;
ssl->options.hashAlgo = ssl->dupWrite->postHandshakeHashAlgo;
}
#endif /* WOLFSSL_POST_HANDSHAKE_AUTH */
#ifdef WOLFSSL_DTLS13
if (ssl->options.dtls) {
/* Schedule key update to be sent. */
if (ssl->keys.keyUpdateRespond) {
ssl->dtls13DoKeyUpdate = 1;
}
/* Copy over ACKs */
ssl->dtls13Rtx.sendAcks |= ssl->dupWrite->sendAcks;
if (ssl->dupWrite->sendAcks) {
/* Insert each record number so the
* ACK message is properly ordered. */
struct Dtls13RecordNumber* rn;
for (rn = ssl->dupWrite->sendAckList; rn != NULL;
rn = rn->next) {
ret = Dtls13RtxAddAck(ssl, rn->epoch, rn->seq);
if (ret != 0) {
break;
}
}
/* Clear only on success so no ACKs get dropped */
if (ret == 0) {
rn = ssl->dupWrite->sendAckList;
ssl->dupWrite->sendAckList = NULL;
ssl->dupWrite->sendAcks = 0;
while (rn != NULL) {
struct Dtls13RecordNumber* next = rn->next;
XFREE(rn, ssl->heap, DYNAMIC_TYPE_DTLS_MSG);
rn = next;
}
}
}
/* Remove KeyUpdate record from RTX list. */
if (ssl->dupWrite->keyUpdateAcked) {
Dtls13RtxRemoveRecord(ssl, ssl->dupWrite->keyUpdateEpoch,
ssl->dupWrite->keyUpdateSeq);
}
/* Store if KeyUpdate was ACKed. */
ssl->dtls13KeyUpdateAcked |= ssl->dupWrite->keyUpdateAcked;
ssl->dupWrite->keyUpdateAcked = 0;
}
#endif /* WOLFSSL_DTLS13 */
}
return ret;
}
/* Perform the TLS 1.3 work delegated by the read side.
*
* Must be called after ssl->dupWrite->dupMutex has been released, as the work
* performed here sends records.
*
* @param [in, out] ssl SSL/TLS object of the write side.
* @return 0 on success.
* @return BAD_MUTEX_E when the write duplicate could not be locked. Returned
* as-is by the caller, so ssl->error is not set for it.
* @return WOLFSSL_FATAL_ERROR on error. Call wolfSSL_get_error() for the
* reason.
*/
static int wolfssl_write_dup_do_tls13_work(WOLFSSL* ssl)
{
int ret = 0;
if (IsAtLeastTLSv1_3(ssl->version)) {
#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
/* Read side received a CertificateRequest but couldn't write;
* send Certificate+CertificateVerify+Finished from the write
* side. */
if (ssl->postHandshakeAuthPending) {
/* reset handshake states */
ssl->postHandshakeAuthPending = 0;
ssl->options.clientState = CLIENT_HELLO_COMPLETE;
ssl->options.connectState = FIRST_REPLY_DONE;
ssl->options.handShakeState = CLIENT_HELLO_COMPLETE;
ssl->options.processReply = 0; /* doProcessInit */
if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) {
if ((ssl->error != WC_NO_ERR_TRACE(WANT_WRITE)) &&
(ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E))) {
WOLFSSL_MSG("Post-handshake auth send failed");
ssl->error = POST_HAND_AUTH_ERROR;
}
ret = WOLFSSL_FATAL_ERROR;
}
/* PHA response fully sent: publish the write side's updated
* transcript to the read side for the next PHA round. */
else if ((ssl->hsHashes != NULL) && (ssl->dupWrite != NULL)) {
if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) {
ret = BAD_MUTEX_E;
}
else {
int syncRet = InitHandshakeHashesAndCopy(ssl,
ssl->hsHashes,
&ssl->dupWrite->postHandshakeSyncedHashState);
if (syncRet != 0) {
/* On failure the copy may have left a partially
* initialized transcript. The read side only checks
* for non-NULL before consuming it, so drop it here to
* avoid hashing onto a corrupt transcript, and surface
* the error to the caller. */
Free_HS_Hashes(
ssl->dupWrite->postHandshakeSyncedHashState,
ssl->heap);
ssl->dupWrite->postHandshakeSyncedHashState = NULL;
}
wc_UnLockMutex(&ssl->dupWrite->dupMutex);
if (syncRet != 0) {
ssl->error = syncRet;
ret = WOLFSSL_FATAL_ERROR;
}
}
}
}
#endif /* WOLFSSL_POST_HANDSHAKE_AUTH */
if (ret == 0) {
#ifdef WOLFSSL_DTLS13
if (ssl->options.dtls) {
if (ssl->dtls13KeyUpdateAcked) {
ret = DoDtls13KeyUpdateAck(ssl);
}
ssl->dtls13KeyUpdateAcked = 0;
if (ret == 0) {
ret = Dtls13DoScheduledWork(ssl);
}
}
else
#endif /* WOLFSSL_DTLS13 */
{
/* keyUpdateRespond is cleared in SendTls13KeyUpdate. */
if (ssl->keys.keyUpdateRespond) {
ret = Tls13UpdateKeys(ssl);
}
}
if (ret != 0) {
ssl->error = ret;
ret = WOLFSSL_FATAL_ERROR;
}
}
}
return ret;
}
#endif /* HAVE_WRITE_DUP && WOLFSSL_TLS13 */
#ifdef HAVE_WRITE_DUP
/* Settle the write duplicate state before application data is sent.
*
* Takes over the work the read side delegated and surfaces any error it
* recorded. Both are held under ssl->dupWrite->dupMutex, so they are collected
* with the lock held and acted on once it has been released.
*
* @param [in, out] ssl SSL/TLS object of the write side.
* @return 0 when the write may proceed.
* @return BAD_MUTEX_E when the write duplicate could not be locked.
* @return WOLFSSL_FATAL_ERROR on error. Call wolfSSL_get_error() for the
* reason.
*/
static int wolfssl_write_dup_prepare(WOLFSSL* ssl)
{
int ret = 0;
/* Lock ssl->dupWrite to gather what needs to be done. */
if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) {
ret = BAD_MUTEX_E;
}
else {
int dupErr = ssl->dupWrite->dupErr; /* local copy */
#ifdef WOLFSSL_TLS13
ret = wolfssl_write_dup_take_tls13_work(ssl);
#endif /* WOLFSSL_TLS13 */
wc_UnLockMutex(&ssl->dupWrite->dupMutex);
/* An error from the read side takes precedence over one hit while
* taking over its work. */
if (dupErr != 0) {
WOLFSSL_MSG("Write dup error from other side");
ret = dupErr;
}
if (ret != 0) {
ssl->error = ret;
ret = WOLFSSL_FATAL_ERROR;
}
#ifdef WOLFSSL_TLS13
else {
/* Do the work delegated by the read side. */
ret = wolfssl_write_dup_do_tls13_work(ssl);
}
#endif /* WOLFSSL_TLS13 */
}
return ret;
}
#endif /* HAVE_WRITE_DUP */
/* Write application data to the peer.
*
* Performs the handshake when it has not completed. When a write duplicate is
* in use, work delegated by the read side, such as sending a key update, is
* done here first.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in] data Application data to write.
* @param [in] sz Length of data in bytes.
* @return Number of bytes written on success.
* @return BAD_FUNC_ARG when ssl or data is NULL.
* @return WRITE_DUP_WRITE_E when called on the read side of a write
* duplicate.
* @return BAD_MUTEX_E when the write duplicate could not be locked. Neither
* of those two sets ssl->error.
* @return WOLFSSL_FATAL_ERROR when the handshake or write fails. Call
* wolfSSL_get_error() for the reason.
*/
static int wolfSSL_write_internal(WOLFSSL* ssl, const void* data, size_t sz)
{
int ret = 0;
WOLFSSL_ENTER("wolfSSL_write_internal");
/* Validate parameters. Nothing on the way to the send reports zero, so ret
* doubles as the "keep going" flag. */
if ((ssl == NULL) || (data == NULL)) {
ret = BAD_FUNC_ARG;
}
#ifdef WOLFSSL_QUIC
if ((ret == 0) && (WOLFSSL_IS_QUIC(ssl))) {
WOLFSSL_MSG("SSL_write() on QUIC not allowed");
ret = BAD_FUNC_ARG;
}
#endif
#ifdef HAVE_WRITE_DUP
if ((ret == 0) && (ssl->dupSide == READ_DUP_SIDE)) {
WOLFSSL_MSG("Read dup side cannot write");
ret = WRITE_DUP_WRITE_E;
}
/* Only enter special dupWrite logic when error is cleared. This will help
* with handling async data and other edge case errors. */
if ((ret == 0) && (ssl->dupWrite != NULL) && (ssl->error == 0)) {
ret = wolfssl_write_dup_prepare(ssl);
}
#endif
if (ret == 0) {
#ifdef HAVE_ERRNO_H
errno = 0;
#endif
#ifdef OPENSSL_EXTRA
if (ssl->CBIS != NULL) {
ssl->CBIS(ssl, WOLFSSL_CB_WRITE, WOLFSSL_SUCCESS);
ssl->cbmode = WOLFSSL_CB_WRITE;
}
#endif
ret = SendData(ssl, data, sz);
WOLFSSL_LEAVE("wolfSSL_write_internal", ret);
if (ret < 0) {
ret = WOLFSSL_FATAL_ERROR;
}
}
return ret;
}
/* Write application data to the peer.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in] data Application data to write.
* @param [in] sz Length of data in bytes.
* @return Number of bytes written on success.
* @return BAD_FUNC_ARG when ssl or data is NULL, or sz is negative.
* @return WOLFSSL_FATAL_ERROR when the write fails. Call
* wolfSSL_get_error() for the reason.
*/
WOLFSSL_ABI
int wolfSSL_write(WOLFSSL* ssl, const void* data, int sz)
{
int ret;
WOLFSSL_ENTER("wolfSSL_write");
/* Validate parameter. */
if (sz < 0) {
ret = BAD_FUNC_ARG;
}
else {
ret = wolfSSL_write_internal(ssl, data, (size_t)sz);
}
return ret;
}
/* Inject data into the input buffer as if it was received from the peer.
*
* Used when the application reads the transport itself.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in] data Data to inject.
* @param [in] sz Length of data in bytes.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl or data is NULL, or sz is not positive.
* @return BUFFER_ERROR when the input buffer lengths are inconsistent.
* @return APP_DATA_READY when the input buffer must be grown while there is
* application data left to read.
* @return Negative error code from growing the input buffer, such as
* MEMORY_E.
*/
int wolfSSL_inject(WOLFSSL* ssl, const void* data, int sz)
{
int ret = WOLFSSL_SUCCESS;
int usedLength = 0;
int maxLength = 0;
bufferStatic* in = NULL;
WOLFSSL_ENTER("wolfSSL_inject");
/* Validate parameters. */
if ((ssl == NULL) || (data == NULL) || (sz <= 0)) {
ret = BAD_FUNC_ARG;
}
if (ret == WOLFSSL_SUCCESS) {
in = &ssl->buffers.inputBuffer;
/* Order the unsigned fields first. Subtracting a larger value wraps
* and can land back in the positive int range. */
if ((in->idx > in->length) || (in->length > in->bufferSize)) {
ret = BUFFER_ERROR;
}
else {
usedLength = (int)(in->length - in->idx);
/* Free space past all buffered data, where new data is appended. */
maxLength = (int)(in->bufferSize - in->length);
if ((usedLength < 0) || (maxLength < 0)) {
ret = BUFFER_ERROR;
}
}
}
if ((ret == WOLFSSL_SUCCESS) && (sz > maxLength)) {
/* Need to make space */
if (ssl->buffers.clearOutputBuffer.length > 0) {
/* clearOutputBuffer points into so reallocating inputBuffer
* will invalidate clearOutputBuffer and lose app data */
WOLFSSL_MSG(
"Can't inject while there is application data to read");
ret = APP_DATA_READY;
}
else {
/* Compacts the unconsumed data, leaving idx 0 and length
* usedLength. */
int growRet = GrowInputBuffer(ssl, sz, usedLength);
if (growRet < 0) {
ret = growRet;
}
}
}
if (ret == WOLFSSL_SUCCESS) {
XMEMCPY(in->buffer + in->length, data, (size_t)sz);
in->length += (word32)sz;
}
return ret;
}
/* Write application data to the peer and return the number of bytes written.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in] data Application data to write.
* @param [in] sz Length of data in bytes.
* @param [out] wr Number of bytes written. May be NULL.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return WOLFSSL_FAILURE when the write fails. Call wolfSSL_get_error() for
* the reason.
*/
int wolfSSL_write_ex(WOLFSSL* ssl, const void* data, size_t sz, size_t* wr)
{
int ret;
if (wr != NULL) {
*wr = 0;
}
/* Validate parameter, matching wolfSSL_read_ex() and the rest of the
* file. Reported as an error code rather than the 0 used for "nothing
* was written", which a caller cannot tell from a short write. */
if (ssl == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wolfSSL_write_internal(ssl, data, sz);
if (ret >= 0) {
if (wr != NULL) {
*wr = (size_t)ret;
}
/* handle partial write cases, if not set then a partial write is
* considered a failure case, or if set and ret is 0 then is a
* fail */
if ((ret == 0) && (ssl->options.partialWrite)) {
ret = 0;
}
else if (((size_t)ret < sz) && (!ssl->options.partialWrite)) {
ret = 0;
}
else {
/* wrote out all application data, or wrote out 1 byte or more
* with partial write flag set */
ret = 1;
}
}
else {
ret = 0;
}
}
return ret;
}
/* Read application data from the peer.
*
* Performs the handshake when it has not completed.
*
* @param [in, out] ssl SSL/TLS object.
* @param [out] data Buffer to hold application data.
* @param [in] sz Length of buffer in bytes.
* @param [in] peek When 1, data is not removed from the input buffer.
* @return Number of bytes read on success.
* @return 0 when the peer has closed the connection.
* @return BAD_FUNC_ARG when ssl or data is NULL.
* @return WRITE_DUP_READ_E when called on the write side of a write
* duplicate. ssl->error is not set for it.
* @return WOLFSSL_FATAL_ERROR when the handshake or read fails. Call
* wolfSSL_get_error() for the reason.
*/
static int wolfSSL_read_internal(WOLFSSL* ssl, void* data, size_t sz, int peek)
{
int ret = 0;
/* A separate flag is needed rather than gating on ret: the OpenSSL
* shutdown simulation below reports WOLFSSL_FAILURE, which is zero. */
int done = 0;
WOLFSSL_ENTER("wolfSSL_read_internal");
/* Validate parameters. */
if ((ssl == NULL) || (data == NULL)) {
ret = BAD_FUNC_ARG;
done = 1;
}
#ifdef WOLFSSL_QUIC
if ((!done) && (WOLFSSL_IS_QUIC(ssl))) {
WOLFSSL_MSG("SSL_read() on QUIC not allowed");
ret = BAD_FUNC_ARG;
done = 1;
}
#endif
#if defined(WOLFSSL_ERROR_CODE_OPENSSL) && defined(OPENSSL_EXTRA)
/* This additional logic is meant to simulate following openSSL behavior:
* After bidirectional SSL_shutdown complete, SSL_read returns 0 and
* SSL_get_error_code returns SSL_ERROR_ZERO_RETURN.
* This behavior is used to know the disconnect of the underlying
* transport layer.
*
* In this logic, CBIORecv is called with a read size of 0 to check the
* transport layer status. It also returns WOLFSSL_FAILURE so that
* SSL_read does not return a positive number on failure.
*/
/* make sure bidirectional TLS shutdown completes */
if ((!done) && ((ssl->error == WOLFSSL_ERROR_SYSCALL) ||
(ssl->options.shutdownDone))) {
/* ask the underlying transport the connection is closed */
if (ssl->CBIORecv(ssl, (char*)data, 0, ssl->IOCB_ReadCtx)
== WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_CONN_CLOSE))
{
ssl->options.isClosed = 1;
ssl->error = WOLFSSL_ERROR_ZERO_RETURN;
}
ret = WOLFSSL_FAILURE;
done = 1;
}
#endif
#ifdef HAVE_WRITE_DUP
if ((!done) && (ssl->dupWrite != NULL) &&
(ssl->dupSide == WRITE_DUP_SIDE)) {
WOLFSSL_MSG("Write dup side cannot read");
ret = WRITE_DUP_READ_E;
done = 1;
}
#endif
if (!done) {
#ifdef HAVE_ERRNO_H
errno = 0;
#endif
ret = ReceiveData(ssl, (byte*)data, sz, peek);
#ifdef HAVE_WRITE_DUP
if (ssl->dupWrite != NULL) {
if ((ssl->error != 0) &&
(ssl->error != WC_NO_ERR_TRACE(WANT_READ))
#ifdef WOLFSSL_ASYNC_CRYPT
&& (ssl->error != WC_NO_ERR_TRACE(WC_PENDING_E))
#endif
) {
int notifyErr;
WOLFSSL_MSG("Notifying write side of fatal read error");
notifyErr = NotifyWriteSide(ssl, ssl->error);
if (notifyErr < 0) {
ret = ssl->error = notifyErr;
}
}
}
#endif
WOLFSSL_LEAVE("wolfSSL_read_internal", ret);
if (ret < 0) {
ret = WOLFSSL_FATAL_ERROR;
}
}
return ret;
}
/* Read application data from the peer without removing it.
*
* The same data is returned by the next call to wolfSSL_read().
*
* @param [in, out] ssl SSL/TLS object.
* @param [out] data Buffer to hold application data.
* @param [in] sz Length of buffer in bytes.
* @return Number of bytes read on success.
* @return BAD_FUNC_ARG when ssl or data is NULL, or sz is negative.
* @return WOLFSSL_FATAL_ERROR when the read fails.
*/
int wolfSSL_peek(WOLFSSL* ssl, void* data, int sz)
{
int ret;
WOLFSSL_ENTER("wolfSSL_peek");
/* Validate parameter. */
if (sz < 0) {
ret = BAD_FUNC_ARG;
}
else {
ret = wolfSSL_read_internal(ssl, data, (size_t)sz, TRUE);
}
return ret;
}
/* Read application data from the peer.
*
* @param [in, out] ssl SSL/TLS object.
* @param [out] data Buffer to hold application data.
* @param [in] sz Length of buffer in bytes.
* @return Number of bytes read on success.
* @return 0 when the peer has closed the connection.
* @return BAD_FUNC_ARG when ssl or data is NULL, or sz is negative.
* @return WOLFSSL_FATAL_ERROR when the read fails. Call wolfSSL_get_error()
* for the reason.
*/
WOLFSSL_ABI
int wolfSSL_read(WOLFSSL* ssl, void* data, int sz)
{
int ret;
WOLFSSL_ENTER("wolfSSL_read");
/* Validate parameters. */
if (sz < 0) {
ret = BAD_FUNC_ARG;
}
#ifdef OPENSSL_EXTRA
else if (ssl == NULL) {
ret = BAD_FUNC_ARG;
}
#endif
else {
#ifdef OPENSSL_EXTRA
if (ssl->CBIS != NULL) {
ssl->CBIS(ssl, WOLFSSL_CB_READ, WOLFSSL_SUCCESS);
ssl->cbmode = WOLFSSL_CB_READ;
}
#endif
ret = wolfSSL_read_internal(ssl, data, (size_t)sz, FALSE);
}
return ret;
}
/* Read application data from the peer and report whether any was read.
*
* @param [in, out] ssl SSL/TLS object.
* @param [out] data Buffer to hold application data.
* @param [in] sz Length of buffer in bytes.
* @param [out] rd Number of bytes read. May be NULL. Only set when
* data was read.
* @return 1 when application data was read.
* @return 0 when no application data was read. Call wolfSSL_get_error() for
* the reason.
* @return BAD_FUNC_ARG when ssl is NULL.
*/
int wolfSSL_read_ex(WOLFSSL* ssl, void* data, size_t sz, size_t* rd)
{
int ret;
/* Validate parameter. Checked unconditionally so the guarded branch does
* not leave a standalone block. */
if (ssl == NULL) {
ret = BAD_FUNC_ARG;
}
else {
#ifdef OPENSSL_EXTRA
if (ssl->CBIS != NULL) {
ssl->CBIS(ssl, WOLFSSL_CB_READ, WOLFSSL_SUCCESS);
ssl->cbmode = WOLFSSL_CB_READ;
}
#endif
ret = wolfSSL_read_internal(ssl, data, sz, FALSE);
if ((ret > 0) && (rd != NULL)) {
*rd = (size_t)ret;
}
ret = (ret > 0) ? 1 : 0;
}
return ret;
}
#ifndef WOLFSSL_LEANPSK
/* Write application data to the peer with socket flags.
*
* Flags are set on the socket for the write and restored afterwards.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in] data Application data to write.
* @param [in] sz Length of data in bytes.
* @param [in] flags Flags to pass to the send call.
* @return Number of bytes written on success.
* @return BAD_FUNC_ARG when ssl or data is NULL, or sz is negative.
* @return WOLFSSL_FATAL_ERROR when the write fails.
*/
int wolfSSL_send(WOLFSSL* ssl, const void* data, int sz, int flags)
{
int ret;
WOLFSSL_ENTER("wolfSSL_send");
/* Validate parameters. */
if ((ssl == NULL) || (data == NULL) || (sz < 0)) {
ret = BAD_FUNC_ARG;
}
else {
int oldFlags = ssl->wflags;
ssl->wflags = flags;
ret = wolfSSL_write(ssl, data, sz);
ssl->wflags = oldFlags;
WOLFSSL_LEAVE("wolfSSL_send", ret);
}
return ret;
}
/* Read application data from the peer with socket flags.
*
* Flags are set on the socket for the read and restored afterwards.
*
* @param [in, out] ssl SSL/TLS object.
* @param [out] data Buffer to hold application data.
* @param [in] sz Length of buffer in bytes.
* @param [in] flags Flags to pass to the recv call.
* @return Number of bytes read on success.
* @return BAD_FUNC_ARG when ssl or data is NULL, or sz is negative.
* @return WOLFSSL_FATAL_ERROR when the read fails.
*/
int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags)
{
int ret;
WOLFSSL_ENTER("wolfSSL_recv");
/* Validate parameters. */
if ((ssl == NULL) || (data == NULL) || (sz < 0)) {
ret = BAD_FUNC_ARG;
}
else {
int oldFlags = ssl->rflags;
ssl->rflags = flags;
ret = wolfSSL_read(ssl, data, sz);
ssl->rflags = oldFlags;
WOLFSSL_LEAVE("wolfSSL_recv", ret);
}
return ret;
}
#endif
/* Send a user_canceled alert to the peer and shut down the connection.
*
* @param [in, out] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_SHUTDOWN_NOT_DONE when the shutdown is not complete.
* @return WOLFSSL_FAILURE when ssl is NULL or sending the alert fails.
*/
int wolfSSL_SendUserCanceled(WOLFSSL* ssl)
{
int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
WOLFSSL_ENTER("wolfSSL_SendUserCanceled");
if (ssl != NULL) {
ssl->error = SendAlert(ssl, alert_warning, user_canceled);
if ((ssl->error == 0) ||
(ssl->error == WC_NO_ERR_TRACE(WANT_WRITE))) {
ssl->options.sentUserCanceled = 1;
}
if (ssl->error < 0) {
WOLFSSL_ERROR(ssl->error);
}
else {
ret = wolfSSL_shutdown(ssl);
}
}
WOLFSSL_LEAVE("wolfSSL_SendUserCanceled", ret);
return ret;
}
/* Flush an alert still sitting in the output buffer.
*
* A previous call may have left the close_notify alert buffered when the
* transport reported WANT_WRITE. Get it out before doing anything else.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in, out] ret Result for wolfSSL_shutdown() to return. Set only on
* the paths that reach a decision; on the others it is
* left as the caller initialized it. Returning 0 while
* assigning WOLFSSL_SHUTDOWN_NOT_DONE would break the
* caller: it treats an undecided result as "no
* close_notify can ever be sent" and converts it to a
* failure. Every path here that reports "call again"
* therefore returns 1.
* @return 1 when no later step of the shutdown may run.
* @return 0 when the caller carries on with the rest of the shutdown. The
* result may already have been decided: the exchange can complete
* here, and the steps that follow then leave it alone.
*/
static int wolfssl_shutdown_flush_alert(WOLFSSL* ssl, int* ret)
{
int done = 0;
if ((ssl->error == WC_NO_ERR_TRACE(WANT_WRITE)) &&
(ssl->buffers.outputBuffer.length > 0)) {
int rc = SendBuffered(ssl);
if (rc != 0) {
ssl->error = rc;
/* for error tracing */
if (rc != WC_NO_ERR_TRACE(WANT_WRITE)) {
WOLFSSL_ERROR(rc);
}
/* The reason has been traced above - don't trace the return
* value as well. */
*ret = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
done = 1;
}
else {
ssl->error = WOLFSSL_ERROR_NONE;
/* we succeeded in sending the alert now */
if (ssl->options.sentNotify) {
/* just after we send the alert, if we didn't receive the
* alert from the other peer yet, return
* WOLFSSL_SHUTDOWN_NOT_DONE */
if (!ssl->options.closeNotify) {
*ret = WOLFSSL_SHUTDOWN_NOT_DONE;
done = 1;
}
else {
ssl->options.shutdownDone = 1;
*ret = WOLFSSL_SUCCESS;
}
}
}
}
return done;
}
/* Send the close_notify alert to the peer.
*
* Not being able to send it right away is not an error - the alert is left in
* the output buffer and goes out eventually.
*
* @param [in, out] ssl SSL/TLS object.
* @param [in, out] ret Result for wolfSSL_shutdown() to return. Set only on
* the paths that reach a decision; on the others it is
* left as the caller initialized it. As in
* wolfssl_shutdown_flush_alert(), a path that reports
* "call again" must return 1 - the caller reads an
* undecided result as "no close_notify can ever be
* sent" and turns it into a failure.
* @return 1 when no later step of the shutdown may run.
* @return 0 when the caller carries on with the rest of the shutdown. The
* result may already have been decided: the exchange can complete
* here, and the steps that follow then leave it alone.
*/
static int wolfssl_shutdown_send_close_notify(WOLFSSL* ssl, int* ret)
{
int done = 0;
/* try to send close notify, not an error if can't */
if ((!ssl->options.isClosed) && (!ssl->options.connReset) &&
(!ssl->options.sentNotify)) {
ssl->error = SendAlert(ssl, alert_warning, close_notify);
/* the alert is now sent or sitting in the buffer,
* where will be sent eventually */
if ((ssl->error == 0) ||
(ssl->error == WC_NO_ERR_TRACE(WANT_WRITE))) {
ssl->options.sentNotify = 1;
}
if (ssl->error < 0) {
WOLFSSL_ERROR(ssl->error);
/* The reason has been traced above - don't trace the return
* value as well. */
*ret = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
done = 1;
}
else if (ssl->options.closeNotify) {
*ret = WOLFSSL_SUCCESS;
ssl->options.shutdownDone = 1;
}
else {
*ret = WOLFSSL_SHUTDOWN_NOT_DONE;
done = 1;
}
}
return done;
}
/* Wait for the peer's close_notify alert to complete a bidirectional shutdown.
*
* Called when this side has sent its close_notify but has not seen the
* peer's, i.e. wolfSSL_shutdown() called again.
*
* @param [in, out] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS when the shutdown is complete.
* @return WOLFSSL_SHUTDOWN_NOT_DONE when the peer's alert has not arrived.
* @return WOLFSSL_FATAL_ERROR on error. Call wolfSSL_get_error() for the
* reason.
*/
static int wolfssl_shutdown_recv_close_notify(WOLFSSL* ssl)
{
int ret;
/* If there is still buffered application data waiting to be read, do not
* process incoming records here. clearOutputBuffer.buffer points into
* inputBuffer, and ProcessReply() may call GrowInputBuffer(), which frees
* and reallocates inputBuffer. Require the pending data to be drained
* first. */
if (ssl->buffers.clearOutputBuffer.length > 0) {
WOLFSSL_MSG("Pending application data, read it before shutdown");
ret = WOLFSSL_SHUTDOWN_NOT_DONE;
}
else {
ret = ProcessReply(ssl);
if ((ret == WC_NO_ERR_TRACE(ZERO_RETURN)) ||
(ret == WC_NO_ERR_TRACE(SOCKET_ERROR_E))) {
/* simulate OpenSSL behavior */
ssl->options.shutdownDone = 1;
/* Clear error */
ssl->error = WOLFSSL_ERROR_NONE;
ret = WOLFSSL_SUCCESS;
}
else if (ret == WC_NO_ERR_TRACE(MEMORY_E)) {
ret = WOLFSSL_FATAL_ERROR;
}
else if (ret == WC_NO_ERR_TRACE(WANT_READ)) {
ssl->error = ret;
ret = WOLFSSL_FATAL_ERROR;
}
else if (ssl->error == WOLFSSL_ERROR_NONE) {
ret = WOLFSSL_SHUTDOWN_NOT_DONE;
}
else {
WOLFSSL_ERROR(ssl->error);
ret = WOLFSSL_FATAL_ERROR;
}
}
return ret;
}
/* Shut the connection down by exchanging close_notify alerts with the peer.
*
* Call repeatedly while WOLFSSL_SHUTDOWN_NOT_DONE is returned to complete a
* bidirectional shutdown.
*
* @param [in, out] ssl SSL/TLS object.