Commit 77f38ace authored by johan's avatar johan
Browse files

Fix memory/crypto wrapper related bugs

parent 6310c8a0
...@@ -113,7 +113,7 @@ bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t e ...@@ -113,7 +113,7 @@ bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t e
* @return 0 on success. * @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); ...@@ -137,9 +137,9 @@ int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context);
* *
*/ */
void bzrtpCrypto_hmacSha256(const uint8_t *key, void bzrtpCrypto_hmacSha256(const uint8_t *key,
uint8_t keyLength, size_t keyLength,
const uint8_t *input, const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hmacLength, uint8_t hmacLength,
uint8_t *output); uint8_t *output);
...@@ -152,7 +152,7 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key, ...@@ -152,7 +152,7 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key,
* *
*/ */
void bzrtpCrypto_sha256(const uint8_t *input, void bzrtpCrypto_sha256(const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hashLength, uint8_t hashLength,
uint8_t *output); uint8_t *output);
...@@ -167,9 +167,9 @@ void bzrtpCrypto_sha256(const uint8_t *input, ...@@ -167,9 +167,9 @@ void bzrtpCrypto_sha256(const uint8_t *input,
* *
*/ */
void bzrtpCrypto_hmacSha1(const uint8_t *key, void bzrtpCrypto_hmacSha1(const uint8_t *key,
uint8_t keyLength, size_t keyLength,
const uint8_t *input, const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hmacLength, uint8_t hmacLength,
uint8_t *output); uint8_t *output);
...@@ -187,7 +187,7 @@ void bzrtpCrypto_hmacSha1(const uint8_t *key, ...@@ -187,7 +187,7 @@ void bzrtpCrypto_hmacSha1(const uint8_t *key,
void bzrtpCrypto_aes128CfbEncrypt(const uint8_t *key, void bzrtpCrypto_aes128CfbEncrypt(const uint8_t *key,
const uint8_t *IV, const uint8_t *IV,
const uint8_t *input, const uint8_t *input,
uint16_t inputLength, size_t inputLength,
uint8_t *output); uint8_t *output);
/** /**
...@@ -204,7 +204,7 @@ void bzrtpCrypto_aes128CfbEncrypt(const uint8_t *key, ...@@ -204,7 +204,7 @@ void bzrtpCrypto_aes128CfbEncrypt(const uint8_t *key,
void bzrtpCrypto_aes128CfbDecrypt(const uint8_t *key, void bzrtpCrypto_aes128CfbDecrypt(const uint8_t *key,
const uint8_t *IV, const uint8_t *IV,
const uint8_t *input, const uint8_t *input,
uint16_t inputLength, size_t inputLength,
uint8_t *output); uint8_t *output);
/** /**
...@@ -245,7 +245,7 @@ bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretL ...@@ -245,7 +245,7 @@ bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretL
* @param[in] rngContext pointer to the rng context if neeeded * @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)( ...@@ -258,7 +258,7 @@ void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(
* @param[in] rngContext Pointer to the RNG function context * @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 { ...@@ -154,10 +154,10 @@ typedef struct bzrtpChannelContext_struct {
uint8_t sasLength; /**< lenght of the SAS depends on the algorithm agreed */ 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 */ /* 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 (*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, uint32_t inputLength, uint8_t hashLength, uint8_t *output); /**< function pointer to the agreed hash function */ 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, uint16_t inputLength, uint8_t *output); /**< function pointer to the agreed cipher block function, encryption mode */ 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, uint16_t inputLength, uint8_t *output); /**< function pointer to the agreed cipher block function, decryption 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 */ void (*sasFunction)(uint32_t sas, char output[4]); /**< function pointer to the agreed sas rendering function */
/* keys */ /* keys */
......
...@@ -170,6 +170,8 @@ void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC) { ...@@ -170,6 +170,8 @@ void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC) {
context->cachedSecret.auxsecret=NULL; context->cachedSecret.auxsecret=NULL;
context->ZRTPSess=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 */ /* destroy the RNG context at the end because it may be needed to destroy some keys */
bzrtpCrypto_destroyRNG(context->RNGContext); bzrtpCrypto_destroyRNG(context->RNGContext);
...@@ -626,8 +628,8 @@ void bzrtp_destroyChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContex ...@@ -626,8 +628,8 @@ void bzrtp_destroyChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContex
/* free the allocated buffers */ /* free the allocated buffers */
for (i=0; i<PACKET_STORAGE_CAPACITY; i++) { for (i=0; i<PACKET_STORAGE_CAPACITY; i++) {
free(zrtpChannelContext->selfPackets[i]); bzrtp_freeZrtpPacket(zrtpChannelContext->selfPackets[i]);
free(zrtpChannelContext->peerPackets[i]); bzrtp_freeZrtpPacket(zrtpChannelContext->peerPackets[i]);
zrtpChannelContext->selfPackets[i] = NULL; zrtpChannelContext->selfPackets[i] = NULL;
zrtpChannelContext->peerPackets[i] = NULL; zrtpChannelContext->peerPackets[i] = NULL;
} }
...@@ -644,4 +646,7 @@ void bzrtp_destroyChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContex ...@@ -644,4 +646,7 @@ void bzrtp_destroyChannelContext(bzrtpContext_t *zrtpContext, bzrtpChannelContex
free(zrtpChannelContext->srtpSecrets.peerSrtpKey); free(zrtpChannelContext->srtpSecrets.peerSrtpKey);
free(zrtpChannelContext->srtpSecrets.peerSrtpSalt); free(zrtpChannelContext->srtpSecrets.peerSrtpSalt);
free(zrtpChannelContext->srtpSecrets.sas); 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 ...@@ -108,10 +108,10 @@ bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t e
return context; 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 */ /* get polarssl context data */
polarsslRNGContext_t *polarsslContext = (polarsslRNGContext_t *)context->cryptoModuleData; 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) { int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context) {
...@@ -139,9 +139,9 @@ int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context) { ...@@ -139,9 +139,9 @@ int bzrtpCrypto_destroyRNG(bzrtpRNGContext_t *context) {
* *
*/ */
void bzrtpCrypto_hmacSha1(const uint8_t *key, void bzrtpCrypto_hmacSha1(const uint8_t *key,
uint8_t keyLength, size_t keyLength,
const uint8_t *input, const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hmacLength, uint8_t hmacLength,
uint8_t *output) uint8_t *output)
{ {
...@@ -206,7 +206,7 @@ bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretL ...@@ -206,7 +206,7 @@ bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretL
} }
/* generate the random secret and compute the public value */ /* 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 */ /* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData; dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
...@@ -220,17 +220,14 @@ void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)( ...@@ -220,17 +220,14 @@ void bzrtpCrypto_DHMCreatePublic(bzrtpDHMContext_t *context, int (*rngFunction)(
/* clean DHM context */ /* clean DHM context */
void bzrtpCrypto_DestroyDHMContext(bzrtpDHMContext_t *context) { void bzrtpCrypto_DestroyDHMContext(bzrtpDHMContext_t *context) {
if (context!= NULL) { 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->secret);
free(context->self); free(context->self);
free(context->key); free(context->key);
free(context->peer); free(context->peer);
dhm_free((dhm_context *)context->cryptoModuleData);
free(context->cryptoModuleData);
free(context); free(context);
} }
} }
...@@ -249,7 +246,7 @@ void bzrtpCrypto_DestroyDHMContext(bzrtpDHMContext_t *context) { ...@@ -249,7 +246,7 @@ void bzrtpCrypto_DestroyDHMContext(bzrtpDHMContext_t *context) {
void bzrtpCrypto_aes128CfbEncrypt(const uint8_t key[16], void bzrtpCrypto_aes128CfbEncrypt(const uint8_t key[16],
const uint8_t IV[16], const uint8_t IV[16],
const uint8_t *input, const uint8_t *input,
uint16_t inputLength, size_t inputLength,
uint8_t *output) uint8_t *output)
{ {
size_t iv_offset=0; /* is not used by us but needed and updated by polarssl */ 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], ...@@ -281,7 +278,7 @@ void bzrtpCrypto_aes128CfbEncrypt(const uint8_t key[16],
void bzrtpCrypto_aes128CfbDecrypt(const uint8_t key[16], void bzrtpCrypto_aes128CfbDecrypt(const uint8_t key[16],
const uint8_t IV[16], const uint8_t IV[16],
const uint8_t *input, const uint8_t *input,
uint16_t inputLength, size_t inputLength,
uint8_t *output) uint8_t *output)
{ {
size_t iv_offset=0; /* is not used by us but needed and updated by polarssl */ 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], ...@@ -317,7 +314,7 @@ void bzrtpCrypto_aes128CfbDecrypt(const uint8_t key[16],
* *
*/ */
void bzrtpCrypto_sha256(const uint8_t *input, void bzrtpCrypto_sha256(const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hashLength, uint8_t hashLength,
uint8_t *output) uint8_t *output)
{ {
...@@ -343,9 +340,9 @@ void bzrtpCrypto_sha256(const uint8_t *input, ...@@ -343,9 +340,9 @@ void bzrtpCrypto_sha256(const uint8_t *input,
* *
*/ */
void bzrtpCrypto_hmacSha256(const uint8_t *key, void bzrtpCrypto_hmacSha256(const uint8_t *key,
uint8_t keyLength, size_t keyLength,
const uint8_t *input, const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hmacLength, uint8_t hmacLength,
uint8_t *output) uint8_t *output)
{ {
...@@ -361,17 +358,14 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key, ...@@ -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 */ /* 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) { void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext) {
/* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
/* import the peer public value G^Y mod P in the polar ssl context */ /* 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 */ /* 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 */ 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) ...@@ -381,9 +375,6 @@ void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)
/* Hashs */ /* Hashs */
#include "polarssl/sha2.h" #include "polarssl/sha2.h"
/* Asymmetrics encryption */
#include "polarssl/dhm.h"
/* /*
* @brief SHA256 wrapper * @brief SHA256 wrapper
* @param[in] input Input data buffer * @param[in] input Input data buffer
...@@ -393,7 +384,7 @@ void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction) ...@@ -393,7 +384,7 @@ void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)
* *
*/ */
void bzrtpCrypto_sha256(const uint8_t *input, void bzrtpCrypto_sha256(const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hashLength, uint8_t hashLength,
uint8_t *output) uint8_t *output)
{ {
...@@ -419,9 +410,9 @@ void bzrtpCrypto_sha256(const uint8_t *input, ...@@ -419,9 +410,9 @@ void bzrtpCrypto_sha256(const uint8_t *input,
* *
*/ */
void bzrtpCrypto_hmacSha256(const uint8_t *key, void bzrtpCrypto_hmacSha256(const uint8_t *key,
uint8_t keyLength, size_t keyLength,
const uint8_t *input, const uint8_t *input,
uint32_t inputLength, size_t inputLength,
uint8_t hmacLength, uint8_t hmacLength,
uint8_t *output) uint8_t *output)
{ {
...@@ -437,17 +428,14 @@ void bzrtpCrypto_hmacSha256(const uint8_t *key, ...@@ -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 */ /* 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) { void bzrtpCrypto_DHMComputeSecret(bzrtpDHMContext_t *context, int (*rngFunction)(void *, uint8_t *, size_t), void *rngContext) {
/* get the polarssl context */
dhm_context *polarsslContext = (dhm_context *)context->cryptoModuleData;
/* import the peer public value G^Y mod P in the polar ssl context */ /* 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 */ /* 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 */ 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 */ #endif /* POLARSSL Version 1.2 */
...@@ -544,8 +544,9 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC ...@@ -544,8 +544,9 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
} }
/* get plain message */ /* get plain message */
uint8_t *confirmPlainMessage = (uint8_t *)malloc(cipherTextLength*sizeof(uint8_t)); uint8_t *confirmPlainMessageBuffer = (uint8_t *)malloc(cipherTextLength*sizeof(uint8_t));
zrtpChannelContext->cipherDecryptionFunction(confirmMessageKey, messageData->CFBIV, messageContent, cipherTextLength, confirmPlainMessage); zrtpChannelContext->cipherDecryptionFunction(confirmMessageKey, messageData->CFBIV, messageContent, cipherTextLength, confirmPlainMessageBuffer);
uint8_t *confirmPlainMessage = confirmPlainMessageBuffer; /* point into the allocated buffer */
/* parse it */ /* parse it */
memcpy(messageData->H0, confirmPlainMessage, 32); memcpy(messageData->H0, confirmPlainMessage, 32);
...@@ -654,6 +655,9 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC ...@@ -654,6 +655,9 @@ int bzrtp_packetParser(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
messageData->signatureBlock = NULL; messageData->signatureBlock = NULL;
} }
/* free plain buffer */
free(confirmPlainMessageBuffer);
/* attach the message structure to the packet one */ /* attach the message structure to the packet one */
zrtpPacket->messageData = (void *)messageData; zrtpPacket->messageData = (void *)messageData;
} }
...@@ -1200,8 +1204,9 @@ bzrtpPacket_t *bzrtp_createZrtpPacket(bzrtpContext_t *zrtpContext, bzrtpChannelC ...@@ -1200,8 +1204,9 @@ bzrtpPacket_t *bzrtp_createZrtpPacket(bzrtpContext_t *zrtpContext, bzrtpChannelC
} }
/* now compute the public value */ /* now compute the public value */
bzrtpCrypto_DHMCreatePublic(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, zrtpContext->RNGContext); bzrtpCrypto_DHMCreatePublic(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, zrtpContext->RNGContext);
zrtpDHPartMessage->pv = zrtpContext->DHMContext->self; /* pv just point to the value into the DHMContext */ 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 */ /* attach the message data to the packet */
zrtpPacket->messageData = zrtpDHPartMessage; zrtpPacket->messageData = zrtpDHPartMessage;
......
...@@ -534,7 +534,7 @@ int state_keyAgreement_sendingCommit(bzrtpEvent_t event) { ...@@ -534,7 +534,7 @@ int state_keyAgreement_sendingCommit(bzrtpEvent_t event) {
/* Compute the shared DH secret */ /* Compute the shared DH secret */
zrtpContext->DHMContext->peer = (uint8_t *)malloc(zrtpChannelContext->keyAgreementLength*sizeof(uint8_t)); zrtpContext->DHMContext->peer = (uint8_t *)malloc(zrtpChannelContext->keyAgreementLength*sizeof(uint8_t));
memcpy (zrtpContext->DHMContext->peer, dhPart1Message->pv, zrtpChannelContext->keyAgreementLength); 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 */ /* Derive the s0 key */
bzrtp_computeS0DHMMode(zrtpContext, zrtpChannelContext); bzrtp_computeS0DHMMode(zrtpContext, zrtpChannelContext);
...@@ -775,8 +775,9 @@ int state_keyAgreement_responderSendingDHPart1(bzrtpEvent_t event) { ...@@ -775,8 +775,9 @@ int state_keyAgreement_responderSendingDHPart1(bzrtpEvent_t event) {
zrtpChannelContext->peerPackets[DHPART_MESSAGE_STORE_ID] = zrtpPacket; zrtpChannelContext->peerPackets[DHPART_MESSAGE_STORE_ID] = zrtpPacket;
/* Compute the shared DH secret */ /* Compute the shared DH secret */
zrtpContext->DHMContext->peer = dhPart2Message->pv; zrtpContext->DHMContext->peer = (uint8_t *)malloc(zrtpChannelContext->keyAgreementLength*sizeof(uint8_t));
bzrtpCrypto_DHMComputeSecret(zrtpContext->DHMContext, (int (*)(void *, uint8_t *, uint16_t))bzrtpCrypto_getRandom, (void *)zrtpContext->RNGContext); 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 */ /* Derive the s0 key */
bzrtp_computeS0DHMMode(zrtpContext, zrtpChannelContext); bzrtp_computeS0DHMMode(zrtpContext, zrtpChannelContext);
...@@ -2012,7 +2013,7 @@ int bzrtp_deriveSrtpKeysFromS0(bzrtpContext_t *zrtpContext, bzrtpChannelContext_ ...@@ -2012,7 +2013,7 @@ int bzrtp_deriveSrtpKeysFromS0(bzrtpContext_t *zrtpContext, bzrtpChannelContext_
/* now get it into a char according to the selected algo */ /* 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])); 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.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); zrtpChannelContext->sasFunction(sasValue, zrtpChannelContext->srtpSecrets.sas);
} }
......
...@@ -26,10 +26,13 @@ ...@@ -26,10 +26,13 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "zidCache.h"
#ifdef HAVE_LIBXML2
#include <libxml/tree.h> #include <libxml/tree.h>
#include <libxml/parser.h> #include <libxml/parser.h>
#include "typedef.h" #include "typedef.h"
#include "zidCache.h"
#define MIN_VALID_CACHE_LENGTH 56 /* root tag + selfZID tag size */ #define MIN_VALID_CACHE_LENGTH 56 /* root tag + selfZID tag size */
#define XML_HEADER_STRING "<?xml version='1.0' encoding='utf-8'?>" #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 ...@@ -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)*/ /* write the cache file and unlock it(TODO)*/
bzrtp_writeCache(context, doc); bzrtp_writeCache(context, doc);
xmlFree(doc);
} }
xmlFree(doc);
free(tagContentHex);
return 0; return 0;
} }
...@@ -378,3 +383,40 @@ uint8_t bzrtp_byteToChar(uint8_t inputByte) { ...@@ -378,3 +383,40 @@ uint8_t bzrtp_byteToChar(uint8_t inputByte) {
/* a-f */ /* a-f */
return inputByte + 0x57; 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 \ ...@@ -6,8 +6,8 @@ bzrtpTest_SOURCES=bzrtpTest.c \
bzrtpParserTest.c bzrtpParserTest.c
AM_LDFLAGS= -lm AM_LDFLAGS= -lm
bzrtpTest_CFLAGS=$(CUNIT_CFLAGS) bzrtpTest_CFLAGS=$(CUNIT_CFLAGS) $(LIBXML2_CFLAGS)
bzrtpTest_LDFLAGS=$(CUNIT_LIBS) $(AM_LDFLAGS) bzrtpTest_LDFLAGS=$(CUNIT_LIBS) $(LIBXML2_LIBS) $(AM_LDFLAGS)
AM_CPPFLAGS=-I$(top_srcdir)/include AM_CPPFLAGS=-I$(top_srcdir)/include
LDADD= $(top_builddir)/src/libbzrtp.la LDADD= $(top_builddir)/src/libbzrtp.la
......
...@@ -292,13 +292,15 @@ void test_dhm2048(void) { ...@@ -292,13 +292,15 @@ void test_dhm2048(void) {
bzrtpDHMContext_t *DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32); bzrtpDHMContext_t *DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
/* Create the public value for Alice G^Xa mod P */ /* 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);