shohei 0.5.1

Infrastructure diagnostics library: DNS, DNSSEC, TLS certificate inspection, email security, DNS propagation, and MCP-integrated AI agent support
Documentation
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
//! TLS certificate chain inspection and validation.

use serde::{Deserialize, Serialize};
use std::net::{SocketAddr, IpAddr};
use std::sync::Arc;
use rustls::pki_types::{CertificateDer, ServerName};
use tokio::net::TcpStream;
use tokio::time::{timeout, Duration};
use tokio_rustls::TlsConnector;
use x509_parser::prelude::*;

use crate::error::{Result, ShoheError};
use crate::api::{check_dns, DnsCheckRequest};

/// Inspect TLS certificate chain for a hostname.
pub async fn check_tls_chain(req: &TlsCheckRequest) -> Result<TlsCheckResult> {
    let hostname_str = &req.hostname;
    let port = req.port;

    // Step 1: DNS resolve hostname to IP
    let ip = resolve_hostname_to_ip(hostname_str, req.timeout_secs).await?;

    // Step 2: TLS connect and capture certs
    let tls_result = connect_and_capture_certs(
        ip,
        port,
        hostname_str,
        req.timeout_secs,
    )
    .await;

    let (connected, certs, connection_error) = match tls_result {
        Ok(certs) => (true, certs, None),
        Err(e) => (false, vec![], Some(e.to_string())),
    };

    // Step 3: Parse certs with x509-parser
    let chain = parse_certificate_chain(&certs);

    // Step 4: Get leaf cert expiry
    let (days_until_expiry, expired, expiry_warning) = if !certs.is_empty() {
        match get_expiry_info(&certs[0]) {
            Ok((days, is_expired, is_warning)) => (Some(days), is_expired, is_warning),
            Err(_) => (None, false, false),
        }
    } else {
        (None, false, false)
    };

    // Step 5: Validate certificate (basic check)
    let valid = connected && !certs.is_empty();

    // Step 6: DANE/TLSA check (optional)
    let dane = if req.check_dane && !certs.is_empty() {
        match check_tlsa_records(hostname_str, port, &certs[0], req.timeout_secs).await {
            Ok(result) => Some(result),
            Err(_) => None,
        }
    } else {
        None
    };

    Ok(TlsCheckResult {
        hostname: hostname_str.clone(),
        port,
        connected,
        chain,
        valid,
        days_until_expiry,
        expired,
        expiry_warning,
        connection_error,
        dane,
    })
}

async fn resolve_hostname_to_ip(hostname: &str, timeout_secs: u64) -> Result<IpAddr> {
    let dns_req = DnsCheckRequest {
        domain: hostname.to_string(),
        record_types: vec!["A".to_string()],
        timeout_secs,
        ..Default::default()
    };

    let results = check_dns(&dns_req).await?;
    if results.is_empty() || results[0].answers.is_empty() {
        return Err(ShoheError::DnsResolution(format!(
            "No DNS records for {}",
            hostname
        )));
    }

    // Extract first A record
    use crate::resolver::RecordData;
    for record in &results[0].answers {
        if let RecordData::A(ip_str) = &record.data {
            if let Ok(ip) = ip_str.parse::<std::net::Ipv4Addr>() {
                return Ok(IpAddr::V4(ip));
            }
        }
    }

    Err(ShoheError::DnsResolution(format!(
        "No A records found for {}",
        hostname
    )))
}

async fn connect_and_capture_certs(
    ip: IpAddr,
    port: u16,
    hostname: &str,
    timeout_secs: u64,
) -> Result<Vec<CertificateDer<'static>>> {
    let addr = SocketAddr::new(ip, port);

    let config = rustls::ClientConfig::builder_with_provider(
        Arc::new(rustls::crypto::ring::default_provider()),
    )
    .with_safe_default_protocol_versions()
    .map_err(|e| ShoheError::Transport(e.to_string()))?
    .dangerous()
    .with_custom_certificate_verifier(Arc::new(SkipVerification))
    .with_no_client_auth();

    let connector = TlsConnector::from(Arc::new(config));

    let server_name = ServerName::try_from(hostname.to_string())
        .map_err(|_| ShoheError::Parse(format!("Invalid hostname: {}", hostname)))?;

    let tcp = timeout(
        Duration::from_secs(timeout_secs),
        TcpStream::connect(addr),
    )
    .await
    .map_err(|_| ShoheError::Transport(format!("TCP timeout: {}", addr)))?
    .map_err(|e| ShoheError::Transport(format!("TCP connect failed: {}", e)))?;

    let tls_stream = timeout(
        Duration::from_secs(timeout_secs),
        connector.connect(server_name, tcp),
    )
    .await
    .map_err(|_| ShoheError::Transport("TLS handshake timeout".into()))?
    .map_err(|e| ShoheError::Transport(format!("TLS handshake failed: {}", e)))?;

    let (_, conn) = tls_stream.get_ref();
    let certs = conn
        .peer_certificates()
        .unwrap_or(&[])
        .iter()
        .cloned()
        .collect();

    Ok(certs)
}

// Minimal cert verifier that accepts everything
#[derive(Debug)]
#[allow(dead_code)]
struct SkipVerification;

impl rustls::client::danger::ServerCertVerifier for SkipVerification {
    fn verify_server_cert(
        &self,
        _end_entity: &CertificateDer<'_>,
        _intermediates: &[CertificateDer<'_>],
        _server_name: &ServerName<'_>,
        _ocsp_response: &[u8],
        _now: rustls::pki_types::UnixTime,
    ) -> std::result::Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
        Ok(rustls::client::danger::ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn verify_tls13_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        vec![
            rustls::SignatureScheme::RSA_PKCS1_SHA256,
            rustls::SignatureScheme::ECDSA_NISTP256_SHA256,
            rustls::SignatureScheme::ED25519,
        ]
    }
}

fn parse_certificate_chain(certs: &[CertificateDer<'_>]) -> Vec<CertInfo> {
    certs
        .iter()
        .enumerate()
        .filter_map(|(idx, cert_der)| {
            match X509Certificate::from_der(cert_der.as_ref()) {
                Ok((_, cert)) => {
                    let is_ca = cert.is_ca();
                    Some(CertInfo {
                        subject_cn: extract_common_name(cert.subject()),
                        subject_san: extract_san(&cert),
                        issuer_cn: extract_common_name(cert.issuer()),
                        not_before: format_time(cert.validity().not_before.to_datetime()),
                        not_after: format_time(cert.validity().not_after.to_datetime()),
                        is_ca,
                        serial: format!("{:x}", cert.serial),
                        is_leaf: idx == 0,
                    })
                }
                Err(_) => None,
            }
        })
        .collect()
}

fn extract_common_name(dn: &X509Name) -> Option<String> {
    dn.iter_common_name()
        .next()
        .and_then(|attr| attr.as_str().ok())
        .map(|s| s.to_string())
}

fn extract_san(cert: &X509Certificate) -> Vec<String> {
    let mut sans = Vec::new();

    if let Ok(Some(ext)) = cert.subject_alternative_name() {
        for general_name in &ext.value.general_names {
            match general_name {
                GeneralName::DNSName(name) => {
                    sans.push(format!("DNS:{}", name));
                }
                GeneralName::IPAddress(ip_bytes) => {
                    if ip_bytes.len() == 4 {
                        sans.push(format!(
                            "IP:{}.{}.{}.{}",
                            ip_bytes[0], ip_bytes[1], ip_bytes[2], ip_bytes[3]
                        ));
                    } else if ip_bytes.len() == 16 {
                        let addr = std::net::Ipv6Addr::new(
                            u16::from_be_bytes([ip_bytes[0], ip_bytes[1]]),
                            u16::from_be_bytes([ip_bytes[2], ip_bytes[3]]),
                            u16::from_be_bytes([ip_bytes[4], ip_bytes[5]]),
                            u16::from_be_bytes([ip_bytes[6], ip_bytes[7]]),
                            u16::from_be_bytes([ip_bytes[8], ip_bytes[9]]),
                            u16::from_be_bytes([ip_bytes[10], ip_bytes[11]]),
                            u16::from_be_bytes([ip_bytes[12], ip_bytes[13]]),
                            u16::from_be_bytes([ip_bytes[14], ip_bytes[15]]),
                        );
                        sans.push(format!("IP:{}", addr));
                    }
                }
                _ => {}
            }
        }
    }

    sans
}

fn format_time(dt: impl std::fmt::Display) -> String {
    dt.to_string()
}

fn get_expiry_info(cert_der: &CertificateDer<'_>) -> Result<(i64, bool, bool)> {
    let (_, cert) = X509Certificate::from_der(cert_der.as_ref())
        .map_err(|e| ShoheError::Parse(format!("Failed to parse cert: {}", e)))?;

    let expiry = cert.validity().not_after.to_datetime();

    // Get current time manually since x509_parser's time doesn't expose Unix now
    let now_timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs() as i64;

    let expiry_timestamp = expiry.unix_timestamp();
    let days = (expiry_timestamp - now_timestamp) / 86400;

    let is_expired = days < 0;
    let is_warning = days >= 0 && days < 30;

    Ok((days, is_expired, is_warning))
}


async fn check_tlsa_records(
    hostname: &str,
    port: u16,
    leaf_cert: &CertificateDer<'_>,
    timeout_secs: u64,
) -> Result<DaneTlsaResult> {
    let tlsa_domain = format!("_{}._{}.{}", port, "tcp", hostname);

    let dns_req = DnsCheckRequest {
        domain: tlsa_domain,
        record_types: vec!["TLSA".to_string()],
        timeout_secs,
        ..Default::default()
    };

    let results = check_dns(&dns_req).await.unwrap_or_default();
    let mut records = Vec::new();
    let mut match_found = false;

    for result in results {
        for record in &result.answers {
            use crate::resolver::RecordData;
            if let RecordData::Tlsa {
                usage,
                selector,
                matching_type,
                cert_data,
            } = &record.data
            {
                records.push(format!(
                    "{}; {}; {}; {}",
                    usage, selector, matching_type, cert_data
                ));

                // Check if this TLSA matches our leaf cert
                if check_tlsa_match(leaf_cert, selector, matching_type, cert_data) {
                    match_found = true;
                }
            }
        }
    }

    Ok(DaneTlsaResult {
        records,
        match_found,
    })
}

fn check_tlsa_match(cert_der: &CertificateDer<'_>, selector: &u8, matching_type: &u8, expected: &str) -> bool {
    use sha2::{Digest, Sha256, Sha512};

    // Step 1: Extract the data to hash based on selector
    // selector 0: full certificate DER
    // selector 1: SubjectPublicKeyInfo DER
    let data = match selector {
        0 => cert_der.as_ref().to_vec(),
        1 => {
            // Extract SPKI from certificate
            if let Ok((_, cert)) = X509Certificate::from_der(cert_der.as_ref()) {
                cert.public_key().raw.to_vec()
            } else {
                return false;
            }
        }
        _ => return false,
    };

    // Step 2: Hash or compare based on matching_type
    match matching_type {
        0 => {
            // Exact match - compare raw bytes as hex
            hex::encode(&data) == *expected
        }
        1 => {
            // SHA-256 match
            let digest = Sha256::digest(&data);
            hex::encode(digest) == *expected
        }
        2 => {
            // SHA-512 match
            let digest = Sha512::digest(&data);
            hex::encode(digest) == *expected
        }
        _ => false,
    }
}

// ── Request/Response Types ─────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsCheckRequest {
    pub hostname: String,
    #[serde(default = "default_port")]
    pub port: u16,
    #[serde(default)]
    pub check_dane: bool,
    #[serde(default = "default_timeout")]
    pub timeout_secs: u64,
}

fn default_port() -> u16 { 443 }
fn default_timeout() -> u64 { 10 }

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsCheckResult {
    pub hostname: String,
    pub port: u16,
    pub connected: bool,
    pub chain: Vec<CertInfo>,
    pub valid: bool,
    pub days_until_expiry: Option<i64>,
    pub expired: bool,
    pub expiry_warning: bool,
    pub connection_error: Option<String>,
    pub dane: Option<DaneTlsaResult>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CertInfo {
    pub subject_cn: Option<String>,
    pub subject_san: Vec<String>,
    pub issuer_cn: Option<String>,
    pub not_before: String,
    pub not_after: String,
    pub is_ca: bool,
    pub serial: String,
    pub is_leaf: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DaneTlsaResult {
    pub records: Vec<String>,
    pub match_found: bool,
}