Debian Codegraph @

crt/s2n/bindings/rust/extended/s2n-tls/src/testing/s2n_tls.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #[cfg(test)]
5 mod tests {
6 use crate::{
7 callbacks::{ClientHelloCallback, ConnectionFuture, ConnectionFutureResult},
8 enums::ClientAuthType,
9 error::ErrorType,
10 testing::{self, client_hello::*, Error, Result, *},
11 };
12 use alloc::sync::Arc;
13 use core::sync::atomic::Ordering;
14 use futures_test::task::{new_count_waker, noop_waker};
15 use security::Policy;
16 use std::{fs, path::Path, pin::Pin, sync::atomic::AtomicUsize};
17
18 #[test]
19 fn handshake_default() {
20 let config = build_config(&security::DEFAULT).unwrap();
21 assert!(TestPair::handshake_with_config(&config).is_ok());
22 }
23
24 #[test]
25 fn handshake_default_tls13() {
26 let config = build_config(&security::DEFAULT_TLS13).unwrap();
27 assert!(TestPair::handshake_with_config(&config).is_ok());
28 }
29
30 #[test]
31 fn kem_group_name_retrieval() -> Result<(), Error> {
32 // PQ isn't supported
33 {
34 let policy = Policy::from_version("20240501")?;
35 let config = build_config(&policy)?;
36 let mut pair = TestPair::from_config(&config);
37
38 // before negotiation, kem_group_name is none
39 assert!(pair.client.kem_group_name().is_none());
40
41 pair.handshake().unwrap();
42 assert!(pair.client.kem_group_name().is_none());
43 }
44
45 // PQ is supported
46 {
47 let policy = Policy::from_version("default_pq")?;
48 let config = build_config(&policy)?;
49 let mut pair = TestPair::from_config(&config);
50
51 pair.handshake().unwrap();
52 assert_eq!(pair.client.kem_group_name(), Some("X25519MLKEM768"));
53 assert_eq!(
54 pair.client.selected_key_exchange_group(),
55 Some("X25519MLKEM768")
56 );
57 }
58
59 Ok(())
60 }
61
62 #[test]
63 fn default_config_and_clone_interaction() -> Result<(), Error> {
64 let config = build_config(&security::DEFAULT_TLS13)?;
65 assert_eq!(config.test_get_refcount()?, 1);
66 {
67 // Create new connection.
68 let mut server = crate::connection::Connection::new_server();
69 // Can't retrieve default config.
70 assert!(server.config().is_none());
71 // Custom config reference count doesn't change.
72 assert_eq!(config.test_get_refcount()?, 1);
73
74 // Set custom config on connection.
75 server.set_config(config.clone())?;
76 // Can retrieve custom config.
77 assert!(server.config().is_some());
78 // Custom config now referenced once more.
79 assert_eq!(config.test_get_refcount()?, 2);
80
81 // Create new connection.
82 let mut client = crate::connection::Connection::new_client();
83 // Can't retrieve default config.
84 assert!(client.config().is_none());
85 // Custom config reference count doesn't change.
86 assert_eq!(config.test_get_refcount()?, 2);
87
88 // Set custom config on connection.
89 client.set_config(config.clone())?;
90 // Can retrieve custom config.
91 assert!(client.config().is_some());
92 // Custom config now referenced once more.
93 assert_eq!(config.test_get_refcount()?, 3);
94
95 // drop all the clones
96 }
97 assert_eq!(config.test_get_refcount()?, 1);
98 Ok(())
99 }
100
101 #[test]
102 fn set_config_multiple_times() -> Result<(), Error> {
103 let config = build_config(&security::DEFAULT_TLS13)?;
104 assert_eq!(config.test_get_refcount()?, 1);
105
106 let mut server = crate::connection::Connection::new_server();
107 assert_eq!(config.test_get_refcount()?, 1);
108
109 // call set_config once
110 server.set_config(config.clone())?;
111 assert_eq!(config.test_get_refcount()?, 2);
112 assert!(server.config().is_some());
113
114 // calling set_config multiple times works since we drop the previous config
115 server.set_config(config.clone())?;
116 assert_eq!(config.test_get_refcount()?, 2);
117 assert!(server.config().is_some());
118 Ok(())
119 }
120
121 #[test]
122 fn connection_waker() {
123 let config = build_config(&security::DEFAULT_TLS13).unwrap();
124 assert_eq!(config.test_get_refcount().unwrap(), 1);
125
126 let mut server = crate::connection::Connection::new_server();
127 server.set_config(config).unwrap();
128
129 assert!(server.waker().is_none());
130
131 let (waker, wake_count) = new_count_waker();
132 server.set_waker(Some(&waker)).unwrap();
133 assert!(server.waker().is_some());
134
135 server.set_waker(None).unwrap();
136 assert!(server.waker().is_none());
137
138 assert_eq!(wake_count, 0);
139 }
140
141 #[test]
142 fn failing_client_hello_callback_sync() -> Result<(), Error> {
143 let (waker, wake_count) = new_count_waker();
144 let config = {
145 let mut config = config_builder(&security::DEFAULT_TLS13)?;
146 config.set_client_hello_callback(FailingCHHandler)?;
147 config.build()?
148 };
149
150 let mut pair = TestPair::from_config(&config);
151 pair.server.set_waker(Some(&waker))?;
152 let s2n_err = pair.handshake().unwrap_err();
153 // the underlying error should be the custom error the application provided
154 let app_err = s2n_err.application_error().unwrap();
155 let io_err = app_err.downcast_ref::<std::io::Error>().unwrap();
156 let _custom_err = io_err
157 .get_ref()
158 .unwrap()
159 .downcast_ref::<CustomError>()
160 .unwrap();
161
162 assert_eq!(wake_count, 0);
163 Ok(())
164 }
165
166 #[test]
167 fn failing_client_hello_callback_async() -> Result<(), Error> {
168 let (waker, wake_count) = new_count_waker();
169 let config = {
170 let mut config = config_builder(&security::DEFAULT_TLS13)?;
171 config.set_client_hello_callback(FailingAsyncCHHandler)?;
172 config.build()?
173 };
174
175 let mut pair = TestPair::from_config(&config);
176 pair.server.set_waker(Some(&waker))?;
177 let s2n_err = pair.handshake().unwrap_err();
178 // the underlying error should be the custom error the application provided
179 let app_err = s2n_err.application_error().unwrap();
180 let io_err = app_err.downcast_ref::<std::io::Error>().unwrap();
181 let _custom_err = io_err
182 .get_ref()
183 .unwrap()
184 .downcast_ref::<CustomError>()
185 .unwrap();
186
187 // assert that the future is async returned Poll::Pending once
188 assert_eq!(wake_count, 1);
189 Ok(())
190 }
191
192 #[test]
193 fn client_hello_callback_async() -> Result<(), Error> {
194 let (waker, wake_count) = new_count_waker();
195 let require_pending_count = 10;
196 let handle = MockClientHelloHandler::new(require_pending_count);
197 let config = {
198 let mut config = config_builder(&security::DEFAULT_TLS13)?;
199 config.set_client_hello_callback(handle.clone())?;
200 // multiple calls to set_client_hello_callback should succeed
201 config.set_client_hello_callback(handle.clone())?;
202 config.build()?
203 };
204
205 let mut pair = TestPair::from_config(&config);
206 pair.server.set_waker(Some(&waker))?;
207 pair.handshake()?;
208
209 // confirm that the callback returned Pending `require_pending_count` times
210 assert_eq!(wake_count, require_pending_count);
211 // confirm that the final invoked count is +1 more than `require_pending_count`
212 assert_eq!(
213 handle.invoked.load(Ordering::SeqCst),
214 require_pending_count + 1
215 );
216
217 Ok(())
218 }
219
220 #[test]
221 fn client_hello_callback_sync() -> Result<(), Error> {
222 let (waker, wake_count) = new_count_waker();
223 #[derive(Clone)]
224 struct ClientHelloSyncCallback(Arc<AtomicUsize>);
225 impl ClientHelloSyncCallback {
226 fn new() -> Self {
227 ClientHelloSyncCallback(Arc::new(AtomicUsize::new(0)))
228 }
229 fn count(&self) -> usize {
230 self.0.load(Ordering::Relaxed)
231 }
232 }
233 impl ClientHelloCallback for ClientHelloSyncCallback {
234 fn on_client_hello(
235 &self,
236 connection: &mut crate::connection::Connection,
237 ) -> Result<Option<Pin<Box<dyn ConnectionFuture>>>, crate::error::Error> {
238 // Test that the config can be changed
239 connection
240 .set_config(build_config(&security::DEFAULT_TLS13).unwrap())
241 .unwrap();
242
243 // Test that server_name_extension_used can be invoked
244 connection.server_name_extension_used();
245
246 self.0.fetch_add(1, Ordering::Relaxed);
247
248 // returning `None` indicates that the client_hello callback is
249 // finished and the handshake can proceed.
250 Ok(None)
251 }
252 }
253 let callback = ClientHelloSyncCallback::new();
254
255 let config = {
256 let mut config = config_builder(&security::DEFAULT_TLS13)?;
257 config.set_client_hello_callback(callback.clone())?;
258 config.build()?
259 };
260
261 let mut pair = TestPair::from_config(&config);
262 pair.server.set_waker(Some(&waker))?;
263
264 assert_eq!(callback.count(), 0);
265
266 pair.handshake()?;
267 assert_eq!(callback.count(), 1);
268 assert_eq!(wake_count, 0);
269 Ok(())
270 }
271
272 #[test]
273 fn new_security_policy() -> Result<(), Error> {
274 use crate::security::Policy;
275
276 let policy = Policy::from_version("default")?;
277 config_builder(&policy)?;
278 Ok(())
279 }
280
281 #[test]
282 fn trust_location() -> Result<(), Error> {
283 let pem_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../certs"));
284 let mut cert = pem_dir.to_path_buf();
285 cert.push("cert.pem");
286 let mut key = pem_dir.to_path_buf();
287 key.push("key.pem");
288
289 let mut builder = crate::config::Builder::new();
290 builder.set_security_policy(&security::DEFAULT_TLS13)?;
291 builder.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
292 builder.load_pem(&fs::read(&cert)?, &fs::read(&key)?)?;
293 builder.trust_location(Some(&cert), None)?;
294
295 TestPair::handshake_with_config(&builder.build()?)?;
296 Ok(())
297 }
298
299 /// `trust_location()` calls `s2n_config_set_verification_ca_location()`, which has the legacy behavior
300 /// of enabling OCSP on clients. Since we do not want to replicate that behavior in the Rust bindings,
301 /// this test verifies that `trust_location()` does not turn on OCSP. It also verifies that turning
302 /// on OCSP explicitly still works when `trust_location()` is called.
303 #[test]
304 fn trust_location_does_not_change_ocsp_status() -> Result<(), Error> {
305 let pem_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../certs"));
306 let mut cert = pem_dir.to_path_buf();
307 cert.push("cert.pem");
308 let mut key = pem_dir.to_path_buf();
309 key.push("key.pem");
310
311 const OCSP_IANA_EXTENSION_ID: u16 = 5;
312
313 for enable_ocsp in [true, false] {
314 let config = {
315 let mut config = crate::config::Builder::new();
316
317 if enable_ocsp {
318 config.enable_ocsp()?;
319 }
320
321 config.set_security_policy(&security::DEFAULT_TLS13)?;
322 config.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
323 config.set_client_hello_callback(HasExtensionClientHelloHandler {
324 // This client hello handler will check for the OCSP extension
325 extension_iana: OCSP_IANA_EXTENSION_ID,
326 extension_expected: enable_ocsp,
327 })?;
328 config.load_pem(&fs::read(&cert)?, &fs::read(&key)?)?;
329 config.trust_location(Some(&cert), None)?;
330 config.build()?
331 };
332
333 let mut pair = TestPair::from_config(&config);
334 pair.server.set_waker(Some(&noop_waker()))?;
335 pair.handshake()?;
336 }
337 Ok(())
338 }
339
340 #[test]
341 fn connection_level_verify_host_callback() -> Result<(), Error> {
342 let reject_config = {
343 let keypair = CertKeyPair::default();
344 let mut config = crate::config::Builder::new();
345 // configure the config VerifyHostNameCallback to reject all certificates
346 config.set_verify_host_callback(RejectAllCertificatesHandler {})?;
347 config.set_security_policy(&security::DEFAULT_TLS13)?;
348 config.load_pem(keypair.cert(), keypair.key())?;
349 config.trust_pem(keypair.cert())?;
350 config.set_client_auth_type(ClientAuthType::Required)?;
351 config.build()?
352 };
353
354 // confirm that default connection establishment fails
355 let mut pair = TestPair::from_config(&reject_config);
356 assert!(pair.handshake().is_err());
357
358 // confirm that overriding the verify_host_callback on connection causes
359 // the handshake to succeed
360 let mut pair = TestPair::from_config(&reject_config);
361 pair.client
362 .set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
363 pair.server
364 .set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
365 pair.handshake()?;
366
367 Ok(())
368 }
369
370 #[test]
371 fn no_client_auth() -> Result<(), Error> {
372 use crate::enums::ClientAuthType;
373
374 let config = {
375 let mut config = config_builder(&security::DEFAULT_TLS13)?;
376 config.set_client_auth_type(ClientAuthType::None)?;
377 config.build()?
378 };
379
380 let mut pair = TestPair::from_config(&config);
381 pair.handshake()?;
382
383 for conn in [pair.server, pair.client] {
384 assert!(!conn.client_cert_used());
385 let cert = conn.client_cert_chain_bytes()?;
386 assert!(cert.is_none());
387 let sig_alg = conn.selected_client_signature_algorithm()?;
388 assert!(sig_alg.is_none());
389 let hash_alg = conn.selected_client_hash_algorithm()?;
390 assert!(hash_alg.is_none());
391 }
392
393 Ok(())
394 }
395
396 #[test]
397 fn client_auth() -> Result<(), Error> {
398 use crate::enums::ClientAuthType;
399
400 let config = {
401 let mut config = config_builder(&security::DEFAULT_TLS13)?;
402 config.set_client_auth_type(ClientAuthType::Optional)?;
403 config.build()?
404 };
405
406 let mut pair = TestPair::from_config(&config);
407 pair.handshake()?;
408
409 let cert = pair.server.client_cert_chain_bytes()?;
410 assert!(cert.is_some());
411 assert!(!cert.unwrap().is_empty());
412
413 for conn in [pair.server, pair.client] {
414 assert!(conn.client_cert_used());
415 let sig_alg = conn.selected_client_signature_algorithm()?;
416 assert!(sig_alg.is_some());
417 let hash_alg = conn.selected_client_hash_algorithm()?;
418 assert!(hash_alg.is_some());
419 }
420
421 Ok(())
422 }
423
424 #[test]
425 fn system_certs_loaded_by_default() -> Result<(), Error> {
426 let keypair = CertKeyPair::default();
427
428 // Load the server certificate into the trust store by overriding the OpenSSL default
429 // certificate location.
430 temp_env::with_var("SSL_CERT_FILE", Some(keypair.cert_path()), || {
431 let mut builder = Builder::new();
432 builder
433 .load_pem(keypair.cert(), keypair.key())?
434 .set_security_policy(&security::DEFAULT_TLS13)?
435 .set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
436
437 let config = builder.build().unwrap();
438 TestPair::handshake_with_config(&config)?;
439 Ok(())
440 })
441 }
442
443 #[test]
444 fn disable_loading_system_certs() -> Result<(), Error> {
445 let keypair = CertKeyPair::default();
446
447 // Load the server certificate into the trust store by overriding the OpenSSL default
448 // certificate location.
449 temp_env::with_var("SSL_CERT_FILE", Some(keypair.cert_path()), || {
450 // Test the Builder itself, and also the Builder produced by the Config builder() API.
451 for mut builder in [Builder::new(), Config::builder()] {
452 builder
453 .load_pem(keypair.cert(), keypair.key())?
454 .set_security_policy(&security::DEFAULT_TLS13)?
455 .set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
456
457 // Disable loading system certificates
458 builder.with_system_certs(false)?;
459
460 let config = builder.build()?;
461 let mut config_with_system_certs = config.clone();
462
463 // System certificates should not be loaded into the trust store. The handshake
464 // should fail since the certificate should not be trusted.
465 assert!(TestPair::handshake_with_config(&config).is_err());
466
467 // The handshake should succeed after trusting the certificate.
468 unsafe {
469 s2n_tls_sys::s2n_config_load_system_certs(
470 config_with_system_certs.as_mut_ptr(),
471 );
472 }
473 TestPair::handshake_with_config(&config_with_system_certs)?;
474 }
475 Ok(())
476 })
477 }
478
479 #[test]
480 fn peer_chain() -> Result<(), Error> {
481 use crate::enums::ClientAuthType;
482
483 let config = {
484 let mut config = config_builder(&security::DEFAULT_TLS13)?;
485 config.set_client_auth_type(ClientAuthType::Optional)?;
486 config.build()?
487 };
488
489 let mut pair = TestPair::from_config(&config);
490 pair.handshake()?;
491
492 for conn in [pair.server, pair.client] {
493 let chain = conn.peer_cert_chain()?;
494 assert_eq!(chain.len(), 1);
495 for cert in chain.iter() {
496 let cert = cert?;
497 let cert = cert.der()?;
498 assert!(!cert.is_empty());
499 }
500 }
501
502 Ok(())
503 }
504
505 #[test]
506 fn selected_cert() -> Result<(), Error> {
507 use crate::enums::ClientAuthType;
508
509 let config = {
510 let mut config = config_builder(&security::DEFAULT_TLS13)?;
511 config.set_client_auth_type(ClientAuthType::Required)?;
512 config.build()?
513 };
514
515 let mut pair = TestPair::from_config(&config);
516
517 // None before handshake...
518 assert!(pair.server.selected_cert().is_none());
519 assert!(pair.client.selected_cert().is_none());
520
521 pair.handshake()?;
522
523 for conn in [&pair.server, &pair.client] {
524 let chain = conn.selected_cert().unwrap();
525 assert_eq!(chain.len(), 1);
526 for cert in chain.iter() {
527 let cert = cert?;
528 let cert = cert.der()?;
529 assert!(!cert.is_empty());
530 }
531 }
532
533 // Same config is used for both and we are doing mTLS, so both should select the same
534 // certificate.
535 assert_eq!(
536 pair.server
537 .selected_cert()
538 .unwrap()
539 .iter()
540 .next()
541 .unwrap()?
542 .der()?,
543 pair.client
544 .selected_cert()
545 .unwrap()
546 .iter()
547 .next()
548 .unwrap()?
549 .der()?
550 );
551
552 Ok(())
553 }
554
555 #[test]
556 fn master_secret_success() -> Result<(), Error> {
557 let policy = security::Policy::from_version("test_all_tls12")?;
558 let config = config_builder(&policy)?.build()?;
559 let mut pair = TestPair::from_config(&config);
560 pair.handshake()?;
561
562 let server_secret = pair.server.master_secret()?;
563 let client_secret = pair.client.master_secret()?;
564 assert_eq!(server_secret, client_secret);
565
566 Ok(())
567 }
568
569 #[test]
570 fn master_secret_failure() -> Result<(), Error> {
571 // TLS1.3 does not support getting the master secret
572 let mut pair = TestPair::from_config(&build_config(&security::DEFAULT_TLS13)?);
573 pair.handshake()?;
574
575 for conn in [pair.client, pair.server] {
576 let err = conn.master_secret().unwrap_err();
577 assert_eq!(err.kind(), ErrorType::UsageError);
578 }
579
580 Ok(())
581 }
582
583 #[cfg(feature = "unstable-ktls")]
584 #[test]
585 fn key_updates() -> Result<(), Error> {
586 use crate::{connection::KeyUpdateCount, enums::PeerKeyUpdate};
587
588 let empty_key_updates = KeyUpdateCount {
589 recv_key_updates: 0,
590 send_key_updates: 0,
591 };
592
593 let mut pair = TestPair::from_config(&build_config(&security::DEFAULT_TLS13)?);
594 pair.handshake()?;
595
596 // there haven't been any key updates at the start of the connection
597 assert_eq!(pair.client.key_update_counts()?, empty_key_updates);
598 assert_eq!(pair.server.key_update_counts()?, empty_key_updates);
599
600 pair.server
601 .request_key_update(PeerKeyUpdate::KeyUpdateNotRequested)?;
602 assert!(pair.server.poll_send(&[0]).is_ready());
603
604 // the server send key has been updated
605 let client_updates = pair.client.key_update_counts()?;
606 assert_eq!(client_updates, empty_key_updates);
607 let server_updates = pair.server.key_update_counts()?;
608 assert_eq!(server_updates.recv_key_updates, 0);
609 assert_eq!(server_updates.send_key_updates, 1);
610
611 Ok(())
612 }
613
614 #[cfg(feature = "fips")]
615 #[test]
616 fn test_fips_mode() {
617 use crate::init;
618
619 init::init();
620 assert!(init::fips_mode().unwrap().is_enabled());
621 }
622
623 /// Test that a context can be used from within a callback.
624 #[test]
625 fn test_app_context_callback() -> Result<(), crate::error::Error> {
626 struct TestApplicationContext {
627 invoked_count: u32,
628 }
629
630 struct TestClientHelloHandler {}
631
632 impl ClientHelloCallback for TestClientHelloHandler {
633 fn on_client_hello(
634 &self,
635 connection: &mut connection::Connection,
636 ) -> ConnectionFutureResult {
637 let app_context = connection
638 .application_context_mut::<TestApplicationContext>()
639 .unwrap();
640 app_context.invoked_count += 1;
641 Ok(None)
642 }
643 }
644
645 let config = {
646 let keypair = CertKeyPair::default();
647 let mut builder = Builder::new();
648 builder
649 .set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})
650 .unwrap();
651 builder
652 .set_client_hello_callback(TestClientHelloHandler {})
653 .unwrap();
654 builder.load_pem(keypair.cert(), keypair.key()).unwrap();
655 builder.trust_pem(keypair.cert()).unwrap();
656 builder.build().unwrap()
657 };
658 let mut pair = TestPair::from_config(&config);
659 pair.server.set_waker(Some(&noop_waker()))?;
660
661 let context = TestApplicationContext { invoked_count: 0 };
662 pair.server.set_application_context(context);
663
664 pair.handshake()?;
665
666 let context = pair
667 .server
668 .application_context::<TestApplicationContext>()
669 .unwrap();
670 assert_eq!(context.invoked_count, 1);
671
672 Ok(())
673 }
674
675 #[test]
676 fn no_application_protocol() -> Result<(), Error> {
677 let config = config_builder(&security::DEFAULT)?.build()?;
678 let mut pair = TestPair::from_config(&config);
679 pair.handshake()?;
680 assert!(pair.server.application_protocol().is_none());
681 Ok(())
682 }
683
684 #[test]
685 fn application_protocol() -> Result<(), Error> {
686 let config = config_builder(&security::DEFAULT)?.build()?;
687 let mut pair = TestPair::from_config(&config);
688 pair.server
689 .set_application_protocol_preference(["http/1.1", "h2"])?;
690 pair.client.append_application_protocol_preference(b"h2")?;
691 pair.handshake()?;
692 let protocol = pair.server.application_protocol().unwrap();
693 assert_eq!(protocol, b"h2");
694 Ok(())
695 }
696
697 #[test]
698 fn client_hello_sslv2_negative() -> Result<(), testing::Error> {
699 let config = testing::build_config(&security::DEFAULT_TLS13)?;
700 let mut pair = TestPair::from_config(&config);
701 pair.handshake()?;
702 assert!(!pair.server.client_hello_is_sslv2()?);
703 Ok(())
704 }
705
706 #[test]
707 fn client_hello_sslv2_positive() -> Result<(), testing::Error> {
708 // copy-pasted from s2n-tls/tests/testlib/s2n_sslv2_client_hello.h
709 // by concatenating these fields together, a valid SSLv2 formatted client hello
710 // can be assembled
711 const SSLV2_CLIENT_HELLO_HEADER: &[u8] = &[0x80, 0xb3, 0x01, 0x03, 0x03];
712 const SSLV2_CLIENT_HELLO_PREFIX: &[u8] = &[0x00, 0x8a, 0x00, 0x00, 0x00, 0x20];
713 const SSLV2_CLIENT_HELLO_CIPHER_SUITES: &[u8] = &[
714 0x00, 0xc0, 0x24, 0x00, 0xc0, 0x28, 0x00, 0x00, 0x3d, 0x00, 0xc0, 0x26, 0x00, 0xc0,
715 0x2a, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x6a, 0x00, 0xc0, 0x0a, 0x07, 0x00, 0xc0, 0x00,
716 0xc0, 0x14, 0x00, 0x00, 0x35, 0x00, 0xc0, 0x05, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x39,
717 0x00, 0x00, 0x38, 0x00, 0xc0, 0x23, 0x00, 0xc0, 0x27, 0x00, 0x00, 0x3c, 0x00, 0xc0,
718 0x25, 0x00, 0xc0, 0x29, 0x00, 0x00, 0x67, 0x00, 0x00, 0x40, 0x00, 0xc0, 0x09, 0x06,
719 0x00, 0x40, 0x00, 0xc0, 0x13, 0x00, 0x00, 0x2f, 0x00, 0xc0, 0x04, 0x01, 0x00, 0x80,
720 0x00, 0xc0, 0x0e, 0x00, 0x00, 0x33, 0x00, 0x00, 0x32, 0x00, 0xc0, 0x2c, 0x00, 0xc0,
721 0x2b, 0x00, 0xc0, 0x30, 0x00, 0x00, 0x9d, 0x00, 0xc0, 0x2e, 0x00, 0xc0, 0x32, 0x00,
722 0x00, 0x9f, 0x00, 0x00, 0xa3, 0x00, 0xc0, 0x2f, 0x00, 0x00, 0x9c, 0x00, 0xc0, 0x2d,
723 0x00, 0xc0, 0x31, 0x00, 0x00, 0x9e, 0x00, 0x00, 0xa2, 0x00, 0x00, 0xff,
724 ];
725 const SSLV2_CLIENT_HELLO_CHALLENGE: &[u8] = &[
726 0x5b, 0xe9, 0xcc, 0xad, 0xd6, 0xa5, 0x20, 0xac, 0xa3, 0xf4, 0x8e, 0x88, 0x06, 0xb5,
727 0x95, 0x53, 0x2d, 0x53, 0xfe, 0xd7, 0xa1, 0x00, 0x57, 0xc0, 0x53, 0x9d, 0x84, 0x71,
728 0x80, 0x7f, 0x30, 0x7e,
729 ];
730
731 let config = testing::build_config(&security::Policy::from_version("test_all")?)?;
732 // we use the pair to setup IO, but we don't want the client to write anything.
733 // So we drop the client and just directly write the SSLv2 header to the
734 // client_tx_stream
735 let mut pair = TestPair::from_config(&config);
736 drop(pair.client);
737
738 let mut client_tx_stream = pair.io.client_tx_stream.borrow_mut();
739 client_tx_stream.write_all(SSLV2_CLIENT_HELLO_HEADER)?;
740 client_tx_stream.write_all(SSLV2_CLIENT_HELLO_PREFIX)?;
741 client_tx_stream.write_all(SSLV2_CLIENT_HELLO_CIPHER_SUITES)?;
742 client_tx_stream.write_all(SSLV2_CLIENT_HELLO_CHALLENGE)?;
743 // end the exclusive borrow
744 drop(client_tx_stream);
745
746 // the first server.poll_negotiate causes the server to read in the client hello
747 assert!(pair.server.poll_negotiate()?.is_pending());
748 assert!(pair.server.client_hello_is_sslv2()?);
749 Ok(())
750 }
751 }