Files
nandi bfc60ee0e5
CI / test (push) Failing after 50s
Add chandra-remote: VNC and web control over Tailscale
- remote/: shared-memory framebuffer mirror served as RFB 3.8 (VNC, port 5900) and an HTTP/WebSocket PWA (port 6080), bound to the Tailscale interface and loopback by default
- Compositor: remote pointer, button, scroll, modifier, key, key-tap, text and clipboard input, routed past OLED blanking
- chandra-ctl: add status, refresh and osk commands; add input-* and clipboard-set for remote control
- Camera: debayer 10-bit CSI-2 Quad-Bayer frames for imx371 and high-quality still capture
- Camera: rotate frames by the libcamera mounting angle (CCW for 90), fix the expected test values, and restyle the UI for a dark theme
2026-11-28 17:57:55 -08:00

330 lines
11 KiB
C

#include "remote_internal.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
void remote_default_config(RemoteConfig *cfg) {
memset(cfg, 0, sizeof *cfg);
cfg->enabled = 1;
snprintf(cfg->bind, sizeof cfg->bind, "tailscale");
cfg->bind_loopback = 1;
cfg->auth_mode = REMOTE_AUTH_NONE;
cfg->vnc_port = 5900;
cfg->web_port = 6080;
cfg->jpeg_quality = 82;
cfg->max_fps = 30;
}
int remote_bind_is_loopback(const char *b) {
return strcmp(b, "127.0.0.1") == 0 || strcmp(b, "::1") == 0 || strcmp(b, "localhost") == 0;
}
int remote_bind_is_tailscale(const char *b) {
return strcmp(b, "tailscale") == 0;
}
void remote_generate_password(char *out, size_t len) {
static const char alpha[] = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz";
size_t n = sizeof(alpha) - 1;
uint8_t rnd[64];
if (len == 0) return;
if (len > sizeof rnd) len = sizeof rnd;
if (RAND_bytes(rnd, (int)len) != 1) {
for (size_t i = 0; i < len - 1; i++) out[i] = alpha[i % n];
out[len - 1] = 0;
return;
}
for (size_t i = 0; i < len - 1; i++) {
out[i] = alpha[rnd[i] % n];
}
out[len - 1] = 0;
}
static char *trim_ws(char *s) {
while (*s && isspace((unsigned char)*s)) s++;
size_t l = strlen(s);
while (l > 0 && isspace((unsigned char)s[l - 1])) s[--l] = 0;
return s;
}
static int clamp_int(int v, int lo, int hi) {
return v < lo ? lo : (v > hi ? hi : v);
}
static void mkdir_parents(const char *file_path) {
char tmp[512];
snprintf(tmp, sizeof tmp, "%s", file_path);
for (char *p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = 0;
mkdir(tmp, 0700);
*p = '/';
}
}
}
int remote_save_config(const char *path, const RemoteConfig *cfg) {
mkdir_parents(path);
char tmp[528];
snprintf(tmp, sizeof tmp, "%s.tmp", path);
FILE *f = fopen(tmp, "w");
if (!f) return -1;
chmod(tmp, 0600);
fprintf(f,
"# Chandra Remote Desktop configuration\n"
"enabled=%d\n"
"bind=%s\n"
"bind_loopback=%d\n"
"auth=%s\n"
"vnc_port=%d\n"
"web_port=%d\n"
"jpeg_quality=%d\n"
"max_fps=%d\n",
cfg->enabled ? 1 : 0,
cfg->bind,
cfg->bind_loopback ? 1 : 0,
cfg->auth_mode == REMOTE_AUTH_NONE ? "none" : "password",
cfg->vnc_port,
cfg->web_port,
cfg->jpeg_quality,
cfg->max_fps);
if (cfg->password[0]) {
fprintf(f, "password=%s\n", cfg->password);
}
fclose(f);
if (rename(tmp, path) < 0) {
unlink(tmp);
return -1;
}
chmod(path, 0600);
return 0;
}
int remote_load_or_init_config(const char *path, RemoteConfig *cfg) {
remote_default_config(cfg);
FILE *f = fopen(path, "r");
if (!f) {
if (errno == ENOENT) {
return remote_save_config(path, cfg);
}
return -1;
}
char line[256];
while (fgets(line, sizeof line, f)) {
char *s = trim_ws(line);
if (!*s || *s == '#') continue;
char *eq = strchr(s, '=');
if (!eq) continue;
*eq = 0;
char *k = trim_ws(s);
char *v = trim_ws(eq + 1);
if (strcmp(k, "enabled") == 0) cfg->enabled = atoi(v) != 0;
else if (strcmp(k, "bind") == 0) snprintf(cfg->bind, sizeof cfg->bind, "%s", v);
else if (strcmp(k, "bind_loopback") == 0) cfg->bind_loopback = atoi(v) != 0;
else if (strcmp(k, "auth") == 0) {
cfg->auth_mode = strcmp(v, "none") == 0 ? REMOTE_AUTH_NONE : REMOTE_AUTH_PASSWORD;
} else if (strcmp(k, "vnc_port") == 0) cfg->vnc_port = clamp_int(atoi(v), 1, 65535);
else if (strcmp(k, "web_port") == 0) cfg->web_port = clamp_int(atoi(v), 1, 65535);
else if (strcmp(k, "jpeg_quality") == 0) cfg->jpeg_quality = clamp_int(atoi(v), 20, 95);
else if (strcmp(k, "max_fps") == 0) cfg->max_fps = clamp_int(atoi(v), 5, 60);
else if (strcmp(k, "password") == 0) snprintf(cfg->password, sizeof cfg->password, "%s", v);
}
fclose(f);
if (cfg->auth_mode == REMOTE_AUTH_PASSWORD && !cfg->password[0]) {
remote_generate_password(cfg->password, 11);
remote_save_config(path, cfg);
}
return 0;
}
int remote_ct_equal(const uint8_t hmac_key[32], const char *a, const char *b) {
uint8_t da[32], db[32];
unsigned int la = 0, lb = 0;
HMAC(EVP_sha256(), hmac_key, 32, (const uint8_t *)a, strlen(a), da, &la);
HMAC(EVP_sha256(), hmac_key, 32, (const uint8_t *)b, strlen(b), db, &lb);
return la == 32 && lb == 32 && CRYPTO_memcmp(da, db, 32) == 0 && strlen(a) == strlen(b);
}
/* ---- Self-contained FIPS-46 DES (no OpenSSL 3 legacy provider needed) ---- */
static const uint8_t des_pc1[56] = {
57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4
};
static const uint8_t des_pc2[48] = {
14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,
41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32
};
static const uint8_t des_rot[16] = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
static const uint8_t des_ip[64] = {
58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7
};
static const uint8_t des_fp[64] = {
40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25
};
static const uint8_t des_e[48] = {
32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1
};
static const uint8_t des_p[32] = {
16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25
};
static const uint8_t des_s[8][64] = {
{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7, 0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0, 15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13},
{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10, 3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15, 13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9},
{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8, 13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7, 1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12},
{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15, 13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4, 3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14},
{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9, 14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14, 11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3},
{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11, 10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6, 4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13},
{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1, 13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2, 6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12},
{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7, 1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8, 2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
};
static uint64_t permute_be(uint64_t val, int in_bits, const uint8_t *tbl, int out_bits) {
uint64_t res = 0;
for (int i = 0; i < out_bits; i++) {
int shift = in_bits - tbl[i];
res = (res << 1) | ((val >> shift) & 1ULL);
}
return res;
}
static uint32_t rol28(uint32_t v, int k) {
return ((v << k) | (v >> (28 - k))) & 0x0FFFFFFFu;
}
static uint32_t des_feistel(uint32_t r, uint64_t subkey) {
uint64_t exp = permute_be(r, 32, des_e, 48) ^ subkey;
uint32_t s_out = 0;
for (int i = 0; i < 8; i++) {
uint8_t six = (uint8_t)((exp >> ((7 - i) * 6)) & 0x3Fu);
int row = ((six & 0x20u) >> 4) | (six & 0x01u);
int col = (six >> 1) & 0x0Fu;
s_out = (s_out << 4) | des_s[i][row * 16 + col];
}
return (uint32_t)permute_be(s_out, 32, des_p, 32);
}
static void des_ecb_encrypt(const uint8_t key[8], const uint8_t in[8], uint8_t out[8]) {
uint64_t k64 = 0, d64 = 0;
for (int i = 0; i < 8; i++) {
k64 = (k64 << 8) | key[i];
d64 = (d64 << 8) | in[i];
}
uint64_t cd = permute_be(k64, 64, des_pc1, 56);
uint32_t c = (uint32_t)(cd >> 28) & 0x0FFFFFFFu;
uint32_t d = (uint32_t)cd & 0x0FFFFFFFu;
uint64_t sk[16];
for (int r = 0; r < 16; r++) {
c = rol28(c, des_rot[r]);
d = rol28(d, des_rot[r]);
sk[r] = permute_be(((uint64_t)c << 28) | d, 56, des_pc2, 48);
}
uint64_t ip = permute_be(d64, 64, des_ip, 64);
uint32_t L = (uint32_t)(ip >> 32);
uint32_t R = (uint32_t)ip;
for (int r = 0; r < 16; r++) {
uint32_t nR = L ^ des_feistel(R, sk[r]);
L = R;
R = nR;
}
uint64_t pre = ((uint64_t)R << 32) | L;
uint64_t ciph = permute_be(pre, 64, des_fp, 64);
for (int i = 0; i < 8; i++) out[i] = (uint8_t)((ciph >> ((7 - i) * 8)) & 0xFFu);
}
static uint8_t rev8(uint8_t b) {
b = (uint8_t)(((b & 0xF0u) >> 4) | ((b & 0x0Fu) << 4));
b = (uint8_t)(((b & 0xCCu) >> 2) | ((b & 0x33u) << 2));
b = (uint8_t)(((b & 0xAAu) >> 1) | ((b & 0x55u) << 1));
return b;
}
void vnc_des_encrypt_challenge(const char *password, const uint8_t challenge[16], uint8_t out[16]) {
uint8_t key[8];
size_t plen = strlen(password);
for (int i = 0; i < 8; i++) {
uint8_t ch = (i < (int)plen) ? (uint8_t)password[i] : 0u;
key[i] = rev8(ch);
}
des_ecb_encrypt(key, challenge, out);
des_ecb_encrypt(key, challenge + 8, out + 8);
}
/* ---- Web session token & RFC 6455 accept ---- */
static const uint64_t WEB_TOKEN_TTL_SEC = 30ULL * 24ULL * 3600ULL;
void web_token_sign(const uint8_t key[32], uint64_t now_sec, char *out, size_t cap) {
char ts[32];
snprintf(ts, sizeof ts, "%llu", (unsigned long long)now_sec);
uint8_t mac[32];
unsigned int mlen = 0;
HMAC(EVP_sha256(), key, 32, (const uint8_t *)ts, strlen(ts), mac, &mlen);
char hex[65];
hex_encode(mac, mlen, hex);
snprintf(out, cap, "%s.%s", ts, hex);
}
int web_token_verify(const uint8_t key[32], const char *tok, uint64_t now_sec) {
if (!tok || !*tok) return 0;
const char *dot = strchr(tok, '.');
if (!dot || dot == tok || strlen(dot + 1) != 64) return 0;
char ts[32];
size_t tlen = (size_t)(dot - tok);
if (tlen >= sizeof ts) return 0;
memcpy(ts, tok, tlen);
ts[tlen] = 0;
char *endp = NULL;
uint64_t issued = strtoull(ts, &endp, 10);
if (!endp || *endp != 0) return 0;
if (now_sec > issued && now_sec - issued > WEB_TOKEN_TTL_SEC) return 0;
if (issued > now_sec + 300ULL) return 0;
uint8_t expected[32], got[32];
unsigned int elen = 0;
HMAC(EVP_sha256(), key, 32, (const uint8_t *)ts, tlen, expected, &elen);
for (int i = 0; i < 32; i++) {
unsigned int v = 0;
if (sscanf(dot + 1 + i * 2, "%2x", &v) != 1) return 0;
got[i] = (uint8_t)v;
}
return elen == 32 && CRYPTO_memcmp(expected, got, 32) == 0;
}
int ws_compute_accept(const char *client_key, char *out, size_t cap) {
static const char guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
char buf[256];
if (!client_key || strlen(client_key) > 128) return -1;
snprintf(buf, sizeof buf, "%s%s", client_key, guid);
uint8_t sha[EVP_MAX_MD_SIZE];
unsigned int slen = 0;
if (!EVP_Digest(buf, strlen(buf), sha, &slen, EVP_sha1(), NULL)) return -1;
if (cap < 32) return -1;
int n = EVP_EncodeBlock((unsigned char *)out, sha, (int)slen);
if (n <= 0 || (size_t)n >= cap) return -1;
out[n] = 0;
return 0;
}