soth-mitm 0.3.1

Rust intercepting proxy crate with deterministic handler/event contracts for SOTH.
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
431
432
433
434
435
436
437
438
439
440
441
    use std::fs;
    use std::path::Path;
    use std::path::PathBuf;
    use std::sync::Arc;
    use std::time::{Duration, SystemTime, UNIX_EPOCH};

    use x509_parser::extensions::GeneralName;
    use x509_parser::parse_x509_certificate;

    use super::{
        build_http1_client_config, build_http1_server_config_for_host,
        build_http_client_config, build_http_client_config_with_policy,
        build_http_client_config_with_policy_and_client_auth, build_http_server_config_for_host,
        classify_tls_error, parse_upstream_client_auth_material, resolve_upstream_server_name,
        CertStoreMetricsSnapshot, CertificateAuthorityConfig, DownstreamCertProfile,
        LeafCacheStatus, MitmCertificateStore, TlsFailureReason, UpstreamClientAuthMode,
        UpstreamTlsConfigCache, UpstreamTlsProfile, UpstreamTlsSniMode,
    };

    #[test]
    fn classifies_unknown_ca_error_text() {
        assert_eq!(
            classify_tls_error("certificate verify failed: unknown ca"),
            TlsFailureReason::UnknownCa
        );
    }

    #[test]
    fn tls_failure_reason_codes_are_stable() {
        assert_eq!(TlsFailureReason::UnknownCa.code(), "unknown_ca");
        assert_eq!(TlsFailureReason::CertValidation.code(), "cert_validation");
        assert_eq!(TlsFailureReason::HandshakeAlert.code(), "handshake");
        assert_eq!(TlsFailureReason::Timeout.code(), "timeout");
        assert_eq!(TlsFailureReason::EofOrReset.code(), "eof_or_reset");
        assert_eq!(TlsFailureReason::Other.code(), "other");
    }

    #[test]
    fn tls_failure_taxonomy_fixture_corpus_meets_accuracy_target() {
        let fixtures = vec![
            (
                "certificate verify failed: unknown ca",
                TlsFailureReason::UnknownCa,
            ),
            (
                "x509: certificate signed by unknown issuer",
                TlsFailureReason::UnknownCa,
            ),
            (
                "tls: self-signed certificate in certificate chain",
                TlsFailureReason::UnknownCa,
            ),
            (
                "unable to get local issuer certificate",
                TlsFailureReason::UnknownCa,
            ),
            (
                "invalid peer certificate: Expired",
                TlsFailureReason::CertValidation,
            ),
            (
                "x509 certificate has expired",
                TlsFailureReason::CertValidation,
            ),
            (
                "x509: certificate is not valid for any names",
                TlsFailureReason::CertValidation,
            ),
            (
                "certificate name mismatch",
                TlsFailureReason::CertValidation,
            ),
            (
                "invalid peer certificate: HostnameMismatch",
                TlsFailureReason::CertValidation,
            ),
            (
                "remote error: tls: handshake failure",
                TlsFailureReason::HandshakeAlert,
            ),
            (
                "tls alert protocol version",
                TlsFailureReason::HandshakeAlert,
            ),
            (
                "received fatal alert: decrypt error",
                TlsFailureReason::HandshakeAlert,
            ),
            (
                "handshake alert: insufficient security",
                TlsFailureReason::HandshakeAlert,
            ),
            ("operation timed out", TlsFailureReason::Timeout),
            (
                "request timed out during handshake",
                TlsFailureReason::Timeout,
            ),
            ("connect timeout", TlsFailureReason::Timeout),
            ("deadline has elapsed", TlsFailureReason::Timeout),
            (
                "unexpected eof while reading handshake",
                TlsFailureReason::EofOrReset,
            ),
            ("connection reset by peer", TlsFailureReason::EofOrReset),
            ("broken pipe", TlsFailureReason::EofOrReset),
            ("connection aborted", TlsFailureReason::EofOrReset),
            ("some unrelated network error", TlsFailureReason::Other),
        ];

        let matched = fixtures
            .iter()
            .filter(|(detail, expected)| classify_tls_error(detail) == *expected)
            .count();
        let accuracy = matched as f64 / fixtures.len() as f64;
        assert!(
            accuracy >= 0.95,
            "taxonomy fixture accuracy {accuracy:.2} below target"
        );
    }

    #[test]
    fn builds_server_tls_config_for_dns_host() {
        let config = build_http1_server_config_for_host("example.com").expect("server config");
        assert_eq!(config.alpn_protocols, vec![b"http/1.1".to_vec()]);
    }

    #[test]
    fn builds_client_tls_config_for_secure_and_insecure_modes() {
        let secure = build_http1_client_config(false);
        assert_eq!(secure.alpn_protocols, vec![b"http/1.1".to_vec()]);

        let insecure = build_http1_client_config(true);
        assert_eq!(insecure.alpn_protocols, vec![b"http/1.1".to_vec()]);
    }

    #[test]
    fn builds_http2_capable_tls_configs_when_enabled() {
        let server = build_http_server_config_for_host("example.com", true).expect("server config");
        assert_eq!(
            server.alpn_protocols,
            vec![b"h2".to_vec(), b"http/1.1".to_vec()]
        );

        let client = build_http_client_config(true, true);
        assert_eq!(
            client.alpn_protocols,
            vec![b"h2".to_vec(), b"http/1.1".to_vec()]
        );
    }

    #[test]
    fn strict_profile_builds_tls13_only_client_with_required_sni() {
        let config = build_http_client_config_with_policy(
            true,
            true,
            UpstreamTlsProfile::Strict,
            UpstreamTlsSniMode::Required,
            "example.com",
        )
        .expect("strict profile client config");
        assert!(config.enable_sni);
        assert_eq!(
            config.alpn_protocols,
            vec![b"h2".to_vec(), b"http/1.1".to_vec()]
        );
        assert_eq!(config.crypto_provider().cipher_suites.len(), 3);
        assert!(config
            .crypto_provider()
            .cipher_suites
            .iter()
            .all(|suite| suite.version() == &rustls::version::TLS13));
    }

    #[test]
    fn compat_profile_reorders_cipher_suites_for_tls12_first() {
        let config = build_http_client_config_with_policy(
            true,
            false,
            UpstreamTlsProfile::Compat,
            UpstreamTlsSniMode::Auto,
            "example.com",
        )
        .expect("compat profile client config");
        let suites = &config.crypto_provider().cipher_suites;
        assert!(!suites.is_empty(), "expected non-empty suite list");
        assert_eq!(
            suites.first().expect("first suite").version(),
            &rustls::version::TLS12
        );
    }

    #[test]
    fn required_sni_mode_rejects_ip_targets() {
        let error = build_http_client_config_with_policy(
            true,
            false,
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Required,
            "127.0.0.1",
        )
        .expect_err("required sni should reject ip target");
        assert!(
            error
                .to_string()
                .contains("upstream_sni_mode=required does not allow IP targets"),
            "{error}"
        );
    }

    #[test]
    fn upstream_client_auth_required_rejects_missing_material() {
        let error = build_http_client_config_with_policy_and_client_auth(
            true,
            false,
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::Required,
            "example.com",
            None,
        )
        .expect_err("required client auth should fail without material");
        assert!(
            error
                .to_string()
                .contains("upstream_client_auth_mode=required but no client cert material"),
            "{error}"
        );
    }

    #[test]
    fn upstream_client_auth_if_requested_allows_missing_material() {
        let config = build_http_client_config_with_policy_and_client_auth(
            true,
            false,
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::IfRequested,
            "example.com",
            None,
        )
        .expect("if_requested should fall back to no-client-auth when material is missing");
        assert_eq!(config.alpn_protocols, vec![b"http/1.1".to_vec()]);
    }

    #[test]
    fn upstream_client_auth_material_parses_and_builds() {
        let store =
            MitmCertificateStore::new(CertificateAuthorityConfig::default()).expect("cert store");
        let issued = store
            .server_config_for_host("upstream-client.example.com")
            .expect("issue upstream client identity");
        let material = parse_upstream_client_auth_material(
            issued.leaf_identity.leaf_cert_pem.as_bytes(),
            issued.leaf_identity.leaf_key_pem.as_bytes(),
        )
        .expect("parse upstream client auth material");

        let config = build_http_client_config_with_policy_and_client_auth(
            true,
            false,
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::IfRequested,
            "example.com",
            Some(material),
        )
        .expect("if_requested with material should build");
        assert_eq!(config.alpn_protocols, vec![b"http/1.1".to_vec()]);
    }

    #[test]
    fn upstream_client_auth_material_rejects_invalid_pem() {
        let error = parse_upstream_client_auth_material(b"invalid-cert", b"invalid-key")
            .expect_err("invalid pem should fail");
        assert!(
            error.to_string().contains("failed to parse upstream client"),
            "{error}"
        );
    }

    #[test]
    fn resolve_server_name_obeys_auto_sni_mode_for_ip_targets() {
        let server_name = resolve_upstream_server_name("127.0.0.1", UpstreamTlsSniMode::Auto)
            .expect("server name for ip");
        let rendered = format!("{server_name:?}");
        assert!(rendered.contains("IpAddress"), "unexpected server name: {rendered}");
    }

    #[test]
    fn cert_store_uses_distinct_cache_entries_for_http_alpn_modes() {
        let store =
            MitmCertificateStore::new(CertificateAuthorityConfig::default()).expect("cert store");

        let http1 = store
            .server_config_for_host_with_http2("api.example.com", false)
            .expect("http1 leaf");
        let http2 = store
            .server_config_for_host_with_http2("api.example.com", true)
            .expect("http2 leaf");

        assert_eq!(http1.cache_status, LeafCacheStatus::Miss);
        assert_eq!(http2.cache_status, LeafCacheStatus::Miss);
        assert_eq!(store.metrics_snapshot().cache_misses, 2);
        assert_eq!(store.metrics_snapshot().cache_hits, 0);
    }

    #[test]
    fn cert_store_cache_reports_hits_and_misses() {
        let store =
            MitmCertificateStore::new(CertificateAuthorityConfig::default()).expect("cert store");

        let first = store
            .server_config_for_host("api.example.com")
            .expect("first leaf");
        assert_eq!(first.cache_status, LeafCacheStatus::Miss);

        let second = store
            .server_config_for_host("api.example.com")
            .expect("second leaf");
        assert_eq!(second.cache_status, LeafCacheStatus::Hit);
        assert!(Arc::ptr_eq(&first.server_config, &second.server_config));

        let metrics = store.metrics_snapshot();
        assert_eq!(
            metrics,
            CertStoreMetricsSnapshot {
                cache_hits: 1,
                cache_misses: 1,
                leaves_issued: 1,
                ca_rotations: 0,
            }
        );
    }

    #[test]
    fn cert_store_with_zero_capacity_never_hits_cache() {
        let config = CertificateAuthorityConfig {
            leaf_cert_cache_capacity: 0,
            ..CertificateAuthorityConfig::default()
        };
        let store = MitmCertificateStore::new(config).expect("cert store");

        let first = store
            .server_config_for_host("api.example.com")
            .expect("first leaf");
        let second = store
            .server_config_for_host("api.example.com")
            .expect("second leaf");

        assert_eq!(first.cache_status, LeafCacheStatus::Miss);
        assert_eq!(second.cache_status, LeafCacheStatus::Miss);
        assert!(!Arc::ptr_eq(&first.server_config, &second.server_config));

        let metrics = store.metrics_snapshot();
        assert_eq!(metrics.cache_hits, 0);
        assert_eq!(metrics.cache_misses, 2);
        assert_eq!(metrics.leaves_issued, 2);
    }

    #[test]
    fn cert_store_compat_profile_issues_compatible_leaf_public_keys() {
        let store = MitmCertificateStore::new(CertificateAuthorityConfig {
            downstream_cert_profile: DownstreamCertProfile::Compat,
            ..CertificateAuthorityConfig::default()
        })
        .expect("cert store");
        let issued = store
            .server_config_for_host("api.example.com")
            .expect("issued leaf");
        let (_, cert) = parse_x509_certificate(issued.leaf_cert_der.as_ref()).expect("parse x509");
        let algorithm_oid = cert
            .public_key()
            .algorithm
            .algorithm
            .to_id_string();
        assert!(
            algorithm_oid == "1.2.840.113549.1.1.1" || algorithm_oid == "1.2.840.10045.2.1",
            "unexpected leaf public key algorithm oid: {algorithm_oid}"
        );
    }

    #[test]
    fn upstream_tls_config_cache_returns_same_arc_for_same_params() {
        let cache = UpstreamTlsConfigCache::new(
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::Never,
            None,
        );
        let a = cache.get_or_build(false, false, "example.com").unwrap();
        let b = cache.get_or_build(false, false, "other.com").unwrap();
        assert!(Arc::ptr_eq(&a, &b), "same params should yield same Arc");
    }

    #[test]
    fn upstream_tls_config_cache_differentiates_by_skip_verify() {
        let cache = UpstreamTlsConfigCache::new(
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::Never,
            None,
        );
        let secure = cache.get_or_build(false, false, "example.com").unwrap();
        let insecure = cache.get_or_build(true, false, "example.com").unwrap();
        assert!(
            !Arc::ptr_eq(&secure, &insecure),
            "different skip_verify should yield different Arcs"
        );
    }

    #[test]
    fn upstream_tls_config_cache_differentiates_by_http2() {
        let cache = UpstreamTlsConfigCache::new(
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::Never,
            None,
        );
        let h1 = cache.get_or_build(false, false, "example.com").unwrap();
        let h2 = cache.get_or_build(false, true, "example.com").unwrap();
        assert!(
            !Arc::ptr_eq(&h1, &h2),
            "different http2 should yield different Arcs"
        );
    }

    #[test]
    fn upstream_tls_config_cache_differentiates_ip_vs_domain_with_auto_sni() {
        let cache = UpstreamTlsConfigCache::new(
            UpstreamTlsProfile::Default,
            UpstreamTlsSniMode::Auto,
            UpstreamClientAuthMode::Never,
            None,
        );
        let domain = cache.get_or_build(false, false, "example.com").unwrap();
        let ip = cache.get_or_build(false, false, "1.2.3.4").unwrap();
        assert!(
            !Arc::ptr_eq(&domain, &ip),
            "domain vs IP should yield different configs (different SNI)"
        );
    }