Commit 77f38ace authored by johan's avatar johan

Fix memory/crypto wrapper related bugs

parent 6310c8a0
......@@ -113,7 +113,7 @@ bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t e
* @return 0 on success.
*
*/
int bzrtpCrypto_getRandom(bzrtpRNGContext_t *context, uint8_t *output, int16_t outputLength);
int bzrtpCrypto_getRandom(bzrtpRNGContext_t *context, uint8_t *output, size_t outputLength);
/**
......@@ -137,9 +137,9 @@ int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context);
*
*/
void bzrtpCrypto_hmacSha256(const uint8_t *key,
uint8_t keyLength,
size_t keyLength,
const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output);
......@@ -152,7 +152,7 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key,
*
*/
void bzrtpCrypto_sha256(const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hashLength,
uint8_t *output);
......@@ -167,9 +167,9 @@ void bzrtpCrypto_sha256(const uint8_t *input,
*
*/
void bzrtpCrypto_hmacSha1(const uint8_t *key,
uint8_t keyLength,
size_t keyLength,
const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output);
......@@ -187,7 +187,7 @@ void bzrtpCrypto_hmacSha1(const uint8_t *key,
void bzrtpCrypto_aes128CfbEncrypt(const uint8_t *key,
const uint8_t *IV,
const uint8_t *input,
uint16_t inputLength,
size_t inputLength,
uint8_t *output);
/**
......@@ -204,7 +204,7 @@ void bzrtpCrypto_aes128CfbEncrypt(const uint8_t *key,
void bzrtpCrypto_aes128CfbDecrypt(const uint8_t *key,
const uint8_t *IV,
const uint8_t *input,
uint16_t inputLength,
size_t inputLength,
uint8_t *output);
/**
......@@ -245,7 +245,7 @@ bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretL
* @param[in] rngContext pointer to the rng context if neeeded
*
*/
void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, uint16_t), void *rngContext);
void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext);
/**
*
......@@ -258,7 +258,7 @@ void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(
* @param[in] rngContext Pointer to the RNG function context
*
*/
void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, uint16_t), void *rngContext);
void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext);
/**
*
......
......@@ -154,10 +154,10 @@ typedef struct bzrtpChannelContext_struct {
uint8_t sasLength; /**< lenght of the SAS depends on the algorithm agreed */
/* function pointer to the agreed algorithms - Note, key agreement manage directly this selection so it is not set here */
void (*hmacFunction)(const uint8_t *key, uint8_t keyLength, const uint8_t *input, uint32_t inputLength, uint8_t hmacLength, uint8_t *output); /**< function pointer to the agreed hmacFunction */
void (*hashFunction)(const uint8_t *input, uint32_t inputLength, uint8_t hashLength, uint8_t *output); /**< function pointer to the agreed hash function */
void (*cipherEncryptionFunction)(const uint8_t *key, const uint8_t *IV, const uint8_t *input, uint16_t inputLength, uint8_t *output); /**< function pointer to the agreed cipher block function, encryption mode */
void (*cipherDecryptionFunction)(const uint8_t *key, const uint8_t *IV, const uint8_t *input, uint16_t inputLength, uint8_t *output); /**< function pointer to the agreed cipher block function, decryption mode */
void (*hmacFunction)(const uint8_t *key, size_t keyLength, const uint8_t *input, size_t inputLength, uint8_t hmacLength, uint8_t *output); /**< function pointer to the agreed hmacFunction */
void (*hashFunction)(const uint8_t *input, size_t inputLength, uint8_t hashLength, uint8_t *output); /**< function pointer to the agreed hash function */
void (*cipherEncryptionFunction)(const uint8_t *key, const uint8_t *IV, const uint8_t *input, size_t inputLength, uint8_t *output); /**< function pointer to the agreed cipher block function, encryption mode */
void (*cipherDecryptionFunction)(const uint8_t *key, const uint8_t *IV, const uint8_t *input, size_t inputLength, uint8_t *output); /**< function pointer to the agreed cipher block function, decryption mode */
void (*sasFunction)(uint32_t sas, char output[4]); /**< function pointer to the agreed sas rendering function */
/* keys */
......
......@@ -170,6 +170,8 @@ void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC) {
context->cachedSecret.auxsecret=NULL;
context->ZRTPSess=NULL;
free(context->cacheBuffer);
context->cacheBuffer=NULL;
/* destroy the RNG context at the end because it may be needed to destroy some keys */
bzrtpCrypto_destroyRNG(context->RNGContext);
......@@ -626,8 +628,8 @@ void bzrtp_destroyChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContex
/* free the allocated buffers */
for (i=0; i<PACKET_STORAGE_CAPACITY; i++) {
free(zrtpChannelContext->selfPackets[i]);
free(zrtpChannelContext->peerPackets[i]);
bzrtp_freeZrtpPacket(zrtpChannelContext->selfPackets[i]);
bzrtp_freeZrtpPacket(zrtpChannelContext->peerPackets[i]);
zrtpChannelContext->selfPackets[i] = NULL;
zrtpChannelContext->peerPackets[i] = NULL;
}
......@@ -644,4 +646,7 @@ void bzrtp_destroyChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContex
free(zrtpChannelContext->srtpSecrets.peerSrtpKey);
free(zrtpChannelContext->srtpSecrets.peerSrtpSalt);
free(zrtpChannelContext->srtpSecrets.sas);
/* free the channel context */
free(zrtpChannelContext);
}
......@@ -108,10 +108,10 @@ bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t e
return context;
}
int bzrtpCrypto_getRandom(bzrtpRNGContext_t *context, uint8_t *output, int16_t outputLength) {
int bzrtpCrypto_getRandom(bzrtpRNGContext_t *context, uint8_t *output, size_t outputLength) {
/* get polarssl context data */
polarsslRNGContext_t *polarsslContext = (polarsslRNGContext_t *)context->cryptoModuleData;
return ctr_drbg_random((void *)&(polarsslContext->rngContext), (unsigned char *)output, (size_t)outputLength);
return ctr_drbg_random((void *)&(polarsslContext->rngContext), (unsigned char *)output, outputLength);
}
int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context) {
......@@ -139,9 +139,9 @@ int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context) {
*
*/
void bzrtpCrypto_hmacSha1(const uint8_t *key,
uint8_t keyLength,
size_t keyLength,
const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
......@@ -206,7 +206,7 @@ bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretL
}
/* generate the random secret and compute the public value */
void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, uint16_t), void *rngContext) {
void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext) {
/* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
......@@ -220,17 +220,14 @@ void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(
/* clean DHM context */
void bzrtpCrypto_DestroyDHMContext(bzrtpDHMContext_t *context) {
if (context!= NULL) {
/* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
dhm_free(polarsslContext);
free(context->cryptoModuleData);
free(context->secret);
free(context->self);
free(context->key);
free(context->peer);
dhm_free((dhm_context *)context->cryptoModuleData);
free(context->cryptoModuleData);
free(context);
}
}
......@@ -249,7 +246,7 @@ void bzrtpCrypto_DestroyDHMContext(bzrtpDHMContext_t *context) {
void bzrtpCrypto_aes128CfbEncrypt(const uint8_t key[16],
const uint8_t IV[16],
const uint8_t *input,
uint16_t inputLength,
size_t inputLength,
uint8_t *output)
{
size_t iv_offset=0; /* is not used by us but needed and updated by polarssl */
......@@ -281,7 +278,7 @@ void bzrtpCrypto_aes128CfbEncrypt(const uint8_t key[16],
void bzrtpCrypto_aes128CfbDecrypt(const uint8_t key[16],
const uint8_t IV[16],
const uint8_t *input,
uint16_t inputLength,
size_t inputLength,
uint8_t *output)
{
size_t iv_offset=0; /* is not used by us but needed and updated by polarssl */
......@@ -317,7 +314,7 @@ void bzrtpCrypto_aes128CfbDecrypt(const uint8_t key[16],
*
*/
void bzrtpCrypto_sha256(const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hashLength,
uint8_t *output)
{
......@@ -343,9 +340,9 @@ void bzrtpCrypto_sha256(const uint8_t *input,
*
*/
void bzrtpCrypto_hmacSha256(const uint8_t *key,
uint8_t keyLength,
size_t keyLength,
const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
......@@ -361,17 +358,14 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key,
}
/* compute secret - the ->peer field of context must have been set before calling this function */
void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, uint16_t), void *rngContext) {
/* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext) {
/* import the peer public value G^Y mod P in the polar ssl context */
dhm_read_public(polarsslContext, context->peer, context->primeLength);
dhm_read_public((dhm_context *)(context->cryptoModuleData), context->peer, context->primeLength);
/* compute the secret key */
uint16_t keyLength= context->primeLength; /* undocumented but this value seems to be in/out, so we must set it to the expected key length */
size_t keyLength = context->primeLength; /* undocumented but this value seems to be in/out, so we must set it to the expected key length */
context->key = (uint8_t *)malloc(keyLength*sizeof(uint8_t)); /* allocate key buffer */
dhm_calc_secret(polarsslContext, context->key, (size_t *)(&keyLength), (int (*)(void *, unsigned char *, size_t))rngFunction, rngContext);
dhm_calc_secret((dhm_context *)(context->cryptoModuleData), context->key, &keyLength, (int (*)(void *, unsigned char *, size_t))rngFunction, rngContext);
}
......@@ -381,9 +375,6 @@ void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)
/* Hashs */
#include "polarssl/sha2.h"
/* Asymmetrics encryption */
#include "polarssl/dhm.h"
/*
* @brief SHA256 wrapper
* @param[in] input Input data buffer
......@@ -393,7 +384,7 @@ void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)
*
*/
void bzrtpCrypto_sha256(const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hashLength,
uint8_t *output)
{
......@@ -419,9 +410,9 @@ void bzrtpCrypto_sha256(const uint8_t *input,
*
*/
void bzrtpCrypto_hmacSha256(const uint8_t *key,
uint8_t keyLength,
size_t keyLength,
const uint8_t *input,
uint32_t inputLength,
size_t inputLength,
uint8_t hmacLength,
uint8_t *output)
{
......@@ -437,17 +428,14 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key,
}
/* compute secret - the ->peer field of context must have been set before calling this function */
void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, uint16_t), void *rngContext) {
/* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext) {
/* import the peer public value G^Y mod P in the polar ssl context */
dhm_read_public(polarsslContext, context->peer, context->primeLength);
dhm_read_public((dhm_context *)(context->cryptoModuleData), context->peer, context->primeLength);
/* compute the secret key */
uint16_t keyLength= context->primeLength; /* undocumented but this value seems to be in/out, so we must set it to the expected key length */
size_t keyLength= context->primeLength; /* undocumented but this value seems to be in/out, so we must set it to the expected key length */
context->key = (uint8_t *)malloc(keyLength*sizeof(uint8_t)); /* allocate key buffer */
dhm_calc_secret(polarsslContext, context->key, (size_t *)(&keyLength));
dhm_calc_secret((dhm_context *)(context->cryptoModuleData), context->key, &keyLength);
}
#endif /* POLARSSL Version 1.2 */
......@@ -544,8 +544,9 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
}
/* get plain message */
uint8_t *confirmPlainMessage = (uint8_t *)malloc(cipherTextLength*sizeof(uint8_t));
zrtpChannelContext->cipherDecryptionFunction(confirmMessageKey, messageData->CFBIV, messageContent, cipherTextLength, confirmPlainMessage);
uint8_t *confirmPlainMessageBuffer = (uint8_t *)malloc(cipherTextLength*sizeof(uint8_t));
zrtpChannelContext->cipherDecryptionFunction(confirmMessageKey, messageData->CFBIV, messageContent, cipherTextLength, confirmPlainMessageBuffer);
uint8_t *confirmPlainMessage = confirmPlainMessageBuffer; /* point into the allocated buffer */
/* parse it */
memcpy(messageData->H0, confirmPlainMessage, 32);
......@@ -654,6 +655,9 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
messageData->signatureBlock = NULL;
}
/* free plain buffer */
free(confirmPlainMessageBuffer);
/* attach the message structure to the packet one */
zrtpPacket->messageData = (void *)messageData;
}
......@@ -1200,8 +1204,9 @@ bzrtpPacket_t *bzrtp_createZrtpPacket(bzrtpContext_t *zrtpContext, bzrtpChannelC
}
/* now compute the public value */
bzrtpCrypto_DHMCreatePublic(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, zrtpContext->RNGContext);
zrtpDHPartMessage->pv = zrtpContext->DHMContext->self; /* pv just point to the value into the DHMContext */
bzrtpCrypto_DHMCreatePublic(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, zrtpContext->RNGContext);
zrtpDHPartMessage->pv = (uint8_t *)malloc((zrtpChannelContext->keyAgreementLength)*sizeof(uint8_t));
memcpy(zrtpDHPartMessage->pv, zrtpContext->DHMContext->self, zrtpChannelContext->keyAgreementLength);
/* attach the message data to the packet */
zrtpPacket->messageData = zrtpDHPartMessage;
......
......@@ -534,7 +534,7 @@ int state_keyAgreement_sendingCommit(bzrtpEvent_t event) {
/* Compute the shared DH secret */
zrtpContext->DHMContext->peer = (uint8_t *)malloc(zrtpChannelContext->keyAgreementLength*sizeof(uint8_t));
memcpy (zrtpContext->DHMContext->peer, dhPart1Message->pv, zrtpChannelContext->keyAgreementLength);
bzrtpCrypto_DHMComputeSecret(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)zrtpContext->RNGContext);
bzrtpCrypto_DHMComputeSecret(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)zrtpContext->RNGContext);
/* Derive the s0 key */
bzrtp_computeS0DHMMode(zrtpContext, zrtpChannelContext);
......@@ -775,8 +775,9 @@ int state_keyAgreement_responderSendingDHPart1(bzrtpEvent_t event) {
zrtpChannelContext->peerPackets[DHPART_MESSAGE_STORE_ID] = zrtpPacket;
/* Compute the shared DH secret */
zrtpContext->DHMContext->peer = dhPart2Message->pv;
bzrtpCrypto_DHMComputeSecret(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)zrtpContext->RNGContext);
zrtpContext->DHMContext->peer = (uint8_t *)malloc(zrtpChannelContext->keyAgreementLength*sizeof(uint8_t));
memcpy (zrtpContext->DHMContext->peer, dhPart2Message->pv, zrtpChannelContext->keyAgreementLength);
bzrtpCrypto_DHMComputeSecret(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)zrtpContext->RNGContext);
/* Derive the s0 key */
bzrtp_computeS0DHMMode(zrtpContext, zrtpChannelContext);
......@@ -2012,7 +2013,7 @@ int bzrtp_deriveSrtpKeysFromS0(bzrtpContext_t *zrtpContext, bzrtpChannelContext_
/* now get it into a char according to the selected algo */
uint32_t sasValue = ((uint32_t)sasHash[0]<<24) | ((uint32_t)sasHash[1]<<16) | ((uint32_t)sasHash[2]<<8) | ((uint32_t)(sasHash[3]));
zrtpChannelContext->srtpSecrets.sasLength = zrtpChannelContext->sasLength;
zrtpChannelContext->srtpSecrets.sas = malloc(zrtpChannelContext->sasLength); /*this shall take in account the selected representation algo for SAS */
zrtpChannelContext->srtpSecrets.sas = (char *)malloc((zrtpChannelContext->sasLength)*sizeof(char)); /*this shall take in account the selected representation algo for SAS */
zrtpChannelContext->sasFunction(sasValue, zrtpChannelContext->srtpSecrets.sas);
}
......
......@@ -26,10 +26,13 @@
*/
#include <stdlib.h>
#include <string.h>
#include "zidCache.h"
#ifdef HAVE_LIBXML2
#include <libxml/tree.h>
#include <libxml/parser.h>
#include "typedef.h"
#include "zidCache.h"
#define MIN_VALID_CACHE_LENGTH 56 /* root tag + selfZID tag size */
#define XML_HEADER_STRING "<?xml version='1.0' encoding='utf-8'?>"
......@@ -284,9 +287,11 @@ int bzrtp_writePeerNode(bzrtpContext_t *context, uint8_t peerZID[12], uint8_t *t
/* write the cache file and unlock it(TODO)*/
bzrtp_writeCache(context, doc);
xmlFree(doc);
}
xmlFree(doc);
free(tagContentHex);
return 0;
}
......@@ -378,3 +383,40 @@ uint8_t bzrtp_byteToChar(uint8_t inputByte) {
/* a-f */
return inputByte + 0x57;
}
#else /* NOT HAVE_LIBXML2 */
int bzrtp_getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]) {
if (context == NULL) {
return ZRTP_ZIDCACHE_INVALID_CONTEXT;
}
/* we are running cacheless, return a random number */
bzrtpCrypto_getRandom(context->RNGContext, selfZID, 12);
return 0;
}
int bzrtp_getPeerAssociatedSecretsHash(bzrtpContext_t *context, uint8_t peerZID[12]) {
if (context == NULL) {
return ZRTP_ZIDCACHE_INVALID_CONTEXT;
}
/* resert cached secret buffer */
free(context->cachedSecret.rs1);
free(context->cachedSecret.rs2);
free(context->cachedSecret.pbxsecret);
free(context->cachedSecret.auxsecret);
context->cachedSecret.rs1 = NULL;
context->cachedSecret.rs1Length = 0;
context->cachedSecret.rs2 = NULL;
context->cachedSecret.rs2Length = 0;
context->cachedSecret.pbxsecret = NULL;
context->cachedSecret.pbxsecretLength = 0;
context->cachedSecret.auxsecret = NULL;
context->cachedSecret.auxsecretLength = 0;
context->cachedSecret.previouslyVerifiedSas = 0;
return 0;
}
int bzrtp_writePeerNode(bzrtpContext_t *context, uint8_t peerZID[12], uint8_t *tagName, uint8_t tagNameLength, uint8_t *tagContent, uint32_t tagContentLength) {
return 0;
}
#endif /* HAVE LIBXML2 */
......@@ -6,8 +6,8 @@ bzrtpTest_SOURCES=bzrtpTest.c \
bzrtpParserTest.c
AM_LDFLAGS= -lm
bzrtpTest_CFLAGS=$(CUNIT_CFLAGS)
bzrtpTest_LDFLAGS=$(CUNIT_LIBS) $(AM_LDFLAGS)
bzrtpTest_CFLAGS=$(CUNIT_CFLAGS) $(LIBXML2_CFLAGS)
bzrtpTest_LDFLAGS=$(CUNIT_LIBS) $(LIBXML2_LIBS) $(AM_LDFLAGS)
AM_CPPFLAGS=-I$(top_srcdir)/include
LDADD= $(top_builddir)/src/libbzrtp.la
......
......@@ -292,13 +292,15 @@ void test_dhm2048(void) {
bzrtpDHMContext_t *DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
/* Create the public value for Alice G^Xa mod P */
bzrtpCrypto_DHMCreatePublic(DHMaContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMCreatePublic(DHMaContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
/* Create the context for Bob */
bzrtpDHMContext_t *DHMbContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
/* Create the public value for Bob G^Xb mod P */
bzrtpCrypto_DHMCreatePublic(DHMbContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMCreatePublic(DHMbContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
printf("Context created %p and %p\n", DHMaContext, DHMbContext);
/* exchange public values */
DHMaContext->peer = (uint8_t *)malloc(DHMaContext->primeLength*sizeof(uint8_t));
......@@ -306,9 +308,10 @@ void test_dhm2048(void) {
memcpy (DHMaContext->peer, DHMbContext->self, DHMaContext->primeLength*sizeof(uint8_t));
memcpy (DHMbContext->peer, DHMaContext->self, DHMbContext->primeLength*sizeof(uint8_t));
printf("Call compute secret\n");
/* compute secret key */
bzrtpCrypto_DHMComputeSecret(DHMaContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMComputeSecret(DHMbContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMComputeSecret(DHMaContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMComputeSecret(DHMbContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
CU_ASSERT_TRUE(memcmp(DHMaContext->key, DHMbContext->key, 256) == 0); /* generated key shall be 256 bytes long */
......@@ -338,13 +341,13 @@ void test_dhm3072(void) {
bzrtpDHMContext_t *DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
/* Create the public value for Alice G^Xa mod P */
bzrtpCrypto_DHMCreatePublic(DHMaContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMCreatePublic(DHMaContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
/* Create the context for Bob */
bzrtpDHMContext_t *DHMbContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
/* Create the public value for Bob G^Xb mod P */
bzrtpCrypto_DHMCreatePublic(DHMbContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMCreatePublic(DHMbContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
/* exchange public values */
DHMaContext->peer = (uint8_t *)malloc(DHMaContext->primeLength*sizeof(uint8_t));
......@@ -353,8 +356,8 @@ void test_dhm3072(void) {
memcpy (DHMbContext->peer, DHMaContext->self, DHMbContext->primeLength*sizeof(uint8_t));
/* compute secret key */
bzrtpCrypto_DHMComputeSecret(DHMaContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMComputeSecret(DHMbContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMComputeSecret(DHMaContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
bzrtpCrypto_DHMComputeSecret(DHMbContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
CU_ASSERT_TRUE(memcmp(DHMaContext->key, DHMbContext->key, 384) == 0); /* generated key shall be 384 bytes long */
......@@ -491,6 +494,8 @@ void test_algoAgreement(void) {
} else {
CU_FAIL("Algo agreement test 2");
}
bzrtp_freeZrtpPacket(helloPacket);
bzrtp_destroyBzrtpContext(zrtpContext, 0x12345678);
}
......@@ -227,8 +227,10 @@ int floadAlice(void *clientData, uint8_t **output, uint32_t *size) {
fseek(ALICECACHE, 0L, SEEK_END); /* Position to end of file */
*size = ftell(ALICECACHE); /* Get file length */
rewind(ALICECACHE); /* Back to start of file */
*output = (uint8_t *)malloc(*size*sizeof(uint8_t));
*output = (uint8_t *)malloc(*size*sizeof(uint8_t)+1);
fread(*output, 1, *size, ALICECACHE);
*(*output+*size) = '\0';
*size += 1;
fclose(ALICECACHE);
return *size;
}
......@@ -253,8 +255,10 @@ int floadBob(void *clientData, uint8_t **output, uint32_t *size) {
fseek(BOBCACHE, 0L, SEEK_END); /* Position to end of file */
*size = ftell(BOBCACHE); /* Get file length */
rewind(BOBCACHE); /* Back to start of file */
*output = (uint8_t *)malloc(*size*sizeof(uint8_t));
*output = (uint8_t *)malloc(*size*sizeof(uint8_t)+1);
fread(*output, 1, *size, BOBCACHE);
*(*output+*size) = '\0';
*size += 1;
fclose(BOBCACHE);
return *size;
}
......@@ -533,6 +537,10 @@ void test_parserComplete() {
if (retval==0) {
contextBob->channelContext[0]->peerSequenceNumber = bob_HelloACKFromAlice->sequenceNumber;
}
bzrtp_freeZrtpPacket(alice_HelloACK);
bzrtp_freeZrtpPacket(bob_HelloACK);
bzrtp_freeZrtpPacket(alice_HelloACKFromBob);
bzrtp_freeZrtpPacket(bob_HelloACKFromAlice);
/* now build the commit message (both Alice and Bob will send it, then use the mechanism of rfc section 4.2 to determine who will be the initiator)*/
......@@ -578,6 +586,7 @@ void test_parserComplete() {
contextAlice->channelContext[0]->peerPackets[COMMIT_MESSAGE_STORE_ID] = alice_CommitFromBob;*/
}
packetDump(alice_CommitFromBob, 0);
bzrtp_freeZrtpPacket(alice_CommitFromBob);
/* Now determine who shall be the initiator : rfc section 4.2 */
/* select the one with the lowest value of hvi */
......@@ -655,8 +664,9 @@ void test_parserComplete() {
/* Now Alice shall check that the PV from bob is not 1 or Prime-1 TODO*/
/* Compute the shared DH secret */
contextAlice->DHMContext->peer = alice_DHPart1FromBob_message->pv;
bzrtpCrypto_DHMComputeSecret(contextAlice->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)contextAlice->RNGContext);
contextAlice->DHMContext->peer = (uint8_t *)malloc(contextAlice->channelContext[0]->keyAgreementLength*sizeof(uint8_t));
memcpy (contextAlice->DHMContext->peer, alice_DHPart1FromBob_message->pv, contextAlice->channelContext[0]->keyAgreementLength);
bzrtpCrypto_DHMComputeSecret(contextAlice->DHMContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)contextAlice->RNGContext);
/* So Alice send bob her DHPart2 message which is already prepared and stored (we just need to update the sequence number) */
bzrtp_packetUpdateSequenceNumber(contextAlice->channelContext[0]->selfPackets[DHPART_MESSAGE_STORE_ID], contextAlice->channelContext[0]->selfSequenceNumber);
......@@ -709,8 +719,9 @@ void test_parserComplete() {
/* Now Bob shall check that the PV from Alice is not 1 or Prime-1 TODO*/
/* Compute the shared DH secret */
contextBob->DHMContext->peer = bob_DHPart2FromAlice_message->pv;
bzrtpCrypto_DHMComputeSecret(contextBob->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)contextAlice->RNGContext);
contextBob->DHMContext->peer = (uint8_t *)malloc(contextBob->channelContext[0]->keyAgreementLength*sizeof(uint8_t));
memcpy (contextBob->DHMContext->peer, bob_DHPart2FromAlice_message->pv, contextBob->channelContext[0]->keyAgreementLength);
bzrtpCrypto_DHMComputeSecret(contextBob->DHMContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)contextAlice->RNGContext);
/* JUST FOR TEST: check that the generated secrets are the same */
......@@ -935,6 +946,7 @@ void test_parserComplete() {
contextBob->channelContext[0]->s0 = (uint8_t *)malloc(32*sizeof(uint8_t));
contextBob->channelContext[0]->hashFunction(dataToHash, totalHashDataLength, 32, contextBob->channelContext[0]->s0);
free(dataToHash);
/* destroy all cached keys in context */
if (contextBob->cachedSecret.rs1!=NULL) {
......@@ -1091,6 +1103,9 @@ void test_parserComplete() {
packetDump(bob_Confirm1,1);
packetDump(alice_Confirm1FromBob,0);
bzrtp_freeZrtpPacket(alice_Confirm1FromBob);
bzrtp_freeZrtpPacket(bob_Confirm1);
/* now Alice build the CONFIRM2 packet and send it to Bob */
bzrtpPacket_t *alice_Confirm2 = bzrtp_createZrtpPacket(contextAlice, contextAlice->channelContext[0], MSGTYPE_CONFIRM2, &retval);
retval += bzrtp_packetBuild(contextAlice, contextAlice->channelContext[0], alice_Confirm2, contextAlice->channelContext[0]->selfSequenceNumber);
......@@ -1115,6 +1130,9 @@ void test_parserComplete() {
packetDump(alice_Confirm2,1);
packetDump(bob_Confirm2FromAlice,0);
bzrtp_freeZrtpPacket(bob_Confirm2FromAlice);
bzrtp_freeZrtpPacket(alice_Confirm2);
/* Bob build the conf2Ack and send it to Alice */
bzrtpPacket_t *bob_Conf2ACK = bzrtp_createZrtpPacket(contextBob, contextBob->channelContext[0], MSGTYPE_CONF2ACK, &retval);
retval += bzrtp_packetBuild(contextBob, contextBob->channelContext[0], bob_Conf2ACK, contextBob->channelContext[0]->selfSequenceNumber);
......@@ -1133,6 +1151,9 @@ void test_parserComplete() {
contextAlice->isSecure = 1;
}
bzrtp_freeZrtpPacket(bob_Conf2ACK);
bzrtp_freeZrtpPacket(alice_Conf2ACKFromBob);
dumpContext("Alice", contextAlice);
dumpContext("Bob", contextBob);
......@@ -1395,6 +1416,8 @@ void test_parserComplete() {
}
packetDump(bob_Confirm1FromAlice,0);
bzrtp_freeZrtpPacket(bob_Confirm1FromAlice);
bzrtp_freeZrtpPacket(alice_Confirm1);
/* now Bob build the CONFIRM2 packet and send it to Alice */
bzrtpPacket_t *bob_Confirm2 = bzrtp_createZrtpPacket(contextBob, contextBob->channelContext[1], MSGTYPE_CONFIRM2, &retval);
......@@ -1415,6 +1438,8 @@ void test_parserComplete() {
}
packetDump(alice_Confirm2FromBob,0);
bzrtp_freeZrtpPacket(alice_Confirm2FromBob);
bzrtp_freeZrtpPacket(bob_Confirm2);
/* Alice build the conf2Ack and send it to Bob */
bzrtpPacket_t *alice_Conf2ACK = bzrtp_createZrtpPacket(contextAlice, contextAlice->channelContext[1], MSGTYPE_CONF2ACK, &retval);
......@@ -1433,6 +1458,9 @@ void test_parserComplete() {
}
bzrtp_freeZrtpPacket(alice_Conf2ACK);
bzrtp_freeZrtpPacket(bob_Conf2ACKFromAlice);
......@@ -1441,7 +1469,11 @@ void test_parserComplete() {
dumpContext("\nAlice", contextAlice);
dumpContext("\nBob", contextBob);
*/
printf("Destroy the contexts\n");
/* destroy the context */
bzrtp_destroyBzrtpContext(contextAlice, 0x45678901);
bzrtp_destroyBzrtpContext(contextBob, 0x54321098);
printf("Destroy the contexts last channel\n");
bzrtp_destroyBzrtpContext(contextBob, 0x87654321);
bzrtp_destroyBzrtpContext(contextAlice, 0x12345678);
......@@ -1481,6 +1513,7 @@ int bzrtp_sendData(void *clientData, uint8_t *packetString, uint16_t packetLengt
} else {
printf("Check says %04x\n", retval);
}
bzrtp_freeZrtpPacket(zrtpPacket);
/* put the message in the message queue */
if (contexts->nom[0] == 'A') { /* message sent by Alice, put it in Bob's queue */
......@@ -1708,5 +1741,13 @@ void test_stateMachine() {
printf("Destroy the contexts\n");
/* destroy the context */
bzrtp_destroyBzrtpContext(contextAlice, 0x34567890);
bzrtp_destroyBzrtpContext(contextBob, 0x09876543);
printf("Destroy the contexts last channel\n");
bzrtp_destroyBzrtpContext(contextBob, 0x87654321);
bzrtp_destroyBzrtpContext(contextAlice, 0x12345678);
}
......@@ -24,9 +24,13 @@
#include "CUnit/Basic.h"
#include "bzrtpCryptoTest.h"
#include "bzrtpParserTest.h"
#include <libxml/parser.h>
int main(int argc, char *argv[] ) {
xmlInitParser();
CU_pSuite cryptoWrapperTestSuite, cryptoUtilsTestSuite, parserTestSuite;
/* initialize the CUnit test registry */
......@@ -52,22 +56,25 @@ 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", test_parser);
CU_add_test(parserTestSuite, "Parse Exchange", test_parserComplete);
/* CU_add_test(parserTestSuite, "State machine", test_stateMachine);*/
CU_add_test(parserTestSuite, "State machine", test_stateMachine);
/* Run all suites */
printf("\n\n#### Run the Bzrtp Crypto Wrappers tests suite\n");
CU_basic_run_suite(cryptoWrapperTestSuite);
printf("\n\n#### Run the Bzrtp Crypto Utils tests suite\n");
CU_basic_run_suite(cryptoUtilsTestSuite);
printf("\n\n#### Run the Bzrtp ZRTP Packet Parser tests suite\n");
CU_basic_run_suite(parserTestSuite);
/* printf("\n\n#### Run the Bzrtp ZRTP Packet Parser tests suite\n");
CU_basic_run_suite(parserTestSuite);*/
/* cleanup the CUnit registry */
CU_cleanup_registry();
/* cleanup libxml2 */
xmlCleanupParser();
return 0;
}
......@@ -206,7 +206,7 @@ void dumpContext(char *title, bzrtpContext_t *zrtpContext) {
printHex(" s0", channelContext->s0, channelContext->hashLength);
}
if(channelContext->srtpSecrets.sas != NULL) {
printf(" sas : %s\n", channelContext->srtpSecrets.sas);
printf(" sas : %.4s\n", channelContext->srtpSecrets.sas);
}
if (channelContext->srtpSecrets.selfSrtpKey != NULL) {
printHex(" selfsrtp key", channelContext->srtpSecrets.selfSrtpKey, channelContext->srtpSecrets.selfSrtpKeyLength);
......
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