-
Notifications
You must be signed in to change notification settings - Fork 16
/
transport.c
executable file
·629 lines (556 loc) · 17.7 KB
/
transport.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
* rfc4253 - SSH Transport Layer Protocol implementation
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include "ssh.h"
#include "log.h"
#include "crypto.h"
#include "win.h"
#define SOCKET unsigned
#define OK 1
#define FAIL 2
#define STRICT 1
typedef struct {
int pos;
int plain_packet_len;
int encrypted_packet_len;
byte *data;
} plain_packet; /* ok, starts life as a plain packet but gets encrypted in time */
typedef struct {
void *client;
iofunc read;
iofunc write;
clfunc close;
char rbuff[MAX_SSH_PACKET_SIZE];
unsigned int _pos;
unsigned int _cur;
unsigned long bytes_read;
unsigned long bytes_write;
uint32 ctr;
} tr_client;
int __fill_buffer(tr_client *t, int bytes, int fl, char **emsg) {
int ecode;
if (t->_pos < MAX_SSH_PACKET_SIZE)
bytes = MAX_SSH_PACKET_SIZE - t->_pos;
else
t->_pos = t->_cur = 0;
ecode = t->read(t->client, t->rbuff + t->_pos, &bytes, fl, emsg);
if (ecode != NETWORK_OK)
return ecode;
t->_pos += bytes;
return NETWORK_OK;
}
void reset_buffer(tr_client *t) {
t->_pos = t->_cur = 0;
}
int read_data2(tr_client *t, char *buff, int *size, unsigned int want_all, char **emsg) {
int status = 0, tbytes = 0, rbytes = *size;
do {
status = t->read(t->client, buff + tbytes, &rbytes, 0, emsg);
if (status != NETWORK_OK)
return status;
tbytes += rbytes;
if (!want_all)
break;
if (tbytes == *size)
break;
else
rbytes = *size - tbytes;
} while (1);
*size = tbytes;
return NETWORK_OK;
}
int read_data(tr_client *t, char *buff, unsigned int *size, unsigned int want_all, char **emsg) {
int ecode;
/* if there is nothing available to read in our local buffers, fill them from the client */
if (t->_cur == t->_pos) {
ecode = __fill_buffer(t, MAX_SSH_PACKET_SIZE, 0, emsg);
if (ecode != NETWORK_OK)
return ecode;
}
/* we have data in our local buffers */
if (*size <= t->_pos - t->_cur) {
/* we can satisfy the full requirement from our local buffer */
memcpy(buff, t->rbuff + t->_cur, *size);
t->_cur += *size;
return NETWORK_OK;
} else {
/* buffer doesn't have *size bytes of data. If the caller doesn't strictly want *size bytes,
we will just send him what we have */
if (!want_all) {
*size = t->_pos - t->_cur;
memcpy(buff, t->rbuff + t->_cur, t->_pos - t->_cur);
t->_cur = t->_pos;
return NETWORK_OK;
} else {
/* caller strictly wants *size bytes, we will have to read data from the client
to get enough bytes */
int old_pos = t->_pos - t->_cur, remaining;
memcpy(buff, t->rbuff + t->_cur, t->_pos - t->_cur);
remaining = *size - (t->_pos - t->_cur);
t->_cur = t->_pos;
ecode = __fill_buffer(t, remaining, READ_ALL, emsg);
if (ecode != NETWORK_OK)
return ecode;
memcpy(buff + old_pos, t->rbuff + t->_cur, remaining);
t->_cur += remaining;
return NETWORK_OK;
}
}
}
int push_back(tr_client *t, unsigned int howmuch, int fl) {
if (howmuch > (t->_cur + 1)) {
if (fl & STRICT)
return -1;
else {
int old = t->_cur;
t->_cur = 0;
return old+1;
}
}
t->_cur -= howmuch;
return howmuch;
}
int seek(tr_client *t, int newpos) {
if (newpos > -1 && newpos < MAX_SSH_PACKET_SIZE) {
t->_cur = newpos;
return newpos;
}
return -1;
}
int write_data(tr_client *t, char *buff, int *size, char **emsg) {
/* write_data() is always write_all */
int ecode, total_written = 0, bytes = *size;
do {
ecode = t->write(t->client, buff + total_written, &bytes, 0, emsg);
if (ecode != NETWORK_OK)
return ecode;
total_written += bytes;
bytes = *size - total_written;
} while (bytes > 0);
*size = total_written;
return NETWORK_OK;
}
void cleanup_transport(session *s) {
tr_client *t = s->transport;
t->close(t->client);
FREE(t);
}
void destroy_ssh_packet(ssh_packet *p) {
FREE(p);
}
void plain_ssh_packet_begin(plain_packet **p, byte type) {
plain_packet *packet;
packet = MALLOC(sizeof(*packet));
packet->data = MALLOC(MAX_SSH_PAYLOAD_SIZE);
packet->pos = 0;
packet->data[packet->pos] = type;
packet->pos++;
*p = packet;
}
void plain_ssh_packet_add_randomness(plain_packet *packet, int bytes) {
char *garbage = crypto_rand(bytes * 8);
memcpy(packet->data + packet->pos, garbage, bytes);
packet->pos += bytes;
FREE(garbage);
}
void plain_ssh_packet_add_uint32(plain_packet *packet, uint32 i) {
uint32 l = long_swap(i);
memcpy(packet->data + packet->pos, (void *)&l, sizeof(l));
packet->pos += sizeof(l);
}
void plain_ssh_packet_add_name_list(plain_packet *packet, char *names) {
uint32 len = (uint32)strlen(names);
plain_ssh_packet_add_uint32(packet, len);
memcpy(packet->data + packet->pos, names, len);
packet->pos += len;
}
void plain_ssh_packet_add_byte(plain_packet *packet, byte b) {
packet->data[packet->pos] = b;
packet->pos++;
}
void plain_ssh_packet_peek(plain_packet *packet, char **data, int *len) {
*data = packet->data;
*len = packet->pos;
}
void plain_ssh_packet_add_string(plain_packet *packet, string *s) {
uint32 bytes = long_swap(*(uint32 *)s) + sizeof(uint32);
memcpy(packet->data + packet->pos, s, bytes);
packet->pos += bytes;
}
void plain_ssh_packet_add_mpint(plain_packet *packet, mpint *s) {
plain_ssh_packet_add_string(packet, s);
}
void plain_ssh_packet_finalize(session *s, plain_packet *p) {
int total, pk_length, upk_length;
byte padding_len, *outbuff, *g;
total = p->pos + sizeof(uint32) + sizeof(byte);
if (!s->enc) {
padding_len = 8 - total % 8;
if (padding_len < 4)
padding_len += 8;
} else {
int block_size = crypto_cipher_block_size(s->e_c2s) / 8;
padding_len = block_size - total % block_size;
if (padding_len < 4)
padding_len += block_size;
}
pk_length = p->pos + sizeof(padding_len) + padding_len;
upk_length = long_swap(pk_length);
outbuff = MALLOC(MAX_SSH_PACKET_SIZE);
memcpy(outbuff, (void *)&upk_length, sizeof(upk_length));
memcpy(outbuff + sizeof(upk_length), (void *)&padding_len, sizeof(padding_len));
memcpy(outbuff + sizeof(upk_length) + sizeof(padding_len), p->data, p->pos);
g = crypto_rand(padding_len * 8);
memcpy(outbuff + sizeof(upk_length) + sizeof(padding_len) + p->pos, g, padding_len);
FREE(g);
FREE(p->data);
p->data = outbuff;
p->plain_packet_len = pk_length + sizeof(pk_length);
}
int plain_ssh_packet_write(session *s, plain_packet *p) {
tr_client *t;
int total;
char *error;
t = s->transport;
total = p->plain_packet_len;
if (write_data(t, p->data, &total, &error) != NETWORK_OK) {
ERR(NOERR, "Failed to write plain ssh packet to client. %s", error);
FREE(p->data);
FREE(p);
return 0;
}
FREE(p->data);
FREE(p);
s->out_sequence++;
return 1;
}
/*
* Section 4.2 - Protocol version exchange
*/
int protocol_version_exchange(session *s) {
char buffer[255];
int total_size, size, ecode = FAIL;
char *error;
tr_client *t = s->transport;
/* hello world */
DEBUG("Protocol Version Exchange started with our version=%s", SSH_IDENT);
sprintf(buffer, "%s\r\n",SSH_IDENT);
size = (int)strlen(buffer);
ecode = write_data(t, buffer, &size, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not write to client. %s", error);
return 1;
}
/* parse the client version */
memset(buffer, 0, 255);
size = 255;
total_size = 0;
do {
int i, parsed = 0;
ecode = read_data(t, buffer + total_size, &size, 0, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not read client's protocol version. %s", error);
return 0;
}
for (i = total_size+1; i < total_size + size; i++) {
if (buffer[i] == '\n' ) {
if (buffer[i-1] == '\r') {
buffer[i-1] = '\0';
parsed = i;
break;
} else { /* some clients may only send a \n instead of \r\n */
buffer[i] = '\0';
parsed = i;
break;
}
}
}
total_size += size;
size = 255 - total_size;
if (parsed) {
seek(t, parsed+1);
ecode = OK;
break;
}
} while (total_size < 255);
if (ecode != OK) {
ERR(NOERR, "Could not parse client version. Maybe this is not a SSH2 client ?");
return 0;
}
if (!strstr(buffer, "SSH-2.0")) {
ERR(NOERR, "Sorry. Not a SSH 2.0 client(%s)", buffer);
return 0;
}
s->client_version = _strdup(buffer);
s->in_sequence = s->out_sequence = -1;
INFO(NOERR, "Client \"%s\" connected", s->client_version);
return 1;
}
int verify_mac(session *s, ssh_packet *p) {
byte temp[MAX_SSH_PACKET_SIZE + sizeof(uint32)];
uint32 seq = long_swap(s->in_sequence);
byte *hmac;
int i;
memcpy(temp, &seq, sizeof(uint32));
memcpy(temp + sizeof(uint32), p->blob, p->packet_length + sizeof(p->packet_length));
hmac = crypto_mac(s->c2s_mac_ctx, s->mac_c2s, temp, p->packet_length + 2*sizeof(uint32));
for (i = 0; i < 20; i++) {
if (hmac[i] != ((byte *)p->mac)[i]) {
DEBUG("MAC mismatch");
FREE(hmac);
return 0;
}
}
FREE(hmac);
return 1;
}
ssh_packet *read_encrypted_ssh_packet(session *s) {
int i;
char *error;
tr_client *t = s->transport;
int ecode, bytes, csize, total_bytes = 0;
ssh_packet *p = MALLOC(sizeof(*p));
byte temp[MAX_SSH_PACKET_SIZE];
p->pos = 0;
/* Read upto the block size for the cipher in use */
csize = bytes = crypto_cipher_block_size(s->e_c2s) / 8;
ecode = read_data(t, temp, &bytes, 1, &error);
if (ecode != NETWORK_OK) {
//ERR(NOERR, "Could not read cipher block size of data. %s", error);
FREE(p);
return NULL;
}
total_bytes += bytes;
/* decrypt the packet length to figure out how much data remains to be read */
crypto_cbc_decrypt(s->dec_ctx, s->e_c2s, temp, p->blob);
p->packet_length = long_swap(*(unsigned long *)p->blob);
bytes = p->packet_length + 4 /* size of packet_length uint32 */ - csize /* cipher size we already read */;
if (bytes != 0) {
ecode = read_data(t, temp + total_bytes, &bytes, 1, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not read %d bytes of encrypted packet. %s", bytes, error);
FREE(p);
return NULL;
}
}
total_bytes += bytes;
/* the full packet is in 'temp', the first cipher block size is decrypted in 'block'. We will decrypt the rest now */
for (i = csize; i < total_bytes; i += csize)
crypto_cbc_decrypt(s->dec_ctx, s->e_c2s, temp + i, p->blob + i);
p->padding_length = *(byte *)(p->blob + sizeof(p->packet_length));
p->payload = p->blob + sizeof(p->padding_length) + sizeof(p->packet_length);
p->data = (byte *)p->payload + sizeof(p->type);
p->padding = (char *)p->payload + p->packet_length - p->padding_length - sizeof(p->packet_length);
p->type = ((byte *)(p->payload))[0];
/* read & verify the 20 byte MAC */
bytes = 20;
ecode = read_data(t, p->blob + total_bytes, &bytes, 1, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not read %d bytes of encrypted packet", bytes);
FREE(p);
return NULL;
}
p->mac = p->blob + total_bytes;
s->in_sequence++;
if (!verify_mac(s,p))
return NULL;
return p;
}
ssh_packet *read_plain_ssh_packet(session *s) {
/* this should only get called during the initial KEX */
char *error;
tr_client *t = s->transport;
int ecode, bytes = MAX_SSH_PACKET_SIZE, total_bytes = 0;
ssh_packet *p = MALLOC(sizeof(*p));
p->pos = 0;
/*
do {
ecode = read_data(t, p->blob + total_bytes, &bytes, 0, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not read ssh packet size. %s", error);
FREE(p);
return NULL;
}
total_bytes += bytes;
bytes = MAX_SSH_PACKET_SIZE - total_bytes;
} while (total_bytes< sizeof(p->padding_length) + sizeof(p->packet_length));
*/
total_bytes = bytes = sizeof(p->padding_length) + sizeof(p->packet_length);
ecode = read_data(t, p->blob, &bytes, READ_ALL, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not read ssh packet size. %s", error);
FREE(p);
return NULL;
}
p->packet_length = make_uint32(p->blob);
p->padding_length = *(unsigned char *)(p->blob + sizeof(p->packet_length));
bytes = p->packet_length + sizeof(p->packet_length) - bytes;
ecode = read_data(t, p->blob + total_bytes, &bytes, READ_ALL, &error);
if (ecode != NETWORK_OK) {
ERR(NOERR, "Could not read ssh packet. %s", error);
FREE(p);
return NULL;
}
p->payload = p->blob + sizeof(p->padding_length) + sizeof(p->packet_length);
p->payload_length = p->packet_length - p->padding_length - 1;
p->data = (char *)p->payload + sizeof(p->type);
p->padding = (char *)p->payload + p->packet_length - p->padding_length - sizeof(p->packet_length);
/* there is no mac negotiated yet */
p->mac = NULL;
p->type = ((char *)p->payload)[0];
s->in_sequence++;
return p;
}
byte *build_mac(session *s, plain_packet *p) {
byte temp[MAX_SSH_PACKET_SIZE + sizeof(uint32)], *hmac;
uint32 seq = long_swap(s->out_sequence);
memcpy(temp, &seq, sizeof(uint32));
memcpy(temp + sizeof(uint32), p->data, p->plain_packet_len);
hmac = crypto_mac(s->s2c_mac_ctx, s->mac_s2c, temp, p->plain_packet_len + sizeof(uint32));
return hmac;
}
int write_encrypted_ssh_packet(session *s, plain_packet *p) {
tr_client *t = s->transport;
byte outbuff[MAX_SSH_PACKET_SIZE], *mac;
int i, total_bytes = 0, csize = crypto_cipher_block_size(s->e_s2c) / 8;
char *error;
for (i = 0; i < p->plain_packet_len; i += csize)
crypto_cbc_encrypt(s->enc_ctx, s->e_s2c, p->data + i, outbuff + i);
s->out_sequence++;;
mac = build_mac(s, p);
memcpy(outbuff + p->plain_packet_len, mac, 20);
FREE(mac);
total_bytes = p->plain_packet_len + 20;
if (write_data(t, outbuff, &total_bytes, &error) != NETWORK_OK) {
ERR(NOERR, "Failed to write encrypted packet to client. %s", error);
FREE(p->data);
FREE(p);
s->out_sequence--;
return 0;
}
FREE(p->data);
FREE(p);
return 1;
}
ssh_packet *read_ssh_packet(session *s) {
if (s->enc)
return read_encrypted_ssh_packet(s);
else
return read_plain_ssh_packet(s);
}
session *make_session() {
session *s = MALLOC(sizeof(*s));
memset(s, 0x0, sizeof(*s));
s->cleanup_transport = cleanup_transport;
s->server_ctx = sshd_server_ctx();
return s;
}
void cleanup_session(session *s) {
if (s->cleanup_channel)
s->cleanup_channel(s);
if (s->cleanup_auth)
s->cleanup_auth(s);
if (s->cleanup_kex)
s->cleanup_kex(s);
if (s->cleanup_transport)
s->cleanup_transport(s);
if (s->client_version)
FREE(s->client_version);
if (s->dec_ctx)
crypto_free_cipher_context(s->dec_ctx, s->e_c2s);
if (s->enc_ctx)
crypto_free_cipher_context(s->enc_ctx, s->e_s2c);
if (s->c2s_mac_ctx)
crypto_free_mac_context(s->c2s_mac_ctx, s->mac_c2s);
if (s->s2c_mac_ctx)
crypto_free_mac_context(s->s2c_mac_ctx, s->mac_s2c);
if (s->session_id)
FREE(s->session_id);
if (s->secret_key) {
FREE(s->secret_key);
}
{
int i;
for (i = 0; i < 6; i++) {
if (s->keys[i])
FREE(s->keys[i]);
}
}
FREE(s);
}
int transport_ignore(session *s, ssh_packet *p) {
return 1;
}
int transport_teardown_session(session *s, ssh_packet *p) {
return -1;
}
int transport_service_request(session *s, ssh_packet *p) {
plain_packet *packet;
char *service = make_cstring(p->data);
DEBUG("Client requested service \"%s\"", service);
if (strcmp(AUTH_SERVICE, service)) {
FREE(service);
return 0;
}
FREE(service);
plain_ssh_packet_begin(&packet, SSH_MSG_SERVICE_ACCEPT);
plain_ssh_packet_add_string(packet, p->data);
plain_ssh_packet_finalize(s, packet);
return write_encrypted_ssh_packet(s, packet);
}
void loop(session *s) {
while (1) {
/*
read packets in a loop and dispatch to the registered handler for
that packet type
*/
int err;
ssh_packet *p = read_ssh_packet(s);
if (!p){
break;
}
DEBUG("incoming packet type=%d", p->type);
if (!handler[p->type]) {
ERR(NOERR, "Unknown packet - no handler registered");
destroy_ssh_packet(p);
break;
}
err = (*handler[p->type])(s, p);
if (!err) {
ERR(NOERR, "Handler raised error - terminating connection");
destroy_ssh_packet(p);
break;
}
destroy_ssh_packet(p);
if (err == -1)
break;
}
cleanup_session(s);
}
void process_client(void *client, void **fns) {
session *s = make_session();
tr_client *t = MALLOC(sizeof(*t));
s->transport = t;
t->client = client;
t->read = (iofunc)fns[0];
t->write = (iofunc)fns[1];
t->close = (clfunc)fns[2];
t->_pos = 0;
t->_cur = 0;
if (!protocol_version_exchange(s)) {
cleanup_session(s);
return;
}
loop(s);
return;
}
void transport_init() {
handler[SSH_MSG_DISCONNECT] = transport_teardown_session;
handler[SSH_MSG_IGNORE] = transport_ignore;
handler[SSH_MSG_UNIMPLEMENTED] = transport_teardown_session;
handler[SSH_MSG_DEBUG] = transport_ignore;
handler[SSH_MSG_SERVICE_REQUEST] = transport_service_request;
}