Skip to content

Commit 16c1964

Browse files
committed
Fix a crash with not handled SSL ciphers #48
Also remove compiler warnings in source code
1 parent 5644af9 commit 16c1964

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/capture_tls.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,24 @@ tls_connection_create(struct in_addr caddr, u_short cport, struct in_addr saddr,
150150
void
151151
tls_connection_destroy(struct SSLConnection *conn)
152152
{
153-
// TODO
153+
struct SSLConnection *c;
154+
155+
// Remove connection from connections list
156+
if (conn == connections) {
157+
connections = conn->next;
158+
} else {
159+
for (c = connections; c; c = c->next) {
160+
if (c->next == conn) {
161+
c->next = conn->next;
162+
break;
163+
}
164+
}
165+
}
166+
167+
// Deallocate connection memory
168+
SSL_CTX_free(conn->ssl_ctx);
169+
SSL_free(conn->ssl);
170+
free(conn);
154171
}
155172

156173
/**
@@ -209,7 +226,7 @@ tls_connection_find(struct in_addr addr, u_short port)
209226
}
210227

211228
int
212-
tls_process_segment(const struct ip *ip, uint8 **out, int *outl)
229+
tls_process_segment(const struct ip *ip, uint8 **out, uint32_t *outl)
213230
{
214231
struct SSLConnection *conn;
215232
struct tcphdr *tcp;
@@ -251,7 +268,8 @@ tls_process_segment(const struct ip *ip, uint8 **out, int *outl)
251268
// Process data segment!
252269
payload = (uint8 *) tcp + tcp_size;
253270
len = ntohs(ip->ip_len) - (ip->ip_hl * 4) - tcp_size;
254-
tls_process_record(conn, payload, len, out, outl);
271+
if (tls_process_record(conn, payload, len, out, outl) != 0)
272+
return 1;
255273
break;
256274
case TCP_STATE_FIN:
257275
case TCP_STATE_CLOSED:
@@ -271,7 +289,7 @@ tls_process_segment(const struct ip *ip, uint8 **out, int *outl)
271289

272290
int
273291
tls_process_record(struct SSLConnection *conn, const uint8 *payload, const int len, uint8 **out,
274-
int *outl)
292+
uint32_t *outl)
275293
{
276294
struct TLSPlaintext *record;
277295
int record_len;
@@ -293,7 +311,8 @@ tls_process_record(struct SSLConnection *conn, const uint8 *payload, const int l
293311
switch (record->type) {
294312
case handshake:
295313
// Hanshake Record, Try to get MasterSecret data
296-
tls_process_record_handshake(conn, fragment);
314+
if (tls_process_record_handshake(conn, fragment) != 0)
315+
return 1;
297316
break;
298317
case change_cipher_spec:
299318
// From now on, this connection will be encrypted using MasterSecret
@@ -314,7 +333,7 @@ tls_process_record(struct SSLConnection *conn, const uint8 *payload, const int l
314333
if (len > record_len)
315334
return tls_process_record(conn, payload + record_len, len - record_len, out, outl);
316335

317-
return *outl;
336+
return 0;
318337
}
319338

320339
int
@@ -345,6 +364,7 @@ tls_process_record_handshake(struct SSLConnection *conn, const opaque *fragment)
345364
if (!(clienthello->client_version.major == 0x03
346365
&& clienthello->client_version.minor == 0x01)) {
347366
tls_connection_destroy(conn);
367+
return 1;
348368
}
349369
break;
350370
case server_hello:
@@ -356,8 +376,10 @@ tls_process_record_handshake(struct SSLConnection *conn, const opaque *fragment)
356376
body + sizeof(struct ServerHello) + serverhello->session_id_length,
357377
sizeof(uint16));
358378
// Check if we have a handled cipher
359-
if (tls_connection_load_cipher(conn) != 0)
379+
if (tls_connection_load_cipher(conn) != 0) {
360380
tls_connection_destroy(conn);
381+
return 1;
382+
}
361383
break;
362384
case certificate:
363385
case certificate_request:
@@ -411,7 +433,7 @@ tls_process_record_handshake(struct SSLConnection *conn, const opaque *fragment)
411433
if (conn->encrypted) {
412434
// Encrypted Hanshake Message
413435
unsigned char *decoded = malloc(48);
414-
int decodedlen;
436+
uint32_t decodedlen;
415437
tls_process_record_data(conn, fragment, 48, &decoded, &decodedlen);
416438
free(decoded);
417439
}
@@ -424,7 +446,7 @@ tls_process_record_handshake(struct SSLConnection *conn, const opaque *fragment)
424446

425447
int
426448
tls_process_record_data(struct SSLConnection *conn, const opaque *fragment, const int len,
427-
uint8 **out, int *outl)
449+
uint8 **out, uint32_t *outl)
428450
{
429451
EVP_CIPHER_CTX *evp;
430452
unsigned char pad;

src/capture_tls.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ tls_connection_find(struct in_addr addr, u_short port);
352352
* @return 0 in all cases
353353
*/
354354
int
355-
tls_process_segment(const struct ip *ip, uint8 **out, int *outl);
355+
tls_process_segment(const struct ip *ip, uint8 **out, uint32_t *outl);
356356

357357
/**
358358
* @brief Process TLS record data
@@ -370,7 +370,7 @@ tls_process_segment(const struct ip *ip, uint8 **out, int *outl);
370370
*/
371371
int
372372
tls_process_record(struct SSLConnection *conn, const uint8 *payload, const int len, uint8 **out,
373-
int *outl);
373+
uint32_t *outl);
374374

375375
/**
376376
* @brief Process TLS Handshake record types
@@ -400,7 +400,7 @@ tls_process_record_handshake(struct SSLConnection *conn, const opaque *fragment)
400400
*/
401401
int
402402
tls_process_record_data(struct SSLConnection *conn, const opaque *fragment, const int len,
403-
uint8 **out, int *outl);
403+
uint8 **out, uint32_t *outl);
404404

405405

406406
/**

src/keybinding.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "config.h"
3131
#include <ctype.h>
3232
#include <string.h>
33+
#include <stdlib.h>
3334
#include "ui_manager.h"
3435
#include "option.h"
3536
#include "keybinding.h"

0 commit comments

Comments
 (0)