From a623021e0f5390dc91e4ad5004a49fa07136ddfc Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Sat, 17 Jun 2023 08:24:33 -0400 Subject: [PATCH] extensions: add `Extensions::iter` (#9081) * extensions: add `Extensions::iter` This will make it easier to do criticality checks across the whole extensions sequence. Signed-off-by: William Woodruff * extensions: add an `Extensions::iter` test Signed-off-by: William Woodruff --------- Signed-off-by: William Woodruff --- src/rust/cryptography-x509/src/extensions.rs | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/rust/cryptography-x509/src/extensions.rs b/src/rust/cryptography-x509/src/extensions.rs index 51c283af352c..2191bc1da16c 100644 --- a/src/rust/cryptography-x509/src/extensions.rs +++ b/src/rust/cryptography-x509/src/extensions.rs @@ -56,6 +56,15 @@ impl<'a> Extensions<'a> { pub fn as_raw(&self) -> &Option> { &self.0 } + + /// Returns an iterator over the underlying extensions. + pub fn iter(&self) -> impl Iterator { + self.as_raw() + .clone() + .map(|raw| raw.unwrap_read().clone()) + .into_iter() + .flatten() + } } #[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Eq, Hash, Clone)] @@ -252,4 +261,26 @@ mod tests { .get_extension(&AUTHORITY_KEY_IDENTIFIER_OID) .is_none()); } + + #[test] + fn test_extensions_iter() { + let extension_value = BasicConstraints { + ca: true, + path_length: Some(3), + }; + let extension = Extension { + extn_id: BASIC_CONSTRAINTS_OID, + critical: true, + extn_value: &asn1::write_single(&extension_value).unwrap(), + }; + let extensions = SequenceOfWriter::new(vec![extension]); + + let der = asn1::write_single(&extensions).unwrap(); + + let extensions: Extensions = + Extensions::from_raw_extensions(Some(&asn1::parse_single(&der).unwrap())).unwrap(); + + let extension_list: Vec<_> = extensions.iter().collect(); + assert_eq!(extension_list.len(), 1); + } }