media_capture.c: V4L2 (YUYV, 4 mmap buffers, poll, QBUF again after use) and a test pattern; a camera with another size is scaled to the call's 640x360, and a build without HAVE_V4L2 gets the pattern. On this T14 the integrated camera is /dev/video0 and gives frames (test_capture: v4l2:/dev/video0, non-uniform frame, 320x180 through the scaler; pattern frames differ and have Y and chroma). configure defines HAVE_V4L2 for linux/videodev2.h. media_video.c gains dc_vpx_enc_keyframe (VPX_EFLAG_FORCE_KF on the next frame), media_packet a DC_MEDIA_KFREQ control, media_call dc_call_request_keyframe/take and DC_MEDIA_KFREQ in the sealed type range. dc_call_video_out now decodes the reassembled VP8 frame into I420 (it returned the compressed frame, which the UI drew as a green picture); dc_call_video_packet keeps the compressed frame for tests and recordings. Video timestamps are wall clock, not frame counts, so a pump slower than 15 fps does not drift behind the audio clock. localui starts camera+encoder on entering the call, sends 15 fps, requests a keyframe after a loss, forces one when asked, decodes incoming frames into the call window, drains up to four 20 ms capture frames per 40 ms pump (one per pump halved the audio clock), and logs video in/out under DTCHAT_CALL_DEBUG; DTCHAT_TEST_VIDEO switches to the pattern. Two UIs under Xvfb: A pattern, B camera, 90 frames each way in about 6 s, audio 300 frames (real time), the window shows the peer's picture (build/mcde-shots/dtchat-t71-video-call.png). test_video_packet uses the packet interface; its 50 KB/42-fragment loopback check still passes. New harness entry DtChat/test_capture (43). In-tree build pending: a foreign make release holds the lock.
385 lines
10 KiB
C
385 lines
10 KiB
C
/*
|
|
* media_packet.c -- dtchat's media packets: X25519, HKDF, ChaCha20-Poly1305
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* One session derives two 32-byte keys from an X25519 exchange and HKDF-
|
|
* SHA256; the initiator sends with the first and receives with the second,
|
|
* the responder the other way round. A packet is a 12-byte header in the
|
|
* clear (version, type, flags, sequence, timestamp), the encrypted payload
|
|
* and a 16-byte tag; the header is the AEAD's additional data, so a changed
|
|
* sequence or timestamp is refused. The nonce is the 64-bit sequence in
|
|
* big-endian after four zero bytes: keys are fresh per session, so no
|
|
* nonce is ever reused.
|
|
*
|
|
* byte 0 version (1)
|
|
* byte 1 type (DC_MEDIA_AUDIO, _VIDEO, _PING, _BYE)
|
|
* byte 2 flags (0 for now)
|
|
* byte 3 reserved (0)
|
|
* bytes 4..7 sequence, big-endian
|
|
* bytes 8..11 timestamp in ms, big-endian, 32 bit
|
|
* payload ..., 16-byte tag
|
|
*
|
|
* The replay window is the 64-packet sliding window of RFC 3711: a packet
|
|
* inside the window that was seen before is refused, an old one outside it
|
|
* too, and the window follows the highest sequence seen.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <openssl/evp.h>
|
|
#include <openssl/kdf.h>
|
|
#include <openssl/rand.h>
|
|
|
|
#define HDR_LEN 12
|
|
|
|
static void
|
|
put_u32(unsigned char *p, uint32_t v)
|
|
{
|
|
p[0] = (unsigned char)(v >> 24);
|
|
p[1] = (unsigned char)(v >> 16);
|
|
p[2] = (unsigned char)(v >> 8);
|
|
p[3] = (unsigned char)v;
|
|
}
|
|
|
|
static uint32_t
|
|
get_u32(const unsigned char *p)
|
|
{
|
|
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
|
|
((uint32_t)p[2] << 8) | (uint32_t)p[3];
|
|
}
|
|
|
|
static void
|
|
put_u64(unsigned char *p, uint64_t v)
|
|
{
|
|
int i;
|
|
|
|
for (i = 7; i >= 0; i--) {
|
|
p[i] = (unsigned char)(v & 0xff);
|
|
v >>= 8;
|
|
}
|
|
}
|
|
|
|
int
|
|
dc_media_keypair(unsigned char pub[DC_MEDIA_KEY_LEN],
|
|
unsigned char priv[DC_MEDIA_KEY_LEN])
|
|
{
|
|
EVP_PKEY *key;
|
|
size_t publen = DC_MEDIA_KEY_LEN;
|
|
size_t privlen = DC_MEDIA_KEY_LEN;
|
|
|
|
if (pub == NULL || priv == NULL)
|
|
return -1;
|
|
if (RAND_bytes(priv, DC_MEDIA_KEY_LEN) != 1)
|
|
return -1;
|
|
key = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, priv,
|
|
DC_MEDIA_KEY_LEN);
|
|
if (key == NULL)
|
|
return -1;
|
|
if (EVP_PKEY_get_raw_public_key(key, pub, &publen) != 1 ||
|
|
publen != DC_MEDIA_KEY_LEN) {
|
|
EVP_PKEY_free(key);
|
|
return -1;
|
|
}
|
|
EVP_PKEY_free(key);
|
|
(void)privlen;
|
|
return 0;
|
|
}
|
|
|
|
/* HKDF-SHA256, 64 bytes out: the two directions of one session */
|
|
static int
|
|
derive(const unsigned char secret[DC_MEDIA_KEY_LEN],
|
|
const unsigned char pub_a[DC_MEDIA_KEY_LEN],
|
|
const unsigned char pub_b[DC_MEDIA_KEY_LEN], unsigned char out[64])
|
|
{
|
|
static const char info[] = "dtchat media v1";
|
|
unsigned char salt[DC_MEDIA_KEY_LEN * 2];
|
|
unsigned char *p = salt;
|
|
EVP_PKEY_CTX *ctx;
|
|
size_t outlen = 64;
|
|
int rc = -1;
|
|
|
|
/* the public keys, sorted, make the salt of the session */
|
|
if (memcmp(pub_a, pub_b, DC_MEDIA_KEY_LEN) <= 0) {
|
|
memcpy(p, pub_a, DC_MEDIA_KEY_LEN);
|
|
memcpy(p + DC_MEDIA_KEY_LEN, pub_b, DC_MEDIA_KEY_LEN);
|
|
} else {
|
|
memcpy(p, pub_b, DC_MEDIA_KEY_LEN);
|
|
memcpy(p + DC_MEDIA_KEY_LEN, pub_a, DC_MEDIA_KEY_LEN);
|
|
}
|
|
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
|
|
if (ctx == NULL)
|
|
return -1;
|
|
if (EVP_PKEY_derive_init(ctx) == 1 &&
|
|
EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) == 1 &&
|
|
EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)) == 1 &&
|
|
EVP_PKEY_CTX_set1_hkdf_key(ctx, secret, DC_MEDIA_KEY_LEN) == 1 &&
|
|
EVP_PKEY_CTX_add1_hkdf_info(ctx, (const unsigned char *)info,
|
|
sizeof(info) - 1) == 1 &&
|
|
EVP_PKEY_derive(ctx, out, &outlen) == 1 && outlen == 64)
|
|
rc = 0;
|
|
EVP_PKEY_CTX_free(ctx);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
dc_media_keys_agree(const unsigned char priv[DC_MEDIA_KEY_LEN],
|
|
const unsigned char peer_pub[DC_MEDIA_KEY_LEN],
|
|
int initiator, DcMediaKeys *keys)
|
|
{
|
|
EVP_PKEY *mine = NULL;
|
|
EVP_PKEY *theirs = NULL;
|
|
EVP_PKEY_CTX *ctx = NULL;
|
|
unsigned char secret[DC_MEDIA_KEY_LEN];
|
|
unsigned char my_pub[DC_MEDIA_KEY_LEN];
|
|
unsigned char both[64];
|
|
size_t seclen = sizeof(secret);
|
|
size_t mylen = sizeof(my_pub);
|
|
int rc = -1;
|
|
|
|
if (priv == NULL || peer_pub == NULL || keys == NULL)
|
|
return -1;
|
|
memset(keys, 0, sizeof(*keys));
|
|
mine = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, priv,
|
|
DC_MEDIA_KEY_LEN);
|
|
theirs = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, peer_pub,
|
|
DC_MEDIA_KEY_LEN);
|
|
if (mine == NULL || theirs == NULL)
|
|
goto out;
|
|
if (EVP_PKEY_get_raw_public_key(mine, my_pub, &mylen) != 1 ||
|
|
mylen != DC_MEDIA_KEY_LEN)
|
|
goto out;
|
|
ctx = EVP_PKEY_CTX_new(mine, NULL);
|
|
if (ctx == NULL || EVP_PKEY_derive_init(ctx) != 1 ||
|
|
EVP_PKEY_derive_set_peer(ctx, theirs) != 1 ||
|
|
EVP_PKEY_derive(ctx, secret, &seclen) != 1 ||
|
|
seclen != DC_MEDIA_KEY_LEN)
|
|
goto out;
|
|
if (derive(secret, my_pub, peer_pub, both) != 0)
|
|
goto out;
|
|
if (initiator) {
|
|
memcpy(keys->send, both, DC_MEDIA_KEY_LEN);
|
|
memcpy(keys->recv, both + DC_MEDIA_KEY_LEN, DC_MEDIA_KEY_LEN);
|
|
} else {
|
|
memcpy(keys->recv, both, DC_MEDIA_KEY_LEN);
|
|
memcpy(keys->send, both + DC_MEDIA_KEY_LEN, DC_MEDIA_KEY_LEN);
|
|
}
|
|
rc = 0;
|
|
out:
|
|
OPENSSL_cleanse(secret, sizeof(secret));
|
|
OPENSSL_cleanse(both, sizeof(both));
|
|
EVP_PKEY_CTX_free(ctx);
|
|
EVP_PKEY_free(mine);
|
|
EVP_PKEY_free(theirs);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
dc_media_keypair_b64(char pub[DC_MEDIA_PUB_B64_LEN],
|
|
unsigned char priv[DC_MEDIA_KEY_LEN])
|
|
{
|
|
unsigned char raw[DC_MEDIA_KEY_LEN];
|
|
|
|
if (pub == NULL || priv == NULL)
|
|
return -1;
|
|
if (dc_media_keypair(raw, priv) != 0)
|
|
return -1;
|
|
EVP_EncodeBlock((unsigned char *)pub, raw, DC_MEDIA_KEY_LEN);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
dc_media_keys_from_b64(const unsigned char priv[DC_MEDIA_KEY_LEN],
|
|
const char *peer_pub_b64, int initiator, DcMediaKeys *keys)
|
|
{
|
|
unsigned char raw[DC_MEDIA_KEY_LEN + 3];
|
|
size_t len;
|
|
int n;
|
|
|
|
if (priv == NULL || peer_pub_b64 == NULL || keys == NULL)
|
|
return -1;
|
|
len = strlen(peer_pub_b64);
|
|
if (len != 44 || len % 4 != 0)
|
|
return -1;
|
|
n = EVP_DecodeBlock(raw, (const unsigned char *)peer_pub_b64, (int)len);
|
|
if (n < 0)
|
|
return -1;
|
|
if (n == DC_MEDIA_KEY_LEN + 1 && peer_pub_b64[len - 1] == '=')
|
|
n = DC_MEDIA_KEY_LEN; /* the padding byte */
|
|
if (n != DC_MEDIA_KEY_LEN)
|
|
return -1;
|
|
return dc_media_keys_agree(priv, raw, initiator, keys);
|
|
}
|
|
|
|
void
|
|
dc_media_send_init(DcMediaSend *send, const unsigned char key[DC_MEDIA_KEY_LEN])
|
|
{
|
|
if (send == NULL)
|
|
return;
|
|
memset(send, 0, sizeof(*send));
|
|
if (key != NULL)
|
|
memcpy(send->key, key, DC_MEDIA_KEY_LEN);
|
|
}
|
|
|
|
void
|
|
dc_media_recv_init(DcMediaRecv *recv, const unsigned char key[DC_MEDIA_KEY_LEN])
|
|
{
|
|
if (recv == NULL)
|
|
return;
|
|
memset(recv, 0, sizeof(*recv));
|
|
if (key != NULL)
|
|
memcpy(recv->key, key, DC_MEDIA_KEY_LEN);
|
|
}
|
|
|
|
size_t
|
|
dc_media_seal(DcMediaSend *send, int type, uint32_t ts, const void *plain,
|
|
size_t len, unsigned char *out, size_t outlen)
|
|
{
|
|
EVP_CIPHER_CTX *ctx = NULL;
|
|
unsigned char nonce[12];
|
|
unsigned char *payload;
|
|
int n = 0;
|
|
int total = 0;
|
|
size_t need;
|
|
|
|
if (send == NULL || out == NULL ||
|
|
(len > 0 && plain == NULL) || len > DC_MEDIA_MAX_PAYLOAD)
|
|
return 0;
|
|
if (type < DC_MEDIA_AUDIO || type > DC_MEDIA_KFREQ)
|
|
return 0;
|
|
need = HDR_LEN + len + DC_MEDIA_TAG_LEN;
|
|
if (outlen < need)
|
|
return 0;
|
|
out[0] = DC_MEDIA_VERSION;
|
|
out[1] = (unsigned char)type;
|
|
out[2] = 0;
|
|
out[3] = 0;
|
|
put_u32(out + 4, (uint32_t)send->seq);
|
|
put_u32(out + 8, ts);
|
|
payload = out + HDR_LEN;
|
|
|
|
memset(nonce, 0, sizeof(nonce));
|
|
put_u64(nonce + 4, send->seq);
|
|
ctx = EVP_CIPHER_CTX_new();
|
|
if (ctx == NULL)
|
|
return 0;
|
|
if (EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
|
|
NULL) != 1 ||
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(nonce),
|
|
NULL) != 1 ||
|
|
EVP_EncryptInit_ex(ctx, NULL, NULL, send->key, nonce) != 1 ||
|
|
EVP_EncryptUpdate(ctx, NULL, &n, out, HDR_LEN) != 1 ||
|
|
EVP_EncryptUpdate(ctx, payload, &n, plain != NULL ? plain :
|
|
(const unsigned char *)"", (int)len) != 1) {
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
return 0;
|
|
}
|
|
total = n;
|
|
if (EVP_EncryptFinal_ex(ctx, payload + total, &n) != 1 ||
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, DC_MEDIA_TAG_LEN,
|
|
payload + len) != 1) {
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
return 0;
|
|
}
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
send->seq++;
|
|
return need;
|
|
}
|
|
|
|
int
|
|
dc_media_open(DcMediaRecv *recv, const unsigned char *packet, size_t len,
|
|
int *type, uint32_t *ts, void *plain, size_t *plainlen)
|
|
{
|
|
EVP_CIPHER_CTX *ctx = NULL;
|
|
unsigned char nonce[12];
|
|
unsigned char *payload;
|
|
uint32_t seq;
|
|
size_t paylen;
|
|
int n = 0;
|
|
int total = 0;
|
|
int ok = 0;
|
|
|
|
if (recv == NULL || packet == NULL || plain == NULL || plainlen == NULL)
|
|
return -1;
|
|
if (len < HDR_LEN + DC_MEDIA_TAG_LEN)
|
|
return -1;
|
|
if (packet[0] != DC_MEDIA_VERSION || packet[2] != 0 || packet[3] != 0)
|
|
return -1;
|
|
if (packet[1] < DC_MEDIA_AUDIO || packet[1] > DC_MEDIA_KFREQ)
|
|
return -1;
|
|
seq = get_u32(packet + 4);
|
|
paylen = len - HDR_LEN - DC_MEDIA_TAG_LEN;
|
|
if (paylen > DC_MEDIA_MAX_PAYLOAD)
|
|
return -1;
|
|
payload = (unsigned char *)packet + HDR_LEN;
|
|
|
|
memset(nonce, 0, sizeof(nonce));
|
|
put_u64(nonce + 4, seq);
|
|
ctx = EVP_CIPHER_CTX_new();
|
|
if (ctx == NULL)
|
|
return -1;
|
|
if (EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
|
|
NULL) != 1 ||
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(nonce),
|
|
NULL) != 1 ||
|
|
EVP_DecryptInit_ex(ctx, NULL, NULL, recv->key, nonce) != 1 ||
|
|
EVP_DecryptUpdate(ctx, NULL, &n, packet, HDR_LEN) != 1 ||
|
|
EVP_DecryptUpdate(ctx, (unsigned char *)plain, &n, payload,
|
|
(int)paylen) != 1) {
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
return -1;
|
|
}
|
|
total = n;
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, DC_MEDIA_TAG_LEN,
|
|
payload + paylen) != 1 ||
|
|
EVP_DecryptFinal_ex(ctx, (unsigned char *)plain + total, &n) != 1)
|
|
goto out;
|
|
total += n;
|
|
if (dc_media_replay_ok(recv, seq) != 1)
|
|
goto out;
|
|
if (type != NULL)
|
|
*type = packet[1];
|
|
if (ts != NULL)
|
|
*ts = get_u32(packet + 8);
|
|
*plainlen = (size_t)total;
|
|
ok = 1;
|
|
out:
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
return ok ? 1 : -1;
|
|
}
|
|
|
|
int
|
|
dc_media_replay_ok(DcMediaRecv *recv, uint32_t seq)
|
|
{
|
|
uint64_t s = seq;
|
|
uint64_t diff;
|
|
|
|
if (recv == NULL)
|
|
return 0;
|
|
if (!recv->started) {
|
|
recv->started = 1;
|
|
recv->highest = s;
|
|
recv->mask = 1;
|
|
return 1;
|
|
}
|
|
if (s > recv->highest) {
|
|
diff = s - recv->highest;
|
|
if (diff >= 64)
|
|
recv->mask = 1;
|
|
else
|
|
recv->mask = (recv->mask << diff) | 1;
|
|
recv->highest = s;
|
|
return 1;
|
|
}
|
|
diff = recv->highest - s;
|
|
if (diff >= 64)
|
|
return 0;
|
|
if (recv->mask & (1ULL << diff))
|
|
return 0;
|
|
recv->mask |= 1ULL << diff;
|
|
return 1;
|
|
}
|