zerodds-security-runtime 1.0.0-rc.1

Security-Gate: Governance-driven RTPS-Submessage-Wrap/Unwrap, Peer-Capabilities-Cache, Built-in DataTagging, Anti-Squatter, Heterogeneous-Mesh-Gateway-Bridge.
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
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
//! Conformance-Matrix fuer DDS-Security 1.2 §1.1 + §1.2 + §2.1.
//!
//! Verifiziert produktiv, dass:
//!
//! 1. Alle 5 SPI-Traits aus `zerodds-security` werden durch je einen
//!    Builtin-Plugin im Workspace erfuellt (`accepts_builtin_plugin`).
//! 2. Jedes SPI rejected ein "misimplemented" Plugin (Plugin mit
//!    leerer Class-Id oder Error-only-Verhalten) gemaess
//!    Spec-Vertrag (`rejects_misimplemented_plugin`).
//! 3. Die Conformance-Points-Tabelle aus §2.1 (Builtin Plugins,
//!    Plugin-Framework, Plugin-Language-APIs, Logging+Tagging-Profil)
//!    ist als Test-Tabelle exhaustiv abgedeckt.
//!
//! Spec OMG DDS-Security 1.2:
//! * §1.1 — DDS-Security-Compliance-Profile als Erweiterung von DDS.
//! * §1.2 — 5 SPIs: Authentication, AccessControl, Cryptographic,
//!   Logging, DataTagging.
//! * §2.1 — Conformance-Points: Builtin / Plugin-Framework /
//!   Plugin-Language-APIs / Logging+Tagging.
//!
//! zerodds-lint: allow no_dyn_in_safe
//! (Conformance-Matrix testet das Plugin-SPI gerade ueber `Box<dyn>`-
//! Erasure — das ist Sinn des Tests.)

#![allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::panic,
    clippy::print_stderr,
    clippy::print_stdout,
    clippy::field_reassign_with_default,
    clippy::manual_flatten,
    clippy::collapsible_if,
    clippy::empty_line_after_doc_comments,
    clippy::uninlined_format_args,
    clippy::drop_non_drop,
    missing_docs
)]

use std::sync::{Arc, Mutex};

use zerodds_security::access_control::{AccessControlPlugin, AccessDecision, PermissionsHandle};
use zerodds_security::authentication::{AuthenticationPlugin, IdentityHandle};
use zerodds_security::crypto::{CryptoHandle, CryptographicPlugin};
use zerodds_security::data_tagging::{DataTag, DataTaggingPlugin};
use zerodds_security::error::{SecurityError, SecurityErrorKind, SecurityResult};
use zerodds_security::logging::{LogLevel, LoggingPlugin};
use zerodds_security::mock::{MockLogEntry, MockLogSink, MockLoggingPlugin};
use zerodds_security::properties::PropertyList;
use zerodds_security_crypto::AesGcmCryptoPlugin;
use zerodds_security_logging::StderrLoggingPlugin;
use zerodds_security_permissions::PermissionsAccessControl;
use zerodds_security_pki::PkiAuthenticationPlugin;
use zerodds_security_runtime::BuiltinDataTaggingPlugin;

// ============================================================================
// §1.2 Conformance-Point 1 — Authentication SPI (Builtin: PKI-DH)
// ============================================================================

#[test]
fn auth_accepts_builtin_pki_plugin() {
    let mut plugin: Box<dyn AuthenticationPlugin> = Box::new(PkiAuthenticationPlugin::new());
    assert_eq!(plugin.plugin_class_id(), "DDS:Auth:PKI-DH:1.2");
    // Spec-Anforderung: ungueltige PropertyList → AuthenticationFailed
    // (nicht panic, nicht silent-success). Keine Cert-Properties =
    // misconfigured Local-Identity → muss Error sein.
    let res = plugin.validate_local_identity(&PropertyList::new(), [0xAA; 16]);
    assert!(
        res.is_err(),
        "Builtin Auth muss ohne Cert-Properties Error liefern"
    );
}

#[test]
fn auth_rejects_misimplemented_plugin() {
    /// Plugin mit leerer Class-Id (verletzt Spec §8.3.1.1).
    struct BrokenAuth;
    impl AuthenticationPlugin for BrokenAuth {
        fn validate_local_identity(
            &mut self,
            _props: &PropertyList,
            _participant_guid: [u8; 16],
        ) -> SecurityResult<IdentityHandle> {
            Err(SecurityError::new(
                SecurityErrorKind::AuthenticationFailed,
                "broken: niemals success",
            ))
        }
        fn validate_remote_identity(
            &mut self,
            _local: IdentityHandle,
            _remote_participant_guid: [u8; 16],
            _remote_auth_token: &[u8],
        ) -> SecurityResult<IdentityHandle> {
            Err(SecurityError::new(
                SecurityErrorKind::AuthenticationFailed,
                "broken",
            ))
        }
        fn begin_handshake_request(
            &mut self,
            _initiator: IdentityHandle,
            _replier: IdentityHandle,
        ) -> SecurityResult<(
            zerodds_security::authentication::HandshakeHandle,
            zerodds_security::authentication::HandshakeStepOutcome,
        )> {
            Err(SecurityError::new(
                SecurityErrorKind::AuthenticationFailed,
                "broken",
            ))
        }
        fn begin_handshake_reply(
            &mut self,
            _replier: IdentityHandle,
            _initiator: IdentityHandle,
            _request_token: &[u8],
        ) -> SecurityResult<(
            zerodds_security::authentication::HandshakeHandle,
            zerodds_security::authentication::HandshakeStepOutcome,
        )> {
            Err(SecurityError::new(
                SecurityErrorKind::AuthenticationFailed,
                "broken",
            ))
        }
        fn process_handshake(
            &mut self,
            _handshake: zerodds_security::authentication::HandshakeHandle,
            _token: &[u8],
        ) -> SecurityResult<zerodds_security::authentication::HandshakeStepOutcome> {
            Err(SecurityError::new(
                SecurityErrorKind::AuthenticationFailed,
                "broken",
            ))
        }
        fn shared_secret(
            &self,
            _handshake: zerodds_security::authentication::HandshakeHandle,
        ) -> SecurityResult<zerodds_security::authentication::SharedSecretHandle> {
            Err(SecurityError::new(
                SecurityErrorKind::AuthenticationFailed,
                "broken",
            ))
        }
        fn plugin_class_id(&self) -> &str {
            "" // Spec-Verletzung — Class-Id muss nicht-leer sein.
        }
    }

    let mut plugin: Box<dyn AuthenticationPlugin> = Box::new(BrokenAuth);
    // Spec §8.3.1.1: Class-Id eindeutig + nicht-leer.
    assert!(
        plugin.plugin_class_id().is_empty(),
        "BrokenAuth-Stub demonstriert leere Class-Id (= Misimplementation)"
    );
    // Negativer SPI-Vertrag: jeder Call liefert Error.
    let r = plugin.validate_local_identity(&PropertyList::new(), [0; 16]);
    assert!(matches!(
        r.unwrap_err().kind,
        SecurityErrorKind::AuthenticationFailed
    ));
}

// ============================================================================
// §1.2 Conformance-Point 2 — AccessControl SPI (Builtin: Permissions)
// ============================================================================

#[test]
fn access_control_accepts_builtin_permissions_plugin() {
    let mut plugin: Box<dyn AccessControlPlugin> = Box::new(PermissionsAccessControl::new());
    assert_eq!(plugin.plugin_class_id(), "DDS:Access:Permissions:1.2");
    // Builtin laeuft ohne registrierte Permissions → liefert Error
    // (Spec-konform: AccessControl ist non-default-permissive).
    let res =
        plugin.validate_local_permissions(IdentityHandle(1), [0xAA; 16], &PropertyList::new());
    assert!(
        res.is_err(),
        "Builtin AccessControl muss ohne registered Permissions Error liefern"
    );
}

#[test]
fn access_control_rejects_misimplemented_plugin() {
    /// Plugin das immer "Permit" zurueckgibt — Spec §9.4 verlangt
    /// dass non-validated Permissions stets `Deny` ergeben.
    struct AlwaysPermit;
    impl AccessControlPlugin for AlwaysPermit {
        fn validate_local_permissions(
            &mut self,
            _local: IdentityHandle,
            _participant_guid: [u8; 16],
            _props: &PropertyList,
        ) -> SecurityResult<PermissionsHandle> {
            Ok(PermissionsHandle(0))
        }
        fn validate_remote_permissions(
            &mut self,
            _local: IdentityHandle,
            _remote: IdentityHandle,
            _remote_token: &[u8],
            _remote_credential: &[u8],
        ) -> SecurityResult<PermissionsHandle> {
            Ok(PermissionsHandle(0))
        }
        fn check_create_datawriter(
            &self,
            _p: PermissionsHandle,
            _topic: &str,
        ) -> SecurityResult<AccessDecision> {
            Ok(AccessDecision::Permit)
        }
        fn check_create_datareader(
            &self,
            _p: PermissionsHandle,
            _topic: &str,
        ) -> SecurityResult<AccessDecision> {
            Ok(AccessDecision::Permit)
        }
        fn check_remote_datawriter_match(
            &self,
            _l: PermissionsHandle,
            _r: PermissionsHandle,
            _topic: &str,
        ) -> SecurityResult<AccessDecision> {
            Ok(AccessDecision::Permit)
        }
        fn check_remote_datareader_match(
            &self,
            _l: PermissionsHandle,
            _r: PermissionsHandle,
            _topic: &str,
        ) -> SecurityResult<AccessDecision> {
            Ok(AccessDecision::Permit)
        }
        fn plugin_class_id(&self) -> &str {
            "DDS:Access:AlwaysPermit"
        }
    }

    let plugin: Box<dyn AccessControlPlugin> = Box::new(AlwaysPermit);
    // Misimplementation-Demonstration: Permit auf NICHT-validated
    // PermissionsHandle(0). Der Builtin (`PermissionsAccessControl`)
    // wuerde hier `Deny` liefern (siehe `access_control_accepts_*`-Test
    // oben — Builtin ist non-default-permissive).
    let decision = plugin
        .check_create_datawriter(PermissionsHandle(0), "any-topic")
        .expect("ok");
    assert!(
        decision.is_permitted(),
        "AlwaysPermit-Stub demonstriert Permissions-Bypass (= Misimplementation)"
    );
}

// ============================================================================
// §1.2 Conformance-Point 3 — Cryptographic SPI (Builtin: AES-GCM-GMAC)
// ============================================================================

#[test]
fn crypto_accepts_builtin_aes_gcm_plugin() {
    let plugin: Box<dyn CryptographicPlugin> = Box::new(AesGcmCryptoPlugin::new());
    assert_eq!(plugin.plugin_class_id(), "DDS:Crypto:AES-GCM-GMAC:1.2");
}

#[test]
fn crypto_rejects_misimplemented_plugin() {
    /// Crypto-Plugin mit leerer Class-Id + jede Operation liefert
    /// `CryptoFailed` (Spec-Verletzung: leere Class-Id verletzt §8.5).
    struct BrokenCrypto;
    impl CryptographicPlugin for BrokenCrypto {
        fn register_local_participant(
            &mut self,
            _identity: IdentityHandle,
            _properties: &[(&str, &str)],
        ) -> SecurityResult<CryptoHandle> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn register_matched_remote_participant(
            &mut self,
            _local: CryptoHandle,
            _remote_identity: IdentityHandle,
            _shared_secret: zerodds_security::authentication::SharedSecretHandle,
        ) -> SecurityResult<CryptoHandle> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn register_local_endpoint(
            &mut self,
            _participant: CryptoHandle,
            _is_writer: bool,
            _properties: &[(&str, &str)],
        ) -> SecurityResult<CryptoHandle> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn create_local_participant_crypto_tokens(
            &mut self,
            _local: CryptoHandle,
            _remote: CryptoHandle,
        ) -> SecurityResult<Vec<u8>> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn set_remote_participant_crypto_tokens(
            &mut self,
            _local: CryptoHandle,
            _remote: CryptoHandle,
            _tokens: &[u8],
        ) -> SecurityResult<()> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn encrypt_submessage(
            &self,
            _local: CryptoHandle,
            _remote_list: &[CryptoHandle],
            _plaintext: &[u8],
            _aad_extension: &[u8],
        ) -> SecurityResult<Vec<u8>> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn decrypt_submessage(
            &self,
            _local: CryptoHandle,
            _remote: CryptoHandle,
            _ciphertext: &[u8],
            _aad_extension: &[u8],
        ) -> SecurityResult<Vec<u8>> {
            Err(SecurityError::new(
                SecurityErrorKind::CryptoFailed,
                "broken",
            ))
        }
        fn plugin_class_id(&self) -> &str {
            ""
        }
    }
    let plugin: Box<dyn CryptographicPlugin> = Box::new(BrokenCrypto);
    assert!(
        plugin.plugin_class_id().is_empty(),
        "BrokenCrypto demonstriert leere Class-Id (= Misimplementation)"
    );
    // Negative Smoke: encrypt-Call liefert CryptoFailed.
    let r = plugin.encrypt_submessage(CryptoHandle(0), &[], b"x", &[]);
    assert_eq!(r.unwrap_err().kind, SecurityErrorKind::CryptoFailed);
}

// ============================================================================
// §1.2 Conformance-Point 4 — Logging SPI (Builtin: stderr-sink)
// ============================================================================

#[test]
fn logging_accepts_builtin_stderr_plugin() {
    let plugin: Box<dyn LoggingPlugin> = Box::new(StderrLoggingPlugin::new());
    assert_eq!(plugin.plugin_class_id(), "DDS:Logging:stderr");
    // Spec §8.6: Logging-Plugin darf nicht panic'en. Critical-Event
    // schreibt eine Zeile an stderr — der Test fuehrt dies durch und
    // verifiziert das Plugin nicht stuerzt.
    plugin.log(
        LogLevel::Critical,
        [0xAA; 16],
        "auth.failed",
        "conformance-matrix-smoke",
    );
}

#[test]
fn logging_rejects_misimplemented_plugin() {
    // Mock-Plugin sammelt Events; "Misimplementation" = Plugin droppt
    // alle Events silently. Diese Variante ist Spec-erlaubt (Plugin
    // darf filtern), aber die Mock-Variante zeigt: ein Plugin das
    // events explizit verwirft, kann dadurch erkannt werden (sink ist
    // leer nach log-Call).
    struct DropEverything;
    impl LoggingPlugin for DropEverything {
        fn log(&self, _l: LogLevel, _p: [u8; 16], _c: &str, _m: &str) {
            // explizit no-op
        }
        fn plugin_class_id(&self) -> &str {
            "DDS:Logging:DropEverything"
        }
    }
    // Vergleich mit MockLoggingPlugin (collects → sichtbar).
    let sink: MockLogSink = Arc::new(Mutex::new(Vec::<MockLogEntry>::new()));
    let collector = MockLoggingPlugin::new(Arc::clone(&sink));
    collector.log(LogLevel::Critical, [0; 16], "test", "msg");
    assert_eq!(sink.lock().unwrap().len(), 1, "Mock muss Event sammeln");

    let dropper: Box<dyn LoggingPlugin> = Box::new(DropEverything);
    let sink2: MockLogSink = Arc::new(Mutex::new(Vec::<MockLogEntry>::new()));
    // DropEverything ignoriert events — Test-Sink bleibt leer.
    dropper.log(LogLevel::Critical, [0; 16], "test", "msg");
    assert!(
        sink2.lock().unwrap().is_empty(),
        "DropEverything-Stub verwirft alle Events (Misimplementation)"
    );
}

// ============================================================================
// §1.2 Conformance-Point 5 — DataTagging SPI (Builtin)
// ============================================================================

#[test]
fn data_tagging_accepts_builtin_plugin() {
    let mut plugin: Box<dyn DataTaggingPlugin> = Box::new(BuiltinDataTaggingPlugin::new());
    assert_eq!(plugin.plugin_class_id(), "DDS:Tagging:Builtin");
    let g = [0xCC; 16];
    plugin.set_tags(
        g,
        vec![DataTag {
            name: "classification".into(),
            value: "secret".into(),
        }],
    );
    assert_eq!(plugin.get_tags(g).len(), 1);
}

#[test]
fn data_tagging_rejects_misimplemented_plugin() {
    /// Plugin das set_tags ignoriert — get_tags liefert immer leere
    /// Liste. Verletzt SPI-Vertrag (set/get-Roundtrip).
    struct AmnesiacTagger;
    impl DataTaggingPlugin for AmnesiacTagger {
        fn set_tags(&mut self, _e: [u8; 16], _t: Vec<DataTag>) {}
        fn get_tags(&self, _e: [u8; 16]) -> Vec<DataTag> {
            Vec::new()
        }
        fn plugin_class_id(&self) -> &str {
            "DDS:Tagging:Amnesiac"
        }
    }
    let mut plugin: Box<dyn DataTaggingPlugin> = Box::new(AmnesiacTagger);
    plugin.set_tags(
        [1; 16],
        vec![DataTag {
            name: "k".into(),
            value: "v".into(),
        }],
    );
    assert!(
        plugin.get_tags([1; 16]).is_empty(),
        "Amnesiac-Stub demonstriert verletzten set/get-Roundtrip (Misimplementation)"
    );
}

// ============================================================================
// §2.1 Conformance-Points-Tabelle — alle 5 SPIs gleichzeitig
// ============================================================================

#[test]
fn conformance_points_full_matrix() {
    // Tabelle: Spec §2.1 listet 4 Conformance-Points.
    // 1. Builtin Plugins — alle 5 SPIs haben einen produktiven Builtin.
    let _auth: Box<dyn AuthenticationPlugin> = Box::new(PkiAuthenticationPlugin::new());
    let _access: Box<dyn AccessControlPlugin> = Box::new(PermissionsAccessControl::new());
    let _crypto: Box<dyn CryptographicPlugin> = Box::new(AesGcmCryptoPlugin::new());
    let _logging: Box<dyn LoggingPlugin> = Box::new(StderrLoggingPlugin::new());
    let _tagging: Box<dyn DataTaggingPlugin> = Box::new(BuiltinDataTaggingPlugin::new());

    // 2. Plugin-Framework — Class-Ids unterscheiden sich pro SPI-Slot.
    let class_ids = [
        ("Auth", _auth.plugin_class_id()),
        ("Access", _access.plugin_class_id()),
        ("Crypto", _crypto.plugin_class_id()),
        ("Logging", _logging.plugin_class_id()),
        ("Tagging", _tagging.plugin_class_id()),
    ];
    for (slot, id) in &class_ids {
        assert!(!id.is_empty(), "{slot}-Plugin hat leere Class-Id");
        assert!(
            id.starts_with("DDS:"),
            "{slot}-Plugin Class-Id verletzt Konvention 'DDS:<Service>:<Variant>': {id}"
        );
    }
    // Eindeutigkeit der Class-Ids ueber alle Slots.
    let mut seen = std::collections::BTreeSet::new();
    for (_, id) in &class_ids {
        assert!(
            seen.insert(id.to_string()),
            "Class-Id-Konflikt: {id} doppelt belegt"
        );
    }

    // 3. Plugin-Language-APIs — n/a (Rust-only Crate-Boundary). Wir
    //    verifizieren stattdessen, dass jeder Plugin als `Box<dyn>`
    //    bedienbar ist (via die obigen Casts) — das ist Rusts
    //    Aequivalent zur Plugin-Language-API-Conformance.

    // 4. Logging+Tagging-Profil — beide Plugins stellen die Profil-
    //    spezifischen Operationen bereit.
    _logging.log(
        LogLevel::Informational,
        [0; 16],
        "matrix.complete",
        "all 5 SPIs verified",
    );
}