-
Notifications
You must be signed in to change notification settings - Fork 8
/
SRUP_Observation_Req.cpp
330 lines (268 loc) · 8.85 KB
/
SRUP_Observation_Req.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// Created by AJ Poulter on 25/05/2018.
//
#include "SRUP_Observation_Req.h"
SRUP_MSG_OBSERVE_REQ::SRUP_MSG_OBSERVE_REQ()
{
m_msgtype[0] = SRUP::SRUP_MESSAGE_TYPE_OBSERVE_REQ;
m_joining_device_id = nullptr;
}
SRUP_MSG_OBSERVE_REQ::~SRUP_MSG_OBSERVE_REQ()
{
if (m_joining_device_id!= nullptr)
delete (m_joining_device_id);
}
const uint64_t* SRUP_MSG_OBSERVE_REQ::joining_device_ID()
{
return m_joining_device_id;
}
bool SRUP_MSG_OBSERVE_REQ::DataCheck()
{
if ((m_crypto->crypt() != nullptr) && (m_token != nullptr) && (m_sequence_ID != nullptr) &&
(m_sender_ID != nullptr) && (m_joining_device_id != nullptr))
return true;
else
return false;
}
bool SRUP_MSG_OBSERVE_REQ::Serialize(bool preSign)
{
// As we're using dynamic memory - and only storing / sending the length of the data we have
// we need to know how long all of the fields are so that we can unmarshall the data at
// the other end...
const uint16_t header_size = 18; // Two-bytes for the main header - plus 8 for the sequence ID + 8 for the sender ID
const uint8_t field_length_size = 2;
// We need the number of variable length fields - including the m_sig_len...
// ... this can't be const as we need to change it depending on whether or not we're in pre-sign.
// (If we are we need to reduce it by one for the unused m_sig_len)
// Here it will be 3 - signature, token, and encrypted_data...
uint8_t var_length_field_count = 3;
// And we need one more – for the joining device ID = 8 bytes
const uint8_t dev_id_length = 8;
uint32_t serial_len;
uint32_t p=0;
uint8_t * msb;
uint8_t * lsb;
if (m_token == nullptr)
return false;
// Now check that we have a sequence ID...
if (m_sequence_ID == nullptr)
return false;
// ...and check that we have a sender ID ...
if (m_sender_ID == nullptr)
return false;
msb=new uint8_t[1];
lsb=new uint8_t[1];
// If we're calling this as a prelude to signing / verifying then we need to exclude the signature data from the
// serial data we generate...
if (preSign)
{
serial_len = m_token_len + m_crypto->cryptLen();
var_length_field_count--;
}
else
serial_len = m_sig_len + m_token_len + m_crypto->cryptLen();
m_serial_length = serial_len + header_size + (field_length_size * var_length_field_count) + dev_id_length;
delete[] m_serialized;
m_serialized = new uint8_t[m_serial_length];
std::memset(m_serialized, 0, m_serial_length);
// The first two fields are fixed length (1 byte each).
std::memcpy(m_serialized, m_version, 1);
p+=1;
std::memcpy(m_serialized + p, m_msgtype, 1);
p+=1;
// Now we need to add the Sequence ID (uint64_t)
// See SRUP_Init.cpp for details...
for (int x=0;x<8;x++)
{
uint8_t byte;
byte = getByteVal(*m_sequence_ID, x);
std::memcpy(m_serialized + p, &byte, 1);
p+=1;
}
// And we need to do the same thing for the Sender ID (uint64_t)
for (int x=0;x<8;x++)
{
uint8_t byte;
byte = getByteVal(*m_sender_ID, x);
std::memcpy(m_serialized + p, &byte, 1);
p+=1;
}
// For the other fields need their length to be specified...
// The token...
encodeLength(lsb, msb, m_token_len);
std::memcpy(m_serialized + p, msb, 1);
p+=1;
std::memcpy(m_serialized + p, lsb, 1);
p+=1;
std::memcpy(m_serialized + p, m_token, m_token_len);
p+=m_token_len;
// If we're executing Serialize as a part of generating the signature - we can't marshall the signature
// as we haven't calculated it yet. So only do the signature if we're not in preSign
if (!preSign)
{
if (m_signature == nullptr)
{
delete[] msb;
delete[] lsb;
return false;
}
else
{
if (m_sig_len == 0)
{
delete[] msb;
delete[] lsb;
return false;
}
else
{
encodeLength(lsb, msb, m_sig_len);
std::memcpy(m_serialized + p, msb, 1);
p += 1;
std::memcpy(m_serialized + p, lsb, 1);
p += 1;
std::memcpy(m_serialized + p, m_signature, m_sig_len);
p += m_sig_len;
}
}
}
// Now we do the joining device ID (fixed size)
for (int x=0;x<8;x++)
{
uint8_t byte;
byte = getByteVal(*m_joining_device_id, x);
std::memcpy(m_serialized + p, &byte, 1);
p+=1;
}
// Lastly we need to add the encrypted data...
int crypt_len = m_crypto->cryptLen();
encodeLength(lsb, msb, crypt_len);
std::memcpy(m_serialized + p, msb, 1);
p+=1;
std::memcpy(m_serialized + p, lsb, 1);
p+=1;
uint8_t* encrypted_data=m_crypto->crypt();
std::memcpy(m_serialized + p, encrypted_data, crypt_len);
// p+=crypt_len; - Not used as this is the last operation.
delete[] msb;
delete[] lsb;
// If we're in preSign we don't have a real value for m_serialized - so copy the data to m_unsigned_message
// and discard (and reset) m_serialized & m_serial_length...
if (preSign)
{
delete[] m_unsigned_message;
m_unsigned_message = new unsigned char[m_serial_length];
std::memcpy(m_unsigned_message, m_serialized, m_serial_length);
m_unsigned_length = m_serial_length;
m_serial_length = 0;
delete[] m_serialized;
m_serialized= nullptr;
}
m_is_serialized = true;
return true;
}
bool SRUP_MSG_OBSERVE_REQ::DeSerialize(const unsigned char* serial_data)
{
uint16_t x;
uint32_t p=0;
uint8_t bytes[2];
// We need to unmarshall the data to reconstruct the object...
// We can start with the two bytes for the header.
// One for the version - and one for the message type.
std::memcpy(m_version, (uint8_t*) serial_data, 1);
p+=1;
std::memcpy(m_msgtype, (uint8_t*) serial_data + p, 1);
p+=1;
// Now we have to unmarshall the sequence ID...
uint8_t sid_bytes[8];
for (unsigned char & sid_byte : sid_bytes)
{
std::memcpy(&sid_byte, (uint8_t*) serial_data + p, 1);
++p;
}
// ... then we copy them into m_sequence_ID
if (m_sequence_ID != nullptr)
delete(m_sequence_ID);
m_sequence_ID = new uint64_t;
std::memcpy(m_sequence_ID, sid_bytes, 8);
// Next we have to unmarshall the sender ID...
uint8_t snd_bytes[8];
for (unsigned char & snd_byte : snd_bytes)
{
std::memcpy(&snd_byte, (uint8_t*) serial_data + p, 1);
++p;
}
// ... then we copy them into the sender ID
if (m_sender_ID != nullptr)
delete(m_sender_ID);
m_sender_ID = new uint64_t;
std::memcpy(m_sender_ID, snd_bytes, 8);
// Now we have two-bytes for the size of the token ... and x bytes for the token
std::memcpy(bytes, serial_data + p, 2);
x = decodeLength(bytes);
p+=2;
delete[] m_token;
m_token = new uint8_t[x+1];
std::memcpy(m_token, (uint8_t *) serial_data + p, x);
m_token_len = x;
p+=x;
// The next two bytes are the size of the signature...
std::memcpy(bytes, serial_data + p, 2);
x = decodeLength(bytes);
p+=2;
m_sig_len = x;
// The next x bytes are the value of the signature.
delete[] m_signature;
m_signature = new uint8_t[x];
std::memcpy(m_signature, serial_data + p, x);
p+=x;
uint8_t dev_bytes[8];
for (unsigned char & dev_byte : dev_bytes)
{
std::memcpy(&dev_byte, (uint8_t*) serial_data + p, 1);
++p;
}
// ... then we copy them into the joining device ID
if (m_joining_device_id != nullptr)
delete(m_joining_device_id);
m_joining_device_id = new uint64_t;
std::memcpy(m_joining_device_id, dev_bytes, 8);
// Lastly we have the encrypted data.
std::memcpy(bytes, serial_data + p, 2);
x = decodeLength(bytes);
p+=2;
auto* encrypted_data = new uint8_t[x];
std::memcpy(encrypted_data, (uint8_t *) serial_data + p, x);
m_crypto->crypt(encrypted_data, x);
delete[] encrypted_data;
return true;
}
bool SRUP_MSG_OBSERVE_REQ::joining_device_ID(const uint64_t* device_id)
{
try
{
if (m_joining_device_id != nullptr)
delete(m_joining_device_id);
m_joining_device_id = new uint64_t;
std::memcpy(m_joining_device_id, device_id, sizeof(uint64_t));
}
catch (...)
{
m_joining_device_id = nullptr;
return false;
}
return true;
}
uint32_t SRUP_MSG_OBSERVE_REQ::SerializedLength()
{
if (!m_is_serialized)
Serialize(false);
return m_serial_length;
}
unsigned char *SRUP_MSG_OBSERVE_REQ::Serialized()
{
if (Serialize(false))
return m_serialized;
else
return nullptr;
}