Commits (5)
.vscode/
build/
coverage/
*.gc*
......
......@@ -40,7 +40,7 @@ PKGCFG_L=$(GLIB_LDFLAGS) \
CFLAGS += -std=c11 -Wall -Wextra -Wpedantic -Wstrict-overflow \
-fno-strict-aliasing -funsigned-char \
-fno-builtin-memset $(PKGCFG_C)
-fno-builtin-memset -g $(PKGCFG_C)
CPPFLAGS += -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE
CFLAGS_CONVERSATIONS=$(CFLAGS) -DOMEMO_XMLNS='"eu.siacs.conversations.axolotl"' -DOMEMO_NS_SEPARATOR='"."' -DOMEMO_NS_NOVERSION
COVFLAGS = --coverage -O0 -g $(CFLAGS)
......
......@@ -110,15 +110,22 @@ struct omemo_message {
* @return Returns the length on success, and negative on error.
*/
static int int_to_string(uint32_t in, char ** out) {
size_t len = snprintf((void *) 0, 0, "%i", in);
size_t buf_len = len + 1;
char * int_string = malloc(buf_len);
int len;
size_t buf_len;
char * int_string;
len = snprintf((void *) 0, 0, "%i", in);
if (len < 0) {
return -1;
}
buf_len = len + 1;
int_string = malloc(buf_len);
if (!int_string) {
return OMEMO_ERR_NOMEM;
}
memset(int_string, 0, buf_len);
size_t result = snprintf(int_string, buf_len, "%i", in);
int result = snprintf(int_string, buf_len, "%i", in);
if (result != len) {
free(int_string);
return -1;
......@@ -170,7 +177,7 @@ int omemo_bundle_create(omemo_bundle ** bundle_pp) {
int omemo_bundle_set_device_id(omemo_bundle * bundle_p, uint32_t device_id) {
char * id_string = (void *) 0;
int ret = int_to_string(device_id, &id_string);
if (ret < 0) {
if (ret <= 0) {
return ret;
}
......@@ -996,6 +1003,7 @@ int omemo_message_create(uint32_t sender_device_id, const omemo_crypto_provider
iv_b64 = g_base64_encode(iv_p, OMEMO_AES_GCM_IV_LENGTH);
if (int_to_string(sender_device_id, &device_id_string) <= 0) {
ret_val = -1;
goto cleanup;
}
header_node_p = mxmlNewElement(MXML_NO_PARENT, HEADER_NODE_NAME);
......@@ -1356,9 +1364,9 @@ int omemo_message_get_encrypted_key(omemo_message * msg_p, uint32_t own_device_i
const char * key_b64 = (void *) 0;
size_t key_len = 0;
ret_val = expect_next_node(msg_p->header_node_p, mxmlGetFirstChild, KEY_NODE_NAME, &key_node_p);
if (ret_val) {
key_node_p = mxmlFindElement(msg_p->header_node_p, msg_p->header_node_p, KEY_NODE_NAME, NULL, NULL, MXML_DESCEND);
if (!key_node_p) {
// if there is not at least one key, skip the rest of the function
ret_val = 0;
*key_pp = (void *) 0;
goto cleanup;
......@@ -1369,6 +1377,7 @@ int omemo_message_get_encrypted_key(omemo_message * msg_p, uint32_t own_device_i
goto cleanup;
}
while (key_node_p) {
if (!strncmp(rid_string, mxmlElementGetAttr(key_node_p, KEY_NODE_RID_ATTR_NAME), strlen(rid_string))) {
key_b64 = mxmlGetOpaque(key_node_p);
......@@ -1430,8 +1439,9 @@ int omemo_message_export_decrypted(omemo_message * msg_p, uint8_t * key_p, size_
}
payload_p = g_base64_decode(payload_b64, &payload_len);
ret_val = expect_next_node(msg_p->header_node_p, mxmlGetLastChild, IV_NODE_NAME, &iv_node_p);
if (ret_val) {
iv_node_p = mxmlFindElement(msg_p->header_node_p, msg_p->header_node_p, IV_NODE_NAME, NULL, NULL, MXML_DESCEND);
if (!iv_node_p) {
ret_val = OMEMO_ERR_MALFORMED_XML;
goto cleanup;
}
......
......@@ -790,6 +790,35 @@ void test_message_get_encrypted_key(void ** state) {
omemo_message_destroy(msg_p);
}
void test_message_get_encrypted_key_after_iv(void ** state) {
(void) state;
char * msg = "<message to='bob@example.com' from='alice@example.com'>"
"<encrypted xmlns='urn:xmpp:omemo:0'>"
"<header sid='1111'>"
"<iv>BASE64ENCODED</iv>"
"<key rid='2222'>sWsAtQ==</key>"
"</header>"
"<payload>BASE64ENCODED</payload>"
"</encrypted>"
"<store xmlns='urn:xmpp:hints'/>"
"</message>";
omemo_message * msg_p;
assert_int_equal(omemo_message_prepare_decryption(msg, &msg_p), 0);
uint8_t * key_p;
size_t key_len;
assert_int_equal(omemo_message_get_encrypted_key(msg_p, 1111, &key_p, &key_len), 0);
assert_ptr_equal(key_p, (void *) 0);
assert_int_equal(omemo_message_get_encrypted_key(msg_p, 2222, &key_p, &key_len), 0);
assert_int_equal(key_len, 4);
assert_memory_equal(key_p, data, key_len);
omemo_message_destroy(msg_p);
}
void test_message_get_encrypted_key_no_keys(void ** state) {
(void) state;
......@@ -1324,6 +1353,7 @@ int main(void) {
cmocka_unit_test(test_message_prepare_encryption_with_extra_data),
cmocka_unit_test(test_message_get_key),
cmocka_unit_test(test_message_get_encrypted_key),
cmocka_unit_test(test_message_get_encrypted_key_after_iv),
cmocka_unit_test(test_message_get_encrypted_key_no_keys),
cmocka_unit_test(test_message_add_recipient),
cmocka_unit_test(test_message_export_encrypted),
......