Skip to content

cms: decode a MessageDigest from an Attribute #1992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion cms/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use alloc::{boxed::Box, vec};
use der::{
DecodeValue, EncodeValue, FixedTag, Length, Tag,
asn1::{OctetString, OctetStringRef},
oid::db::rfc6268,
referenced::OwnedToRef,
};

use x509_cert::time::Time;
use x509_cert::{attr::Attribute, time::Time};

use crate::signed_data::SignerInfo;

Expand Down Expand Up @@ -101,6 +102,30 @@ impl From<MessageDigest> for vec::Vec<u8> {
}
}

impl TryFrom<&Attribute> for MessageDigest {
type Error = der::Error;

fn try_from(attr: &Attribute) -> Result<Self, Self::Error> {
if attr.oid != rfc6268::ID_MESSAGE_DIGEST {
return Err(der::ErrorKind::OidUnknown { oid: attr.oid }.into());
}

// A message-digest attribute MUST have a single attribute value, even
// though the syntax is defined as a SET OF AttributeValue. There MUST
// NOT be zero or multiple instances of AttributeValue present.

if attr.values.len() != 1 {
return Err(der::ErrorKind::Value { tag: Tag::Set }.into());
}
let message_digest = attr
.values
.get(0)
.expect("Invariant violation, only one value is present in the attribute");

message_digest.decode_as::<OctetString>().map(Self)
}
}

/// The `SigningTime` attribute is defined in [RFC 5652 Section 11.3].
///
/// ```text
Expand Down