Skip to content

Commit

Permalink
extensions: add Extensions::iter (#9081)
Browse files Browse the repository at this point in the history
* extensions: add `Extensions::iter`

This will make it easier to do criticality checks across
the whole extensions sequence.

Signed-off-by: William Woodruff <[email protected]>

* extensions: add an `Extensions::iter` test

Signed-off-by: William Woodruff <[email protected]>

---------

Signed-off-by: William Woodruff <[email protected]>
  • Loading branch information
woodruffw committed Jun 17, 2023
1 parent 0fc7e79 commit a623021
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/rust/cryptography-x509/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ impl<'a> Extensions<'a> {
pub fn as_raw(&self) -> &Option<RawExtensions<'_>> {
&self.0
}

/// Returns an iterator over the underlying extensions.
pub fn iter(&self) -> impl Iterator<Item = Extension> {
self.as_raw()
.clone()
.map(|raw| raw.unwrap_read().clone())
.into_iter()
.flatten()
}
}

#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Eq, Hash, Clone)]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a623021

Please sign in to comment.