nwep-rs 0.1.8

Rust bindings for the NWEP (WEB/1) protocol library
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
#![allow(unsafe_op_in_unsafe_fn)]

use crate::anchor::AnchorSet;
use crate::bls::BlsPubkey;
use crate::checkpoint::Checkpoint;
use crate::error::{Error, check};
use crate::ffi;
use crate::merkle::{MerkleEntry, MerkleProof};
use crate::types::{Duration, MerkleHash, NodeId, Tstamp};

/// `STALENESS_WARNING_NS` is the checkpoint age (1 hour) at which [`Staleness::Warning`] is returned.
pub const STALENESS_WARNING_NS: Duration = 3600 * crate::types::SECONDS;
/// `STALENESS_REJECT_NS` is the checkpoint age (24 hours) at which [`Staleness::Reject`] is returned.
pub const STALENESS_REJECT_NS: Duration = 86400 * crate::types::SECONDS;
/// `IDENTITY_CACHE_TTL` is the default TTL (1 hour) for entries in the identity cache.
pub const IDENTITY_CACHE_TTL: Duration = 3600 * crate::types::SECONDS;

/// `Staleness` indicates how old the latest checkpoint in a [`TrustStore`] is.
///
/// Returned by [`TrustStore::check_staleness`]. Callers should at minimum warn on
/// `Warning` and refuse to accept new identities on `Reject`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Staleness {
    /// `Fresh` — the latest checkpoint is younger than [`STALENESS_WARNING_NS`].
    Fresh,
    /// `Warning` — the latest checkpoint is older than [`STALENESS_WARNING_NS`] but younger than [`STALENESS_REJECT_NS`].
    Warning,
    /// `Reject` — the latest checkpoint is older than [`STALENESS_REJECT_NS`]; identity verification should be refused.
    Reject,
}

impl From<ffi::nwep_staleness> for Staleness {
    fn from(s: ffi::nwep_staleness) -> Self {
        match s {
            ffi::nwep_staleness_NWEP_STALENESS_WARNING => Staleness::Warning,
            ffi::nwep_staleness_NWEP_STALENESS_REJECT => Staleness::Reject,
            _ => Staleness::Fresh,
        }
    }
}

/// `VerifiedIdentity` is the result of a successful [`TrustStore::verify_identity`] call.
///
/// It records the node's current public key, the log position and checkpoint epoch at
/// which it was verified, and whether it has since been revoked.
#[derive(Clone, Debug)]
pub struct VerifiedIdentity {
    /// The 32-byte node identifier.
    pub node_id: NodeId,
    /// The node's active Ed25519 public key at the time of verification.
    pub pubkey: [u8; 32],
    /// The Merkle log index of the entry that was verified.
    pub log_index: u64,
    /// The epoch of the checkpoint used to anchor this verification.
    pub checkpoint_epoch: u64,
    /// Timestamp (nanoseconds since epoch) when this identity was verified.
    pub verified_at: Tstamp,
    /// `true` if the node's identity has been revoked.
    pub revoked: bool,
}

impl VerifiedIdentity {
    pub(crate) fn from_ffi(v: &ffi::nwep_verified_identity) -> Self {
        VerifiedIdentity {
            node_id: NodeId(v.nodeid.data),
            pubkey: v.pubkey,
            log_index: v.log_index,
            checkpoint_epoch: v.checkpoint_epoch,
            verified_at: v.verified_at,
            revoked: v.revoked != 0,
        }
    }
}

/// `Equivocation` describes an anchor that has signed two conflicting checkpoints.
///
/// Returned by [`TrustStore::check_equivocation`] when misbehaviour is detected.
/// An equivocating anchor has signed two different Merkle roots for the same `epoch`,
/// which is a protocol violation; the anchor's key should be removed from the trust set.
#[derive(Clone, Debug)]
pub struct Equivocation {
    /// The BLS public key of the equivocating anchor.
    pub anchor: BlsPubkey,
    /// The epoch for which two conflicting signatures were found.
    pub epoch: u64,
    /// The first Merkle root signed by the anchor for this epoch.
    pub root1: MerkleHash,
    /// The second, conflicting Merkle root signed by the anchor for this epoch.
    pub root2: MerkleHash,
}

impl Equivocation {
    fn from_ffi(e: &ffi::nwep_equivocation) -> Self {
        Equivocation {
            anchor: BlsPubkey(e.anchor.data),
            epoch: e.epoch,
            root1: MerkleHash(e.root1.data),
            root2: MerkleHash(e.root2.data),
        }
    }
}

/// `TrustSettings` configures staleness thresholds and identity cache behaviour for a [`TrustStore`].
pub struct TrustSettings {
    /// Checkpoint age (nanoseconds) at which [`Staleness::Warning`] is returned. Default: [`STALENESS_WARNING_NS`].
    pub staleness_warning_ns: Duration,
    /// Checkpoint age (nanoseconds) at which [`Staleness::Reject`] is returned. Default: [`STALENESS_REJECT_NS`].
    pub staleness_reject_ns: Duration,
    /// Time-to-live (nanoseconds) for cached identity entries. Default: [`IDENTITY_CACHE_TTL`].
    pub identity_cache_ttl: Duration,
    /// Minimum number of anchor signatures required to validate a checkpoint.
    pub anchor_threshold: usize,
}

impl Default for TrustSettings {
    fn default() -> Self {
        TrustSettings {
            staleness_warning_ns: STALENESS_WARNING_NS,
            staleness_reject_ns: STALENESS_REJECT_NS,
            identity_cache_ttl: IDENTITY_CACHE_TTL,
            anchor_threshold: crate::types::DEFAULT_ANCHOR_THRESHOLD,
        }
    }
}

/// `TrustStorage` provides persistence callbacks for anchor keys and checkpoints.
///
/// Pass to [`TrustStore::new_with_storage`] to persist the trust store across
/// restarts. All fields are optional; unset callbacks are silently skipped.
pub struct TrustStorage {
    /// Called on startup to load the saved anchor public keys.
    pub anchor_load: Option<Box<dyn Fn() -> Result<Vec<BlsPubkey>, Error> + Send>>,
    /// Called when the anchor set changes, to persist the updated set.
    pub anchor_save: Option<Box<dyn Fn(&[BlsPubkey]) -> Result<(), Error> + Send>>,
    /// Called on startup to load previously saved checkpoints.
    pub checkpoint_load: Option<Box<dyn Fn() -> Result<Vec<Checkpoint>, Error> + Send>>,
    /// Called when a new checkpoint is added, to persist it durably.
    pub checkpoint_save: Option<Box<dyn Fn(&Checkpoint) -> Result<(), Error> + Send>>,
}

impl Default for TrustStorage {
    fn default() -> Self {
        TrustStorage {
            anchor_load: None,
            anchor_save: None,
            checkpoint_load: None,
            checkpoint_save: None,
        }
    }
}

struct StorageCallbacks {
    storage: TrustStorage,
}

unsafe extern "C" fn anchor_load_cb(
    user_data: *mut std::ffi::c_void,
    anchors: *mut ffi::nwep_bls_pubkey,
    max_anchors: usize,
) -> std::ffi::c_int {
    let cb = &*(user_data as *const StorageCallbacks);
    if let Some(f) = &cb.storage.anchor_load {
        match f() {
            Ok(pks) => {
                let n = pks.len().min(max_anchors);
                for (i, pk) in pks[..n].iter().enumerate() {
                    *anchors.add(i) = ffi::nwep_bls_pubkey { data: pk.0 };
                }
                n as i32
            }
            Err(e) => e.code,
        }
    } else {
        0
    }
}

unsafe extern "C" fn anchor_save_cb(
    user_data: *mut std::ffi::c_void,
    anchors: *const ffi::nwep_bls_pubkey,
    count: usize,
) -> std::ffi::c_int {
    let cb = &*(user_data as *const StorageCallbacks);
    if let Some(f) = &cb.storage.anchor_save {
        let pks: Vec<BlsPubkey> = (0..count)
            .map(|i| BlsPubkey((*anchors.add(i)).data))
            .collect();
        match f(&pks) {
            Ok(()) => 0,
            Err(e) => e.code,
        }
    } else {
        0
    }
}

unsafe extern "C" fn checkpoint_load_cb(
    user_data: *mut std::ffi::c_void,
    checkpoints: *mut ffi::nwep_checkpoint,
    max_checkpoints: usize,
) -> std::ffi::c_int {
    let cb = &*(user_data as *const StorageCallbacks);
    if let Some(f) = &cb.storage.checkpoint_load {
        match f() {
            Ok(cps) => {
                let n = cps.len().min(max_checkpoints);
                for (i, cp) in cps[..n].iter().enumerate() {
                    *checkpoints.add(i) = cp.to_ffi();
                }
                n as i32
            }
            Err(e) => e.code,
        }
    } else {
        0
    }
}

unsafe extern "C" fn checkpoint_save_cb(
    user_data: *mut std::ffi::c_void,
    cp: *const ffi::nwep_checkpoint,
) -> std::ffi::c_int {
    let cb = &*(user_data as *const StorageCallbacks);
    if let Some(f) = &cb.storage.checkpoint_save {
        let checkpoint = Checkpoint::from_ffi(&*cp);
        match f(&checkpoint) {
            Ok(()) => 0,
            Err(e) => e.code,
        }
    } else {
        0
    }
}

/// `TrustStore` verifies node identities against a quorum of BLS-signed Merkle log checkpoints.
///
/// `TrustStore` holds a set of trusted anchor public keys and a collection of checkpoints.
/// Call [`verify_identity`](TrustStore::verify_identity) to confirm that a [`MerkleEntry`]
/// is covered by a valid checkpoint signed by enough anchors. Use
/// [`cache_identity`](TrustStore::cache_identity) to store the result so that subsequent
/// connections from the same peer are fast.
pub struct TrustStore {
    ptr: *mut ffi::nwep_trust_store,
    _callbacks: Option<Box<StorageCallbacks>>,
}

unsafe impl Send for TrustStore {}

impl TrustStore {
    /// `new` creates a `TrustStore` with in-memory state that is not persisted across restarts.
    ///
    /// Use [`new_with_storage`](TrustStore::new_with_storage) if persistence is required.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the underlying C allocation fails.
    pub fn new(settings: TrustSettings) -> Result<Self, Error> {
        let ffi_settings = ffi::nwep_trust_settings {
            staleness_warning_ns: settings.staleness_warning_ns,
            staleness_reject_ns: settings.staleness_reject_ns,
            identity_cache_ttl: settings.identity_cache_ttl,
            anchor_threshold: settings.anchor_threshold,
        };
        let mut ptr: *mut ffi::nwep_trust_store = std::ptr::null_mut();
        check(unsafe { ffi::nwep_trust_store_new(&mut ptr, &ffi_settings, std::ptr::null()) })?;
        Ok(TrustStore {
            ptr,
            _callbacks: None,
        })
    }

    /// `new_with_storage` creates a `TrustStore` backed by caller-provided persistence callbacks.
    ///
    /// On creation the C library calls `anchor_load` and `checkpoint_load` to restore
    /// previously saved state. Subsequent changes call `anchor_save` and `checkpoint_save`.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the underlying C allocation fails.
    pub fn new_with_storage(settings: TrustSettings, storage: TrustStorage) -> Result<Self, Error> {
        let ffi_settings = ffi::nwep_trust_settings {
            staleness_warning_ns: settings.staleness_warning_ns,
            staleness_reject_ns: settings.staleness_reject_ns,
            identity_cache_ttl: settings.identity_cache_ttl,
            anchor_threshold: settings.anchor_threshold,
        };
        let mut cb = Box::new(StorageCallbacks { storage });
        let cb_ptr = cb.as_mut() as *mut _ as *mut std::ffi::c_void;
        let ffi_storage = ffi::nwep_trust_storage {
            anchor_load: Some(anchor_load_cb),
            anchor_save: Some(anchor_save_cb),
            checkpoint_load: Some(checkpoint_load_cb),
            checkpoint_save: Some(checkpoint_save_cb),
            user_data: cb_ptr,
        };
        let mut ptr: *mut ffi::nwep_trust_store = std::ptr::null_mut();
        check(unsafe { ffi::nwep_trust_store_new(&mut ptr, &ffi_settings, &ffi_storage) })?;
        Ok(TrustStore {
            ptr,
            _callbacks: Some(cb),
        })
    }

    /// `add_anchor` adds a BLS anchor public key to the trust store's anchor set.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the key is already present or the C call fails.
    pub fn add_anchor(&mut self, pk: &BlsPubkey, builtin: bool) -> Result<(), Error> {
        let ffi_pk = ffi::nwep_bls_pubkey { data: pk.0 };
        check(unsafe { ffi::nwep_trust_store_add_anchor(self.ptr, &ffi_pk, builtin as i32) })
    }

    /// `remove_anchor` removes a BLS anchor public key from the trust store.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the key is not present or the C call fails.
    pub fn remove_anchor(&mut self, pk: &BlsPubkey) -> Result<(), Error> {
        let ffi_pk = ffi::nwep_bls_pubkey { data: pk.0 };
        check(unsafe { ffi::nwep_trust_store_remove_anchor(self.ptr, &ffi_pk) })
    }

    /// `anchors` returns a shared reference to the internal [`AnchorSet`].
    ///
    /// The returned reference is valid for the lifetime of `self`.
    pub fn anchors(&self) -> &AnchorSet {
        unsafe {
            let ptr = ffi::nwep_trust_store_get_anchors(self.ptr);
            // SAFETY: The returned pointer is owned by the trust store and valid as long as self is.
            // We transmute the const pointer to a reference to AnchorSet which has the same layout.
            &*(ptr as *const AnchorSet)
        }
    }

    /// `add_checkpoint` adds a signed checkpoint to the trust store.
    ///
    /// The checkpoint is validated against the current anchor set before being stored.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the checkpoint signature is invalid or the C call fails.
    pub fn add_checkpoint(&mut self, cp: &Checkpoint) -> Result<(), Error> {
        let ffi_cp = cp.to_ffi();
        check(unsafe { ffi::nwep_trust_store_add_checkpoint(self.ptr, &ffi_cp) })
    }

    /// `get_latest_checkpoint` returns the most recently stored checkpoint.
    ///
    /// # Errors
    ///
    /// Returns `Err` if no checkpoints have been stored yet.
    pub fn get_latest_checkpoint(&self) -> Result<Checkpoint, Error> {
        let mut cp = unsafe { std::mem::zeroed::<ffi::nwep_checkpoint>() };
        check(unsafe { ffi::nwep_trust_store_get_latest_checkpoint(self.ptr, &mut cp) })?;
        Ok(Checkpoint::from_ffi(&cp))
    }

    /// `get_checkpoint` retrieves the checkpoint for a specific epoch.
    ///
    /// # Errors
    ///
    /// Returns `Err` if no checkpoint for `epoch` has been stored.
    pub fn get_checkpoint(&self, epoch: u64) -> Result<Checkpoint, Error> {
        let mut cp = unsafe { std::mem::zeroed::<ffi::nwep_checkpoint>() };
        check(unsafe { ffi::nwep_trust_store_get_checkpoint(self.ptr, epoch, &mut cp) })?;
        Ok(Checkpoint::from_ffi(&cp))
    }

    /// `checkpoint_count` returns the number of checkpoints stored in the trust store.
    pub fn checkpoint_count(&self) -> usize {
        unsafe { ffi::nwep_trust_store_checkpoint_count(self.ptr) }
    }

    /// `check_staleness` reports how old the latest stored checkpoint is relative to `now`.
    ///
    /// Returns [`Staleness::Fresh`] if checkpoints are recent, [`Staleness::Warning`] if
    /// older than [`STALENESS_WARNING_NS`], or [`Staleness::Reject`] if older than
    /// [`STALENESS_REJECT_NS`].
    pub fn check_staleness(&self, now: Tstamp) -> Staleness {
        Staleness::from(unsafe { ffi::nwep_trust_store_check_staleness(self.ptr, now) })
    }

    /// `get_staleness_age` returns the age (nanoseconds) of the latest checkpoint relative to `now`.
    pub fn get_staleness_age(&self, now: Tstamp) -> Duration {
        unsafe { ffi::nwep_trust_store_get_staleness_age(self.ptr, now) }
    }

    /// `verify_identity` verifies that a Merkle log entry is covered by a trusted, quorum-signed checkpoint.
    ///
    /// Checks that `proof` is a valid inclusion proof for `entry` in a checkpoint with a root
    /// known to the trust store, and that the checkpoint has sufficient anchor signatures.
    /// If `checkpoint` is `Some`, that specific checkpoint is used; otherwise the best known
    /// checkpoint is selected automatically.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the proof is invalid, no suitable checkpoint exists, the checkpoint
    /// lacks sufficient signatures, or the identity has been revoked.
    pub fn verify_identity(
        &mut self,
        entry: &MerkleEntry,
        proof: &MerkleProof,
        checkpoint: Option<&Checkpoint>,
        now: Tstamp,
    ) -> Result<VerifiedIdentity, Error> {
        let ffi_entry = entry.to_ffi();
        let ffi_proof = proof.to_ffi();
        let ffi_cp_owned;
        let ffi_cp_ptr = match checkpoint {
            Some(cp) => {
                ffi_cp_owned = cp.to_ffi();
                &ffi_cp_owned as *const _
            }
            None => std::ptr::null(),
        };
        let mut result = unsafe { std::mem::zeroed::<ffi::nwep_verified_identity>() };
        check(unsafe {
            ffi::nwep_trust_store_verify_identity(
                self.ptr,
                &ffi_entry,
                &ffi_proof,
                ffi_cp_ptr,
                now,
                &mut result,
            )
        })?;
        Ok(VerifiedIdentity::from_ffi(&result))
    }

    /// `cache_identity` stores a previously verified identity for fast future lookups.
    ///
    /// The cached entry expires after `identity_cache_ttl` nanoseconds.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the C call fails.
    pub fn cache_identity(&mut self, vi: &VerifiedIdentity) -> Result<(), Error> {
        let ffi_vi = ffi::nwep_verified_identity {
            nodeid: ffi::nwep_nodeid { data: vi.node_id.0 },
            pubkey: vi.pubkey,
            log_index: vi.log_index,
            checkpoint_epoch: vi.checkpoint_epoch,
            verified_at: vi.verified_at,
            revoked: vi.revoked as i32,
        };
        check(unsafe { ffi::nwep_trust_store_cache_identity(self.ptr, &ffi_vi) })
    }

    /// `lookup_identity` retrieves a cached verified identity if it has not expired.
    ///
    /// # Errors
    ///
    /// Returns `Err` if no valid cached entry exists for `node_id`.
    pub fn lookup_identity(
        &self,
        node_id: &NodeId,
        now: Tstamp,
    ) -> Result<VerifiedIdentity, Error> {
        let ffi_nid = ffi::nwep_nodeid { data: node_id.0 };
        let mut out = unsafe { std::mem::zeroed::<ffi::nwep_verified_identity>() };
        check(unsafe { ffi::nwep_trust_store_lookup_identity(self.ptr, &ffi_nid, now, &mut out) })?;
        Ok(VerifiedIdentity::from_ffi(&out))
    }

    /// `check_equivocation` detects whether an anchor has signed two conflicting checkpoints for the same epoch.
    ///
    /// Returns `Ok(Some(equivocation))` if misbehaviour is detected, `Ok(None)` if the checkpoint
    /// is consistent with previously seen checkpoints, or `Err` on a C-level failure.
    pub fn check_equivocation(&mut self, cp: &Checkpoint) -> Result<Option<Equivocation>, Error> {
        let ffi_cp = cp.to_ffi();
        let mut out = unsafe { std::mem::zeroed::<ffi::nwep_equivocation>() };
        let rc = unsafe { ffi::nwep_trust_store_check_equivocation(self.ptr, &ffi_cp, &mut out) };
        if rc == 0 {
            Ok(None)
        } else if rc == crate::error::ERR_TRUST_EQUIVOCATION {
            Ok(Some(Equivocation::from_ffi(&out)))
        } else {
            Err(Error::from_code(rc))
        }
    }
}

impl Drop for TrustStore {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::nwep_trust_store_free(self.ptr) }
        }
    }
}