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
// ASN and CIDR Input Support Module
// Supports ASN (Autonomous System Number) and CIDR notation as input

use crate::Result;
use crate::error::TlsError;
use ipnetwork::IpNetwork;
use std::net::IpAddr;

/// ASN and CIDR parser
pub struct AsnCidrParser;

impl AsnCidrParser {
    /// Parse ASN and expand to IP ranges
    /// Format: AS1449 or 1449
    pub async fn expand_asn(asn: &str) -> Result<Vec<IpNetwork>> {
        let asn_number = Self::parse_asn_number(asn)?;

        // Query BGP tables via RIPEstat API
        let prefixes = Self::query_ripestat_api(asn_number).await?;

        Ok(prefixes)
    }

    /// Parse ASN number from string (supports "AS1449" or "1449")
    fn parse_asn_number(asn: &str) -> Result<u32> {
        let asn_str = asn.trim();

        // Remove "AS" prefix if present
        let num_str = if asn_str.to_uppercase().starts_with("AS") {
            &asn_str[2..]
        } else {
            asn_str
        };

        num_str.parse::<u32>().map_err(|e| TlsError::InvalidInput {
            message: format!("Invalid ASN format '{}': {}", asn, e),
        })
    }

    /// Query RIPEstat API for ASN prefixes
    async fn query_ripestat_api(asn: u32) -> Result<Vec<IpNetwork>> {
        let url = format!(
            "https://stat.ripe.net/data/announced-prefixes/data.json?resource=AS{}",
            asn
        );

        let client = reqwest::Client::builder()
            .user_agent("CipherRun/1.0")
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .map_err(|e| TlsError::ConfigError {
                message: format!("Failed to create HTTP client: {}", e),
            })?;

        let response = client
            .get(&url)
            .send()
            .await
            .map_err(|e| TlsError::HttpError {
                status: 0,
                details: format!("RIPEstat API request failed: {}", e),
            })?;

        if !response.status().is_success() {
            return Err(TlsError::HttpError {
                status: response.status().as_u16(),
                details: format!("RIPEstat API returned error: {}", response.status()),
            });
        }

        let json: serde_json::Value = response.json().await.map_err(|e| TlsError::ParseError {
            message: format!("Failed to parse RIPEstat response: {}", e),
        })?;

        // Parse prefixes from JSON response
        let mut prefixes = Vec::new();

        if let Some(data) = json.get("data")
            && let Some(prefixes_array) = data.get("prefixes").and_then(|p| p.as_array())
        {
            for prefix_obj in prefixes_array {
                if let Some(prefix_str) = prefix_obj.get("prefix").and_then(|p| p.as_str())
                    && let Ok(network) = prefix_str.parse::<IpNetwork>()
                {
                    prefixes.push(network);
                }
            }
        }

        if prefixes.is_empty() {
            return Err(TlsError::InvalidInput {
                message: format!("No prefixes found for AS{}", asn),
            });
        }

        Ok(prefixes)
    }

    /// Expand CIDR notation to individual IPs
    /// For large ranges, returns the network object instead of all IPs
    pub fn expand_cidr(cidr: &str) -> Result<CidrExpansion> {
        let network = cidr
            .parse::<IpNetwork>()
            .map_err(|e| TlsError::InvalidInput {
                message: format!("Invalid CIDR format '{}': {}", cidr, e),
            })?;

        let total_ips = Self::calculate_ip_count(&network);

        // For large networks, don't expand all IPs (memory-efficient)
        const MAX_EXPAND: u64 = 1024; // Expand up to /22 for IPv4, /118 for IPv6

        if total_ips <= MAX_EXPAND {
            // Small network - expand all IPs
            let ips: Vec<IpAddr> = network.iter().collect();
            Ok(CidrExpansion::FullList {
                network,
                ips,
                total: total_ips,
            })
        } else {
            // Large network - return iterator-based expansion
            Ok(CidrExpansion::Network {
                network,
                total: total_ips,
            })
        }
    }

    /// Calculate total IP count for a network
    fn calculate_ip_count(network: &IpNetwork) -> u64 {
        match network {
            IpNetwork::V4(v4) => {
                let prefix_len = v4.prefix();
                if prefix_len >= 32 {
                    1
                } else {
                    2u64.pow(32 - prefix_len as u32)
                }
            }
            IpNetwork::V6(v6) => {
                let prefix_len = v6.prefix();
                if prefix_len >= 128 {
                    1
                } else if prefix_len <= 64 {
                    // For large IPv6 networks, return a large number
                    u64::MAX
                } else {
                    2u64.pow(128 - prefix_len as u32)
                }
            }
        }
    }

    /// Detect input type from string
    pub fn parse_input(input: &str) -> InputType {
        let input = input.trim();

        // Check for ASN format
        if input.to_uppercase().starts_with("AS") || input.parse::<u32>().is_ok() {
            // Verify it's a valid ASN number
            if let Ok(asn_num) = Self::parse_asn_number(input)
                && asn_num > 0
                && asn_num < 4_294_967_295
            {
                return InputType::Asn(input.to_string());
            }
        }

        // Check for CIDR notation
        if input.contains('/') && input.parse::<IpNetwork>().is_ok() {
            return InputType::Cidr(input.to_string());
        }

        // Check for IP address
        if let Ok(ip) = input.parse::<IpAddr>() {
            return InputType::Ip(ip);
        }

        // Default to hostname
        InputType::Hostname(input.to_string())
    }

    /// Expand multiple inputs (supports mixed types)
    pub async fn expand_inputs(inputs: Vec<String>) -> Result<Vec<ExpandedInput>> {
        let mut results = Vec::new();

        for input in inputs {
            let input_type = Self::parse_input(&input);
            let expanded = Self::expand_input_type(input_type).await?;
            results.push(expanded);
        }

        Ok(results)
    }

    /// Expand a single input type
    async fn expand_input_type(input_type: InputType) -> Result<ExpandedInput> {
        match input_type {
            InputType::Asn(asn) => {
                let networks = Self::expand_asn(&asn).await?;
                Ok(ExpandedInput::Asn {
                    asn: asn.clone(),
                    networks,
                })
            }
            InputType::Cidr(cidr) => {
                let expansion = Self::expand_cidr(&cidr)?;
                Ok(ExpandedInput::Cidr {
                    cidr: cidr.clone(),
                    expansion,
                })
            }
            InputType::Ip(ip) => Ok(ExpandedInput::Ip { ip }),
            InputType::Hostname(hostname) => Ok(ExpandedInput::Hostname { hostname }),
        }
    }
}

/// Input type classification
#[derive(Debug, Clone)]
pub enum InputType {
    Asn(String),
    Cidr(String),
    Ip(IpAddr),
    Hostname(String),
}

/// CIDR expansion result
#[derive(Debug, Clone)]
pub enum CidrExpansion {
    /// Small network with all IPs expanded
    FullList {
        network: IpNetwork,
        ips: Vec<IpAddr>,
        total: u64,
    },
    /// Large network (use iterator for memory efficiency)
    Network { network: IpNetwork, total: u64 },
}

impl CidrExpansion {
    /// Get iterator over all IPs in the network
    pub fn iter(&self) -> Box<dyn Iterator<Item = IpAddr> + '_> {
        match self {
            CidrExpansion::FullList { ips, .. } => Box::new(ips.iter().copied()),
            CidrExpansion::Network { network, .. } => Box::new(network.iter()),
        }
    }

    /// Get total IP count
    pub fn total_ips(&self) -> u64 {
        match self {
            CidrExpansion::FullList { total, .. } => *total,
            CidrExpansion::Network { total, .. } => *total,
        }
    }

    /// Get network
    pub fn network(&self) -> &IpNetwork {
        match self {
            CidrExpansion::FullList { network, .. } => network,
            CidrExpansion::Network { network, .. } => network,
        }
    }
}

/// Expanded input (after processing)
#[derive(Debug, Clone)]
pub enum ExpandedInput {
    Asn {
        asn: String,
        networks: Vec<IpNetwork>,
    },
    Cidr {
        cidr: String,
        expansion: CidrExpansion,
    },
    Ip {
        ip: IpAddr,
    },
    Hostname {
        hostname: String,
    },
}

impl ExpandedInput {
    /// Get total target count
    pub fn target_count(&self) -> u64 {
        match self {
            ExpandedInput::Asn { networks, .. } => networks.iter().map(Self::network_size).sum(),
            ExpandedInput::Cidr { expansion, .. } => expansion.total_ips(),
            ExpandedInput::Ip { .. } => 1,
            ExpandedInput::Hostname { .. } => 1,
        }
    }

    /// Calculate network size
    fn network_size(network: &IpNetwork) -> u64 {
        match network {
            IpNetwork::V4(v4) => {
                let prefix = v4.prefix();
                if prefix >= 32 {
                    1
                } else {
                    2u64.pow(32 - prefix as u32)
                }
            }
            IpNetwork::V6(v6) => {
                let prefix = v6.prefix();
                if prefix >= 128 {
                    1
                } else if prefix <= 64 {
                    u64::MAX
                } else {
                    2u64.pow(128 - prefix as u32)
                }
            }
        }
    }
}

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

    #[test]
    fn test_parse_asn_number() {
        assert_eq!(AsnCidrParser::parse_asn_number("1449").unwrap(), 1449);
        assert_eq!(AsnCidrParser::parse_asn_number("AS1449").unwrap(), 1449);
        assert_eq!(AsnCidrParser::parse_asn_number("as1449").unwrap(), 1449);

        assert!(AsnCidrParser::parse_asn_number("invalid").is_err());
        assert!(AsnCidrParser::parse_asn_number("AS").is_err());
    }

    #[test]
    fn test_parse_input_asn() {
        match AsnCidrParser::parse_input("AS1449") {
            InputType::Asn(asn) => assert_eq!(asn, "AS1449"),
            _ => panic!("Expected ASN input type"),
        }

        match AsnCidrParser::parse_input("1449") {
            InputType::Asn(asn) => assert_eq!(asn, "1449"),
            _ => panic!("Expected ASN input type"),
        }
    }

    #[test]
    fn test_parse_input_cidr() {
        match AsnCidrParser::parse_input("192.0.2.0/24") {
            InputType::Cidr(cidr) => assert_eq!(cidr, "192.0.2.0/24"),
            _ => panic!("Expected CIDR input type"),
        }

        match AsnCidrParser::parse_input("2001:db8::/32") {
            InputType::Cidr(cidr) => assert_eq!(cidr, "2001:db8::/32"),
            _ => panic!("Expected CIDR input type"),
        }
    }

    #[test]
    fn test_parse_input_ip() {
        match AsnCidrParser::parse_input("192.0.2.1") {
            InputType::Ip(ip) => assert_eq!(ip.to_string(), "192.0.2.1"),
            _ => panic!("Expected IP input type"),
        }

        match AsnCidrParser::parse_input("2001:db8::1") {
            InputType::Ip(ip) => assert!(ip.to_string().contains("2001:db8")),
            _ => panic!("Expected IP input type"),
        }
    }

    #[test]
    fn test_parse_input_hostname() {
        match AsnCidrParser::parse_input("example.com") {
            InputType::Hostname(hostname) => assert_eq!(hostname, "example.com"),
            _ => panic!("Expected Hostname input type"),
        }
    }

    #[test]
    fn test_expand_cidr_small() {
        let expansion =
            AsnCidrParser::expand_cidr("192.0.2.0/30").expect("test assertion should succeed");

        match expansion {
            CidrExpansion::FullList { ips, total, .. } => {
                assert_eq!(total, 4);
                assert_eq!(ips.len(), 4);
            }
            _ => panic!("Expected FullList for small network"),
        }
    }

    #[test]
    fn test_expand_cidr_large() {
        let expansion =
            AsnCidrParser::expand_cidr("10.0.0.0/8").expect("test assertion should succeed");

        match expansion {
            CidrExpansion::Network { total, .. } => {
                assert_eq!(total, 16777216); // 2^24
            }
            _ => panic!("Expected Network for large network"),
        }
    }

    #[test]
    fn test_cidr_expansion_iter() {
        let expansion =
            AsnCidrParser::expand_cidr("192.0.2.0/30").expect("test assertion should succeed");
        let ips: Vec<IpAddr> = expansion.iter().collect();

        assert_eq!(ips.len(), 4);
        assert_eq!(ips[0].to_string(), "192.0.2.0");
        assert_eq!(ips[3].to_string(), "192.0.2.3");
    }

    // Note: ASN API tests require network access and are integration tests
}