This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
kssl_helpers.c
550 lines (472 loc) · 16.7 KB
/
kssl_helpers.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// kssl_helpers.c: protocol helper operations for keyless ssl
//
// Copyright (c) 2013 CloudFlare, Inc.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "kssl.h"
#include "kssl_helpers.h"
#if PLATFORM_WINDOWS
#include <ws2tcpip.h>
#else
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "kssl_log.h"
// Helper macros for stream processing. These macros ensure that the correct
// byte ordering is used.
// b is the buffer to read from/write to
// o is the offset in the buffer, incremented after the read/write
// v is the value to set
#define READ_BYTE(b, o) (b)[(o)]; (o)++;
#define READ_WORD(b, o) ntohs(*(WORD*)(&(b)[(o)])); (o) += sizeof(WORD);
#define READ_DWORD(b, o) ntohl(*(DWORD*)(&(b)[(o)])); (o) += sizeof(DWORD);
#define WRITE_BYTE(b, o, v) (b)[(o)] = (v); (o)++;
#define WRITE_WORD(b, o, v) *(WORD*)(&(b)[(o)]) = htons((v)); (o) += sizeof(WORD);
#define WRITE_DWORD(b, o, v) *(DWORD*)(&(b)[(o)]) = htonl((v)); (o) += sizeof(DWORD);
#define WRITE_BUFFER(b, o, v, l) memcpy(&(b)[(o)], (v), (l)); (o) += l;
#if PLATFORM_WINDOWS
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif
#define PRINT_IP InetNtop
#else
#define PRINT_IP inet_ntop
#endif
// parse_header: populates a kssl_header structure from a byte stream. Returns
// KSSL_ERROR_NONE if successful.
kssl_error_code parse_header(BYTE *bytes, // Stream of bytes
// containing a
// kssl_header
kssl_header *header) { // Returns the populated
// header (must be
// allocated by caller)
int offset = 0;
if (bytes == NULL || header == NULL) {
return KSSL_ERROR_INTERNAL;
}
header->version_maj = READ_BYTE(bytes, offset);
header->version_min = READ_BYTE(bytes, offset);
header->length = READ_WORD(bytes, offset);
header->id = READ_DWORD(bytes, offset);
return KSSL_ERROR_NONE;
}
// parse_item: Parse a kssl_item out of the body of a KSSL message
// NOTE: The payload for the item is not copied, a reference
// to the original stream is added to the kssl_item struct. The offset
// is updated if provided. Returns KSSL_ERROR_NONE if successful.
kssl_error_code parse_item(BYTE *bytes, // Byte stream to parse
// kssl_item from
int *offset, // (optional) if present
// specifies offset into bytes.
kssl_item *item) { // The kssl_item parsed (must be
// allocated by caller)
int local_offset = 0;
BYTE local_tag;
WORD local_len;
BYTE *local_data;
if (bytes == NULL || item == NULL) {
return KSSL_ERROR_INTERNAL;
}
if (offset != NULL) {
local_offset = *offset;
}
local_tag = READ_BYTE(bytes, local_offset);
local_len = READ_WORD(bytes, local_offset);
local_data = &bytes[local_offset];
local_offset += local_len;
item->tag = local_tag;
item->length = local_len;
item->data = local_data;
if (offset != NULL) {
*offset = local_offset;
}
return KSSL_ERROR_NONE;
}
// flatten_header: serialize a header into a pre-allocated byte array
// at a given offset. The offset is updated as bytes are written. If
// offset pointer is NULL this function starts at offset 0.
kssl_error_code flatten_header(kssl_header *header, // Pointer to kssl_header
// to serialize
BYTE *bytes, // Byte buffer to write
// into (must be allocated
// and have sufficient
// space for a
// kssl_header)
int *offset) { // (optional) offset into
// bytes to
int local_offset = 0;
// write to
if (bytes == NULL || header == NULL) {
return KSSL_ERROR_INTERNAL;
}
if (offset != NULL) {
local_offset = *offset;
}
WRITE_BYTE(bytes, local_offset, header->version_maj);
WRITE_BYTE(bytes, local_offset, header->version_min);
WRITE_WORD(bytes, local_offset, header->length);
WRITE_DWORD(bytes, local_offset, header->id);
if (offset != NULL) {
*offset = local_offset;
}
return KSSL_ERROR_NONE;
}
// flatten_item_byte: serialize a kssl_item with a given tag and one
// byte payload at an offset. The offset is updated as bytes are written.
// If offset pointer is NULL this function starts at offset 0. Returns
// KSSL_ERROR_NONE if successful.
kssl_error_code flatten_item_byte(BYTE tag, // The kssl_item's tag (see
// kssl.h)
BYTE payload , // A single byte for the
// payload
BYTE *bytes, // Buffer into which
// kssl_item is written (must
// be pre-allocated and have
// room)
int *offset) { // (optional) offset into
// bytes to start writing at
int local_offset = 0;
if (bytes == NULL) {
return KSSL_ERROR_INTERNAL;
}
if (offset != NULL) {
local_offset = *offset;
}
WRITE_BYTE(bytes, local_offset, tag);
WRITE_WORD(bytes, local_offset, 1);
WRITE_BYTE(bytes, local_offset, payload);
if (offset != NULL) {
*offset = local_offset;
}
return KSSL_ERROR_NONE;
}
// flatten_item: Serialize a single kssl_item. The offset is updated
// as bytes are written. If offset pointer is NULL this function
// starts at offset 0. Returns KSSL_ERROR_NONE if successful.
kssl_error_code flatten_item(BYTE tag, // The kssl_item's tag (see
// kssl.h)
BYTE *payload, // Buffer containing the item's
// payload
WORD payload_len, // Length of data from payload
// to copy
BYTE *bytes, // Buffer into which item is
// serialized
int *offset) { // (optional) offset into bytes
// to write from
int local_offset = 0;
if (bytes == NULL) {
return KSSL_ERROR_INTERNAL;
}
if (offset != NULL) {
local_offset = *offset;
}
WRITE_BYTE(bytes, local_offset, tag);
WRITE_WORD(bytes, local_offset, payload_len);
if (payload_len > 0) {
WRITE_BUFFER(bytes, local_offset, payload, payload_len);
}
if (offset != NULL) {
*offset = local_offset;
}
return KSSL_ERROR_NONE;
}
// add_padding: adds padding bytes to a KSSL message. Assumes that the buffer
// being written to is calloced.
kssl_error_code add_padding(WORD size, // Length of padding
BYTE *bytes, // Buffer into which item is
// serialized
int *offset) { // (optional) offset into bytes
// to write from
int local_offset = 0;
if (bytes == NULL) {
return KSSL_ERROR_INTERNAL;
}
if (offset != NULL) {
local_offset = *offset;
}
// Add the padding. This gets added even is padding_size == 0
WRITE_BYTE(bytes, local_offset, KSSL_TAG_PADDING);
WRITE_WORD(bytes, local_offset, size);
if (offset != NULL) {
*offset = local_offset;
}
return KSSL_ERROR_NONE;
}
// flatten_operation: serialize a kssl_operation
kssl_error_code flatten_operation(kssl_header *header,
kssl_operation *operation,
BYTE **out_operation,
int *length) {
int local_req_len;
BYTE *local_req;
int offset = 0;
int padding_size = 0;
if (header == NULL ||
operation == NULL ||
out_operation == NULL ||
length == NULL) {
return KSSL_ERROR_INTERNAL;
}
// Allocate response (header + opcode + response)
local_req_len = KSSL_HEADER_SIZE;
if (operation->is_opcode_set) {
local_req_len += KSSL_OPCODE_ITEM_SIZE;
}
if (operation->is_payload_set) {
local_req_len += KSSL_ITEM_HEADER_SIZE + operation->payload_len;
}
if (operation->is_ski_set) {
local_req_len += KSSL_ITEM_HEADER_SIZE + KSSL_SKI_SIZE;
}
if (operation->is_digest_set) {
local_req_len += KSSL_ITEM_HEADER_SIZE + KSSL_DIGEST_SIZE;
}
if (operation->is_ip_set) {
local_req_len += KSSL_ITEM_HEADER_SIZE + operation->ip_len;
}
// The operation will always be padded to KSSL_PAD_TO +
// KSSL_ITEM_HEADER_SIZE bytes
if (local_req_len < KSSL_PAD_TO) {
padding_size = KSSL_PAD_TO - local_req_len;
}
local_req_len += KSSL_ITEM_HEADER_SIZE + padding_size;
// The memory is calloced here to ensure that it is all zero. This is
// important because the padding added below is done by just adding a
// KSSL_ITEM at the end of the message stating that it has N bytes of
// padding.
local_req = (BYTE *)calloc(local_req_len, 1);
if (local_req == NULL) {
return KSSL_ERROR_INTERNAL;
}
// Override header length
header->length = local_req_len - KSSL_HEADER_SIZE;
flatten_header(header, local_req, &offset);
if (operation->is_opcode_set) {
flatten_item_byte(KSSL_TAG_OPCODE, operation->opcode, local_req, &offset);
}
if (operation->is_payload_set) {
flatten_item(KSSL_TAG_PAYLOAD, operation->payload, operation->payload_len,
local_req, &offset);
}
if (operation->is_ski_set) {
flatten_item(KSSL_TAG_SKI, operation->ski, KSSL_SKI_SIZE,
local_req, &offset);
}
if (operation->is_digest_set) {
flatten_item(KSSL_TAG_DIGEST, operation->digest, KSSL_DIGEST_SIZE,
local_req, &offset);
}
if (operation->is_ip_set) {
flatten_item(KSSL_TAG_CLIENT_IP, operation->ip, operation->ip_len,
local_req, &offset);
}
add_padding(padding_size, local_req, &offset);
*out_operation = local_req;
*length = local_req_len;
return KSSL_ERROR_NONE;
}
// zero_operation: initialize a kssl_operation struct
void zero_operation(kssl_operation *operation) {
if (operation != NULL) {
operation->is_opcode_set = 0;
operation->opcode = 0;
operation->is_ski_set = 0;
operation->ski = NULL;
operation->is_digest_set = 0;
operation->digest = NULL;
operation->is_payload_set = 0;
operation->payload = NULL;
operation->payload_len = 0;
operation->is_ip_set = 0;
operation->ip = NULL;
operation->ip_len = 0;
}
}
// parse_message_payload: parse a message payload into a
// kssl_operation struct
kssl_error_code parse_message_payload(BYTE *payload, //
int len, //
kssl_operation *operation) { //
int offset = 0;
kssl_item temp_item;
if (payload == NULL || operation == NULL) {
return KSSL_ERROR_INTERNAL;
}
zero_operation(operation);
// Count number of items and validate structure
while (offset < len) {
if (len - offset < (int)(KSSL_ITEM_HEADER_SIZE)) {
return KSSL_ERROR_FORMAT;
}
if (parse_item(payload, &offset, &temp_item) != KSSL_ERROR_NONE ||
len < offset) {
return KSSL_ERROR_FORMAT;
}
// Iterate through known tags, populating necessary values
switch (temp_item.tag) {
case KSSL_TAG_OPCODE:
{
// Skip over malformed tags
if (temp_item.length != 1) {
continue;
}
operation->opcode = temp_item.data[0];
operation->is_opcode_set = 1;
break;
}
case KSSL_TAG_SKI:
{
// Skip over malformed tags
if (temp_item.length != KSSL_SKI_SIZE) continue;
operation->ski = temp_item.data;
operation->is_ski_set = 1;
break;
}
case KSSL_TAG_DIGEST:
{
// Skip over malformed tags
if (temp_item.length != KSSL_DIGEST_SIZE) continue;
operation->digest = temp_item.data;
operation->is_digest_set = 1;
break;
}
case KSSL_TAG_PAYLOAD:
{
operation->payload_len = temp_item.length;
operation->payload = temp_item.data;
operation->is_payload_set = 1;
break;
}
case KSSL_TAG_CLIENT_IP:
{
operation->ip_len = temp_item.length;
operation->ip = temp_item.data;
operation->is_ip_set = 1;
break;
}
case KSSL_TAG_PADDING:
{
break;
}
default:
break;
}
}
// check to see if opcode and payload are set
if (operation->is_opcode_set == 0 || operation->is_payload_set == 0) {
return KSSL_ERROR_FORMAT;
}
return KSSL_ERROR_NONE;
}
// opstring: convert a KSSL opcode byte to a string
const char *opstring(BYTE op) {
switch (op) {
case KSSL_OP_ERROR:
return "KSSL_OP_ERROR";
case KSSL_OP_PING:
return "KSSL_OP_PING";
case KSSL_OP_PONG:
return "KSSL_OP_PONG";
case KSSL_OP_RSA_DECRYPT:
return "KSSL_OP_RSA_DECRYPT";
case KSSL_OP_RSA_DECRYPT_RAW:
return "KSSL_OP_RSA_DECRYPT_RAW";
case KSSL_OP_RESPONSE:
return "KSSL_OP_RESPONSE";
case KSSL_OP_RSA_SIGN_MD5SHA1:
return "KSSL_OP_RSA_SIGN_MD5SHA1";
case KSSL_OP_RSA_SIGN_SHA1:
return "KSSL_OP_RSA_SIGN_SHA1";
case KSSL_OP_RSA_SIGN_SHA224:
return "KSSL_OP_RSA_SIGN_SHA224";
case KSSL_OP_RSA_SIGN_SHA256:
return "KSSL_OP_RSA_SIGN_SHA256";
case KSSL_OP_RSA_SIGN_SHA384:
return "KSSL_OP_RSA_SIGN_SHA384";
case KSSL_OP_RSA_SIGN_SHA512:
return "KSSL_OP_RSA_SIGN_SHA512";
case KSSL_OP_ECDSA_SIGN_MD5SHA1:
return "KSSL_OP_ECDSA_SIGN_MD5SHA1";
case KSSL_OP_ECDSA_SIGN_SHA1:
return "KSSL_OP_ECDSA_SIGN_SHA1";
case KSSL_OP_ECDSA_SIGN_SHA224:
return "KSSL_OP_ECDSA_SIGN_SHA224";
case KSSL_OP_ECDSA_SIGN_SHA256:
return "KSSL_OP_ECDSA_SIGN_SHA256";
case KSSL_OP_ECDSA_SIGN_SHA384:
return "KSSL_OP_ECDSA_SIGN_SHA384";
case KSSL_OP_ECDSA_SIGN_SHA512:
return "KSSL_OP_ECDSA_SIGN_SHA512";
}
return "UNKNOWN";
}
// errstring: convert a KSSL error to a string
const char *errstring(BYTE err) {
switch (err) {
case KSSL_ERROR_NONE:
return "KSSL_ERROR_NONE";
case KSSL_ERROR_CRYPTO_FAILED:
return "KSSL_ERROR_CRYPTO_FAILED";
case KSSL_ERROR_KEY_NOT_FOUND:
return "KSSL_ERROR_KEY_NOT_FOUND";
case KSSL_ERROR_READ:
return "KSSL_ERROR_READ";
case KSSL_ERROR_VERSION_MISMATCH:
return "KSSL_ERROR_VERSION_MISMATCH";
case KSSL_ERROR_BAD_OPCODE:
return "KSSL_ERROR_BAD_OPCODE";
case KSSL_ERROR_UNEXPECTED_OPCODE:
return "KSSL_ERROR_UNEXPECTED_OPCODE";
case KSSL_ERROR_FORMAT:
return "KSSL_ERROR_FORMAT";
case KSSL_ERROR_INTERNAL:
return "KSSL_ERROR_INTERNAL";
}
return "UNKNOWN";
}
static void print_ip(kssl_operation *op, char *ip_string) {
if (op == NULL) return;
if (op->is_ip_set) {
// IPv4 printing
if (op->ip_len == 4) {
struct in_addr ip;
memcpy((void *)&ip.s_addr, op->ip, 4);
PRINT_IP(AF_INET, &ip, ip_string, INET_ADDRSTRLEN);
}
if (op->ip_len == 16) {
struct in6_addr ip;
memcpy((void *)ip.s6_addr, op->ip, 16);
PRINT_IP(AF_INET6, &ip, ip_string, INET6_ADDRSTRLEN);
}
}
}
// log_operation: write out a KSSL operation to the log
void log_operation(kssl_header *header, kssl_operation *op) {
char ip_string[INET6_ADDRSTRLEN] = {0};
// The \n at the end of the ctime return is chopped off here.
time_t now = time(NULL);
char nowstring[32]; // ctime_r documentation says there must be
// room here for 26 bytes.
ctime_r(&now, &nowstring[0]);
// Strip the trailing \n
nowstring[strlen(nowstring)-1] = '\0';
print_ip(op, ip_string);
write_log(0, "version:%d.%d, id:%d, op:%s, ip <%s>, time %s",
header->version_maj, header->version_min, header->id,
opstring(op->opcode), ip_string, nowstring);
}
// log_error: log an error of the operation
void log_error(DWORD id, BYTE code) {
time_t now = time(NULL);
char nowstring[32]; // ctime_r documentation says there must be
// room here for 26 bytes.
ctime_r(&now, &nowstring[0]);
write_log(1, "id:%d, error:%s, time:%s",
id, errstring(code), nowstring);
}