Skip to main content

x509_cert/
macros.rs

1//! Macros used by this crate
2
3/// Implements the following traits for a newtype of a `der` decodable/encodable type:
4///
5/// - `From` conversions to/from the inner type
6/// - `AsRef` and `AsMut`
7/// - `DecodeValue` and `EncodeValue`
8/// - `FixedTag` mapping to the inner value's `FixedTag::TAG`
9///
10/// The main case is simplifying newtypes which need an `AssociatedOid`
11/// Optional type parameters follow the inner type, for example `P: Profile`.
12#[macro_export]
13macro_rules! impl_newtype {
14    ($newtype:ty, $inner:ty $(, $param:ident : $bound:path)*) => {
15        #[allow(unused_lifetimes)]
16        impl<'a $(, $param: $bound)*> From<$inner> for $newtype {
17            #[inline]
18            fn from(value: $inner) -> Self {
19                Self(value)
20            }
21        }
22
23        #[allow(unused_lifetimes)]
24        impl<'a $(, $param: $bound)*> From<$newtype> for $inner {
25            #[inline]
26            fn from(value: $newtype) -> Self {
27                value.0
28            }
29        }
30
31        #[allow(unused_lifetimes)]
32        impl<'a $(, $param: $bound)*> AsRef<$inner> for $newtype {
33            #[inline]
34            fn as_ref(&self) -> &$inner {
35                &self.0
36            }
37        }
38
39        #[allow(unused_lifetimes)]
40        impl<'a $(, $param: $bound)*> AsMut<$inner> for $newtype {
41            #[inline]
42            fn as_mut(&mut self) -> &mut $inner {
43                &mut self.0
44            }
45        }
46
47        #[allow(unused_lifetimes)]
48        impl<'a $(, $param: $bound)*> ::der::FixedTag for $newtype {
49            const TAG: ::der::Tag = <$inner as ::der::FixedTag>::TAG;
50        }
51
52        impl<'a $(, $param: $bound)*> ::der::DecodeValue<'a> for $newtype {
53            type Error = ::der::Error;
54
55            fn decode_value<R: ::der::Reader<'a>>(
56                decoder: &mut R,
57                header: ::der::Header,
58            ) -> ::der::Result<Self> {
59                Ok(Self(<$inner as ::der::DecodeValue>::decode_value(
60                    decoder, header,
61                )?))
62            }
63        }
64
65        #[allow(unused_lifetimes)]
66        impl<'a $(, $param: $bound)*> ::der::EncodeValue for $newtype {
67            fn encode_value(&self, encoder: &mut impl ::der::Writer) -> ::der::Result<()> {
68                self.0.encode_value(encoder)
69            }
70
71            fn value_len(&self) -> ::der::Result<::der::Length> {
72                self.0.value_len()
73            }
74        }
75
76        #[allow(unused_lifetimes)]
77        impl<'a $(, $param: $bound)*> ::der::ValueOrd for $newtype {
78            fn value_cmp(&self, other: &Self) -> ::der::Result<::core::cmp::Ordering> {
79                self.0.value_cmp(&other.0)
80            }
81        }
82    };
83}
84
85/// Implements the Criticality trait for every defined Extension paylooad
86macro_rules! impl_extension {
87    ($newtype:ty) => {
88        impl_extension!($newtype, critical = false);
89    };
90    ($newtype:ty, critical = $critical:expr) => {
91        impl crate::ext::Criticality for $newtype {
92            fn criticality(
93                &self,
94                _subject: &crate::name::Name,
95                _extensions: &[crate::ext::Extension],
96            ) -> bool {
97                $critical
98            }
99        }
100    };
101}
102
103/// Implements conversions between [`spki::SubjectPublicKeyInfo`] and [`SubjectKeyIdentifier`] or [`AuthorityKeyIdentifier`]
104macro_rules! impl_key_identifier {
105    ($newtype:ty, $out:expr) => {
106        #[cfg(feature = "builder")]
107        mod builder_key_identifier {
108            use super::*;
109            use der::asn1::OctetString;
110            use sha1::{Digest, Sha1};
111            use spki::SubjectPublicKeyInfoRef;
112
113            impl<'a> TryFrom<SubjectPublicKeyInfoRef<'a>> for $newtype {
114                type Error = der::Error;
115
116                fn try_from(issuer: SubjectPublicKeyInfoRef<'a>) -> Result<Self, Self::Error> {
117                    // https://www.rfc-editor.org/info/rfc5280#section-4.2.1.2
118                    //
119                    //  For CA certificates, subject key identifiers SHOULD be derived from
120                    //  the public key or a method that generates unique values.  Two common
121                    //  methods for generating key identifiers from the public key are:
122
123                    //     (1) The keyIdentifier is composed of the 160-bit SHA-1 hash of the
124                    //          value of the BIT STRING subjectPublicKey (excluding the tag,
125                    //          length, and number of unused bits).
126
127                    //     (2) The keyIdentifier is composed of a four-bit type field with
128                    //          the value 0100 followed by the least significant 60 bits of
129                    //          the SHA-1 hash of the value of the BIT STRING
130                    //          subjectPublicKey (excluding the tag, length, and number of
131                    //          unused bits).
132
133                    // Here we're using the first method
134
135                    let result = Sha1::digest(issuer.subject_public_key.raw_bytes());
136                    $out(result.as_slice())
137                }
138            }
139        }
140    };
141}