@@ -150,7 +150,24 @@ tls_connection_create(struct in_addr caddr, u_short cport, struct in_addr saddr,
150150void
151151tls_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
211228int
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
272290int
273291tls_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
320339int
@@ -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
425447int
426448tls_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 ;
0 commit comments