-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssl.rs
1727 lines (1531 loc) · 69.9 KB
/
ssl.rs
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
use crate::{
callback::{IOCallbackResult, IOCallbacks},
context::Context,
error::{Error, Poll, PollResult, Result},
CurveGroup, Protocol, ProtocolVersion, TLS_MAX_RECORD_SIZE,
};
use bytes::{Buf, Bytes, BytesMut};
use parking_lot::Mutex;
use thiserror::Error;
use std::{
ffi::{c_int, c_uchar, c_ushort, c_void},
ptr::NonNull,
time::Duration,
};
#[cfg(feature = "debug")]
use crate::debug::{Tls13SecretCallbacksArg, RANDOM_SIZE};
/// Convert a [`std::io::ErrorKind`] into WOLFSSL_CBIO error as descibed in [`EmbedReceive`][0].
///
/// `would_block` is returned if the variant is
/// [`std::io::ErrorKind::WouldBlock`], since wolfssl has different
/// error names (although under the hood the value is the same). Note
/// that the application is expected to have returned
/// [`IOCallbackResult::WouldBlock`] in this case so we shouldn't be
/// here in the first place, but be tollerant in this case.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/wolfio_8h.html#function-embedreceive
fn io_errorkind_into_wolfssl_cbio_error(
kind: std::io::ErrorKind,
would_block: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
use std::io::ErrorKind::*;
match kind {
// Note that WouldBlock also covers EAGAIN errors under the hood.
WouldBlock => would_block,
TimedOut => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_TIMEOUT,
ConnectionReset => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_CONN_RST,
Interrupted => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_ISR,
ConnectionAborted => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_CONN_CLOSE,
_ => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_GENERAL,
}
}
/// Stores configurations we want to initialize a [`Session`] with.
pub struct SessionConfig<IOCB: IOCallbacks> {
/// I/O callback handlers
pub io: IOCB,
/// If set and the session is DTLS, sets the nonblocking mode.
pub dtls_use_nonblock: Option<bool>,
/// If set and the session is DTLS, sets the MTU of the session.
///
/// If value exceeds wolfSSL's `MAX_RECORD_SIZE` (currently 2^14), or
/// is 0, ignored.
pub dtls_mtu: Option<u16>,
/// If set, configures SNI (Server Name Indication) for the session with the
/// given hostname.
pub server_name_indicator: Option<String>,
/// If set, configures the session to check the given domain against the
/// peer certificate during connection.
pub checked_domain_name: Option<String>,
/// If set, specifies a curve group to use for key share
pub keyshare_group: Option<CurveGroup>,
/// If set, callback will be called for all TLS1.3 secret
#[cfg(feature = "debug")]
pub keylogger: Option<Tls13SecretCallbacksArg>,
}
impl<IOCB: IOCallbacks> SessionConfig<IOCB> {
/// Creates a default [`Self`]. A set of IO callbacks implementing
/// [`IOCallbacks`] must be provided.
pub fn new(io: IOCB) -> Self {
Self {
io,
dtls_use_nonblock: Default::default(),
dtls_mtu: Default::default(),
server_name_indicator: Default::default(),
checked_domain_name: Default::default(),
keyshare_group: Default::default(),
#[cfg(feature = "debug")]
keylogger: Default::default(),
}
}
/// When `cond` is True call `func` on `Self`
pub fn when<F>(self, cond: bool, func: F) -> Self
where
F: FnOnce(Self) -> Self,
{
if cond {
func(self)
} else {
self
}
}
/// When `maybe` is Some(_) call `func` on `Self` and the contained value
pub fn when_some<F, T>(self, maybe: Option<T>, func: F) -> Self
where
F: FnOnce(Self, T) -> Self,
{
if let Some(t) = maybe {
func(self, t)
} else {
self
}
}
/// Sets [`Self::dtls_use_nonblock`]
pub fn with_dtls_nonblocking(mut self, is_nonblocking: bool) -> Self {
self.dtls_use_nonblock = Some(is_nonblocking);
self
}
/// Sets [`Self::dtls_mtu`]
pub fn with_dtls_mtu(mut self, mtu: u16) -> Self {
self.dtls_mtu = Some(mtu);
self
}
/// Sets [`Self::server_name_indicator`]
pub fn with_sni(mut self, hostname: &str) -> Self {
self.server_name_indicator = Some(hostname.to_string());
self
}
/// Sets [`Self::checked_domain_name`]
pub fn with_checked_domain_name(mut self, domain: &str) -> Self {
self.checked_domain_name = Some(domain.to_string());
self
}
/// Sets [`Self::keyshare_group`]
pub fn with_keyshare_group(mut self, curve: CurveGroup) -> Self {
self.keyshare_group = Some(curve);
self
}
/// Sets [`Self::keylogger`]
#[cfg(feature = "debug")]
pub fn with_key_logger(mut self, keylogger: Tls13SecretCallbacksArg) -> Self {
self.keylogger = Some(keylogger);
self
}
}
// Wrap a valid pointer to a [`wolfssl_sys::WOLFSSL`] such that we can
// add traits such as `Send`.
struct WolfsslPointer(NonNull<wolfssl_sys::WOLFSSL>);
impl std::ops::Deref for WolfsslPointer {
type Target = NonNull<wolfssl_sys::WOLFSSL>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
// SAFETY: Per [Library Design][] under "Thread Safety"
//
// > A client may share an WOLFSSL object across multiple threads but
// > access must be synchronized, i.e., trying to read/write at the same
// > time from two different threads with the same SSL pointer is not
// > supported.
//
// This is consistent with the requirements for `Send`. The required
// syncronization is handled by wrapping the type in a `Mutex` if
// required.
//
// [Library Design]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html
unsafe impl Send for WolfsslPointer {}
/// Wraps a `WOLFSSL` pointer, as well as the additional fields needed to
/// write into, and read from, wolfSSL's custom IO callbacks.
pub struct Session<IOCB: IOCallbacks> {
protocol: Protocol,
ssl: Mutex<WolfsslPointer>,
/// Box so we have a stable address to pass to FFI.
io: Box<IOCB>,
#[cfg(feature = "debug")]
secret_cb: Option<Box<Tls13SecretCallbacksArg>>,
}
/// Error creating a [`Session`] object.
#[derive(Error, Debug)]
pub enum NewSessionError {
/// `wolfSSL_new` failed
#[error("Failed to allocate WolfSSL Session")]
CreateFailed,
/// A setup operation on the WolfSSL Session
#[error("Failed to setup SSL session context: {0}: {1}")]
SetupFailed(&'static str, Error),
}
impl<IOCB: IOCallbacks> Session<IOCB> {
/// Invokes [`wolfSSL_new`][0]
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__Setup.html#function-wolfssl_new
pub fn new_from_context(
ctx: &Context,
config: SessionConfig<IOCB>,
) -> std::result::Result<Self, NewSessionError> {
// SAFETY: [`wolfSSL_new`][0] ([also][1]) needs a valid `wolfssl_sys::WOLFSSL_CTX` pointer as per documentation
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__Setup.html#function-wolfssl_new
// [1]: https://www.wolfssl.com/doxygen/group__Setup.html#gaa37dc22775da8f6a3b5c149d5dfd6e1c
let ptr = unsafe { wolfssl_sys::wolfSSL_new(ctx.ctx().as_ptr()) };
let mut session = Self {
protocol: ctx.protocol(),
ssl: Mutex::new(WolfsslPointer(
NonNull::new(ptr).ok_or(NewSessionError::CreateFailed)?,
)),
io: Box::new(config.io),
#[cfg(feature = "debug")]
secret_cb: Default::default(),
};
session.register_io_context();
if let Some(is_nonblocking) = config.dtls_use_nonblock {
session.dtls_set_nonblock_use(is_nonblocking);
}
if let Some(mtu) = config.dtls_mtu {
session.dtls_set_mtu(mtu as c_ushort);
}
if let Some(sni) = config.server_name_indicator {
session
.set_server_name_indication(&sni)
.map_err(|e| NewSessionError::SetupFailed("set_server_name_indication", e))?;
}
if let Some(name) = config.checked_domain_name {
session
.set_domain_name_to_check(&name)
.map_err(|e| NewSessionError::SetupFailed("set_domain_name_to_check", e))?;
}
if let Some(curve) = config.keyshare_group {
session
.use_key_share_curve(curve)
.map_err(|e| NewSessionError::SetupFailed("use_key_share_curve", e))?;
}
#[cfg(feature = "debug")]
if let Some(keylogger) = config.keylogger {
session
.enable_tls13_keylog(keylogger)
.map_err(|e| NewSessionError::SetupFailed("keylogger", e))?;
}
Ok(session)
}
/// Gets the protocol version used for the session.
/// Invokes [`wolfssl_sys::wolfSSL_version`]
///
/// No online documentation available for `wolfSSL_version`
pub fn version(&self) -> ProtocolVersion {
let ssl = self.ssl.lock();
// SAFETY: No documentation found for [`wolfSSL_version`][] api,
// From implementation, the api expects valid pointer to `WOLFSSL`
let version = unsafe { wolfssl_sys::wolfSSL_version(ssl.as_ptr()) };
match version as u32 {
wolfssl_sys::TLS1_VERSION => ProtocolVersion::TlsV1_0,
wolfssl_sys::TLS1_1_VERSION => ProtocolVersion::TlsV1_1,
wolfssl_sys::TLS1_2_VERSION => ProtocolVersion::TlsV1_2,
wolfssl_sys::TLS1_3_VERSION => ProtocolVersion::TlsV1_3,
wolfssl_sys::DTLS1_VERSION => ProtocolVersion::DtlsV1_0,
wolfssl_sys::DTLS1_2_VERSION => ProtocolVersion::DtlsV1_2,
wolfssl_sys::DTLS1_3_VERSION => ProtocolVersion::DtlsV1_3,
wolfssl_sys::SSL2_VERSION => ProtocolVersion::SslV2,
wolfssl_sys::SSL3_VERSION => ProtocolVersion::SslV3,
_ => ProtocolVersion::Unknown,
}
}
/// Gets the current cipher of the session.
/// If the cipher name is "None", return None.
pub fn get_current_cipher_name(&self) -> Option<String> {
// SAFETY: [`wolfSSL_get_current_cipher`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Return value is the pointer inside the ssl session. Caller can read it safely
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_get_current_cipher
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#ga0a2985d2088f0b331a4949860fda400d
let cipher = unsafe { wolfssl_sys::wolfSSL_get_current_cipher(self.ssl.lock().as_ptr()) };
let cipher = if !cipher.is_null() {
// SAFETY: Documentation for [`wolfSSL_CIPHER_get_name`][0] ([also][1]) is not clear about the memory usage
// From implementation, return value is the pointer to static buffer. Caller can read it safely
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_cipher_get_name
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#ga1d77df578e8cebd9d75d2211b927d868
let name = unsafe {
let name = wolfssl_sys::wolfSSL_CIPHER_get_name(cipher);
std::ffi::CStr::from_ptr(name).to_str().ok()?.to_string()
};
Some(name)
} else {
None
};
match cipher {
Some(x) if x == "None" => None,
x => x,
}
}
/// Gets the current curve of the session if ECDH was used,
/// otherwise `None`.
pub fn get_current_curve_name(&self) -> Option<String> {
// SAFETY: No online documentation available for
// `wolfSSL_get_curve_name`. In code documentation states:
//
// > Return the name of the curve used for key exchange as a printable string.
// >
// > ssl The SSL/TLS object.
// > returns NULL if ECDH was not used, otherwise the name as a string.
let curve = unsafe { wolfssl_sys::wolfSSL_get_curve_name(self.ssl.lock().as_ptr()) };
if !curve.is_null() {
// SAFETY: Per above if `wolfSSL_get_curve_name` returns
// non-null then the result is a valid string.
unsafe { Some(std::ffi::CStr::from_ptr(curve).to_str().ok()?.to_string()) }
} else {
None
}
}
/// Invokes [`wolfSSL_is_init_finished`][0]
///
/// "Init" in this case is the formation of the TLS connection.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__TLS.html#function-wolfssl_is_init_finished
pub fn is_init_finished(&self) -> bool {
// SAFETY: [`wolfSSL_is_init_finished`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Documentation for return values seems incorrect though, having same text for both success and error case.
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__TLS.html#function-wolfssl_is_init_finished
// [1]: https://www.wolfssl.com/doxygen/group__TLS.html#gaa0bd0ae911e350d1e64b0cc9d3c8292b
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe { wolfssl_sys::wolfSSL_is_init_finished(self.ssl.lock().as_ptr()) } {
0 => false,
1 => true,
e => unreachable!("{e:?}"),
}
}
/// Get a reference to the IOCB embedded in this session
pub fn io_cb(&self) -> &IOCB {
self.io.as_ref()
}
/// Get a mutable reference to the IOCB embedded in this session
pub fn io_cb_mut(&mut self) -> &mut IOCB {
self.io.as_mut()
}
/// Invokes [`wolfSSL_negotiate`][0] *once*.
///
/// The distinction is important because it takes more than one invocation
/// to successfully form a secure session.
///
/// This method will trigger WolfSSL's IO callbacks
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_negotiate
pub fn try_negotiate(&self) -> PollResult<()> {
// SAFETY: [`wolfSSL_negotiate`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_negotiate
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#gaf6780235ee9a7abe3f704a585eb77849
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_negotiate(ssl.as_ptr())
} {
wolfssl_sys::WOLFSSL_SUCCESS => Ok(Poll::Ready(())),
x @ wolfssl_sys::WOLFSSL_FATAL_ERROR => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_WANT_READ => Ok(Poll::PendingRead),
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Ok(Poll::PendingWrite),
wolfssl_sys::wolfSSL_ErrorCodes_APP_DATA_READY
if self.is_secure_renegotiation_supported() =>
{
self.handle_app_data().map(Poll::AppData)
}
e => Err(Error::fatal(e)),
},
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_shutdown`][0] *once*.
///
/// Returns `Poll::Ready(true)` if the connection has been fully
/// (bidirectionally) shutdown, including having seen the "closing
/// notify" message from the peer.
///
/// Returns `Poll::Ready(false)` if the connection has only been
/// shutdown from this end. If you intend to reuse the connection
/// then you must call `try_shutdown` again. You do not need to
/// poll for new I/O first, `Poll::Pending{Read,Write}` will be
/// returned if I/O is required.
///
/// If there is no intent to reuse the connection, you do not need
/// to await for a response from the other side and
/// `Poll::Ready(false)` can be ignored.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_shutdown
pub fn try_shutdown(&self) -> PollResult<bool> {
// SAFETY: [`wolfSSL_shutdown`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_shutdown
// [1]: https://www.wolfssl.com/doxygen/group__TLS.html#ga51f54ec99e4d87f4b25a92fe031439ae
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_shutdown(ssl.as_ptr())
} {
wolfssl_sys::WOLFSSL_SUCCESS => Ok(Poll::Ready(true)),
wolfssl_sys::WOLFSSL_SHUTDOWN_NOT_DONE => Ok(Poll::Ready(false)),
x @ wolfssl_sys::WOLFSSL_FATAL_ERROR => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_WANT_READ => Ok(Poll::PendingRead),
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Ok(Poll::PendingWrite),
wolfssl_sys::wolfSSL_ErrorCodes_APP_DATA_READY
if self.is_secure_renegotiation_supported() =>
{
self.handle_app_data().map(Poll::AppData)
}
x => Err(Error::fatal(x)),
},
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_write`][0] *once*.
///
/// Given a buffer, consumes as much of it as possible, writing into the
/// network.
///
/// This method will return the number of bytes that was successfully
/// written into wolfSSL.
///
/// It is not guaranteed that the entire buffer will be consumed, since we
/// only invoke `wolfSSL_write` once.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_write
//
// Note that even if no data was consumed, WolfSSL might take this
// opportunity to update its internal state (for example, if it needs to
// update encryption keys). This can be seen in
// [`Self::trigger_update_keys`].
pub fn try_write(&self, data_in: &mut BytesMut) -> PollResult<usize> {
// SAFETY: [`wolfSSL_write`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_write
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#gad6cbb3cb90e4d606e9507e4ec06197df
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_write(
ssl.as_ptr(),
data_in.as_ptr() as *const c_void,
data_in.len() as c_int,
)
} {
x if x > 0 => {
data_in.advance(x as usize);
Ok(Poll::Ready(x as usize))
}
x @ (0 | wolfssl_sys::WOLFSSL_FATAL_ERROR) => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_NONE => Ok(Poll::Ready(0)),
wolfssl_sys::WOLFSSL_ERROR_WANT_READ => Ok(Poll::PendingRead),
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Ok(Poll::PendingWrite),
wolfssl_sys::wolfSSL_ErrorCodes_APP_DATA_READY
if self.is_secure_renegotiation_supported() =>
{
self.handle_app_data().map(Poll::AppData)
}
e => Err(Error::fatal(e)),
},
e => Err(Error::fatal(e)),
}
}
/// Invokes [`wolfSSL_read`][0] *once*.
///
/// This can be thought of as the inverse to [`Self::try_write`]:
/// - It reads data from WolfSSL into a buffer.
/// - It appends data to the given buffer, up to its given capacity.
/// - It does not alter existing data inside the buffer.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_read
//
// Like [`Self::try_write`], we call through to `wolfSSL_read` even if there
// is no space to allow WolfSSL's internal state to advance.
pub fn try_read(&self, data_out: &mut BytesMut) -> PollResult<usize> {
let buf = data_out.spare_capacity_mut();
// SAFETY: [`wolfSSL_read`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// The input `buf` is a valid mutable buffer, with proper length.
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_read
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#ga80c3ccd3c0441c77307df3afe88a5c35
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_read(
ssl.as_ptr(),
buf.as_mut_ptr() as *mut c_void,
buf.len() as c_int,
)
} {
x if x > 0 => {
// SAFETY: Now that we've initialized this memory segment, it is safe to update the
// length to account for the initialized data
unsafe {
data_out.set_len(data_out.len() + x as usize);
}
Ok(Poll::Ready(x as usize))
}
x @ (0 | wolfssl_sys::WOLFSSL_FATAL_ERROR) => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_WANT_READ => Ok(Poll::PendingRead),
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Ok(Poll::PendingWrite),
wolfssl_sys::WOLFSSL_ERROR_NONE => Ok(Poll::Ready(0)),
wolfssl_sys::wolfSSL_ErrorCodes_APP_DATA_READY
if self.is_secure_renegotiation_supported() =>
{
self.handle_app_data().map(Poll::AppData)
}
e => Err(Error::fatal(e)),
},
e => Err(Error::fatal(e)),
}
}
/// Checks if this session supports secure renegotiation
///
/// Only some D/TLS connections support secure renegotiation, so this method
/// checks if it's something we can do here.
pub fn is_secure_renegotiation_supported(&self) -> bool {
// SAFETY: No documentation available for `wolfSSL_SSL_get_secure_renegotiation_support`
// But based on the implementation, it is safe to call the api as long as the `ssl` pointer points
// to valid `WOLFSSL` struct
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_SSL_get_secure_renegotiation_support(ssl.as_ptr())
} {
0 => false,
1 => true,
e => unreachable!("{e:?}"),
}
}
/// Checks if there is an ongoing secure renegotiation triggered by
/// [`Self::try_rehandshake`].
//
// NOTE: No documentation found for `wolfSSL_SSL_renegotiate_pending`
pub fn is_secure_renegotiation_pending(&self) -> bool {
// SAFETY: No documentation available for `wolfSSL_SSL_renegotiate_pending`
// But based on the implementation, it is safe to call the api as long as the `ssl` pointer points
// to valid `WOLFSSL` struct
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_SSL_renegotiate_pending(ssl.as_ptr())
} {
0 => false,
1 => true,
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_Rehandshake`][0] *once*.
///
/// Is a no-op unless the session supports secure renegotiation.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html?query=wolfssl_rehandshake#function-wolfssl_rehandshake
pub fn try_rehandshake(&self) -> PollResult<()> {
if !self.is_secure_renegotiation_supported() {
return Ok(Poll::Ready(()));
}
// SAFETY: [`wolfSSL_Rehandshake`][0] ([also][1]) expects valid pointer to `WOLFSSL` and since the `WOLFSSL` struct
// can be used in multiple threads based on [`Library design`][2], protected by a mutex lock
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_rehandshake
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#ga7ba02472014a68d0717ca9243d9dd646
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_Rehandshake(ssl.as_ptr())
} {
wolfssl_sys::WOLFSSL_SUCCESS => Ok(Poll::Ready(())),
x @ wolfssl_sys::WOLFSSL_FATAL_ERROR => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_WANT_READ => Ok(Poll::PendingRead),
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Ok(Poll::PendingWrite),
wolfssl_sys::wolfSSL_ErrorCodes_APP_DATA_READY
if self.is_secure_renegotiation_supported() =>
{
self.handle_app_data().map(Poll::AppData)
}
e => Err(Error::fatal(e)),
},
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_update_keys`][0] *once*
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_update_keys
pub fn try_trigger_update_key(&self) -> PollResult<()> {
if !self.protocol.is_tls_13() && !self.protocol.is_dtls_13() {
return Ok(Poll::Ready(()));
}
if !self.is_init_finished() {
return Ok(Poll::Ready(()));
}
// SAFETY: [`wolfSSL_update_keys`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Other requirements including the protocol version and handshake completed which is checked above
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_update_keys
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#ga38ef7eb0a15b65f3b68d2490dd0535a0
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_update_keys(ssl.as_ptr())
} {
wolfssl_sys::WOLFSSL_SUCCESS => Ok(Poll::Ready(())),
e @ wolfssl_sys::BAD_FUNC_ARG => unreachable!("{e:?}"),
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Ok(Poll::PendingWrite),
e => unreachable!("Received unknown code {e}"),
}
}
/// Invokes [`wolfSSL_key_update_response`][0]
///
/// Returns `true` if the client has sent a key update and is expecting a
/// response, `false` otherwise.
///
/// Note that this is a TLS/DTLS 1.3 only feature. If the session is not TLS/DTLS 1.3
/// we will always return false.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_key_update_response
pub fn is_update_keys_pending(&self) -> bool {
if !self.protocol.is_tls_13() && !self.protocol.is_dtls_13() {
return false;
}
let mut required = std::mem::MaybeUninit::<c_int>::uninit();
// SAFETY: [`wolfSSL_key_update_response`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Other requirements including the protocol version TLS 1.3 is checked above
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_key_update_response
// [1]: https://www.wolfssl.com/doxygen/group__IO.html#ga2f38357d4d7fba294745516caa8f4180
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_key_update_response(ssl.as_ptr(), required.as_mut_ptr())
} {
0 => {}
// panic on non-success, because `ssl` is always non-null and the
// method here must be TLS1.3
e => unreachable!("{e:?}"),
}
// SAFETY: Based on `wolfSSL_key_update_response`][0], required will be populated if the api returns success.
// So safety to call `assume_init()` on success case. On error case we paniced above!
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__IO.html#function-wolfssl_key_update_response
match unsafe { required.assume_init() } {
1 => true,
0 => false,
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_dtls_get_current_timeout`][0].
///
/// This reports how long the calling application needs to wait for
/// available received data, in seconds.
///
/// WolfSSL implements a backoff, so the returned value will likely change.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_get_current_timeout
pub fn dtls_current_timeout(&self) -> Duration {
if !self.is_dtls() {
log::debug!("Session is not configured for DTLS");
}
// SAFETY: [`wolfSSL_dtls_get_current_timeout`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Other requirements including the protocol version DTLS is checked above
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_get_current_timeout
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a07da5ada53a2a68ee8e7a6dab9b5f429
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_dtls_get_current_timeout(ssl.as_ptr())
} {
e @ wolfssl_sys::NOT_COMPILED_IN => unreachable!("{e:?}"),
x if x > 0 => Duration::from_secs(x as u64),
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_dtls_set_timeout_init`][0]
///
/// This sets both the initial timeout (the value WolfSSL uses before any
/// kind of backoff), and the current, ongoing timeout if there is one.
///
/// There are multiple timeout values because WolfSSL has a backoff.
///
/// The duration:
/// - Should not be 0
/// - Should not exceed the current maximum timeout (refer to
/// [`Self::dtls_set_max_timeout`]).
///
/// Truncates to the nearest second.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_set_timeout_init
pub fn dtls_set_timeout(&self, time: Duration) -> Result<()> {
if !self.is_dtls() {
log::debug!("Session is not configured for DTLS");
}
// SAFETY: [`wolfSSL_dtls_set_timeout_init`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Other requirements including the protocol version DTLS is checked above
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__Setup.html#function-wolfssl_dtls_set_timeout_init
// [1]: https://www.wolfssl.com/doxygen/group__Setup.html#ga1dd3c408c996a80b9abfae8f74645d21
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_dtls_set_timeout_init(ssl.as_ptr(), time.as_secs() as c_int)
} {
wolfssl_sys::WOLFSSL_SUCCESS => Ok(()),
x @ wolfssl_sys::BAD_FUNC_ARG => Err(Error::fatal(x)),
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_dtls_set_timeout_max`][0]
///
/// This sets the maximum amount of time WolfSSL is allowed to wait before
/// declaring a timeout, including backoff. (defaults to `DTLS_TIMEOUT_MAX`)
///
/// Returns an error if the argument is set to 0, exceeds WolfSSL's internal
/// limits, or if the argument is lower than the current timeout as set by
/// [`Self::dtls_set_timeout`].
///
/// Truncates to the nearest second.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_set_timeout_max
pub fn dtls_set_max_timeout(&self, time: Duration) -> Result<()> {
if !self.is_dtls() {
log::debug!("Session is not configured for DTLS");
}
// SAFETY: [`wolfSSL_dtls_set_timeout_max`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Other requirements including the protocol version DTLS is checked above
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_set_timeout_max
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a10d57d8c34afabdf6242b9cb164485be
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_dtls_set_timeout_max(ssl.as_ptr(), time.as_secs() as c_int)
} {
wolfssl_sys::WOLFSSL_SUCCESS => Ok(()),
x @ wolfssl_sys::BAD_FUNC_ARG => Err(Error::fatal(x)),
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_dtls_got_timeout`][0]
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_got_timeout
pub fn dtls_has_timed_out(&self) -> Poll<bool> {
if !self.is_dtls() {
log::debug!("Session is not configured for DTLS");
}
// SAFETY: [`wolfSSL_dtls_got_timeout`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_got_timeout
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a86c630a78e966b768332c5b19e485a51
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe {
let ssl = self.ssl.lock();
wolfssl_sys::wolfSSL_dtls_got_timeout(ssl.as_ptr())
} {
e @ wolfssl_sys::NOT_COMPILED_IN => unreachable!("{e:?}"),
wolfssl_sys::WOLFSSL_SUCCESS => Poll::Ready(false),
x @ wolfssl_sys::WOLFSSL_FATAL_ERROR => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_WANT_READ => Poll::PendingRead,
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE => Poll::PendingWrite,
_ => Poll::Ready(true),
},
e => unreachable!("{e:?}"),
}
}
/// Invokes [`wolfSSL_dtls13_use_quick_timeout`][0]
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls13_use_quick_timeout
pub fn dtls13_use_quick_timeout(&self) -> bool {
if !self.is_dtls() {
log::debug!("Session is not configured for DTLS");
return false;
}
let ssl = self.ssl.lock();
// SAFETY: [`wolfSSL_dtls13_use_quick_timeout`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls13_use_quick_timeout
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a61f3b53cb0397dd1debc8b8daaa490c2
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
0 != unsafe { wolfssl_sys::wolfSSL_dtls13_use_quick_timeout(ssl.as_ptr()) }
}
unsafe extern "C" fn io_recv_shim(
_ssl: *mut wolfssl_sys::WOLFSSL,
buf: *mut ::std::os::raw::c_char,
sz: ::std::os::raw::c_int,
ctx: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
debug_assert!(!_ssl.is_null());
debug_assert!(!buf.is_null());
debug_assert!(!ctx.is_null());
// SAFETY:
// We know that this pointer is to the contents of a `Box`
// owned by the `Session`. See `register_io_context` below for
// an argument as to why IO will be stopped (by releasing
// `WOLFSSL`) before that box is dropped.
let io = unsafe { &*(ctx as *mut IOCB) };
let buf = std::slice::from_raw_parts_mut(buf as *mut u8, sz as usize);
match io.recv(buf) {
IOCallbackResult::Ok(nr) => nr as std::os::raw::c_int,
IOCallbackResult::WouldBlock => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_WANT_READ,
IOCallbackResult::Err(err) => io_errorkind_into_wolfssl_cbio_error(
err.kind(),
wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_WANT_READ,
),
}
}
unsafe extern "C" fn io_send_shim(
_ssl: *mut wolfssl_sys::WOLFSSL,
buf: *mut ::std::os::raw::c_char,
sz: ::std::os::raw::c_int,
ctx: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
debug_assert!(!_ssl.is_null());
debug_assert!(!buf.is_null());
debug_assert!(!ctx.is_null());
// SAFETY: We know that this pointer is to the contents of a `Box`
// owned by the `Session`. See `register_io_context` below for
// an argument as to why IO will be stopped (by releasing
// `WOLFSSL`) before that box is dropped.
let io = unsafe { &*(ctx as *mut IOCB) };
let buf = std::slice::from_raw_parts(buf as *mut u8, sz as usize);
match io.send(buf) {
IOCallbackResult::Ok(nr) => nr as std::os::raw::c_int,
IOCallbackResult::WouldBlock => wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_WANT_WRITE,
IOCallbackResult::Err(err) => io_errorkind_into_wolfssl_cbio_error(
err.kind(),
wolfssl_sys::IOerrors_WOLFSSL_CBIO_ERR_WANT_WRITE,
),
}
}
/// Registers a context that will be visible within the custom IO callbacks
/// tied to this `WOLFSSL` session.
///
/// This is done via `wolfSSL_SSLSetIORecv` and
/// `wolfSSL_SSLSetIOSend` (see [`wolfSSL_CTX_SetIORecv`][0] for
/// related docs) [`wolfSSL_SetIOReadCtx`][1] and
/// [`wolfSSL_SetIOWriteCtx`][2].
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/wolfio_8h.html#function-wolfssl_ctx_setiorecv
/// [1]: https://www.wolfssl.com/documentation/manuals/wolfssl/wolfio_8h.html#function-wolfssl_setioreadctx
/// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/wolfio_8h.html#function-wolfssl_setiowritectx
fn register_io_context(&mut self) {
let ssl = self.ssl.lock();
// SAFETY:
// The functions here are 'static so must live longer than `self.ssl`.
unsafe {
wolfssl_sys::wolfSSL_SSLSetIORecv(ssl.as_ptr(), Some(Self::io_recv_shim));
wolfssl_sys::wolfSSL_SSLSetIOSend(ssl.as_ptr(), Some(Self::io_send_shim));
}
let io = &mut *self.io as *mut IOCB as *mut std::ffi::c_void;
// SAFETY:
// `io` here is behind a `Box<>` (`self.io`) so the address is stable.
//
// We free `self.ssl` (the `wolfssl_sys::WOLFSSL`) on drop of
// `self` so we release (and thus quiesce) any use of the io
// callbacks before `io` can be dropped.
//
// Therefore `io` here is valid for as long as it needs to be.
unsafe {
wolfssl_sys::wolfSSL_SetIOReadCtx(ssl.as_ptr(), io);
wolfssl_sys::wolfSSL_SetIOWriteCtx(ssl.as_ptr(), io);
}
}
/// Extracts a given error code from this session, by invoking
/// [`wolfSSL_get_error`][0].
///
/// This is stateful, and collects the error of the previous invoked method.
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/group__Debug.html#function_wolfssl_get_error
fn get_error(&self, ret: c_int) -> c_int {
let ssl = self.ssl.lock();
// SAFETY: [`wolfSSL_get_error`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_get_error
// [1]: https://www.wolfssl.com/doxygen/group__Debug.html#gaafd5671d443fa684913ba5955a4eb591
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
unsafe { wolfssl_sys::wolfSSL_get_error(ssl.as_ptr(), ret) }
}
/// During secure renegotiation, wolfssl allows the user to send and receive data.
///
/// If data is detected, WolfSSL will return a `APP_DATA_READY` code, it is then
/// expected that we immediately read this data, or risk it dropping.
///
/// Since every WolfSSL TLS API could raise this error, the logic is
/// centralized here, in this helper function.
//
// It's implied that the data has already arrived and `wolfSSL_read` will
// not return a `WANT_READ` or similar error code, so if we see them we will
// convert it to an error.
fn handle_app_data(&self) -> Result<Bytes> {
debug_assert!(self.is_secure_renegotiation_supported());
let mut buf = BytesMut::with_capacity(TLS_MAX_RECORD_SIZE);
// Collect the appdata wolfssl kindly informed us about.
match self.try_read(&mut buf) {
Ok(Poll::Ready(_)) => Ok(buf.freeze()),
Err(Error::Fatal(e) | Error::AppData(e)) => Err(Error::AppData(e)),
Ok(Poll::PendingRead | Poll::PendingWrite) => {
unreachable!("App data is ready, so why are we waiting?")
}
// Lightway Core (C) does recurse, but only seems to
// care about the last wolfssl_read, which likely
// means that this won't recurse.
Ok(Poll::AppData(_)) => {
unreachable!("We assume that no nested calls are possible.")
}
}
}
/// Invokes [`wolfSSL_set_using_nonblock`][0]
///
/// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_set_using_nonblock
fn dtls_set_nonblock_use(&self, is_nonblock: bool) {
if !self.is_dtls() {
log::debug!("Session is not configured for DTLS");
return;
}
// SAFETY: [`wolfSSL_dtls_set_using_nonblock`][0] ([also][1]) expects a valid pointer to `WOLFSSL`. Per the
// [Library design][2] access is synchronized via the containing [`Mutex`]
// Other requirements including the protocol version DTLS is checked above
//
// [0]: https://www.wolfssl.com/documentation/manuals/wolfssl/ssl_8h.html#function-wolfssl_dtls_set_using_nonblock
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a585412eb9473686f4d65b971c8afc223