Commit 66034463 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Started to get back LIME working for file transfer

parent ecb802ea
......@@ -396,6 +396,8 @@ int lime_encryptMessage(limeKey_t *key, const uint8_t *plainMessage, uint32_t me
int lime_encryptFile(void **cryptoContext, unsigned char *key, size_t length, char *plain, char *cipher) {
bctbx_aes_gcm_context_t *gcmContext;
if (key == NULL) return -1;
if (*cryptoContext == NULL) { /* first call to the function, allocate a crypto context and initialise it */
/* key contains 192bits of key || 64 bits of Initialisation Vector, no additional data */
gcmContext = bctbx_aes_gcm_context_new(key, 24, NULL, 0, key+24, 8, BCTBX_GCM_ENCRYPT);
......
......@@ -25,6 +25,7 @@
#include "chat/chat-room/chat-room-p.h"
#include "core/core.h"
#include "logger/logger.h"
#include "bctoolbox/crypto.h"
#include "file-transfer-chat-message-modifier.h"
......@@ -268,7 +269,6 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h
belle_sip_user_body_handler_t *body_handler = (belle_sip_user_body_handler_t *)first_part_bh;
// No need to add again the callback for progression, otherwise it will be called twice
first_part_bh = (belle_sip_body_handler_t *)belle_sip_file_body_handler_new(currentFileContentToTransfer->getFilePath().c_str(), nullptr, this);
//linphone_content_set_size(cFileTransferInformation, belle_sip_file_body_handler_get_file_size((belle_sip_file_body_handler_t *)first_part_bh));
belle_sip_file_body_handler_set_user_body_handler((belle_sip_file_body_handler_t *)first_part_bh, body_handler);
} else if (!currentFileContentToTransfer->isEmpty()) {
first_part_bh = (belle_sip_body_handler_t *)belle_sip_memory_body_handler_new_from_buffer(
......@@ -293,11 +293,10 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h
} else if (code == 200) { // file has been uploaded correctly, get server reply and send it
const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response);
if (body && strlen(body) > 0) {
//TODO
FileTransferContent *fileTransferContent = new FileTransferContent();
// if we have an encryption key for the file, we must insert it into the msg and restore the correct filename
/*const char *content_key = linphone_content_get_key(cFileTransferInformation);
size_t content_key_size = linphone_content_get_key_size(cFileTransferInformation);
if (content_key != nullptr) {
string content_key = currentFileContentToTransfer->getFileKey();
if (!content_key.empty()) {
// parse the msg body
xmlDocPtr xmlMessageBody = xmlParseDoc((const xmlChar *)body);
......@@ -310,15 +309,17 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h
xmlChar *typeAttribute = xmlGetProp(cur, (const xmlChar *)"type");
// this is the node we are looking for : add a file-key children node
if (!xmlStrcmp(typeAttribute, (const xmlChar *)"file")) {
size_t content_key_size = content_key.length();
// need to parse the children node to update the file-name one
xmlNodePtr fileInfoNodeChildren = cur->xmlChildrenNode;
// convert key to base64
size_t b64Size = b64_encode(nullptr, content_key_size, nullptr, 0);
char *keyb64 = (char *)ms_malloc0(b64Size + 1);
size_t b64Size;
bctbx_base64_encode(nullptr, &b64Size, (unsigned char *)content_key.c_str(), content_key_size);
unsigned char *keyb64 = (unsigned char *)ms_malloc0(b64Size + 1);
int xmlStringLength;
b64Size = b64_encode(content_key, content_key_size, keyb64, b64Size);
keyb64[b64Size] = '\0'; // libxml need a null terminated string
bctbx_base64_encode(keyb64, &b64Size, (unsigned char *)content_key.c_str(), content_key_size);
keyb64[b64Size] = '\0'; // libxml need a null terminated string
// add the node containing the key to the file-info node
xmlNewTextChild(cur, nullptr, (const xmlChar *)"file-key", (const xmlChar *)keyb64);
......@@ -330,7 +331,7 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h
// we found a the file-name node, update its content with the real filename
if (!xmlStrcmp(fileInfoNodeChildren->name, (const xmlChar *)"file-name")) {
// update node content
xmlNodeSetContent(fileInfoNodeChildren, (const xmlChar *)(linphone_content_get_name(cFileTransferInformation)));
xmlNodeSetContent(fileInfoNodeChildren, (const xmlChar *)(currentFileContentToTransfer->getFileName().c_str()));
break;
}
fileInfoNodeChildren = fileInfoNodeChildren->next;
......@@ -339,7 +340,7 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h
// dump the xml into msg->message
char *buffer;
xmlDocDumpFormatMemoryEnc(xmlMessageBody, (xmlChar **)&buffer, &xmlStringLength, "UTF-8", 0);
setText(buffer);
fileTransferContent->setBody(buffer);
break;
}
xmlFree(typeAttribute);
......@@ -348,16 +349,13 @@ void FileTransferChatMessageModifier::processResponseFromPostFile (const belle_h
}
}
xmlFreeDoc(xmlMessageBody);
} else { // no encryption key, transfer in plain, just copy the msg sent by server
setText(body);
} else { // no encryption key, transfer in plain, just copy the msg sent by server
fileTransferContent->setBody(body);
}
setContentType(ContentType::FileTransfer);*/
FileContent *fileContent = currentFileContentToTransfer;
FileTransferContent *fileTransferContent = new FileTransferContent();
FileContent *fileContent = currentFileContentToTransfer;
fileTransferContent->setContentType(ContentType::FileTransfer);
fileTransferContent->setFileContent(fileContent);
fileTransferContent->setBody(body);
message->removeContent(*fileContent);
message->addContent(*fileTransferContent);
......@@ -634,20 +632,21 @@ static void createFileTransferInformationsFromVndGsmaRcsFtHttpXml (FileTransferC
fileUrl = xmlGetProp(cur, (const xmlChar *)"url");
}
//TODO
/*if (!xmlStrcmp(cur->name, (const xmlChar *)"file-key")) {
if (!xmlStrcmp(cur->name, (const xmlChar *)"file-key")) {
// there is a key in the msg: file has been encrypted
// convert the key from base 64
xmlChar *keyb64 = xmlNodeListGetString(xmlMessageBody, cur->xmlChildrenNode, 1);
size_t keyLength = b64::b64_decode((char *)keyb64, strlen((char *)keyb64), nullptr, 0);
size_t keyLength;
bctbx_base64_decode(NULL, &keyLength, (unsigned char *)keyb64, strlen((const char *)keyb64));
uint8_t *keyBuffer = (uint8_t *)malloc(keyLength);
// decode the key into local key buffer
b64::b64_decode((char *)keyb64, strlen((char *)keyb64), keyBuffer, keyLength);
linphone_content_set_key(content, (char *)keyBuffer, keyLength);
bctbx_base64_decode(keyBuffer, &keyLength, (unsigned char *)keyb64, strlen((const char *)keyb64));
string key = string((const char *)keyBuffer);
fileContent->setFileKey(key);
// duplicate key value into the linphone content private structure
xmlFree(keyb64);
free(keyBuffer);
}*/
}
cur = cur->next;
}
......
......@@ -36,6 +36,7 @@ public:
string fileName;
string filePath;
size_t fileSize = 0;
string fileKey;
};
// -----------------------------------------------------------------------------
......@@ -47,6 +48,7 @@ FileContent::FileContent (const FileContent &src) : Content(*new FileContentPriv
d->fileName = src.getFileName();
d->filePath = src.getFilePath();
d->fileSize = src.getFileSize();
d->fileKey = src.getFileKey();
}
FileContent::FileContent (FileContent &&src) : Content(*new FileContentPrivate) {
......@@ -54,6 +56,7 @@ FileContent::FileContent (FileContent &&src) : Content(*new FileContentPrivate)
d->fileName = move(src.getPrivate()->fileName);
d->filePath = move(src.getPrivate()->filePath);
d->fileSize = move(src.getPrivate()->fileSize);
d->fileKey = move(src.getPrivate()->fileKey);
}
FileContent &FileContent::operator= (const FileContent &src) {
......@@ -63,6 +66,7 @@ FileContent &FileContent::operator= (const FileContent &src) {
d->fileName = src.getFileName();
d->filePath = src.getFilePath();
d->fileSize = src.getFileSize();
d->fileKey = src.getFileKey();
}
return *this;
......@@ -74,6 +78,7 @@ FileContent &FileContent::operator= (FileContent &&src) {
d->fileName = move(src.getPrivate()->fileName);
d->filePath = move(src.getPrivate()->filePath);
d->fileSize = move(src.getPrivate()->fileSize);
d->fileKey = move(src.getPrivate()->fileKey);
return *this;
}
......@@ -82,7 +87,8 @@ bool FileContent::operator== (const FileContent &content) const {
return Content::operator==(content) &&
d->fileName == content.getFileName() &&
d->filePath == content.getFilePath() &&
d->fileSize == content.getFileSize();
d->fileSize == content.getFileSize() &&
d->fileKey == content.getFileKey();
}
void FileContent::setFileSize (size_t size) {
......@@ -115,6 +121,16 @@ const string &FileContent::getFilePath () const {
return d->filePath;
}
void FileContent::setFileKey (const string &key) {
L_D();
d->fileKey = key;
}
const string &FileContent::getFileKey () const {
L_D();
return d->fileKey;
}
bool FileContent::isFile () const {
return true;
}
......@@ -125,6 +141,7 @@ LinphoneContent *FileContent::toLinphoneContent () const {
linphone_content_set_subtype(content, getContentType().getSubType().c_str());
linphone_content_set_name(content, getFileName().c_str());
linphone_content_set_size(content, getFileSize());
linphone_content_set_key(content, getFileKey().c_str(), getFileKey().size());
return content;
}
......
......@@ -48,6 +48,9 @@ public:
void setFilePath (const std::string &path);
const std::string &getFilePath () const;
void setFileKey (const std::string &key);
const std::string &getFileKey () const;
bool isFile () const override;
// TODO: Remove me later.
......
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