-
Notifications
You must be signed in to change notification settings - Fork 8
/
SRUP_Simple.cpp
236 lines (193 loc) · 6.27 KB
/
SRUP_Simple.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
//
// Created by AJ Poulter on 11/05/2018.
//
#include "SRUP_Simple.h"
bool SRUP_MSG_SIMPLE::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
uint8_t var_length_field_count = 2;
uint32_t serial_len;
uint32_t p = 0;
uint8_t *msb;
uint8_t *lsb;
msb = new uint8_t[1];
lsb = new uint8_t[1];
// Now check that we have a sequence ID...
if (m_sequence_ID == nullptr)
{
delete[] msb;
delete[] lsb;
return false;
}
// ...and a sender ID
if (m_sender_ID == nullptr)
{
delete[] msb;
delete[] lsb;
return false;
}
// 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;
var_length_field_count--;
}
else
serial_len = m_sig_len + m_token_len;
m_serial_length = serial_len + header_size + (field_length_size * var_length_field_count);
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 the same for the sender ID
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;
}
// All of the other fields need their length to be specified...
// 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)
return false;
else
{
if (m_sig_len == 0)
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; - the value is not used since this is the final 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;
}
uint32_t SRUP_MSG_SIMPLE::SerializedLength()
{
if (!m_is_serialized)
Serialize(false);
return m_serial_length;
}
unsigned char *SRUP_MSG_SIMPLE::Serialized()
{
if (Serialize(false))
return m_serialized;
else
return nullptr;
}
bool SRUP_MSG_SIMPLE::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);
// ...and also for 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;
}
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, (char*) 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 unsigned char[x];
std::memcpy(m_signature, serial_data + p, x);
return true;
}
bool SRUP_MSG_SIMPLE::DataCheck()
{
if ((m_token == nullptr) || (m_sequence_ID == nullptr) || (m_sender_ID == nullptr))
return false;
else
return true;
}