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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Database Module
// Complete database abstraction layer for CipherRun

pub mod analytics;
pub mod config;
pub mod connection;
pub mod migrations;
pub mod models;
pub mod repositories;
pub mod traits;

// Re-exports
pub use config::{Config, DatabaseConfig, DatabaseType, RetentionConfig};
pub use connection::{BindValue, DatabasePool, QueryBuilder};
pub use migrations::{revert_migration, run_migrations};
pub use models::*;
pub use traits::*;

use crate::scanner::ScanResults;
use repositories::ScanRepositoryImpl;

/// Main database struct
pub struct CipherRunDatabase {
    pool: DatabasePool,
    scan_repo: ScanRepositoryImpl,
}

impl CipherRunDatabase {
    /// Create new database instance
    pub async fn new(config: &DatabaseConfig) -> crate::Result<Self> {
        let pool = DatabasePool::new(config).await?;

        // Run migrations
        run_migrations(&pool).await?;

        let scan_repo = ScanRepositoryImpl::new(pool.clone());

        Ok(Self { pool, scan_repo })
    }

    /// Create database from config file
    pub async fn from_config_file(path: &str) -> crate::Result<Self> {
        let config = DatabaseConfig::from_file(path)?;
        Self::new(&config.database).await
    }

    /// Get database pool
    pub fn pool(&self) -> &DatabasePool {
        &self.pool
    }

    /// Close database connection
    pub async fn close(self) {
        self.pool.close().await;
    }

    /// Store complete scan results
    pub async fn store_scan(&self, results: &ScanResults) -> crate::Result<i64> {
        use crate::db::models::*;

        // Parse target
        let parts: Vec<&str> = results.target.split(':').collect();
        let hostname = parts.first().unwrap_or(&"unknown").to_string();
        let port: u16 = parts.get(1).and_then(|p| p.parse().ok()).unwrap_or(443);

        // Create scan record
        let mut scan = ScanRecord::new(hostname, port);

        if let Some(rating) = results.ssl_rating() {
            scan = scan.with_rating(rating.grade.to_string(), rating.score);
        }

        scan = scan.with_duration(results.scan_time_ms);

        // Insert scan
        let scan_id = self.scan_repo.create_scan(&scan).await?;

        // Store protocols
        self.store_protocols(scan_id, results).await?;

        // Store ciphers
        self.store_ciphers(scan_id, results).await?;

        // Store vulnerabilities
        self.store_vulnerabilities(scan_id, results).await?;

        // Store ratings
        if let Some(rating) = results.ssl_rating() {
            self.store_ratings(scan_id, rating).await?;
        }

        // Store certificate chain
        if let Some(cert_data) = &results.certificate_chain {
            self.store_certificates(scan_id, cert_data).await?;
        }

        Ok(scan_id)
    }

    /// Store protocols for a scan
    ///
    /// Performance optimization: Uses batch INSERT with multiple VALUES clauses
    /// instead of N individual queries. This reduces round-trips to the database.
    ///
    /// Time complexity: O(1) database round-trip instead of O(n)
    /// Expected improvement: 10-100x faster for n>10 protocols
    async fn store_protocols(&self, scan_id: i64, results: &ScanResults) -> crate::Result<()> {
        use crate::db::models::ProtocolRecord;

        if results.protocols.is_empty() {
            return Ok(());
        }

        // Build single batch INSERT with multiple VALUES
        let mut qb = self.pool.query_builder();
        let columns = &["scan_id", "protocol_name", "enabled", "preferred"];

        // Collect all bind values
        let mut all_bindings = Vec::new();
        for protocol_result in &results.protocols {
            let protocol = ProtocolRecord::new(
                scan_id,
                protocol_result.protocol.name().to_string(),
                protocol_result.supported,
                protocol_result.preferred,
            );

            all_bindings.push(vec![
                BindValue::Int64(protocol.scan_id),
                BindValue::String(protocol.protocol_name),
                BindValue::Bool(protocol.enabled),
                BindValue::Bool(protocol.preferred),
            ]);
        }

        // Build batch query
        let query = qb.batch_insert_query("protocols", columns, all_bindings.len());

        // Flatten bindings for execution
        let flat_bindings: Vec<BindValue> = all_bindings.into_iter().flatten().collect();

        self.pool
            .execute(&query, flat_bindings)
            .await
            .map_err(|e| {
                crate::TlsError::DatabaseError(format!("Failed to batch insert protocols: {}", e))
            })?;

        Ok(())
    }

    /// Store ciphers for a scan
    ///
    /// Performance optimization: Uses batch INSERT with multiple VALUES clauses
    /// instead of N individual queries. This dramatically improves performance
    /// when storing large cipher lists.
    ///
    /// Time complexity: O(1) database round-trip instead of O(n*m)
    /// Expected improvement: 50-500x faster for typical cipher counts (200-500 ciphers)
    async fn store_ciphers(&self, scan_id: i64, results: &ScanResults) -> crate::Result<()> {
        let total_ciphers: usize = results
            .ciphers
            .values()
            .map(|s| s.supported_ciphers.len())
            .sum();

        if total_ciphers == 0 {
            return Ok(());
        }

        let columns = &[
            "scan_id",
            "protocol_name",
            "cipher_name",
            "key_exchange",
            "authentication",
            "encryption",
            "mac",
            "bits",
            "forward_secrecy",
            "strength",
        ];

        let mut all_bindings = Vec::with_capacity(total_ciphers);

        for (protocol, summary) in &results.ciphers {
            let protocol_name = protocol.name();
            for cipher_result in &summary.supported_ciphers {
                let strength = match cipher_result.strength() {
                    crate::ciphers::CipherStrength::NULL => "null",
                    crate::ciphers::CipherStrength::Export => "export",
                    crate::ciphers::CipherStrength::Low => "low",
                    crate::ciphers::CipherStrength::Medium => "medium",
                    crate::ciphers::CipherStrength::High => "high",
                };

                all_bindings.push(vec![
                    BindValue::Int64(scan_id),
                    BindValue::String(protocol_name.to_string()),
                    BindValue::String(cipher_result.iana_name.clone()),
                    BindValue::OptString(Some(cipher_result.key_exchange.clone())),
                    BindValue::OptString(Some(cipher_result.authentication.clone())),
                    BindValue::OptString(Some(cipher_result.encryption.clone())),
                    BindValue::OptString(Some(cipher_result.mac.clone())),
                    BindValue::OptInt32(Some(cipher_result.bits as i32)),
                    BindValue::Bool(cipher_result.has_forward_secrecy()),
                    BindValue::String(strength.to_string()),
                ]);
            }
        }

        let mut qb = self.pool.query_builder();
        let query = qb.batch_insert_query("cipher_suites", columns, all_bindings.len());

        let flat_bindings: Vec<BindValue> = all_bindings.into_iter().flatten().collect();

        self.pool
            .execute(&query, flat_bindings)
            .await
            .map_err(|e| {
                crate::TlsError::DatabaseError(format!("Failed to batch insert ciphers: {}", e))
            })?;

        Ok(())
    }

    /// Store vulnerabilities for a scan
    async fn store_vulnerabilities(
        &self,
        scan_id: i64,
        results: &ScanResults,
    ) -> crate::Result<()> {
        let mut qb = self.pool.query_builder();
        let query = qb.insert_query(
            "vulnerabilities",
            &[
                "scan_id",
                "vulnerability_type",
                "severity",
                "description",
                "cve_id",
                "affected_component",
            ],
        );

        for vuln_result in &results.vulnerabilities {
            if !vuln_result.vulnerable {
                continue;
            }

            let severity = match vuln_result.severity {
                crate::vulnerabilities::Severity::Critical => "critical",
                crate::vulnerabilities::Severity::High => "high",
                crate::vulnerabilities::Severity::Medium => "medium",
                crate::vulnerabilities::Severity::Low => "low",
                crate::vulnerabilities::Severity::Info => "info",
            };

            self.pool
                .execute(
                    &query,
                    vec![
                        BindValue::Int64(scan_id),
                        BindValue::String(format!("{:?}", vuln_result.vuln_type)),
                        BindValue::String(severity.to_string()),
                        BindValue::OptString(Some(vuln_result.details.clone())),
                        BindValue::OptString(vuln_result.cve.clone()),
                        BindValue::OptString(None),
                    ],
                )
                .await
                .map_err(|e| {
                    crate::TlsError::DatabaseError(format!("Failed to insert vulnerability: {}", e))
                })?;
        }

        Ok(())
    }

    /// Store rating components for a scan
    async fn store_ratings(
        &self,
        scan_id: i64,
        rating: &crate::rating::RatingResult,
    ) -> crate::Result<()> {
        use crate::db::models::RatingRecord;

        let mut qb = self.pool.query_builder();
        let query = qb.insert_query(
            "ratings",
            &["scan_id", "category", "score", "grade", "rationale"],
        );

        let ratings = vec![
            RatingRecord::new(scan_id, "certificate".to_string(), rating.certificate_score),
            RatingRecord::new(scan_id, "protocol".to_string(), rating.protocol_score),
            RatingRecord::new(
                scan_id,
                "key_exchange".to_string(),
                rating.key_exchange_score,
            ),
            RatingRecord::new(scan_id, "cipher".to_string(), rating.cipher_strength_score),
        ];

        for rating_record in ratings {
            self.pool
                .execute(
                    &query,
                    vec![
                        BindValue::Int64(rating_record.scan_id),
                        BindValue::String(rating_record.category),
                        BindValue::Int32(rating_record.score),
                        BindValue::OptString(rating_record.grade),
                        BindValue::OptString(rating_record.rationale),
                    ],
                )
                .await
                .map_err(|e| {
                    crate::TlsError::DatabaseError(format!("Failed to insert rating: {}", e))
                })?;
        }

        Ok(())
    }

    /// Store certificate chain for a scan
    async fn store_certificates(
        &self,
        scan_id: i64,
        cert_data: &crate::scanner::CertificateAnalysisResult,
    ) -> crate::Result<()> {
        use chrono::{DateTime, Utc};

        for (position, cert_info) in cert_data.chain.certificates.iter().enumerate() {
            let fingerprint = cert_info
                .fingerprint_sha256
                .as_deref()
                .map(String::from)
                .unwrap_or_else(|| format!("unknown_{}", position));

            let not_before = DateTime::parse_from_rfc3339(&cert_info.not_before)
                .ok()
                .map(|dt| dt.with_timezone(&Utc))
                .unwrap_or_else(Utc::now);

            let not_after = DateTime::parse_from_rfc3339(&cert_info.not_after)
                .ok()
                .map(|dt| dt.with_timezone(&Utc))
                .unwrap_or_else(Utc::now);

            let cert_id = self
                .insert_or_get_certificate_direct(
                    &fingerprint,
                    &cert_info.subject,
                    &cert_info.issuer,
                    Some(&cert_info.serial_number),
                    not_before,
                    not_after,
                    Some(&cert_info.signature_algorithm),
                    Some(&cert_info.public_key_algorithm),
                    cert_info.public_key_size.map(|s| s as i32),
                    &cert_info.san,
                    cert_info.is_ca,
                    &cert_info.key_usage,
                    &cert_info.extended_key_usage,
                    Some(&cert_info.der_bytes),
                )
                .await?;

            self.link_certificate(scan_id, cert_id, position as i32)
                .await?;
        }

        Ok(())
    }

    /// Insert certificate or get existing ID by fingerprint (direct binding version)
    ///
    /// Note: Many parameters are required to fully represent an X.509 certificate.
    /// Using a struct would add complexity without benefit for this internal function.
    #[allow(clippy::too_many_arguments)]
    async fn insert_or_get_certificate_direct(
        &self,
        fingerprint: &str,
        subject: &str,
        issuer: &str,
        serial_number: Option<&str>,
        not_before: chrono::DateTime<chrono::Utc>,
        not_after: chrono::DateTime<chrono::Utc>,
        signature_algorithm: Option<&str>,
        public_key_algorithm: Option<&str>,
        public_key_size: Option<i32>,
        san_domains: &[String],
        is_ca: bool,
        key_usage: &[String],
        extended_key_usage: &[String],
        der_bytes: Option<&[u8]>,
    ) -> crate::Result<i64> {
        let mut qb = self.pool.query_builder();
        let select_query = qb.select_where_query("certificates", "cert_id", "fingerprint_sha256");

        if let Some(existing_id) = self
            .pool
            .fetch_optional_id(
                &select_query,
                vec![BindValue::String(fingerprint.to_string())],
            )
            .await?
        {
            return Ok(existing_id);
        }

        let san_json = serde_json::to_string(san_domains).unwrap_or_default();
        let key_usage_json = serde_json::to_string(key_usage).unwrap_or_default();
        let extended_key_usage_json = serde_json::to_string(extended_key_usage).unwrap_or_default();

        let mut qb = self.pool.query_builder();
        let insert_query = qb.insert_returning_query(
            "certificates",
            &[
                "fingerprint_sha256",
                "subject",
                "issuer",
                "serial_number",
                "not_before",
                "not_after",
                "signature_algorithm",
                "public_key_algorithm",
                "public_key_size",
                "san_domains",
                "is_ca",
                "key_usage",
                "extended_key_usage",
                "der_bytes",
            ],
            "cert_id",
        );

        let bindings = vec![
            BindValue::String(fingerprint.to_string()),
            BindValue::String(subject.to_string()),
            BindValue::String(issuer.to_string()),
            BindValue::OptString(serial_number.map(String::from)),
            BindValue::DateTime(not_before),
            BindValue::DateTime(not_after),
            BindValue::OptString(signature_algorithm.map(String::from)),
            BindValue::OptString(public_key_algorithm.map(String::from)),
            BindValue::OptInt32(public_key_size),
            BindValue::String(san_json),
            BindValue::Bool(is_ca),
            BindValue::String(key_usage_json),
            BindValue::String(extended_key_usage_json),
            BindValue::OptBytes(der_bytes.map(Vec::from)),
        ];

        self.pool
            .execute_insert_returning(&insert_query, bindings)
            .await
            .map_err(|e| {
                crate::TlsError::DatabaseError(format!("Failed to insert certificate: {}", e))
            })
    }

    /// Link certificate to scan
    async fn link_certificate(
        &self,
        scan_id: i64,
        cert_id: i64,
        position: i32,
    ) -> crate::Result<()> {
        let mut qb = self.pool.query_builder();
        let query = qb.insert_query(
            "scan_certificates",
            &["scan_id", "cert_id", "chain_position"],
        );

        self.pool
            .execute(
                &query,
                vec![
                    BindValue::Int64(scan_id),
                    BindValue::Int64(cert_id),
                    BindValue::Int32(position),
                ],
            )
            .await
            .map_err(|e| {
                crate::TlsError::DatabaseError(format!("Failed to link certificate: {}", e))
            })
    }

    /// Get scan history for a hostname
    pub async fn get_scan_history(
        &self,
        hostname: &str,
        port: u16,
        limit: i64,
    ) -> crate::Result<Vec<ScanRecord>> {
        self.scan_repo
            .get_scans_by_hostname(hostname, port, limit)
            .await
    }

    /// Get latest scan for a hostname
    pub async fn get_latest_scan(
        &self,
        hostname: &str,
        port: u16,
    ) -> crate::Result<Option<ScanRecord>> {
        self.scan_repo.get_latest_scan(hostname, port).await
    }

    /// Cleanup old scans based on retention policy
    pub async fn cleanup_old_scans(&self, days: i64) -> crate::Result<u64> {
        self.scan_repo.delete_old_scans(days).await
    }
}

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

    #[tokio::test]
    async fn test_database_creation() {
        let config = DatabaseConfig::sqlite(PathBuf::from(":memory:"));
        let db = CipherRunDatabase::new(&config)
            .await
            .expect("test assertion should succeed");

        // Verify database was created
        assert!(matches!(db.pool.db_type(), DatabaseType::Sqlite));

        db.close().await;
    }
}