diff --git a/coreapi/Makefile.am b/coreapi/Makefile.am index 969d39794..df58a716b 100644 --- a/coreapi/Makefile.am +++ b/coreapi/Makefile.am @@ -65,7 +65,7 @@ liblinphone_la_SOURCES=\ linphonecore_utils.h \ localplayer.c \ lpc2xml.c \ - lime.c \ + lime.c lime.h\ lpconfig.c lpconfig.h \ lsd.c \ message_storage.c \ diff --git a/coreapi/bellesip_sal/sal_op_message.c b/coreapi/bellesip_sal/sal_op_message.c index e44e15823..7423fbe76 100644 --- a/coreapi/bellesip_sal/sal_op_message.c +++ b/coreapi/bellesip_sal/sal_op_message.c @@ -113,25 +113,25 @@ void sal_process_incoming_message(SalOp *op,const belle_sip_request_event_t *eve if (content_type && (cipher_xml=is_cipher_xml(content_type))) { /* access the zrtp cache to get keys needed to decipher the message */ LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op)); - FILE *CACHEFD = fopen(lc->zrtp_secrets_cache, "r+"); + FILE *CACHEFD = fopen(lc->zrtp_secrets_cache, "rb+"); if (CACHEFD == NULL) { ms_warning("Unable to access ZRTP ZID cache to decrypt message"); } else { - int cacheSize; - uint8_t *cacheString; + size_t cacheSize; + char *cacheString; int retval; xmlDocPtr cacheXml; - - fseek(CACHEFD, 0L, SEEK_END); /* Position to end of file */ - cacheSize = ftell(CACHEFD); /* Get file length */ - rewind(CACHEFD); /* Back to start of file */ - cacheString = (uint8_t *)malloc(cacheSize*sizeof(uint8_t)+1); /* string must be null terminated */ - fread(cacheString, 1, cacheSize, CACHEFD); + + cacheString=ms_load_file_content(CACHEFD, &cacheSize); + if (!cacheString){ + ms_warning("Unable to load content of ZRTP ZID cache to decrypt message"); + return; + } cacheString[cacheSize] = '\0'; cacheSize += 1; fclose(CACHEFD); - cacheXml = xmlParseDoc(cacheString); - free(cacheString); + cacheXml = xmlParseDoc((xmlChar*)cacheString); + ms_free(cacheString); retval = lime_decryptMultipartMessage(cacheXml, (uint8_t *)belle_sip_message_get_body(BELLE_SIP_MESSAGE(req)), &decryptedMessage); if (retval != 0) { ms_warning("Unable to decrypt message, reason : %s - op [%p]", lime_error_code_to_string(retval), op); @@ -146,8 +146,10 @@ void sal_process_incoming_message(SalOp *op,const belle_sip_request_event_t *eve int xmlStringLength; xmlDocDumpFormatMemoryEnc(cacheXml, &xmlStringOutput, &xmlStringLength, "UTF-8", 0); /* write it to the cache file */ - CACHEFD = fopen(lc->zrtp_secrets_cache, "w+"); - fwrite(xmlStringOutput, 1, xmlStringLength, CACHEFD); + CACHEFD = fopen(lc->zrtp_secrets_cache, "wb+"); + if (fwrite(xmlStringOutput, 1, xmlStringLength, CACHEFD)<=0){ + ms_warning("Fail to write cache"); + } xmlFree(xmlStringOutput); fclose(CACHEFD); } @@ -258,44 +260,48 @@ int sal_message_send(SalOp *op, const char *from, const char *to, const char* co if ((strcmp(content_type, "xml/cipher") == 0) || ((strcmp(content_type, "application/cipher.vnd.gsma.rcs-ft-http+xml") == 0))) { /* access the zrtp cache to get keys needed to cipher the message */ LinphoneCore *lc=(LinphoneCore *)sal_get_user_pointer(sal_op_get_sal(op)); - FILE *CACHEFD = fopen(lc->zrtp_secrets_cache, "r+"); + FILE *CACHEFD = fopen(lc->zrtp_secrets_cache, "rb+"); if (CACHEFD == NULL) { ms_warning("Unable to access ZRTP ZID cache to encrypt message"); + /*probably not a good idea to do this:*/ sal_error_info_set(&op->error_info, SalReasonNotAcceptable, 488, "Unable to encrypt IM", NULL); op->base.root->callbacks.text_delivery_update(op,SalTextDeliveryFailed); - return 0; + return -1; } else { - int cacheSize; - uint8_t *cacheString; + size_t cacheSize; + char *cacheString; xmlDocPtr cacheXml; int retval; - fseek(CACHEFD, 0L, SEEK_END); /* Position to end of file */ - cacheSize = ftell(CACHEFD); /* Get file length */ - rewind(CACHEFD); /* Back to start of file */ - cacheString = (uint8_t *)malloc(cacheSize*sizeof(uint8_t)+1); /* string must be null terminated */ - fread(cacheString, 1, cacheSize, CACHEFD); + cacheString=ms_load_file_content(CACHEFD, &cacheSize); + if (!cacheString){ + ms_warning("Unable to load content of ZRTP ZID cache to encrypt message"); + return -1; + } cacheString[cacheSize] = '\0'; cacheSize += 1; fclose(CACHEFD); - cacheXml = xmlParseDoc(cacheString); - free(cacheString); + cacheXml = xmlParseDoc((xmlChar*)cacheString); + ms_free(cacheString); retval = lime_createMultipartMessage(cacheXml, (uint8_t *)msg, (uint8_t *)peer_uri, &multipartEncryptedMessage); if (retval != 0) { ms_warning("Unable to encrypt message for %s : %s - op [%p]", peer_uri, lime_error_code_to_string(retval), op); xmlFreeDoc(cacheXml); free(multipartEncryptedMessage); + /*probably not a good idea to do this:*/ sal_error_info_set(&op->error_info, SalReasonNotAcceptable, 488, "Unable to encrypt IM", NULL); op->base.root->callbacks.text_delivery_update(op,SalTextDeliveryFailed); - return 0; + return -1; } else { /* dump updated cache to a string */ xmlChar *xmlStringOutput; int xmlStringLength; xmlDocDumpFormatMemoryEnc(cacheXml, &xmlStringOutput, &xmlStringLength, "UTF-8", 0); /* write it to the cache file */ - CACHEFD = fopen(lc->zrtp_secrets_cache, "w+"); - fwrite(xmlStringOutput, 1, xmlStringLength, CACHEFD); + CACHEFD = fopen(lc->zrtp_secrets_cache, "wb+"); + if (fwrite(xmlStringOutput, 1, xmlStringLength, CACHEFD)<=0){ + ms_warning("Unable to write zid cache"); + } xmlFree(xmlStringOutput); fclose(CACHEFD); content_length = strlen((const char *)multipartEncryptedMessage); diff --git a/coreapi/lime.h b/coreapi/lime.h index 861c9acd7..7cb40efa8 100644 --- a/coreapi/lime.h +++ b/coreapi/lime.h @@ -43,7 +43,7 @@ typedef struct limeURIKeys_struct { * * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_getSelfZid(xmlDocPtr cacheBuffer, uint8_t selfZid[25]); +int lime_getSelfZid(xmlDocPtr cacheBuffer, uint8_t selfZid[25]); /** * @brief Get from cache all the senders keys associated to the given URI @@ -55,7 +55,7 @@ __attribute__ ((visibility ("default"))) int lime_getSelfZid(xmlDocPtr cacheBuff * * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_getCachedSndKeysByURI(xmlDocPtr cacheBuffer, limeURIKeys_t *associatedKeys); +int lime_getCachedSndKeysByURI(xmlDocPtr cacheBuffer, limeURIKeys_t *associatedKeys); /** * @brief Get the receiver key associated to the ZID given in the associatedKey parameter @@ -65,7 +65,7 @@ __attribute__ ((visibility ("default"))) int lime_getCachedSndKeysByURI(xmlDocPt * * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_getCachedRcvKeyByZid(xmlDocPtr cacheBuffer, limeKey_t *associatedKey); +int lime_getCachedRcvKeyByZid(xmlDocPtr cacheBuffer, limeKey_t *associatedKey); /** * @brief Set in cache the given key material, association is made by ZID contained in the associatedKey parameter @@ -77,7 +77,7 @@ __attribute__ ((visibility ("default"))) int lime_getCachedRcvKeyByZid(xmlDocPtr * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_setCachedKey(xmlDocPtr cacheBuffer, limeKey_t *associatedKey, uint8_t role); +int lime_setCachedKey(xmlDocPtr cacheBuffer, limeKey_t *associatedKey, uint8_t role); /** * @brief Free all allocated data in the associated keys structure @@ -86,7 +86,7 @@ __attribute__ ((visibility ("default"))) int lime_setCachedKey(xmlDocPtr cacheBu * @param[in/out] associatedKeys The structure to be cleaned * */ -__attribute__ ((visibility ("default"))) void lime_freeKeys(limeURIKeys_t associatedKeys); +void lime_freeKeys(limeURIKeys_t associatedKeys); /** * @brief Derive in place the key given in parameter and increment session index @@ -96,7 +96,7 @@ __attribute__ ((visibility ("default"))) void lime_freeKeys(limeURIKeys_t associ * * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_deriveKey(limeKey_t *key); +int lime_deriveKey(limeKey_t *key); /** * @brief encrypt a message with the given key @@ -111,7 +111,7 @@ __attribute__ ((visibility ("default"))) int lime_deriveKey(limeKey_t *key); * @return 0 on success, error code otherwise * */ -__attribute__ ((visibility ("default"))) int lime_encryptMessage(limeKey_t *key, uint8_t *plainMessage, uint32_t messageLength, uint8_t selfZID[12], uint8_t *encryptedMessage); +int lime_encryptMessage(limeKey_t *key, uint8_t *plainMessage, uint32_t messageLength, uint8_t selfZID[12], uint8_t *encryptedMessage); /** * @brief Encrypt a file before transfering it to the server, encryption is done in several call, first one will be done with cryptoContext null, last one with length = 0 @@ -125,7 +125,7 @@ __attribute__ ((visibility ("default"))) int lime_encryptMessage(limeKey_t *key, * @return 0 on success, error code otherwise * */ -__attribute__ ((visibility ("default"))) int lime_encryptFile(void **cryptoContext, unsigned char *key, size_t length, char *plain, char *cipher); +int lime_encryptFile(void **cryptoContext, unsigned char *key, size_t length, char *plain, char *cipher); /** * @brief Decrypt a file retrieved from server, decryption is done in several call, first one will be done with cryptoContext null, last one with length = 0 @@ -139,7 +139,7 @@ __attribute__ ((visibility ("default"))) int lime_encryptFile(void **cryptoConte * @return 0 on success, error code otherwise * */ -__attribute__ ((visibility ("default"))) int lime_decryptFile(void **cryptoContext, unsigned char *key, size_t length, char *plain, char *cipher); +int lime_decryptFile(void **cryptoContext, unsigned char *key, size_t length, char *plain, char *cipher); /** * @brief decrypt and authentify a message with the given key @@ -155,7 +155,7 @@ __attribute__ ((visibility ("default"))) int lime_decryptFile(void **cryptoConte * */ -__attribute__ ((visibility ("default"))) int lime_decryptMessage(limeKey_t *key, uint8_t *encryptedMessage, uint32_t messageLength, uint8_t selfZID[12], uint8_t *plainMessage); +int lime_decryptMessage(limeKey_t *key, uint8_t *encryptedMessage, uint32_t messageLength, uint8_t selfZID[12], uint8_t *plainMessage); /** * @brief create the encrypted multipart xml message from plain text and destination URI @@ -168,7 +168,7 @@ __attribute__ ((visibility ("default"))) int lime_decryptMessage(limeKey_t *key, * * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_createMultipartMessage(xmlDocPtr cacheBuffer, uint8_t *message, uint8_t *peerURI, uint8_t **output); +int lime_createMultipartMessage(xmlDocPtr cacheBuffer, uint8_t *message, uint8_t *peerURI, uint8_t **output); /** * @brief decrypt a multipart xml message @@ -181,7 +181,7 @@ __attribute__ ((visibility ("default"))) int lime_createMultipartMessage(xmlDocP * @return 0 on success, error code otherwise */ -__attribute__ ((visibility ("default"))) int lime_decryptMultipartMessage(xmlDocPtr cacheBuffer, uint8_t *message, uint8_t **output); +int lime_decryptMultipartMessage(xmlDocPtr cacheBuffer, uint8_t *message, uint8_t **output); /** * @brief given a readable version of error code generated by Lime functions diff --git a/tester/message_tester.c b/tester/message_tester.c index 0eea776b4..0a9fd26bd 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -1148,7 +1148,7 @@ void printHex(char *title, uint8_t *data, uint32_t length) { static void lime_unit(void) { int retval; - int size; + size_t size; uint8_t *cacheBufferString; xmlDocPtr cacheBufferAlice; xmlDocPtr cacheBufferBob; @@ -1167,30 +1167,22 @@ static void lime_unit(void) { xmlDocPtr cacheBuffer; /* Load Alice cache file */ - FILE *CACHE = fopen("ZIDCacheAlice.xml", "r+"); - fseek(CACHE, 0L, SEEK_END); /* Position to end of file */ - size = ftell(CACHE); /* Get file length */ - rewind(CACHE); /* Back to start of file */ - cacheBufferString = (uint8_t *)malloc(size*sizeof(uint8_t)+1); - fread(cacheBufferString, 1, size, CACHE); + FILE *CACHE = fopen("ZIDCacheAlice.xml", "rb+"); + cacheBufferString = (uint8_t *)ms_load_file_content(CACHE, &size); *(cacheBufferString+size) = '\0'; fclose(CACHE); /* parse it to an xmlDoc */ cacheBufferAlice = xmlParseDoc(cacheBufferString); - free(cacheBufferString); + ms_free(cacheBufferString); /* Load Bob cache file */ - CACHE = fopen("ZIDCacheBob.xml", "r+"); - fseek(CACHE, 0L, SEEK_END); /* Position to end of file */ - size = ftell(CACHE); /* Get file length */ - rewind(CACHE); /* Back to start of file */ - cacheBufferString = (uint8_t *)malloc(size*sizeof(uint8_t)+1); - fread(cacheBufferString, 1, size, CACHE); + CACHE = fopen("ZIDCacheBob.xml", "rb+"); + cacheBufferString = (uint8_t *)ms_load_file_content(CACHE, &size); *(cacheBufferString+size) = '\0'; fclose(CACHE); /* parse it to an xmlDoc */ cacheBufferBob = xmlParseDoc(cacheBufferString); - free(cacheBufferString); + ms_free(cacheBufferString); @@ -1216,14 +1208,14 @@ static void lime_unit(void) { /* dump the xml document into a string */ xmlDocDumpFormatMemoryEnc(cacheBufferAlice, &xmlStringOutput, &xmlStringLength, "UTF-8", 0); /* write it to the file */ - CACHE = fopen("ZIDCacheAlice.xml", "w+"); + CACHE = fopen("ZIDCacheAlice.xml", "wb+"); fwrite(xmlStringOutput, 1, xmlStringLength, CACHE); xmlFree(xmlStringOutput); fclose(CACHE); xmlDocDumpFormatMemoryEnc(cacheBufferBob, &xmlStringOutput, &xmlStringLength, "UTF-8", 0); /* write it to the file */ - CACHE = fopen("ZIDCacheBob.xml", "w+"); + CACHE = fopen("ZIDCacheBob.xml", "wb+"); fwrite(xmlStringOutput, 1, xmlStringLength, CACHE); xmlFree(xmlStringOutput); fclose(CACHE); @@ -1233,17 +1225,13 @@ static void lime_unit(void) { xmlFreeDoc(cacheBufferBob); /* Load cache file */ - CACHE = fopen("ZIDCache.xml", "r+"); - fseek(CACHE, 0L, SEEK_END); /* Position to end of file */ - size = ftell(CACHE); /* Get file length */ - rewind(CACHE); /* Back to start of file */ - cacheBufferString = (uint8_t *)malloc(size*sizeof(uint8_t)+1); - fread(cacheBufferString, 1, size, CACHE); + CACHE = fopen("ZIDCache.xml", "rb+"); + cacheBufferString = (uint8_t*) ms_load_file_content(CACHE, &size); *(cacheBufferString+size) = '\0'; fclose(CACHE); /* parse it to an xmlDoc */ cacheBuffer = xmlParseDoc(cacheBufferString); - free(cacheBufferString); + ms_free(cacheBufferString); /* get data from cache : sender */ associatedKeys.peerURI = (uint8_t *)malloc(15);