Skip to main content

structured_pkix/
der.rs

1//! Holding a range to the distinguished rules.
2//!
3//! Certificates are not the only structure whose schema fixes its encoding
4//! regardless of the envelope carrying it. A signed message is BER, and the
5//! attributes inside it that a signature covers "MUST be DER encoded, even if
6//! the rest of the structure is BER encoded" (RFC 5652 5.3). The walk that
7//! decides that question already exists here, so it is exposed rather than
8//! written a second time: two answers to what counts as DER is one answer more
9//! than a product can defend.
10
11use crate::error::{Error, ErrorCode, Result};
12use structured_asn1::{Element, Reader};
13
14/// Whether a range and everything under it is DER.
15///
16/// Definite lengths in their shortest form, primitive encodings where the
17/// distinguished rules require them, assigned universal types, and the members
18/// of a set in ascending order of their encodings (X.690 11.6).
19///
20/// The reader decides which rules apply to the outermost header, so a caller
21/// holding a range to DER inside a BER document reads it again under DER first;
22/// [`structured_asn1::Reader::reread`] is what that is for.
23///
24/// # Errors
25///
26/// Reports which rule was broken and where.
27pub fn conforms<'a>(parent: &Reader<'a>, element: &Element<'a>, field: &'static str) -> Result<()> {
28    crate::name::walk_der(parent, element, field, crate::name::Strings::Checked)
29}
30
31/// Whether two members of a set are in the order the distinguished rules put
32/// them in.
33///
34/// "The encodings of the component values of a set-of value shall appear in
35/// ascending order" (X.690 11.6), comparing octet by octet with the shorter
36/// encoding treated as though padded with zeros.
37///
38/// It is separate from [`conforms`] because a set is not always written under
39/// a set's own tag: a signed message writes its attributes under a context tag
40/// and signs them under `SET OF`, so the ordering is the schema's to check
41/// where the tag no longer says so.
42///
43/// `parent` supplies the current execution account and original parser-local
44/// cumulative ceiling. Before comparison, one work unit per possible octet
45/// pair is admitted: the longer length, because X.690 compares the shorter
46/// encoding with virtual zero padding. A minimum of one unit covers an empty
47/// comparison. This is an upper bound; an unequal pair may stop the walk early.
48/// `parent` must belong to the session and source that produced `next`; this
49/// is a caller precondition, not a provenance check.
50///
51/// # Errors
52/// Returns a resource refusal at `next`'s original offset before comparing
53/// bytes. `Ok(false)` means incorrect order, independently of resource errors.
54pub fn set_ordered(
55    parent: &Reader<'_>,
56    previous: &[u8],
57    next: &Element<'_>,
58    field: &'static str,
59) -> Result<bool> {
60    let work = previous.len().max(next.as_bytes().len()).max(1) as u64;
61    parent
62        .charge(work)
63        .map_err(|error| Error::new(ErrorCode::Encoding(error.code()), field, next.span().start))?;
64    Ok(crate::name::der_set_ordered(previous, next.as_bytes()))
65}