hdds-c 1.0.11

High-performance DDS (Data Distribution Service) implementation in pure 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
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright (c) 2025-2026 naskel.com

//! DDS Security configuration C FFI bindings.
//!
//! Provides opaque handle and setter API for configuring DDS Security v1.1.
//! The security config is attached to a `HddsParticipantConfig` before building.
//!
//! # Usage from C
//!
//! ```c
//! HddsSecurityConfig* sec = hdds_security_config_create();
//! hdds_security_config_set_identity_cert(sec, "/certs/part1.pem");
//! hdds_security_config_set_private_key(sec, "/certs/part1_key.pem");
//! hdds_security_config_set_ca_cert(sec, "/certs/ca.pem");
//! hdds_security_config_enable_encryption(sec, true);
//! hdds_config_set_security(cfg, sec); // attach to participant config
//! // sec is consumed, do NOT destroy it
//! ```

use std::ffi::CStr;
use std::os::raw::c_char;
use std::path::PathBuf;

use crate::HddsError;

/// Internal security config accumulator with pub fields.
/// Translated to `SecurityConfig` at participant build time.
pub(crate) struct SecurityConfigInner {
    pub identity_certificate: Option<PathBuf>,
    pub private_key: Option<PathBuf>,
    pub ca_certificates: Option<PathBuf>,
    pub governance_xml: Option<PathBuf>,
    pub permissions_xml: Option<PathBuf>,
    pub enable_encryption: bool,
    pub enable_audit_log: bool,
    pub audit_log_path: Option<PathBuf>,
    pub require_authentication: bool,
    pub check_certificate_revocation: bool,
}

impl SecurityConfigInner {
    fn new() -> Self {
        Self {
            identity_certificate: None,
            private_key: None,
            ca_certificates: None,
            governance_xml: None,
            permissions_xml: None,
            enable_encryption: false,
            enable_audit_log: false,
            audit_log_path: None,
            require_authentication: true,
            check_certificate_revocation: false,
        }
    }
}

/// Opaque handle to a security configuration.
#[repr(C)]
pub struct HddsSecurityConfig {
    _private: [u8; 0],
}

// =============================================================================
// Create / Destroy
// =============================================================================

/// Create a new security configuration.
///
/// Must be attached to a participant config via `hdds_config_set_security`
/// or freed with `hdds_security_config_destroy`.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_create() -> *mut HddsSecurityConfig {
    let inner = SecurityConfigInner::new();
    Box::into_raw(Box::new(inner)).cast::<HddsSecurityConfig>()
}

/// Destroy a security configuration.
///
/// Only call this if NOT passed to `hdds_config_set_security` (which consumes it).
///
/// # Safety
/// - `config` must be a valid pointer from `hdds_security_config_create`, or NULL.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_destroy(config: *mut HddsSecurityConfig) {
    if !config.is_null() {
        let _ = Box::from_raw(config.cast::<SecurityConfigInner>());
    }
}

// =============================================================================
// Certificate paths
// =============================================================================

/// Set the identity certificate path (X.509 PEM format, required).
///
/// # Safety
/// - `config` must be valid. `path` must be null-terminated.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_set_identity_cert(
    config: *mut HddsSecurityConfig,
    path: *const c_char,
) -> HddsError {
    if config.is_null() || path.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let Ok(path_str) = CStr::from_ptr(path).to_str() else {
        return HddsError::HddsInvalidArgument;
    };
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.identity_certificate = Some(PathBuf::from(path_str));
    HddsError::HddsOk
}

/// Set the private key path (PEM format, required).
///
/// # Safety
/// - `config` must be valid. `path` must be null-terminated.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_set_private_key(
    config: *mut HddsSecurityConfig,
    path: *const c_char,
) -> HddsError {
    if config.is_null() || path.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let Ok(path_str) = CStr::from_ptr(path).to_str() else {
        return HddsError::HddsInvalidArgument;
    };
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.private_key = Some(PathBuf::from(path_str));
    HddsError::HddsOk
}

/// Set the CA certificates path (PEM format, required).
///
/// # Safety
/// - `config` must be valid. `path` must be null-terminated.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_set_ca_cert(
    config: *mut HddsSecurityConfig,
    path: *const c_char,
) -> HddsError {
    if config.is_null() || path.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let Ok(path_str) = CStr::from_ptr(path).to_str() else {
        return HddsError::HddsInvalidArgument;
    };
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.ca_certificates = Some(PathBuf::from(path_str));
    HddsError::HddsOk
}

/// Set the governance XML path (optional, for domain-level security rules).
///
/// # Safety
/// - `config` must be valid. `path` must be null-terminated.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_set_governance_xml(
    config: *mut HddsSecurityConfig,
    path: *const c_char,
) -> HddsError {
    if config.is_null() || path.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let Ok(path_str) = CStr::from_ptr(path).to_str() else {
        return HddsError::HddsInvalidArgument;
    };
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.governance_xml = Some(PathBuf::from(path_str));
    HddsError::HddsOk
}

/// Set the permissions XML path (optional, for topic/partition authorization).
///
/// # Safety
/// - `config` must be valid. `path` must be null-terminated.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_set_permissions_xml(
    config: *mut HddsSecurityConfig,
    path: *const c_char,
) -> HddsError {
    if config.is_null() || path.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let Ok(path_str) = CStr::from_ptr(path).to_str() else {
        return HddsError::HddsInvalidArgument;
    };
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.permissions_xml = Some(PathBuf::from(path_str));
    HddsError::HddsOk
}

// =============================================================================
// Boolean toggles
// =============================================================================

/// Enable or disable AES-256-GCM encryption (default: false).
///
/// # Safety
/// - `config` must be valid.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_enable_encryption(
    config: *mut HddsSecurityConfig,
    enabled: bool,
) -> HddsError {
    if config.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.enable_encryption = enabled;
    HddsError::HddsOk
}

/// Enable or disable audit logging (default: false).
///
/// # Safety
/// - `config` must be valid.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_enable_audit_log(
    config: *mut HddsSecurityConfig,
    enabled: bool,
) -> HddsError {
    if config.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.enable_audit_log = enabled;
    HddsError::HddsOk
}

/// Set audit log file path (optional, in-memory only if not set).
///
/// # Safety
/// - `config` must be valid. `path` must be null-terminated.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_set_audit_log_path(
    config: *mut HddsSecurityConfig,
    path: *const c_char,
) -> HddsError {
    if config.is_null() || path.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let Ok(path_str) = CStr::from_ptr(path).to_str() else {
        return HddsError::HddsInvalidArgument;
    };
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.audit_log_path = Some(PathBuf::from(path_str));
    HddsError::HddsOk
}

/// Require authentication for all participants (default: true).
///
/// # Safety
/// - `config` must be valid.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_require_authentication(
    config: *mut HddsSecurityConfig,
    required: bool,
) -> HddsError {
    if config.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.require_authentication = required;
    HddsError::HddsOk
}

/// Enable CRL/OCSP certificate revocation checking (default: false).
///
/// Adds ~50-200ms network round-trip per participant.
///
/// # Safety
/// - `config` must be valid.
#[no_mangle]
pub unsafe extern "C" fn hdds_security_config_check_revocation(
    config: *mut HddsSecurityConfig,
    enabled: bool,
) -> HddsError {
    if config.is_null() {
        return HddsError::HddsInvalidArgument;
    }
    let inner = &mut *config.cast::<SecurityConfigInner>();
    inner.check_certificate_revocation = enabled;
    HddsError::HddsOk
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::CString;
    use std::ptr;

    #[test]
    fn test_security_config_create_destroy() {
        unsafe {
            let config = hdds_security_config_create();
            assert!(!config.is_null());
            hdds_security_config_destroy(config);
        }
    }

    #[test]
    fn test_security_config_null_safety() {
        unsafe {
            hdds_security_config_destroy(ptr::null_mut());
            assert_eq!(
                hdds_security_config_set_identity_cert(ptr::null_mut(), ptr::null()),
                HddsError::HddsInvalidArgument,
            );
            assert_eq!(
                hdds_security_config_set_private_key(ptr::null_mut(), ptr::null()),
                HddsError::HddsInvalidArgument,
            );
            assert_eq!(
                hdds_security_config_set_ca_cert(ptr::null_mut(), ptr::null()),
                HddsError::HddsInvalidArgument,
            );
            assert_eq!(
                hdds_security_config_enable_encryption(ptr::null_mut(), true),
                HddsError::HddsInvalidArgument,
            );
        }
    }

    #[test]
    fn test_security_config_setters() {
        unsafe {
            let config = hdds_security_config_create();

            let cert = CString::new("/certs/participant1.pem").unwrap();
            let key = CString::new("/certs/participant1_key.pem").unwrap();
            let ca = CString::new("/certs/ca.pem").unwrap();
            let gov = CString::new("/certs/governance.xml").unwrap();
            let perms = CString::new("/certs/permissions.xml").unwrap();
            let audit = CString::new("/var/log/hdds_audit.log").unwrap();

            assert_eq!(
                hdds_security_config_set_identity_cert(config, cert.as_ptr()),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_set_private_key(config, key.as_ptr()),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_set_ca_cert(config, ca.as_ptr()),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_set_governance_xml(config, gov.as_ptr()),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_set_permissions_xml(config, perms.as_ptr()),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_enable_encryption(config, true),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_enable_audit_log(config, true),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_set_audit_log_path(config, audit.as_ptr()),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_require_authentication(config, false),
                HddsError::HddsOk,
            );
            assert_eq!(
                hdds_security_config_check_revocation(config, true),
                HddsError::HddsOk,
            );

            // Verify internal state
            let inner = &*config.cast::<SecurityConfigInner>();
            assert_eq!(
                inner
                    .identity_certificate
                    .as_ref()
                    .unwrap()
                    .to_str()
                    .unwrap(),
                "/certs/participant1.pem",
            );
            assert_eq!(
                inner.private_key.as_ref().unwrap().to_str().unwrap(),
                "/certs/participant1_key.pem",
            );
            assert_eq!(
                inner.ca_certificates.as_ref().unwrap().to_str().unwrap(),
                "/certs/ca.pem",
            );
            assert!(inner.enable_encryption);
            assert!(inner.enable_audit_log);
            assert!(!inner.require_authentication);
            assert!(inner.check_certificate_revocation);

            hdds_security_config_destroy(config);
        }
    }
}