ipsec_parser/
ikev2_transforms.rs

1use rusticata_macros::newtype_enum;
2use std::convert::From;
3
4/// Transform (cryptographic algorithm) type
5///
6/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3.2
7#[derive(Clone, Copy, PartialEq, Eq)]
8pub struct IkeTransformType(pub u8);
9
10newtype_enum! {
11impl debug IkeTransformType {
12    EncryptionAlgorithm     = 1,
13    PseudoRandomFunction    = 2,
14    IntegrityAlgorithm      = 3,
15    DiffieHellmanGroup      = 4,
16    ExtendedSequenceNumbers = 5,
17}
18}
19
20/// Encryption values
21///
22/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3.2
23///
24/// See also [IKEV2IANA](https://www.iana.org/assignments/ikev2-parameters/ikev2-parameters.xhtml) for the latest values.
25#[derive(Clone, Copy, PartialEq, Eq)]
26pub struct IkeTransformEncType(pub u16);
27
28newtype_enum! {
29impl debug IkeTransformEncType {
30    // 0 is reserved
31    ENCR_DES_IV64           = 1,
32    ENCR_DES                = 2,
33    ENCR_3DES               = 3,
34    ENCR_RC5                = 4,
35    ENCR_IDEA               = 5,
36    ENCR_CAST               = 6,
37    ENCR_BLOWFISH           = 7,
38    ENCR_3IDEA              = 8,
39    ENCR_DES_IV32           = 9,
40    // 10 is reserved
41    ENCR_NULL                = 11,
42    ENCR_AES_CBC             = 12,
43    ENCR_AES_CTR             = 13,
44    ENCR_AES_CCM_8           = 14,
45    ENCR_AES_CCM_12          = 15,
46    ENCR_AES_CCM_16          = 16,
47    // 17 is unassigned
48    ENCR_AES_GCM_8           = 18,
49    ENCR_AES_GCM_12          = 19,
50    ENCR_AES_GCM_16          = 20,
51    ENCR_NULL_AUTH_AES_GMAC  = 21,
52    // 22 is reserved for IEEE P1619 XTS-AES
53    ENCR_CAMELLIA_CBC        = 23,
54    ENCR_CAMELLIA_CTR        = 24,
55    ENCR_CAMELLIA_CCM_8      = 25,
56    ENCR_CAMELLIA_CCM_12     = 26,
57    ENCR_CAMELLIA_CCM_16     = 27,
58    ENCR_CHACHA20_POLY1305   = 28, // [RFC7634]
59}
60}
61
62impl IkeTransformEncType {
63    pub fn is_aead(self) -> bool {
64        matches!(
65            self,
66            IkeTransformEncType::ENCR_AES_CCM_8
67                | IkeTransformEncType::ENCR_AES_CCM_12
68                | IkeTransformEncType::ENCR_AES_CCM_16
69                | IkeTransformEncType::ENCR_AES_GCM_8
70                | IkeTransformEncType::ENCR_AES_GCM_12
71                | IkeTransformEncType::ENCR_AES_GCM_16
72                | IkeTransformEncType::ENCR_CAMELLIA_CCM_8
73                | IkeTransformEncType::ENCR_CAMELLIA_CCM_12
74                | IkeTransformEncType::ENCR_CAMELLIA_CCM_16
75                | IkeTransformEncType::ENCR_CHACHA20_POLY1305
76        )
77    }
78
79    pub fn is_unassigned(self) -> bool {
80        self.0 >= 23 && self.0 <= 1023
81    }
82    pub fn is_private_use(self) -> bool {
83        self.0 >= 1024
84    }
85}
86
87/// Pseudo-Random Function values
88///
89/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3.2
90///
91/// See also [IKEV2IANA](https://www.iana.org/assignments/ikev2-parameters/ikev2-parameters.xhtml) for the latest values.
92#[derive(Clone, Copy, PartialEq, Eq)]
93pub struct IkeTransformPRFType(pub u16);
94
95newtype_enum! {
96impl debug IkeTransformPRFType {
97    PRF_NULL          = 0,
98    PRF_HMAC_MD5      = 1,
99    PRF_HMAC_SHA1     = 2,
100    PRF_HMAC_TIGER    = 3,
101    PRF_AES128_XCBC   = 4,
102    PRF_HMAC_SHA2_256 = 5,
103    PRF_HMAC_SHA2_384 = 6,
104    PRF_HMAC_SHA2_512 = 7,
105    PRF_AES128_CMAC   = 8,
106}
107}
108
109impl IkeTransformPRFType {
110    pub fn is_unassigned(self) -> bool {
111        self.0 >= 9 && self.0 <= 1023
112    }
113    pub fn is_private_use(self) -> bool {
114        self.0 >= 1024
115    }
116}
117
118/// Authentication / Integrity values
119///
120/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3.2
121#[derive(Clone, Copy, PartialEq, Eq)]
122pub struct IkeTransformAuthType(pub u16);
123
124newtype_enum! {
125impl debug IkeTransformAuthType {
126    NONE                   = 0,
127    AUTH_HMAC_MD5_96       = 1,
128    AUTH_HMAC_SHA1_96      = 2,
129    AUTH_DES_MAC           = 3,
130    AUTH_KPDK_MD5          = 4,
131    AUTH_AES_XCBC_96       = 5,
132    AUTH_HMAC_MD5_128      = 6,
133    AUTH_HMAC_SHA1_160     = 7,
134    AUTH_AES_CMAC_96       = 8,
135    AUTH_AES_128_GMAC      = 9,
136    AUTH_AES_192_GMAC      = 10,
137    AUTH_AES_256_GMAC      = 11,
138    AUTH_HMAC_SHA2_256_128 = 12,
139    AUTH_HMAC_SHA2_384_192 = 13,
140    AUTH_HMAC_SHA2_512_256 = 14,
141}
142}
143
144impl IkeTransformAuthType {
145    pub fn is_unassigned(self) -> bool {
146        self.0 >= 15 && self.0 <= 1023
147    }
148    pub fn is_private_use(self) -> bool {
149        self.0 >= 1024
150    }
151}
152
153/// Diffie-Hellman values
154///
155/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3.2
156///
157/// See also [IKEV2IANA](https://www.iana.org/assignments/ikev2-parameters/ikev2-parameters.xhtml) for the latest values.
158#[derive(Clone, Copy, PartialEq, Eq)]
159pub struct IkeTransformDHType(pub u16);
160
161newtype_enum! {
162impl debug IkeTransformDHType {
163    None            = 0,
164    Modp768         = 1,
165    Modp1024        = 2,
166    Modp1536        = 5,
167    Modp2048        = 14,
168    Modp3072        = 15,
169    Modp4096        = 16,
170    Modp6144        = 17,
171    Modp8192        = 18,
172    Ecp256          = 19,
173    Ecp384          = 20,
174    Ecp521          = 21,
175    Modp1024s160    = 22,
176    Modp2048s224    = 23,
177    Modp2048s256    = 24,
178    Ecp192          = 25,
179    Ecp224          = 26,
180    BrainpoolP224r1 = 27,
181    BrainpoolP256r1 = 28,
182    BrainpoolP384r1 = 29,
183    BrainpoolP512r1 = 30,
184    Curve25519      = 31,
185    Curve448        = 32,
186}
187}
188
189impl IkeTransformDHType {
190    pub fn is_unassigned(self) -> bool {
191        self.0 >= 15 && self.0 <= 1023
192    }
193    pub fn is_private_use(self) -> bool {
194        self.0 >= 1024
195    }
196}
197
198/// Extended Sequence Number values
199///
200/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3.2
201#[derive(Clone, Copy, PartialEq, Eq)]
202pub struct IkeTransformESNType(pub u16);
203
204newtype_enum! {
205impl debug IkeTransformESNType {
206    NoESN = 0,
207    ESN   = 1,
208}
209}
210
211/// Raw representation of a transform (cryptographic algorithm) and parameters
212///
213/// Use the `From` method to convert it to a [`IkeV2Transform`](enum.IkeV2Transform.html)
214///
215/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3
216#[derive(Clone, PartialEq)]
217pub struct IkeV2RawTransform<'a> {
218    pub last: u8,
219    pub reserved1: u8,
220    pub transform_length: u16,
221    pub transform_type: IkeTransformType,
222    pub reserved2: u8,
223    pub transform_id: u16,
224    pub attributes: Option<&'a [u8]>,
225}
226
227/// IKEv2 Transform (cryptographic algorithm)
228///
229/// This structure is a simple representation of a transform, containing only the type (encryption,
230/// etc.). To store the parameters, use [`IkeV2RawTransform`](struct.IkeV2RawTransform.html).
231///
232/// Defined in [RFC7296](https://tools.ietf.org/html/rfc7296) section 3.3
233#[derive(Debug, PartialEq)]
234pub enum IkeV2Transform {
235    Encryption(IkeTransformEncType),
236    PRF(IkeTransformPRFType),
237    Auth(IkeTransformAuthType),
238    DH(IkeTransformDHType),
239    ESN(IkeTransformESNType),
240    /// Unknown tranform (type,id)
241    Unknown(IkeTransformType, u16),
242}
243
244impl<'a> From<&'a IkeV2RawTransform<'a>> for IkeV2Transform {
245    fn from(r: &IkeV2RawTransform) -> IkeV2Transform {
246        match r.transform_type {
247            IkeTransformType::EncryptionAlgorithm => {
248                IkeV2Transform::Encryption(IkeTransformEncType(r.transform_id))
249            }
250            IkeTransformType::PseudoRandomFunction => {
251                IkeV2Transform::PRF(IkeTransformPRFType(r.transform_id))
252            }
253            IkeTransformType::IntegrityAlgorithm => {
254                IkeV2Transform::Auth(IkeTransformAuthType(r.transform_id))
255            }
256            IkeTransformType::DiffieHellmanGroup => {
257                IkeV2Transform::DH(IkeTransformDHType(r.transform_id))
258            }
259            IkeTransformType::ExtendedSequenceNumbers => {
260                IkeV2Transform::ESN(IkeTransformESNType(r.transform_id))
261            }
262            _ => IkeV2Transform::Unknown(r.transform_type, r.transform_id),
263        }
264    }
265}
266
267impl<'a> From<IkeV2RawTransform<'a>> for IkeV2Transform {
268    fn from(r: IkeV2RawTransform) -> IkeV2Transform {
269        (&r).into()
270    }
271}