1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::net::{IpAddr,Ipv4Addr,Ipv6Addr};
use enum_primitive::FromPrimitive;
use ikev2_transforms::*;
enum_from_primitive! {
#[derive(Debug,PartialEq)]
#[repr(u8)]
pub enum IkeExchangeType {
IkeSAInit = 34,
IkeAuth = 35,
CreateChildSA = 36,
Informational = 37,
}
}
enum_from_primitive! {
#[derive(Debug,PartialEq)]
#[repr(u8)]
pub enum IkeProtocolID {
Ike = 1,
Ah = 2,
Esp = 3,
}
}
enum_from_primitive! {
#[derive(Debug,PartialEq)]
#[repr(u8)]
pub enum IkeCertificateEncodingType {
Pkcs7X509 = 1,
Pgp = 2,
Dns = 3,
X509Sig = 4,
Kerberos = 6,
Crl = 7,
Arl = 8,
SpkiCert = 9,
X509Attr = 10,
RawRsa = 11,
HashUrlX509Cert = 12,
HashUrlX509Bundle = 13,
OCSPContent = 14,
RawPublicKey = 15,
}
}
pub const IKEV2_FLAG_INITIATOR : u8 = 0b1000;
pub const IKEV2_FLAG_VERSION : u8 = 0b10000;
pub const IKEV2_FLAG_RESPONSE : u8 = 0b100000;
#[derive(Debug,PartialEq)]
pub struct IkeV2Header<'a> {
pub init_spi: &'a[u8],
pub resp_spi: &'a[u8],
pub next_payload: u8,
pub maj_ver: u8,
pub min_ver: u8,
pub exch_type: u8,
pub flags: u8,
pub msg_id: u32,
pub length: u32,
}
enum_from_primitive! {
#[derive(Debug,PartialEq)]
#[repr(u8)]
pub enum IkePayloadType {
NoNextPayload = 0,
SecurityAssociation = 33,
KeyExchange = 34,
IdentInitiator = 35,
IdentResponder = 36,
Certificate = 37,
CertificateRequest = 38,
Authentication = 39,
Nonce = 40,
Notify = 41,
Delete = 42,
VendorID = 43,
TrafficSelectorInitiator = 44,
TrafficSelectorResponder = 45,
EncryptedAndAuthenticated = 46,
Configuration = 47,
ExtensibleAuthentication = 48,
}
}
#[derive(Debug,PartialEq)]
pub struct IkeV2GenericPayload<'a> {
pub hdr: IkeV2PayloadHeader,
pub payload: &'a[u8],
}
#[derive(Clone,Debug,PartialEq)]
pub struct IkeV2Proposal<'a> {
pub last: u8,
pub reserved: u8,
pub proposal_length: u16,
pub proposal_num: u8,
pub protocol_id: u8,
pub spi_size: u8,
pub num_transforms: u8,
pub spi: Option<&'a[u8]>,
pub transforms: Vec<IkeV2RawTransform<'a>>,
}
#[derive(Debug,PartialEq)]
pub struct KeyExchangePayload<'a> {
pub dh_group: u16,
pub reserved: u16,
pub kex_data: &'a[u8],
}
#[derive(Debug,PartialEq)]
pub struct IdentificationPayload<'a> {
pub id_type: u8,
pub reserved1: u8,
pub reserved2: u16,
pub ident_data: &'a[u8],
}
#[derive(Debug,PartialEq)]
pub struct CertificatePayload<'a> {
pub cert_encoding: u8,
pub cert_data: &'a[u8],
}
#[derive(Debug,PartialEq)]
pub struct CertificateRequestPayload<'a> {
pub cert_encoding: u8,
pub ca_data: &'a[u8],
}
#[derive(Debug,PartialEq)]
pub struct AuthenticationPayload<'a> {
pub auth_method: u8,
pub auth_data: &'a[u8],
}
#[derive(PartialEq)]
pub struct NoncePayload<'a> {
pub nonce_data: &'a[u8],
}
#[derive(PartialEq)]
pub struct NotifyPayload<'a> {
pub protocol_id: u8,
pub spi_size: u8,
pub notify_type: u16,
pub spi: Option<&'a[u8]>,
pub notify_data: Option<&'a[u8]>,
}
#[derive(Debug,PartialEq)]
pub struct DeletePayload<'a> {
pub protocol_id: u8,
pub spi_size: u8,
pub num_spi: u16,
pub spi: &'a[u8],
}
#[derive(Debug,PartialEq)]
pub struct VendorIDPayload<'a> {
pub vendor_id: &'a[u8],
}
enum_from_primitive! {
#[derive(Debug,PartialEq)]
#[repr(u8)]
pub enum TSType {
IPv4AddrRange = 7,
IPv6AddrRange = 8,
}
}
#[derive(Debug,PartialEq)]
pub struct TrafficSelector<'a> {
pub ts_type: u8,
pub ip_proto_id: u8,
pub sel_length: u16,
pub start_port: u16,
pub end_port: u16,
pub start_addr: &'a[u8],
pub end_addr: &'a[u8],
}
fn ipv4_from_slice(b:&[u8]) -> Ipv4Addr {
Ipv4Addr::new(b[0], b[1], b[2], b[3])
}
fn ipv6_from_slice(b:&[u8]) -> Ipv6Addr {
Ipv6Addr::new(
(b[0] as u16) << 8 | (b[1] as u16),
(b[2] as u16) << 8 | (b[3] as u16),
(b[4] as u16) << 8 | (b[5] as u16),
(b[6] as u16) << 8 | (b[7] as u16),
(b[8] as u16) << 8 | (b[9] as u16),
(b[10] as u16) << 8 | (b[11] as u16),
(b[12] as u16) << 8 | (b[13] as u16),
(b[14] as u16) << 8 | (b[15] as u16),
)
}
impl<'a> TrafficSelector<'a> {
pub fn get_ts_type(&self) -> Option<TSType> {
TSType::from_u8(self.ts_type)
}
pub fn get_start_addr(&self) -> Option<IpAddr> {
match self.ts_type {
7 => Some(IpAddr::V4(ipv4_from_slice(self.start_addr))),
8 => Some(IpAddr::V6(ipv6_from_slice(self.start_addr))),
_ => None,
}
}
pub fn get_end_addr(&self) -> Option<IpAddr> {
match self.ts_type {
7 => Some(IpAddr::V4(ipv4_from_slice(self.end_addr))),
8 => Some(IpAddr::V6(ipv6_from_slice(self.end_addr))),
_ => None,
}
}
}
#[derive(Debug,PartialEq)]
pub struct TrafficSelectorPayload<'a> {
pub num_ts: u8,
pub reserved: &'a[u8],
pub ts: Vec<TrafficSelector<'a>>,
}
#[derive(Debug,PartialEq)]
pub enum IkeV2PayloadContent<'a> {
SA(Vec<IkeV2Proposal<'a>>),
KE(KeyExchangePayload<'a>),
IDi(IdentificationPayload<'a>),
IDr(IdentificationPayload<'a>),
Certificate(CertificatePayload<'a>),
CertificateRequest(CertificateRequestPayload<'a>),
Authentication(AuthenticationPayload<'a>),
Nonce(NoncePayload<'a>),
Notify(NotifyPayload<'a>),
Delete(DeletePayload<'a>),
VendorID(VendorIDPayload<'a>),
TSi(TrafficSelectorPayload<'a>),
TSr(TrafficSelectorPayload<'a>),
Unknown(&'a[u8]),
Dummy,
}
#[derive(Clone,Debug,PartialEq)]
pub struct IkeV2PayloadHeader {
pub next_payload_type: u8,
pub critical: bool,
pub reserved: u8,
pub payload_length: u16,
}
impl IkeV2PayloadHeader {
pub fn get_next_payload_type(&self) -> Option<IkePayloadType> {
IkePayloadType::from_u8(self.next_payload_type)
}
}
#[derive(Debug,PartialEq)]
pub struct IkeV2Payload<'a> {
pub hdr: IkeV2PayloadHeader,
pub content: IkeV2PayloadContent<'a>,
}