forked from mfontanini/libtins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eapol.cpp
286 lines (235 loc) · 7.79 KB
/
eapol.cpp
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
/*
* Copyright (c) 2017, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <cstring>
#include <tins/eapol.h>
#include <tins/exceptions.h>
#include <tins/rawpdu.h>
#include <tins/memory_helpers.h>
using std::memset;
using std::memcpy;
using Tins::Memory::InputMemoryStream;
using Tins::Memory::OutputMemoryStream;
namespace Tins {
PDU::metadata EAPOL::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
if (TINS_UNLIKELY(total_sz < sizeof(eapol_header))) {
throw malformed_packet();
}
const eapol_header* header = (const eapol_header*)buffer;
uint32_t advertised_size = Endian::be_to_host<uint16_t>(header->length) + 4;
const uint32_t actual_size = (total_sz < advertised_size) ? total_sz : advertised_size;
return metadata(actual_size, pdu_flag, PDU::UNKNOWN);
}
EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type)
: header_() {
header_.version = 1;
header_.packet_type = packet_type;
header_.type = (uint8_t)type;
}
EAPOL::EAPOL(const uint8_t* buffer, uint32_t total_sz) {
InputMemoryStream stream(buffer, total_sz);
stream.read(header_);
}
EAPOL* EAPOL::from_bytes(const uint8_t* buffer, uint32_t total_sz) {
if (TINS_UNLIKELY(total_sz < sizeof(eapol_header))) {
throw malformed_packet();
}
const eapol_header* ptr = (const eapol_header*)buffer;
uint32_t data_len = Endian::be_to_host<uint16_t>(ptr->length);
// at least 4 for fields always present
data_len += 4;
total_sz = (total_sz < data_len) ? total_sz : data_len;
switch(ptr->type) {
case RC4:
return new Tins::RC4EAPOL(buffer, total_sz);
break;
case RSN:
case EAPOL_WPA:
return new Tins::RSNEAPOL(buffer, total_sz);
break;
}
return 0;
}
void EAPOL::version(uint8_t new_version) {
header_.version = new_version;
}
void EAPOL::packet_type(uint8_t new_ptype) {
header_.packet_type = new_ptype;
}
void EAPOL::length(uint16_t new_length) {
header_.length = Endian::host_to_be(new_length);
}
void EAPOL::type(uint8_t new_type) {
header_.type = new_type;
}
void EAPOL::write_serialization(uint8_t* buffer, uint32_t total_sz) {
OutputMemoryStream stream(buffer, total_sz);
length(total_sz - 4);
stream.write(header_);
memcpy(buffer, &header_, sizeof(header_));
write_body(stream);
}
/* RC4EAPOL */
RC4EAPOL::RC4EAPOL()
: EAPOL(0x03, RC4) {
memset(&header_, 0, sizeof(header_));
}
RC4EAPOL::RC4EAPOL(const uint8_t* buffer, uint32_t total_sz)
: EAPOL(buffer, total_sz) {
InputMemoryStream stream(buffer, total_sz);
stream.skip(sizeof(eapol_header));
stream.read(header_);
if (stream.size() >= key_length()) {
stream.read(key_, key_length());
if (stream) {
inner_pdu(new RawPDU(stream.pointer(), stream.size()));
}
}
}
void RC4EAPOL::key_length(uint16_t length) {
header_.key_length = Endian::host_to_be(length);
}
void RC4EAPOL::replay_counter(uint64_t value) {
header_.replay_counter = Endian::host_to_be(value);
}
void RC4EAPOL::key_iv(const uint8_t* ptr) {
memcpy(header_.key_iv, ptr, sizeof(header_.key_iv));
}
void RC4EAPOL::key_flag(small_uint<1> flag) {
header_.key_flag = flag;
}
void RC4EAPOL::key_index(small_uint<7> new_key_index) {
header_.key_index = new_key_index;
}
void RC4EAPOL::key_sign(const uint8_t* ptr) {
memcpy(header_.key_sign, ptr, sizeof(header_.key_sign));
}
void RC4EAPOL::key(const key_type& new_key) {
key_ = new_key;
}
uint32_t RC4EAPOL::header_size() const {
return static_cast<uint32_t>(sizeof(eapol_header) + sizeof(header_) + key_.size());
}
void RC4EAPOL::write_body(OutputMemoryStream& stream) {
if (key_.size()) {
header_.key_length = Endian::host_to_be(static_cast<uint16_t>(key_.size()));
}
stream.write(header_);
stream.write(key_.begin(), key_.end());
}
/* RSNEAPOL */
RSNEAPOL::RSNEAPOL()
: EAPOL(0x03, RSN) {
memset(&header_, 0, sizeof(header_));
}
RSNEAPOL::RSNEAPOL(const uint8_t* buffer, uint32_t total_sz)
: EAPOL(buffer, total_sz) {
InputMemoryStream stream(buffer, total_sz);
stream.skip(sizeof(eapol_header));
stream.read(header_);
if (stream.size() >= wpa_length()) {
stream.read(key_, wpa_length());
if (stream) {
inner_pdu(new RawPDU(stream.pointer(), stream.size()));
}
}
}
void RSNEAPOL::nonce(const uint8_t* ptr) {
memcpy(header_.nonce, ptr, nonce_size);
}
void RSNEAPOL::rsc(const uint8_t* ptr) {
memcpy(header_.rsc, ptr, rsc_size);
}
void RSNEAPOL::id(const uint8_t* ptr) {
memcpy(header_.id, ptr, id_size);
}
void RSNEAPOL::replay_counter(uint64_t new_replay_counter) {
header_.replay_counter = Endian::host_to_be(new_replay_counter);
}
void RSNEAPOL::mic(const uint8_t* ptr) {
memcpy(header_.mic, ptr, mic_size);
}
void RSNEAPOL::wpa_length(uint16_t length) {
header_.wpa_length = Endian::host_to_be(length);
}
void RSNEAPOL::key_iv(const uint8_t* ptr) {
memcpy(header_.key_iv, ptr, sizeof(header_.key_iv));
}
void RSNEAPOL::key_length(uint16_t length) {
header_.key_length = Endian::host_to_be(length);
}
void RSNEAPOL::key(const key_type& value) {
key_ = value;
}
void RSNEAPOL::key_mic(small_uint<1> flag) {
header_.key_mic = flag;
}
void RSNEAPOL::secure(small_uint<1> flag) {
header_.secure = flag;
}
void RSNEAPOL::error(small_uint<1> flag) {
header_.error = flag;
}
void RSNEAPOL::request(small_uint<1> flag) {
header_.request = flag;
}
void RSNEAPOL::encrypted(small_uint<1> flag) {
header_.encrypted = flag;
}
void RSNEAPOL::key_descriptor(small_uint<3> new_key_descriptor) {
header_.key_descriptor = new_key_descriptor;
}
void RSNEAPOL::key_t(small_uint<1> flag) {
header_.key_t = flag;
}
void RSNEAPOL::key_index(small_uint<2> value) {
header_.key_index = value;
}
void RSNEAPOL::install(small_uint<1> flag) {
header_.install = flag;
}
void RSNEAPOL::key_ack(small_uint<1> flag) {
header_.key_ack = flag;
}
uint32_t RSNEAPOL::header_size() const {
return static_cast<uint32_t>(sizeof(eapol_header) + sizeof(header_) + key_.size());
}
void RSNEAPOL::write_body(OutputMemoryStream& stream) {
if (key_.size()) {
if (!header_.key_t && header_.install) {
header_.key_length = Endian::host_to_be<uint16_t>(32);
wpa_length(static_cast<uint16_t>(key_.size()));
}
else if (key_.size()) {
wpa_length(static_cast<uint16_t>(key_.size()));
}
}
stream.write(header_);
stream.write(key_.begin(), key_.end());
}
} // Tins