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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Storage strategy for the one canonicalization kernel. The ordinary API
//! retains native storage; scoped XMLDSig uses the caller's actual execution.

use std::{cmp::Ordering, collections::HashMap, ops::Deref};

use super::C14nError;

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct NamespaceKey<'a> {
    pub prefix: &'a str,
    pub utilizer: bool,
}

pub(crate) trait NamespaceMap<'a>: Sized {
    fn get(&self, prefix: &str, utilizer: bool) -> Result<Option<Option<&'a str>>, C14nError>;
    fn insert(
        &mut self,
        prefix: &'a str,
        utilizer: bool,
        value: Option<&'a str>,
    ) -> Result<(), C14nError>;
    fn remove(&mut self, prefix: &'a str) -> Result<(), C14nError>;
    fn copy(&self) -> Result<Self, C14nError>;
}

pub(crate) trait List<T>: Deref<Target = [T]> + std::ops::DerefMut {
    #[cfg(feature = "xmldsig")]
    fn capacity(&self) -> usize;
    #[cfg(feature = "xmldsig")]
    fn reserve_exact(&mut self, additional: usize) -> Result<(), C14nError>;
    fn push(&mut self, value: T) -> Result<(), C14nError>;
    fn sort(
        &mut self,
        compare: impl FnMut(&T, &T) -> Result<Ordering, C14nError>,
    ) -> Result<(), C14nError>;
    fn pop(&mut self) -> Option<T>;
}

pub(crate) trait Text {
    fn view(&self) -> Result<&str, C14nError>;
}

pub(crate) trait TextBuilder {
    type Text: Text + Deref<Target = str>;
    fn append(&mut self, text: &str) -> Result<(), C14nError>;
    fn reserve(&mut self, bytes: usize) -> Result<(), C14nError>;
    fn last_byte(&self) -> Option<u8>;
    fn finish(self) -> Result<Self::Text, C14nError>;
}

#[cfg(feature = "xmldsig")]
pub(crate) trait Bytes: Deref<Target = [u8]> + std::ops::DerefMut {
    fn capacity(&self) -> usize;
    fn reserve(&mut self, additional: usize) -> Result<(), C14nError>;
    fn append(&mut self, bytes: &[u8]) -> Result<(), C14nError>;
    fn append_filtered(
        &mut self,
        bytes: &[u8],
        keep: impl FnMut(u8) -> bool,
    ) -> Result<(), C14nError>;
    fn zeros(&mut self, length: usize) -> Result<(), C14nError>;
    fn truncate(&mut self, length: usize);
}

#[cfg(feature = "xmldsig")]
impl Bytes for Vec<u8> {
    fn capacity(&self) -> usize {
        Vec::capacity(self)
    }
    fn append(&mut self, bytes: &[u8]) -> Result<(), C14nError> {
        self.extend_from_slice(bytes);
        Ok(())
    }
    fn reserve(&mut self, additional: usize) -> Result<(), C14nError> {
        Vec::reserve(self, additional);
        Ok(())
    }
    fn append_filtered(
        &mut self,
        bytes: &[u8],
        mut keep: impl FnMut(u8) -> bool,
    ) -> Result<(), C14nError> {
        self.extend(bytes.iter().copied().filter(|byte| keep(*byte)));
        Ok(())
    }
    fn zeros(&mut self, length: usize) -> Result<(), C14nError> {
        self.resize(length, 0);
        Ok(())
    }
    fn truncate(&mut self, length: usize) {
        Vec::truncate(self, length);
    }
}

impl Text for String {
    fn view(&self) -> Result<&str, C14nError> {
        Ok(self)
    }
}

impl TextBuilder for String {
    type Text = String;
    fn reserve(&mut self, bytes: usize) -> Result<(), C14nError> {
        self.reserve_exact(bytes);
        Ok(())
    }
    fn last_byte(&self) -> Option<u8> {
        self.as_bytes().last().copied()
    }
    fn finish(self) -> Result<String, C14nError> {
        Ok(self)
    }
    fn append(&mut self, text: &str) -> Result<(), C14nError> {
        self.push_str(text);
        Ok(())
    }
}

pub(crate) trait Scratch {
    #[cfg(feature = "xmldsig")]
    const ACCOUNTED: bool;
    type Values<T>: List<T>;
    #[cfg(feature = "xmldsig")]
    type Bytes: Bytes;
    type Namespaces<'a>: NamespaceMap<'a>;
    type Text: Text + Deref<Target = str>;
    type Builder: TextBuilder<Text = Self::Text>;
    fn builder(&self) -> Self::Builder;
    fn values<T>(&self) -> Self::Values<T>;
    #[cfg(feature = "xmldsig")]
    fn bytes(&self) -> Self::Bytes;
    fn namespaces<'a>(&self) -> Self::Namespaces<'a>;
    fn charge(&self, bytes: usize) -> Result<(), C14nError>;
    fn text(&self, parts: &[&str]) -> Result<Self::Text, C14nError>;
}

pub(crate) type NamespaceRendering<'a, S> = (
    <S as Scratch>::Values<(&'a str, &'a str)>,
    <S as Scratch>::Namespaces<'a>,
);

pub(crate) struct Native;

impl<T> List<T> for Vec<T> {
    #[cfg(feature = "xmldsig")]
    fn capacity(&self) -> usize {
        Vec::capacity(self)
    }
    #[cfg(feature = "xmldsig")]
    fn reserve_exact(&mut self, additional: usize) -> Result<(), C14nError> {
        self.try_reserve_exact(additional).map_err(|_| {
            C14nError::SharedExecution(structured_asn1::memory::MemoryError::OutOfMemory)
        })
    }
    fn pop(&mut self) -> Option<T> {
        Vec::pop(self)
    }
    fn push(&mut self, value: T) -> Result<(), C14nError> {
        Vec::push(self, value);
        Ok(())
    }
    fn sort(
        &mut self,
        mut compare: impl FnMut(&T, &T) -> Result<Ordering, C14nError>,
    ) -> Result<(), C14nError> {
        let mut failure = None;
        self.sort_unstable_by(|left, right| {
            if failure.is_some() {
                return Ordering::Equal;
            }
            match compare(left, right) {
                Ok(order) => order,
                Err(error) => {
                    failure = Some(error);
                    Ordering::Equal
                }
            }
        });
        failure.map_or(Ok(()), Err)
    }
}

#[derive(Clone, Default)]
pub(crate) struct NativeNamespaces<'a>(HashMap<NamespaceKey<'a>, Option<&'a str>>);

#[cfg(test)]
impl<'a> NativeNamespaces<'a> {
    pub(super) fn into_pairs(self) -> impl Iterator<Item = (&'a str, &'a str)> {
        self.0.into_iter().filter_map(|(key, value)| {
            if key.utilizer {
                None
            } else {
                value.map(|uri| (key.prefix, uri))
            }
        })
    }
}

impl<'a> NamespaceMap<'a> for NativeNamespaces<'a> {
    fn get(&self, prefix: &str, utilizer: bool) -> Result<Option<Option<&'a str>>, C14nError> {
        Ok(self.0.get(&NamespaceKey { prefix, utilizer }).copied())
    }
    fn insert(
        &mut self,
        prefix: &'a str,
        utilizer: bool,
        value: Option<&'a str>,
    ) -> Result<(), C14nError> {
        self.0.insert(NamespaceKey { prefix, utilizer }, value);
        Ok(())
    }
    fn remove(&mut self, prefix: &'a str) -> Result<(), C14nError> {
        self.0.remove(&NamespaceKey {
            prefix,
            utilizer: false,
        });
        Ok(())
    }
    fn copy(&self) -> Result<Self, C14nError> {
        Ok(self.clone())
    }
}

impl Scratch for Native {
    #[cfg(feature = "xmldsig")]
    const ACCOUNTED: bool = false;
    type Values<T> = Vec<T>;
    #[cfg(feature = "xmldsig")]
    type Bytes = Vec<u8>;
    type Namespaces<'a> = NativeNamespaces<'a>;
    type Text = String;
    type Builder = String;
    fn builder(&self) -> String {
        String::new()
    }
    fn values<T>(&self) -> Vec<T> {
        Vec::new()
    }
    #[cfg(feature = "xmldsig")]
    fn bytes(&self) -> Self::Bytes {
        Vec::new()
    }
    fn namespaces<'a>(&self) -> NativeNamespaces<'a> {
        NativeNamespaces::default()
    }
    fn charge(&self, _bytes: usize) -> Result<(), C14nError> {
        Ok(())
    }
    fn text(&self, parts: &[&str]) -> Result<String, C14nError> {
        Ok(parts.concat())
    }
}

#[cfg(feature = "xmldsig")]
mod accounted {
    use super::*;
    use structured_asn1::memory::{
        HashWork, MemoryError, OwnedMap, OwnedText, OwnedVec, SortError, TextErrorKind, Work,
    };

    pub(crate) struct Accounted<'w, W: Work>(pub &'w W);

    impl HashWork for NamespaceKey<'_> {
        fn hash_work(&self) -> Result<u64, MemoryError> {
            u64::try_from(self.prefix.len())
                .ok()
                .and_then(|n| n.checked_add(2))
                .ok_or(MemoryError::WorkExceeded)
        }
    }

    impl<T, W: Work> List<T> for OwnedVec<T, W> {
        fn capacity(&self) -> usize {
            OwnedVec::capacity(self)
        }
        fn reserve_exact(&mut self, additional: usize) -> Result<(), C14nError> {
            Ok(self.try_reserve_exact(additional)?)
        }
        fn pop(&mut self) -> Option<T> {
            OwnedVec::pop(self)
        }
        fn push(&mut self, value: T) -> Result<(), C14nError> {
            Ok(self.try_push(value)?)
        }
        fn sort(
            &mut self,
            compare: impl FnMut(&T, &T) -> Result<Ordering, C14nError>,
        ) -> Result<(), C14nError> {
            self.try_sort_unstable_by(compare)
                .map_err(|error| match error {
                    SortError::Resource(error) => C14nError::SharedExecution(error),
                    SortError::Comparison(error) => error,
                })
        }
    }

    impl<W: Work> Bytes for OwnedVec<u8, W> {
        fn capacity(&self) -> usize {
            OwnedVec::capacity(self)
        }
        fn append(&mut self, bytes: &[u8]) -> Result<(), C14nError> {
            Ok(self.try_extend_from_slice(bytes)?)
        }
        fn reserve(&mut self, additional: usize) -> Result<(), C14nError> {
            Ok(self.try_reserve_exact(additional)?)
        }
        fn append_filtered(
            &mut self,
            bytes: &[u8],
            mut keep: impl FnMut(u8) -> bool,
        ) -> Result<(), C14nError> {
            let mut chunk = [0; 1024];
            let mut used = 0;
            for byte in bytes {
                if !keep(*byte) {
                    continue;
                }
                chunk[used] = *byte;
                used += 1;
                if used == chunk.len() {
                    self.try_extend_from_slice(&chunk)?;
                    used = 0;
                }
            }
            Ok(self.try_extend_from_slice(&chunk[..used])?)
        }
        fn zeros(&mut self, length: usize) -> Result<(), C14nError> {
            self.try_reserve_exact(length)?;
            while self.len() < length {
                let remaining = length - self.len();
                self.try_extend_from_slice(&[0; 256][..remaining.min(256)])?;
            }
            Ok(())
        }
        fn truncate(&mut self, length: usize) {
            while self.len() > length {
                self.pop();
            }
        }
    }

    pub(crate) struct AccountedNamespaces<'w, 'a, W: Work> {
        values: OwnedMap<NamespaceKey<'a>, Option<&'a str>, W>,
        work: &'w W,
    }

    pub(crate) struct AccountedTextBuilder<W: Work> {
        bytes: OwnedVec<u8, W>,
    }
    impl<W: Work> Text for OwnedText<W> {
        fn view(&self) -> Result<&str, C14nError> {
            Ok(self.as_str())
        }
    }
    impl<W: Work> TextBuilder for AccountedTextBuilder<W> {
        type Text = OwnedText<W>;
        fn reserve(&mut self, bytes: usize) -> Result<(), C14nError> {
            Ok(self.bytes.try_reserve_exact(bytes)?)
        }
        fn last_byte(&self) -> Option<u8> {
            self.bytes.last().copied()
        }
        fn finish(self) -> Result<Self::Text, C14nError> {
            OwnedText::from_utf8(self.bytes).map_err(|error| match error.kind() {
                TextErrorKind::Resource(cause) => cause.into(),
                TextErrorKind::Utf8(_) => unreachable!("only UTF-8 fragments are appended"),
            })
        }
        fn append(&mut self, text: &str) -> Result<(), C14nError> {
            Ok(self.bytes.try_extend_from_slice(text.as_bytes())?)
        }
    }

    impl<'a, W: Work> NamespaceMap<'a> for AccountedNamespaces<'_, 'a, W> {
        fn get(&self, prefix: &str, utilizer: bool) -> Result<Option<Option<&'a str>>, C14nError> {
            Ok(self
                .values
                .get(&NamespaceKey { prefix, utilizer }, self.work)?
                .copied())
        }
        fn insert(
            &mut self,
            prefix: &'a str,
            utilizer: bool,
            value: Option<&'a str>,
        ) -> Result<(), C14nError> {
            self.values
                .try_insert(NamespaceKey { prefix, utilizer }, value)?;
            Ok(())
        }
        fn remove(&mut self, prefix: &'a str) -> Result<(), C14nError> {
            self.values.remove(
                &NamespaceKey {
                    prefix,
                    utilizer: false,
                },
                self.work,
            )?;
            Ok(())
        }
        fn copy(&self) -> Result<Self, C14nError> {
            let mut values = self.work.map();
            for (key, value) in self.values.try_iter(self.work)? {
                values.try_insert(*key, *value)?;
            }
            Ok(Self {
                values,
                work: self.work,
            })
        }
    }

    impl<'w, W: Work> Scratch for Accounted<'w, W> {
        const ACCOUNTED: bool = true;
        type Values<T> = OwnedVec<T, W>;
        type Bytes = OwnedVec<u8, W>;
        type Namespaces<'a> = AccountedNamespaces<'w, 'a, W>;
        type Text = OwnedText<W>;
        type Builder = AccountedTextBuilder<W>;
        fn builder(&self) -> Self::Builder {
            AccountedTextBuilder {
                bytes: self.0.vector(),
            }
        }
        fn values<T>(&self) -> Self::Values<T> {
            self.0.vector()
        }
        fn bytes(&self) -> Self::Bytes {
            self.0.vector()
        }
        fn namespaces<'a>(&self) -> Self::Namespaces<'a> {
            AccountedNamespaces {
                values: self.0.map(),
                work: self.0,
            }
        }
        fn charge(&self, bytes: usize) -> Result<(), C14nError> {
            self.0
                .charge_work(u64::try_from(bytes).map_err(|_| MemoryError::WorkExceeded)?)?;
            Ok(())
        }
        fn text(&self, parts: &[&str]) -> Result<Self::Text, C14nError> {
            if let [text] = parts {
                return Ok(OwnedText::try_from_str(text, self.0)?);
            }
            let mut text = self.builder();
            let length = parts
                .iter()
                .try_fold(0usize, |n, part| n.checked_add(part.len()))
                .ok_or(MemoryError::CapacityOverflow)?;
            text.reserve(length)?;
            for part in parts {
                text.append(part)?;
            }
            text.finish()
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use std::sync::Arc;
        use structured_asn1::memory::{Memory, MemoryAccount};

        #[test]
        fn refused_uri_text_validation_releases_buffer_and_preserves_cause() {
            // Exhaust the local query after construction: freezing must refuse
            // before scanning and release the consumed builder's allocation.
            use structured_asn1::{Encoding, Limits, Parser};
            let memory = Memory::new(Arc::new(MemoryAccount::new(4096, 1 << 20)));
            let parser = Parser::new(
                Encoding::Der,
                Limits {
                    max_work: 4096,
                    ..Default::default()
                },
                memory.clone(),
            );
            let reader = parser.read(&[], 0).expect("empty query carrier");
            let work = reader.execution();
            let mut builder = Accounted(&work).builder();
            builder.append("fragment").expect("first URI fragment fits");
            builder.append("/tail").expect("second URI fragment fits");
            assert!(
                memory.used() > 0,
                "unfinished builder retains actual capacity"
            );
            work.charge_work((4096 - memory.work_used()) as u64)
                .expect("exhaust local query work");
            let before = memory.work_used();
            assert!(matches!(
                builder.finish(),
                Err(C14nError::SharedExecution(MemoryError::WorkExceeded))
            ));
            assert_eq!(memory.work_used(), before, "no scan before admission");
            assert_eq!(memory.used(), 0);
        }

        #[test]
        fn repeated_uri_text_views_do_not_rescan_or_copy_utf8() {
            // URI resolution reads completed fragments several times. Each
            // immutable view must reuse validation and the same allocation.
            let memory = Memory::new(Arc::new(MemoryAccount::new(4096, 1 << 20)));
            let mut builder = Accounted(&memory).builder();
            builder.reserve(16).expect("fixed URI capacity fits");
            builder.append("urn:").expect("URI scheme fits");
            builder.append("example").expect("URI path fits");
            builder.append("/next").expect("URI suffix fits");
            let original_pointer = builder.bytes.as_ptr();
            let capacity = memory.used();
            let text = builder.finish().expect("complete UTF-8 URI");
            assert_eq!(
                text.as_bytes().as_ptr(),
                original_pointer,
                "zero-copy finish"
            );
            assert_eq!(memory.used(), capacity, "finishing does not allocate");
            let pointer = text.view().expect("first UTF-8 view").as_ptr();
            let work = memory.work_used();
            assert_eq!(text.view().expect("repeated view"), "urn:example/next");
            assert_eq!(text.view().expect("same allocation").as_ptr(), pointer);
            assert_eq!(memory.work_used(), work, "no repeated UTF-8 scan");
            assert_eq!(memory.used(), capacity, "no view allocation");
            drop(text);
            assert_eq!(memory.used(), 0, "final storage owner releases capacity");
        }
    }
}

#[cfg(feature = "xmldsig")]
pub(crate) use accounted::Accounted;