| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use openssl::ssl::{SslContextBuilder, SslVerifyMode, SslVersion}; |
| 5 |
use s2n_tls::{enums::ClientAuthType, renegotiate::RenegotiateResponse, security::Policy}; |
| 6 |
use std::io::{Read, Write}; |
| 7 |
use tls_harness::{ |
| 8 |
cohort::{OpenSslConnection, S2NConnection}, |
| 9 |
harness::{ |
| 10 |
read_to_bytes, PemType, SigType, TlsConfigBuilder, TlsConfigBuilderPair, TlsConnPair, |
| 11 |
TlsConnection, |
| 12 |
}, |
| 13 |
openssl_extension::{SslExtension, SslStreamExtension}, |
| 14 |
}; |
| 15 |
|
| 16 |
|
| 17 |
const POLICY_WITH_TLS12: &str = "20240503"; |
| 18 |
|
| 19 |
fn renegotiate_pair( |
| 20 |
pair: &mut TlsConnPair<S2NConnection, OpenSslConnection>, |
| 21 |
app_data: Option<Vec<u8>>, |
| 22 |
) -> Result<(), Box<dyn std::error::Error>> { |
| 23 |
|
| 24 |
pair.server.connection.mut_ssl().renegotiate(); |
| 25 |
assert!(pair.server.connection.ssl().renegotiate_pending()); |
| 26 |
|
| 27 |
|
| 28 |
let _ = pair.server.connection.write(&[]); |
| 29 |
|
| 30 |
|
| 31 |
let _ = pair.client.connection_mut().poll_recv(&mut [0]); |
| 32 |
|
| 33 |
if let Some(data) = &app_data { |
| 34 |
|
| 35 |
let _ = pair.server.connection.write(data); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
let _ = pair.server.connection.read(&mut [0]); |
| 40 |
|
| 41 |
if let Some(data) = &app_data { |
| 42 |
|
| 43 |
let mut buffer = [0; 1_024]; |
| 44 |
assert!(data.len() < buffer.len()); |
| 45 |
let _ = pair |
| 46 |
.client |
| 47 |
.connection_mut() |
| 48 |
.poll_recv(&mut buffer[0..data.len()]); |
| 49 |
assert_eq!(&buffer[0..data.len()], data); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
while pair.server.connection.ssl().renegotiate_pending() { |
| 56 |
let _ = pair.client.connection_mut().poll_recv(&mut [0]); |
| 57 |
let _ = pair.server.connection.read(&mut [0]); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
let _ = pair.client.connection_mut().poll_recv(&mut [0]); |
| 62 |
|
| 63 |
|
| 64 |
assert!(!pair.server.connection.ssl().renegotiate_pending()); |
| 65 |
Ok(()) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#[test] |
| 71 |
fn s2n_client_renegotiation_is_patched() -> Result<(), Box<dyn std::error::Error>> { |
| 72 |
let mut configs: TlsConfigBuilderPair<s2n_tls::config::Builder, SslContextBuilder> = |
| 73 |
TlsConfigBuilderPair::default(); |
| 74 |
configs.set_cert(SigType::Ecdsa256); |
| 75 |
|
| 76 |
let policy_with_tls12 = Policy::from_version(POLICY_WITH_TLS12)?; |
| 77 |
configs.client.set_security_policy(&policy_with_tls12)?; |
| 78 |
configs |
| 79 |
.server |
| 80 |
.set_max_proto_version(Some(SslVersion::TLS1_2))?; |
| 81 |
|
| 82 |
let mut pair: TlsConnPair<S2NConnection, OpenSslConnection> = configs.connection_pair(); |
| 83 |
|
| 84 |
assert!(pair.handshake().is_ok()); |
| 85 |
assert!(pair.server.connection.ssl().secure_renegotiation_support()); |
| 86 |
Ok(()) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#[test] |
| 93 |
fn s2n_client_ignores_openssl_renegotiate_request() -> Result<(), Box<dyn std::error::Error>> { |
| 94 |
let mut configs: TlsConfigBuilderPair<s2n_tls::config::Builder, SslContextBuilder> = |
| 95 |
TlsConfigBuilderPair::default(); |
| 96 |
configs.set_cert(SigType::Ecdsa256); |
| 97 |
|
| 98 |
let policy_with_tls12 = Policy::from_version(POLICY_WITH_TLS12)?; |
| 99 |
configs.client.set_security_policy(&policy_with_tls12)?; |
| 100 |
configs |
| 101 |
.server |
| 102 |
.set_max_proto_version(Some(SslVersion::TLS1_2))?; |
| 103 |
|
| 104 |
let mut pair: TlsConnPair<S2NConnection, OpenSslConnection> = configs.connection_pair(); |
| 105 |
|
| 106 |
assert!(pair.handshake().is_ok()); |
| 107 |
|
| 108 |
|
| 109 |
pair.server.connection.mut_ssl().renegotiate(); |
| 110 |
pair.server.send(&[0]); |
| 111 |
assert!(pair.server.connection.ssl().renegotiate_pending()); |
| 112 |
assert!(!pair.io.server_tx_stream.borrow().is_empty()); |
| 113 |
|
| 114 |
|
| 115 |
pair.round_trip_assert(1_024).unwrap(); |
| 116 |
pair.round_trip_assert(1_024).unwrap(); |
| 117 |
|
| 118 |
|
| 119 |
assert!(pair.server.connection.ssl().renegotiate_pending()); |
| 120 |
|
| 121 |
pair.shutdown().unwrap(); |
| 122 |
Ok(()) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#[test] |
| 129 |
fn s2n_client_rejects_openssl_renegotiate_request() -> Result<(), Box<dyn std::error::Error>> { |
| 130 |
let mut configs: TlsConfigBuilderPair<s2n_tls::config::Builder, SslContextBuilder> = |
| 131 |
TlsConfigBuilderPair::default(); |
| 132 |
configs.set_cert(SigType::Ecdsa256); |
| 133 |
configs |
| 134 |
.client |
| 135 |
.set_security_policy(&Policy::from_version(POLICY_WITH_TLS12)?)? |
| 136 |
.set_renegotiate_callback(RenegotiateResponse::Reject)?; |
| 137 |
configs |
| 138 |
.server |
| 139 |
.set_max_proto_version(Some(SslVersion::TLS1_2))?; |
| 140 |
|
| 141 |
let mut pair: TlsConnPair<S2NConnection, OpenSslConnection> = configs.connection_pair(); |
| 142 |
|
| 143 |
pair.handshake()?; |
| 144 |
|
| 145 |
|
| 146 |
pair.server.connection.mut_ssl().renegotiate(); |
| 147 |
pair.server.send(&[0]); |
| 148 |
assert!(pair.server.connection.ssl().renegotiate_pending()); |
| 149 |
assert!(!pair.io.server_tx_stream.borrow().is_empty()); |
| 150 |
|
| 151 |
|
| 152 |
pair.client.recv(&mut [0]).unwrap(); |
| 153 |
|
| 154 |
pair.client.send(&[0]); |
| 155 |
|
| 156 |
let server_error = pair.server.recv(&mut [0]).unwrap_err(); |
| 157 |
assert!(server_error.to_string().contains("no renegotiation")); |
| 158 |
|
| 159 |
pair.client.recv(&mut [0]).unwrap_err(); |
| 160 |
assert_eq!(pair.client.connection().alert(), Some(40)); |
| 161 |
Ok(()) |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
#[test] |
| 168 |
fn s2n_client_renegotiate_with_openssl() -> Result<(), Box<dyn std::error::Error>> { |
| 169 |
let mut configs: TlsConfigBuilderPair<s2n_tls::config::Builder, SslContextBuilder> = |
| 170 |
TlsConfigBuilderPair::default(); |
| 171 |
configs.set_cert(SigType::Ecdsa256); |
| 172 |
configs |
| 173 |
.client |
| 174 |
.set_security_policy(&Policy::from_version(POLICY_WITH_TLS12)?)? |
| 175 |
.set_renegotiate_callback(RenegotiateResponse::Schedule)?; |
| 176 |
configs |
| 177 |
.server |
| 178 |
.set_max_proto_version(Some(SslVersion::TLS1_2))?; |
| 179 |
|
| 180 |
let mut pair: TlsConnPair<S2NConnection, OpenSslConnection> = configs.connection_pair(); |
| 181 |
|
| 182 |
pair.handshake()?; |
| 183 |
pair.round_trip_assert(1_024)?; |
| 184 |
renegotiate_pair(&mut pair, None)?; |
| 185 |
pair.round_trip_assert(1_024)?; |
| 186 |
pair.shutdown()?; |
| 187 |
|
| 188 |
Ok(()) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#[test] |
| 196 |
fn s2n_client_renegotiate_with_client_auth_with_openssl() -> Result<(), Box<dyn std::error::Error>> |
| 197 |
{ |
| 198 |
let mut configs: TlsConfigBuilderPair<s2n_tls::config::Builder, SslContextBuilder> = |
| 199 |
TlsConfigBuilderPair::default(); |
| 200 |
configs.set_cert(SigType::Ecdsa256); |
| 201 |
configs |
| 202 |
.client |
| 203 |
.set_security_policy(&Policy::from_version(POLICY_WITH_TLS12)?)? |
| 204 |
.set_renegotiate_callback(RenegotiateResponse::Schedule)? |
| 205 |
.set_client_auth_type(ClientAuthType::Optional)? |
| 206 |
.load_pem( |
| 207 |
read_to_bytes(PemType::ClientCertChain, SigType::Ecdsa256).as_slice(), |
| 208 |
read_to_bytes(PemType::ClientKey, SigType::Ecdsa256).as_slice(), |
| 209 |
)?; |
| 210 |
configs |
| 211 |
.server |
| 212 |
.set_max_proto_version(Some(SslVersion::TLS1_2))?; |
| 213 |
configs.server.set_trust(SigType::Ecdsa256); |
| 214 |
|
| 215 |
let mut pair: TlsConnPair<S2NConnection, OpenSslConnection> = configs.connection_pair(); |
| 216 |
|
| 217 |
pair.handshake()?; |
| 218 |
assert!(pair.server.connection.ssl().peer_certificate().is_none()); |
| 219 |
|
| 220 |
|
| 221 |
pair.server |
| 222 |
.connection |
| 223 |
.mut_ssl() |
| 224 |
.set_verify(SslVerifyMode::FAIL_IF_NO_PEER_CERT | SslVerifyMode::PEER); |
| 225 |
|
| 226 |
renegotiate_pair(&mut pair, None)?; |
| 227 |
assert!(pair.server.connection.ssl().peer_certificate().is_some()); |
| 228 |
pair.round_trip_assert(1_024)?; |
| 229 |
pair.shutdown()?; |
| 230 |
|
| 231 |
Ok(()) |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
#[test] |
| 236 |
fn s2n_client_renegotiate_with_app_data_with_openssl() -> Result<(), Box<dyn std::error::Error>> { |
| 237 |
let mut configs: TlsConfigBuilderPair<s2n_tls::config::Builder, SslContextBuilder> = |
| 238 |
TlsConfigBuilderPair::default(); |
| 239 |
configs.set_cert(SigType::Ecdsa256); |
| 240 |
configs |
| 241 |
.client |
| 242 |
.set_security_policy(&Policy::from_version(POLICY_WITH_TLS12)?)? |
| 243 |
.set_renegotiate_callback(RenegotiateResponse::Schedule)?; |
| 244 |
|
| 245 |
|
| 246 |
configs |
| 247 |
.server |
| 248 |
.set_max_proto_version(Some(SslVersion::TLS1_2))?; |
| 249 |
|
| 250 |
let mut pair: TlsConnPair<S2NConnection, OpenSslConnection> = configs.connection_pair(); |
| 251 |
pair.handshake()?; |
| 252 |
|
| 253 |
renegotiate_pair(&mut pair, Some(Vec::from(b"some application data")))?; |
| 254 |
pair.round_trip_assert(1_024)?; |
| 255 |
pair.shutdown()?; |
| 256 |
Ok(()) |
| 257 |
} |