Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/handshake/handshake_failure_errors.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 brass_aphid_wire_decryption::decryption::key_manager::KeyManager;
5 use brass_aphid_wire_messages::{
6 iana::{self},
7 protocol::{
8 content_value::{ContentValue, HandshakeMessageValue},
9 AlertDescription, AlertLevel,
10 },
11 };
12 use openssl::ssl::{SslContextBuilder, SslVersion};
13 use s2n_tls::{error::ErrorType, security::Policy, testing::TestPair};
14 use tls_harness::{
15 cohort::{s2n_tls::HostNameHandler, OpenSslConnection, S2NConnection},
16 harness::TlsConfigBuilderPair,
17 TlsConnPair,
18 };
19
20 use crate::{
21 capability_check::{required_capability, required_capability_with_inner_result, Capability},
22 utilities::certs::{self, CertMaterials},
23 };
24
25 #[test]
26 fn no_protocols_in_common() {
27 required_capability(&[Capability::Tls13], || {
28 let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = {
29 let tls13_only_policy = Policy::from_version("20250414").unwrap();
30 let mut configs =
31 TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default();
32 configs
33 .server
34 .set_security_policy(&tls13_only_policy)
35 .unwrap();
36 configs
37 .client
38 .set_max_proto_version(Some(SslVersion::TLS1_2))
39 .unwrap();
40
41 configs.connection_pair()
42 };
43
44 let error = pair.handshake().unwrap_err();
45 let s2n_error: Box<s2n_tls::error::Error> = error.downcast().unwrap();
46 assert_eq!(s2n_error.kind(), ErrorType::ProtocolError);
47 assert_eq!(s2n_error.name(), "S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED");
48 assert_eq!(
49 s2n_error.message(),
50 "TLS protocol version is not supported by configuration"
51 );
52 });
53 }
54
55 #[test]
56 fn no_ciphers_in_common() {
57 required_capability(&[Capability::Tls13], || {
58 let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = {
59 let tls13_only_policy = Policy::from_version("20250414").unwrap();
60 let mut configs =
61 TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default();
62 configs
63 .server
64 .set_security_policy(&tls13_only_policy)
65 .unwrap();
66 configs
67 .client
68 .set_ciphersuites("TLS_SHA256_SHA256")
69 .unwrap();
70
71 configs.connection_pair()
72 };
73
74 let error = pair.handshake().unwrap_err();
75 let s2n_error: Box<s2n_tls::error::Error> = error.downcast().unwrap();
76 assert_eq!(s2n_error.kind(), ErrorType::ProtocolError);
77 assert_eq!(s2n_error.name(), "S2N_ERR_CIPHER_NOT_SUPPORTED");
78 assert_eq!(s2n_error.message(), "Cipher is not supported");
79 })
80 }
81
82 #[test]
83 fn no_groups_in_common() {
84 required_capability(&[Capability::Tls13], || {
85 let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = {
86 let tls13_only_policy = Policy::from_version("20250414").unwrap();
87 let mut configs =
88 TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default();
89 configs
90 .server
91 .set_security_policy(&tls13_only_policy)
92 .unwrap();
93 configs.client.set_groups_list("x448").unwrap();
94
95 configs.connection_pair()
96 };
97
98 let error = pair.handshake().unwrap_err();
99 let s2n_error: Box<s2n_tls::error::Error> = error.downcast().unwrap();
100 assert_eq!(s2n_error.kind(), ErrorType::InternalError);
101 assert_eq!(s2n_error.name(), "S2N_ERR_INVALID_SUPPORTED_GROUP_STATE");
102 assert_eq!(s2n_error.message(), "SupportedGroup preference decision entered invalid state and selected both KEM and EC Curve");
103 })
104 }
105
106 /// When there are no signature schemes in common, s2n-tls will make a "best effort"
107 /// to proceed with the handshake anyways. This means that s2n-tls won't actually
108 /// fail the connection, and instead it will show up as an alert from the peer.
109 #[test]
110 fn no_signatures_in_common() {
111 required_capability(&[Capability::Tls13], || {
112 let key_manager = KeyManager::new();
113
114 let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = {
115 // "20250211" doesn't allow any SHA256 signatures
116 let sha384_only_policy = Policy::from_version("20250211").unwrap();
117 let mut configs =
118 TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default();
119 configs
120 .server
121 .set_security_policy(&sha384_only_policy)
122 .unwrap();
123 key_manager.enable_s2n_logging(&mut configs.server);
124 configs
125 .client
126 .set_sigalgs_list("rsa_pss_pss_sha256")
127 .unwrap();
128
129 configs.connection_pair()
130 };
131 pair.io.enable_decryption(key_manager);
132
133 let error = pair.handshake().unwrap_err();
134 let ossl_error: Box<openssl::ssl::Error> = error.downcast().unwrap();
135 // the client/openssl fails when checking the signature, because s2n-tls sent
136 // a signature that it did not advertise support for
137 // Error {
138 // code: ErrorCode(1),
139 // cause: Some(Ssl(ErrorStack([
140 // Error {
141 // code: 167772530,
142 // library: "SSL routines",
143 // function: "tls12_check_peer_sigalg",
144 // reason: "wrong signature type",
145 // file: "ssl/t1_lib.c",
146 // line: 2797 }
147 // ]))) }
148 assert!(ossl_error.to_string().contains("tls12_check_peer_sigalg"));
149 assert!(ossl_error.to_string().contains("wrong signature type"));
150
151 let transcript = pair.io.decrypter.borrow().as_ref().unwrap().transcript();
152 let content = transcript.content_transcript.lock().unwrap().clone();
153
154 // confirm that s2n-tls sent an unexpected signature.
155 {
156 let certificate_verify = content
157 .iter()
158 .filter_map(|(_sender, content_value)| match content_value {
159 ContentValue::Handshake(HandshakeMessageValue::CertVerifyTls13(
160 cert_verify,
161 )) => Some(cert_verify),
162 _ => None,
163 })
164 .next()
165 .unwrap();
166 // s2n-tls send a certificate verify with rsa_pss_rsae_sha384, which is not
167 // one of the client's supported signature algorithms
168 assert_eq!(
169 certificate_verify.algorithm,
170 iana::constants::rsa_pss_rsae_sha384
171 );
172 }
173
174 // confirm that the client sent an alert indicating its displeasure
175 {
176 let alert = content
177 .iter()
178 .filter(|(sender, _content)| {
179 *sender == brass_aphid_wire_decryption::decryption::Mode::Client
180 })
181 .filter_map(|(_sender, content_value)| match content_value {
182 ContentValue::Alert(alert) => Some(alert),
183 _ => None,
184 })
185 .next()
186 .unwrap();
187 assert_eq!(alert.level, AlertLevel::Fatal);
188 assert_eq!(alert.description, AlertDescription::HandshakeFailure)
189 }
190 })
191 }
192
193 #[test]
194 fn host_name_verification() {
195 let cert = &certs::BEAVER;
196 const HOST_NAME: &str = "www.beaver.com";
197
198 let server = {
199 let mut server = s2n_tls::config::Builder::new();
200 server
201 .load_pem(&cert.server_chain(), &cert.server_key())
202 .unwrap();
203 server.build().unwrap()
204 };
205
206 let client = {
207 let mut client = s2n_tls::config::Builder::new();
208 client.trust_pem(&cert.ca()).unwrap();
209 client.build().unwrap()
210 };
211
212 // sanity check: handshake succeeds when the right hostname is set.
213 {
214 let mut pair = TestPair::from_configs(&client, &server);
215 pair.client.set_server_name(HOST_NAME).unwrap();
216 pair.handshake().unwrap();
217 }
218
219 // failure: the server_name is not set, so the default hostname verify cb will fail
220 {
221 let mut pair = TestPair::from_configs(&client, &server);
222 let err = pair.handshake().unwrap_err();
223 assert_eq!(err.kind(), ErrorType::ProtocolError);
224 assert_eq!(err.name(), "S2N_ERR_CERT_INVALID_HOSTNAME");
225 }
226 }
227
228 /// When a client cert chain is signed with signatures that aren't allowed by the
229 /// `certificate_signature_preferences` field in the security policy we return an
230 /// S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT error
231 #[test]
232 fn mtls_cert_signature_not_allowed() -> Result<(), Box<dyn std::error::Error>> {
233 /// Certificate Signatures must have a SHA384 digest
234 /// Transcript Signatures must have a SHA384 digest
235 /// Certificate keys must be RSA3072 or secp384r1
236 const POLICY: &str = "20251013";
237
238 let server_cert = CertMaterials::from_permutation("rsae_pkcs_3072_sha384");
239 // The client cert has a valid key (RSA3072) but an invalid signature
240 // (SHA256 digest)
241 let client_cert = CertMaterials::from_permutation("rsae_pkcs_3072_sha256");
242
243 let mut pair: TlsConnPair<S2NConnection, S2NConnection> = {
244 let sha384_only_policy = Policy::from_version(POLICY)?;
245 let mut server = s2n_tls::config::Builder::new();
246
247 server
248 .set_security_policy(&sha384_only_policy)?
249 .set_verify_host_callback(HostNameHandler::new("localhost"))?
250 .load_pem(&server_cert.server_chain(), &server_cert.server_key())?
251 .set_client_auth_type(s2n_tls::enums::ClientAuthType::Required)?
252 .trust_pem(&client_cert.ca())?;
253 let server = server.build().unwrap();
254
255 let mut client = s2n_tls::config::Builder::new();
256 client
257 .set_verify_host_callback(HostNameHandler::new("localhost"))?
258 .load_pem(&client_cert.server_chain(), &client_cert.server_key())?
259 .trust_pem(&server_cert.ca())?;
260 let client = client.build().unwrap();
261
262 TlsConnPair::from_configs(&client.into(), &server.into())
263 };
264
265 let error = pair.handshake().unwrap_err();
266 let s2n_error: Box<s2n_tls::error::Error> = error.downcast()?;
267 assert_eq!(s2n_error.kind(), ErrorType::UsageError);
268 assert_eq!(
269 s2n_error.name(),
270 "S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT"
271 );
272
273 Ok(())
274 }
275
276 /// When a client cert chain uses keys that aren't allowed by the `certificate_key_preferences`
277 /// field in the security policy, we return an
278 /// S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT error
279 #[test]
280 fn mtls_cert_key_not_allowed() -> Result<(), Box<dyn std::error::Error>> {
281 /// Certificate Signatures must have a SHA384 digest
282 /// Transcript Signatures must have a SHA384 digest
283 /// Certificate keys must be RSA3072 or secp384r1
284 const POLICY: &str = "20251013";
285
286 let server_cert = CertMaterials::from_permutation("ec_ecdsa_p384_sha384");
287 // the cert has a valid signature (ECDSA+SHA384) but an invalid key (p256).
288 let client_cert = CertMaterials::from_permutation("ec_ecdsa_p256_sha384");
289
290 let mut pair: TlsConnPair<S2NConnection, S2NConnection> = {
291 let policy = Policy::from_version(POLICY)?;
292
293 let mut server = s2n_tls::config::Builder::new();
294 server
295 .set_verify_host_callback(HostNameHandler::new("localhost"))?
296 .load_pem(&server_cert.server_chain(), &server_cert.server_key())?
297 .set_security_policy(&policy)?
298 .set_client_auth_type(s2n_tls::enums::ClientAuthType::Required)?
299 .trust_pem(&client_cert.ca())?;
300 let server = server.build()?;
301
302 let mut client = s2n_tls::config::Builder::new();
303
304 client
305 .set_verify_host_callback(HostNameHandler::new("localhost"))?
306 .load_pem(&client_cert.server_chain(), &client_cert.server_key())?
307 .trust_pem(&server_cert.ca())?;
308 let client = client.build()?;
309 TlsConnPair::from_configs(&client.into(), &server.into())
310 };
311
312 let error = pair.handshake().unwrap_err();
313 let s2n_error: Box<s2n_tls::error::Error> = error.downcast()?;
314 assert_eq!(s2n_error.kind(), ErrorType::UsageError);
315 assert_eq!(
316 s2n_error.name(),
317 "S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT"
318 );
319 Ok(())
320 }
321
322 /// When a server is doing mTLS, it sends a CertificateRequest to the client
323 /// which includes the server's supported signature algorithms. For TLS 1.3, this
324 /// will restrict the _key type_ of ECDSA certs, because TLS 1.3 added key type
325 /// as part of the signature specification.
326 ///
327 /// So the handshake will either fail
328 /// - at the client side, because it recognizes that it doesn't have an ECDSA cert
329 /// capable of generating the signature that the server requires.
330 /// - at the server side, because the client makes a "best effort" to continue and
331 /// then the server is responsible for rejecting it.
332 ///
333 /// This test documents the various failure modes of different TLS implementations.
334 #[test]
335 fn mtls_tls13_transcript_signature_not_negotiable() {
336 /// allows a wide range of certificate signatures/keys, but transcript signatures
337 /// must use a SHA384 digest (ecdsa_sha384).
338 const POLICY: &str = "20250211";
339
340 required_capability_with_inner_result(&[Capability::Tls13], || {
341 let server_cert = CertMaterials::from_permutation("ec_ecdsa_p384_sha384");
342 // The client cert will produce an invalid transcript signature (ecdsa_secp256r1_sha256)
343 // which is not allowed by the server's security policy.
344 let client_cert = CertMaterials::from_permutation("ec_ecdsa_p256_sha384");
345
346 // OpenSSL client
347 {
348 let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = {
349 let policy = Policy::from_version(POLICY)?;
350 let mut configs =
351 TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default();
352
353 configs
354 .server
355 .set_security_policy(&policy)?
356 .set_verify_host_callback(HostNameHandler::new("localhost"))?
357 .set_client_auth_type(s2n_tls::enums::ClientAuthType::Required)?
358 .trust_pem(&client_cert.ca())?;
359
360 configs
361 .client
362 .set_certificate_chain_file(&client_cert.server_chain_path)?;
363 configs.client.set_private_key_file(
364 &client_cert.server_key_path,
365 openssl::ssl::SslFiletype::PEM,
366 )?;
367
368 configs.connection_pair()
369 };
370
371 // OpenSSL will not send the client certificate if it doesn't satisfy
372 // the server Signature Algorithm requirements.
373 let error = pair.handshake().unwrap_err();
374 let s2n_error: Box<s2n_tls::error::Error> = error.downcast()?;
375 assert_eq!(s2n_error.kind(), ErrorType::ProtocolError);
376 assert_eq!(s2n_error.name(), "S2N_ERR_MISSING_CLIENT_CERT");
377 }
378
379 // s2n-tls client
380 {
381 let mut pair: TlsConnPair<S2NConnection, S2NConnection> = {
382 let policy = Policy::from_version(POLICY)?;
383 let mut server = s2n_tls::config::Builder::new();
384
385 server
386 .set_security_policy(&policy)?
387 .set_verify_host_callback(HostNameHandler::new("localhost"))?
388 .load_pem(&server_cert.server_chain(), &server_cert.server_key())?
389 .set_client_auth_type(s2n_tls::enums::ClientAuthType::Required)?
390 .trust_pem(&client_cert.ca())?;
391 let server = server.build().unwrap();
392
393 let mut client = s2n_tls::config::Builder::new();
394 client
395 .set_verify_host_callback(HostNameHandler::new("localhost"))?
396 .load_pem(&client_cert.server_chain(), &client_cert.server_key())?
397 .trust_pem(&server_cert.ca())?;
398 let client = client.build().unwrap();
399
400 TlsConnPair::from_configs(&client.into(), &server.into())
401 };
402
403 // The s2n-tls client will "best-effort" send the transcript signature,
404 // which is then rejected by the server because it isn't allowed by
405 // the security policy
406 let error = pair.handshake().unwrap_err();
407 let s2n_error: Box<s2n_tls::error::Error> = error.downcast()?;
408 assert_eq!(s2n_error.kind(), ErrorType::ProtocolError);
409 assert_eq!(s2n_error.name(), "S2N_ERR_INVALID_SIGNATURE_SCHEME");
410 }
411
412 Ok(())
413 });
414 }