From version 4.2
edited by Sylvain Berfini
on 2017/02/21 14:45
To version 5.1
edited by Sylvain Berfini
on 2017/02/21 15:58
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -45,6 +45,10 @@
45 45  
46 46  Each callback will also be given at least the LinphoneImEncryptionEngine object and the LinphoneChatMessage. From the LinphoneChatMessage, you can access multiple relevant information such as content type, the message itself, the LinphoneChatRoom, the LinphoneCore etc... and in LinphoneImEncryptionEngine you can store whatever you want in the user_data field.
47 47  
48 +{{warning}}
49 +Keep in mind that if you change the content type when encrypting the message, you must change it back to text/plain when you decrypt it!
50 +{{/warning}}
51 +
48 48  File transfer callbacks give directly in parameters the buffer to encrypt/decrypt, the offset of the buffer in the file, the size of the buffer and an empty buffer of the same size for you to store the result of the encryption/decryption.
49 49  
50 50  {{warning}}
... ... @@ -56,3 +56,80 @@
56 56  {{code language="c"}}
57 57  linphone_content_set_key(msg->file_transfer_information, keyBuffer, keySize); /* key is duplicated in the content private structure */
58 58  {{/code}}
63 +
64 +=== Sample code ===
65 +
66 +The message_tester.c file in tester/ contains a simple test that does a XOR encryption using the IMEE:
67 +
68 +{{code language="C"}}
69 +char* xor(char* message, char* key) {
70 + size_t messagelen = strlen(message);
71 + size_t keylen = strlen(key);
72 + char* encrypted = (char *)ms_malloc(messagelen+1);
73 +
74 + int i;
75 + for(i = 0; i < (int)messagelen; i++) {
76 + encrypted[i] = message[i] ^ key[i % keylen];
77 + }
78 + encrypted[messagelen] = '\0';
79 +
80 + return encrypted;
81 +}
82 +
83 +int xor_im_encryption_engine_process_incoming_message_cb(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
84 + char *new_content_type = "cipher/xor";
85 + if (msg->content_type) {
86 + if (strcmp(msg->content_type, new_content_type) == 0) {
87 + msg->message = xor(msg->message, "SuperSecretXorKey");
88 + msg->content_type = ms_strdup("text/plain");
89 + return 0;
90 + } else if (strcmp(msg->content_type, "text/plain") == 0) {
91 + return -1; // Not encrypted, nothing to do
92 + } else {
93 + return 488; // Not acceptable
94 + }
95 + }
96 + return 500;
97 +}
98 +
99 +int xor_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
100 + char *new_content_type = "cipher/xor";
101 + msg->message = xor(msg->message, "SuperSecretXorKey");
102 + msg->content_type = ms_strdup(new_content_type);
103 + return 0;
104 +}
105 +
106 +void im_encryption_engine_xor(void) {
107 + LinphoneChatMessage *chat_msg = NULL;
108 + LinphoneChatRoom* chat_room = NULL;
109 + LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
110 + LinphoneImEncryptionEngine *marie_imee = linphone_im_encryption_engine_new(marie->lc);
111 + LinphoneImEncryptionEngineCbs *marie_cbs = linphone_im_encryption_engine_get_callbacks(marie_imee);
112 + LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
113 + LinphoneImEncryptionEngine *pauline_imee = linphone_im_encryption_engine_new(pauline->lc);
114 + LinphoneImEncryptionEngineCbs *pauline_cbs = linphone_im_encryption_engine_get_callbacks(pauline_imee);
115 +
116 + linphone_im_encryption_engine_cbs_set_process_incoming_message(marie_cbs, xor_im_encryption_engine_process_incoming_message_cb);
117 + linphone_im_encryption_engine_cbs_set_process_outgoing_message(marie_cbs, xor_im_encryption_engine_process_outgoing_message_cb);
118 + linphone_im_encryption_engine_cbs_set_process_incoming_message(pauline_cbs, xor_im_encryption_engine_process_incoming_message_cb);
119 + linphone_im_encryption_engine_cbs_set_process_outgoing_message(pauline_cbs, xor_im_encryption_engine_process_outgoing_message_cb);
120 +
121 + linphone_core_set_im_encryption_engine(marie->lc, marie_imee);
122 + linphone_core_set_im_encryption_engine(pauline->lc, pauline_imee);
123 +
124 + chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
125 + chat_msg = linphone_chat_room_create_message(chat_room, "Bla bla bla bla");
126 + linphone_chat_room_send_chat_message(chat_room, chat_msg);
127 + BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceived,1));
128 + BC_ASSERT_PTR_NOT_NULL(marie->stat.last_received_chat_message);
129 + if (marie->stat.last_received_chat_message) {
130 + BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_text(marie->stat.last_received_chat_message), "Bla bla bla bla");
131 + }
132 + BC_ASSERT_PTR_NOT_NULL(linphone_core_get_chat_room(marie->lc,pauline->identity));
133 +
134 + linphone_im_encryption_engine_unref(marie_imee);
135 + linphone_im_encryption_engine_unref(pauline_imee);
136 + linphone_core_manager_destroy(marie);
137 + linphone_core_manager_destroy(pauline);
138 +}
139 +{{/code}}