-
Notifications
You must be signed in to change notification settings - Fork 48
/
packet.c
329 lines (279 loc) · 7.6 KB
/
packet.c
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
/* Copyright (c) 2014-2016, Robert Escriva
* 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.
* * Neither the name of this project nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* c */
#include <assert.h>
#include <stdint.h>
#include <string.h>
/* macaroons */
#include "constants.h"
#include "packet.h"
#include "port.h"
static unsigned char*
packet_memmove(unsigned char* ptr, const unsigned char* src, size_t sz)
{
memmove(ptr, src, sz);
return ptr + sz;
}
static unsigned char*
packet_header(size_t sz, unsigned char* ptr)
{
static const char hex[] = "0123456789abcdef";
assert(sz < 65536);
ptr[0] = hex[(sz >> 12) & 15];
ptr[1] = hex[(sz >> 8) & 15];
ptr[2] = hex[(sz >> 4) & 15];
ptr[3] = hex[(sz) & 15];
assert(PACKET_PREFIX == 4); /* modify above on failure */
return ptr + PACKET_PREFIX;
}
const unsigned char*
parse_packet(const unsigned char* ptr,
const unsigned char* const end,
struct packet* pkt)
{
static const char hex[] = "0123456789abcdef";
const char* tmp;
uint32_t sz = 0;
int i = 0;
if (end - ptr < PACKET_PREFIX)
{
return NULL;
}
for (i = 0; i < PACKET_PREFIX; ++i)
{
sz <<= 4;
tmp = strchr(hex, ptr[i]);
if (!tmp)
{
return NULL;
}
sz |= tmp - hex;
}
if (end - ptr < sz)
{
return NULL;
}
pkt->data = ptr;
pkt->size = sz;
return ptr + sz;
}
int
parse_kv_packet(const struct packet* pkt,
const unsigned char** key, size_t* key_sz,
const unsigned char** val, size_t* val_sz)
{
unsigned char* tmp = NULL;
unsigned char prefix[PACKET_PREFIX];
*key = NULL;
*key_sz = 0;
*val = NULL;
*val_sz = 0;
if (pkt->size > PACKET_MAX_SIZE)
{
return -1;
}
packet_header(pkt->size, prefix);
if (pkt->size < PACKET_PREFIX + 2 ||
memcmp(pkt->data, prefix, PACKET_PREFIX) != 0 ||
pkt->data[pkt->size - 1] != '\n')
{
return -1;
}
tmp = memchr(pkt->data + PACKET_PREFIX, ' ', pkt->size - PACKET_PREFIX);
if (!tmp)
{
return -1;
}
*key = pkt->data + PACKET_PREFIX;
*key_sz = tmp - *key;
*val = tmp + 1;
*val_sz = pkt->size - PACKET_PREFIX - 2 - *key_sz;
return 0;
}
unsigned char*
serialize_slice_as_packet(const char* _key, size_t key_sz,
const struct slice* from,
unsigned char* ptr)
{
const unsigned char* key = (const unsigned char*) _key;
size_t sz = PACKET_PREFIX + 2 + key_sz + from->size;
memset(ptr, 0, sz);
ptr = packet_header(sz, ptr);
ptr = packet_memmove(ptr, key, key_sz);
*ptr = ' ';
++ptr;
ptr = packet_memmove(ptr, from->data, from->size);
*ptr = '\n';
++ptr;
return ptr;
}
int
copy_if_parses(const unsigned char** rptr,
const unsigned char* const end,
int (*f)(const struct packet* pkt,
const unsigned char** s, size_t* s_sz),
struct slice* to,
unsigned char** wptr)
{
const unsigned char* tmp;
size_t tmp_sz;
struct packet pkt;
*rptr = parse_packet(*rptr, end, &pkt);
if (!*rptr || f(&pkt, &tmp, &tmp_sz) < 0)
{
return -1;
}
*wptr = copy_to_slice(tmp, tmp_sz, to, *wptr);
return 0;
}
int
parse_location_packet(const struct packet* packet,
const unsigned char** location, size_t* location_sz)
{
const unsigned char* key;
const unsigned char* val;
size_t key_sz;
size_t val_sz;
if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0)
{
return -1;
}
if (key_sz != LOCATION_SZ ||
memcmp(key, LOCATION, LOCATION_SZ) != 0)
{
return -1;
}
*location = val;
*location_sz = val_sz;
return 0;
}
int
parse_identifier_packet(const struct packet* packet,
const unsigned char** identifier, size_t* identifier_sz)
{
const unsigned char* key;
const unsigned char* val;
size_t key_sz;
size_t val_sz;
if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0)
{
return -1;
}
if (key_sz != IDENTIFIER_SZ ||
memcmp(key, IDENTIFIER, IDENTIFIER_SZ) != 0)
{
return -1;
}
*identifier = val;
*identifier_sz = val_sz;
return 0;
}
int
parse_signature_packet(const struct packet* packet,
const unsigned char** signature)
{
const unsigned char* key;
const unsigned char* val;
size_t key_sz;
size_t val_sz;
if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0)
{
return -1;
}
if (key_sz != SIGNATURE_SZ ||
val_sz != MACAROON_HASH_BYTES ||
memcmp(key, SIGNATURE, SIGNATURE_SZ) != 0)
{
return -1;
}
*signature = val;
return 0;
}
int
parse_cid_packet(const struct packet* packet,
const unsigned char** cid, size_t* cid_sz)
{
const unsigned char* key;
const unsigned char* val;
size_t key_sz;
size_t val_sz;
if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0)
{
return -1;
}
if (key_sz != CID_SZ || memcmp(key, CID, CID_SZ) != 0)
{
return -1;
}
*cid = val;
*cid_sz = val_sz;
return 0;
}
int
parse_vid_packet(const struct packet* packet,
const unsigned char** vid, size_t* vid_sz)
{
const unsigned char* key;
const unsigned char* val;
size_t key_sz;
size_t val_sz;
if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0)
{
return -1;
}
if (key_sz != VID_SZ || memcmp(key, VID, VID_SZ) != 0)
{
return -1;
}
*vid = val;
*vid_sz = val_sz;
return 0;
}
int
parse_cl_packet(const struct packet* packet,
const unsigned char** cl, size_t* cl_sz)
{
const unsigned char* key;
const unsigned char* val;
size_t key_sz;
size_t val_sz;
if (parse_kv_packet(packet, &key, &key_sz, &val, &val_sz) < 0)
{
return -1;
}
if (key_sz != CL_SZ || memcmp(key, CL, CL_SZ) != 0)
{
return -1;
}
*cl = val;
*cl_sz = val_sz;
return 0;
}