The local protocol gets media-key: each side sends its ephemeral X25519 public key (base64) and UDP port over the ready text connection; both derive the direction keys with HKDF-SHA256, the caller is the initiator. localui sends the key on Conversation > Call, answers an offer with its own key, creates the call with dc_call_new_bound on the listener port (TLS and media share the number), rings, and opens the call window; an incoming ring enables Answer. The Xt loop pumps the call: drain, Opus decode to the playback stream, capture and encode only while our own PTT is held, video frames to the window, ring timeout. dc_local_conn_remote_addr gives the numeric peer address; dc_call_new_bound binds the announced port. dc_call_talking() is now only our own key, dc_call_peer_talking() the peer's (a received PTT used to start our microphone too). The call window has an Answer button and lays the buttons above the video so they stay visible. --call-first and DTCHAT_TEST_TRUST/DTCHAT_TEST_AUTOANSWER/DTCHAT_TEST_PTT drive the test: two UI instances auto-paired, exchanged media-key, rang, answered, reached IN_CALL and carried 200 Opus frames caller->callee with the callee silent (audio out 200 on the receiver, audio in 200 on the sender); screenshot build/mcde-shots/dtchat-t65-live.png. The smoke still passes; test_media_call uses peer_talking; test_media_packet covers the base64 key exchange. The test switch is documented in the security review.
335 lines
8.8 KiB
C
335 lines
8.8 KiB
C
/*
|
|
* test_media_packet.c -- tests for the media packets (T6.3)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static int fails;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
|
|
#cond); \
|
|
fails++; \
|
|
} \
|
|
} while (0)
|
|
|
|
struct Session {
|
|
DcMediaKeys initiator;
|
|
DcMediaKeys responder;
|
|
DcMediaSend snd;
|
|
DcMediaRecv rcv;
|
|
};
|
|
|
|
static int
|
|
session_new(struct Session *s)
|
|
{
|
|
unsigned char pub_a[DC_MEDIA_KEY_LEN], priv_a[DC_MEDIA_KEY_LEN];
|
|
unsigned char pub_b[DC_MEDIA_KEY_LEN], priv_b[DC_MEDIA_KEY_LEN];
|
|
|
|
CHECK(dc_media_keypair(pub_a, priv_a) == 0);
|
|
CHECK(dc_media_keypair(pub_b, priv_b) == 0);
|
|
CHECK(dc_media_keys_agree(priv_a, pub_b, 1, &s->initiator) == 0);
|
|
CHECK(dc_media_keys_agree(priv_b, pub_a, 0, &s->responder) == 0);
|
|
/* the initiator's send key is the responder's receive key */
|
|
CHECK(memcmp(s->initiator.send, s->responder.recv,
|
|
DC_MEDIA_KEY_LEN) == 0);
|
|
CHECK(memcmp(s->initiator.recv, s->responder.send,
|
|
DC_MEDIA_KEY_LEN) == 0);
|
|
CHECK(memcmp(s->initiator.send, s->initiator.recv,
|
|
DC_MEDIA_KEY_LEN) != 0);
|
|
dc_media_send_init(&s->snd, s->initiator.send);
|
|
dc_media_recv_init(&s->rcv, s->responder.recv);
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
test_roundtrip(void)
|
|
{
|
|
struct Session s;
|
|
unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64];
|
|
unsigned char plain[DC_MEDIA_MAX_PAYLOAD];
|
|
unsigned char got[DC_MEDIA_MAX_PAYLOAD];
|
|
size_t len;
|
|
size_t gotlen = 0;
|
|
int type = 0;
|
|
uint32_t ts = 0;
|
|
int i;
|
|
|
|
if (!session_new(&s))
|
|
return;
|
|
for (i = 0; i < (int)sizeof(plain); i++)
|
|
plain[i] = (unsigned char)(i * 7 + 3);
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 1234, plain, 160, packet,
|
|
sizeof(packet));
|
|
CHECK(len == 12 + 160 + DC_MEDIA_TAG_LEN);
|
|
CHECK(dc_media_open(&s.rcv, packet, len, &type, &ts, got, &gotlen)
|
|
== 1);
|
|
CHECK(type == DC_MEDIA_AUDIO);
|
|
CHECK(ts == 1234);
|
|
CHECK(gotlen == 160);
|
|
CHECK(memcmp(plain, got, gotlen) == 0);
|
|
|
|
/* zero length works too */
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_PING, 1, NULL, 0, packet,
|
|
sizeof(packet));
|
|
CHECK(len == 12 + DC_MEDIA_TAG_LEN);
|
|
CHECK(dc_media_open(&s.rcv, packet, len, &type, &ts, got, &gotlen)
|
|
== 1);
|
|
CHECK(type == DC_MEDIA_PING);
|
|
CHECK(gotlen == 0);
|
|
}
|
|
|
|
static void
|
|
test_b64(void)
|
|
{
|
|
unsigned char priv_a[32], priv_b[32];
|
|
char b64_a[DC_MEDIA_PUB_B64_LEN], b64_b[DC_MEDIA_PUB_B64_LEN];
|
|
DcMediaKeys ka, kb;
|
|
unsigned char packet[128];
|
|
unsigned char plain[16] = "hello";
|
|
unsigned char got[16];
|
|
size_t len, gotlen = 0;
|
|
int type;
|
|
|
|
CHECK(dc_media_keypair_b64(b64_a, priv_a) == 0);
|
|
CHECK(dc_media_keypair_b64(b64_b, priv_b) == 0);
|
|
CHECK(strlen(b64_a) == 44);
|
|
CHECK(dc_media_keys_from_b64(priv_a, b64_b, 1, &ka) == 0);
|
|
CHECK(dc_media_keys_from_b64(priv_b, b64_a, 0, &kb) == 0);
|
|
CHECK(memcmp(ka.send, kb.recv, 32) == 0);
|
|
CHECK(memcmp(ka.recv, kb.send, 32) == 0);
|
|
|
|
{
|
|
DcMediaSend snd;
|
|
DcMediaRecv rcv;
|
|
|
|
dc_media_send_init(&snd, ka.send);
|
|
dc_media_recv_init(&rcv, kb.recv);
|
|
len = dc_media_seal(&snd, DC_MEDIA_AUDIO, 0, plain, 5, packet,
|
|
sizeof(packet));
|
|
CHECK(len > 0);
|
|
CHECK(dc_media_open(&rcv, packet, len, &type, NULL, got,
|
|
&gotlen) == 1);
|
|
CHECK(gotlen == 5 && memcmp(got, plain, 5) == 0);
|
|
}
|
|
CHECK(dc_media_keys_from_b64(priv_a, "not-base64", 1, &ka) == -1);
|
|
CHECK(dc_media_keys_from_b64(priv_a, "", 1, &ka) == -1);
|
|
}
|
|
|
|
static void
|
|
test_tamper(void)
|
|
{
|
|
struct Session s;
|
|
unsigned char packet[512];
|
|
unsigned char got[512];
|
|
unsigned char save;
|
|
size_t len;
|
|
size_t gotlen;
|
|
int type;
|
|
|
|
if (!session_new(&s))
|
|
return;
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_VIDEO, 99, "payload", 7, packet,
|
|
sizeof(packet));
|
|
CHECK(len > 0);
|
|
|
|
/* a changed header (the sequence is additional data) */
|
|
save = packet[7];
|
|
packet[7] ^= 1;
|
|
CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen)
|
|
== -1);
|
|
packet[7] = save;
|
|
|
|
/* a changed payload */
|
|
save = packet[12];
|
|
packet[12] ^= 0x80;
|
|
CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen)
|
|
== -1);
|
|
packet[12] = save;
|
|
|
|
/* a changed tag */
|
|
save = packet[len - 1];
|
|
packet[len - 1] ^= 1;
|
|
CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen)
|
|
== -1);
|
|
packet[len - 1] = save;
|
|
|
|
/* truncated */
|
|
CHECK(dc_media_open(&s.rcv, packet, len - 1, &type, NULL, got,
|
|
&gotlen) == -1);
|
|
CHECK(dc_media_open(&s.rcv, packet, 10, &type, NULL, got, &gotlen)
|
|
== -1);
|
|
|
|
/* the unchanged packet still opens */
|
|
CHECK(dc_media_open(&s.rcv, packet, len, &type, NULL, got, &gotlen)
|
|
== 1);
|
|
|
|
/* a wrong key is refused */
|
|
{
|
|
DcMediaRecv wrong;
|
|
unsigned char key[DC_MEDIA_KEY_LEN];
|
|
|
|
memset(key, 0x42, sizeof(key));
|
|
dc_media_recv_init(&wrong, key);
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 1, "x", 1, packet,
|
|
sizeof(packet));
|
|
CHECK(dc_media_open(&wrong, packet, len, &type, NULL, got,
|
|
&gotlen) == -1);
|
|
}
|
|
}
|
|
|
|
static void
|
|
test_replay(void)
|
|
{
|
|
struct Session s;
|
|
unsigned char got[256];
|
|
size_t gotlen;
|
|
int i;
|
|
int type;
|
|
uint32_t ts;
|
|
|
|
if (!session_new(&s))
|
|
return;
|
|
/* seal five, deliver them out of order */
|
|
{
|
|
unsigned char packets[5][256];
|
|
size_t lens[5];
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
lens[i] = dc_media_seal(&s.snd, DC_MEDIA_AUDIO,
|
|
(uint32_t)i, &i, sizeof i, packets[i],
|
|
sizeof packets[i]);
|
|
CHECK(lens[i] > 0);
|
|
}
|
|
CHECK(dc_media_open(&s.rcv, packets[4], lens[4], &type, &ts,
|
|
got, &gotlen) == 1);
|
|
CHECK(dc_media_open(&s.rcv, packets[3], lens[3], &type, &ts,
|
|
got, &gotlen) == 1);
|
|
CHECK(dc_media_open(&s.rcv, packets[1], lens[1], &type, &ts,
|
|
got, &gotlen) == 1);
|
|
/* the same packet twice is a replay */
|
|
CHECK(dc_media_open(&s.rcv, packets[3], lens[3], &type, &ts,
|
|
got, &gotlen) == -1);
|
|
CHECK(dc_media_open(&s.rcv, packets[1], lens[1], &type, &ts,
|
|
got, &gotlen) == -1);
|
|
CHECK(dc_media_open(&s.rcv, packets[2], lens[2], &type, &ts,
|
|
got, &gotlen) == 1);
|
|
CHECK(dc_media_open(&s.rcv, packets[0], lens[0], &type, &ts,
|
|
got, &gotlen) == 1);
|
|
}
|
|
|
|
/* the window: 100 packets, then an old one outside it */
|
|
{
|
|
DcMediaRecv win;
|
|
unsigned char key[DC_MEDIA_KEY_LEN];
|
|
uint64_t base = 1000;
|
|
|
|
memset(key, 9, sizeof(key));
|
|
dc_media_recv_init(&win, key);
|
|
CHECK(dc_media_replay_ok(&win, (uint32_t)base) == 1);
|
|
(void)base;
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 64) == 1);
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 64 - 63) == 1);
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 64 - 63) == 0);
|
|
/* 64 packets behind the highest is outside the window */
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 64 - 64) == 0);
|
|
CHECK(dc_media_replay_ok(&win, 1000 - 1) == 0);
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 65) == 1);
|
|
/* the move kept 1064, dropped everything at or below 1001 */
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 64) == 0);
|
|
CHECK(dc_media_replay_ok(&win, 1001) == 0);
|
|
CHECK(dc_media_replay_ok(&win, 1000 + 63) == 1);
|
|
}
|
|
}
|
|
|
|
static void
|
|
test_limits(void)
|
|
{
|
|
struct Session s;
|
|
unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64];
|
|
unsigned char big[DC_MEDIA_MAX_PAYLOAD + 1];
|
|
size_t len;
|
|
|
|
if (!session_new(&s))
|
|
return;
|
|
memset(big, 'a', sizeof(big));
|
|
/* too much for the buffer */
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 0, big, 100, packet, 50);
|
|
CHECK(len == 0);
|
|
/* more than a packet carries */
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_AUDIO, 0, big, sizeof(big),
|
|
packet, sizeof(packet));
|
|
CHECK(len == 0);
|
|
/* a bad type */
|
|
len = dc_media_seal(&s.snd, 0, 0, big, 10, packet, sizeof(packet));
|
|
CHECK(len == 0);
|
|
len = dc_media_seal(&s.snd, 99, 0, big, 10, packet, sizeof(packet));
|
|
CHECK(len == 0);
|
|
/* exactly the maximum */
|
|
len = dc_media_seal(&s.snd, DC_MEDIA_VIDEO, 0, big,
|
|
DC_MEDIA_MAX_PAYLOAD, packet, sizeof(packet));
|
|
CHECK(len == 12 + DC_MEDIA_MAX_PAYLOAD + DC_MEDIA_TAG_LEN);
|
|
}
|
|
|
|
static void
|
|
test_bulk(void)
|
|
{
|
|
struct Session s;
|
|
unsigned char packet[DC_MEDIA_MAX_PAYLOAD + 64];
|
|
unsigned char plain[DC_MEDIA_MAX_PAYLOAD];
|
|
unsigned char got[DC_MEDIA_MAX_PAYLOAD];
|
|
unsigned int x = 7;
|
|
size_t i;
|
|
int n;
|
|
|
|
if (!session_new(&s))
|
|
return;
|
|
for (n = 0; n < 1000; n++) {
|
|
size_t len = (size_t)(n * 37 % (DC_MEDIA_MAX_PAYLOAD + 1));
|
|
size_t gotlen = 0;
|
|
int type = 0;
|
|
|
|
x = x * 1103515245u + 12345u;
|
|
for (i = 0; i < len; i++) {
|
|
x = x * 1103515245u + 12345u;
|
|
plain[i] = (unsigned char)(x >> 16);
|
|
}
|
|
if (dc_media_seal(&s.snd, DC_MEDIA_AUDIO, (uint32_t)n, plain,
|
|
len, packet, sizeof(packet)) != 12 + len +
|
|
DC_MEDIA_TAG_LEN) {
|
|
CHECK(0);
|
|
return;
|
|
}
|
|
if (dc_media_open(&s.rcv, packet, 12 + len +
|
|
DC_MEDIA_TAG_LEN, &type, NULL, got, &gotlen) != 1 ||
|
|
gotlen != len || memcmp(plain, got, len) != 0) {
|
|
CHECK(0);
|
|
return;
|
|
}
|
|
}
|
|
CHECK(1);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_roundtrip();
|
|
test_b64();
|
|
test_tamper();
|
|
test_replay();
|
|
test_limits();
|
|
test_bulk();
|
|
if (fails != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", fails);
|
|
return 1;
|
|
}
|
|
printf("test_media_packet: all ok\n");
|
|
return 0;
|
|
}
|