Commit 6c59f37d authored by Ghislain MARY's avatar Ghislain MARY

Fix compilation with Visual Studio.

parent 84de5850
......@@ -23,8 +23,15 @@
*/
#ifndef BZRTP_H
#define BZRTP_H
#include <stdint.h>
#ifdef _MSC_VER
#define BZRTP_EXPORT __declspec(dllexport)
#else
#define BZRTP_EXPORT __attribute__ ((visibility ("default")))
#endif
/**
* Some defines used internally by zrtp but also needed by client to interpretate the cipher block and auth tag algorithms used by srtp */
#define ZRTP_UNSET_ALGO 0x00
......@@ -92,7 +99,7 @@ typedef struct bzrtpContext_struct bzrtpContext_t;
* @return The ZRTP engine context data
*
*/
__attribute__ ((visibility ("default"))) bzrtpContext_t *bzrtp_createBzrtpContext(uint32_t selfSSRC);
BZRTP_EXPORT bzrtpContext_t *bzrtp_createBzrtpContext(uint32_t selfSSRC);
/**
* @brief Perform some initialisation which can't be done without some callback functions:
......@@ -100,7 +107,7 @@ __attribute__ ((visibility ("default"))) bzrtpContext_t *bzrtp_createBzrtpContex
*
* @param[in] context The context to initialise
*/
__attribute__ ((visibility ("default"))) void bzrtp_initBzrtpContext(bzrtpContext_t *context);
BZRTP_EXPORT void bzrtp_initBzrtpContext(bzrtpContext_t *context);
/**
* Free memory of context structure to a channel, if all channels are freed, free the global zrtp context
......@@ -108,7 +115,7 @@ __attribute__ ((visibility ("default"))) void bzrtp_initBzrtpContext(bzrtpContex
* @param[in] selfSSRC The SSRC identifying the channel to be destroyed
*
*/
__attribute__ ((visibility ("default"))) void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC);
BZRTP_EXPORT void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC);
#define ZRTP_CALLBACK_LOADCACHE 0x0101
#define ZRTP_CALLBACK_WRITECACHE 0x0102
......@@ -125,7 +132,7 @@ __attribute__ ((visibility ("default"))) void bzrtp_destroyBzrtpContext(bzrtpCon
* @return 0 on success
*
*/
__attribute__ ((visibility ("default"))) int bzrtp_setCallback(bzrtpContext_t *context, int (*functionPointer)(), uint16_t functionID);
BZRTP_EXPORT int bzrtp_setCallback(bzrtpContext_t *context, int (*functionPointer)(), uint16_t functionID);
/**
* @brief Set the client data pointer in a channel context
......@@ -137,7 +144,7 @@ __attribute__ ((visibility ("default"))) int bzrtp_setCallback(bzrtpContext_t *c
* @return 0 on success
*
*/
__attribute__ ((visibility ("default"))) int bzrtp_setClientData(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, void *clientData);
BZRTP_EXPORT int bzrtp_setClientData(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, void *clientData);
/**
* @brief Add a channel to an existing context, this can be done only if the first channel has concluded a DH key agreement
......@@ -147,7 +154,7 @@ __attribute__ ((visibility ("default"))) int bzrtp_setClientData(bzrtpContext_t
*
* @return 0 on succes, error code otherwise
*/
__attribute__ ((visibility ("default"))) int bzrtp_addChannel(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
BZRTP_EXPORT int bzrtp_addChannel(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
/**
......@@ -158,7 +165,7 @@ __attribute__ ((visibility ("default"))) int bzrtp_addChannel(bzrtpContext_t *zr
*
* @return 0 on succes, error code otherwise
*/
__attribute__ ((visibility ("default"))) int bzrtp_startChannelEngine(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
BZRTP_EXPORT int bzrtp_startChannelEngine(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
/**
* @brief Send the current time to a specified channel, it will check if it has to trig some timer
......@@ -169,7 +176,7 @@ __attribute__ ((visibility ("default"))) int bzrtp_startChannelEngine(bzrtpConte
*
* @return 0 on succes, error code otherwise
*/
__attribute__ ((visibility ("default"))) int bzrtp_iterate(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint64_t timeReference);
BZRTP_EXPORT int bzrtp_iterate(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint64_t timeReference);
/**
......@@ -180,7 +187,7 @@ __attribute__ ((visibility ("default"))) int bzrtp_iterate(bzrtpContext_t *zrtpC
*
* @return 0 if this channel is not ready to secure SRTP communication, 1 if it is ready
*/
__attribute__ ((visibility ("default"))) int bzrtp_isSecure(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
BZRTP_EXPORT int bzrtp_isSecure(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
/**
......@@ -193,21 +200,21 @@ __attribute__ ((visibility ("default"))) int bzrtp_isSecure(bzrtpContext_t *zrtp
*
* @return 0 on success, errorcode otherwise
*/
__attribute__ ((visibility ("default"))) int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t *zrtpPacketString, uint16_t zrtpPacketStringLength);
BZRTP_EXPORT int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t *zrtpPacketString, uint16_t zrtpPacketStringLength);
/**
* @brief Called by user when the SAS has been verified
*
* @param[in/out] zrtpContext The ZRTP context we're dealing with
*/
__attribute__ ((visibility ("default"))) void bzrtp_SASVerified(bzrtpContext_t *zrtpContext);
BZRTP_EXPORT void bzrtp_SASVerified(bzrtpContext_t *zrtpContext);
/**
* @brief Called by user when the SAS has been set to unverified
*
* @param[in/out] zrtpContext The ZRTP context we're dealing with
*/
__attribute__ ((visibility ("default"))) void bzrtp_resetSASVerified(bzrtpContext_t *zrtpContext);
BZRTP_EXPORT void bzrtp_resetSASVerified(bzrtpContext_t *zrtpContext);
/**
* @brief Reset the retransmission timer of a given channel.
......@@ -220,7 +227,7 @@ __attribute__ ((visibility ("default"))) void bzrtp_resetSASVerified(bzrtpContex
*
* return 0 on success, error code otherwise
*/
__attribute__ ((visibility ("default"))) int bzrtp_resetRetransmissionTimer(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
BZRTP_EXPORT int bzrtp_resetRetransmissionTimer(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
#define BZRTP_CUSTOMCACHE_USEKDF 1
#define BZRTP_CUSTOMCACHE_PLAINDATA 0
......@@ -251,6 +258,6 @@ __attribute__ ((visibility ("default"))) int bzrtp_resetRetransmissionTimer(bzrt
*
* @return 0 on success, errorcode otherwise
*/
__attribute__ ((visibility ("default"))) int bzrtp_addCustomDataInCache(bzrtpContext_t *zrtpContext, uint8_t peerZID[12], uint8_t *tagName, uint16_t tagNameLength, uint8_t *tagContent, uint16_t tagContentLength, uint8_t derivedDataLength, uint8_t useKDF, uint8_t fileFlag);
BZRTP_EXPORT int bzrtp_addCustomDataInCache(bzrtpContext_t *zrtpContext, uint8_t peerZID[12], uint8_t *tagName, uint16_t tagNameLength, uint8_t *tagContent, uint16_t tagContentLength, uint8_t derivedDataLength, uint8_t useKDF, uint8_t fileFlag);
#endif /* ifndef BZRTP_H */
......@@ -131,13 +131,14 @@ void bzrtp_initBzrtpContext(bzrtpContext_t *context) {
*
*/
void bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC) {
int i;
int validChannelsNumber = 0;
if (context == NULL) {
return;
}
int i;
/* Find the channel to be destroyed, destroy it and check if we have anymore valid channels */
int validChannelsNumber = 0;
for (i=0; i<ZRTP_MAX_CHANNEL_NUMBER; i++) {
if (context->channelContext[i] != NULL) {
if (context->channelContext[i]->selfSSRC == selfSSRC) {
......@@ -221,6 +222,9 @@ int bzrtp_setCallback(bzrtpContext_t *context, int (*functionPointer)(), uint16_
int bzrtp_addChannel(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
bzrtpChannelContext_t *zrtpChannelContext = NULL;
int i=0;
/* is zrtp context valid */
if (zrtpContext==NULL) {
return BZRTP_ERROR_INVALIDCONTEXT;
......@@ -237,9 +241,6 @@ int bzrtp_addChannel(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
}
/* get the first free channel context from ZRTP context and create a channel context */
bzrtpChannelContext_t *zrtpChannelContext = NULL;
int i=0;
while(i<ZRTP_MAX_CHANNEL_NUMBER && zrtpChannelContext==NULL) {
if (zrtpContext->channelContext[i] == NULL) {
int retval;
......@@ -276,6 +277,7 @@ int bzrtp_addChannel(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
*/
int bzrtp_startChannelEngine(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
bzrtpEvent_t initEvent;
/* get channel context */
bzrtpChannelContext_t *zrtpChannelContext = getChannelContext(zrtpContext, selfSSRC);
......@@ -291,7 +293,6 @@ int bzrtp_startChannelEngine(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
zrtpChannelContext->stateMachine = state_discovery_init;
/* create an INIT event to call the init state function which will create a hello packet and start sending it */
bzrtpEvent_t initEvent;
initEvent.eventType = BZRTP_EVENT_INIT;
initEvent.bzrtpPacketString = NULL;
initEvent.bzrtpPacketStringLength = 0;
......@@ -324,11 +325,11 @@ int bzrtp_iterate(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint64_t timeR
if (zrtpChannelContext->timer.status == BZRTP_TIMER_ON) {
if (zrtpChannelContext->timer.firingTime<=timeReference) { /* we must trig the timer */
bzrtpEvent_t timerEvent;
zrtpChannelContext->timer.firingCount++;
/* create a timer event */
bzrtpEvent_t timerEvent;
timerEvent.eventType = BZRTP_EVENT_TIMER;
timerEvent.bzrtpPacketString = NULL;
timerEvent.bzrtpPacketStringLength = 0;
......@@ -380,6 +381,10 @@ int bzrtp_setClientData(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, void *cl
* @return 0 on success, errorcode otherwise
*/
int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t *zrtpPacketString, uint16_t zrtpPacketStringLength) {
int retval;
bzrtpPacket_t *zrtpPacket;
bzrtpEvent_t event;
/* get channel context */
bzrtpChannelContext_t *zrtpChannelContext = getChannelContext(zrtpContext, selfSSRC);
......@@ -393,8 +398,7 @@ int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t
}
/* first check the packet */
int retval;
bzrtpPacket_t *zrtpPacket = bzrtp_packetCheck(zrtpPacketString, zrtpPacketStringLength, zrtpChannelContext->peerSequenceNumber, &retval);
zrtpPacket = bzrtp_packetCheck(zrtpPacketString, zrtpPacketStringLength, zrtpChannelContext->peerSequenceNumber, &retval);
if (retval != 0) {
/*TODO: check the returned error code and do something or silent drop? */
return retval;
......@@ -403,11 +407,12 @@ int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t
/* TODO: Intercept error and ping zrtp packets */
/* if we have a ping packet, just answer with a ping ACK and do not forward to the state machine */
if (zrtpPacket->messageType == MSGTYPE_PING) {
bzrtpPacket_t *pingAckPacket = NULL;
bzrtp_packetParser(zrtpContext, zrtpChannelContext, zrtpPacketString, zrtpPacketStringLength, zrtpPacket);
/* store ping packet in the channel context as packet creator will need it to create the pingACK */
zrtpChannelContext->pingPacket = zrtpPacket;
/* create the pingAck packet */
bzrtpPacket_t *pingAckPacket = NULL;
pingAckPacket = bzrtp_createZrtpPacket(zrtpContext, zrtpChannelContext, MSGTYPE_PINGACK, &retval);
if (retval == 0) {
retval = bzrtp_packetBuild(zrtpContext, zrtpChannelContext, pingAckPacket, zrtpChannelContext->selfSequenceNumber);
......@@ -426,7 +431,6 @@ int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t
}
/* build a packet event of it and send it to the state machine */
bzrtpEvent_t event;
event.eventType = BZRTP_EVENT_MESSAGE;
event.bzrtpPacketString = zrtpPacketString;
event.bzrtpPacketStringLength = zrtpPacketStringLength;
......@@ -467,12 +471,13 @@ int bzrtp_isSecure(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
*/
void bzrtp_SASVerified(bzrtpContext_t *zrtpContext) {
if (zrtpContext != NULL) {
uint8_t pvsFlag = 1;
/* check if we must update the cache(delayed until sas verified in case of cache mismatch) */
if (zrtpContext->cacheMismatchFlag == 1) {
zrtpContext->cacheMismatchFlag = 0;
bzrtp_updateCachedSecrets(zrtpContext, zrtpContext->channelContext[0]); /* channel[0] is the only one in DHM mode, so the only one able to have a cache mismatch */
}
uint8_t pvsFlag = 1;
bzrtp_writePeerNode(zrtpContext, zrtpContext->peerZID, (uint8_t *)"pvs", 3, &pvsFlag, 1, BZRTP_CACHE_TAGISBYTE|BZRTP_CACHE_NOMULTIPLETAGS, BZRTP_CACHE_LOADFILE|BZRTP_CACHE_WRITEFILE);
}
}
......@@ -517,6 +522,7 @@ int bzrtp_addCustomDataInCache(bzrtpContext_t *zrtpContext, uint8_t peerZID[12],
if (useKDF == BZRTP_CUSTOMCACHE_PLAINDATA) { /* write content as provided : content is a string and multiple tag is allowed as we are writing the peer URI(To be modified if needed) */
return bzrtp_writePeerNode(zrtpContext, peerZID, tagName, tagNameLength, tagContent, tagContentLength, BZRTP_CACHE_TAGISSTRING|BZRTP_CACHE_ALLOWMULTIPLETAGS, fileFlag);
} else { /* we must derive the content using the key derivation function */
uint8_t derivedContent[32];
/* check we have s0 and KDFContext in channel[0] */
bzrtpChannelContext_t *zrtpChannelContext = zrtpContext->channelContext[0];
......@@ -524,7 +530,6 @@ int bzrtp_addCustomDataInCache(bzrtpContext_t *zrtpContext, uint8_t peerZID[12],
return BZRTP_ERROR_INVALIDCONTEXT;
}
/* We derive a maximum of 32 bytes for a 256 bit key */
uint8_t derivedContent[32];
if (derivedDataLength>32) {
derivedDataLength = 32;
}
......@@ -585,11 +590,12 @@ int bzrtp_resetRetransmissionTimer(bzrtpContext_t *zrtpContext, uint32_t selfSSR
* @return a pointer to the channel context, NULL if the context is invalid or channel not found
*/
bzrtpChannelContext_t *getChannelContext(bzrtpContext_t *zrtpContext, uint32_t selfSSRC) {
int i;
if (zrtpContext==NULL) {
return NULL;
}
int i;
for (i=0; i<ZRTP_MAX_CHANNEL_NUMBER; i++) {
if (zrtpContext->channelContext[i]!=NULL) {
if (zrtpContext->channelContext[i]->selfSSRC == selfSSRC) {
......
......@@ -91,6 +91,8 @@ typedef struct polarsslRNGContext_struct {
} polarsslRNGContext_t;
bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t entropyStringLength) {
bzrtpRNGContext_t *context;
/* create the polarssl context, it contains entropy and rng contexts */
polarsslRNGContext_t *polarsslContext = (polarsslRNGContext_t *)malloc(sizeof(polarsslRNGContext_t));
......@@ -101,7 +103,7 @@ bzrtpRNGContext_t *bzrtpCrypto_startRNG(const uint8_t *entropyString, uint16_t e
}
/* create the context and attach the polarssl one in it's unique field */
bzrtpRNGContext_t *context = (bzrtpRNGContext_t *)malloc(sizeof(bzrtpRNGContext_t));
context = (bzrtpRNGContext_t *)malloc(sizeof(bzrtpRNGContext_t));
context->cryptoModuleData = (void *)polarsslContext;
......@@ -159,12 +161,14 @@ void bzrtpCrypto_hmacSha1(const uint8_t *key,
/* initialise de DHM context according to requested algorithm */
bzrtpDHMContext_t *bzrtpCrypto_CreateDHMContext(uint8_t DHMAlgo, uint8_t secretLength)
{
dhm_context *polarsslDhmContext;
/* create the context */
bzrtpDHMContext_t *context = (bzrtpDHMContext_t *)malloc(sizeof(bzrtpDHMContext_t));
memset (context, 0, sizeof(bzrtpDHMContext_t));
/* create the polarssl context for DHM */
dhm_context *polarsslDhmContext=(dhm_context *)malloc(sizeof(dhm_context));
polarsslDhmContext=(dhm_context *)malloc(sizeof(dhm_context));
memset(polarsslDhmContext, 0, sizeof(dhm_context));
context->cryptoModuleData=(void *)polarsslDhmContext;
......@@ -249,12 +253,12 @@ void bzrtpCrypto_aes128CfbEncrypt(const uint8_t key[16],
size_t inputLength,
uint8_t *output)
{
uint8_t IVbuffer[16];
size_t iv_offset=0; /* is not used by us but needed and updated by polarssl */
aes_context context;
memset (&context, 0, sizeof(aes_context));
/* make a local copy of IV which is modified by the polar ssl AES-CFB function */
uint8_t IVbuffer[16];
memcpy(IVbuffer, IV, 16*sizeof(uint8_t));
/* initialise the aes context and key */
......@@ -281,12 +285,12 @@ void bzrtpCrypto_aes128CfbDecrypt(const uint8_t key[16],
size_t inputLength,
uint8_t *output)
{
uint8_t IVbuffer[16];
size_t iv_offset=0; /* is not used by us but needed and updated by polarssl */
aes_context context;
memset (&context, 0, sizeof(aes_context));
/* make a local copy of IV which is modified by the polar ssl AES-CFB function */
uint8_t IVbuffer[16];
memcpy(IVbuffer, IV, 16*sizeof(uint8_t));
/* initialise the aes context and key - use the aes_setkey_enc function as requested by the documentation of aes_crypt_cfb128 function */
......@@ -429,11 +433,12 @@ 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 *, size_t), void *rngContext) {
size_t keyLength;
/* import the peer public value G^Y mod P in the polar ssl context */
dhm_read_public((dhm_context *)(context->cryptoModuleData), context->peer, context->primeLength);
/* compute the secret key */
size_t keyLength= context->primeLength; /* undocumented but this value seems to be in/out, so we must set it to the expected key length */
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((dhm_context *)(context->cryptoModuleData), context->key, &keyLength);
}
......
......@@ -207,6 +207,18 @@ uint32_t bzrtp_CRC32(uint8_t *input, uint16_t length) {
*
*/
int crypoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpChannelContext, bzrtpHelloMessage_t *peerHelloMessage) {
uint8_t selfCommonKeyAgreementType[7];
uint8_t peerCommonKeyAgreementType[7];
uint8_t commonKeyAgreementTypeNumber = 0;
uint8_t commonCipherType[7];
uint8_t commonCipherTypeNumber;
uint8_t commonHashType[7];
uint8_t commonHashTypeNumber;
uint8_t commonAuthTagType[7];
uint8_t commonAuthTagTypeNumber;
uint8_t commonSasType[7];
uint8_t commonSasTypeNumber;
/* check context and Message */
if (zrtpContext == NULL) {
return ZRTP_CRYPTOAGREEMENT_INVALIDCONTEXT;
......@@ -225,10 +237,6 @@ int crypoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
}
/* now check what is in common in self and peer order */
uint8_t selfCommonKeyAgreementType[7];
uint8_t peerCommonKeyAgreementType[7];
uint8_t commonKeyAgreementTypeNumber = 0;
/* self ordering: get the common list in the self order of preference */
commonKeyAgreementTypeNumber = selectCommonAlgo(zrtpContext->supportedKeyAgreement, zrtpContext->kc, peerHelloMessage->supportedKeyAgreement, peerHelloMessage->kc, selfCommonKeyAgreementType);
......@@ -257,17 +265,17 @@ int crypoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
/*** Cipher block algorithm ***/
/* get the self cipher types availables */
uint8_t commonCipherType[7];
uint8_t commonCipherTypeNumber = selectCommonAlgo(zrtpContext->supportedCipher, zrtpContext->cc, peerHelloMessage->supportedCipher, peerHelloMessage->cc, commonCipherType);
commonCipherTypeNumber = selectCommonAlgo(zrtpContext->supportedCipher, zrtpContext->cc, peerHelloMessage->supportedCipher, peerHelloMessage->cc, commonCipherType);
if (commonCipherTypeNumber == 0) {/* This shall never happend but... */
return ZRTP_CRYPTOAGREEMENT_INVALIDCIPHER;
}
/* rfc section 5.1.5 specifies that if EC38 is choosen we SHOULD use AES256 or AES192 */
if (zrtpChannelContext->keyAgreementAlgo == ZRTP_KEYAGREEMENT_EC38) {
int i=0;
zrtpChannelContext->cipherAlgo = ZRTP_UNSET_ALGO;
/* is AES3 available */
int i=0;
while (i<commonCipherTypeNumber && zrtpChannelContext->cipherAlgo == ZRTP_UNSET_ALGO) {
if (commonCipherType[i] == ZRTP_CIPHER_AES3) {
zrtpChannelContext->cipherAlgo = ZRTP_CIPHER_AES3;
......@@ -293,17 +301,17 @@ int crypoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
/*** Hash algorithm ***/
/* get the self hash types availables */
uint8_t commonHashType[7];
uint8_t commonHashTypeNumber = selectCommonAlgo(zrtpContext->supportedHash, zrtpContext->hc, peerHelloMessage->supportedHash, peerHelloMessage->hc, commonHashType);
commonHashTypeNumber = selectCommonAlgo(zrtpContext->supportedHash, zrtpContext->hc, peerHelloMessage->supportedHash, peerHelloMessage->hc, commonHashType);
if (commonHashTypeNumber == 0) {/* This shall never happend but... */
return ZRTP_CRYPTOAGREEMENT_INVALIDHASH;
}
/* rfc section 5.1.5 specifies that if EC38 is choosen we SHOULD use SHA384 */
if (zrtpChannelContext->keyAgreementAlgo == ZRTP_KEYAGREEMENT_EC38) {
int i=0;
zrtpChannelContext->hashAlgo = ZRTP_UNSET_ALGO;
/* is S384 available */
int i=0;
while (i<commonHashTypeNumber && zrtpChannelContext->hashAlgo == ZRTP_UNSET_ALGO) {
if (commonHashType[i] == ZRTP_HASH_S384) {
zrtpChannelContext->hashAlgo = ZRTP_HASH_S384;
......@@ -319,8 +327,7 @@ int crypoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
/*** Authentication Tag algorithm ***/
/* get the self authentication tag types availables */
uint8_t commonAuthTagType[7];
uint8_t commonAuthTagTypeNumber = selectCommonAlgo(zrtpContext->supportedAuthTag, zrtpContext->ac, peerHelloMessage->supportedAuthTag, peerHelloMessage->ac, commonAuthTagType);
commonAuthTagTypeNumber = selectCommonAlgo(zrtpContext->supportedAuthTag, zrtpContext->ac, peerHelloMessage->supportedAuthTag, peerHelloMessage->ac, commonAuthTagType);
if (commonAuthTagTypeNumber == 0) {/* This shall never happend but... */
return ZRTP_CRYPTOAGREEMENT_INVALIDAUTHTAG;
}
......@@ -328,8 +335,7 @@ int crypoAlgoAgreement(bzrtpContext_t *zrtpContext, bzrtpChannelContext_t *zrtpC
/*** Sas algorithm ***/
/* get the self Sas rendering types availables */
uint8_t commonSasType[7];
uint8_t commonSasTypeNumber = selectCommonAlgo(zrtpContext->supportedSas, zrtpContext->sc, peerHelloMessage->supportedSas, peerHelloMessage->sc, commonSasType);
commonSasTypeNumber = selectCommonAlgo(zrtpContext->supportedSas, zrtpContext->sc, peerHelloMessage->supportedSas, peerHelloMessage->sc, commonSasType);
if (commonSasTypeNumber == 0) {/* This shall never happend but... */
return ZRTP_CRYPTOAGREEMENT_INVALIDSAS;
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -46,6 +46,8 @@ uint8_t bzrtp_charToByte(uint8_t inputChar);
void bzrtp_writeCache(bzrtpContext_t *zrtpContext);
int bzrtp_getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]) {
uint8_t *selfZidHex = NULL;
if (context == NULL) {
return ZRTP_ZIDCACHE_INVALID_CONTEXT;
}
......@@ -62,7 +64,6 @@ int bzrtp_getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]) {
return 0;
}
uint8_t *selfZidHex = NULL;
if (context->cacheBuffer != NULL ) { /* there is a cache, try to find our ZID */
xmlNodePtr cur = xmlDocGetRootElement(context->cacheBuffer);
/* if we found a root element, parse its children node */
......@@ -84,17 +85,19 @@ int bzrtp_getSelfZID(bzrtpContext_t *context, uint8_t selfZID[12]) {
/* if we didn't found anything in cache, or we have no cache at all: generate ZID, cache string and write it to file */
if (selfZidHex==NULL) {
uint8_t newZidHex[25];
xmlNodePtr rootNode;
/* generate a random ZID */
bzrtpCrypto_getRandom(context->RNGContext, selfZID, 12);
/* convert it to an Hexa String */
uint8_t newZidHex[25];
bzrtp_int8ToStr(newZidHex, selfZID, 12);
newZidHex[24] = '\0'; /* the string must be null terminated for libxml2 to add it correctly in the element */
xmlFree(context->cacheBuffer);
/* Create a new xml doc */
context->cacheBuffer = xmlNewDoc((const xmlChar *)"1.0");
/* root tag is "cache" */
xmlNodePtr rootNode = xmlNewDocNode(context->cacheBuffer, NULL, (const xmlChar *)"cache", NULL);
rootNode = xmlNewDocNode(context->cacheBuffer, NULL, (const xmlChar *)"cache", NULL);
xmlDocSetRootElement(context->cacheBuffer, rootNode);
/* add the ZID child */
xmlNewTextChild(rootNode, NULL, (const xmlChar *)"selfZID", newZidHex);
......@@ -139,9 +142,11 @@ int bzrtp_getPeerAssociatedSecretsHash(bzrtpContext_t *context, uint8_t peerZID[
/* parse the cache to find the peer element matching the given ZID */
if (context->cacheBuffer != NULL ) { /* there is a cache, try to find our peer element */
uint8_t peerZidHex[24];
xmlNodePtr cur;
bzrtp_int8ToStr(peerZidHex, peerZID, 12); /* compute the peerZID as an Hexa string */
xmlNodePtr cur = xmlDocGetRootElement(context->cacheBuffer);
cur = xmlDocGetRootElement(context->cacheBuffer);
/* if we found a root element, parse its children node */
if (cur!=NULL)
{
......@@ -221,11 +226,12 @@ int bzrtp_getPeerAssociatedSecretsHash(bzrtpContext_t *context, uint8_t peerZID[
* return 0 on success, error code otherwise
*/
int bzrtp_writePeerNode(bzrtpContext_t *context, uint8_t peerZID[12], uint8_t *tagName, uint8_t tagNameLength, uint8_t *tagContent, uint32_t tagContentLength, uint8_t nodeFlag, uint8_t fileFlag) {
uint8_t *tagContentHex; /* this one will store the actual value to be written in cache */
if ((context == NULL) || (context->zrtpCallbacks.bzrtp_loadCache == NULL)) {
return ZRTP_ZIDCACHE_INVALID_CONTEXT;
}
uint8_t *tagContentHex; /* this one will store the actual value to be written in cache */
if ((nodeFlag&BZRTP_CACHE_ISSTRINGBIT) == BZRTP_CACHE_TAGISBYTE) { /* tag content is a byte buffer, convert it to hexa string */
/* turn the tagContent to an hexa string null terminated */
tagContentHex = (uint8_t *)malloc(2*tagContentLength+1);
......@@ -237,11 +243,12 @@ int bzrtp_writePeerNode(bzrtpContext_t *context, uint8_t peerZID[12], uint8_t *t
}
if ((fileFlag&BZRTP_CACHE_LOADFILEBIT) == BZRTP_CACHE_LOADFILE) { /* we must reload the cache from file */
uint8_t *cacheStringBuffer;
uint32_t cacheStringLength;
/* reload cache from file locking it (TODO: lock) */
xmlFreeDoc(context->cacheBuffer);
context->cacheBuffer = NULL;
uint8_t *cacheStringBuffer;
uint32_t cacheStringLength;
context->zrtpCallbacks.bzrtp_loadCache(context->channelContext[0]->clientData, &cacheStringBuffer, &cacheStringLength);
context->cacheBuffer = xmlParseDoc(cacheStringBuffer);
free(cacheStringBuffer);
......@@ -250,17 +257,19 @@ int bzrtp_writePeerNode(bzrtpContext_t *context, uint8_t peerZID[12], uint8_t *t
/* parse the cache to find the peer element matching the given ZID */
if (context->cacheBuffer != NULL ) { /* there is a cache, try to find our peer element */
uint8_t peerZidHex[25];
xmlNodePtr rootNode;
xmlNodePtr cur = NULL;
uint8_t nodeUpdated = 0; /* a boolean flag set if node is updated */
bzrtp_int8ToStr(peerZidHex, peerZID, 12); /* compute the peerZID as an Hexa string */
peerZidHex[24]='\0';
xmlNodePtr rootNode = xmlDocGetRootElement(context->cacheBuffer);
xmlNodePtr cur = NULL;
rootNode = xmlDocGetRootElement(context->cacheBuffer);
/* if we found a root element, parse its children node */
if (rootNode!=NULL)
{
cur = rootNode->xmlChildrenNode->next; /* first node is selfZID, don't parse it */
}
uint8_t nodeUpdated = 0; /* a boolean flag set if node is updated */
while (cur!=NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"peer"))){ /* found a peer, check his ZID element */
xmlChar *currentZidHex = xmlNodeListGetString(context->cacheBuffer, cur->xmlChildrenNode->xmlChildrenNode, 1); /* ZID is the first element of peer */
......
......@@ -284,18 +284,22 @@ void test_aes128CFB(void) {
*
*/
void test_dhm2048(void) {
bzrtpRNGContext_t *RNGcontext;
bzrtpDHMContext_t *DHMaContext;
bzrtpDHMContext_t *DHMbContext;
bzrtpDHMContext_t *DHMcContext;
/* start the Random Number Generator */
bzrtpRNGContext_t *RNGcontext = bzrtpCrypto_startRNG((uint8_t *)"36 15 Grouiik vous souhaite une bonne journee", 46);
RNGcontext = bzrtpCrypto_startRNG((uint8_t *)"36 15 Grouiik vous souhaite une bonne journee", 46);
/* Create the context for Alice */
bzrtpDHMContext_t *DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
/* Create the public value for Alice G^Xa mod P */
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);
DHMbContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
/* Create the public value for Bob G^Xb mod P */
bzrtpCrypto_DHMCreatePublic(DHMbContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
......@@ -320,7 +324,7 @@ void test_dhm2048(void) {
bzrtpCrypto_DestroyDHMContext(DHMbContext);
/* create an unused context and destroy it to check the correct implementation of the create/destroy functions */
bzrtpDHMContext_t *DHMcContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
DHMcContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH2k, 32);
bzrtpCrypto_DestroyDHMContext(DHMcContext);
/* destroy the RNG context */
......@@ -333,18 +337,22 @@ void test_dhm2048(void) {
*
*/
void test_dhm3072(void) {
bzrtpRNGContext_t *RNGcontext;
bzrtpDHMContext_t *DHMaContext;
bzrtpDHMContext_t *DHMbContext;
bzrtpDHMContext_t *DHMcContext;
/* start the Random Number Generator */
bzrtpRNGContext_t *RNGcontext = bzrtpCrypto_startRNG((uint8_t *)"36 15 Grouiik vous souhaite une bonne journee", 46);
RNGcontext = bzrtpCrypto_startRNG((uint8_t *)"36 15 Grouiik vous souhaite une bonne journee", 46);
/* Create the context for Alice */
bzrtpDHMContext_t *DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
DHMaContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
/* Create the public value for Alice G^Xa mod P */
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);
DHMbContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
/* Create the public value for Bob G^Xb mod P */
bzrtpCrypto_DHMCreatePublic(DHMbContext, (int (*)(void *, uint8_t *, size_t))bzrtpCrypto_getRandom, (void *)RNGcontext);
......@@ -366,7 +374,7 @@ void test_dhm3072(void) {
bzrtpCrypto_DestroyDHMContext(DHMbContext);
/* create an unused context and destroy it to check the correct implementation of the create/destroy functions */
bzrtpDHMContext_t *DHMcContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
DHMcContext = bzrtpCrypto_CreateDHMContext(ZRTP_KEYAGREEMENT_DH3k, 32);
bzrtpCrypto_DestroyDHMContext(DHMcContext);
/* destroy the RNG context */
......@@ -457,6 +465,8 @@ void test_CRC32(void) {
void test_algoAgreement(void) {
int retval;
bzrtpHelloMessage_t *helloMessage;
/* first we have to create a context */
bzrtpContext_t *zrtpContext = bzrtp_createBzrtpContext(0x12345678); /* we don't use the SSRC for this test, set it to 12345678 */
......@@ -479,7 +489,7 @@ void test_algoAgreement(void) {
/* 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 */
bzrtpHelloMessage_t *helloMessage = (bzrtpHelloMessage_t *)helloPacket->messageData;
helloMessage = (bzrtpHelloMessage_t *)helloPacket->messageData;
helloMessage->supportedKeyAgreement[0] = ZRTP_KEYAGREEMENT_DH2k;
helloMessage->supportedKeyAgreement[1] = ZRTP_KEYAGREEMENT_DH3k;
......
This diff is collapsed.
......@@ -32,13 +32,12 @@
int main(int argc, char *argv[] ) {
CU_pSuite cryptoWrapperTestSuite, cryptoUtilsTestSuite, parserTestSuite;
#ifdef HAVE_LIBXML2
xmlInitParser();
#endif
CU_pSuite cryptoWrapperTestSuite, cryptoUtilsTestSuite, parserTestSuite;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry()) {
return CU_get_error();
......
......@@ -29,8 +29,8 @@
#include "cryptoUtils.h"
void printHex(char *title, uint8_t *data, uint32_t length) {
printf ("%s : ", title);
int i;
printf ("%s : ", title);
for (i=0; i<length; i++) {
printf ("0x%02x, ", data[i]);
}
......@@ -48,14 +48,16 @@ void packetDump(bzrtpPacket_t *zrtpPacket, uint8_t addRawMessage) {
switch (zrtpPacket->messageType) {
case MSGTYPE_HELLO :
{
bzrtpHelloMessage_t *messageData;
uint8_t algoTypeString[4];
printf(" - Message Type : Hello\n");
bzrtpHelloMessage_t *messageData = (bzrtpHelloMessage_t *)zrtpPacket->messageData;
messageData = (bzrtpHelloMessage_t *)zrtpPacket->messageData;
printf ("Version %.4s\nIdentifier %.16s\n", messageData->version, messageData->clientIdentifier);
printHex ("H3", messageData->H3, 32);
printHex ("ZID", messageData->ZID, 12);
printf ("S : %d - M : %d - P : %d\nhc : %x - cc : %x - ac : %x - kc : %x - sc : %x\n", messageData->S, messageData->M, messageData->P, messageData->hc, messageData->cc, messageData->ac, messageData->kc, messageData->sc);
printf ("hc ");
uint8_t algoTypeString[4];
for (j=0; j<messageData->hc; j++) {
cryptoAlgoTypeIntToString(messageData->supportedHash[j], algoTypeString);
printf("%.4s, ", algoTypeString);
......@@ -94,8 +96,10 @@ void packetDump(bzrtpPacket_t *zrtpPacket, uint8_t addRawMessage) {
case MSGTYPE_COMMIT:
{
uint8_t algoTypeString[4];
bzrtpCommitMessage_t *messageData;
printf(" - Message Type : Commit\n");
bzrtpCommitMessage_t *messageData = (bzrtpCommitMessage_t *)zrtpPacket->messageData;
messageData = (bzrtpCommitMessage_t *)zrtpPacket->messageData;
printHex("H2", messageData->H2, 32);
printHex("ZID", messageData->ZID, 12);
cryptoAlgoTypeIntToString(messageData->hashAlgo, algoTypeString);
......@@ -126,12 +130,14 @@ void packetDump(bzrtpPacket_t *zrtpPacket, uint8_t addRawMessage) {
case MSGTYPE_DHPART1:
case MSGTYPE_DHPART2:
{
bzrtpDHPartMessage_t *messageData;
if (zrtpPacket->messageType == MSGTYPE_DHPART1) {
printf(" - Message Type : DHPart1\n");
} else {
printf(" - Message Type : DHPart2\n");
}
bzrtpDHPartMessage_t *messageData = (bzrtpDHPartMessage_t *)zrtpPacket->messageData;
messageData = (bzrtpDHPartMessage_t *)zrtpPacket->messageData;
printHex ("H1", messageData->H1, 32);
printHex ("rs1ID", messageData->rs1ID, 8);
printHex ("rs2ID", messageData->rs2ID, 8);
......@@ -147,12 +153,14 @@ void packetDump(bzrtpPacket_t *zrtpPacket, uint8_t addRawMessage) {
case MSGTYPE_CONFIRM1:
case MSGTYPE_CONFIRM2:
{
bzrtpConfirmMessage_t *messageData;
if (zrtpPacket->messageType == MSGTYPE_CONFIRM1) {
printf(" - Message Type : Confirm1\n");
} else {
printf(" - Message Type : Confirm2\n");
}
bzrtpConfirmMessage_t *messageData = (bzrtpConfirmMessage_t *)zrtpPacket->messageData;
messageData = (bzrtpConfirmMessage_t *)zrtpPacket->messageData;
printHex("H0", messageData->H0, 32);
printf("sig_len %d\n", messageData->sig_len);
printf("E %d V %d A %d D %d\n", messageData->E, messageData->V, messageData->A, messageData->D);
......
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