Commit 4e60d73a authored by johan's avatar johan

Have the whole cache file loaded in a buffer

+ remove peerSSRC from context as it was useless
+ get correct test patterns for parser/builder
parent b01d785c
......@@ -64,7 +64,6 @@ typedef struct bzrtpSrtpSecrets_struct {
uint8_t authTagAlgo; /**< srtp authentication tag algorithm agreed on after Hello packet exchange */
char *sas; /**< a null terminated char containing the Short Authentication String */
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;
#define ZRTP_MAGIC_COOKIE 0x5a525450
......@@ -112,10 +111,8 @@ __attribute__ ((visibility ("default"))) void bzrtp_initBzrtpContext(bzrtpContex
*/
__attribute__ ((visibility ("default"))) void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC);
#define ZRTP_CALLBACK_READCACHE 0x0101
#define ZRTP_CALLBACK_LOADCACHE 0x0101
#define ZRTP_CALLBACK_WRITECACHE 0x0102
#define ZRTP_CALLBACK_SETCACHEPOSITION 0x0104
#define ZRTP_CALLBACK_GETCACHEPOSITION 0x0108
#define ZRTP_CALLBACK_SENDDATA 0x0110
#define ZRTP_CALLBACK_SRTPSECRETSAVAILABLE 0x0120
#define ZRTP_CALLBACK_STARTSRTPSESSION 0x0140
......
......@@ -100,11 +100,9 @@ typedef struct cachedSecretsHash_struct {
* @brief All the callback functions provided by the client needed by the ZRTP engine
*/
typedef struct zrtpCallbacks_struct {
/* cache */
int (* bzrtp_readCache)(uint8_t *output, uint16_t size); /**< Cache related function : read size bytes from cache, shall return the number of bytes read */
int (* bzrtp_writeCache)(uint8_t *input, uint16_t size); /**< Cache related function : write size bytes to cache */
int (* bzrtp_setCachePosition)(long position); /**< Cache related function : set cache position in cache file, rewind when passing 0 */
int (* bzrtp_getCachePosition)(long *position); /**< Cache related function : get the current cache position in cache file */
/* cache related functions */
int (* bzrtp_loadCache)(uint8_t **cacheBuffer, uint32_t *cacheBufferSize); /**< Cache related function : load the whole cache file in a buffer allocated by the function, return the buffer and its size in bytes */
int (* bzrtp_writeCache)(uint8_t *input, uint32_t size); /**< Cache related function : write size bytes to cache */
/* sending packets */
int (* bzrtp_sendData)(void *clientData, uint8_t *packetString, uint16_t packetLength); /**< Send a ZRTP packet to peer. Shall return 0 on success */
......@@ -128,11 +126,6 @@ typedef struct bzrtpChannelContext_struct {
uint32_t selfSSRC; /**< A context is identified by his own SSRC and the peer one */
/* USELESS?? */
uint32_t peerSSRC; /**< the SSRC of the peer end point */
uint8_t peerSSRCAssociated; /**< true if this channel is already associated with a peer SSRC, false otherwise */
/* USELESS?? */
/* flags */
uint8_t isSecure; /**< This flag is set to 1 when the ZRTP negociation ends and SRTP secrets are generated and confirmed for this channel */
......@@ -219,6 +212,8 @@ typedef struct bzrtpContext_struct {
uint8_t supportedSas[7]; /**< list of supported Sas representations mapped to uint8_t */
/* ZIDs and cache */
uint8_t *cacheBuffer; /**< cache file is load in this buffer to be parsed/written */
uint32_t cacheBufferLength; /**< length in byte of the cache buffer */
uint8_t selfZID[12]; /**< The ZRTP Identifier of this ZRTP end point - a random if running cache less */
uint8_t peerZID[12]; /**< The ZRTP Identifier of the peer ZRTP end point - given by the Hello packet */
cachedSecrets_t cachedSecret; /**< the local cached secrets */
......
......@@ -4,7 +4,7 @@
@brief all ZID and cache related operations are implemented in this file
- get or create ZID
- get/update associated secrets
It supports cacheless implementation (as a compile option)
It supports cacheless implementation when cache file access functions are null
@author Johan Pascal
......@@ -43,7 +43,7 @@
*
* @return 0 on success
*/
int getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]);
int bzrtp_getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]);
/**
* @brief Parse the cache to find secrets associated to the given ZID, set them and their length in the context if they are found
......@@ -53,6 +53,6 @@ int getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]);
*
* return 0 on succes, error code otherwise
*/
int getPeerAssociatedSecretsHash(bzrtpContext_t *context, uint8_t peerZID[12]);
int bzrtp_getPeerAssociatedSecretsHash(bzrtpContext_t *context, uint8_t peerZID[12]);
#endif /* ZIDCACHE_H */
......@@ -65,10 +65,8 @@ bzrtpContext_t *bzrtp_createBzrtpContext(uint32_t selfSSRC)
context->peerSupportMultiChannel = 0; /* peer does not support Multichannel by default */
/* set to NULL all callbacks pointer */
context->zrtpCallbacks.bzrtp_readCache = NULL;
context->zrtpCallbacks.bzrtp_loadCache = NULL;
context->zrtpCallbacks.bzrtp_writeCache = NULL;
context->zrtpCallbacks.bzrtp_setCachePosition = NULL;
context->zrtpCallbacks.bzrtp_getCachePosition = NULL;
context->zrtpCallbacks.bzrtp_sendData = NULL;
context->zrtpCallbacks.bzrtp_srtpSecretsAvailable = NULL;
context->zrtpCallbacks.bzrtp_startSrtpSession = NULL;
......@@ -91,6 +89,8 @@ bzrtpContext_t *bzrtp_createBzrtpContext(uint32_t selfSSRC)
context->sc = bzrtpCrypto_getAvailableCryptoTypes(ZRTP_SAS_TYPE, context->supportedSas);
/* initialise cached secret buffer to null */
context->cacheBuffer = NULL;
context->cacheBufferLength = 0;
context->cachedSecret.rs1 = NULL;
context->cachedSecret.rs1Length = 0;
context->cachedSecret.rs2 = NULL;
......@@ -114,9 +114,13 @@ bzrtpContext_t *bzrtp_createBzrtpContext(uint32_t selfSSRC)
* @param[in] context The context to initialise
*/
void bzrtp_initBzrtpContext(bzrtpContext_t *context) {
/* load the cache buffer */
if (context->zrtpCallbacks.bzrtp_loadCache != 0) {
context->zrtpCallbacks.bzrtp_loadCache(&context->cacheBuffer, &(context->cacheBufferLength));
}
/* initialise ZID. Randomly generated if no ZID is found in cache */
getSelfZID(context, context->selfZID);
/* initialise ZID. Randomly generated if no ZID is found in cache or no cache found */
bzrtp_getSelfZID(context, context->selfZID);
}
/*
......@@ -187,17 +191,11 @@ void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC)
*/
int bzrtp_setCallback(bzrtpContext_t *context, int (*functionPointer)(), uint16_t functionID) {
switch (functionID) {
case ZRTP_CALLBACK_READCACHE:
context->zrtpCallbacks.bzrtp_readCache = (int (*)(uint8_t *, uint16_t))functionPointer;
case ZRTP_CALLBACK_LOADCACHE:
context->zrtpCallbacks.bzrtp_loadCache = (int (*)(uint8_t **, uint32_t *))functionPointer;
break;
case ZRTP_CALLBACK_WRITECACHE:
context->zrtpCallbacks.bzrtp_writeCache = (int (*)(uint8_t *, uint16_t))functionPointer;
break;
case ZRTP_CALLBACK_SETCACHEPOSITION:
context->zrtpCallbacks.bzrtp_setCachePosition = (int (*)(long))functionPointer;
break;
case ZRTP_CALLBACK_GETCACHEPOSITION:
context->zrtpCallbacks.bzrtp_getCachePosition = (int (*)(long *))functionPointer;
context->zrtpCallbacks.bzrtp_writeCache = (int (*)(uint8_t *, uint32_t))functionPointer;
break;
case ZRTP_CALLBACK_SENDDATA:
context->zrtpCallbacks.bzrtp_sendData = (int (*)(void *, uint8_t *, uint16_t))functionPointer;
......@@ -534,7 +532,6 @@ int bzrtp_initChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t
zrtpChannelContext->srtpSecrets.authTagAlgo = ZRTP_UNSET_ALGO;
zrtpChannelContext->srtpSecrets.sas = NULL;
zrtpChannelContext->srtpSecrets.sasLength = 0;
zrtpChannelContext->srtpSecrets.peerSSRC = 0;
return 0;
}
......
......@@ -1511,11 +1511,10 @@ int bzrtp_responseToHelloMessage(bzrtpContext_t *zrtpContext, bzrtpChannelContex
memcpy(zrtpContext->peerZID, helloMessage->ZID, 12); /* peer ZID */
memcpy(zrtpChannelContext->peerH[3], helloMessage->H3, 32); /* H3 */
zrtpChannelContext->peerPackets[HELLO_MESSAGE_STORE_ID] = zrtpPacket; /* peer hello packet */
zrtpChannelContext->peerSSRC = zrtpPacket->sourceIdentifier;
/* get from cache, if relevant, the retained secrets associated to the peer ZID */
if (zrtpContext->cachedSecret.rs1 == NULL) { /* if we do not have already secret hashes in this session context. Note, they may be updated in cache file but they also will be in the context at the same time, so no need to parse the cache again */
getPeerAssociatedSecretsHash(zrtpContext, helloMessage->ZID);
bzrtp_getPeerAssociatedSecretsHash(zrtpContext, helloMessage->ZID);
}
/* now compute the retained secret hashes (secrets may be updated but not their hash) as in rfc section 4.3.1 */
......@@ -1979,8 +1978,5 @@ int bzrtp_deriveSrtpKeysFromS0(bzrtpContext_t *zrtpContext, bzrtpChannelContext_
zrtpChannelContext->sasFunction(sasValue, zrtpChannelContext->srtpSecrets.sas);
}
/* add the peerSSRC */
zrtpChannelContext->srtpSecrets.peerSSRC = zrtpChannelContext->peerSSRC;
return 0;
}
This diff is collapsed.
......@@ -460,12 +460,9 @@ void test_algoAgreement(void) {
/* and an hello packet to simulate the one received from peer */
bzrtpPacket_t *helloPacket = bzrtp_createZrtpPacket(zrtpContext, zrtpContext->channelContext[0], MSGTYPE_HELLO, &retval); /* 0x12345678 is the SSRC of sender */
dumpContext("initial", zrtpContext);
/* Test 1: Context and packet have been initialised with default values algo : DH3k, DH2k for key agreement type and shall then return DH3k as choosen key agreement algo */
retval = crypoAlgoAgreement(zrtpContext, zrtpContext->channelContext[0], helloPacket->messageData);
printf("cryptagre retval is %x\n", retval);
if ((retval==0)
&& (zrtpContext->channelContext[0]->keyAgreementAlgo == ZRTP_KEYAGREEMENT_DH3k)
&& (zrtpContext->channelContext[0]->hashAlgo == ZRTP_HASH_S256)
......@@ -476,7 +473,6 @@ void test_algoAgreement(void) {
} else {
CU_FAIL("Algo agreement test 1");
}
dumpContext("test1", zrtpContext);
/* Test 2: now modify the Hello packet to have "DH2k, DH3k" preference order in the hello packet but keep the context order "DH3k, DH2k".
* We shall pick the fastest -> DH2k */
......@@ -485,13 +481,12 @@ void test_algoAgreement(void) {
helloMessage->supportedKeyAgreement[1] = ZRTP_KEYAGREEMENT_DH3k;
retval = crypoAlgoAgreement(zrtpContext, zrtpContext->channelContext[0], helloPacket->messageData);
printf("cryptagre retval is %x\n", retval);
if ((retval==0)
&& (zrtpContext->channelContext[0]->keyAgreementAlgo == ZRTP_KEYAGREEMENT_DH2k)
&& (zrtpContext->channelContext[0]->hashAlgo == ZRTP_HASH_S256)
&& (zrtpContext->channelContext[0]->cipherAlgo == ZRTP_CIPHER_AES1)
&& (zrtpContext->channelContext[0]->authTagAlgo == ZRTP_AUTHTAG_HS32)
&& (zrtpContext->channelContext[0]->sasAlgo == ZRTP_SAS_B32)) {
if ((retval==0)
&& (zrtpContext->channelContext[0]->keyAgreementAlgo == ZRTP_KEYAGREEMENT_DH2k)
&& (zrtpContext->channelContext[0]->hashAlgo == ZRTP_HASH_S256)
&& (zrtpContext->channelContext[0]->cipherAlgo == ZRTP_CIPHER_AES1)
&& (zrtpContext->channelContext[0]->authTagAlgo == ZRTP_AUTHTAG_HS32)
&& (zrtpContext->channelContext[0]->sasAlgo == ZRTP_SAS_B32)) {
CU_PASS("Algo agreement test 2");
} else {
CU_FAIL("Algo agreement test 2");
......
This diff is collapsed.
......@@ -52,9 +52,9 @@ int main(int argc, char *argv[] ) {
/* Add the parser suite to the registry */
parserTestSuite = CU_add_suite("Bzrtp ZRTP Packet Parser", NULL, NULL);
/* CU_add_test(parserTestSuite, "Parse", test_parser);
CU_add_test(parserTestSuite, "Parse Exchange", test_parserComplete);*/
CU_add_test(parserTestSuite, "State machine", test_stateMachine);
/* CU_add_test(parserTestSuite, "Parse", test_parser);*/
CU_add_test(parserTestSuite, "Parse Exchange", test_parserComplete);
/* CU_add_test(parserTestSuite, "State machine", test_stateMachine);*/
/* Run all suites */
printf("\n\n#### Run the Bzrtp Crypto Wrappers tests suite\n");
......
......@@ -32,7 +32,7 @@ void printHex(char *title, uint8_t *data, uint32_t length) {
printf ("%s : ", title);
int i;
for (i=0; i<length; i++) {
printf ("%02x ", data[i]);
printf ("0x%02x, ", data[i]);
}
printf ("\n");
}
......@@ -180,7 +180,7 @@ void dumpContext(char *title, bzrtpContext_t *zrtpContext) {
for (i=0; i<ZRTP_MAX_CHANNEL_NUMBER; i++) {
if (zrtpContext->channelContext[i] != NULL) {
bzrtpChannelContext_t *channelContext = zrtpContext->channelContext[i];
printf("Channel %i\n self: %08x peer %08x\n", i, channelContext->selfSSRC, channelContext->peerSSRC);
printf("Channel %i\n self: %08x\n", i, channelContext->selfSSRC);
printf (" selfH: ");
for (j=0; j<4; j++) {
printHex(" ", channelContext->selfH[j], 32);
......@@ -214,6 +214,18 @@ void dumpContext(char *title, bzrtpContext_t *zrtpContext) {
printHex(" peersrtp key", channelContext->srtpSecrets.peerSrtpKey, channelContext->srtpSecrets.peerSrtpKeyLength);
printHex(" peersrtp salt", channelContext->srtpSecrets.peerSrtpSalt, channelContext->srtpSecrets.peerSrtpSaltLength);
}
if (channelContext->mackeyi!=NULL) {
printHex(" mackeyi", channelContext->mackeyi, channelContext->hashLength);
}
if (channelContext->mackeyr!=NULL) {
printHex(" mackeyr", channelContext->mackeyr, channelContext->hashLength);
}
if (channelContext->zrtpkeyi!=NULL) {
printHex(" zrtpkeyi", channelContext->zrtpkeyi, channelContext->cipherKeyLength);
}
if (channelContext->zrtpkeyr!=NULL) {
printHex(" zrtpkeyr", channelContext->zrtpkeyr, channelContext->cipherKeyLength);
}
}
}
......
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