rwebtransport 0.1.0 → 0.1.2

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 (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,375 +1,375 @@
1
- use super::*;
2
-
3
- use libc::c_long;
4
-
5
- #[allow(non_camel_case_types)]
6
- #[repr(transparent)]
7
- struct CRYPTO_BUFFER {
8
- _unused: c_void,
9
- }
10
-
11
- #[repr(C)]
12
- #[allow(non_camel_case_types)]
13
- pub(super) struct SSL_QUIC_METHOD {
14
- set_read_secret: Option<
15
- unsafe extern "C" fn(
16
- ssl: *mut SSL,
17
- level: crypto::Level,
18
- cipher: *const SSL_CIPHER,
19
- secret: *const u8,
20
- secret_len: usize,
21
- ) -> c_int,
22
- >,
23
-
24
- set_write_secret: Option<
25
- unsafe extern "C" fn(
26
- ssl: *mut SSL,
27
- level: crypto::Level,
28
- cipher: *const SSL_CIPHER,
29
- secret: *const u8,
30
- secret_len: usize,
31
- ) -> c_int,
32
- >,
33
-
34
- add_handshake_data: Option<
35
- unsafe extern "C" fn(
36
- ssl: *mut SSL,
37
- level: crypto::Level,
38
- data: *const u8,
39
- len: usize,
40
- ) -> c_int,
41
- >,
42
-
43
- flush_flight: Option<extern "C" fn(ssl: *mut SSL) -> c_int>,
44
-
45
- send_alert: Option<
46
- extern "C" fn(ssl: *mut SSL, level: crypto::Level, alert: u8) -> c_int,
47
- >,
48
- }
49
-
50
- #[cfg(test)]
51
- #[repr(C)]
52
- #[allow(non_camel_case_types)]
53
- struct SSL_PRIVATE_KEY_METHOD {
54
- sign: Option<
55
- unsafe extern "C" fn(
56
- ssl: *mut SSL,
57
- out: *mut u8,
58
- out_len: *mut usize,
59
- max_out: usize,
60
- signature_algorithm: u16,
61
- r#in: *const u8,
62
- in_len: usize,
63
- ) -> ssl_private_key_result_t,
64
- >,
65
-
66
- decrypt: Option<
67
- unsafe extern "C" fn(
68
- ssl: *mut SSL,
69
- out: *mut u8,
70
- out_len: *mut usize,
71
- max_out: usize,
72
- r#in: *const u8,
73
- in_len: usize,
74
- ) -> ssl_private_key_result_t,
75
- >,
76
-
77
- complete: Option<
78
- unsafe extern "C" fn(
79
- ssl: *mut SSL,
80
- out: *mut u8,
81
- out_len: *mut usize,
82
- max_out: usize,
83
- ) -> ssl_private_key_result_t,
84
- >,
85
- }
86
-
87
- pub(super) static QUICHE_STREAM_METHOD: SSL_QUIC_METHOD = SSL_QUIC_METHOD {
88
- set_read_secret: Some(set_read_secret),
89
- set_write_secret: Some(set_write_secret),
90
- add_handshake_data: Some(add_handshake_data),
91
- flush_flight: Some(flush_flight),
92
- send_alert: Some(send_alert),
93
- };
94
-
95
- impl Context {
96
- pub fn set_early_data_enabled(&mut self, _enabled: bool) {
97
- unsafe {
98
- SSL_CTX_set_early_data_enabled(
99
- self.as_mut_ptr(),
100
- i32::from(_enabled),
101
- );
102
- }
103
- }
104
- }
105
-
106
- impl Handshake {
107
- pub fn set_quic_early_data_context(&mut self, context: &[u8]) -> Result<()> {
108
- map_result(unsafe {
109
- SSL_set_quic_early_data_context(
110
- self.as_mut_ptr(),
111
- context.as_ptr(),
112
- context.len(),
113
- )
114
- })
115
- }
116
-
117
- pub fn set_session(&mut self, session: &[u8]) -> Result<()> {
118
- unsafe {
119
- let ctx = SSL_get_SSL_CTX(self.as_ptr());
120
-
121
- if ctx.is_null() {
122
- return Err(Error::TlsFail);
123
- }
124
-
125
- let session =
126
- SSL_SESSION_from_bytes(session.as_ptr(), session.len(), ctx);
127
-
128
- if session.is_null() {
129
- return Err(Error::TlsFail);
130
- }
131
-
132
- let rc = SSL_set_session(self.as_mut_ptr(), session);
133
- SSL_SESSION_free(session);
134
-
135
- map_result(rc)
136
- }
137
- }
138
-
139
- pub fn reset_early_data_reject(&mut self) {
140
- unsafe { SSL_reset_early_data_reject(self.as_mut_ptr()) };
141
- }
142
-
143
- pub fn curve(&self) -> Option<String> {
144
- let curve = unsafe {
145
- let curve_id = SSL_get_curve_id(self.as_ptr());
146
- if curve_id == 0 {
147
- return None;
148
- }
149
-
150
- let curve_name = SSL_get_curve_name(curve_id);
151
- match ffi::CStr::from_ptr(curve_name).to_str() {
152
- Ok(v) => v,
153
-
154
- Err(_) => return None,
155
- }
156
- };
157
-
158
- Some(curve.to_string())
159
- }
160
-
161
- pub fn sigalg(&self) -> Option<String> {
162
- let sigalg = unsafe {
163
- let sigalg_id = SSL_get_peer_signature_algorithm(self.as_ptr());
164
- if sigalg_id == 0 {
165
- return None;
166
- }
167
-
168
- let sigalg_name = SSL_get_signature_algorithm_name(sigalg_id, 1);
169
- match ffi::CStr::from_ptr(sigalg_name).to_str() {
170
- Ok(v) => v,
171
-
172
- Err(_) => return None,
173
- }
174
- };
175
-
176
- Some(sigalg.to_string())
177
- }
178
-
179
- pub fn peer_cert_chain(&self) -> Option<Vec<&[u8]>> {
180
- let cert_chain = unsafe {
181
- let chain =
182
- map_result_ptr(SSL_get0_peer_certificates(self.as_ptr())).ok()?;
183
-
184
- let num = sk_num(chain);
185
- if num == 0 {
186
- return None;
187
- }
188
-
189
- let mut cert_chain = vec![];
190
- for i in 0..num {
191
- let buffer =
192
- map_result_ptr(sk_value(chain, i) as *const CRYPTO_BUFFER)
193
- .ok()?;
194
-
195
- let out_len = CRYPTO_BUFFER_len(buffer);
196
- if out_len == 0 {
197
- return None;
198
- }
199
-
200
- let out = CRYPTO_BUFFER_data(buffer);
201
- let slice = slice::from_raw_parts(out, out_len);
202
-
203
- cert_chain.push(slice);
204
- }
205
-
206
- cert_chain
207
- };
208
-
209
- Some(cert_chain)
210
- }
211
-
212
- pub fn peer_cert(&self) -> Option<&[u8]> {
213
- let peer_cert = unsafe {
214
- let chain =
215
- map_result_ptr(SSL_get0_peer_certificates(self.as_ptr())).ok()?;
216
- if sk_num(chain) == 0 {
217
- return None;
218
- }
219
-
220
- let buffer =
221
- map_result_ptr(sk_value(chain, 0) as *const CRYPTO_BUFFER)
222
- .ok()?;
223
-
224
- let out_len = CRYPTO_BUFFER_len(buffer);
225
- if out_len == 0 {
226
- return None;
227
- }
228
-
229
- let out = CRYPTO_BUFFER_data(buffer);
230
- slice::from_raw_parts(out, out_len)
231
- };
232
-
233
- Some(peer_cert)
234
- }
235
-
236
- // Only used for testing handling of failure during key signing.
237
- #[cfg(test)]
238
- pub fn set_failing_private_key_method(&mut self) {
239
- extern "C" fn failing_sign(
240
- _ssl: *mut SSL, _out: *mut u8, _out_len: *mut usize, _max_out: usize,
241
- _signature_algorithm: u16, _in: *const u8, _in_len: usize,
242
- ) -> ssl_private_key_result_t {
243
- ssl_private_key_result_t::ssl_private_key_failure
244
- }
245
-
246
- extern "C" fn failing_decrypt(
247
- _ssl: *mut SSL, _out: *mut u8, _out_len: *mut usize, _max_out: usize,
248
- _in: *const u8, _in_len: usize,
249
- ) -> ssl_private_key_result_t {
250
- ssl_private_key_result_t::ssl_private_key_failure
251
- }
252
-
253
- extern "C" fn failing_complete(
254
- _ssl: *mut SSL, _out: *mut u8, _out_len: *mut usize, _max_out: usize,
255
- ) -> ssl_private_key_result_t {
256
- ssl_private_key_result_t::ssl_private_key_failure
257
- }
258
-
259
- static QUICHE_PRIVATE_KEY_METHOD: SSL_PRIVATE_KEY_METHOD =
260
- SSL_PRIVATE_KEY_METHOD {
261
- decrypt: Some(failing_decrypt),
262
- sign: Some(failing_sign),
263
- complete: Some(failing_complete),
264
- };
265
-
266
- unsafe {
267
- SSL_set_private_key_method(
268
- self.as_mut_ptr(),
269
- &QUICHE_PRIVATE_KEY_METHOD,
270
- );
271
- }
272
- }
273
-
274
- pub fn is_in_early_data(&self) -> bool {
275
- unsafe { SSL_in_early_data(self.as_ptr()) == 1 }
276
- }
277
-
278
- pub fn early_data_reason(&self) -> u32 {
279
- let reuse_reason_status =
280
- unsafe { SSL_get_early_data_reason(self.as_ptr()) };
281
- reuse_reason_status.0
282
- }
283
- }
284
-
285
- pub(super) fn get_session_bytes(session: *mut SSL_SESSION) -> Result<Vec<u8>> {
286
- let session_bytes = unsafe {
287
- let mut out: *mut u8 = ptr::null_mut();
288
- let mut out_len: usize = 0;
289
-
290
- if SSL_SESSION_to_bytes(session, &mut out, &mut out_len) == 0 {
291
- return Err(Error::TlsFail);
292
- }
293
- let session_bytes = slice::from_raw_parts(out, out_len).to_vec();
294
- OPENSSL_free(out as *mut c_void);
295
- session_bytes
296
- };
297
-
298
- Ok(session_bytes)
299
- }
300
- pub(super) const TLS_ERROR: c_int = 3;
301
-
302
- #[allow(non_camel_case_types)]
303
- #[repr(transparent)]
304
- #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
305
- pub struct ssl_early_data_reason_t(pub ::std::os::raw::c_uint);
306
- extern "C" {
307
- // SSL_METHOD specific for boringssl.
308
- pub(super) fn SSL_CTX_set_tlsext_ticket_keys(
309
- ctx: *mut SSL_CTX, key: *const u8, key_len: usize,
310
- ) -> c_int;
311
- fn SSL_CTX_set_early_data_enabled(ctx: *mut SSL_CTX, enabled: i32);
312
-
313
- pub(super) fn SSL_CTX_set_session_cache_mode(
314
- ctx: *mut SSL_CTX, mode: c_int,
315
- ) -> c_int;
316
- pub(super) fn SSL_get_ex_new_index(
317
- argl: c_long, argp: *const c_void, unused: *const c_void,
318
- dup_unused: *const c_void, free_func: *const c_void,
319
- ) -> c_int;
320
-
321
- fn SSL_get_curve_id(ssl: *const SSL) -> u16;
322
- fn SSL_get_curve_name(curve: u16) -> *const c_char;
323
-
324
- fn SSL_get_peer_signature_algorithm(ssl: *const SSL) -> u16;
325
- fn SSL_get_signature_algorithm_name(
326
- sigalg: u16, include_curve: i32,
327
- ) -> *const c_char;
328
-
329
- fn SSL_get0_peer_certificates(ssl: *const SSL) -> *const STACK_OF;
330
-
331
- pub(super) fn SSL_set_min_proto_version(ssl: *mut SSL, version: u16)
332
- -> c_int;
333
-
334
- pub(super) fn SSL_set_max_proto_version(ssl: *mut SSL, version: u16)
335
- -> c_int;
336
-
337
- pub(super) fn SSL_set_tlsext_host_name(
338
- ssl: *mut SSL, name: *const c_char,
339
- ) -> c_int;
340
-
341
- fn SSL_set_quic_early_data_context(
342
- ssl: *mut SSL, context: *const u8, context_len: usize,
343
- ) -> c_int;
344
-
345
- #[cfg(test)]
346
- fn SSL_set_private_key_method(
347
- ssl: *mut SSL, key_method: *const SSL_PRIVATE_KEY_METHOD,
348
- );
349
-
350
- fn SSL_reset_early_data_reject(ssl: *mut SSL);
351
-
352
- fn SSL_in_early_data(ssl: *const SSL) -> c_int;
353
-
354
- fn SSL_get_early_data_reason(ssl: *const SSL) -> ssl_early_data_reason_t;
355
-
356
- fn SSL_SESSION_to_bytes(
357
- session: *const SSL_SESSION, out: *mut *mut u8, out_len: *mut usize,
358
- ) -> c_int;
359
-
360
- fn SSL_SESSION_from_bytes(
361
- input: *const u8, input_len: usize, ctx: *const SSL_CTX,
362
- ) -> *mut SSL_SESSION;
363
-
364
- // STACK_OF
365
-
366
- fn sk_num(stack: *const STACK_OF) -> usize;
367
-
368
- fn sk_value(stack: *const STACK_OF, idx: usize) -> *mut c_void;
369
-
370
- // CRYPTO_BUFFER
371
-
372
- fn CRYPTO_BUFFER_len(buffer: *const CRYPTO_BUFFER) -> usize;
373
-
374
- fn CRYPTO_BUFFER_data(buffer: *const CRYPTO_BUFFER) -> *const u8;
375
- }
1
+ use super::*;
2
+
3
+ use libc::c_long;
4
+
5
+ #[allow(non_camel_case_types)]
6
+ #[repr(transparent)]
7
+ struct CRYPTO_BUFFER {
8
+ _unused: c_void,
9
+ }
10
+
11
+ #[repr(C)]
12
+ #[allow(non_camel_case_types)]
13
+ pub(super) struct SSL_QUIC_METHOD {
14
+ set_read_secret: Option<
15
+ unsafe extern "C" fn(
16
+ ssl: *mut SSL,
17
+ level: crypto::Level,
18
+ cipher: *const SSL_CIPHER,
19
+ secret: *const u8,
20
+ secret_len: usize,
21
+ ) -> c_int,
22
+ >,
23
+
24
+ set_write_secret: Option<
25
+ unsafe extern "C" fn(
26
+ ssl: *mut SSL,
27
+ level: crypto::Level,
28
+ cipher: *const SSL_CIPHER,
29
+ secret: *const u8,
30
+ secret_len: usize,
31
+ ) -> c_int,
32
+ >,
33
+
34
+ add_handshake_data: Option<
35
+ unsafe extern "C" fn(
36
+ ssl: *mut SSL,
37
+ level: crypto::Level,
38
+ data: *const u8,
39
+ len: usize,
40
+ ) -> c_int,
41
+ >,
42
+
43
+ flush_flight: Option<extern "C" fn(ssl: *mut SSL) -> c_int>,
44
+
45
+ send_alert: Option<
46
+ extern "C" fn(ssl: *mut SSL, level: crypto::Level, alert: u8) -> c_int,
47
+ >,
48
+ }
49
+
50
+ #[cfg(test)]
51
+ #[repr(C)]
52
+ #[allow(non_camel_case_types)]
53
+ struct SSL_PRIVATE_KEY_METHOD {
54
+ sign: Option<
55
+ unsafe extern "C" fn(
56
+ ssl: *mut SSL,
57
+ out: *mut u8,
58
+ out_len: *mut usize,
59
+ max_out: usize,
60
+ signature_algorithm: u16,
61
+ r#in: *const u8,
62
+ in_len: usize,
63
+ ) -> ssl_private_key_result_t,
64
+ >,
65
+
66
+ decrypt: Option<
67
+ unsafe extern "C" fn(
68
+ ssl: *mut SSL,
69
+ out: *mut u8,
70
+ out_len: *mut usize,
71
+ max_out: usize,
72
+ r#in: *const u8,
73
+ in_len: usize,
74
+ ) -> ssl_private_key_result_t,
75
+ >,
76
+
77
+ complete: Option<
78
+ unsafe extern "C" fn(
79
+ ssl: *mut SSL,
80
+ out: *mut u8,
81
+ out_len: *mut usize,
82
+ max_out: usize,
83
+ ) -> ssl_private_key_result_t,
84
+ >,
85
+ }
86
+
87
+ pub(super) static QUICHE_STREAM_METHOD: SSL_QUIC_METHOD = SSL_QUIC_METHOD {
88
+ set_read_secret: Some(set_read_secret),
89
+ set_write_secret: Some(set_write_secret),
90
+ add_handshake_data: Some(add_handshake_data),
91
+ flush_flight: Some(flush_flight),
92
+ send_alert: Some(send_alert),
93
+ };
94
+
95
+ impl Context {
96
+ pub fn set_early_data_enabled(&mut self, _enabled: bool) {
97
+ unsafe {
98
+ SSL_CTX_set_early_data_enabled(
99
+ self.as_mut_ptr(),
100
+ i32::from(_enabled),
101
+ );
102
+ }
103
+ }
104
+ }
105
+
106
+ impl Handshake {
107
+ pub fn set_quic_early_data_context(&mut self, context: &[u8]) -> Result<()> {
108
+ map_result(unsafe {
109
+ SSL_set_quic_early_data_context(
110
+ self.as_mut_ptr(),
111
+ context.as_ptr(),
112
+ context.len(),
113
+ )
114
+ })
115
+ }
116
+
117
+ pub fn set_session(&mut self, session: &[u8]) -> Result<()> {
118
+ unsafe {
119
+ let ctx = SSL_get_SSL_CTX(self.as_ptr());
120
+
121
+ if ctx.is_null() {
122
+ return Err(Error::TlsFail);
123
+ }
124
+
125
+ let session =
126
+ SSL_SESSION_from_bytes(session.as_ptr(), session.len(), ctx);
127
+
128
+ if session.is_null() {
129
+ return Err(Error::TlsFail);
130
+ }
131
+
132
+ let rc = SSL_set_session(self.as_mut_ptr(), session);
133
+ SSL_SESSION_free(session);
134
+
135
+ map_result(rc)
136
+ }
137
+ }
138
+
139
+ pub fn reset_early_data_reject(&mut self) {
140
+ unsafe { SSL_reset_early_data_reject(self.as_mut_ptr()) };
141
+ }
142
+
143
+ pub fn curve(&self) -> Option<String> {
144
+ let curve = unsafe {
145
+ let curve_id = SSL_get_curve_id(self.as_ptr());
146
+ if curve_id == 0 {
147
+ return None;
148
+ }
149
+
150
+ let curve_name = SSL_get_curve_name(curve_id);
151
+ match ffi::CStr::from_ptr(curve_name).to_str() {
152
+ Ok(v) => v,
153
+
154
+ Err(_) => return None,
155
+ }
156
+ };
157
+
158
+ Some(curve.to_string())
159
+ }
160
+
161
+ pub fn sigalg(&self) -> Option<String> {
162
+ let sigalg = unsafe {
163
+ let sigalg_id = SSL_get_peer_signature_algorithm(self.as_ptr());
164
+ if sigalg_id == 0 {
165
+ return None;
166
+ }
167
+
168
+ let sigalg_name = SSL_get_signature_algorithm_name(sigalg_id, 1);
169
+ match ffi::CStr::from_ptr(sigalg_name).to_str() {
170
+ Ok(v) => v,
171
+
172
+ Err(_) => return None,
173
+ }
174
+ };
175
+
176
+ Some(sigalg.to_string())
177
+ }
178
+
179
+ pub fn peer_cert_chain(&self) -> Option<Vec<&[u8]>> {
180
+ let cert_chain = unsafe {
181
+ let chain =
182
+ map_result_ptr(SSL_get0_peer_certificates(self.as_ptr())).ok()?;
183
+
184
+ let num = sk_num(chain);
185
+ if num == 0 {
186
+ return None;
187
+ }
188
+
189
+ let mut cert_chain = vec![];
190
+ for i in 0..num {
191
+ let buffer =
192
+ map_result_ptr(sk_value(chain, i) as *const CRYPTO_BUFFER)
193
+ .ok()?;
194
+
195
+ let out_len = CRYPTO_BUFFER_len(buffer);
196
+ if out_len == 0 {
197
+ return None;
198
+ }
199
+
200
+ let out = CRYPTO_BUFFER_data(buffer);
201
+ let slice = slice::from_raw_parts(out, out_len);
202
+
203
+ cert_chain.push(slice);
204
+ }
205
+
206
+ cert_chain
207
+ };
208
+
209
+ Some(cert_chain)
210
+ }
211
+
212
+ pub fn peer_cert(&self) -> Option<&[u8]> {
213
+ let peer_cert = unsafe {
214
+ let chain =
215
+ map_result_ptr(SSL_get0_peer_certificates(self.as_ptr())).ok()?;
216
+ if sk_num(chain) == 0 {
217
+ return None;
218
+ }
219
+
220
+ let buffer =
221
+ map_result_ptr(sk_value(chain, 0) as *const CRYPTO_BUFFER)
222
+ .ok()?;
223
+
224
+ let out_len = CRYPTO_BUFFER_len(buffer);
225
+ if out_len == 0 {
226
+ return None;
227
+ }
228
+
229
+ let out = CRYPTO_BUFFER_data(buffer);
230
+ slice::from_raw_parts(out, out_len)
231
+ };
232
+
233
+ Some(peer_cert)
234
+ }
235
+
236
+ // Only used for testing handling of failure during key signing.
237
+ #[cfg(test)]
238
+ pub fn set_failing_private_key_method(&mut self) {
239
+ extern "C" fn failing_sign(
240
+ _ssl: *mut SSL, _out: *mut u8, _out_len: *mut usize, _max_out: usize,
241
+ _signature_algorithm: u16, _in: *const u8, _in_len: usize,
242
+ ) -> ssl_private_key_result_t {
243
+ ssl_private_key_result_t::ssl_private_key_failure
244
+ }
245
+
246
+ extern "C" fn failing_decrypt(
247
+ _ssl: *mut SSL, _out: *mut u8, _out_len: *mut usize, _max_out: usize,
248
+ _in: *const u8, _in_len: usize,
249
+ ) -> ssl_private_key_result_t {
250
+ ssl_private_key_result_t::ssl_private_key_failure
251
+ }
252
+
253
+ extern "C" fn failing_complete(
254
+ _ssl: *mut SSL, _out: *mut u8, _out_len: *mut usize, _max_out: usize,
255
+ ) -> ssl_private_key_result_t {
256
+ ssl_private_key_result_t::ssl_private_key_failure
257
+ }
258
+
259
+ static QUICHE_PRIVATE_KEY_METHOD: SSL_PRIVATE_KEY_METHOD =
260
+ SSL_PRIVATE_KEY_METHOD {
261
+ decrypt: Some(failing_decrypt),
262
+ sign: Some(failing_sign),
263
+ complete: Some(failing_complete),
264
+ };
265
+
266
+ unsafe {
267
+ SSL_set_private_key_method(
268
+ self.as_mut_ptr(),
269
+ &QUICHE_PRIVATE_KEY_METHOD,
270
+ );
271
+ }
272
+ }
273
+
274
+ pub fn is_in_early_data(&self) -> bool {
275
+ unsafe { SSL_in_early_data(self.as_ptr()) == 1 }
276
+ }
277
+
278
+ pub fn early_data_reason(&self) -> u32 {
279
+ let reuse_reason_status =
280
+ unsafe { SSL_get_early_data_reason(self.as_ptr()) };
281
+ reuse_reason_status.0
282
+ }
283
+ }
284
+
285
+ pub(super) fn get_session_bytes(session: *mut SSL_SESSION) -> Result<Vec<u8>> {
286
+ let session_bytes = unsafe {
287
+ let mut out: *mut u8 = ptr::null_mut();
288
+ let mut out_len: usize = 0;
289
+
290
+ if SSL_SESSION_to_bytes(session, &mut out, &mut out_len) == 0 {
291
+ return Err(Error::TlsFail);
292
+ }
293
+ let session_bytes = slice::from_raw_parts(out, out_len).to_vec();
294
+ OPENSSL_free(out as *mut c_void);
295
+ session_bytes
296
+ };
297
+
298
+ Ok(session_bytes)
299
+ }
300
+ pub(super) const TLS_ERROR: c_int = 3;
301
+
302
+ #[allow(non_camel_case_types)]
303
+ #[repr(transparent)]
304
+ #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
305
+ pub struct ssl_early_data_reason_t(pub ::std::os::raw::c_uint);
306
+ extern "C" {
307
+ // SSL_METHOD specific for boringssl.
308
+ pub(super) fn SSL_CTX_set_tlsext_ticket_keys(
309
+ ctx: *mut SSL_CTX, key: *const u8, key_len: usize,
310
+ ) -> c_int;
311
+ fn SSL_CTX_set_early_data_enabled(ctx: *mut SSL_CTX, enabled: i32);
312
+
313
+ pub(super) fn SSL_CTX_set_session_cache_mode(
314
+ ctx: *mut SSL_CTX, mode: c_int,
315
+ ) -> c_int;
316
+ pub(super) fn SSL_get_ex_new_index(
317
+ argl: c_long, argp: *const c_void, unused: *const c_void,
318
+ dup_unused: *const c_void, free_func: *const c_void,
319
+ ) -> c_int;
320
+
321
+ fn SSL_get_curve_id(ssl: *const SSL) -> u16;
322
+ fn SSL_get_curve_name(curve: u16) -> *const c_char;
323
+
324
+ fn SSL_get_peer_signature_algorithm(ssl: *const SSL) -> u16;
325
+ fn SSL_get_signature_algorithm_name(
326
+ sigalg: u16, include_curve: i32,
327
+ ) -> *const c_char;
328
+
329
+ fn SSL_get0_peer_certificates(ssl: *const SSL) -> *const STACK_OF;
330
+
331
+ pub(super) fn SSL_set_min_proto_version(ssl: *mut SSL, version: u16)
332
+ -> c_int;
333
+
334
+ pub(super) fn SSL_set_max_proto_version(ssl: *mut SSL, version: u16)
335
+ -> c_int;
336
+
337
+ pub(super) fn SSL_set_tlsext_host_name(
338
+ ssl: *mut SSL, name: *const c_char,
339
+ ) -> c_int;
340
+
341
+ fn SSL_set_quic_early_data_context(
342
+ ssl: *mut SSL, context: *const u8, context_len: usize,
343
+ ) -> c_int;
344
+
345
+ #[cfg(test)]
346
+ fn SSL_set_private_key_method(
347
+ ssl: *mut SSL, key_method: *const SSL_PRIVATE_KEY_METHOD,
348
+ );
349
+
350
+ fn SSL_reset_early_data_reject(ssl: *mut SSL);
351
+
352
+ fn SSL_in_early_data(ssl: *const SSL) -> c_int;
353
+
354
+ fn SSL_get_early_data_reason(ssl: *const SSL) -> ssl_early_data_reason_t;
355
+
356
+ fn SSL_SESSION_to_bytes(
357
+ session: *const SSL_SESSION, out: *mut *mut u8, out_len: *mut usize,
358
+ ) -> c_int;
359
+
360
+ fn SSL_SESSION_from_bytes(
361
+ input: *const u8, input_len: usize, ctx: *const SSL_CTX,
362
+ ) -> *mut SSL_SESSION;
363
+
364
+ // STACK_OF
365
+
366
+ fn sk_num(stack: *const STACK_OF) -> usize;
367
+
368
+ fn sk_value(stack: *const STACK_OF, idx: usize) -> *mut c_void;
369
+
370
+ // CRYPTO_BUFFER
371
+
372
+ fn CRYPTO_BUFFER_len(buffer: *const CRYPTO_BUFFER) -> usize;
373
+
374
+ fn CRYPTO_BUFFER_data(buffer: *const CRYPTO_BUFFER) -> *const u8;
375
+ }