Skip to main content

Header

Struct Header 

Source
pub struct Header(/* private fields */);
Expand description

A COSE Headers / Generic_Headers map (RFC 9052 §3).

The underlying representation is a CoseMap, with header-specific accessors for common parameters. It dereferences to CoseMap so custom header parameters can still be inserted with raw labels and values.

Implementations§

Source§

impl Header

Source

pub fn new() -> Self

Creates an empty header map.

Source

pub fn from_slice(data: &[u8]) -> Result<Self, Error>

Decodes a header map from CBOR bytes.

Source

pub fn to_vec(&self) -> Result<Vec<u8>, Error>

Encodes the header map to canonical CBOR bytes.

Source

pub fn as_map(&self) -> &CoseMap

Returns a reference to the underlying map.

Source

pub fn as_mut_map(&mut self) -> &mut CoseMap

Returns a mutable reference to the underlying map.

Source

pub fn into_map(self) -> CoseMap

Consumes the header and returns the underlying map.

Source

pub fn iter(&self) -> Iter<'_, Label, Value>

Iterates over the (label, value) entries in label order.

Source

pub fn alg(&self) -> Result<Option<Label>, Error>

Returns the algorithm identifier (alg, label 1), if present.

COSE algorithm identifiers are int / tstr; this crate represents that shape with Label.

Source

pub fn set_alg(&mut self, alg: impl Into<Label>) -> &mut Self

Sets the algorithm identifier (alg, label 1).

Source

pub fn kid(&self) -> Result<Option<&[u8]>, Error>

Returns the key identifier (kid, label 4), if present.

Source

pub fn set_kid(&mut self, kid: impl Into<Vec<u8>>) -> &mut Self

Sets the key identifier (kid, label 4).

Source

pub fn content_type(&self) -> Result<Option<Label>, Error>

Returns the content type (content type, label 3), if present.

The value is tstr / uint (RFC 9052 §3.1); this crate represents that shape with Label.

Source

pub fn set_content_type(&mut self, content_type: impl Into<Label>) -> &mut Self

Sets the content type (content type, label 3).

Source

pub fn iv(&self) -> Result<Option<&[u8]>, Error>

Returns the full initialization vector (iv, label 5), if present.

Source

pub fn set_iv(&mut self, iv: impl Into<Vec<u8>>) -> &mut Self

Sets the full initialization vector (iv, label 5).

Source

pub fn partial_iv(&self) -> Result<Option<&[u8]>, Error>

Returns the partial initialization vector (Partial IV, label 6), if present.

Source

pub fn set_partial_iv(&mut self, partial_iv: impl Into<Vec<u8>>) -> &mut Self

Sets the partial initialization vector (Partial IV, label 6).

Source

pub fn crit(&self) -> Result<Option<Vec<Label>>, Error>

Returns the critical protected header labels (crit, label 2), if present.

Source

pub fn set_crit<I, L>(&mut self, labels: I) -> &mut Self
where I: IntoIterator<Item = L>, L: Into<Label>,

Sets the critical protected header labels (crit, label 2).

Source

pub fn ensure_crit_understood(&self, understood: &[Label]) -> Result<(), Error>

Enforces RFC 9052 §3.1 critical-header processing: every label listed in crit must be understood, otherwise processing the message is a fatal error.

A label is considered understood when it is one of the common header parameters this crate models natively (see is_understood_header) or when the caller lists it in understood. Applications that process untrusted messages SHOULD call this on the protected header of each layer (and each COSE_Signature) after decoding, passing the application-specific labels they are able to process.

Returns Ok(()) when there is no crit parameter or when every listed label is understood.

Methods from Deref<Target = CoseMap>§

Source

pub fn len(&self) -> usize

Returns the number of entries.

Source

pub fn is_empty(&self) -> bool

Returns true if the map has no entries.

Source

pub fn contains_key(&self, key: impl Into<Label>) -> bool

Returns true if the map contains the given label.

Source

pub fn get(&self, key: impl Into<Label>) -> Option<&Value>

Returns the raw Value for a label, if present.

Source

pub fn insert( &mut self, key: impl Into<Label>, value: impl Into<Value>, ) -> Option<Value>

Inserts a value, returning the previous value for the label if any.

Source

pub fn remove(&mut self, key: impl Into<Label>) -> Option<Value>

Removes and returns the value for a label, if present.

Source

pub fn iter(&self) -> Iter<'_, Label, Value>

Iterates over the (label, value) entries in label order.

Source

pub fn get_i64(&self, key: impl Into<Label>) -> Result<Option<i64>, Error>

Returns the value for a label as an i64.

Returns Ok(None) when the label is absent and an Error::UnexpectedType when the value is not an in-range integer.

Source

pub fn get_bytes(&self, key: impl Into<Label>) -> Result<Option<&[u8]>, Error>

Returns the value for a label as a byte string.

Source

pub fn get_text(&self, key: impl Into<Label>) -> Result<Option<&str>, Error>

Returns the value for a label as a text string.

Source

pub fn get_label(&self, key: impl Into<Label>) -> Result<Option<Label>, Error>

Returns the value for a label as an int / tstr COSE identifier.

Source

pub fn get_bool(&self, key: impl Into<Label>) -> Result<Option<bool>, Error>

Returns the value for a label as a boolean.

Source

pub fn get_array( &self, key: impl Into<Label>, ) -> Result<Option<&[Value]>, Error>

Returns the value for a label as a CBOR array.

Source

pub fn to_vec(&self) -> Result<Vec<u8>, Error>

Encodes the map to canonical (deterministic) CBOR bytes.

Trait Implementations§

Source§

impl Clone for Header

Source§

fn clone(&self) -> Header

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Header

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Header

Source§

fn default() -> Header

Returns the “default value” for a type. Read more
Source§

impl Deref for Header

Source§

type Target = CoseMap

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Header

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'de> Deserialize<'de> for Header

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<CoseMap> for Header

Source§

fn from(value: CoseMap) -> Self

Converts to this type from the input type.
Source§

impl From<Header> for CoseMap

Source§

fn from(value: Header) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<(Label, Value)> for Header

Source§

fn from_iter<T: IntoIterator<Item = (Label, Value)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for Header

Source§

fn eq(&self, other: &Header) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Header

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Header

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.