rullst-security 12.3.0

Runtime application security, RASP, OWASP headers, Login Jail, and WAF middleware for Rullst.
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
//! Zero-Trust Secret Management & In-Memory Zeroization (Rullst Vault).

use aes_gcm::{
    Aes256Gcm,
    aead::{Aead, Generate, KeyInit, Nonce, Payload},
};
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use std::fmt;
use zeroize::Zeroize;

const ENVELOPE_PREFIX: &str = "RULLST";
const LEGACY_ENVELOPE_PREFIX: &str = "ENC";
const ENVELOPE_VERSION: &str = "v2";
const LEGACY_ENVELOPE_VERSION: &str = "v1";
const DEFAULT_KEY_ID: &str = "default";
const KEY_LENGTH: usize = 32;
const NONCE_LENGTH: usize = 12;
const TAG_LENGTH: usize = 16;
const MAX_KEY_ID_LENGTH: usize = 128;
const AAD_DOMAIN: &[u8] = b"rullst-security:field-encryption:aes-256-gcm";

/// Zero-Trust wrapper for sensitive in-memory secrets (API keys, DB passwords, private tokens).
/// Zeroizes the wrapped value on drop, reducing how long that allocation keeps
/// the secret. It cannot erase prior copies or prevent process-memory capture.
pub struct VaultSecret<T: Zeroize> {
    inner: T,
}

impl<T: Zeroize> VaultSecret<T> {
    /// Creates a new VaultSecret wrapping a value.
    pub fn new(value: T) -> Self {
        Self { inner: value }
    }

    /// Accesses the inner secret value.
    pub fn expose_secret(&self) -> &T {
        &self.inner
    }
}

impl<T: Zeroize> Drop for VaultSecret<T> {
    fn drop(&mut self) {
        self.inner.zeroize();
    }
}

impl<T: Zeroize> fmt::Debug for VaultSecret<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "VaultSecret(***REDACTED***)")
    }
}

impl<T: Zeroize> fmt::Display for VaultSecret<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "***REDACTED***")
    }
}

/// Strongly typed failures produced by field-level encryption.
#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum VaultError {
    /// AES-256 requires exactly 32 bytes of key material.
    #[error("AES-256-GCM keys must be exactly {expected} bytes; received {actual}")]
    InvalidKeyLength {
        /// Required AES-256 key length.
        expected: usize,
        /// Supplied key length.
        actual: usize,
    },

    /// Key identifiers are required so encrypted fields can participate in key rotation.
    #[error("field-encryption key identifiers cannot be empty")]
    EmptyKeyId,

    /// Key identifiers have a conservative limit to keep envelopes bounded.
    #[error("field-encryption key identifier is too long (maximum {maximum}, received {actual})")]
    KeyIdTooLong {
        /// Maximum supported key identifier length.
        maximum: usize,
        /// Supplied key identifier length.
        actual: usize,
    },

    /// Key identifiers may contain only portable, delimiter-safe characters.
    #[error(
        "field-encryption key identifiers may contain only ASCII letters, digits, '.', '_' and '-'"
    )]
    InvalidKeyId,

    /// The envelope does not have the required fields or prefix.
    #[error("invalid field-encryption envelope")]
    InvalidEnvelope,

    /// The previous `v1` format was an irreversible hash and cannot be migrated by decryption.
    #[error("legacy irreversible field-encryption envelope '{version}' cannot be decrypted")]
    LegacyIrreversibleEnvelope {
        /// Legacy envelope version found in storage.
        version: String,
    },

    /// The envelope is well formed but belongs to an unknown future version.
    #[error("unsupported field-encryption envelope version '{version}'")]
    UnsupportedEnvelopeVersion {
        /// Unsupported envelope version found in storage.
        version: String,
    },

    /// A binary envelope component is not valid URL-safe base64.
    #[error("invalid base64 encoding in field-encryption envelope component '{component}'")]
    InvalidEnvelopeEncoding {
        /// Name of the malformed component.
        component: &'static str,
    },

    /// AES-GCM uses a fixed 96-bit nonce.
    #[error("invalid AES-GCM nonce length (expected {expected}, received {actual})")]
    InvalidNonceLength {
        /// Required nonce length.
        expected: usize,
        /// Supplied nonce length.
        actual: usize,
    },

    /// AES-GCM ciphertext must contain at least its authentication tag.
    #[error("encrypted payload is too short (minimum {minimum}, received {actual})")]
    CiphertextTooShort {
        /// Minimum ciphertext and tag length.
        minimum: usize,
        /// Supplied ciphertext length.
        actual: usize,
    },

    /// The operating system could not provide cryptographically secure randomness.
    #[error("operating-system randomness is unavailable")]
    RandomnessUnavailable,

    /// AES-GCM rejected an encryption request.
    #[error("AES-256-GCM encryption failed")]
    EncryptionFailed,

    /// Authentication failed because the key, AAD, nonce, tag, or ciphertext does not match.
    #[error("AES-256-GCM authentication failed")]
    AuthenticationFailed,

    /// The authenticated plaintext was not a UTF-8 string.
    #[error("decrypted field is not valid UTF-8")]
    InvalidPlaintextUtf8,

    /// No key with the envelope's key identifier exists in the supplied rotation keyring.
    #[error("field-encryption key '{key_id}' was not found in the keyring")]
    KeyNotFound {
        /// Identifier requested by the envelope.
        key_id: String,
    },
}

impl From<VaultError> for crate::error::SecurityError {
    fn from(error: VaultError) -> Self {
        Self::VaultError(error.to_string())
    }
}

/// AES-256-GCM field-level encryption helper.
///
/// Encrypted values use the versioned envelope
/// `RULLST:v2:<key-id>:<nonce>:<ciphertext-and-tag>`. The nonce and encrypted payload
/// are URL-safe base64 without padding. The envelope version, algorithm domain,
/// key identifier, and caller-provided AAD are authenticated by AES-GCM.
///
/// Keys must be exactly 32 bytes of high-entropy key material. Passwords must be
/// processed by a suitable password-based KDF before they are passed here.
pub struct FieldEncryptor;

impl FieldEncryptor {
    /// Number of bytes required for an AES-256 key.
    pub const KEY_LENGTH: usize = KEY_LENGTH;

    /// Current field-encryption envelope version.
    pub const ENVELOPE_VERSION: &'static str = ENVELOPE_VERSION;

    /// Encrypts a UTF-8 field with the default key identifier and no caller AAD.
    ///
    /// Prefer [`Self::encrypt_with_aad`] when a stable record identity is available.
    pub fn encrypt(plain_text: &str, secret_key: impl AsRef<[u8]>) -> Result<String, VaultError> {
        Self::encrypt_with_aad(plain_text, secret_key, b"")
    }

    /// Encrypts a UTF-8 field and binds it to caller-provided additional data.
    ///
    /// Suitable AAD includes a tenant, table, column, and stable record identifier.
    /// Decryption must receive the exact same bytes.
    pub fn encrypt_with_aad(
        plain_text: &str,
        secret_key: impl AsRef<[u8]>,
        aad: &[u8],
    ) -> Result<String, VaultError> {
        Self::encrypt_with_key_id(plain_text, secret_key, DEFAULT_KEY_ID, aad)
    }

    /// Encrypts a field with an explicit key identifier for key rotation.
    pub fn encrypt_with_key_id(
        plain_text: &str,
        secret_key: impl AsRef<[u8]>,
        key_id: impl Into<String>,
        aad: &[u8],
    ) -> Result<String, VaultError> {
        let key_id = key_id.into();
        validate_key_id(&key_id)?;
        let cipher = create_cipher(secret_key.as_ref())?;
        let nonce =
            Nonce::<Aes256Gcm>::try_generate().map_err(|_| VaultError::RandomnessUnavailable)?;
        let authenticated_data = build_authenticated_data(&key_id, aad);
        let ciphertext = cipher
            .encrypt(
                &nonce,
                Payload {
                    msg: plain_text.as_bytes(),
                    aad: &authenticated_data,
                },
            )
            .map_err(|_| VaultError::EncryptionFailed)?;

        Ok(format!(
            "{ENVELOPE_PREFIX}:{ENVELOPE_VERSION}:{key_id}:{}:{}",
            URL_SAFE_NO_PAD.encode(nonce),
            URL_SAFE_NO_PAD.encode(ciphertext)
        ))
    }

    /// Decrypts a field encrypted with [`Self::encrypt`].
    pub fn decrypt(cipher_text: &str, secret_key: impl AsRef<[u8]>) -> Result<String, VaultError> {
        Self::decrypt_with_aad(cipher_text, secret_key, b"")
    }

    /// Decrypts a field and authenticates the caller-provided additional data.
    pub fn decrypt_with_aad(
        cipher_text: &str,
        secret_key: impl AsRef<[u8]>,
        aad: &[u8],
    ) -> Result<String, VaultError> {
        let envelope = parse_envelope(cipher_text)?;
        decrypt_envelope(&envelope, secret_key.as_ref(), aad)
    }

    /// Decrypts using the key selected by the authenticated envelope identifier.
    ///
    /// Keep the current key and any still-readable previous keys in `keyring` while
    /// records are re-encrypted during rotation.
    pub fn decrypt_with_keyring(
        cipher_text: &str,
        keyring: &[(&str, &[u8])],
        aad: &[u8],
    ) -> Result<String, VaultError> {
        let envelope = parse_envelope(cipher_text)?;
        let key = keyring
            .iter()
            .find_map(|(key_id, key)| (*key_id == envelope.key_id).then_some(*key))
            .ok_or_else(|| VaultError::KeyNotFound {
                key_id: envelope.key_id.to_string(),
            })?;

        decrypt_envelope(&envelope, key, aad)
    }

    /// Returns the key identifier carried by a validated envelope.
    pub fn envelope_key_id(cipher_text: &str) -> Result<&str, VaultError> {
        Ok(parse_envelope(cipher_text)?.key_id)
    }
}

struct ParsedEnvelope<'a> {
    key_id: &'a str,
    nonce: Vec<u8>,
    ciphertext: Vec<u8>,
}

fn create_cipher(secret_key: &[u8]) -> Result<Aes256Gcm, VaultError> {
    if secret_key.len() != KEY_LENGTH {
        return Err(VaultError::InvalidKeyLength {
            expected: KEY_LENGTH,
            actual: secret_key.len(),
        });
    }

    Aes256Gcm::new_from_slice(secret_key).map_err(|_| VaultError::InvalidKeyLength {
        expected: KEY_LENGTH,
        actual: secret_key.len(),
    })
}

fn validate_key_id(key_id: &str) -> Result<(), VaultError> {
    if key_id.is_empty() {
        return Err(VaultError::EmptyKeyId);
    }
    if key_id.len() > MAX_KEY_ID_LENGTH {
        return Err(VaultError::KeyIdTooLong {
            maximum: MAX_KEY_ID_LENGTH,
            actual: key_id.len(),
        });
    }
    if !key_id.bytes().all(is_valid_key_id_byte) {
        return Err(VaultError::InvalidKeyId);
    }

    Ok(())
}

const fn is_valid_key_id_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-')
}

fn build_authenticated_data(key_id: &str, caller_aad: &[u8]) -> Vec<u8> {
    let mut aad = Vec::new();
    aad.extend_from_slice(AAD_DOMAIN);
    aad.push(0);
    aad.extend_from_slice(ENVELOPE_VERSION.as_bytes());
    aad.push(0);
    aad.extend_from_slice(key_id.as_bytes());
    aad.push(0);
    aad.extend_from_slice(caller_aad);
    aad
}

fn parse_envelope(cipher_text: &str) -> Result<ParsedEnvelope<'_>, VaultError> {
    let mut fields = cipher_text.split(':');
    if !matches!(
        fields.next(),
        Some(ENVELOPE_PREFIX | LEGACY_ENVELOPE_PREFIX)
    ) {
        return Err(VaultError::InvalidEnvelope);
    }

    let version = fields.next().ok_or(VaultError::InvalidEnvelope)?;
    if version == LEGACY_ENVELOPE_VERSION {
        return Err(VaultError::LegacyIrreversibleEnvelope {
            version: version.to_string(),
        });
    }
    if version != ENVELOPE_VERSION {
        return Err(VaultError::UnsupportedEnvelopeVersion {
            version: version.to_string(),
        });
    }

    let key_id = fields.next().ok_or(VaultError::InvalidEnvelope)?;
    let encoded_nonce = fields.next().ok_or(VaultError::InvalidEnvelope)?;
    let encoded_ciphertext = fields.next().ok_or(VaultError::InvalidEnvelope)?;
    if fields.next().is_some() {
        return Err(VaultError::InvalidEnvelope);
    }
    validate_key_id(key_id)?;

    let nonce = URL_SAFE_NO_PAD
        .decode(encoded_nonce)
        .map_err(|_| VaultError::InvalidEnvelopeEncoding { component: "nonce" })?;
    if nonce.len() != NONCE_LENGTH {
        return Err(VaultError::InvalidNonceLength {
            expected: NONCE_LENGTH,
            actual: nonce.len(),
        });
    }

    let ciphertext = URL_SAFE_NO_PAD.decode(encoded_ciphertext).map_err(|_| {
        VaultError::InvalidEnvelopeEncoding {
            component: "ciphertext",
        }
    })?;
    if ciphertext.len() < TAG_LENGTH {
        return Err(VaultError::CiphertextTooShort {
            minimum: TAG_LENGTH,
            actual: ciphertext.len(),
        });
    }

    Ok(ParsedEnvelope {
        key_id,
        nonce,
        ciphertext,
    })
}

fn decrypt_envelope(
    envelope: &ParsedEnvelope<'_>,
    secret_key: &[u8],
    aad: &[u8],
) -> Result<String, VaultError> {
    let cipher = create_cipher(secret_key)?;
    let nonce = Nonce::<Aes256Gcm>::try_from(envelope.nonce.as_slice()).map_err(|_| {
        VaultError::InvalidNonceLength {
            expected: NONCE_LENGTH,
            actual: envelope.nonce.len(),
        }
    })?;
    let authenticated_data = build_authenticated_data(envelope.key_id, aad);
    let plaintext = cipher
        .decrypt(
            &nonce,
            Payload {
                msg: &envelope.ciphertext,
                aad: &authenticated_data,
            },
        )
        .map_err(|_| VaultError::AuthenticationFailed)?;

    match String::from_utf8(plaintext) {
        Ok(plaintext) => Ok(plaintext),
        Err(error) => {
            let mut plaintext = error.into_bytes();
            plaintext.zeroize();
            Err(VaultError::InvalidPlaintextUtf8)
        }
    }
}

#[cfg(test)]
mod tests;

#[cfg(kani)]
#[cfg_attr(mutants, mutants::skip)]
mod kani_proofs {
    use super::*;

    #[kani::proof]
    fn proof_key_id_byte_policy() {
        let byte: u8 = kani::any();
        let expected = byte.is_ascii_uppercase()
            || byte.is_ascii_lowercase()
            || byte.is_ascii_digit()
            || matches!(byte, b'.' | b'_' | b'-');

        assert_eq!(is_valid_key_id_byte(byte), expected);
    }
}