Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/record/serialization.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use s2n_tls::{
5 enums::SerializationVersion,
6 error::ErrorType,
7 security::Policy,
8 testing::{self, TestPair},
9 };
10 use std::task::Poll;
11
12 /// The TLS record header is 5 bytes: 1 (content type) + 2 (protocol version) + 2 (length)
13 const TLS_RECORD_HEADER_LEN: usize = 5;
14
15 /// Policy arbitrarily selected to negotiate TLS 1.2. We negotiate TLS 1.2 as a
16 /// convenience to avoid the TLS 1.3 required capability logic.
17 const TEST_POLICY: &str = "20240501";
18
19 fn build_serializable_config() -> s2n_tls::config::Config {
20 let mut builder = testing::config_builder(&Policy::from_version(TEST_POLICY).unwrap()).unwrap();
21 builder
22 .set_serialization_version(SerializationVersion::V1)
23 .unwrap();
24 builder.build().unwrap()
25 }
26
27 fn assert_invalid_state_error(err: s2n_tls::error::Error) {
28 assert_eq!(err.kind(), ErrorType::UsageError);
29 assert_eq!(err.name(), "S2N_ERR_INVALID_STATE");
30 assert_eq!(
31 err.message(),
32 "Invalid state, this is the result of invalid use of an API. \
33 Check the API documentation for the function that raised this error for more info"
34 );
35 }
36
37 /// Attempt to serialize the connection. Returns the result.
38 fn try_serialize(conn: &s2n_tls::connection::Connection) -> Result<(), s2n_tls::error::Error> {
39 let length = conn.serialization_length()?;
40 let mut buf = vec![0u8; length];
41 conn.serialize(&mut buf)
42 }
43
44 /// This test serves as documentation/confirmation of some sharp edges around
45 /// serialization failure. Specifically, there is no way to detect whether a record
46 /// fragment has been ingested. https://github.com/aws/s2n-tls/issues/5863
47 #[test]
48 fn serialization_io_edge_cases() {
49 let config = build_serializable_config();
50
51 // case 1: buffer contains incomplete header
52 // s2n-tls has a separate buffer for header so we test this separately from
53 // the "partial record" case.
54 {
55 let mut pair = TestPair::from_config(&config);
56 pair.handshake().unwrap();
57
58 // Client sends data, producing encrypted TLS records in client_tx_stream
59 assert!(pair.client.poll_send(&[0; 100]).is_ready());
60
61 // Truncate the buffer to only 2 bytes (incomplete 5-byte header)
62 pair.io.client_tx_stream.borrow_mut().truncate(2);
63
64 // Server attempts to read but only gets a partial header
65 assert!(matches!(
66 pair.server.poll_recv(&mut [0; 100]),
67 Poll::Pending
68 ));
69 // This is "invisible" data, not yet decrypted
70 assert_eq!(pair.server.peek_len(), 0);
71
72 // Serialization must fail because header_in has pending data
73 assert_invalid_state_error(try_serialize(&pair.server).unwrap_err());
74 }
75
76 // case 2: buffer contains partial record, waiting for complete record
77 {
78 let mut pair = TestPair::from_config(&config);
79 pair.handshake().unwrap();
80
81 // Client sends data
82 assert!(pair.client.poll_send(&[0; 100]).is_ready());
83
84 // Truncate the buffer to header + 1 byte of body (partial record)
85 pair.io
86 .client_tx_stream
87 .borrow_mut()
88 .truncate(TLS_RECORD_HEADER_LEN + 1);
89
90 // Server attempts to read but only gets a partial record
91 assert!(matches!(
92 pair.server.poll_recv(&mut [0; 100]),
93 Poll::Pending
94 ));
95 // This is "invisible" data, not yet decrypted
96 assert_eq!(pair.server.peek_len(), 0);
97
98 // Serialization must fail because conn->in has pending partial record data
99 assert_invalid_state_error(try_serialize(&pair.server).unwrap_err());
100 }
101
102 // case 3: buffer contains decrypted record, waiting for application read
103 {
104 let mut pair = TestPair::from_config(&config);
105 pair.handshake().unwrap();
106
107 // Client sends data
108 assert!(pair.client.poll_send(&[0; 100]).is_ready());
109
110 // Server reads with a 1-byte buffer: decrypts the full record but only
111 // returns 1 byte to the application, leaving the rest buffered internally
112 assert!(matches!(
113 pair.server.poll_recv(&mut [0; 1]),
114 Poll::Ready(Ok(1))
115 ));
116 assert_eq!(pair.server.peek_len(), 99);
117
118 // Serialization must fail because conn->in has pending decrypted data
119 assert_invalid_state_error(try_serialize(&pair.server).unwrap_err());
120
121 // read the rest of the data, serialization then succeeds
122 assert!(pair.server.poll_recv(&mut [0; 100]).is_ready());
123 assert!(pair.server.serialize(&mut [0; 1_024]).is_ok());
124 }
125 }