cipherrun 0.3.0

A fast, modular, and scalable TLS/SSL security scanner written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// CipherRun - Conservative Aggregation for Multi-IP Scans
// Copyright (C) 2024 Marc Rivero López
// Licensed under GPL-3.0

//! Conservative aggregation module for combining scan results from multiple IPs.
//!
//! This module implements a worst-case aggregation strategy:
//! - Protocols: Only marked as supported if ALL IPs support them
//! - Cipher suites: Union of all cipher suites across all IPs
//! - Grade: Takes the WORST (lowest) grade from all IPs
//! - Certificates: Most common certificate, or marks differences
//!
//! This conservative approach ensures that the aggregated result represents
//! the weakest security posture in a load-balanced environment.

use crate::certificates::parser::CertificateInfo;
use crate::ciphers::tester::ProtocolCipherSummary;
use crate::protocols::{Protocol, ProtocolTestResult};
use crate::scanner::inconsistency::{Inconsistency, SingleIpScanResult};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::net::IpAddr;

/// Aggregated scan result representing the conservative view across all IPs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AggregatedScanResult {
    /// Protocol test results (only protocols supported by ALL IPs)
    pub protocols: Vec<ProtocolTestResult>,

    /// Cipher suites (union of all ciphers across all IPs)
    pub ciphers: HashMap<Protocol, ProtocolCipherSummary>,

    /// Overall grade (WORST grade from all IPs)
    pub grade: (String, u8),

    /// Most common certificate (or indication of differences)
    pub certificate_info: Option<CertificateInfo>,
    pub certificate_consistent: bool,

    /// List of detected inconsistencies
    pub inconsistencies: Vec<Inconsistency>,

    /// ALPN protocols (intersection - only those supported by all)
    pub alpn_protocols: Vec<String>,

    /// Session resumption (conservative - only if all support it)
    pub session_resumption_caching: bool,
    pub session_resumption_tickets: bool,
}

/// Conservative aggregator for multi-IP scan results
pub struct ConservativeAggregator {
    results: HashMap<IpAddr, SingleIpScanResult>,
    inconsistencies: Vec<Inconsistency>,
}

impl ConservativeAggregator {
    /// Create a new conservative aggregator
    pub fn new(
        results: HashMap<IpAddr, SingleIpScanResult>,
        inconsistencies: Vec<Inconsistency>,
    ) -> Self {
        Self {
            results,
            inconsistencies,
        }
    }

    /// Aggregate all results using conservative strategy
    pub fn aggregate(&self) -> AggregatedScanResult {
        AggregatedScanResult {
            protocols: self.aggregate_protocols_conservative(),
            ciphers: self.aggregate_ciphers_conservative(),
            grade: self.aggregate_grade_conservative(),
            certificate_info: self.aggregate_certificate(),
            certificate_consistent: self.check_certificate_consistency(),
            inconsistencies: self.inconsistencies.clone(),
            alpn_protocols: self.aggregate_alpn_conservative(),
            session_resumption_caching: self.aggregate_session_resumption_caching(),
            session_resumption_tickets: self.aggregate_session_resumption_tickets(),
        }
    }

    /// Aggregate protocols conservatively - only protocols supported by ALL IPs
    fn aggregate_protocols_conservative(&self) -> Vec<ProtocolTestResult> {
        let mut aggregated = Vec::new();

        // Get all successful results
        let successful_results: Vec<_> = self
            .results
            .values()
            .filter(|r| r.error.is_none())
            .collect();

        if successful_results.is_empty() {
            return aggregated;
        }

        // For each protocol, check if ALL IPs support it
        for protocol in Protocol::all() {
            let all_support = successful_results.iter().all(|result| {
                result
                    .scan_result
                    .protocols
                    .iter()
                    .any(|p| p.protocol == protocol && p.supported)
            });

            // Take the best protocol test result details if supported by all
            if all_support {
                // Find the first result for this protocol to use as template
                if let Some(first_result) = successful_results.first()
                    && let Some(protocol_result) = first_result
                        .scan_result
                        .protocols
                        .iter()
                        .find(|p| p.protocol == protocol)
                {
                    aggregated.push(protocol_result.clone());
                }
            } else {
                // Add as unsupported
                aggregated.push(ProtocolTestResult {
                    protocol,
                    supported: false,
                    preferred: false,
                    ciphers_count: 0,
                    handshake_time_ms: None,
                    heartbeat_enabled: None,
                    session_resumption_caching: None,
                    session_resumption_tickets: None,
                    secure_renegotiation: None,
                });
            }
        }

        aggregated
    }

    /// Aggregate cipher suites conservatively - union of all ciphers
    fn aggregate_ciphers_conservative(&self) -> HashMap<Protocol, ProtocolCipherSummary> {
        let mut aggregated: HashMap<Protocol, HashSet<String>> = HashMap::new();

        // Collect all unique cipher suite names per protocol
        for result in self.results.values() {
            if result.error.is_some() {
                continue;
            }

            for (protocol, summary) in &result.scan_result.ciphers {
                let cipher_set = aggregated.entry(*protocol).or_default();
                for cipher in &summary.supported_ciphers {
                    cipher_set.insert(cipher.openssl_name.clone()); // Necessary: HashSet<String>
                }
            }
        }

        // Convert to ProtocolCipherSummary
        let mut result = HashMap::new();
        for (protocol, _cipher_names) in aggregated {
            // Find any summary from the results to use as template
            let template = self
                .results
                .values()
                .filter(|r| r.error.is_none())
                .find_map(|r| r.scan_result.ciphers.get(&protocol));

            if let Some(template_summary) = template {
                // Collect all CipherSuite objects for this protocol
                // Performance optimization: Use references during iteration, clone only unique ciphers
                let mut unique_ciphers = Vec::new();
                let mut seen = HashSet::new();

                for result in self.results.values() {
                    if result.error.is_some() {
                        continue;
                    }
                    if let Some(summary) = result.scan_result.ciphers.get(&protocol) {
                        for cipher in &summary.supported_ciphers {
                            if seen.insert(&cipher.openssl_name) {
                                unique_ciphers.push(cipher.clone()); // Necessary: building owned Vec
                            }
                        }
                    }
                }

                // Create aggregated summary with recalculated cipher counts
                let mut summary = template_summary.clone(); // Necessary: owned result struct
                summary.supported_ciphers = unique_ciphers;

                // Recalculate ALL cipher counts based on aggregated cipher list
                // This matches the logic in CipherTester::calculate_cipher_counts
                use crate::ciphers::CipherStrength;

                summary.counts.total = summary.supported_ciphers.len();
                summary.counts.null_ciphers = 0;
                summary.counts.export_ciphers = 0;
                summary.counts.low_strength = 0;
                summary.counts.medium_strength = 0;
                summary.counts.high_strength = 0;
                summary.counts.forward_secrecy = 0;
                summary.counts.aead = 0;

                for cipher in &summary.supported_ciphers {
                    match cipher.strength() {
                        CipherStrength::NULL => summary.counts.null_ciphers += 1,
                        CipherStrength::Export => summary.counts.export_ciphers += 1,
                        CipherStrength::Low => summary.counts.low_strength += 1,
                        CipherStrength::Medium => summary.counts.medium_strength += 1,
                        CipherStrength::High => summary.counts.high_strength += 1,
                    }

                    if cipher.has_forward_secrecy() {
                        summary.counts.forward_secrecy += 1;
                    }

                    if cipher.is_aead() {
                        summary.counts.aead += 1;
                    }
                }

                result.insert(protocol, summary);
            }
        }

        result
    }

    /// Aggregate grade conservatively - take the WORST grade
    fn aggregate_grade_conservative(&self) -> (String, u8) {
        let mut worst_grade: Option<(String, u8)> = None;

        for result in self.results.values() {
            if result.error.is_some() {
                continue;
            }

            if let Some(rating) = result.scan_result.ssl_rating() {
                let current_grade = (format!("{}", rating.grade), rating.score);

                match worst_grade {
                    None => worst_grade = Some(current_grade),
                    Some((ref _grade, score)) => {
                        if current_grade.1 < score {
                            worst_grade = Some(current_grade);
                        }
                    }
                }
            }
        }

        worst_grade.unwrap_or_else(|| ("F".to_string(), 0))
    }

    /// Aggregate certificate - return most common certificate
    fn aggregate_certificate(&self) -> Option<CertificateInfo> {
        // Performance optimization: Track counts using references, clone only the winner
        let mut cert_counts: HashMap<&str, (&CertificateInfo, usize)> = HashMap::new();

        for result in self.results.values() {
            if result.error.is_some() {
                continue;
            }

            if let Some(ref cert_chain) = result.scan_result.certificate_chain
                && let Some(cert) = cert_chain.chain.leaf()
                && let Some(ref fingerprint) = cert.fingerprint_sha256
            {
                cert_counts
                    .entry(fingerprint.as_str())
                    .and_modify(|(_, count)| *count += 1)
                    .or_insert((cert, 1));
            }
        }

        // Return the most common certificate (clone only once)
        cert_counts
            .into_iter()
            .max_by_key(|(_, (_, count))| *count)
            .map(|(_, (cert, _))| cert.clone())
    }

    /// Check if all backends serve the same certificate
    fn check_certificate_consistency(&self) -> bool {
        // Performance optimization: Use references instead of cloning fingerprints
        let mut fingerprints = HashSet::new();

        for result in self.results.values() {
            if result.error.is_some() {
                continue;
            }

            if let Some(ref cert_chain) = result.scan_result.certificate_chain
                && let Some(cert) = cert_chain.chain.leaf()
                && let Some(ref fingerprint) = cert.fingerprint_sha256
            {
                fingerprints.insert(fingerprint.as_str());
            }
        }

        fingerprints.len() <= 1
    }

    /// Aggregate ALPN protocols conservatively - intersection of all
    fn aggregate_alpn_conservative(&self) -> Vec<String> {
        let successful_results: Vec<_> = self
            .results
            .values()
            .filter(|r| r.error.is_none())
            .collect();

        if successful_results.is_empty() {
            return Vec::new();
        }

        // Performance optimization: Collect ALPN protocols using references
        let mut protocol_sets: Vec<HashSet<&str>> = Vec::new();

        for result in &successful_results {
            if let Some(alpn_report) = result.scan_result.alpn_result()
                && alpn_report.alpn_enabled
            {
                let protocols: HashSet<&str> = alpn_report
                    .alpn_result
                    .supported_protocols
                    .iter()
                    .map(|s| s.as_str())
                    .collect();
                protocol_sets.push(protocols);
            }
        }

        // If no IPs have ALPN enabled, return empty
        if protocol_sets.is_empty() {
            return Vec::new();
        }

        // Find intersection - only protocols supported by ALL IPs
        // Safety: protocol_sets is guaranteed non-empty by check above
        let Some(first_set) = protocol_sets.first() else {
            return Vec::new(); // Unreachable, but explicit for safety
        };
        let intersection: HashSet<&str> = protocol_sets
            .iter()
            .skip(1)
            .fold(first_set.clone(), |acc, set| {
                acc.intersection(set).copied().collect()
            });

        // Convert to sorted vector for consistent output (clone only final strings)
        let mut result: Vec<String> = intersection.into_iter().map(|s| s.to_string()).collect();
        result.sort();
        result
    }

    /// Aggregate session resumption (caching) - only if ALL support it
    fn aggregate_session_resumption_caching(&self) -> bool {
        let successful_results: Vec<_> = self
            .results
            .values()
            .filter(|r| r.error.is_none())
            .collect();

        if successful_results.is_empty() {
            return false;
        }

        successful_results.iter().all(|result| {
            result
                .scan_result
                .protocols
                .iter()
                .any(|p| p.session_resumption_caching == Some(true))
        })
    }

    /// Aggregate session resumption (tickets) - only if ALL support it
    fn aggregate_session_resumption_tickets(&self) -> bool {
        let successful_results: Vec<_> = self
            .results
            .values()
            .filter(|r| r.error.is_none())
            .collect();

        if successful_results.is_empty() {
            return false;
        }

        successful_results.iter().all(|result| {
            result
                .scan_result
                .protocols
                .iter()
                .any(|p| p.session_resumption_tickets == Some(true))
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_aggregation() {
        let aggregator = ConservativeAggregator::new(HashMap::new(), Vec::new());
        let result = aggregator.aggregate();
        assert!(result.protocols.is_empty());
        assert_eq!(result.grade, ("F".to_string(), 0));
    }

    #[test]
    fn test_certificate_consistency_single_cert() {
        let aggregator = ConservativeAggregator::new(HashMap::new(), Vec::new());
        assert!(aggregator.check_certificate_consistency());
    }
}