tlspuffin/tls/
violation.rs

1use itertools::Itertools;
2use puffin::claims::SecurityViolationPolicy;
3use security_claims::ClaimTLSVersion;
4
5use crate::claims::{ClaimData, ClaimDataMessage, Finished, TlsClaim};
6use crate::protocol::AgentType;
7use crate::static_certs::{ALICE_CERT, BOB_CERT};
8
9pub struct TlsSecurityViolationPolicy;
10
11impl SecurityViolationPolicy for TlsSecurityViolationPolicy {
12    type C = TlsClaim;
13
14    fn check_violation(claims: &[TlsClaim]) -> Option<&'static str> {
15        if let Some((claim_a, claim_b)) = find_two_finished_messages(claims) {
16            if let Some(((_, client), (_, server))) = get_client_server(claim_a, claim_b) {
17                if client.tls_version != server.tls_version {
18                    return Some("Mismatching versions");
19                }
20
21                if client.master_secret != server.master_secret {
22                    return Some("Mismatching master secrets");
23                }
24
25                if client.server_random != server.server_random {
26                    return Some("Mismatching server random");
27                }
28                if client.client_random != server.client_random {
29                    return Some("Mismatching client random");
30                }
31
32                if client.chosen_cipher != server.chosen_cipher {
33                    return Some("Mismatching ciphers");
34                }
35
36                if client.signature_algorithm != server.peer_signature_algorithm
37                    || server.signature_algorithm != client.peer_signature_algorithm
38                {
39                    return Some("mismatching signature algorithms");
40                }
41
42                if server.authenticate_peer && server.peer_certificate.as_slice() != BOB_CERT.1 {
43                    return Some("Authentication bypass");
44                }
45
46                if client.authenticate_peer && client.peer_certificate.as_slice() != ALICE_CERT.1 {
47                    return Some("Authentication bypass");
48                }
49
50                match client.tls_version {
51                    ClaimTLSVersion::CLAIM_TLS_VERSION_V1_2 => {
52                        // TLS 1.2 Checks
53
54                        // https://datatracker.ietf.org/doc/html/rfc5077#section-3.4
55                        if !server.session_id.is_empty() && client.session_id != server.session_id {
56                            return Some("Mismatching session ids");
57                        }
58                    }
59                    ClaimTLSVersion::CLAIM_TLS_VERSION_V1_3 => {
60                        // TLS 1.3 Checks
61                        if client.session_id != server.session_id {
62                            return Some("Mismatching session ids");
63                        }
64
65                        if !client.available_ciphers.is_empty()
66                            && !server.available_ciphers.is_empty()
67                        {
68                            let best_cipher = {
69                                let mut cipher = None;
70                                for server_cipher in &server.available_ciphers {
71                                    if client.available_ciphers.contains(server_cipher) {
72                                        cipher = Some(*server_cipher);
73                                        break;
74                                    }
75                                }
76
77                                cipher
78                            };
79
80                            if let Some(best_cipher) = best_cipher {
81                                if best_cipher != server.chosen_cipher {
82                                    return Some("Not the best cipher choosen");
83                                }
84                                if best_cipher != client.chosen_cipher {
85                                    return Some("Not the best cipher choosen");
86                                }
87                            }
88                        }
89                    }
90                    ClaimTLSVersion::CLAIM_TLS_VERSION_UNDEFINED => {
91                        return Some("Version undefined after handshake");
92                    }
93                }
94            } else {
95                // Could not choose exactly one server and client
96                // possibly two server because of session resumption
97            }
98        } else {
99            // this is the case for seed_client_attacker12 which records only the server claims
100
101            let found = claims.iter().find_map(|claim| match &claim.data {
102                ClaimData::Message(ClaimDataMessage::Finished(data)) => {
103                    if data.outbound {
104                        None
105                    } else {
106                        Some((claim, data))
107                    }
108                }
109                _ => None,
110            });
111            if let Some((claim, finished)) = found {
112                if !finished.available_ciphers.is_empty()
113                    && !finished.available_ciphers.contains(&finished.chosen_cipher)
114                {
115                    // available_ciphers is set with the SSL context, it's the list of accepted
116                    // ciphers, chosen_cipher is the negotiated one
117                    return Some("Negotiated cipher is not in agent's configured ciphers list");
118                }
119
120                let violation = finished.authenticate_peer
121                    && match claim.origin {
122                        AgentType::Server => finished.peer_certificate.as_slice() != BOB_CERT.1,
123                        AgentType::Client => finished.peer_certificate.as_slice() != ALICE_CERT.1,
124                    };
125
126                if violation {
127                    return Some("Authentication bypass");
128                }
129            }
130        }
131
132        None
133    }
134}
135
136pub fn find_two_finished_messages(
137    claims: &[TlsClaim],
138) -> Option<((&TlsClaim, &Finished), (&TlsClaim, &Finished))> {
139    let two_finishes: Option<((&TlsClaim, &Finished), (&TlsClaim, &Finished))> = claims
140        .iter()
141        .filter_map(|claim| match &claim.data {
142            ClaimData::Message(ClaimDataMessage::Finished(data)) => {
143                if data.outbound {
144                    None
145                } else {
146                    Some((claim, data))
147                }
148            }
149            _ => None,
150        })
151        .collect_tuple();
152
153    if let Some(((claim_a, _), (claim_b, _))) = two_finishes {
154        if claim_a.agent_name == claim_b.agent_name {
155            // One agent finished twice because of session resumption
156            return None;
157        }
158    }
159
160    two_finishes
161}
162
163pub fn get_client_server<'a, T>(
164    a: (&'a TlsClaim, &'a T),
165    b: (&'a TlsClaim, &'a T),
166) -> Option<((&'a TlsClaim, &'a T), (&'a TlsClaim, &'a T))> {
167    match a.0.origin {
168        AgentType::Server => match b.0.origin {
169            AgentType::Server => None,
170            AgentType::Client => Some((b, a)),
171        },
172        AgentType::Client => match b.0.origin {
173            AgentType::Server => Some((a, b)),
174            AgentType::Client => None,
175        },
176    }
177}