dtchat: verify adapter files with SHA-256 (T4.5)
The file protocol now carries the sender's SHA-256: the adapter computes it with dc_file_sha256 for an outgoing transfer, puts it into the IBB open metadata (urn:dtchat:file:0 sha256 attribute) and into file-sent, and passes the attribute of an incoming open through in file-offer. The UI stores the offered hash, hashes the part file at file-end and only renames when it matches; a mismatch removes the part file, reports 'file rejected: checksum mismatch' in the status line and the transcript, and never exposes the bytes under the final name. The fake adapter offers the correct hash in mode files and a wrong one in mode badhash. tests/xmpp_ibb.py checks the SHA-256 in the outgoing open and in the incoming offer. Verified stand-alone under Xvfb: mode files writes the 11-byte hello world, mode badhash leaves no file and no .part; the XMPP IBB suite passes with the hash checks. In-tree build and harness follow.
This commit is contained in:
1 parent
48eae8eadb
commit
fc23161a4e
4 files changed
+67
-13
No files matched your search
@@ -51,6 +51,9 @@ static void
|
||||
send_file_offer(void)
|
||||
{
|
||||
DcJson *json = dc_json_new_object();
|
||||
const char *sha = strcmp(mode, "badhash") == 0 ?
|
||||
"0000000000000000000000000000000000000000000000000000000000000000" :
|
||||
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
|
||||
|
||||
(void)dc_json_object_set(json, "type",
|
||||
dc_json_new_string("file-offer"));
|
||||
@@ -58,6 +61,7 @@ send_file_offer(void)
|
||||
(void)dc_json_object_set(json, "id", dc_json_new_string("f1"));
|
||||
(void)dc_json_object_set(json, "name", dc_json_new_string("note.txt"));
|
||||
(void)dc_json_object_set(json, "size", dc_json_new_int(11));
|
||||
(void)dc_json_object_set(json, "sha256", dc_json_new_string(sha));
|
||||
send_json(json);
|
||||
}
|
||||
|
||||
@@ -106,7 +110,8 @@ handle(DcJson *cmd)
|
||||
_exit(1);
|
||||
}
|
||||
send_state("online");
|
||||
if (strcmp(mode, "files") == 0)
|
||||
if (strcmp(mode, "files") == 0 ||
|
||||
strcmp(mode, "badhash") == 0)
|
||||
send_file_offer();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -54,6 +55,7 @@ struct Incoming {
|
||||
char conv[256];
|
||||
char final[600];
|
||||
char part[620];
|
||||
char sha256[65];
|
||||
FILE *fp;
|
||||
long long size;
|
||||
long long got;
|
||||
@@ -303,6 +305,7 @@ file_offer(DtAccounts *accounts, AccountRun *run, const DcJson *event)
|
||||
const char *id = dc_json_string(dc_json_get(event, "id"));
|
||||
const char *conv = dc_json_string(dc_json_get(event, "conv"));
|
||||
const char *name = dc_json_string(dc_json_get(event, "name"));
|
||||
const char *sha256 = dc_json_string(dc_json_get(event, "sha256"));
|
||||
long long size = dc_json_get(event, "size") != NULL ?
|
||||
dc_json_int(dc_json_get(event, "size")) : 0;
|
||||
Incoming *in;
|
||||
@@ -323,6 +326,8 @@ file_offer(DtAccounts *accounts, AccountRun *run, const DcJson *event)
|
||||
in->run = run;
|
||||
snprintf(in->id, sizeof(in->id), "%s", id);
|
||||
snprintf(in->conv, sizeof(in->conv), "%s", conv);
|
||||
snprintf(in->sha256, sizeof(in->sha256), "%s",
|
||||
sha256 != NULL ? sha256 : "");
|
||||
in->size = size;
|
||||
in->fp = fopen(in->part, "wb");
|
||||
if (in->fp == NULL) {
|
||||
@@ -388,6 +393,28 @@ file_end(DtAccounts *accounts, AccountRun *run, const DcJson *event)
|
||||
fclose(in->fp);
|
||||
in->fp = NULL;
|
||||
}
|
||||
if (in->sha256[0] != '\0') {
|
||||
char got[65];
|
||||
|
||||
if (dc_file_sha256(in->part, got, NULL) != 0 ||
|
||||
strcasecmp(got, in->sha256) != 0) {
|
||||
(void)unlink(in->part);
|
||||
set_account_status(accounts, run,
|
||||
"file rejected: checksum mismatch");
|
||||
convp = dc_model_add_conv(accounts->model, run->id,
|
||||
in->conv, DC_CONV_DIRECT, in->conv);
|
||||
if (convp != NULL) {
|
||||
(void)dc_conv_add_message(convp, "system",
|
||||
"file rejected: checksum mismatch",
|
||||
(long long)time(NULL),
|
||||
DTCHAT_MSG_SYSTEM);
|
||||
dtchat_ui_message_added(accounts->ui,
|
||||
run->id, in->conv);
|
||||
}
|
||||
incoming_free(accounts, in, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (rename(in->part, in->final) != 0) {
|
||||
snprintf(line, sizeof(line), "cannot rename %s", in->part);
|
||||
set_account_status(accounts, run, line);
|
||||
|
||||
@@ -332,6 +332,7 @@ typedef struct {
|
||||
char id[64];
|
||||
char conv[256];
|
||||
char name[256];
|
||||
char sha256[65];
|
||||
FILE *fp;
|
||||
unsigned long long size;
|
||||
unsigned long long sent;
|
||||
@@ -348,6 +349,7 @@ typedef struct {
|
||||
char from[256];
|
||||
char iqid[64];
|
||||
char name[256];
|
||||
char sha256[65];
|
||||
unsigned long long size;
|
||||
unsigned long long got;
|
||||
int state; /* 1 offered, 2 streaming */
|
||||
@@ -591,6 +593,7 @@ out_open(OutFile *o)
|
||||
xmpp_stanza_set_ns(file, FILE_NS);
|
||||
xmpp_stanza_set_attribute(file, "name", o->name);
|
||||
xmpp_stanza_set_attribute(file, "size", size);
|
||||
xmpp_stanza_set_attribute(file, "sha256", o->sha256);
|
||||
xmpp_stanza_add_child(open, file);
|
||||
xmpp_stanza_release(file);
|
||||
}
|
||||
@@ -610,6 +613,7 @@ file_send_start(const DcJson *cmd)
|
||||
const char *path = dc_json_string(dc_json_get(cmd, "path"));
|
||||
const char *name = dc_json_string(dc_json_get(cmd, "name"));
|
||||
OutFile *o = NULL;
|
||||
long long hashed_size = 0;
|
||||
long size;
|
||||
int i;
|
||||
|
||||
@@ -631,22 +635,20 @@ file_send_start(const DcJson *cmd)
|
||||
return;
|
||||
}
|
||||
memset(o, 0, sizeof(*o));
|
||||
if (dc_file_sha256(path, o->sha256, &hashed_size) != 0) {
|
||||
file_error(id, "cannot read the file");
|
||||
return;
|
||||
}
|
||||
if (hashed_size > IBB_FILE_MAX) {
|
||||
file_error(id, "the file is larger than 32 MiB");
|
||||
return;
|
||||
}
|
||||
o->fp = fopen(path, "rb");
|
||||
if (o->fp == NULL) {
|
||||
file_error(id, "cannot open the file");
|
||||
return;
|
||||
}
|
||||
if (fseek(o->fp, 0, SEEK_END) != 0 ||
|
||||
(size = ftell(o->fp)) < 0 || fseek(o->fp, 0, SEEK_SET) != 0) {
|
||||
fclose(o->fp);
|
||||
file_error(id, "cannot measure the file");
|
||||
return;
|
||||
}
|
||||
if ((unsigned long)size > IBB_FILE_MAX) {
|
||||
fclose(o->fp);
|
||||
file_error(id, "the file is larger than 32 MiB");
|
||||
return;
|
||||
}
|
||||
size = (long)hashed_size;
|
||||
o->used = 1;
|
||||
snprintf(o->id, sizeof(o->id), "%s", id);
|
||||
snprintf(o->conv, sizeof(o->conv), "%s", conv);
|
||||
@@ -683,6 +685,8 @@ out_iq_result(OutFile *o, int error)
|
||||
dc_json_new_string(o->id));
|
||||
(void)dc_json_object_set(json, "size",
|
||||
dc_json_new_int((long long)o->size));
|
||||
(void)dc_json_object_set(json, "sha256",
|
||||
dc_json_new_string(o->sha256));
|
||||
send_json(json);
|
||||
/* the close result only acknowledges; the transfer
|
||||
* itself is done */
|
||||
@@ -702,6 +706,7 @@ in_offer(xmpp_stanza_t *stanza, xmpp_stanza_t *open)
|
||||
xmpp_stanza_t *file;
|
||||
const char *name;
|
||||
const char *size;
|
||||
const char *sha256;
|
||||
InFile *in = NULL;
|
||||
int i;
|
||||
|
||||
@@ -729,9 +734,13 @@ in_offer(xmpp_stanza_t *stanza, xmpp_stanza_t *open)
|
||||
file = xmpp_stanza_get_child_by_name_and_ns(open, "file", FILE_NS);
|
||||
name = file != NULL ? xmpp_stanza_get_attribute(file, "name") : NULL;
|
||||
size = file != NULL ? xmpp_stanza_get_attribute(file, "size") : NULL;
|
||||
sha256 = file != NULL ? xmpp_stanza_get_attribute(file, "sha256") :
|
||||
NULL;
|
||||
snprintf(in->name, sizeof(in->name), "%s",
|
||||
name != NULL ? name : sid);
|
||||
in->size = size != NULL ? strtoull(size, NULL, 10) : 0;
|
||||
snprintf(in->sha256, sizeof(in->sha256), "%s",
|
||||
sha256 != NULL ? sha256 : "");
|
||||
if (in->size > IBB_FILE_MAX) {
|
||||
send_iq_error(from, iqid, "resource-constraint");
|
||||
file_reject_event(sid, "the file is larger than 32 MiB");
|
||||
@@ -754,6 +763,9 @@ in_offer(xmpp_stanza_t *stanza, xmpp_stanza_t *open)
|
||||
dc_json_new_string(in->name));
|
||||
(void)dc_json_object_set(json, "size",
|
||||
dc_json_new_int((long long)in->size));
|
||||
if (in->sha256[0] != '\0')
|
||||
(void)dc_json_object_set(json, "sha256",
|
||||
dc_json_new_string(in->sha256));
|
||||
send_json(json);
|
||||
}
|
||||
}
|
||||
|
||||
+11
-1
@@ -12,6 +12,7 @@ The server speaks stream, SASL PLAIN and bind, then:
|
||||
* offers a second file (s2) and checks that a reject becomes an IQ error.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
@@ -142,6 +143,11 @@ def server():
|
||||
fm = re.search(rb'<file[^>]*name=["\']([^"\']+)["\']', open_iq)
|
||||
check(fm is not None and fm.group(1).decode() == "payload.bin",
|
||||
"the open carries the file name")
|
||||
hm = re.search(rb'<file[^>]*sha256=["\']([0-9a-f]{64})["\']',
|
||||
open_iq)
|
||||
check(hm is not None and
|
||||
hm.group(1).decode() == hashlib.sha256(payload).hexdigest(),
|
||||
"the open carries the SHA-256")
|
||||
reply(open_iq)
|
||||
seq = 0
|
||||
while True:
|
||||
@@ -172,7 +178,9 @@ def server():
|
||||
b"<open xmlns='http://jabber.org/protocol/ibb' "
|
||||
b"sid='s1' block-size='4096'>"
|
||||
b"<file xmlns='urn:dtchat:file:0' name='note.txt' "
|
||||
b"size='11'/></open></iq>")
|
||||
b"size='11' sha256='" +
|
||||
hashlib.sha256(b"hello world").hexdigest().encode() +
|
||||
b"'/></open></iq>")
|
||||
result = read_iq_with(rb'id=["\']in1["\']')
|
||||
accepted.append(result is not None)
|
||||
check(result is not None, "the adapter answered the accepted offer")
|
||||
@@ -280,6 +288,8 @@ check(offer is not None, "file-offer for the incoming transfer")
|
||||
if offer is not None:
|
||||
check(offer.get("name") == "note.txt", "the offer has the name")
|
||||
check(offer.get("size") == 11, "the offer has the size")
|
||||
check(offer.get("sha256") == hashlib.sha256(b"hello world").hexdigest(),
|
||||
"the offer has the SHA-256")
|
||||
check(offer.get("conv") == "bob@example.org",
|
||||
"the offer is filed under the contact")
|
||||
send({"type": "file-accept", "id": "s1"})
|
||||
|
||||
Reference in new issue
Block a user