xml-sec 0.1.23

Pure Rust XML Security: XMLDSig, XMLEnc, C14N. Drop-in replacement for libxmlsec1.
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
use std::borrow::Borrow;
use std::str::FromStr;
use std::{cell::RefCell, collections::HashSet, fmt, hash, ops::Deref, rc::Rc};
use structured_asn1::memory::{MemoryError, OwnedMap, SharedString, Work};

#[cfg(not(feature = "embedded"))]
pub type StorageText = Rc<str>;

/// Retained labels keep the storage owner chosen when their arena was built.
#[cfg(feature = "embedded")]
#[derive(Clone)]
pub enum StorageText {
    Native(Rc<str>),
    Current(SharedString),
}

#[cfg(feature = "embedded")]
impl From<Rc<str>> for StorageText {
    fn from(value: Rc<str>) -> Self {
        Self::Native(value)
    }
}

#[cfg(feature = "embedded")]
impl Deref for StorageText {
    type Target = str;
    fn deref(&self) -> &str {
        match self {
            Self::Native(value) => value,
            Self::Current(value) => value,
        }
    }
}

/// A callback-scoped view preserves retained label ownership without a copy.
#[cfg(feature = "embedded")]
#[derive(Clone, Copy)]
pub enum Label<'a> {
    Borrowed(&'a str),
    Static(&'static str),
    Current(&'a SharedString),
}

#[cfg(feature = "embedded")]
impl<'a> Label<'a> {
    pub fn as_str(self) -> &'a str {
        match self {
            Self::Borrowed(text) => text,
            Self::Static(text) => text,
            Self::Current(text) => text,
        }
    }
}

#[cfg(feature = "embedded")]
impl Deref for Label<'_> {
    type Target = str;
    fn deref(&self) -> &str {
        self.as_str()
    }
}

#[cfg(feature = "embedded")]
impl<'a> From<&'a str> for Label<'a> {
    fn from(text: &'a str) -> Self {
        Self::Borrowed(text)
    }
}

fn native_text(value: Rc<str>) -> StorageText {
    #[cfg(feature = "embedded")]
    {
        StorageText::Native(value)
    }
    #[cfg(not(feature = "embedded"))]
    {
        value
    }
}

#[derive(Clone)]
pub struct InternedString<S: Deref<Target = str> = StorageText>(S);

#[cfg(feature = "embedded")]
impl<S: Deref<Target = str>> structured_asn1::memory::HashWork for InternedString<S> {
    fn hash_work(&self) -> Result<u64, MemoryError> {
        structured_asn1::memory::HashWork::hash_work(&**self)
    }
}

impl InternedString {
    #[cfg(feature = "embedded")]
    pub(crate) fn label(&self) -> Label<'_> {
        match &self.0 {
            StorageText::Native(text) => Label::Borrowed(text),
            StorageText::Current(text) => Label::Current(text),
        }
    }
    #[allow(clippy::should_implement_trait)] // We implement FromStr; this is a convenience that returns T directly
    pub fn from_str(s: &str) -> InternedString {
        InternedString(native_text(Rc::<str>::from(s)))
    }
}

impl FromStr for InternedString {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(InternedString::from_str(s))
    }
}

impl<S: Deref<Target = str>> fmt::Debug for InternedString<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&**self, f)
    }
}

impl<S: Deref<Target = str>> fmt::Display for InternedString<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&**self, f)
    }
}

impl<S: Deref<Target = str>> PartialEq for InternedString<S> {
    fn eq(&self, other: &Self) -> bool {
        *self.0 == *other.0
    }
}

impl<S: Deref<Target = str>> PartialEq<str> for InternedString<S> {
    fn eq(&self, other: &str) -> bool {
        &*self.0 == other
    }
}

impl<S: Deref<Target = str>> PartialEq<&str> for InternedString<S> {
    fn eq(&self, other: &&str) -> bool {
        &*self.0 == *other
    }
}

impl<S: Deref<Target = str>> PartialEq<InternedString<S>> for &str {
    fn eq(&self, other: &InternedString<S>) -> bool {
        *self == &*other.0
    }
}

impl<S: Deref<Target = str>> Eq for InternedString<S> {}

impl<S: Deref<Target = str>> hash::Hash for InternedString<S> {
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher,
    {
        (*self.0).hash(state)
    }
}

impl<S: Deref<Target = str>> Borrow<str> for InternedString<S> {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl<S: Deref<Target = str>> Deref for InternedString<S> {
    type Target = str;

    fn deref(&self) -> &str {
        &self.0
    }
}

impl<S: Deref<Target = str>> From<InternedString<S>> for String {
    fn from(s: InternedString<S>) -> String {
        s.deref().to_owned()
    }
}

impl<S: Deref<Target = str>> PartialEq<String> for InternedString<S> {
    fn eq(&self, other: &String) -> bool {
        &*self.0 == other.as_str()
    }
}

trait PoolStorage {
    type Text: Clone + Deref<Target = str>;
    type Error;
    fn find(&self, text: &str) -> Result<Option<Self::Text>, Self::Error>;
    fn create(&self, text: &str) -> Result<Self::Text, Self::Error>;
    fn insert(&mut self, text: Self::Text) -> Result<(), Self::Error>;
}

// Both storage strategies use one lookup-before-create interning algorithm.
// In particular, a failed duplicate lookup cannot trigger a temporary string.
fn intern<S: PoolStorage>(
    storage: &RefCell<S>,
    text: &str,
) -> Result<InternedString<S::Text>, S::Error> {
    let mut storage = storage.borrow_mut();
    if let Some(existing) = storage.find(text)? {
        return Ok(InternedString(existing));
    }
    let value = storage.create(text)?;
    storage.insert(value.clone())?;
    Ok(InternedString(value))
}

#[derive(Default)]
struct NativePool(HashSet<Rc<str>>);
impl PoolStorage for NativePool {
    type Text = Rc<str>;
    type Error = std::convert::Infallible;
    fn find(&self, text: &str) -> Result<Option<Rc<str>>, Self::Error> {
        Ok(self.0.get(text).cloned())
    }
    fn create(&self, text: &str) -> Result<Rc<str>, Self::Error> {
        Ok(Rc::from(text))
    }
    fn insert(&mut self, text: Rc<str>) -> Result<(), Self::Error> {
        self.0.insert(text);
        Ok(())
    }
}

pub struct StringPool {
    index: RefCell<NativePool>,
}

impl Default for StringPool {
    fn default() -> Self {
        Self::new()
    }
}

impl StringPool {
    pub fn new() -> StringPool {
        StringPool {
            index: RefCell::new(NativePool::default()),
        }
    }

    pub fn intern(&self, s: &str) -> InternedString {
        if s.is_empty() {
            return InternedString::from_str("");
        }
        match intern(&self.index, s) {
            Ok(value) => InternedString(native_text(value.0)),
            Err(never) => match never {},
        }
    }
}

#[cfg(feature = "embedded")]
impl InternedString<SharedString> {
    pub(crate) fn into_retained(self) -> InternedString {
        InternedString(StorageText::Current(self.0))
    }
}

struct PaidPool<'w, W: Work> {
    index: OwnedMap<SharedString, (), W>,
    work: &'w W,
}
impl<W: Work> PoolStorage for PaidPool<'_, W> {
    type Text = SharedString;
    type Error = MemoryError;
    fn find(&self, text: &str) -> Result<Option<SharedString>, MemoryError> {
        Ok(self
            .index
            .get_key_value_borrowed(text, self.work)?
            .map(|(key, ())| key.clone()))
    }
    fn create(&self, text: &str) -> Result<SharedString, MemoryError> {
        SharedString::try_from_str(text, self.work)
    }
    fn insert(&mut self, text: SharedString) -> Result<(), MemoryError> {
        self.index.try_insert(text, ())?;
        Ok(())
    }
}

/// XML-owned interning with independently retained, fallibly created strings.
pub struct AccountedStringPool<'w, W: Work> {
    index: RefCell<PaidPool<'w, W>>,
}
impl<'w, W: Work> AccountedStringPool<'w, W> {
    /// Creates an empty interner without allocating storage.
    pub fn new(work: &'w W) -> Self {
        Self {
            index: RefCell::new(PaidPool {
                index: work.map(),
                work,
            }),
        }
    }
    /// Finds or creates a canonical string using this pool's current execution.
    pub fn intern(&self, text: &str) -> Result<InternedString<SharedString>, MemoryError> {
        intern(&self.index, text)
    }
    /// Reads retained strings using the supplied query, without rebinding growth.
    pub fn find(
        &self,
        text: &str,
        current: &impl Work,
    ) -> Result<Option<InternedString<SharedString>>, MemoryError> {
        Ok(self
            .index
            .borrow()
            .index
            .get_key_value_borrowed(text, current)?
            .map(|(key, ())| InternedString(key.clone())))
    }
}

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

    #[test]
    fn accounted_interning_retains_one_canonical_allocation() {
        // A borrowed duplicate lookup must not create a temporary shared string;
        // its canonical handle survives both source input and pool destruction.
        use structured_asn1::memory::{Memory, MemoryAccount, SharedString};
        let memory = Memory::new(std::sync::Arc::new(MemoryAccount::new(8192, 65536)));
        let pool = super::AccountedStringPool::new(&memory);
        let first = pool
            .intern(&String::from("namespace"))
            .expect("initial intern");
        let used = memory.used();
        let second = pool.intern("namespace").expect("duplicate intern");
        assert_eq!(memory.used(), used);
        assert!(SharedString::ptr_eq(&first.0, &second.0));
        drop(pool);
        assert_eq!(&*first, "namespace");
        assert_eq!(memory.used(), first.0.allocated_bytes());
        drop(first);
        assert_eq!(&*second, "namespace");
        drop(second);
        assert_eq!(memory.used(), 0);
    }

    #[test]
    fn retained_intern_lookup_uses_current_work_without_allocating() {
        // Original storage is not a licence to charge a completed producer;
        // a fresh query can clone the canonical key without any new capacity.
        use structured_asn1::{
            Encoding, Limits, Parser,
            memory::{Memory, MemoryAccount, MemoryError, Work},
        };
        let memory = Memory::new(std::sync::Arc::new(MemoryAccount::new(8192, 65536)));
        let producer = Parser::new(
            Encoding::Der,
            Limits {
                max_work: 4096,
                ..Default::default()
            },
            memory.clone(),
        );
        let reader = producer.read(&[], 0).expect("empty producer");
        let work = reader.execution();
        let pool = super::AccountedStringPool::new(&work);
        let first = pool.intern("namespace").expect("initial intern");
        for shift in (0..=12).rev() {
            while work.charge_work(1 << shift).is_ok() {}
        }
        let _held = memory
            .reserve(memory.limit() - memory.used())
            .expect("hold all remaining capacity");
        let query = Memory::new(std::sync::Arc::new(MemoryAccount::new(0, 65536)));
        let second = pool
            .find("namespace", &query)
            .expect("current lookup")
            .expect("canonical value");
        assert!(structured_asn1::memory::SharedString::ptr_eq(
            &first.0, &second.0
        ));
        assert_eq!(query.used(), 0);
        assert!(query.work_used() > 0);
        assert_eq!(
            pool.find("namespace", &work),
            Err(MemoryError::WorkExceeded)
        );
        drop(second);
        drop(first);
        drop(pool);
        drop(_held);
        assert_eq!(memory.used(), 0);
    }

    #[test]
    fn accounted_interning_refuses_storage_before_insertion() {
        // Failed string/table admission cannot leave a partially inserted key
        // or a leaked capacity charge in an otherwise empty interner.
        use structured_asn1::memory::{Memory, MemoryAccount, MemoryError};
        let memory = Memory::new(std::sync::Arc::new(MemoryAccount::new(0, 65536)));
        let pool = super::AccountedStringPool::new(&memory);
        assert_eq!(pool.intern("namespace"), Err(MemoryError::LimitExceeded));
        assert_eq!(pool.find("namespace", &memory).expect("empty lookup"), None);
        assert_eq!(memory.used(), 0);
    }

    #[test]
    fn keeps_the_same_string() {
        let s = StringPool::new();
        let interned = s.intern("hello");
        assert_eq!(&*interned, "hello");
    }

    #[test]
    fn reuses_rc_for_repeated_input() {
        let s = StringPool::new();
        let interned1 = s.intern("world");
        let interned2 = s.intern("world");
        assert_eq!(&*interned1, &*interned2);
    }

    #[test]
    fn ignores_the_lifetime_of_the_input_string() {
        let s = StringPool::new();
        let interned = {
            let allocated_string = "green".to_owned();
            s.intern(&allocated_string)
        };
        assert_eq!(&*interned, "green");
    }

    #[test]
    fn can_be_dropped_immediately() {
        StringPool::new();
    }

    #[test]
    fn does_not_reuse_the_pointer_of_the_input() {
        let s = StringPool::new();
        let input = "hello";
        let interned = s.intern(input);
        assert!(input.as_bytes().as_ptr() != interned.as_bytes().as_ptr());
    }

    #[test]
    fn can_return_storage_populated_with_values() {
        fn return_populated_storage() -> StringPool {
            let s = StringPool::new();
            s.intern("hello");
            s
        }
        let s = return_populated_storage();
        let interned = s.intern("hello");
        assert_eq!(&*interned, "hello");
    }
}