Skip to content

Commit 99a35d9

Browse files
committed
cms: decode a MessageDigest from an Attribute
1 parent 4db4c09 commit 99a35d9

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cms/src/attr.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use core::borrow::Borrow;
55
use der::{
66
DecodeValue, EncodeValue, FixedTag, Length, Tag,
77
asn1::{OctetString, OctetStringRef},
8+
oid::db::rfc6268,
89
};
9-
use x509_cert::time::Time;
10+
11+
use x509_cert::{attr::Attribute, time::Time};
1012

1113
use crate::signed_data::SignerInfo;
1214

@@ -101,6 +103,30 @@ impl From<MessageDigest> for vec::Vec<u8> {
101103
}
102104
}
103105

106+
impl TryFrom<&Attribute> for MessageDigest {
107+
type Error = der::Error;
108+
109+
fn try_from(attr: &Attribute) -> Result<Self, Self::Error> {
110+
if attr.oid != rfc6268::ID_MESSAGE_DIGEST {
111+
return Err(der::ErrorKind::OidUnknown { oid: attr.oid }.into());
112+
}
113+
114+
// A message-digest attribute MUST have a single attribute value, even
115+
// though the syntax is defined as a SET OF AttributeValue. There MUST
116+
// NOT be zero or multiple instances of AttributeValue present.
117+
118+
if attr.values.len() != 1 {
119+
return Err(der::ErrorKind::Value { tag: Tag::Set }.into());
120+
}
121+
let message_digest = attr
122+
.values
123+
.get(0)
124+
.expect("Invariant violation, only one value is present in the attribute");
125+
126+
message_digest.decode_as::<OctetString>().map(Self)
127+
}
128+
}
129+
104130
/// The `SigningTime` attribute is defined in [RFC 5652 Section 11.3].
105131
///
106132
/// ```text

0 commit comments

Comments
 (0)