craton-hsm 0.10.5

A production-grade PKCS#11 v3.0-compliant Software HSM written in Rust
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Craton Software Company
//! Per-session and global rate limiting for cryptographic operations.
//!
//! Uses a token bucket algorithm with `Instant`-based timing to prevent
//! abuse and denial-of-service attacks against the HSM.

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

use dashmap::DashMap;
use serde::Deserialize;

use crate::error::HsmError;

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Rate-limiting configuration, deserializable from TOML.
///
/// All limits default to `0` (unlimited / disabled).
#[derive(Debug, Clone, Deserialize, Default)]
pub struct RateLimitConfig {
    /// Max operations per session per second. 0 = unlimited.
    #[serde(default)]
    pub max_ops_per_session_per_sec: u64,
    /// Max global operations per second across all sessions. 0 = unlimited.
    #[serde(default)]
    pub max_global_ops_per_sec: u64,
    /// Max concurrent operations across all sessions. 0 = unlimited.
    #[serde(default)]
    pub max_concurrent_ops: u64,
}

// ---------------------------------------------------------------------------
// Per-session token bucket state (integer-based, using microtokens)
// ---------------------------------------------------------------------------

/// 1 token = 1_000_000 microtokens. Using integer arithmetic avoids
/// floating-point drift that would accumulate over many refill cycles.
const MICROTOKENS_PER_OP: u64 = 1_000_000;

/// Maximum number of per-session buckets stored simultaneously.
/// Prevents OOM if an attacker exhausts session IDs.
const MAX_SESSION_BUCKETS: usize = 10_000;

/// Token bucket state for a single session (integer microtokens).
#[derive(Debug)]
struct SessionRateState {
    /// Available microtokens.
    tokens: u64,
    /// Last time the bucket was refilled.
    last_refill: Instant,
    /// Refill rate in microtokens per second.
    rate: u64,
    /// Maximum bucket capacity in microtokens.
    capacity: u64,
}

impl SessionRateState {
    fn new(rate: u64) -> Self {
        let rate_micro = rate.saturating_mul(MICROTOKENS_PER_OP);
        Self {
            tokens: rate_micro, // start with a full bucket
            last_refill: Instant::now(),
            rate: rate_micro,
            capacity: rate_micro,
        }
    }

    /// Refill tokens based on elapsed time, then try to consume one operation.
    /// Returns `true` if the operation is allowed.
    fn try_consume(&mut self) -> bool {
        let now = Instant::now();
        let elapsed_micros = now.duration_since(self.last_refill).as_micros() as u64;
        // refill = rate (microtokens/sec) * elapsed_micros / 1_000_000
        let refill = (self.rate as u128).saturating_mul(elapsed_micros as u128) / 1_000_000;
        self.tokens = self.tokens.saturating_add(refill as u64).min(self.capacity);
        self.last_refill = now;

        if self.tokens >= MICROTOKENS_PER_OP {
            self.tokens -= MICROTOKENS_PER_OP;
            true
        } else {
            false
        }
    }
}

// ---------------------------------------------------------------------------
// Global token bucket state
// ---------------------------------------------------------------------------

/// Token bucket for the global (cross-session) rate limit (integer microtokens).
#[derive(Debug)]
struct GlobalRateState {
    /// Available microtokens.
    tokens: u64,
    last_refill: Instant,
    /// Refill rate in microtokens per second.
    rate: u64,
    /// Maximum bucket capacity in microtokens.
    capacity: u64,
}

impl GlobalRateState {
    fn new(rate: u64) -> Self {
        let rate_micro = rate.saturating_mul(MICROTOKENS_PER_OP);
        Self {
            tokens: rate_micro,
            last_refill: Instant::now(),
            rate: rate_micro,
            capacity: rate_micro,
        }
    }

    fn try_consume(&mut self) -> bool {
        let now = Instant::now();
        let elapsed_micros = now.duration_since(self.last_refill).as_micros() as u64;
        let refill = (self.rate as u128).saturating_mul(elapsed_micros as u128) / 1_000_000;
        self.tokens = self.tokens.saturating_add(refill as u64).min(self.capacity);
        self.last_refill = now;

        if self.tokens >= MICROTOKENS_PER_OP {
            self.tokens -= MICROTOKENS_PER_OP;
            true
        } else {
            false
        }
    }
}

// ---------------------------------------------------------------------------
// RateLimiter
// ---------------------------------------------------------------------------

/// Enforces per-session and global rate limits on cryptographic operations.
///
/// Thread-safe: all interior state uses atomics or `DashMap` / `parking_lot`.
pub struct RateLimiter {
    config: RateLimitConfig,
    /// Per-session token buckets keyed by session handle.
    session_buckets: DashMap<u64, SessionRateState>,
    /// Global token bucket (protected by a lightweight mutex).
    global_bucket: parking_lot::Mutex<Option<GlobalRateState>>,
    /// Current number of in-flight concurrent operations.
    concurrent_ops: AtomicU64,
}

impl std::fmt::Debug for RateLimiter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RateLimiter")
            .field("config", &self.config)
            .field(
                "concurrent_ops",
                &self.concurrent_ops.load(Ordering::Relaxed),
            )
            .finish()
    }
}

impl RateLimiter {
    /// Create a rate limiter from the given configuration.
    pub fn new(config: &RateLimitConfig) -> Self {
        let global_bucket = if config.max_global_ops_per_sec > 0 {
            Some(GlobalRateState::new(config.max_global_ops_per_sec))
        } else {
            None
        };
        Self {
            config: config.clone(),
            session_buckets: DashMap::new(),
            global_bucket: parking_lot::Mutex::new(global_bucket),
            concurrent_ops: AtomicU64::new(0),
        }
    }

    /// Create a no-op rate limiter where all checks pass (all limits = 0).
    pub fn disabled() -> Self {
        Self::new(&RateLimitConfig::default())
    }

    /// Check whether `session_handle` is allowed to perform an operation
    /// under the per-session and global rate limits.
    ///
    /// Returns `Err(HsmError::GeneralError)` if the rate limit is exceeded.
    pub fn check_rate(&self, session_handle: u64) -> Result<(), HsmError> {
        // --- per-session limit ---
        if self.config.max_ops_per_session_per_sec > 0 {
            // Guard against OOM from unbounded session bucket growth.
            if !self.session_buckets.contains_key(&session_handle)
                && self.session_buckets.len() >= MAX_SESSION_BUCKETS
            {
                return Err(HsmError::GeneralError);
            }

            let mut entry = self
                .session_buckets
                .entry(session_handle)
                .or_insert_with(|| SessionRateState::new(self.config.max_ops_per_session_per_sec));
            if !entry.value_mut().try_consume() {
                return Err(HsmError::GeneralError);
            }
        }

        // --- global limit ---
        if self.config.max_global_ops_per_sec > 0 {
            let mut guard = self.global_bucket.lock();
            if let Some(ref mut bucket) = *guard {
                if !bucket.try_consume() {
                    return Err(HsmError::GeneralError);
                }
            }
        }

        Ok(())
    }

    /// Signal the start of a concurrent operation.
    ///
    /// Returns `Err(HsmError::GeneralError)` if the concurrent-ops limit
    /// would be exceeded.
    pub fn begin_operation(&self) -> Result<(), HsmError> {
        if self.config.max_concurrent_ops == 0 {
            return Ok(());
        }
        // Atomically increment, then check. If over limit, decrement back.
        let prev = self.concurrent_ops.fetch_add(1, Ordering::SeqCst);
        if prev >= self.config.max_concurrent_ops {
            self.concurrent_ops.fetch_sub(1, Ordering::SeqCst);
            return Err(HsmError::GeneralError);
        }
        Ok(())
    }

    /// Signal the end of a concurrent operation (decrement counter).
    pub fn end_operation(&self) {
        if self.config.max_concurrent_ops == 0 {
            return;
        }
        self.concurrent_ops.fetch_sub(1, Ordering::SeqCst);
    }

    /// Remove all rate-limit state for a closed session.
    pub fn remove_session(&self, session_handle: u64) {
        self.session_buckets.remove(&session_handle);
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn disabled_limiter_always_passes() {
        let rl = RateLimiter::disabled();
        for i in 0..1000 {
            assert!(rl.check_rate(i).is_ok());
            assert!(rl.begin_operation().is_ok());
            rl.end_operation();
        }
    }

    #[test]
    fn per_session_rate_limit_rejects_after_burst() {
        let config = RateLimitConfig {
            max_ops_per_session_per_sec: 5,
            max_global_ops_per_sec: 0,
            max_concurrent_ops: 0,
        };
        let rl = RateLimiter::new(&config);
        let handle = 42u64;

        // First 5 should succeed (bucket starts full with 5 tokens).
        for _ in 0..5 {
            assert!(rl.check_rate(handle).is_ok());
        }
        // 6th should fail — bucket is empty.
        assert!(rl.check_rate(handle).is_err());

        // A different session should still work.
        assert!(rl.check_rate(99).is_ok());
    }

    #[test]
    fn concurrent_ops_limit() {
        let config = RateLimitConfig {
            max_ops_per_session_per_sec: 0,
            max_global_ops_per_sec: 0,
            max_concurrent_ops: 3,
        };
        let rl = RateLimiter::new(&config);

        assert!(rl.begin_operation().is_ok());
        assert!(rl.begin_operation().is_ok());
        assert!(rl.begin_operation().is_ok());
        // 4th should fail.
        assert!(rl.begin_operation().is_err());

        // End one, then the next should succeed.
        rl.end_operation();
        assert!(rl.begin_operation().is_ok());
    }

    #[test]
    fn session_cleanup_removes_state() {
        let config = RateLimitConfig {
            max_ops_per_session_per_sec: 2,
            max_global_ops_per_sec: 0,
            max_concurrent_ops: 0,
        };
        let rl = RateLimiter::new(&config);
        let handle = 7u64;

        // Exhaust the bucket.
        assert!(rl.check_rate(handle).is_ok());
        assert!(rl.check_rate(handle).is_ok());
        assert!(rl.check_rate(handle).is_err());

        // Remove and re-check — should get a fresh bucket.
        rl.remove_session(handle);
        assert!(rl.check_rate(handle).is_ok());
    }

    #[test]
    fn global_rate_limit_rejects_after_burst() {
        let config = RateLimitConfig {
            max_ops_per_session_per_sec: 0,
            max_global_ops_per_sec: 4,
            max_concurrent_ops: 0,
        };
        let rl = RateLimiter::new(&config);

        // Use different sessions — global limit applies across all.
        for i in 0..4 {
            assert!(rl.check_rate(i).is_ok());
        }
        // 5th call (any session) should fail.
        assert!(rl.check_rate(100).is_err());
    }

    #[test]
    fn integer_bucket_does_not_drift_over_many_cycles() {
        // Verify that consuming and fully refilling many times doesn't
        // accumulate floating-point-style drift.  We manipulate `tokens`
        // directly (no wall-clock dependency) so the test is deterministic.
        let rate = 100u64;
        let state_capacity = rate.saturating_mul(MICROTOKENS_PER_OP);

        let mut tokens: u64 = state_capacity; // start full

        for _ in 0..10_000 {
            // Drain the entire bucket.
            let mut consumed = 0u64;
            while tokens >= MICROTOKENS_PER_OP {
                tokens -= MICROTOKENS_PER_OP;
                consumed += 1;
            }
            assert_eq!(
                consumed, rate,
                "bucket should yield exactly `rate` tokens after a full refill"
            );
            assert_eq!(
                tokens, 0,
                "residual microtokens should be exactly 0 after drain"
            );

            // Full refill: add capacity microtokens, cap at capacity.
            tokens = tokens.saturating_add(state_capacity).min(state_capacity);
        }

        // After 10 000 drain/refill cycles the bucket must still be exact.
        assert_eq!(
            tokens, state_capacity,
            "bucket should be at full capacity after final refill"
        );
    }

    #[test]
    fn session_bucket_count_is_bounded() {
        let config = RateLimitConfig {
            max_ops_per_session_per_sec: 10,
            max_global_ops_per_sec: 0,
            max_concurrent_ops: 0,
        };
        let rl = RateLimiter::new(&config);

        // Fill up to the maximum allowed session buckets.
        for i in 0..(MAX_SESSION_BUCKETS as u64) {
            assert!(
                rl.check_rate(i).is_ok(),
                "session {} should succeed while under limit",
                i,
            );
        }

        // The next *new* session must be rejected.
        let overflow_handle = MAX_SESSION_BUCKETS as u64 + 1;
        assert!(
            rl.check_rate(overflow_handle).is_err(),
            "new session beyond MAX_SESSION_BUCKETS must be rejected",
        );

        // An existing session should still work.
        assert!(
            rl.check_rate(0).is_ok(),
            "existing session should still be allowed",
        );
    }
}