Removed redundant encryption and asserts

This commit is contained in:
mannol 2014-06-28 04:13:38 +02:00
parent f433065875
commit aeaf997ca5
5 changed files with 96 additions and 371 deletions

View File

@ -31,7 +31,6 @@
#include "event.h"
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
@ -85,8 +84,6 @@ GENERIC_HEADER ( CallType )
GENERIC_HEADER ( CallId )
GENERIC_HEADER ( Info )
GENERIC_HEADER ( Reason )
GENERIC_HEADER ( CryptoKey )
GENERIC_HEADER ( Nonce )
/**
@ -103,8 +100,6 @@ typedef struct _MSIMessage {
MSIHeaderInfo info;
MSIHeaderReason reason;
MSIHeaderCallId callid;
MSIHeaderCryptoKey cryptokey;
MSIHeaderNonce nonce;
struct _MSIMessage *next;
@ -138,8 +133,6 @@ inline__ void invoke_callback(int32_t call_index, MSICallbackID id)
#define REASON_FIELD "Reason"
#define CALLTYPE_FIELD "Call-type"
#define CALLID_FIELD "Call-id"
#define CRYPTOKEY_FIELD "Crypto-key"
#define NONCE_FIELD "Nonce"
/* protocol descriptors */
#define end_byte 0x0
@ -227,7 +220,8 @@ if ( *iterator != value_byte || size_con <= type_size_const) { return -1; } size
iterator ++; if(size_con <= 3) {return -1;} size_con -= 3; \
uint16_t _value_size; memcpy(&_value_size, iterator, sizeof(_value_size)); _value_size = ntohs(_value_size);\
if(size_con < _value_size) { return -1; } size_con -= _value_size; \
header.header_value = calloc(sizeof(uint8_t), _value_size); \
if ( !(header.header_value = calloc(sizeof(uint8_t), _value_size)) ); \
LOGGER_ERROR("Allocation failed! Program might misbehave!"); \
header.size = _value_size; \
memcpy(header.header_value, iterator + 2, _value_size);\
iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ }
@ -265,11 +259,6 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
}
break;
case 5: { /* NONCE header */
if ON_HEADER ( _it, size_max, msg->nonce, NONCE_FIELD, 5 )
}
break;
case 6: { /* Reason header */
if ON_HEADER ( _it, size_max, msg->reason, REASON_FIELD, 6 )
}
@ -292,11 +281,6 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
}
break;
case 10: { /* Crypto-key headers */
if ON_HEADER ( _it, size_max, msg->cryptokey, CRYPTOKEY_FIELD, 10 )
}
break;
default:
LOGGER_ERROR("Unkown field value");
return -1;
@ -315,8 +299,8 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
#define ALLOCATE_HEADER( var, mheader_value, t_size) \
var.header_value = calloc(sizeof *mheader_value, t_size); \
if (var.header_value == NULL) { LOGGER_WARNING("Header allocation failed!"); } \
if (!(var.header_value = calloc(sizeof *mheader_value, t_size))) \
{ LOGGER_WARNING("Header allocation failed! Program might misbehave!"); } \
else { memcpy(var.header_value, mheader_value, t_size); \
var.size = t_size; }
@ -339,8 +323,6 @@ void free_message ( MSIMessage *msg )
free ( msg->response.header_value );
free ( msg->version.header_value );
free ( msg->info.header_value );
free ( msg->cryptokey.header_value );
free ( msg->nonce.header_value );
free ( msg->reason.header_value );
free ( msg->callid.header_value );
@ -361,7 +343,7 @@ MSIMessage *msi_new_message ( uint8_t type, const uint8_t *type_id )
MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
if ( _retu == NULL ) {
LOGGER_WARNING("Allocation failed!");
LOGGER_WARNING("Allocation failed! Program might misbehave!");
return NULL;
}
@ -399,7 +381,7 @@ MSIMessage *parse_message ( const uint8_t *data, uint16_t length )
MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
if ( _retu == NULL ) {
LOGGER_WARNING("Allocation failed!");
LOGGER_WARNING("Allocation failed! Program might misbehave!");
return NULL;
}
@ -422,47 +404,6 @@ MSIMessage *parse_message ( const uint8_t *data, uint16_t length )
}
/**
* @brief Makes clear message presentation
*
* @param msg Message
* @param dest Dest string
* @return int
*/
int stringify_message(MSIMessage *msg, char *dest)
{
// THIS CODE HAS NO EFFECT, AND THE ARGUMENTS ARE NOT MODIFIED
#if 0
#define HDR_TO_STR(__dest, __hdr) if (__hdr.header_value) {\
char nltstr[MSI_MAXMSG_SIZE]; memset(nltstr+__hdr.size, '\0', MSI_MAXMSG_SIZE-__hdr.size); int i = 0; \
for ( ; i < __hdr.size; i ++) nltstr[i] = (char)__hdr.header_value[i]; \
}
if ( !msg || !dest )
return -1;
HDR_TO_STR(dest, msg->version);
HDR_TO_STR(dest, msg->request);
HDR_TO_STR(dest, msg->response);
HDR_TO_STR(dest, msg->reason);
HDR_TO_STR(dest, msg->callid);
HDR_TO_STR(dest, msg->calltype);
HDR_TO_STR(dest, msg->cryptokey);
HDR_TO_STR(dest, msg->nonce);
// if (msg->version.header_value) {
// U8_TO_NLTCHAR(msg->version.header_value, msg->version.size, nltstr, MSI_MAXMSG_SIZE);
// sprintf(dest, "Version: %s\n", nltstr);
// }
#endif
return 0;
}
/**
* @brief Speaks for it self.
*
@ -482,7 +423,7 @@ uint8_t *append_header_to_string (
{
if ( dest == NULL ) {
LOGGER_ERROR("No destination space!");
assert(dest);
return NULL;
}
if (header_value == NULL) {
@ -577,8 +518,6 @@ uint16_t message_to_send ( MSIMessage *msg, uint8_t *dest )
CLEAN_ASSIGN ( _size, _iterated, INFO_FIELD, msg->info );
CLEAN_ASSIGN ( _size, _iterated, CALLID_FIELD, msg->callid );
CLEAN_ASSIGN ( _size, _iterated, REASON_FIELD, msg->reason );
CLEAN_ASSIGN ( _size, _iterated, CRYPTOKEY_FIELD, msg->cryptokey );
CLEAN_ASSIGN ( _size, _iterated, NONCE_FIELD, msg->nonce );
*_iterated = end_byte;
_size ++;
@ -589,7 +528,7 @@ uint16_t message_to_send ( MSIMessage *msg, uint8_t *dest )
#define GENERIC_SETTER_DEFINITION(header) \
void msi_msg_set_##header ( MSIMessage* _msg, const uint8_t* header_value, uint16_t _size ) \
{ assert(_msg); assert(header_value); \
{ if ( !_msg || !header_value) { LOGGER_WARNING("No setter values!"); return; } \
free(_msg->header.header_value); \
ALLOCATE_HEADER( _msg->header, header_value, _size )}
@ -597,8 +536,6 @@ GENERIC_SETTER_DEFINITION ( calltype )
GENERIC_SETTER_DEFINITION ( reason )
GENERIC_SETTER_DEFINITION ( info )
GENERIC_SETTER_DEFINITION ( callid )
GENERIC_SETTER_DEFINITION ( cryptokey )
GENERIC_SETTER_DEFINITION ( nonce )
/**
@ -608,7 +545,7 @@ GENERIC_SETTER_DEFINITION ( nonce )
* @param size Size of string.
* @return void
*/
void t_randomstr ( uint8_t *str, size_t size )
void t_randomstr ( uint8_t *str, uint32_t size )
{
if (str == NULL) {
LOGGER_DEBUG("Empty destination!");
@ -620,7 +557,7 @@ void t_randomstr ( uint8_t *str, size_t size )
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
size_t _it = 0;
uint32_t _it = 0;
for ( ; _it < size; _it++ ) {
str[_it] = _bytes[ random_int() % 61 ];
@ -708,14 +645,6 @@ int send_message ( MSISession *session, MSICall *call, MSIMessage *msg, uint32_t
return -1;
}
/*
LOGGER_SCOPE(
char cast[MSI_MAXMSG_SIZE];
stringify_message(msg, cast);
LOGGER_DEBUG("[Call: %s] [to: %u] Sending message: len: %d\n%s", call->id, to, _length, cast);
);*/
if ( m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ) {
LOGGER_DEBUG("Sent message");
return 0;
@ -854,16 +783,18 @@ int send_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_
*/
void add_peer( MSICall *call, int peer_id )
{
if ( !call->peers ) {
call->peers = calloc(sizeof(uint32_t), 1);
call->peer_count = 1;
} else {
call->peer_count ++;
call->peers = realloc( call->peers, sizeof(uint32_t) * call->peer_count);
uint32_t* peers = !call->peers ? peers = calloc(sizeof(uint32_t), 1) :
realloc( call->peers, sizeof(uint32_t) * call->peer_count);
if (!peers) {
LOGGER_WARNING("Allocation failed! Program might misbehave!");
return;
}
call->peer_count ++;
call->peers = peers;
call->peers[call->peer_count - 1] = peer_id;
LOGGER_DEBUG("Added peer: %d", peer_id);
}
@ -888,7 +819,12 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
for (; _call_idx < session->max_calls; _call_idx ++) {
if ( !session->calls[_call_idx] ) {
session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 );
if (!(session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
LOGGER_WARNING("Allocation failed! Program might misbehave!");
return NULL;
}
break;
}
}
@ -901,16 +837,11 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
MSICall *_call = session->calls[_call_idx];
if ( _call == NULL ) {
LOGGER_WARNING("Allocation failed!");
return NULL;
}
_call->call_idx = _call_idx;
_call->type_peer = calloc ( sizeof ( MSICallType ), peers );
if ( _call->type_peer == NULL ) {
LOGGER_WARNING("Allocation failed!");
if ( !(_call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) {
LOGGER_WARNING("Allocation failed! Program might misbehave!");
free(_call);
return NULL;
}
@ -921,11 +852,6 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
_call->request_timer_id = 0;
_call->ringing_timer_id = 0;
_call->key_local = NULL;
_call->key_peer = NULL;
_call->nonce_local = NULL;
_call->nonce_peer = NULL;
_call->ringing_tout_ms = ringing_timeout;
pthread_mutex_init ( &_call->mutex, NULL );
@ -966,8 +892,6 @@ int terminate_call ( MSISession *session, MSICall *call )
session->calls[call->call_idx] = NULL;
free ( call->type_peer );
free ( call->key_local );
free ( call->key_peer );
free ( call->peers);
/* Release handle */
@ -1103,21 +1027,8 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
pthread_mutex_lock(&session->mutex);
if ( !msg->cryptokey.header_value ) {
int rc = send_error ( session, call, error_no_crypto_key, msg->friend_id );
terminate_call(session, call);
pthread_mutex_unlock(&session->mutex);
return rc;
}
call->state = call_active;
call->key_peer = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
memcpy ( call->key_peer, msg->cryptokey.header_value, crypto_box_KEYBYTES );
call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
memcpy ( call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
flush_peer_type ( call, msg, 0 );
pthread_mutex_unlock(&session->mutex);
@ -1230,33 +1141,9 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
if ( !msg->cryptokey.header_value ) {
int rc = send_error ( session, call, error_no_crypto_key, msg->friend_id );
terminate_call(session, call);
pthread_mutex_unlock(&session->mutex);
return rc;
}
/* Generate local key/nonce to send */
call->key_local = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
new_symmetric_key ( call->key_local );
call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
new_nonce ( call->nonce_local );
/* Save peer key/nonce */
call->key_peer = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
memcpy ( call->key_peer, msg->cryptokey.header_value, crypto_box_KEYBYTES );
call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
memcpy ( call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
call->state = call_active;
MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
msi_msg_set_cryptokey ( _msg_start, call->key_local, crypto_box_KEYBYTES );
msi_msg_set_nonce ( _msg_start, call->nonce_local, crypto_box_NONCEBYTES );
send_message ( session, call, _msg_start, msg->friend_id );
free_message ( _msg_start );
@ -1491,14 +1378,19 @@ MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
MSISession *_retu = calloc ( sizeof ( MSISession ), 1 );
if (_retu == NULL) {
LOGGER_ERROR("Allocation failed!");
LOGGER_ERROR("Allocation failed! Program might misbehave!");
return NULL;
}
_retu->messenger_handle = messenger;
_retu->agent_handler = NULL;
_retu->calls = calloc( sizeof (MSICall *), max_calls );
if (!(_retu->calls = calloc( sizeof (MSICall *), max_calls ))) {
LOGGER_ERROR("Allocation failed! Program might misbehave!");
free(_retu);
return NULL;
}
_retu->max_calls = max_calls;
_retu->frequ = 10000; /* default value? */
@ -1690,17 +1582,6 @@ int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type
( _msg_starting, ( const uint8_t *) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
}
/* Now set the local encryption key and pass it with STARTING message */
session->calls[call_index]->key_local = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
new_symmetric_key ( session->calls[call_index]->key_local );
session->calls[call_index]->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
new_nonce ( session->calls[call_index]->nonce_local );
msi_msg_set_cryptokey ( _msg_starting, session->calls[call_index]->key_local, crypto_box_KEYBYTES );
msi_msg_set_nonce ( _msg_starting, session->calls[call_index]->nonce_local, crypto_box_NONCEBYTES );
send_message ( session, session->calls[call_index], _msg_starting,
session->calls[call_index]->peers[session->calls[call_index]->peer_count - 1] );
free_message ( _msg_starting );

View File

@ -71,12 +71,6 @@ typedef struct _MSICall { /* Call info structure */
uint8_t id[CALL_ID_LEN]; /* Random value identifying the call */
uint8_t *key_local; /* The key for encryption */
uint8_t *key_peer; /* The key for decryption */
uint8_t *nonce_local; /* Local nonce */
uint8_t *nonce_peer; /* Peer nonce */
int ringing_tout_ms; /* Ringing timeout in ms */
int request_timer_id; /* Timer id for outgoing request/action */

View File

@ -27,7 +27,6 @@
#include "../toxcore/util.h"
#include "rtp.h"
#include <assert.h>
#include <stdlib.h>
@ -50,7 +49,6 @@
#define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7)
#define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f)
/**
* @brief Checks if message came in late.
*
@ -70,46 +68,6 @@ inline__ int check_late_message (RTPSession *session, RTPMessage *msg)
return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1;
}
/**
* @brief Increases nonce value by 'target'
*
* @param nonce The nonce
* @param target The target
* @return void
*/
inline__ void increase_nonce(uint8_t *nonce, uint16_t target)
{
uint16_t _nonce_counter;
uint8_t _reverse_bytes[2];
_reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1];
_reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2];
bytes_to_U16(&_nonce_counter, _reverse_bytes );
/* Check overflow */
if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */
uint8_t _it = 3;
while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ?
crypto_box_NONCEBYTES : 1;
_nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */
} else { /* Increase nonce */
_nonce_counter += target;
}
/* Assign the last bytes */
U16_to_bytes( _reverse_bytes, _nonce_counter);
nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0];
nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1];
}
/**
* @brief Speaks for it self.
*
@ -146,11 +104,17 @@ RTPHeader *extract_header ( const uint8_t *payload, int length )
return NULL;
}
const uint8_t *_it = payload;
RTPHeader *_retu = calloc(1, sizeof (RTPHeader));
assert(_retu);
if ( !_retu ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
return NULL;
}
bytes_to_U16(&_retu->sequnum, payload);
const uint8_t *_it = payload + 2;
_retu->flags = *_it;
++_it;
@ -214,7 +178,11 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
const uint8_t *_it = payload;
RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader));
assert(_retu);
if ( !_retu ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
return NULL;
}
uint16_t _ext_length;
bytes_to_U16(&_ext_length, _it);
@ -231,8 +199,11 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
bytes_to_U16(&_retu->type, _it);
_it += 2;
_retu->table = calloc(_ext_length, sizeof (uint32_t));
assert(_retu->table);
if ( !(_retu->table = calloc(_ext_length, sizeof (uint32_t))) ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
free(_retu);
return NULL;
}
uint16_t _x;
@ -319,7 +290,11 @@ uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload )
RTPHeader *build_header ( RTPSession *session )
{
RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) );
assert(_retu);
if ( !_retu ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
return NULL;
}
ADD_FLAG_VERSION ( _retu, session->version );
ADD_FLAG_PADDING ( _retu, session->padding );
@ -355,7 +330,7 @@ RTPHeader *build_header ( RTPSession *session )
* @return RTPMessage*
* @retval NULL Error occurred.
*/
RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
RTPMessage *msg_parse ( const uint8_t *data, int length )
{
RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
@ -367,11 +342,9 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
return NULL;
}
_retu->header->sequnum = sequnum;
uint16_t _from_pos = _retu->header->length;
_retu->length = length - _from_pos;
_retu->length = length - _retu->header->length;
uint16_t _from_pos = _retu->header->length - 2 /* Since sequ num is excluded */ ;
if ( GET_FLAG_EXTENSION ( _retu->header ) ) {
@ -418,7 +391,7 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
RTPSession *_session = object;
RTPMessage *_msg;
if ( !_session || length < 13 + crypto_box_MACBYTES) { /* 12 is the minimum length for rtp + desc. byte */
if ( !_session || length < 13 ) { /* 12 is the minimum length for rtp + desc. byte */
LOGGER_WARNING("No session or invalid length of received buffer!");
return -1;
}
@ -428,55 +401,7 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
return -1;
}
uint8_t _plain[MAX_UDP_PACKET_SIZE];
uint16_t _sequnum;
bytes_to_U16(&_sequnum, data + 1);
/* Clculate the right nonce */
uint8_t _calculated[crypto_box_NONCEBYTES];
memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES);
increase_nonce ( _calculated, _sequnum );
/* Decrypt message */
int _decrypted_length = decrypt_data_symmetric(
(uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
/* This packet is either not encrypted properly or late
*/
if ( -1 == _decrypted_length ) {
/* If this is the case, then the packet is most likely late.
* Try with old nonce cycle.
*/
if ( _session->rsequnum < _sequnum ) {
_decrypted_length = decrypt_data_symmetric(
(uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
if ( _decrypted_length == -1 ) {
LOGGER_WARNING("Packet not ecrypted properly!");
return -1; /* This packet is not encrypted properly */
}
/* Otherwise, if decryption is ok with new cycle, set new cycle
*/
} else {
increase_nonce ( _calculated, MAX_SEQU_NUM );
_decrypted_length = decrypt_data_symmetric(
(uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
if ( _decrypted_length == -1 ) {
LOGGER_WARNING("Error decrypting!");
return -1; /* This is just an error */
}
/* A new cycle setting. */
memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES);
memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES);
}
}
_msg = msg_parse ( _sequnum, _plain, _decrypted_length );
_msg = msg_parse ( data + 1, length - 1 );
if ( !_msg ) {
LOGGER_WARNING("Could not parse message!");
@ -526,22 +451,28 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
uint8_t *_from_pos;
RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
assert(_retu);
if ( !_retu ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
return NULL;
}
/* Sets header values and copies the extension header in _retu */
_retu->header = build_header ( session ); /* It allocates memory and all */
_retu->ext_header = session->ext_header;
uint32_t _total_length = length + _retu->header->length;
uint32_t _total_length = length + _retu->header->length + 1;
_retu->data[0] = session->prefix;
if ( _retu->ext_header ) {
_total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
_from_pos = add_header ( _retu->header, _retu->data );
_from_pos = add_header ( _retu->header, _retu->data + 1 );
_from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 );
} else {
_from_pos = add_header ( _retu->header, _retu->data );
_from_pos = add_header ( _retu->header, _retu->data + 1 );
}
/*
@ -679,41 +610,17 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
return -1;
}
uint8_t _send_data [ MAX_UDP_PACKET_SIZE ];
_send_data[0] = session->prefix;
/* Generate the right nonce */
uint8_t _calculated[crypto_box_NONCEBYTES];
memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES);
increase_nonce ( _calculated, msg->header->sequnum );
/* Need to skip 2 bytes that are for sequnum */
int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
(uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
_send_data[1] = msg->data[0];
_send_data[2] = msg->data[1];
if ( -1 == send_custom_lossy_packet(messenger, session->dest, _send_data, encrypted_length + 3) ) {
if ( -1 == send_custom_lossy_packet(messenger, session->dest, msg->data, msg->length) ) {
LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno));
rtp_free_msg ( session, msg );
return -1;
}
/* Set sequ number */
if ( session->sequnum >= MAX_SEQU_NUM ) {
session->sequnum = 0;
memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES);
} else {
session->sequnum++;
}
/* Set sequ number */
session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1;
rtp_free_msg ( session, msg );
return 0;
}
@ -752,26 +659,19 @@ void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
* @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
* @param messenger Tox* object.
* @param friend_num Friend id.
* @param encrypt_key Speaks for it self.
* @param decrypt_key Speaks for it self.
* @param encrypt_nonce Speaks for it self.
* @param decrypt_nonce Speaks for it self.
* @return RTPSession* Created control session.
* @retval NULL Error occurred.
*/
RTPSession *rtp_init_session ( int payload_type,
Messenger *messenger,
int friend_num,
const uint8_t *encrypt_key,
const uint8_t *decrypt_key,
const uint8_t *encrypt_nonce,
const uint8_t *decrypt_nonce )
RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int friend_num )
{
RTPSession *_retu = calloc(1, sizeof(RTPSession));
assert(_retu);
if ( !_retu ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
return NULL;
}
if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) ||
!encrypt_key || !decrypt_key || !encrypt_nonce || !decrypt_nonce) {
if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu)) {
LOGGER_ERROR("Error setting custom register handler for rtp session");
free(_retu);
return NULL;
@ -794,23 +694,12 @@ RTPSession *rtp_init_session ( int payload_type,
_retu->ext_header = NULL; /* When needed allocate */
_retu->encrypt_key = encrypt_key;
_retu->decrypt_key = decrypt_key;
/* Need to allocate new memory */
_retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
assert(_retu->encrypt_nonce);
_retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
assert(_retu->decrypt_nonce);
_retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
assert(_retu->nonce_cycle);
memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES);
memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES);
memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES);
_retu->csrc = calloc(1, sizeof (uint32_t));
assert(_retu->csrc);
if ( !(_retu->csrc = calloc(1, sizeof (uint32_t))) ) {
LOGGER_WARNING("Alloc failed! Program might misbehave!");
free(_retu);
return NULL;
}
_retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */
@ -853,9 +742,6 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
free ( session->ext_header );
free ( session->csrc );
free ( session->decrypt_nonce );
free ( session->encrypt_nonce );
free ( session->nonce_cycle );
pthread_mutex_unlock(&session->mutex);
@ -865,4 +751,4 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
free ( session );
return 0;
}
}

View File

@ -105,18 +105,6 @@ typedef struct _RTPSession {
*/
RTPExtHeader *ext_header;
/* Since these are only references of the
* call structure don't allocate or free
*/
const uint8_t *encrypt_key;
const uint8_t *decrypt_key;
uint8_t *encrypt_nonce;
uint8_t *decrypt_nonce;
uint8_t *nonce_cycle;
RTPMessage *oldest_msg;
RTPMessage *last_msg; /* tail */
@ -192,20 +180,10 @@ void rtp_free_msg ( RTPSession *session, RTPMessage *msg );
* @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
* @param messenger Tox* object.
* @param friend_num Friend id.
* @param encrypt_key Speaks for it self.
* @param decrypt_key Speaks for it self.
* @param encrypt_nonce Speaks for it self.
* @param decrypt_nonce Speaks for it self.
* @return RTPSession* Created control session.
* @retval NULL Error occurred.
*/
RTPSession *rtp_init_session ( int payload_type,
Messenger *messenger,
int friend_num,
const uint8_t *encrypt_key,
const uint8_t *decrypt_key,
const uint8_t *encrypt_nonce,
const uint8_t *decrypt_nonce );
RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int friend_num );
/**

View File

@ -304,14 +304,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
CallSpecific *call = &av->calls[call_index];
call->crtps[audio_index] =
rtp_init_session(
type_audio,
av->messenger,
av->msi_session->calls[call_index]->peers[0],
av->msi_session->calls[call_index]->key_peer,
av->msi_session->calls[call_index]->key_local,
av->msi_session->calls[call_index]->nonce_peer,
av->msi_session->calls[call_index]->nonce_local);
rtp_init_session(type_audio, av->messenger, av->msi_session->calls[call_index]->peers[0]);
if ( !call->crtps[audio_index] ) {
@ -322,14 +315,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
if ( support_video ) {
call->crtps[video_index] =
rtp_init_session (
type_video,
av->messenger,
av->msi_session->calls[call_index]->peers[0],
av->msi_session->calls[call_index]->key_peer,
av->msi_session->calls[call_index]->key_local,
av->msi_session->calls[call_index]->nonce_peer,
av->msi_session->calls[call_index]->nonce_local);
rtp_init_session(type_video, av->messenger, av->msi_session->calls[call_index]->peers[0]);
if ( !call->crtps[video_index] ) {