File size: 7,338 Bytes
b90fa8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
mod bte_interface;
mod hardware_authorizer;
mod key_broker;
mod replay_store;
mod state_machine;
mod types;

use bte_interface::BehavioralTrustEngine;
use hardware_authorizer::{KeyAuthority, MockHardwareAuthorizer};
use key_broker::KeyBroker;
use state_machine::SystemStateMachine;
use types::*;

fn main() {
    println!("=== UBDA V1.2-alpha Architectural Refactor Integration Test ===");

    let mut state_machine = SystemStateMachine::new(0.70);
    let mock_authorizer = MockHardwareAuthorizer::new();
    let mut key_broker = KeyBroker::new(&mock_authorizer);
    let bte = BehavioralTrustEngine::new(0.70);

    let session_id = "sess_ubda_v1_alpha";
    let now = chrono::Utc::now().timestamp();

    // 1. Establish State T0 -> T1 -> T2
    println!("\n[1/7] Transitioning State: T0 -> T1 -> T2...");
    state_machine.handle_device_authenticated().unwrap();

    let telemetry = BehavioralTelemetry {
        anomaly_score: 0.10,
        cadence_entropy: 0.90,
        spatial_risk_factor: 0.02,
        timestamp: now,
    };

    let assertion =
        bte.process_telemetry(session_id, telemetry.clone(), state_machine.current_state());
    state_machine
        .handle_behavioral_assertion(&telemetry)
        .unwrap();

    assert_eq!(
        state_machine.current_state(),
        TrustState::BehavioralContinuity
    );
    println!(" -> Active State: T2 (BehavioralContinuity)");

    // 2. Reject Insufficient Classification (T2 requesting D2 data)
    println!("\n[2/7] Testing Classification Policy Enforcement (T2 requesting D2 data)...");
    let d2_issuance = mock_authorizer.issue_dac(
        &assertion,
        "financial_records.enc".to_string(),
        DataClassification::D2, // Requires T3
        AccessOperation::Read,
        now,
        300,
        2001,
    );

    match d2_issuance {
        Err(hardware_authorizer::AuthorizerError::InsufficientTrustState { .. }) => {
            println!(" -> PASS: Authorizer blocked D2 issuance under T2 trust level.");
        }
        _ => panic!(" -> FAIL: D2 capability improperly issued under T2 state!"),
    }

    // 3. Valid D1 Capability Cycle
    println!("\n[3/7] Issuing & Consuming Valid D1 Capability...");
    let valid_dac = mock_authorizer
        .issue_dac(
            &assertion,
            "personal_document.enc".to_string(),
            DataClassification::D1, // Requires T2
            AccessOperation::Read,
            now,
            300,
            2002,
        )
        .expect("D1 DAC issuance failed");

    let key = key_broker
        .execute_key_use(
            &valid_dac,
            session_id,
            AccessOperation::Read,
            now,
            state_machine.current_state(),
        )
        .expect("Key derivation failed");

    println!(
        " -> PASS: Derived Ephemeral Key (Digest: {:?})",
        &key.key_bytes[0..4]
    );

    // 4. Anti-Replay Enforcement
    println!("\n[4/7] Testing Replay Defense...");
    let replay_result = key_broker.execute_key_use(
        &valid_dac,
        session_id,
        AccessOperation::Read,
        now + 1,
        state_machine.current_state(),
    );

    match replay_result {
        Err(key_broker::KeyBrokerError::ReplayDetected) => {
            println!(" -> PASS: Key Broker blocked consumed DAC replay attempt.");
        }
        _ => panic!(" -> FAIL: Replayed DAC was accepted!"),
    }

    // 5. Signature Tamper Verification
    println!("\n[5/7] Testing Signature Integrity Verification...");
    let mut tampered_dac = mock_authorizer
        .issue_dac(
            &assertion,
            "personal_document_2.enc".to_string(),
            DataClassification::D1,
            AccessOperation::Read,
            now,
            300,
            2003,
        )
        .unwrap();

    // Field modification by attacker
    tampered_dac.required_state = TrustState::CriticalElevation;

    let tamper_result = key_broker.execute_key_use(
        &tampered_dac,
        session_id,
        AccessOperation::Read,
        now,
        TrustState::CriticalElevation,
    );

    match tamper_result {
        Err(key_broker::KeyBrokerError::InvalidSignature) => {
            println!(
                " -> PASS: Key Broker detected field modification via canonical verification."
            );
        }
        _ => panic!(" -> FAIL: Tampered capability signature validated!"),
    }

    // 6. Full Elevation to T3/T4 and Issuing High-Classification Capabilities
    println!("\n[6/7] Elevating T2 -> T3 -> T4 and Issuing D2/D3 Capabilities...");
    state_machine.handle_step_up_auth().unwrap();
    assert_eq!(state_machine.current_state(), TrustState::HighAssurance);
    println!(" -> Active State: T3 (HighAssurance)");

    let d2_assertion =
        bte.process_telemetry(session_id, telemetry.clone(), state_machine.current_state());
    let d2_dac = mock_authorizer
        .issue_dac(
            &d2_assertion,
            "financial_records.enc".to_string(),
            DataClassification::D2,
            AccessOperation::Read,
            now,
            300,
            2004,
        )
        .expect("D2 DAC issuance failed at T3");
    key_broker
        .execute_key_use(
            &d2_dac,
            session_id,
            AccessOperation::Read,
            now,
            state_machine.current_state(),
        )
        .expect("D2 key derivation failed at T3");
    println!(" -> PASS: Issued & consumed D2 capability under T3 trust level.");

    state_machine.handle_critical_elevation().unwrap();
    assert_eq!(state_machine.current_state(), TrustState::CriticalElevation);
    println!(" -> Active State: T4 (CriticalElevation)");

    let d3_assertion =
        bte.process_telemetry(session_id, telemetry.clone(), state_machine.current_state());
    let d3_dac = mock_authorizer
        .issue_dac(
            &d3_assertion,
            "root_signing_key.enc".to_string(),
            DataClassification::D3,
            AccessOperation::Read,
            now,
            300,
            2005,
        )
        .expect("D3 DAC issuance failed at T4");
    key_broker
        .execute_key_use(
            &d3_dac,
            session_id,
            AccessOperation::Read,
            now,
            state_machine.current_state(),
        )
        .expect("D3 key derivation failed at T4");
    println!(" -> PASS: Issued & consumed D3 (master key) capability under T4 trust level.");

    // 7. Hard Anomaly Lockout & Controlled Recovery
    println!("\n[7/7] Injecting Hard Anomaly Interrupt (T_-1 Lockout)...");
    state_machine.trigger_compromise();
    assert_eq!(state_machine.current_state(), TrustState::Compromised);

    assert!(state_machine.handle_device_authenticated().is_err());
    println!(" -> PASS: Escalation blocked in T_-1 state.");

    state_machine.handle_hardware_recovery_reset().unwrap();
    assert_eq!(state_machine.current_state(), TrustState::Unauthenticated);
    println!(" -> PASS: System performed clean hardware recovery to T0 state.");

    println!("\n=== ALL UBDA V1.2-alpha PROTOCOL TESTS COMPLETED ===");
}