Skip to main content
This is unreleased documentation for the main (development) branch of crypto-glue.

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