aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / der / src / asn1 / oid.rs
| 1 | //! ASN.1 `OBJECT IDENTIFIER` |
| 2 | |
| 3 | use crate::{ |
| 4 | asn1::AnyRef, ord::OrdIsValueOrd, DecodeValue, EncodeValue, Error, FixedTag, Header, Length, |
| 5 | Reader, Result, Tag, Tagged, Writer, |
| 6 | }; |
| 7 | use const_oid::ObjectIdentifier; |
| 8 | |
| 9 | #[cfg(feature = "alloc")] |
| 10 | use super::Any; |
| 11 | |
| 12 | impl<'a> DecodeValue<'a> for ObjectIdentifier { |
| 13 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
| 14 | let mut buf = [0u8; ObjectIdentifier::MAX_SIZE]; |
| 15 | let slice = buf |
| 16 | .get_mut(..header.length.try_into()?) |
| 17 | .ok_or_else(|| Self::TAG.length_error())?; |
| 18 | |
| 19 | let actual_len = reader.read_into(slice)?.len(); |
| 20 | debug_assert_eq!(actual_len, header.length.try_into()?); |
| 21 | Ok(Self::from_bytes(slice)?) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl EncodeValue for ObjectIdentifier { |
| 26 | fn value_len(&self) -> Result<Length> { |
| 27 | Length::try_from(self.as_bytes().len()) |
| 28 | } |
| 29 | |
| 30 | fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { |
| 31 | writer.write(self.as_bytes()) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl FixedTag for ObjectIdentifier { |
| 36 | const TAG: Tag = Tag::ObjectIdentifier; |
| 37 | } |
| 38 | |
| 39 | impl OrdIsValueOrd for ObjectIdentifier {} |
| 40 | |
| 41 | impl<'a> From<&'a ObjectIdentifier> for AnyRef<'a> { |
| 42 | fn from(oid: &'a ObjectIdentifier) -> AnyRef<'a> { |
| 43 | // Note: ensuring an infallible conversion is possible relies on the |
| 44 | // invariant that `const_oid::MAX_LEN <= Length::max()`. |
| 45 | // |
| 46 | // The `length()` test below ensures this is the case. |
| 47 | let value = oid |
| 48 | .as_bytes() |
| 49 | .try_into() |
| 50 | .expect("OID length invariant violated"); |
| 51 | |
| 52 | AnyRef::from_tag_and_value(Tag::ObjectIdentifier, value) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[cfg(feature = "alloc")] |
| 57 | impl From<ObjectIdentifier> for Any { |
| 58 | fn from(oid: ObjectIdentifier) -> Any { |
| 59 | AnyRef::from(&oid).into() |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl TryFrom<AnyRef<'_>> for ObjectIdentifier { |
| 64 | type Error = Error; |
| 65 | |
| 66 | fn try_from(any: AnyRef<'_>) -> Result<ObjectIdentifier> { |
| 67 | any.tag().assert_eq(Tag::ObjectIdentifier)?; |
| 68 | Ok(ObjectIdentifier::from_bytes(any.value())?) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod tests { |
| 74 | use super::ObjectIdentifier; |
| 75 | use crate::{Decode, Encode, Length}; |
| 76 | |
| 77 | const EXAMPLE_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549"); |
| 78 | const EXAMPLE_OID_BYTES: &[u8; 8] = &[0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d]; |
| 79 | |
| 80 | #[test] |
| 81 | fn decode() { |
| 82 | let oid = ObjectIdentifier::from_der(EXAMPLE_OID_BYTES).unwrap(); |
| 83 | assert_eq!(EXAMPLE_OID, oid); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn encode() { |
| 88 | let mut buffer = [0u8; 8]; |
| 89 | assert_eq!( |
| 90 | EXAMPLE_OID_BYTES, |
| 91 | EXAMPLE_OID.encode_to_slice(&mut buffer).unwrap() |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn length() { |
| 97 | // Ensure an infallible `From` conversion to `Any` will never panic |
| 98 | assert!(ObjectIdentifier::MAX_SIZE <= Length::MAX.try_into().unwrap()); |
| 99 | } |
| 100 | } |