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
// Certificate Change Detector

use crate::certificates::parser::CertificateInfo;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Types of certificate changes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeType {
    NewCertificate,
    Renewal,                  // Same issuer, new serial
    IssuerChange,             // Different issuer
    KeySizeChange,            // Different key size
    SignatureAlgorithmChange, // Different signature algorithm
    SANChange,                // SAN domains changed
    ExpiryExtended,           // Expiry date extended
    ExpiryShortened,          // Expiry date shortened
}

/// Severity of certificate change
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ChangeSeverity {
    Info,     // Routine renewal
    Low,      // Expiry extended
    Medium,   // SAN change
    High,     // Key size change
    Critical, // Issuer change (potential compromise)
}

impl std::fmt::Display for ChangeSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChangeSeverity::Info => write!(f, "Info"),
            ChangeSeverity::Low => write!(f, "Low"),
            ChangeSeverity::Medium => write!(f, "Medium"),
            ChangeSeverity::High => write!(f, "High"),
            ChangeSeverity::Critical => write!(f, "Critical"),
        }
    }
}

/// Certificate change event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeEvent {
    pub change_type: ChangeType,
    pub severity: ChangeSeverity,
    pub description: String,
    pub previous_value: Option<String>,
    pub current_value: Option<String>,
    pub detected_at: DateTime<Utc>,
}

/// Certificate change detector
pub struct ChangeDetector {}

impl ChangeDetector {
    /// Create new change detector
    pub fn new() -> Self {
        Self {}
    }

    /// Detect changes between two certificates
    pub fn detect_changes(
        &self,
        previous: &CertificateInfo,
        current: &CertificateInfo,
    ) -> Vec<ChangeEvent> {
        let mut changes = Vec::new();
        let now = Utc::now();

        // Check serial number change (indicates renewal or replacement)
        if previous.serial_number != current.serial_number {
            // Check if issuer is the same (renewal) or different (issuer change)
            if previous.issuer == current.issuer {
                changes.push(ChangeEvent {
                    change_type: ChangeType::Renewal,
                    severity: ChangeSeverity::Info,
                    description: "Certificate renewed with new serial number".to_string(),
                    previous_value: Some(previous.serial_number.clone()),
                    current_value: Some(current.serial_number.clone()),
                    detected_at: now,
                });
            } else {
                changes.push(ChangeEvent {
                    change_type: ChangeType::IssuerChange,
                    severity: ChangeSeverity::Critical,
                    description: "Certificate issuer changed - possible security compromise"
                        .to_string(),
                    previous_value: Some(previous.issuer.clone()),
                    current_value: Some(current.issuer.clone()),
                    detected_at: now,
                });
            }
        }

        // Check key size change
        if previous.public_key_size != current.public_key_size {
            changes.push(ChangeEvent {
                change_type: ChangeType::KeySizeChange,
                severity: ChangeSeverity::High,
                description: "Public key size changed".to_string(),
                previous_value: Some(format!("{} bits", previous.public_key_size.unwrap_or(0))),
                current_value: Some(format!("{} bits", current.public_key_size.unwrap_or(0))),
                detected_at: now,
            });
        }

        // Check signature algorithm change
        if previous.signature_algorithm != current.signature_algorithm {
            changes.push(ChangeEvent {
                change_type: ChangeType::SignatureAlgorithmChange,
                severity: ChangeSeverity::Medium,
                description: "Signature algorithm changed".to_string(),
                previous_value: Some(previous.signature_algorithm.clone()),
                current_value: Some(current.signature_algorithm.clone()),
                detected_at: now,
            });
        }

        // Check SAN changes
        let prev_sans: std::collections::HashSet<_> = previous.san.iter().collect();
        let curr_sans: std::collections::HashSet<_> = current.san.iter().collect();

        if prev_sans != curr_sans {
            let added: Vec<_> = curr_sans.difference(&prev_sans).collect();
            let removed: Vec<_> = prev_sans.difference(&curr_sans).collect();

            let mut description = String::from("Subject Alternative Names changed");
            if !added.is_empty() {
                description.push_str(&format!(" (added: {})", added.len()));
            }
            if !removed.is_empty() {
                description.push_str(&format!(" (removed: {})", removed.len()));
            }

            changes.push(ChangeEvent {
                change_type: ChangeType::SANChange,
                severity: ChangeSeverity::Medium,
                description,
                previous_value: Some(format!("{} domains", prev_sans.len())),
                current_value: Some(format!("{} domains", curr_sans.len())),
                detected_at: now,
            });
        }

        // Check expiry date changes
        if previous.not_after != current.not_after {
            // Parse dates to determine if extended or shortened
            // For now, do simple string comparison
            let is_extended = current.not_after > previous.not_after;

            if is_extended {
                changes.push(ChangeEvent {
                    change_type: ChangeType::ExpiryExtended,
                    severity: ChangeSeverity::Low,
                    description: "Certificate expiry date extended".to_string(),
                    previous_value: Some(previous.not_after.clone()),
                    current_value: Some(current.not_after.clone()),
                    detected_at: now,
                });
            } else {
                changes.push(ChangeEvent {
                    change_type: ChangeType::ExpiryShortened,
                    severity: ChangeSeverity::Medium,
                    description: "Certificate expiry date shortened".to_string(),
                    previous_value: Some(previous.not_after.clone()),
                    current_value: Some(current.not_after.clone()),
                    detected_at: now,
                });
            }
        }

        changes
    }

    /// Classify severity of a change type
    pub fn classify_severity(&self, change_type: &ChangeType) -> ChangeSeverity {
        match change_type {
            ChangeType::NewCertificate => ChangeSeverity::Info,
            ChangeType::Renewal => ChangeSeverity::Info,
            ChangeType::IssuerChange => ChangeSeverity::Critical,
            ChangeType::KeySizeChange => ChangeSeverity::High,
            ChangeType::SignatureAlgorithmChange => ChangeSeverity::Medium,
            ChangeType::SANChange => ChangeSeverity::Medium,
            ChangeType::ExpiryExtended => ChangeSeverity::Low,
            ChangeType::ExpiryShortened => ChangeSeverity::Medium,
        }
    }

    /// Check if change requires immediate alert
    pub fn requires_immediate_alert(&self, change: &ChangeEvent) -> bool {
        matches!(
            change.severity,
            ChangeSeverity::Critical | ChangeSeverity::High
        )
    }

    /// Get the most severe change from a list
    pub fn most_severe<'a>(&self, changes: &'a [ChangeEvent]) -> Option<&'a ChangeEvent> {
        changes.iter().max_by_key(|c| c.severity)
    }
}

impl Default for ChangeDetector {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn create_test_cert(
        serial: &str,
        issuer: &str,
        key_size: Option<usize>,
        sans: Vec<String>,
    ) -> CertificateInfo {
        CertificateInfo {
            subject: "CN=example.com".to_string(),
            issuer: issuer.to_string(),
            serial_number: serial.to_string(),
            not_before: "2024-01-01 00:00:00 UTC".to_string(),
            not_after: "2025-01-01 00:00:00 UTC".to_string(),
            expiry_countdown: None,
            signature_algorithm: "sha256WithRSAEncryption".to_string(),
            public_key_algorithm: "rsaEncryption".to_string(),
            public_key_size: key_size,
            rsa_exponent: Some("e 65537".to_string()),
            san: sans,
            is_ca: false,
            key_usage: vec![],
            extended_key_usage: vec![],
            extended_validation: false,
            ev_oids: vec![],
            pin_sha256: None,
            fingerprint_sha256: None,
            debian_weak_key: None,
            aia_url: None,
            certificate_transparency: None,
            der_bytes: vec![],
        }
    }

    #[test]
    fn test_detect_renewal() {
        let detector = ChangeDetector::new();

        let previous = create_test_cert("123", "CN=Let's Encrypt", Some(2048), vec![]);
        let current = create_test_cert("456", "CN=Let's Encrypt", Some(2048), vec![]);

        let changes = detector.detect_changes(&previous, &current);

        assert_eq!(changes.len(), 1);
        assert!(matches!(changes[0].change_type, ChangeType::Renewal));
        assert_eq!(changes[0].severity, ChangeSeverity::Info);
    }

    #[test]
    fn test_detect_issuer_change() {
        let detector = ChangeDetector::new();

        let previous = create_test_cert("123", "CN=Let's Encrypt", Some(2048), vec![]);
        let current = create_test_cert("456", "CN=DigiCert", Some(2048), vec![]);

        let changes = detector.detect_changes(&previous, &current);

        assert_eq!(changes.len(), 1);
        assert!(matches!(changes[0].change_type, ChangeType::IssuerChange));
        assert_eq!(changes[0].severity, ChangeSeverity::Critical);
    }

    #[test]
    fn test_detect_key_size_change() {
        let detector = ChangeDetector::new();

        let previous = create_test_cert("123", "CN=Let's Encrypt", Some(2048), vec![]);
        let current = create_test_cert("123", "CN=Let's Encrypt", Some(4096), vec![]);

        let changes = detector.detect_changes(&previous, &current);

        assert_eq!(changes.len(), 1);
        assert!(matches!(changes[0].change_type, ChangeType::KeySizeChange));
        assert_eq!(changes[0].severity, ChangeSeverity::High);
    }

    #[test]
    fn test_detect_san_change() {
        let detector = ChangeDetector::new();

        let previous = create_test_cert(
            "123",
            "CN=Let's Encrypt",
            Some(2048),
            vec!["example.com".to_string(), "www.example.com".to_string()],
        );
        let current = create_test_cert(
            "123",
            "CN=Let's Encrypt",
            Some(2048),
            vec![
                "example.com".to_string(),
                "www.example.com".to_string(),
                "api.example.com".to_string(),
            ],
        );

        let changes = detector.detect_changes(&previous, &current);

        assert_eq!(changes.len(), 1);
        assert!(matches!(changes[0].change_type, ChangeType::SANChange));
        assert_eq!(changes[0].severity, ChangeSeverity::Medium);
    }

    #[test]
    fn test_classify_severity() {
        let detector = ChangeDetector::new();

        assert_eq!(
            detector.classify_severity(&ChangeType::NewCertificate),
            ChangeSeverity::Info
        );
        assert_eq!(
            detector.classify_severity(&ChangeType::Renewal),
            ChangeSeverity::Info
        );
        assert_eq!(
            detector.classify_severity(&ChangeType::IssuerChange),
            ChangeSeverity::Critical
        );
        assert_eq!(
            detector.classify_severity(&ChangeType::KeySizeChange),
            ChangeSeverity::High
        );
        assert_eq!(
            detector.classify_severity(&ChangeType::SANChange),
            ChangeSeverity::Medium
        );
    }

    #[test]
    fn test_requires_immediate_alert() {
        let detector = ChangeDetector::new();

        let critical_event = ChangeEvent {
            change_type: ChangeType::IssuerChange,
            severity: ChangeSeverity::Critical,
            description: "Test".to_string(),
            previous_value: None,
            current_value: None,
            detected_at: Utc::now(),
        };

        let info_event = ChangeEvent {
            change_type: ChangeType::Renewal,
            severity: ChangeSeverity::Info,
            description: "Test".to_string(),
            previous_value: None,
            current_value: None,
            detected_at: Utc::now(),
        };

        assert!(detector.requires_immediate_alert(&critical_event));
        assert!(!detector.requires_immediate_alert(&info_event));
    }

    #[test]
    fn test_most_severe() {
        let detector = ChangeDetector::new();

        let changes = vec![
            ChangeEvent {
                change_type: ChangeType::Renewal,
                severity: ChangeSeverity::Info,
                description: "Test1".to_string(),
                previous_value: None,
                current_value: None,
                detected_at: Utc::now(),
            },
            ChangeEvent {
                change_type: ChangeType::IssuerChange,
                severity: ChangeSeverity::Critical,
                description: "Test2".to_string(),
                previous_value: None,
                current_value: None,
                detected_at: Utc::now(),
            },
            ChangeEvent {
                change_type: ChangeType::SANChange,
                severity: ChangeSeverity::Medium,
                description: "Test3".to_string(),
                previous_value: None,
                current_value: None,
                detected_at: Utc::now(),
            },
        ];

        let most_severe = detector.most_severe(&changes);
        assert!(most_severe.is_some());
        assert_eq!(most_severe.unwrap().severity, ChangeSeverity::Critical);
    }
}