Commit b01d785c authored by johan's avatar johan

Add ping and ping2ACK packets parsing/building

parent 2976e208
...@@ -62,8 +62,9 @@ typedef struct bzrtpSrtpSecrets_struct { ...@@ -62,8 +62,9 @@ typedef struct bzrtpSrtpSecrets_struct {
uint8_t cipherAlgo; /**< The cipher block algorithm used by srtp */ uint8_t cipherAlgo; /**< The cipher block algorithm used by srtp */
uint8_t cipherKeyLength; /**< The key length in bytes for the cipher block algorithm used by srtp */ uint8_t cipherKeyLength; /**< The key length in bytes for the cipher block algorithm used by srtp */
uint8_t authTagAlgo; /**< srtp authentication tag algorithm agreed on after Hello packet exchange */ uint8_t authTagAlgo; /**< srtp authentication tag algorithm agreed on after Hello packet exchange */
char *sas; /* a null terminated char containing the Short Authentication String */ char *sas; /**< a null terminated char containing the Short Authentication String */
uint8_t sasLength; /* The lenght of sas, including the termination character */ uint8_t sasLength; /**< The lenght of sas, including the termination character */
uint32_t peerSSRC; /**< The peer SSRC in order to associate the srtp keys with correct input stream */
} bzrtpSrtpSecrets_t; } bzrtpSrtpSecrets_t;
#define ZRTP_MAGIC_COOKIE 0x5a525450 #define ZRTP_MAGIC_COOKIE 0x5a525450
......
...@@ -51,25 +51,26 @@ ...@@ -51,25 +51,26 @@
#define BZRTP_CREATE_ERROR_INVALIDMESSAGETYPE 0x0a01 #define BZRTP_CREATE_ERROR_INVALIDMESSAGETYPE 0x0a01
#define BZRTP_CREATE_ERROR_UNABLETOCREATECRYPTOCONTEXT 0x0a02 #define BZRTP_CREATE_ERROR_UNABLETOCREATECRYPTOCONTEXT 0x0a02
#define BZRTP_CREATE_ERROR_INVALIDCONTEXT 0x0a04
/* map all message type to an int32_t value */
#define MSGTYPE_INVALID 0x00000000 /* map all message type to an uint8_t value */
#define MSGTYPE_HELLO 0x00000001 #define MSGTYPE_INVALID 0x00
#define MSGTYPE_HELLOACK 0x00000002 #define MSGTYPE_HELLO 0x01
#define MSGTYPE_COMMIT 0x00000004 #define MSGTYPE_HELLOACK 0x02
#define MSGTYPE_DHPART1 0x00000008 #define MSGTYPE_COMMIT 0x03
#define MSGTYPE_DHPART2 0x00000010 #define MSGTYPE_DHPART1 0x04
#define MSGTYPE_CONFIRM1 0x00000020 #define MSGTYPE_DHPART2 0x05
#define MSGTYPE_CONFIRM2 0x00000040 #define MSGTYPE_CONFIRM1 0x06
#define MSGTYPE_CONF2ACK 0x00000080 #define MSGTYPE_CONFIRM2 0x07
#define MSGTYPE_ERROR 0x00000100 #define MSGTYPE_CONF2ACK 0x08
#define MSGTYPE_ERRORACK 0x00000200 #define MSGTYPE_ERROR 0x10
#define MSGTYPE_GOCLEAR 0x00000400 #define MSGTYPE_ERRORACK 0x11
#define MSGTYPE_CLEARACK 0x00000800 #define MSGTYPE_GOCLEAR 0x12
#define MSGTYPE_SASRELAY 0x00001000 #define MSGTYPE_CLEARACK 0x13
#define MSGTYPE_RELAYACK 0x00002000 #define MSGTYPE_SASRELAY 0x14
#define MSGTYPE_PING 0x00004000 #define MSGTYPE_RELAYACK 0x15
#define MSGTYPE_PINGACK 0x00008000 #define MSGTYPE_PING 0x16
#define MSGTYPE_PINGACK 0x17
/** /**
* @brief Store all zrtpPacket informations * @brief Store all zrtpPacket informations
...@@ -256,7 +257,7 @@ typedef struct bzrtpPingAckMessage_struct { ...@@ -256,7 +257,7 @@ typedef struct bzrtpPingAckMessage_struct {
uint8_t version[4]; /**< a string defining the current version, shall be 1.10 (32 bits) */ uint8_t version[4]; /**< a string defining the current version, shall be 1.10 (32 bits) */
uint8_t endpointHash[8]; /**< see section 5.16 for the endpointHash definition (64 bits) */ uint8_t endpointHash[8]; /**< see section 5.16 for the endpointHash definition (64 bits) */
uint8_t endpointHashReceived[8]; /**< the endpoint hash received in the ping Message we're acknowledging (64 bits) */ uint8_t endpointHashReceived[8]; /**< the endpoint hash received in the ping Message we're acknowledging (64 bits) */
uint8_t SSRC; /**< the SSRC received in the ping packet we're acknowledging (64 bits) */ uint32_t SSRC; /**< the SSRC received in the ping packet we're acknowledging (32 bits) */
} bzrtpPingAckMessage_t; } bzrtpPingAckMessage_t;
......
...@@ -180,7 +180,8 @@ typedef struct bzrtpChannelContext_struct { ...@@ -180,7 +180,8 @@ typedef struct bzrtpChannelContext_struct {
uint8_t initiatorAuxsecretID[8]; /**< initiator auxiliary secret Hash */ uint8_t initiatorAuxsecretID[8]; /**< initiator auxiliary secret Hash */
uint8_t responderAuxsecretID[8]; /**< responder auxiliary secret Hash */ uint8_t responderAuxsecretID[8]; /**< responder auxiliary secret Hash */
/* temporary buffer stored in the channel context */
bzrtpPacket_t *pingPacket; /**< Temporary stores a ping packet when received to be used to create the pingACK response */
} bzrtpChannelContext_t; } bzrtpChannelContext_t;
......
...@@ -659,9 +659,27 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC ...@@ -659,9 +659,27 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
zrtpPacket->messageData = (void *)messageData; zrtpPacket->messageData = (void *)messageData;
} }
break; /* MSGTYPE_CONFIRM1 and MSGTYPE_CONFIRM2 */ break; /* MSGTYPE_CONFIRM1 and MSGTYPE_CONFIRM2 */
case MSGTYPE_CONF2ACK: case MSGTYPE_CONF2ACK:
/* nothing to do for this one */ /* nothing to do for this one */
break; /* MSGTYPE_CONF2ACK */ break; /* MSGTYPE_CONF2ACK */
case MSGTYPE_PING:
{
/* allocate a ping message structure */
bzrtpPingMessage_t *messageData;
messageData = (bzrtpPingMessage_t *)malloc(sizeof(bzrtpPingMessage_t));
/* fill the structure */
memcpy(messageData->version, messageContent, 4);
messageContent +=4;
memcpy(messageData->endpointHash, messageContent, 8);
/* attach the message structure to the packet one */
zrtpPacket->messageData = (void *)messageData;
}
break; /* MSGTYPE_PING */
} }
return 0; return 0;
...@@ -838,6 +856,7 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh ...@@ -838,6 +856,7 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh
case MSGTYPE_DHPART1 : case MSGTYPE_DHPART1 :
case MSGTYPE_DHPART2 : case MSGTYPE_DHPART2 :
{
/* get the DHPart message structure */ /* get the DHPart message structure */
if (zrtpPacket->messageData == NULL) { if (zrtpPacket->messageData == NULL) {
return BZRTP_BUILDER_ERROR_INVALIDMESSAGE; return BZRTP_BUILDER_ERROR_INVALIDMESSAGE;
...@@ -875,7 +894,7 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh ...@@ -875,7 +894,7 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh
/* there is a MAC to compute, set the pointers to the key and MAC output buffer */ /* there is a MAC to compute, set the pointers to the key and MAC output buffer */
MACbuffer = messageString; MACbuffer = messageString;
MACkey = zrtpChannelContext->selfH[0]; /* HMAC of Hello packet is keyed by H0 which have been set at context initialising */ MACkey = zrtpChannelContext->selfH[0]; /* HMAC of Hello packet is keyed by H0 which have been set at context initialising */
}
break; /* MSGTYPE_DHPART1 and 2 */ break; /* MSGTYPE_DHPART1 and 2 */
case MSGTYPE_CONFIRM1: case MSGTYPE_CONFIRM1:
...@@ -951,8 +970,8 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh ...@@ -951,8 +970,8 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh
/* add the CFB IV */ /* add the CFB IV */
memcpy(messageString, messageData->CFBIV, 16); memcpy(messageString, messageData->CFBIV, 16);
} }
break; /* MSGTYPE_CONFIRM1 and MSGTYPE_CONFIRM2 */ break; /* MSGTYPE_CONFIRM1 and MSGTYPE_CONFIRM2 */
case MSGTYPE_CONF2ACK: case MSGTYPE_CONF2ACK:
{ {
/* the message length is fixed */ /* the message length is fixed */
...@@ -961,9 +980,33 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh ...@@ -961,9 +980,33 @@ int bzrtp_packetBuild(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpCh
/* allocate the packetString buffer : packet is header+message+crc */ /* allocate the packetString buffer : packet is header+message+crc */
zrtpPacket->packetString = (uint8_t *)malloc((ZRTP_PACKET_HEADER_LENGTH+ZRTP_CONF2ACKMESSAGE_FIXED_LENGTH+ZRTP_PACKET_CRC_LENGTH)*sizeof(uint8_t)); zrtpPacket->packetString = (uint8_t *)malloc((ZRTP_PACKET_HEADER_LENGTH+ZRTP_CONF2ACKMESSAGE_FIXED_LENGTH+ZRTP_PACKET_CRC_LENGTH)*sizeof(uint8_t));
} }
break; /* MSGTYPE_CONF2ACK */ break; /* MSGTYPE_CONF2ACK */
case MSGTYPE_PINGACK:
{
/* the message length is fixed */
zrtpPacket->messageLength = ZRTP_PINGACKMESSAGE_FIXED_LENGTH;
/* allocate the packetString buffer : packet is header+message+crc */
zrtpPacket->packetString = (uint8_t *)malloc((ZRTP_PACKET_HEADER_LENGTH+ZRTP_PINGACKMESSAGE_FIXED_LENGTH+ZRTP_PACKET_CRC_LENGTH)*sizeof(uint8_t));
messageString = zrtpPacket->packetString + ZRTP_PACKET_HEADER_LENGTH + ZRTP_MESSAGE_HEADER_LENGTH;
/* now insert the different message parts into the packetString */
bzrtpPingAckMessage_t *messageData = (bzrtpPingAckMessage_t *)zrtpPacket->messageData;
memcpy(messageString, messageData->version, 4);
messageString += 4;
memcpy(messageString, messageData->endpointHash, 8);
messageString += 8;
memcpy(messageString, messageData->endpointHashReceived, 8);
messageString += 8;
*messageString++ = (uint8_t)((messageData->SSRC>>24)&0xFF);
*messageString++ = (uint8_t)((messageData->SSRC>>16)&0xFF);
*messageString++ = (uint8_t)((messageData->SSRC>>8)&0xFF);
*messageString++ = (uint8_t)(messageData->SSRC&0xFF);
}
break; /* MSGTYPE_PINGACK */
} }
/* write headers only if we have a packet string */ /* write headers only if we have a packet string */
...@@ -1193,6 +1236,30 @@ bzrtpPacket_t *bzrtp_createZrtpPacket(bzrtpContext_t *zrtpContext, bzrtpChannelC ...@@ -1193,6 +1236,30 @@ bzrtpPacket_t *bzrtp_createZrtpPacket(bzrtpContext_t *zrtpContext, bzrtpChannelC
/* nothing to do for the conf2ACK packet as it just contains it's type */ /* nothing to do for the conf2ACK packet as it just contains it's type */
} }
break; /* MSGTYPE_CONF2ACK */ break; /* MSGTYPE_CONF2ACK */
case MSGTYPE_PINGACK:
{
/* to create a pingACK we must have a ping packet in the channel context, check it */
bzrtpPacket_t *pingPacket = zrtpChannelContext->pingPacket;
if (pingPacket == NULL) {
*exitCode = BZRTP_CREATE_ERROR_INVALIDCONTEXT;
return NULL;
}
bzrtpPingMessage_t *pingMessage = (bzrtpPingMessage_t *)pingPacket->messageData;
/* create the message */
bzrtpPingAckMessage_t *zrtpPingAckMessage = (bzrtpPingAckMessage_t *)malloc(sizeof(bzrtpPingAckMessage_t));
memset(zrtpPingAckMessage, 0, sizeof(bzrtpPingAckMessage_t));
/* initialise all fields using zrtp context data and the received ping message */
memcpy(zrtpPingAckMessage->version,ZRTP_VERSION , 4); /* we support version 1.10 only, so no need to even check what was sent in the ping */
memcpy(zrtpPingAckMessage->endpointHash, zrtpContext->selfZID, 8); /* as suggested in rfc section 5.16, use the truncated ZID as endPoint hash */
memcpy(zrtpPingAckMessage->endpointHashReceived, pingMessage->endpointHash, 8);
zrtpPingAckMessage->SSRC = pingPacket->sourceIdentifier;
/* attach the message data to the packet */
zrtpPacket->messageData = zrtpPingAckMessage;
} /* MSGTYPE_PINGACK */
break;
default: default:
free(zrtpPacket); free(zrtpPacket);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment