1 use serde::{Deserialize, Serialize}; 2 3 /// User-provided raw DER wrapper. 4 /// 5 /// Allow user to provide raw DER: no tag is added by serializer and bytes are bumped as it. 6 /// Note that provided DER header has to be valid to determine length on deserialization. 7 /// 8 /// # Example 9 /// ``` 10 /// use picky_asn1_der::Asn1RawDer; 11 /// use serde::{Serialize, Deserialize}; 12 /// 13 /// #[derive(Serialize, Deserialize, PartialEq, Debug)] 14 /// struct A { 15 /// number: u8, 16 /// user_provided: Asn1RawDer, 17 /// } 18 /// 19 /// let plain_a = A { 20 /// number: 7, 21 /// user_provided: Asn1RawDer(vec![ 22 /// 0x30, 0x08, 23 /// 0x0C, 0x03, 0x41, 0x62, 0x63, 24 /// 0x02, 0x01, 0x05, 25 /// ]), 26 /// }; 27 /// 28 /// let serialized_a = picky_asn1_der::to_vec(&plain_a).expect("A to vec"); 29 /// assert_eq!( 30 /// serialized_a, 31 /// [ 32 /// 0x30, 0x0D, 33 /// 0x02, 0x01, 0x07, 34 /// 0x30, 0x08, 35 /// 0x0C, 0x03, 0x41, 0x62, 0x63, 36 /// 0x02, 0x01, 0x05, 37 /// ] 38 /// ); 39 /// 40 /// let deserialized_a = picky_asn1_der::from_bytes(&serialized_a).expect("A from bytes"); 41 /// assert_eq!(plain_a, deserialized_a); 42 /// 43 /// // we can deserialize into a compatible B structure. 44 /// 45 /// #[derive(Deserialize, Debug, PartialEq)] 46 /// struct B { 47 /// number: u8, 48 /// tuple: (String, u8), 49 /// } 50 /// 51 /// let plain_b = B { number: 7, tuple: ("Abc".to_owned(), 5) }; 52 /// let deserialized_b: B = picky_asn1_der::from_bytes(&serialized_a).expect("B from bytes"); 53 /// assert_eq!(deserialized_b, plain_b); 54 /// ``` 55 #[derive(Serialize, Deserialize, Debug, PartialEq, PartialOrd, Hash, Clone)] 56 pub struct Asn1RawDer(#[serde(with = "serde_bytes")] pub Vec<u8>); 57 58 impl Asn1RawDer { 59 pub const NAME: &'static str = "Asn1RawDer"; 60 } 61 62 #[cfg(test)] 63 mod tests { 64 use super::*; 65 use picky_asn1::wrapper::ApplicationTag0; 66 67 #[test] raw_der_behind_application_tag()68 fn raw_der_behind_application_tag() { 69 let encoded = crate::to_vec(&ApplicationTag0(Asn1RawDer(vec![0x02, 0x01, 0x07]))).expect("to vec"); 70 pretty_assertions::assert_eq!(encoded.as_slice(), [0xA0, 0x03, 0x02, 0x01, 0x07]); 71 72 let decoded: ApplicationTag0<Asn1RawDer> = crate::from_bytes(&encoded).expect("from bytes"); 73 pretty_assertions::assert_eq!((decoded.0).0.as_slice(), [0x02, 0x01, 0x07]); 74 } 75 } 76