rwebtransport 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/Cargo.lock +666 -0
  2. package/Cargo.toml +31 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +41 -0
  5. package/README.md +550 -0
  6. package/crates/echo-server/Cargo.toml +20 -0
  7. package/crates/echo-server/src/main.rs +666 -0
  8. package/crates/native/Cargo.toml +23 -0
  9. package/crates/native/src/config.rs +129 -0
  10. package/crates/native/src/driver.rs +463 -0
  11. package/crates/native/src/lib.rs +821 -0
  12. package/crates/native/src/server.rs +718 -0
  13. package/crates/native/src/session.rs +1191 -0
  14. package/crates/native/src/tls.rs +96 -0
  15. package/crates/wtcore/Cargo.toml +15 -0
  16. package/crates/wtcore/src/h3.rs +226 -0
  17. package/crates/wtcore/src/lib.rs +9 -0
  18. package/dist/datagrams.d.ts +16 -0
  19. package/dist/errors.d.ts +10 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.js +1512 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/index.mjs +1473 -0
  24. package/dist/index.mjs.map +1 -0
  25. package/dist/loader.d.ts +2 -0
  26. package/dist/native.d.ts +226 -0
  27. package/dist/server.d.ts +44 -0
  28. package/dist/streams.d.ts +14 -0
  29. package/dist/types.d.ts +24 -0
  30. package/dist/webtransport.d.ts +20 -0
  31. package/package.json +79 -0
  32. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  33. package/scripts/bench.mjs +180 -0
  34. package/scripts/build.js +67 -0
  35. package/scripts/postinstall.js +67 -0
  36. package/scripts/setup-unix.sh +129 -0
  37. package/scripts/setup-windows.ps1 +229 -0
  38. package/vendor/quiche/AGENTS.md +75 -0
  39. package/vendor/quiche/COPYING +23 -0
  40. package/vendor/quiche/Cargo.lock +1212 -0
  41. package/vendor/quiche/Cargo.toml +215 -0
  42. package/vendor/quiche/README.md +466 -0
  43. package/vendor/quiche/examples/Makefile +46 -0
  44. package/vendor/quiche/examples/README.md +20 -0
  45. package/vendor/quiche/examples/cert-big.crt +90 -0
  46. package/vendor/quiche/examples/cert.crt +18 -0
  47. package/vendor/quiche/examples/cert.key +28 -0
  48. package/vendor/quiche/examples/client.c +343 -0
  49. package/vendor/quiche/examples/client.rs +281 -0
  50. package/vendor/quiche/examples/gen-certs.sh +15 -0
  51. package/vendor/quiche/examples/http3-client.c +473 -0
  52. package/vendor/quiche/examples/http3-client.rs +349 -0
  53. package/vendor/quiche/examples/http3-server.c +614 -0
  54. package/vendor/quiche/examples/http3-server.rs +680 -0
  55. package/vendor/quiche/examples/qpack-decode.rs +87 -0
  56. package/vendor/quiche/examples/qpack-encode.rs +88 -0
  57. package/vendor/quiche/examples/rootca.crt +21 -0
  58. package/vendor/quiche/examples/server.c +529 -0
  59. package/vendor/quiche/examples/server.rs +529 -0
  60. package/vendor/quiche/include/quiche.h +1282 -0
  61. package/vendor/quiche/quiche.svg +1 -0
  62. package/vendor/quiche/src/buffers.rs +105 -0
  63. package/vendor/quiche/src/build.rs +66 -0
  64. package/vendor/quiche/src/cid.rs +1079 -0
  65. package/vendor/quiche/src/crypto/boringssl.rs +373 -0
  66. package/vendor/quiche/src/crypto/mod.rs +671 -0
  67. package/vendor/quiche/src/dgram.rs +118 -0
  68. package/vendor/quiche/src/error.rs +263 -0
  69. package/vendor/quiche/src/ffi.rs +2290 -0
  70. package/vendor/quiche/src/flowcontrol.rs +246 -0
  71. package/vendor/quiche/src/frame.rs +2172 -0
  72. package/vendor/quiche/src/h3/AGENTS.md +54 -0
  73. package/vendor/quiche/src/h3/ffi.rs +442 -0
  74. package/vendor/quiche/src/h3/frame.rs +1404 -0
  75. package/vendor/quiche/src/h3/mod.rs +8223 -0
  76. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
  77. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
  78. package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
  79. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
  80. package/vendor/quiche/src/h3/stream.rs +1595 -0
  81. package/vendor/quiche/src/lib.rs +9556 -0
  82. package/vendor/quiche/src/minmax.rs +446 -0
  83. package/vendor/quiche/src/packet.rs +2334 -0
  84. package/vendor/quiche/src/path.rs +1374 -0
  85. package/vendor/quiche/src/pmtud.rs +652 -0
  86. package/vendor/quiche/src/rand.rs +64 -0
  87. package/vendor/quiche/src/range_buf.rs +186 -0
  88. package/vendor/quiche/src/ranges.rs +755 -0
  89. package/vendor/quiche/src/recovery/AGENTS.md +80 -0
  90. package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
  91. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
  92. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
  93. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
  94. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
  95. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
  96. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
  97. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
  98. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
  99. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
  100. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
  101. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
  102. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
  103. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
  104. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
  105. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
  106. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
  107. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
  108. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
  109. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
  110. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
  111. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
  112. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
  113. package/vendor/quiche/src/recovery/mod.rs +3086 -0
  114. package/vendor/quiche/src/recovery/rtt.rs +152 -0
  115. package/vendor/quiche/src/stream/mod.rs +2210 -0
  116. package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
  117. package/vendor/quiche/src/stream/send_buf.rs +849 -0
  118. package/vendor/quiche/src/test_utils.rs +681 -0
  119. package/vendor/quiche/src/tests.rs +12922 -0
  120. package/vendor/quiche/src/tls/boringssl.rs +375 -0
  121. package/vendor/quiche/src/tls/mod.rs +1245 -0
  122. package/vendor/quiche/src/transport_params.rs +628 -0
@@ -0,0 +1,1245 @@
1
+ // Copyright (C) 2018-2019, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ use std::ffi;
28
+ use std::mem::ManuallyDrop;
29
+ use std::ptr;
30
+ use std::slice;
31
+
32
+ use std::io::Write;
33
+
34
+ use std::sync::LazyLock;
35
+
36
+ use libc::c_char;
37
+ use libc::c_int;
38
+ use libc::c_uint;
39
+ use libc::c_void;
40
+
41
+ use crate::Error;
42
+ use crate::Result;
43
+
44
+ use crate::Connection;
45
+ use crate::ConnectionError;
46
+
47
+ use crate::crypto;
48
+ use crate::packet;
49
+
50
+ const TLS1_3_VERSION: u16 = 0x0304;
51
+ const TLS_ALERT_ERROR: u64 = 0x100;
52
+ const INTERNAL_ERROR: u64 = 0x01;
53
+
54
+ #[allow(non_camel_case_types)]
55
+ #[repr(transparent)]
56
+ struct SSL_METHOD {
57
+ _unused: c_void,
58
+ }
59
+
60
+ #[allow(non_camel_case_types)]
61
+ #[repr(transparent)]
62
+ struct SSL_CTX {
63
+ _unused: c_void,
64
+ }
65
+
66
+ #[allow(non_camel_case_types)]
67
+ #[repr(transparent)]
68
+ struct SSL {
69
+ _unused: c_void,
70
+ }
71
+
72
+ #[allow(non_camel_case_types)]
73
+ #[repr(transparent)]
74
+ struct SSL_CIPHER {
75
+ _unused: c_void,
76
+ }
77
+
78
+ #[allow(non_camel_case_types)]
79
+ #[repr(transparent)]
80
+ struct SSL_SESSION {
81
+ _unused: c_void,
82
+ }
83
+
84
+ #[allow(non_camel_case_types)]
85
+ #[repr(transparent)]
86
+ struct X509_VERIFY_PARAM {
87
+ _unused: c_void,
88
+ }
89
+
90
+ #[allow(non_camel_case_types)]
91
+ #[repr(transparent)]
92
+ #[cfg(windows)]
93
+ struct X509_STORE {
94
+ _unused: c_void,
95
+ }
96
+
97
+ #[allow(non_camel_case_types)]
98
+ #[repr(transparent)]
99
+ struct X509_STORE_CTX {
100
+ _unused: c_void,
101
+ }
102
+
103
+ #[allow(non_camel_case_types)]
104
+ #[repr(transparent)]
105
+ #[cfg(windows)]
106
+ struct X509 {
107
+ _unused: c_void,
108
+ }
109
+
110
+ #[allow(non_camel_case_types)]
111
+ #[repr(transparent)]
112
+ struct STACK_OF {
113
+ _unused: c_void,
114
+ }
115
+
116
+ #[cfg(test)]
117
+ #[repr(C)]
118
+ #[allow(non_camel_case_types)]
119
+ #[allow(dead_code)]
120
+ enum ssl_private_key_result_t {
121
+ ssl_private_key_success,
122
+ ssl_private_key_retry,
123
+ ssl_private_key_failure,
124
+ }
125
+
126
+ /// BoringSSL ex_data index for quiche connections.
127
+ pub static QUICHE_EX_DATA_INDEX: LazyLock<c_int> = LazyLock::new(|| unsafe {
128
+ SSL_get_ex_new_index(0, ptr::null(), ptr::null(), ptr::null(), ptr::null())
129
+ });
130
+
131
+ pub struct Context(*mut SSL_CTX);
132
+
133
+ impl Context {
134
+ // Note: some vendor-specific methods are implemented in the boringssl
135
+ // submodule.
136
+ pub fn new() -> Result<Context> {
137
+ unsafe {
138
+ let ctx_raw = SSL_CTX_new(TLS_method());
139
+
140
+ let mut ctx = Context(ctx_raw);
141
+
142
+ ctx.set_session_callback();
143
+
144
+ ctx.load_ca_certs()?;
145
+
146
+ Ok(ctx)
147
+ }
148
+ }
149
+
150
+ #[cfg(feature = "boringssl-boring-crate")]
151
+ pub fn from_boring(
152
+ ssl_ctx_builder: boring::ssl::SslContextBuilder,
153
+ ) -> Context {
154
+ use foreign_types_shared::ForeignType;
155
+
156
+ let mut ctx = Context(ssl_ctx_builder.build().into_ptr() as _);
157
+ ctx.set_session_callback();
158
+
159
+ ctx
160
+ }
161
+
162
+ pub fn new_handshake(&mut self) -> Result<Handshake> {
163
+ unsafe {
164
+ let ssl = SSL_new(self.as_mut_ptr());
165
+ Ok(Handshake::new(ssl))
166
+ }
167
+ }
168
+
169
+ pub fn load_verify_locations_from_file(&mut self, file: &str) -> Result<()> {
170
+ let file = ffi::CString::new(file).map_err(|_| Error::TlsFail)?;
171
+ map_result(unsafe {
172
+ SSL_CTX_load_verify_locations(
173
+ self.as_mut_ptr(),
174
+ file.as_ptr(),
175
+ ptr::null(),
176
+ )
177
+ })
178
+ }
179
+
180
+ pub fn load_verify_locations_from_directory(
181
+ &mut self, path: &str,
182
+ ) -> Result<()> {
183
+ let path = ffi::CString::new(path).map_err(|_| Error::TlsFail)?;
184
+ map_result(unsafe {
185
+ SSL_CTX_load_verify_locations(
186
+ self.as_mut_ptr(),
187
+ ptr::null(),
188
+ path.as_ptr(),
189
+ )
190
+ })
191
+ }
192
+
193
+ pub fn use_certificate_chain_file(&mut self, file: &str) -> Result<()> {
194
+ let cstr = ffi::CString::new(file).map_err(|_| Error::TlsFail)?;
195
+ map_result(unsafe {
196
+ SSL_CTX_use_certificate_chain_file(self.as_mut_ptr(), cstr.as_ptr())
197
+ })
198
+ }
199
+
200
+ pub fn use_privkey_file(&mut self, file: &str) -> Result<()> {
201
+ let cstr = ffi::CString::new(file).map_err(|_| Error::TlsFail)?;
202
+ map_result(unsafe {
203
+ SSL_CTX_use_PrivateKey_file(self.as_mut_ptr(), cstr.as_ptr(), 1)
204
+ })
205
+ }
206
+
207
+ #[cfg(not(windows))]
208
+ fn load_ca_certs(&mut self) -> Result<()> {
209
+ unsafe { map_result(SSL_CTX_set_default_verify_paths(self.as_mut_ptr())) }
210
+ }
211
+
212
+ #[cfg(windows)]
213
+ fn load_ca_certs(&mut self) -> Result<()> {
214
+ unsafe {
215
+ let cstr = ffi::CString::new("Root").map_err(|_| Error::TlsFail)?;
216
+ let sys_store =
217
+ windows_sys::Win32::Security::Cryptography::CertOpenSystemStoreA(
218
+ 0,
219
+ cstr.as_ptr() as windows_sys::core::PCSTR,
220
+ );
221
+ if sys_store.is_null() {
222
+ return Err(Error::TlsFail);
223
+ }
224
+
225
+ let ctx_store = SSL_CTX_get_cert_store(self.as_mut_ptr());
226
+ if ctx_store.is_null() {
227
+ return Err(Error::TlsFail);
228
+ }
229
+
230
+ let mut ctx_p = windows_sys::Win32::Security::Cryptography::CertEnumCertificatesInStore(
231
+ sys_store,
232
+ ptr::null(),
233
+ );
234
+
235
+ while !ctx_p.is_null() {
236
+ let in_p = (*ctx_p).pbCertEncoded as *const u8;
237
+
238
+ let cert = d2i_X509(
239
+ ptr::null_mut(),
240
+ &in_p,
241
+ (*ctx_p).cbCertEncoded as i32,
242
+ );
243
+ if !cert.is_null() {
244
+ X509_STORE_add_cert(ctx_store, cert);
245
+ }
246
+
247
+ X509_free(cert);
248
+
249
+ ctx_p = windows_sys::Win32::Security::Cryptography::CertEnumCertificatesInStore(
250
+ sys_store, ctx_p,
251
+ );
252
+ }
253
+
254
+ // tidy up
255
+ windows_sys::Win32::Security::Cryptography::CertFreeCertificateContext(ctx_p);
256
+ windows_sys::Win32::Security::Cryptography::CertCloseStore(
257
+ sys_store, 0,
258
+ );
259
+ }
260
+
261
+ Ok(())
262
+ }
263
+
264
+ fn set_session_callback(&mut self) {
265
+ unsafe {
266
+ // This is needed to enable the session callback on the client. On
267
+ // the server it doesn't do anything.
268
+ SSL_CTX_set_session_cache_mode(
269
+ self.as_mut_ptr(),
270
+ 0x0001, // SSL_SESS_CACHE_CLIENT
271
+ );
272
+
273
+ SSL_CTX_sess_set_new_cb(self.as_mut_ptr(), Some(new_session));
274
+ };
275
+ }
276
+
277
+ pub fn set_verify(&mut self, verify: bool) {
278
+ // true -> 0x01 SSL_VERIFY_PEER
279
+ // false -> 0x00 SSL_VERIFY_NONE
280
+ let mode = i32::from(verify);
281
+
282
+ // Note: Base on two used modes(see above), it seems ok for both, bssl and
283
+ // ossl. If mode needs to be ored then it may need to be adjusted.
284
+ unsafe {
285
+ SSL_CTX_set_verify(self.as_mut_ptr(), mode, None);
286
+ }
287
+ }
288
+
289
+ pub fn enable_keylog(&mut self) {
290
+ unsafe {
291
+ SSL_CTX_set_keylog_callback(self.as_mut_ptr(), Some(keylog));
292
+ }
293
+ }
294
+
295
+ pub fn set_alpn(&mut self, v: &[&[u8]]) -> Result<()> {
296
+ let mut protos: Vec<u8> = Vec::new();
297
+
298
+ for proto in v {
299
+ protos.push(proto.len() as u8);
300
+ protos.extend_from_slice(proto);
301
+ }
302
+
303
+ // Configure ALPN for servers.
304
+ unsafe {
305
+ SSL_CTX_set_alpn_select_cb(
306
+ self.as_mut_ptr(),
307
+ Some(select_alpn),
308
+ ptr::null_mut(),
309
+ );
310
+ }
311
+
312
+ // Configure ALPN for clients.
313
+ map_result_zero_is_success(unsafe {
314
+ SSL_CTX_set_alpn_protos(
315
+ self.as_mut_ptr(),
316
+ protos.as_ptr(),
317
+ protos.len(),
318
+ )
319
+ })
320
+ }
321
+
322
+ pub fn set_ticket_key(&mut self, key: &[u8]) -> Result<()> {
323
+ map_result(unsafe {
324
+ SSL_CTX_set_tlsext_ticket_keys(
325
+ self.as_mut_ptr(),
326
+ key.as_ptr(),
327
+ key.len(),
328
+ )
329
+ })
330
+ }
331
+
332
+ fn as_mut_ptr(&mut self) -> *mut SSL_CTX {
333
+ self.0
334
+ }
335
+ }
336
+
337
+ // NOTE: These traits are not automatically implemented for Context due to the
338
+ // raw pointer it wraps. However, the underlying data is not aliased (as Context
339
+ // should be its only owner), and there is no interior mutability, as the
340
+ // pointer is not accessed directly outside of this module, and the Context
341
+ // object API should preserve Rust's borrowing guarantees.
342
+ unsafe impl Send for Context {}
343
+ unsafe impl Sync for Context {}
344
+
345
+ impl Drop for Context {
346
+ fn drop(&mut self) {
347
+ unsafe { SSL_CTX_free(self.as_mut_ptr()) }
348
+ }
349
+ }
350
+
351
+ pub struct Handshake {
352
+ /// Raw pointer
353
+ ptr: *mut SSL,
354
+ /// SSL_process_quic_post_handshake should be called when whenever
355
+ /// SSL_provide_quic_data is called to process the provided data.
356
+ provided_data_outstanding: bool,
357
+ }
358
+
359
+ impl Handshake {
360
+ // Note: some vendor-specific methods are implemented in the boringssl
361
+ // submodule.
362
+ #[cfg(any(feature = "ffi", feature = "boringssl-boring-crate"))]
363
+ pub unsafe fn from_ptr(ssl: *mut c_void) -> Handshake {
364
+ Handshake::new(ssl as *mut SSL)
365
+ }
366
+
367
+ fn new(ptr: *mut SSL) -> Handshake {
368
+ Handshake {
369
+ ptr,
370
+ provided_data_outstanding: false,
371
+ }
372
+ }
373
+
374
+ pub fn get_error(&self, ret_code: c_int) -> c_int {
375
+ unsafe { SSL_get_error(self.as_ptr(), ret_code) }
376
+ }
377
+
378
+ pub fn init(&mut self, is_server: bool) -> Result<()> {
379
+ self.set_state(is_server);
380
+
381
+ self.set_min_proto_version(TLS1_3_VERSION)?;
382
+ self.set_max_proto_version(TLS1_3_VERSION)?;
383
+
384
+ self.set_quic_method()?;
385
+
386
+ // TODO: the early data context should include transport parameters and
387
+ // HTTP/3 SETTINGS in wire format.
388
+ self.set_quic_early_data_context(b"quiche")?;
389
+
390
+ self.set_quiet_shutdown(true);
391
+
392
+ Ok(())
393
+ }
394
+
395
+ pub fn use_legacy_codepoint(&mut self, use_legacy: bool) {
396
+ unsafe {
397
+ SSL_set_quic_use_legacy_codepoint(
398
+ self.as_mut_ptr(),
399
+ use_legacy as c_int,
400
+ );
401
+ }
402
+ }
403
+
404
+ pub fn set_state(&mut self, is_server: bool) {
405
+ unsafe {
406
+ if is_server {
407
+ SSL_set_accept_state(self.as_mut_ptr());
408
+ } else {
409
+ SSL_set_connect_state(self.as_mut_ptr());
410
+ }
411
+ }
412
+ }
413
+
414
+ pub fn set_ex_data<T>(&mut self, idx: c_int, data: *const T) -> Result<()> {
415
+ map_result(unsafe {
416
+ let ptr = data as *mut c_void;
417
+ SSL_set_ex_data(self.as_mut_ptr(), idx, ptr)
418
+ })
419
+ }
420
+
421
+ pub fn set_quic_method(&mut self) -> Result<()> {
422
+ map_result(unsafe {
423
+ SSL_set_quic_method(self.as_mut_ptr(), &QUICHE_STREAM_METHOD)
424
+ })
425
+ }
426
+
427
+ pub fn set_min_proto_version(&mut self, version: u16) -> Result<()> {
428
+ map_result(unsafe {
429
+ SSL_set_min_proto_version(self.as_mut_ptr(), version)
430
+ })
431
+ }
432
+
433
+ pub fn set_max_proto_version(&mut self, version: u16) -> Result<()> {
434
+ map_result(unsafe {
435
+ SSL_set_max_proto_version(self.as_mut_ptr(), version)
436
+ })
437
+ }
438
+
439
+ pub fn set_quiet_shutdown(&mut self, mode: bool) {
440
+ unsafe { SSL_set_quiet_shutdown(self.as_mut_ptr(), i32::from(mode)) }
441
+ }
442
+
443
+ pub fn set_host_name(&mut self, name: &str) -> Result<()> {
444
+ let cstr = ffi::CString::new(name).map_err(|_| Error::TlsFail)?;
445
+ let rc =
446
+ unsafe { SSL_set_tlsext_host_name(self.as_mut_ptr(), cstr.as_ptr()) };
447
+ self.map_result_ssl(rc)?;
448
+
449
+ let param = unsafe { SSL_get0_param(self.as_mut_ptr()) };
450
+
451
+ map_result(unsafe {
452
+ X509_VERIFY_PARAM_set1_host(param, cstr.as_ptr(), name.len())
453
+ })
454
+ }
455
+
456
+ pub fn set_quic_transport_params(
457
+ &mut self, params: &crate::TransportParams, is_server: bool,
458
+ ) -> Result<()> {
459
+ let mut raw_params = [0; 128];
460
+
461
+ let raw_params =
462
+ crate::TransportParams::encode(params, is_server, &mut raw_params)?;
463
+
464
+ let rc = unsafe {
465
+ SSL_set_quic_transport_params(
466
+ self.as_mut_ptr(),
467
+ raw_params.as_ptr(),
468
+ raw_params.len(),
469
+ )
470
+ };
471
+ self.map_result_ssl(rc)
472
+ }
473
+
474
+ pub fn quic_transport_params(&self) -> &[u8] {
475
+ let mut ptr: *const u8 = ptr::null();
476
+ let mut len: usize = 0;
477
+
478
+ unsafe {
479
+ SSL_get_peer_quic_transport_params(self.as_ptr(), &mut ptr, &mut len);
480
+ }
481
+
482
+ if len == 0 {
483
+ return &mut [];
484
+ }
485
+
486
+ unsafe { slice::from_raw_parts(ptr, len) }
487
+ }
488
+
489
+ pub fn alpn_protocol(&self) -> &[u8] {
490
+ let mut ptr: *const u8 = ptr::null();
491
+ let mut len: u32 = 0;
492
+
493
+ unsafe {
494
+ SSL_get0_alpn_selected(self.as_ptr(), &mut ptr, &mut len);
495
+ }
496
+
497
+ if len == 0 {
498
+ return &mut [];
499
+ }
500
+
501
+ unsafe { slice::from_raw_parts(ptr, len as usize) }
502
+ }
503
+
504
+ pub fn server_name(&self) -> Option<&str> {
505
+ let s = unsafe {
506
+ let ptr = SSL_get_servername(
507
+ self.as_ptr(),
508
+ 0, // TLSEXT_NAMETYPE_host_name
509
+ );
510
+
511
+ if ptr.is_null() {
512
+ return None;
513
+ }
514
+
515
+ ffi::CStr::from_ptr(ptr)
516
+ };
517
+
518
+ s.to_str().ok()
519
+ }
520
+
521
+ pub fn provide_data(
522
+ &mut self, level: crypto::Level, buf: &[u8],
523
+ ) -> Result<()> {
524
+ self.provided_data_outstanding = true;
525
+ let rc = unsafe {
526
+ SSL_provide_quic_data(
527
+ self.as_mut_ptr(),
528
+ level,
529
+ buf.as_ptr(),
530
+ buf.len(),
531
+ )
532
+ };
533
+ self.map_result_ssl(rc)
534
+ }
535
+
536
+ pub fn do_handshake(&mut self, ex_data: &mut ExData) -> Result<()> {
537
+ self.set_ex_data(*QUICHE_EX_DATA_INDEX, ex_data)?;
538
+ let rc = unsafe { SSL_do_handshake(self.as_mut_ptr()) };
539
+ self.set_ex_data::<Connection>(*QUICHE_EX_DATA_INDEX, ptr::null())?;
540
+
541
+ self.set_transport_error(ex_data, rc);
542
+ self.map_result_ssl(rc)
543
+ }
544
+
545
+ pub fn process_post_handshake(&mut self, ex_data: &mut ExData) -> Result<()> {
546
+ // If SSL_provide_quic_data hasn't been called since we last called
547
+ // SSL_process_quic_post_handshake, then there's nothing to do.
548
+ if !self.provided_data_outstanding {
549
+ return Ok(());
550
+ }
551
+ self.provided_data_outstanding = false;
552
+
553
+ self.set_ex_data(*QUICHE_EX_DATA_INDEX, ex_data)?;
554
+ let rc = unsafe { SSL_process_quic_post_handshake(self.as_mut_ptr()) };
555
+ self.set_ex_data::<Connection>(*QUICHE_EX_DATA_INDEX, ptr::null())?;
556
+
557
+ self.set_transport_error(ex_data, rc);
558
+ self.map_result_ssl(rc)
559
+ }
560
+
561
+ pub fn write_level(&self) -> crypto::Level {
562
+ unsafe { SSL_quic_write_level(self.as_ptr()) }
563
+ }
564
+
565
+ pub fn cipher(&self) -> Option<crypto::Algorithm> {
566
+ let cipher =
567
+ map_result_ptr(unsafe { SSL_get_current_cipher(self.as_ptr()) });
568
+
569
+ get_cipher_from_ptr(cipher.ok()?).ok()
570
+ }
571
+
572
+ #[cfg(test)]
573
+ pub fn set_options(&mut self, opts: u32) {
574
+ unsafe {
575
+ SSL_set_options(self.as_mut_ptr(), opts);
576
+ }
577
+ }
578
+
579
+ pub fn is_completed(&self) -> bool {
580
+ unsafe { SSL_in_init(self.as_ptr()) == 0 }
581
+ }
582
+
583
+ pub fn is_resumed(&self) -> bool {
584
+ unsafe { SSL_session_reused(self.as_ptr()) == 1 }
585
+ }
586
+
587
+ pub fn clear(&mut self) -> Result<()> {
588
+ let rc = unsafe { SSL_clear(self.as_mut_ptr()) };
589
+ self.map_result_ssl(rc)
590
+ }
591
+
592
+ fn as_ptr(&self) -> *const SSL {
593
+ self.ptr
594
+ }
595
+
596
+ fn as_mut_ptr(&mut self) -> *mut SSL {
597
+ self.ptr
598
+ }
599
+
600
+ fn map_result_ssl(&mut self, bssl_result: c_int) -> Result<()> {
601
+ match bssl_result {
602
+ 1 => Ok(()),
603
+
604
+ _ => {
605
+ let ssl_err = self.get_error(bssl_result);
606
+ match ssl_err {
607
+ // SSL_ERROR_SSL
608
+ 1 => {
609
+ log_ssl_error();
610
+
611
+ Err(Error::TlsFail)
612
+ },
613
+
614
+ // SSL_ERROR_WANT_READ
615
+ 2 => Err(Error::Done),
616
+
617
+ // SSL_ERROR_WANT_WRITE
618
+ 3 => Err(Error::Done),
619
+
620
+ // SSL_ERROR_WANT_X509_LOOKUP
621
+ 4 => Err(Error::Done),
622
+
623
+ // SSL_ERROR_SYSCALL
624
+ 5 => Err(Error::TlsFail),
625
+
626
+ // SSL_ERROR_PENDING_SESSION
627
+ 11 => Err(Error::Done),
628
+
629
+ // SSL_ERROR_PENDING_CERTIFICATE
630
+ 12 => Err(Error::Done),
631
+
632
+ // SSL_ERROR_WANT_PRIVATE_KEY_OPERATION
633
+ 13 => Err(Error::Done),
634
+
635
+ // SSL_ERROR_PENDING_TICKET
636
+ 14 => Err(Error::Done),
637
+
638
+ // SSL_ERROR_EARLY_DATA_REJECTED
639
+ 15 => {
640
+ self.reset_early_data_reject();
641
+ Err(Error::Done)
642
+ },
643
+
644
+ // SSL_ERROR_WANT_CERTIFICATE_VERIFY
645
+ 16 => Err(Error::Done),
646
+
647
+ _ => Err(Error::TlsFail),
648
+ }
649
+ },
650
+ }
651
+ }
652
+
653
+ fn set_transport_error(&mut self, ex_data: &mut ExData, bssl_result: c_int) {
654
+ // SSL_ERROR_SSL
655
+ if self.get_error(bssl_result) == 1 {
656
+ // SSL_ERROR_SSL can't be recovered so ensure we set a
657
+ // local_error so the connection is closed.
658
+ // See https://www.openssl.org/docs/man1.1.1/man3/SSL_get_error.html
659
+ if ex_data.local_error.is_none() {
660
+ *ex_data.local_error = Some(ConnectionError {
661
+ is_app: false,
662
+ error_code: INTERNAL_ERROR,
663
+ reason: Vec::new(),
664
+ })
665
+ }
666
+ }
667
+ }
668
+
669
+ #[cfg(feature = "boringssl-boring-crate")]
670
+ pub(crate) fn ssl_mut(&mut self) -> &mut boring::ssl::SslRef {
671
+ use foreign_types_shared::ForeignTypeRef;
672
+
673
+ unsafe { boring::ssl::SslRef::from_ptr_mut(self.as_mut_ptr() as _) }
674
+ }
675
+ }
676
+
677
+ // NOTE: These traits are not automatically implemented for Handshake due to the
678
+ // raw pointer it wraps. However, the underlying data is not aliased (as
679
+ // Handshake should be its only owner), and there is no interior mutability, as
680
+ // the pointer is not accessed directly outside of this module, and the
681
+ // Handshake object API should preserve Rust's borrowing guarantees.
682
+ unsafe impl Send for Handshake {}
683
+ unsafe impl Sync for Handshake {}
684
+
685
+ impl Drop for Handshake {
686
+ fn drop(&mut self) {
687
+ unsafe { SSL_free(self.as_mut_ptr()) }
688
+ }
689
+ }
690
+
691
+ pub struct ExData<'a> {
692
+ pub application_protos: &'a Vec<Vec<u8>>,
693
+
694
+ pub crypto_ctx: &'a mut [packet::CryptoContext; packet::Epoch::count()],
695
+
696
+ pub session: &'a mut Option<Vec<u8>>,
697
+
698
+ pub local_error: &'a mut Option<ConnectionError>,
699
+
700
+ pub keylog: Option<&'a mut Box<dyn Write + Send + Sync>>,
701
+
702
+ pub trace_id: &'a str,
703
+
704
+ pub local_transport_params: crate::TransportParams,
705
+
706
+ pub recovery_config: crate::recovery::RecoveryConfig,
707
+
708
+ pub tx_cap_factor: f64,
709
+
710
+ /// PMTUD configuration: (enable, max_probes)
711
+ pub pmtud: Option<(bool, u8)>,
712
+
713
+ pub is_server: bool,
714
+
715
+ /// see [`Connection::use_initial_max_data_as_flow_control_win`]
716
+ pub use_initial_max_data_as_flow_control_win: bool,
717
+ }
718
+
719
+ impl<'a> ExData<'a> {
720
+ fn from_ssl_ptr(ptr: *const SSL) -> Option<&'a mut Self> {
721
+ get_ex_data_from_ptr::<ExData>(ptr, *QUICHE_EX_DATA_INDEX)
722
+ }
723
+
724
+ #[cfg(feature = "boringssl-boring-crate")]
725
+ pub fn from_ssl_ref(ssl: &mut boring::ssl::SslRef) -> Option<&mut Self> {
726
+ use boring::ex_data::Index;
727
+
728
+ // SAFETY: the QUICHE_EX_DATA_INDEX index is guaranteed to be created,
729
+ // and the associated data is always `ExData`.
730
+ let idx: Index<boring::ssl::Ssl, ExData> =
731
+ unsafe { Index::from_raw(*QUICHE_EX_DATA_INDEX) };
732
+
733
+ ssl.ex_data_mut(idx)
734
+ }
735
+ }
736
+
737
+ fn get_ex_data_from_ptr<'a, T>(ptr: *const SSL, idx: c_int) -> Option<&'a mut T> {
738
+ unsafe {
739
+ let data = SSL_get_ex_data(ptr, idx) as *mut T;
740
+ data.as_mut()
741
+ }
742
+ }
743
+
744
+ fn get_cipher_from_ptr(cipher: *const SSL_CIPHER) -> Result<crypto::Algorithm> {
745
+ let cipher_id = unsafe { SSL_CIPHER_get_id(cipher) };
746
+
747
+ let alg = match cipher_id {
748
+ 0x0300_1301 => crypto::Algorithm::AES128_GCM,
749
+ 0x0300_1302 => crypto::Algorithm::AES256_GCM,
750
+ 0x0300_1303 => crypto::Algorithm::ChaCha20_Poly1305,
751
+ _ => return Err(Error::TlsFail),
752
+ };
753
+
754
+ Ok(alg)
755
+ }
756
+
757
+ extern "C" fn set_read_secret(
758
+ ssl: *mut SSL, level: crypto::Level, cipher: *const SSL_CIPHER,
759
+ secret: *const u8, secret_len: usize,
760
+ ) -> c_int {
761
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
762
+ Some(v) => v,
763
+
764
+ None => return 0,
765
+ };
766
+
767
+ trace!("{} set read secret lvl={:?}", ex_data.trace_id, level);
768
+
769
+ let space = match level {
770
+ crypto::Level::Initial => &mut ex_data.crypto_ctx[packet::Epoch::Initial],
771
+ crypto::Level::ZeroRTT =>
772
+ &mut ex_data.crypto_ctx[packet::Epoch::Application],
773
+ crypto::Level::Handshake =>
774
+ &mut ex_data.crypto_ctx[packet::Epoch::Handshake],
775
+ crypto::Level::OneRTT =>
776
+ &mut ex_data.crypto_ctx[packet::Epoch::Application],
777
+ };
778
+
779
+ let aead = match get_cipher_from_ptr(cipher) {
780
+ Ok(v) => v,
781
+
782
+ Err(_) => return 0,
783
+ };
784
+
785
+ // 0-RTT read secrets are present only on the server.
786
+ if level != crypto::Level::ZeroRTT || ex_data.is_server {
787
+ let secret = unsafe { slice::from_raw_parts(secret, secret_len) };
788
+
789
+ let open = match crypto::Open::from_secret(aead, secret) {
790
+ Ok(v) => v,
791
+
792
+ Err(_) => return 0,
793
+ };
794
+
795
+ if level == crypto::Level::ZeroRTT {
796
+ space.crypto_0rtt_open = Some(open);
797
+ return 1;
798
+ }
799
+
800
+ space.crypto_open = Some(open);
801
+ }
802
+
803
+ 1
804
+ }
805
+
806
+ extern "C" fn set_write_secret(
807
+ ssl: *mut SSL, level: crypto::Level, cipher: *const SSL_CIPHER,
808
+ secret: *const u8, secret_len: usize,
809
+ ) -> c_int {
810
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
811
+ Some(v) => v,
812
+
813
+ None => return 0,
814
+ };
815
+
816
+ trace!("{} set write secret lvl={:?}", ex_data.trace_id, level);
817
+
818
+ let space = match level {
819
+ crypto::Level::Initial => &mut ex_data.crypto_ctx[packet::Epoch::Initial],
820
+ crypto::Level::ZeroRTT =>
821
+ &mut ex_data.crypto_ctx[packet::Epoch::Application],
822
+ crypto::Level::Handshake =>
823
+ &mut ex_data.crypto_ctx[packet::Epoch::Handshake],
824
+ crypto::Level::OneRTT =>
825
+ &mut ex_data.crypto_ctx[packet::Epoch::Application],
826
+ };
827
+
828
+ let aead = match get_cipher_from_ptr(cipher) {
829
+ Ok(v) => v,
830
+
831
+ Err(_) => return 0,
832
+ };
833
+
834
+ // 0-RTT write secrets are present only on the client.
835
+ if level != crypto::Level::ZeroRTT || !ex_data.is_server {
836
+ let secret = unsafe { slice::from_raw_parts(secret, secret_len) };
837
+
838
+ let seal = match crypto::Seal::from_secret(aead, secret) {
839
+ Ok(v) => v,
840
+
841
+ Err(_) => return 0,
842
+ };
843
+
844
+ space.crypto_seal = Some(seal);
845
+ }
846
+
847
+ 1
848
+ }
849
+
850
+ extern "C" fn add_handshake_data(
851
+ ssl: *mut SSL, level: crypto::Level, data: *const u8, len: usize,
852
+ ) -> c_int {
853
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
854
+ Some(v) => v,
855
+
856
+ None => return 0,
857
+ };
858
+
859
+ trace!(
860
+ "{} write message lvl={:?} len={}",
861
+ ex_data.trace_id,
862
+ level,
863
+ len
864
+ );
865
+
866
+ let buf = unsafe { slice::from_raw_parts(data, len) };
867
+
868
+ let space = match level {
869
+ crypto::Level::Initial => &mut ex_data.crypto_ctx[packet::Epoch::Initial],
870
+ crypto::Level::ZeroRTT => unreachable!(),
871
+ crypto::Level::Handshake =>
872
+ &mut ex_data.crypto_ctx[packet::Epoch::Handshake],
873
+ crypto::Level::OneRTT =>
874
+ &mut ex_data.crypto_ctx[packet::Epoch::Application],
875
+ };
876
+
877
+ if space.crypto_stream.send.write(buf, false).is_err() {
878
+ return 0;
879
+ }
880
+
881
+ 1
882
+ }
883
+
884
+ extern "C" fn flush_flight(_ssl: *mut SSL) -> c_int {
885
+ // We don't really need to anything here since the output packets are
886
+ // generated separately, when conn.send() is called.
887
+
888
+ 1
889
+ }
890
+
891
+ extern "C" fn send_alert(
892
+ ssl: *mut SSL, level: crypto::Level, alert: u8,
893
+ ) -> c_int {
894
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
895
+ Some(v) => v,
896
+
897
+ None => return 0,
898
+ };
899
+
900
+ trace!(
901
+ "{} send alert lvl={:?} alert={:x}",
902
+ ex_data.trace_id,
903
+ level,
904
+ alert
905
+ );
906
+
907
+ let error: u64 = TLS_ALERT_ERROR + u64::from(alert);
908
+ *ex_data.local_error = Some(ConnectionError {
909
+ is_app: false,
910
+ error_code: error,
911
+ reason: Vec::new(),
912
+ });
913
+
914
+ 1
915
+ }
916
+
917
+ extern "C" fn keylog(ssl: *const SSL, line: *const c_char) {
918
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
919
+ Some(v) => v,
920
+
921
+ None => return,
922
+ };
923
+
924
+ if let Some(keylog) = &mut ex_data.keylog {
925
+ let data = unsafe { ffi::CStr::from_ptr(line).to_bytes() };
926
+
927
+ let mut full_line = Vec::with_capacity(data.len() + 1);
928
+ full_line.extend_from_slice(data);
929
+ full_line.push(b'\n');
930
+
931
+ keylog.write_all(&full_line[..]).ok();
932
+ keylog.flush().ok();
933
+ }
934
+ }
935
+
936
+ extern "C" fn select_alpn(
937
+ ssl: *mut SSL, out: *mut *const u8, out_len: *mut u8, inp: *mut u8,
938
+ in_len: c_uint, _arg: *mut c_void,
939
+ ) -> c_int {
940
+ // SSL_TLSEXT_ERR_OK 0
941
+ // SSL_TLSEXT_ERR_ALERT_WARNING 1
942
+ // SSL_TLSEXT_ERR_ALERT_FATAL 2
943
+ // SSL_TLSEXT_ERR_NOACK 3
944
+
945
+ // Boringssl internally overwrite the return value from this callback, if
946
+ // the returned value is SSL_TLSEXT_ERR_NOACK and is quic, then the value
947
+ // gets overwritten to SSL_TLSEXT_ERR_ALERT_FATAL.
948
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
949
+ Some(v) => v,
950
+
951
+ None => return TLS_ERROR,
952
+ };
953
+
954
+ if ex_data.application_protos.is_empty() {
955
+ return TLS_ERROR;
956
+ }
957
+
958
+ let mut protos = octets::Octets::with_slice(unsafe {
959
+ slice::from_raw_parts(inp, in_len as usize)
960
+ });
961
+
962
+ while let Ok(proto) = protos.get_bytes_with_u8_length() {
963
+ let found = ex_data.application_protos.iter().any(|expected| {
964
+ trace!(
965
+ "checking peer ALPN {:?} against {:?}",
966
+ std::str::from_utf8(proto.as_ref()),
967
+ std::str::from_utf8(expected.as_slice())
968
+ );
969
+
970
+ if expected.len() == proto.len() &&
971
+ expected.as_slice() == proto.as_ref()
972
+ {
973
+ unsafe {
974
+ *out = expected.as_slice().as_ptr();
975
+ *out_len = expected.len() as u8;
976
+ }
977
+
978
+ return true;
979
+ }
980
+
981
+ false
982
+ });
983
+
984
+ if found {
985
+ return 0; // SSL_TLSEXT_ERR_OK
986
+ }
987
+ }
988
+
989
+ TLS_ERROR
990
+ }
991
+
992
+ extern "C" fn new_session(ssl: *mut SSL, session: *mut SSL_SESSION) -> c_int {
993
+ let ex_data = match ExData::from_ssl_ptr(ssl) {
994
+ Some(v) => v,
995
+
996
+ None => return 0,
997
+ };
998
+
999
+ // This callback receives a borrowed `SSL*`, so the temporary `Handshake`
1000
+ // must not free it on any return path.
1001
+ let handshake = ManuallyDrop::new(Handshake::new(ssl));
1002
+ let peer_params = handshake.quic_transport_params();
1003
+
1004
+ // Serialize session object into buffer.
1005
+ let session_bytes = match get_session_bytes(session) {
1006
+ Ok(v) => v,
1007
+ Err(_) => return 0,
1008
+ };
1009
+
1010
+ let mut buffer =
1011
+ Vec::with_capacity(8 + peer_params.len() + 8 + session_bytes.len());
1012
+
1013
+ let session_bytes_len = session_bytes.len() as u64;
1014
+
1015
+ if buffer.write(&session_bytes_len.to_be_bytes()).is_err() {
1016
+ return 0;
1017
+ }
1018
+
1019
+ if buffer.write(&session_bytes).is_err() {
1020
+ return 0;
1021
+ }
1022
+
1023
+ let peer_params_len = peer_params.len() as u64;
1024
+
1025
+ if buffer.write(&peer_params_len.to_be_bytes()).is_err() {
1026
+ return 0;
1027
+ }
1028
+
1029
+ if buffer.write(peer_params).is_err() {
1030
+ return 0;
1031
+ }
1032
+
1033
+ *ex_data.session = Some(buffer);
1034
+
1035
+ 0
1036
+ }
1037
+
1038
+ pub fn map_result(bssl_result: c_int) -> Result<()> {
1039
+ match bssl_result {
1040
+ 1 => Ok(()),
1041
+ _ => Err(Error::TlsFail),
1042
+ }
1043
+ }
1044
+
1045
+ pub fn map_result_zero_is_success(bssl_result: c_int) -> Result<()> {
1046
+ match bssl_result {
1047
+ 0 => Ok(()),
1048
+ _ => Err(Error::TlsFail),
1049
+ }
1050
+ }
1051
+
1052
+ pub fn map_result_ptr<'a, T>(bssl_result: *const T) -> Result<&'a T> {
1053
+ match unsafe { bssl_result.as_ref() } {
1054
+ Some(v) => Ok(v),
1055
+ None => Err(Error::TlsFail),
1056
+ }
1057
+ }
1058
+
1059
+ fn log_ssl_error() {
1060
+ let mut err = [0u8; 1024];
1061
+
1062
+ unsafe {
1063
+ let e = ERR_peek_error();
1064
+ ERR_error_string_n(e, err.as_mut_ptr() as *mut c_char, err.len());
1065
+ }
1066
+
1067
+ let cstr = ffi::CStr::from_bytes_until_nul(&err)
1068
+ .expect("ERR_error_string_n should write a null terminated string");
1069
+
1070
+ trace!(
1071
+ "{}",
1072
+ cstr.to_str()
1073
+ .expect("ERR_error_string_n should create a valid UTF-8 message")
1074
+ );
1075
+ }
1076
+
1077
+ extern "C" {
1078
+ // Note: some vendor-specific methods are implemented in the boringssl
1079
+ // submodule.
1080
+
1081
+ // SSL_METHOD
1082
+ fn TLS_method() -> *const SSL_METHOD;
1083
+
1084
+ // SSL_CTX
1085
+ fn SSL_CTX_new(method: *const SSL_METHOD) -> *mut SSL_CTX;
1086
+ fn SSL_CTX_free(ctx: *mut SSL_CTX);
1087
+
1088
+ fn SSL_CTX_use_certificate_chain_file(
1089
+ ctx: *mut SSL_CTX, file: *const c_char,
1090
+ ) -> c_int;
1091
+
1092
+ fn SSL_CTX_use_PrivateKey_file(
1093
+ ctx: *mut SSL_CTX, file: *const c_char, ty: c_int,
1094
+ ) -> c_int;
1095
+
1096
+ fn SSL_CTX_load_verify_locations(
1097
+ ctx: *mut SSL_CTX, file: *const c_char, path: *const c_char,
1098
+ ) -> c_int;
1099
+
1100
+ #[cfg(not(windows))]
1101
+ fn SSL_CTX_set_default_verify_paths(ctx: *mut SSL_CTX) -> c_int;
1102
+
1103
+ #[cfg(windows)]
1104
+ fn SSL_CTX_get_cert_store(ctx: *mut SSL_CTX) -> *mut X509_STORE;
1105
+
1106
+ fn SSL_CTX_set_verify(
1107
+ ctx: *mut SSL_CTX, mode: c_int,
1108
+ cb: Option<
1109
+ unsafe extern "C" fn(
1110
+ ok: c_int,
1111
+ store_ctx: *mut X509_STORE_CTX,
1112
+ ) -> c_int,
1113
+ >,
1114
+ );
1115
+
1116
+ fn SSL_CTX_set_keylog_callback(
1117
+ ctx: *mut SSL_CTX,
1118
+ cb: Option<unsafe extern "C" fn(ssl: *const SSL, line: *const c_char)>,
1119
+ );
1120
+
1121
+ fn SSL_CTX_set_alpn_protos(
1122
+ ctx: *mut SSL_CTX, protos: *const u8, protos_len: usize,
1123
+ ) -> c_int;
1124
+
1125
+ fn SSL_CTX_set_alpn_select_cb(
1126
+ ctx: *mut SSL_CTX,
1127
+ cb: Option<
1128
+ unsafe extern "C" fn(
1129
+ ssl: *mut SSL,
1130
+ out: *mut *const u8,
1131
+ out_len: *mut u8,
1132
+ inp: *mut u8,
1133
+ in_len: c_uint,
1134
+ arg: *mut c_void,
1135
+ ) -> c_int,
1136
+ >,
1137
+ arg: *mut c_void,
1138
+ );
1139
+
1140
+ fn SSL_CTX_sess_set_new_cb(
1141
+ ctx: *mut SSL_CTX,
1142
+ cb: Option<
1143
+ unsafe extern "C" fn(
1144
+ ssl: *mut SSL,
1145
+ session: *mut SSL_SESSION,
1146
+ ) -> c_int,
1147
+ >,
1148
+ );
1149
+
1150
+ fn SSL_new(ctx: *mut SSL_CTX) -> *mut SSL;
1151
+
1152
+ fn SSL_get_error(ssl: *const SSL, ret_code: c_int) -> c_int;
1153
+
1154
+ fn SSL_set_accept_state(ssl: *mut SSL);
1155
+ fn SSL_set_connect_state(ssl: *mut SSL);
1156
+
1157
+ fn SSL_get0_param(ssl: *mut SSL) -> *mut X509_VERIFY_PARAM;
1158
+
1159
+ fn SSL_set_ex_data(ssl: *mut SSL, idx: c_int, ptr: *mut c_void) -> c_int;
1160
+ fn SSL_get_ex_data(ssl: *const SSL, idx: c_int) -> *mut c_void;
1161
+
1162
+ fn SSL_get_current_cipher(ssl: *const SSL) -> *const SSL_CIPHER;
1163
+
1164
+ fn SSL_set_session(ssl: *mut SSL, session: *mut SSL_SESSION) -> c_int;
1165
+
1166
+ fn SSL_get_SSL_CTX(ssl: *const SSL) -> *mut SSL_CTX;
1167
+
1168
+ fn SSL_set_quiet_shutdown(ssl: *mut SSL, mode: c_int);
1169
+
1170
+ fn SSL_set_quic_transport_params(
1171
+ ssl: *mut SSL, params: *const u8, params_len: usize,
1172
+ ) -> c_int;
1173
+
1174
+ fn SSL_set_quic_method(
1175
+ ssl: *mut SSL, quic_method: *const SSL_QUIC_METHOD,
1176
+ ) -> c_int;
1177
+
1178
+ fn SSL_set_quic_use_legacy_codepoint(ssl: *mut SSL, use_legacy: c_int);
1179
+
1180
+ #[cfg(test)]
1181
+ fn SSL_set_options(ssl: *mut SSL, opts: u32) -> u32;
1182
+
1183
+ fn SSL_get_peer_quic_transport_params(
1184
+ ssl: *const SSL, out_params: *mut *const u8, out_params_len: *mut usize,
1185
+ );
1186
+
1187
+ fn SSL_get0_alpn_selected(
1188
+ ssl: *const SSL, out: *mut *const u8, out_len: *mut u32,
1189
+ );
1190
+
1191
+ fn SSL_get_servername(ssl: *const SSL, ty: c_int) -> *const c_char;
1192
+
1193
+ fn SSL_provide_quic_data(
1194
+ ssl: *mut SSL, level: crypto::Level, data: *const u8, len: usize,
1195
+ ) -> c_int;
1196
+
1197
+ fn SSL_process_quic_post_handshake(ssl: *mut SSL) -> c_int;
1198
+
1199
+ fn SSL_do_handshake(ssl: *mut SSL) -> c_int;
1200
+
1201
+ fn SSL_quic_write_level(ssl: *const SSL) -> crypto::Level;
1202
+
1203
+ fn SSL_session_reused(ssl: *const SSL) -> c_int;
1204
+
1205
+ fn SSL_in_init(ssl: *const SSL) -> c_int;
1206
+
1207
+ fn SSL_clear(ssl: *mut SSL) -> c_int;
1208
+
1209
+ fn SSL_free(ssl: *mut SSL);
1210
+
1211
+ // SSL_CIPHER
1212
+ fn SSL_CIPHER_get_id(cipher: *const SSL_CIPHER) -> c_uint;
1213
+
1214
+ // SSL_SESSION
1215
+
1216
+ fn SSL_SESSION_free(session: *mut SSL_SESSION);
1217
+
1218
+ // X509_VERIFY_PARAM
1219
+ fn X509_VERIFY_PARAM_set1_host(
1220
+ param: *mut X509_VERIFY_PARAM, name: *const c_char, namelen: usize,
1221
+ ) -> c_int;
1222
+
1223
+ // X509_STORE
1224
+ #[cfg(windows)]
1225
+ fn X509_STORE_add_cert(ctx: *mut X509_STORE, x: *mut X509) -> c_int;
1226
+
1227
+ // X509
1228
+ #[cfg(windows)]
1229
+ fn X509_free(x: *mut X509);
1230
+ #[cfg(windows)]
1231
+ fn d2i_X509(px: *mut X509, input: *const *const u8, len: c_int) -> *mut X509;
1232
+
1233
+ // ERR
1234
+ fn ERR_peek_error() -> c_uint;
1235
+
1236
+ fn ERR_error_string_n(err: c_uint, buf: *mut c_char, len: usize);
1237
+
1238
+ // OPENSSL
1239
+ #[allow(dead_code)]
1240
+ fn OPENSSL_free(ptr: *mut c_void);
1241
+
1242
+ }
1243
+
1244
+ mod boringssl;
1245
+ use boringssl::*;