Files
lotherk 3aab4e35c8 dtchat: add the local TLS listener and TOFU hello (T5.4)
local.c binds a nonblocking TLS listener (identity certificate, 127.0.0.1 or all interfaces, port 0 = ephemeral) and drives the handshake from DcLoop. A peer sends one JSON line {type:hello,id,pin}; the listener checks id and pin against what it expects, answers {type:hello-ack,ok:true|false,pin:...} and closes on a wrong pin, a lying or malformed hello. The accepted hello goes to the callback once; connections stay open for the protocol of T5.5. identity.c now also exposes the private key as PEM for libtls. test_local starts a listener per case and connects with a blocking libtls client: matching pin accepted (callback sees id/pin), wrong pin rejected (no callback, ok:false), any-pin accepted. Standalone test all ok; make mcde-host could not run because another agent's release+sourcesets+usb chain holds build/.build.lock (pid 2126511); the next build verifies the wiring.
2026-11-16 20:23:14 +00:00

409 lines
9.1 KiB
C

/*
* identity.c -- dtchat's Ed25519 identity, certificate and pin
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The identity is one Ed25519 key, a self-signed X.509 certificate (CN
* "dtchat", ten years) and the SHA-256 pin of the certificate's DER form.
* identity.json is written through the store (dir 0700, file 0600, atomic):
*
* {"version":1,"algo":"ed25519","public":"<b64 raw 32>",
* "private":"<b64 PKCS#8 DER>","cert":"<PEM>","pin":"sha256:<hex>"}
*
* A corrupt identity is never overwritten: it is moved to identity.json.bad
* by the store and dc_identity_load_or_create() fails.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
struct DcIdentity {
EVP_PKEY *key;
X509 *cert;
char *cert_pem;
char *key_pem;
char pin[80];
char pub_b64[64];
};
static char *
b64_encode_raw(const unsigned char *data, size_t len)
{
size_t outlen = 4 * ((len + 2) / 3);
char *out = malloc(outlen + 1);
if (out == NULL)
return NULL;
EVP_EncodeBlock((unsigned char *)out, data, (int)len);
out[outlen] = '\0';
return out;
}
static unsigned char *
b64_decode_raw(const char *text, size_t *len_out)
{
size_t len = strlen(text);
size_t alloc = (len / 4 + 1) * 3;
unsigned char *out = malloc(alloc + 1);
int n;
if (out == NULL)
return NULL;
n = EVP_DecodeBlock(out, (const unsigned char *)text, (int)len);
if (n < 0) {
free(out);
return NULL;
}
while (len > 0 && text[len - 1] == '=') {
n--;
len--;
}
*len_out = (size_t)n;
return out;
}
static int
sha256_hex(const unsigned char *data, size_t len, char *out, size_t outlen)
{
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int mdlen = 0;
size_t i;
if (outlen < 2 * 32 + 1)
return -1;
if (EVP_Digest(data, len, md, &mdlen, EVP_sha256(), NULL) != 1)
return -1;
for (i = 0; i < mdlen; i++)
snprintf(out + 2 * i, outlen - 2 * i, "%02x", md[i]);
out[2 * mdlen] = '\0';
return 0;
}
static char *
key_to_pem(EVP_PKEY *key)
{
BIO *bio = BIO_new(BIO_s_mem());
char *data = NULL;
long len;
if (bio == NULL)
return NULL;
if (PEM_write_bio_PrivateKey(bio, key, NULL, NULL, 0, NULL, NULL) != 1) {
BIO_free(bio);
return NULL;
}
len = BIO_get_mem_data(bio, &data);
if (len <= 0) {
BIO_free(bio);
return NULL;
}
{
char *pem = malloc((size_t)len + 1);
if (pem != NULL) {
memcpy(pem, data, (size_t)len);
pem[len] = '\0';
}
BIO_free(bio);
return pem;
}
}
static char *
cert_to_pem(X509 *cert)
{
BIO *bio = BIO_new(BIO_s_mem());
char *data = NULL;
long len;
if (bio == NULL)
return NULL;
if (PEM_write_bio_X509(bio, cert) != 1) {
BIO_free(bio);
return NULL;
}
len = BIO_get_mem_data(bio, &data);
if (len <= 0) {
BIO_free(bio);
return NULL;
}
{
char *pem = malloc((size_t)len + 1);
if (pem != NULL) {
memcpy(pem, data, (size_t)len);
pem[len] = '\0';
}
BIO_free(bio);
return pem;
}
}
static int
cert_pin(X509 *cert, char *out, size_t outlen)
{
unsigned char *der = NULL;
int derlen = i2d_X509(cert, &der);
char hex[65];
int rc;
if (derlen <= 0 || der == NULL)
return -1;
rc = sha256_hex(der, (size_t)derlen, hex, sizeof(hex));
OPENSSL_free(der);
if (rc != 0)
return -1;
snprintf(out, outlen, "sha256:%s", hex);
return 0;
}
static int
public_b64(EVP_PKEY *key, char *out, size_t outlen)
{
unsigned char raw[64];
size_t rawlen = sizeof(raw);
char *b64;
if (EVP_PKEY_get_raw_public_key(key, raw, &rawlen) != 1)
return -1;
b64 = b64_encode_raw(raw, rawlen);
if (b64 == NULL)
return -1;
snprintf(out, outlen, "%s", b64);
free(b64);
return 0;
}
static DcIdentity *
identity_generate(void)
{
DcIdentity *id = calloc(1, sizeof(*id));
EVP_PKEY_CTX *pctx = NULL;
X509_NAME *name;
if (id == NULL)
return NULL;
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL);
if (pctx == NULL ||
EVP_PKEY_keygen_init(pctx) != 1 ||
EVP_PKEY_keygen(pctx, &id->key) != 1) {
EVP_PKEY_CTX_free(pctx);
dc_identity_free(id);
return NULL;
}
EVP_PKEY_CTX_free(pctx);
id->cert = X509_new();
if (id->cert == NULL ||
X509_set_version(id->cert, 2) != 1 ||
ASN1_INTEGER_set(X509_get_serialNumber(id->cert), 1) != 1 ||
X509_gmtime_adj(X509_getm_notBefore(id->cert), 0) == NULL ||
X509_gmtime_adj(X509_getm_notAfter(id->cert),
10L * 365 * 24 * 60 * 60) == NULL ||
X509_set_pubkey(id->cert, id->key) != 1) {
dc_identity_free(id);
return NULL;
}
name = X509_get_subject_name(id->cert);
if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
(const unsigned char *)"dtchat", -1, -1, 0) != 1 ||
X509_set_issuer_name(id->cert, name) != 1 ||
X509_sign(id->cert, id->key, NULL) <= 0) {
dc_identity_free(id);
return NULL;
}
id->cert_pem = cert_to_pem(id->cert);
id->key_pem = key_to_pem(id->key);
if (id->cert_pem == NULL || id->key_pem == NULL ||
cert_pin(id->cert, id->pin, sizeof(id->pin)) != 0 ||
public_b64(id->key, id->pub_b64, sizeof(id->pub_b64)) != 0) {
dc_identity_free(id);
return NULL;
}
return id;
}
static DcIdentity *
identity_from_json(const DcJson *json)
{
DcIdentity *id;
const char *priv_b64;
const char *cert_pem;
const char *pin;
const char *pub;
unsigned char *der = NULL;
size_t derlen = 0;
const unsigned char *p;
BIO *bio = NULL;
if (json == NULL)
return NULL;
if (dc_json_int(dc_json_get(json, "version")) != 1)
return NULL;
if (dc_json_string(dc_json_get(json, "algo")) == NULL ||
strcmp(dc_json_string(dc_json_get(json, "algo")),
"ed25519") != 0)
return NULL;
priv_b64 = dc_json_string(dc_json_get(json, "private"));
cert_pem = dc_json_string(dc_json_get(json, "cert"));
pin = dc_json_string(dc_json_get(json, "pin"));
pub = dc_json_string(dc_json_get(json, "public"));
if (priv_b64 == NULL || cert_pem == NULL || pin == NULL || pub == NULL)
return NULL;
id = calloc(1, sizeof(*id));
if (id == NULL)
return NULL;
der = b64_decode_raw(priv_b64, &derlen);
if (der == NULL)
goto fail;
p = der;
id->key = d2i_AutoPrivateKey(NULL, &p, (long)derlen);
free(der);
der = NULL;
if (id->key == NULL)
goto fail;
bio = BIO_new_mem_buf(cert_pem, -1);
if (bio == NULL)
goto fail;
id->cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
BIO_free(bio);
bio = NULL;
if (id->cert == NULL)
goto fail;
if (X509_check_private_key(id->cert, id->key) != 1)
goto fail;
id->cert_pem = strdup(cert_pem);
id->key_pem = key_to_pem(id->key);
if (id->cert_pem == NULL || id->key_pem == NULL ||
cert_pin(id->cert, id->pin, sizeof(id->pin)) != 0 ||
public_b64(id->key, id->pub_b64, sizeof(id->pub_b64)) != 0)
goto fail;
/* the stored pin and public key must match the certificate */
if (strcmp(id->pin, pin) != 0 || strcmp(id->pub_b64, pub) != 0)
goto fail;
return id;
fail:
if (bio != NULL)
BIO_free(bio);
dc_identity_free(id);
return NULL;
}
int
dc_identity_save(const DcIdentity *id, const char *dir)
{
DcStore *store;
DcJson *json;
unsigned char *der = NULL;
int derlen;
char *priv_b64 = NULL;
int rc = -1;
if (id == NULL || id->key == NULL || id->cert == NULL || dir == NULL)
return -1;
derlen = i2d_PrivateKey(id->key, &der);
if (derlen <= 0 || der == NULL)
return -1;
priv_b64 = b64_encode_raw(der, (size_t)derlen);
OPENSSL_free(der);
if (priv_b64 == NULL)
return -1;
store = dc_store_open(dir);
json = dc_json_new_object();
if (store == NULL || json == NULL)
goto out;
(void)dc_json_object_set(json, "version", dc_json_new_int(1));
(void)dc_json_object_set(json, "algo",
dc_json_new_string("ed25519"));
(void)dc_json_object_set(json, "public",
dc_json_new_string(id->pub_b64));
(void)dc_json_object_set(json, "private",
dc_json_new_string(priv_b64));
(void)dc_json_object_set(json, "cert",
dc_json_new_string(id->cert_pem));
(void)dc_json_object_set(json, "pin", dc_json_new_string(id->pin));
rc = dc_store_save_identity(store, json);
out:
dc_json_free(json);
dc_store_close(store);
free(priv_b64);
return rc;
}
DcIdentity *
dc_identity_load_or_create(const char *dir)
{
DcStore *store;
DcJson *json;
DcIdentity *id;
int missing = 0;
char err[256];
if (dir == NULL || *dir == '\0')
return NULL;
store = dc_store_open(dir);
if (store == NULL)
return NULL;
json = dc_store_load(store, "identity.json", &missing, err,
sizeof(err));
dc_store_close(store);
if (json != NULL) {
id = identity_from_json(json);
dc_json_free(json);
return id;
}
if (!missing)
return NULL; /* corrupt: the store moved it aside */
id = identity_generate();
if (id == NULL)
return NULL;
if (dc_identity_save(id, dir) != 0) {
dc_identity_free(id);
return NULL;
}
return id;
}
void
dc_identity_free(DcIdentity *id)
{
if (id == NULL)
return;
EVP_PKEY_free(id->key);
X509_free(id->cert);
free(id->cert_pem);
free(id->key_pem);
free(id);
}
const char *
dc_identity_pin(const DcIdentity *id)
{
return id != NULL ? id->pin : NULL;
}
const char *
dc_identity_public_b64(const DcIdentity *id)
{
return id != NULL ? id->pub_b64 : NULL;
}
const char *
dc_identity_cert_pem(const DcIdentity *id)
{
return id != NULL ? id->cert_pem : NULL;
}
const char *
dc_identity_key_pem(const DcIdentity *id)
{
return id != NULL ? id->key_pem : NULL;
}