A fresh install used to require openssl before the server could speak TLS: two empty paths in [tls] meant the identity failed to load and every ClientHello was refused. TLS is now on by default and, when no custom certificate is configured, the server generates its own self-signed one the first time it needs it -- RSA-2048, SHA-256, CN=yama-server, CA:TRUE, valid for ten years from one day before now. Plaintext clients are untouched, since the device port still splits plaintext from TLS by sniffing the first six bytes of a connection. The certificate and its PKCS#8 private key are stored side by side in the config under [tls] BuiltinCert / BuiltinKey as single-line base64 DER. Single line, because the server has two config backends -- the registry for Release, settings.ini for a Debug master -- and only a line without newlines means the same read/write code serves both. That in turn required fixing the registry reader: iniFile::GetStr copied into a 512 byte stack buffer, so a 1.5 KB value came back as a truncated or empty string. The two-phase dynamic read now lives in common/iniFile.h as ReadRegistryValueSz / ReadRegistrySz, and the copy that client/TlsChannel.cpp had been carrying for its own trust anchor is gone. This also makes the built-in certificate self-checking: Ensure() re-reads what it just wrote and fails loudly if the round trip did not survive. Import has to go through NCRYPT_PKCS8_PRIVATE_KEY_BLOB, matching the import path in BuildIdentityFromDer. NCRYPT_PKCS8_PRIV_KEY_INFO_BLOB, which most samples reach for, is not implemented by the Microsoft Software KSP and returns NTE_INVALID_PARAMETER; a probe confirmed it, and the export type that works is the one the importer already expected. Building an identity is now split into a file front-end (PEM, fullchain, PKCS#1 hints) and a shared DER core, so the built-in and file paths cannot drift apart. That key also exposed a second problem worth fixing here. The identity binds its private key to the certificate by name -- CRYPT_KEY_PROV_INFO with pwszContainerName -- and Schannel resolves that name on every handshake through CryptAcquireCertificatePrivateKey. With the fixed name "YamaServerTlsKey", two server processes under the same Windows user shared one key slot: whichever loaded last overwrote it, and the other process's next handshake picked up a private key that does not match its certificate. That is exactly the debug-plus-release pairing used here, and defaulting to a built-in certificate is what makes it reachable, since the debug instance no longer points at the same PEM as the release instance. The name is now the base name plus the first six bytes of the certificate's SHA-1 thumbprint, e.g. YamaServerTlsKey-c0d469eae8a4: different certificates get different keys, instances sharing one certificate still share one key. MCP picks this up too. The effective name goes to the trace log so a collision is visible without a debugger. A stale key from an older build may still sit in the key store under the old name; nothing references it and it is not deleted automatically, since a running instance of an older build could be using it. The trust anchor still has to be pushed. The client pins exactly one root through an exclusive-root chain engine and checks no host name, so a self-signed CA:TRUE leaf chains cleanly to itself -- but only if the client is told about it. That is the existing 启用 TLS / 切换到 WSS command, which now takes the PEM from ServerTlsIdentity::AnchorPem instead of re-reading the configured cert file, so it reports the same certificate the server actually serves for both sources. Losing the stored certificate (cleared registry, new Windows user) regenerates it, and every client must be re-pushed the anchor. Two places that used to fall back to [tls] CertPath now skip that fallback while the built-in certificate is active: the MCP HTTPS front end at startup and the prefill in its settings dialog. A self-signed certificate cannot pass validation at a public MCP client, and the retained CertPath is only a leftover from switching back to a custom certificate, so pretending to use it would fail later with a worse message. MCP HTTPS with empty paths already fails closed with a log line. The TLS dialog gains a 内置证书 checkbox, ticked by default and unticked when certificate paths are present; ticking it greys out the path rows without clearing them, and shows either "generated on first use" or the expiry date read back from the stored certificate. Saving writes four keys -- Enabled, UseBuiltIn, CertPath, KeyPath -- and an explicit UseBuiltIn keeps the paths from being used as an implicit switch. Every reader of [tls] Enabled now defaults to 1, so the menu check, the dialog and the loader agree on a fresh install. The status bar panes on 无 / 有 grew a third state, 内置, and the pane width is measured across all three. Translations were added to en_US, zh_TW and de_DE; skipping the German file would have left the new dialog text in Chinese there, as de_DE is loaded from disk rather than embedded. Verified with the debug server against openssl s_client. Both the generated built-in certificate (CN=yama-server) and an existing custom one (CN=localhost) complete a TLS 1.2 handshake with the pinned certificate, and their log lines show distinct key names, c0d469eae8a4 and 272f2adfb19c. An unmodified client still runs in plaintext without an anchor, an existing custom certificate deployment behaves exactly as before, and client and server both build clean at Debug x64. Co-Authored-By: deepseek-flash
442 lines
13 KiB
C++
442 lines
13 KiB
C++
// McpTlsProxy.cpp — MCP 端口的 TLS 前端实现(Schannel 入站,同端口嗅探)
|
|
#include "StdAfx.h"
|
|
#include "McpTlsProxy.h"
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
namespace
|
|
{
|
|
// MCP 身份在 CNG KSP 里的键名基名,与设备端口的 YamaServerTlsKey 区分,避免互相覆盖。
|
|
// 实际键名还会拼上证书指纹(见 BuildIdentityFromDer)。
|
|
const wchar_t* kMcpKeyName = L"YamaMcpTlsKey";
|
|
|
|
const size_t kMaxConns = 64; // 并发连接上限(MCP 客户端数量很少,防资源耗尽)
|
|
const DWORD kSniffTimeoutMs = 10000; // 首字节嗅探超时:连上不说话的直接断开
|
|
}
|
|
|
|
bool McpTlsProxy::WaitReady(SOCKET sock, bool forRead, int timeoutMs)
|
|
{
|
|
fd_set fds;
|
|
FD_ZERO(&fds);
|
|
FD_SET(sock, &fds);
|
|
timeval tv;
|
|
tv.tv_sec = timeoutMs / 1000;
|
|
tv.tv_usec = (timeoutMs % 1000) * 1000;
|
|
return select(0, forRead ? &fds : NULL, forRead ? NULL : &fds, NULL, &tv) > 0;
|
|
}
|
|
|
|
bool McpTlsProxy::SendAll(SOCKET sock, const void* data, int len)
|
|
{
|
|
const char* p = (const char*)data;
|
|
int sent = 0;
|
|
while (sent < len)
|
|
{
|
|
int n = send(sock, p + sent, len - sent, 0);
|
|
if (n == SOCKET_ERROR)
|
|
{
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK)
|
|
{
|
|
if (!WaitReady(sock, false, 30000))
|
|
return false;
|
|
continue;
|
|
}
|
|
return false;
|
|
}
|
|
if (n <= 0)
|
|
return false;
|
|
sent += n;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
SOCKET McpTlsProxy::ConnectLoopback(int port)
|
|
{
|
|
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
if (s == INVALID_SOCKET)
|
|
return INVALID_SOCKET;
|
|
|
|
sockaddr_in addr = {};
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons((u_short)port);
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
if (connect(s, (sockaddr*)&addr, sizeof(addr)) != 0)
|
|
{
|
|
closesocket(s);
|
|
return INVALID_SOCKET;
|
|
}
|
|
u_long nb = 1;
|
|
ioctlsocket(s, FIONBIO, &nb); // 连接建立后转非阻塞,交给 select 驱动
|
|
return s;
|
|
}
|
|
|
|
bool McpTlsProxy::Start(const std::string& bind, int listenPort, int backendPort,
|
|
const std::string& certPath, const std::string& keyPath)
|
|
{
|
|
if (m_running.load())
|
|
return true;
|
|
if (listenPort <= 0 || backendPort <= 0)
|
|
return false;
|
|
|
|
// 身份必须先加载成功:证书坏了就 fail-closed,绝不以明文对外监听。
|
|
if (!ServerTlsLoadIdentityFromPem(certPath, keyPath, kMcpKeyName, m_identity))
|
|
{
|
|
Mprintf("[McpTls] FATAL: cannot load MCP TLS identity (cert=%s key=%s); "
|
|
"refusing to start MCP listener\n", certPath.c_str(), keyPath.c_str());
|
|
return false;
|
|
}
|
|
m_identityLoaded = true;
|
|
m_backendPort = backendPort;
|
|
|
|
addrinfo hints = {};
|
|
hints.ai_family = AF_INET;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
addrinfo* res = nullptr;
|
|
char portStr[16] = {};
|
|
sprintf_s(portStr, "%d", listenPort);
|
|
const char* node = (bind.empty() || bind == "0.0.0.0") ? "0.0.0.0" : bind.c_str();
|
|
|
|
if (getaddrinfo(node, portStr, &hints, &res) != 0 || res == nullptr)
|
|
{
|
|
Mprintf("[McpTls] FATAL: cannot resolve bind address %s\n", node);
|
|
ServerTlsFreeIdentity(m_identity);
|
|
m_identityLoaded = false;
|
|
return false;
|
|
}
|
|
|
|
SOCKET ls = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
if (ls == INVALID_SOCKET)
|
|
{
|
|
freeaddrinfo(res);
|
|
ServerTlsFreeIdentity(m_identity);
|
|
m_identityLoaded = false;
|
|
return false;
|
|
}
|
|
BOOL reuse = TRUE;
|
|
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
|
|
u_long nb = 1;
|
|
ioctlsocket(ls, FIONBIO, &nb); // 非阻塞监听,accept 用 select 驱动
|
|
|
|
if (::bind(ls, res->ai_addr, (int)res->ai_addrlen) != 0 ||
|
|
listen(ls, SOMAXCONN) != 0)
|
|
{
|
|
Mprintf("[McpTls] FATAL: bind/listen %s:%d failed (%d)\n", node, listenPort, WSAGetLastError());
|
|
freeaddrinfo(res);
|
|
closesocket(ls);
|
|
ServerTlsFreeIdentity(m_identity);
|
|
m_identityLoaded = false;
|
|
return false;
|
|
}
|
|
freeaddrinfo(res);
|
|
|
|
m_listen = ls;
|
|
m_running.store(true);
|
|
m_acceptThread = std::thread(&McpTlsProxy::AcceptLoop, this);
|
|
Mprintf("[McpTls] listening on %s:%d (TLS + plaintext passthrough) -> 127.0.0.1:%d\n",
|
|
node, listenPort, backendPort);
|
|
return true;
|
|
}
|
|
|
|
void McpTlsProxy::Stop()
|
|
{
|
|
if (!m_running.exchange(false) && m_listen == INVALID_SOCKET && !m_acceptThread.joinable())
|
|
{
|
|
// 从未启动或已停止
|
|
if (m_identityLoaded)
|
|
{
|
|
ServerTlsFreeIdentity(m_identity);
|
|
m_identityLoaded = false;
|
|
}
|
|
return;
|
|
}
|
|
|
|
SOCKET ls = m_listen;
|
|
m_listen = INVALID_SOCKET;
|
|
if (ls != INVALID_SOCKET)
|
|
{
|
|
shutdown(ls, SD_BOTH); // 唤醒阻塞 accept
|
|
closesocket(ls);
|
|
}
|
|
if (m_acceptThread.joinable())
|
|
m_acceptThread.join();
|
|
|
|
std::vector<std::shared_ptr<Conn>> all;
|
|
{
|
|
std::lock_guard<std::mutex> lk(m_connMtx);
|
|
all.swap(m_conns);
|
|
}
|
|
for (auto& c : all)
|
|
{
|
|
if (c->done.load())
|
|
continue; // 已自行收尾,句柄可能已关闭,勿再触碰
|
|
SOCKET cl = c->client.load();
|
|
if (cl != INVALID_SOCKET)
|
|
shutdown(cl, SD_BOTH);
|
|
SOCKET b = c->backend.load();
|
|
if (b != INVALID_SOCKET)
|
|
shutdown(b, SD_BOTH);
|
|
}
|
|
for (auto& c : all)
|
|
{
|
|
if (c->th.joinable())
|
|
c->th.join();
|
|
}
|
|
|
|
if (m_identityLoaded)
|
|
{
|
|
ServerTlsFreeIdentity(m_identity);
|
|
m_identityLoaded = false;
|
|
}
|
|
Mprintf("[McpTls] stopped\n");
|
|
}
|
|
|
|
void McpTlsProxy::SweepFinished()
|
|
{
|
|
std::vector<std::shared_ptr<Conn>> finished;
|
|
{
|
|
std::lock_guard<std::mutex> lk(m_connMtx);
|
|
for (auto it = m_conns.begin(); it != m_conns.end();)
|
|
{
|
|
if ((*it)->done.load())
|
|
{
|
|
finished.push_back(*it);
|
|
it = m_conns.erase(it);
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
for (auto& c : finished)
|
|
{
|
|
if (c->th.joinable())
|
|
c->th.join();
|
|
}
|
|
}
|
|
|
|
void McpTlsProxy::AcceptLoop()
|
|
{
|
|
while (m_running.load())
|
|
{
|
|
if (!WaitReady(m_listen, true, 500))
|
|
continue; // 超时轮询 m_running,便于 Stop 及时退出
|
|
|
|
SOCKET cs = accept(m_listen, NULL, NULL);
|
|
if (cs == INVALID_SOCKET)
|
|
{
|
|
if (!m_running.load())
|
|
break;
|
|
int e = WSAGetLastError();
|
|
if (e == WSAEWOULDBLOCK)
|
|
continue;
|
|
Mprintf("[McpTls] accept failed (%d); listener stops\n", e);
|
|
break;
|
|
}
|
|
|
|
SweepFinished();
|
|
|
|
std::lock_guard<std::mutex> lk(m_connMtx);
|
|
if (m_conns.size() >= kMaxConns)
|
|
{
|
|
closesocket(cs);
|
|
continue;
|
|
}
|
|
u_long nb = 1;
|
|
ioctlsocket(cs, FIONBIO, &nb); // 嗅探/泵送全部走 select
|
|
|
|
auto c = std::make_shared<Conn>();
|
|
c->client.store(cs);
|
|
m_conns.push_back(c);
|
|
c->th = std::thread([this, c]() { ServeConn(c); });
|
|
}
|
|
}
|
|
|
|
void McpTlsProxy::CloseConn(Conn& c)
|
|
{
|
|
SOCKET cl = c.client.exchange(INVALID_SOCKET);
|
|
if (cl != INVALID_SOCKET)
|
|
{
|
|
shutdown(cl, SD_BOTH);
|
|
closesocket(cl);
|
|
}
|
|
SOCKET b = c.backend.exchange(INVALID_SOCKET);
|
|
if (b != INVALID_SOCKET)
|
|
{
|
|
shutdown(b, SD_BOTH);
|
|
closesocket(b);
|
|
}
|
|
c.done.store(true);
|
|
}
|
|
|
|
void McpTlsProxy::ServeConn(std::shared_ptr<Conn> sp)
|
|
{
|
|
Conn& c = *sp;
|
|
SOCKET client = c.client.load();
|
|
|
|
// ---- 1) 首字节嗅探(同设备端口 §5.2):0x16 0x03 = TLS ClientHello ----
|
|
BYTE peek[8] = {};
|
|
int pn = 0;
|
|
DWORD start = GetTickCount();
|
|
for (;;)
|
|
{
|
|
int r = recv(client, (char*)peek, sizeof(peek), MSG_PEEK);
|
|
if (r > 0)
|
|
{
|
|
pn = r;
|
|
break;
|
|
}
|
|
if (r == 0)
|
|
{
|
|
CloseConn(c); // 对端直接关闭
|
|
return;
|
|
}
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
|
{
|
|
CloseConn(c);
|
|
return;
|
|
}
|
|
if (GetTickCount() - start > kSniffTimeoutMs || !m_running.load())
|
|
{
|
|
CloseConn(c); // 连上不说话的,直接断开
|
|
return;
|
|
}
|
|
WaitReady(client, true, 200);
|
|
}
|
|
const bool isTls = (peek[0] == 0x16) && (pn < 2 || peek[1] == 0x03);
|
|
|
|
// ---- 2) 后端:httplib 只监听 127.0.0.1 的内部端口 ----
|
|
SOCKET backend = ConnectLoopback(m_backendPort);
|
|
if (backend == INVALID_SOCKET)
|
|
{
|
|
Mprintf("[McpTls] backend 127.0.0.1:%d not reachable\n", m_backendPort);
|
|
CloseConn(c);
|
|
return;
|
|
}
|
|
c.backend.store(backend);
|
|
|
|
// ---- 3) 分流 ----
|
|
if (isTls)
|
|
{
|
|
// 嗅探用的是 MSG_PEEK(不消费),ClientHello 原样留在内核缓冲里;交多少字节
|
|
// HandshakeWithCred 都能自己补齐,这里直接让它从 socket 读完整首条记录。
|
|
ServerTlsSession sess;
|
|
if (!sess.HandshakeWithCred(client, NULL, 0, m_identity.cred))
|
|
{
|
|
Mprintf("[McpTls] TLS handshake failed; closing connection\n");
|
|
CloseConn(c);
|
|
return;
|
|
}
|
|
PumpTls(c, sess);
|
|
}
|
|
else
|
|
{
|
|
PumpRaw(c); // 本机 http://127.0.0.1:port 的既有用法原样透传
|
|
}
|
|
CloseConn(c);
|
|
}
|
|
|
|
void McpTlsProxy::PumpRaw(Conn& c)
|
|
{
|
|
char buf[16384];
|
|
const SOCKET client = c.client.load();
|
|
const SOCKET backend = c.backend.load();
|
|
|
|
for (;;)
|
|
{
|
|
fd_set rd;
|
|
FD_ZERO(&rd);
|
|
FD_SET(client, &rd);
|
|
FD_SET(backend, &rd);
|
|
int n = select(0, &rd, NULL, NULL, NULL);
|
|
if (n <= 0)
|
|
break;
|
|
|
|
if (FD_ISSET(client, &rd))
|
|
{
|
|
int r = recv(client, buf, sizeof(buf), 0);
|
|
if (r == 0) break;
|
|
if (r < 0)
|
|
{
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
|
|
break;
|
|
}
|
|
if (!SendAll(backend, buf, r)) break;
|
|
}
|
|
if (FD_ISSET(backend, &rd))
|
|
{
|
|
int r = recv(backend, buf, sizeof(buf), 0);
|
|
if (r == 0) break;
|
|
if (r < 0)
|
|
{
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
|
|
break;
|
|
}
|
|
if (!SendAll(client, buf, r)) break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void McpTlsProxy::PumpTls(Conn& c, ServerTlsSession& sess)
|
|
{
|
|
char buf[16384];
|
|
const SOCKET client = c.client.load();
|
|
const SOCKET backend = c.backend.load();
|
|
std::vector<BYTE> plain;
|
|
std::vector<BYTE> wire;
|
|
|
|
// 握手阶段已经收到的明文要先送给后端:客户端复用 TLS 会话时会把 ChangeCipherSpec +
|
|
// Finished + 首条请求挤在同一批字节里,握手只会消费前两个,请求留在会话缓冲里。
|
|
// 不在这里取走,后端就一个字节也收不到,5s 后按 keep-alive 超时关连接,客户端看到的是
|
|
// “fetch failed (other side closed)”(实测 undici/Node 24 稳定复现)。
|
|
{
|
|
std::vector<BYTE> head;
|
|
if (sess.Decrypt(NULL, 0, head) < 0)
|
|
return;
|
|
if (!head.empty() && !SendAll(backend, head.data(), (int)head.size()))
|
|
return;
|
|
}
|
|
|
|
for (;;)
|
|
{
|
|
fd_set rd;
|
|
FD_ZERO(&rd);
|
|
FD_SET(client, &rd);
|
|
FD_SET(backend, &rd);
|
|
int n = select(0, &rd, NULL, NULL, NULL);
|
|
if (n <= 0)
|
|
break;
|
|
|
|
if (FD_ISSET(client, &rd))
|
|
{
|
|
int r = recv(client, buf, sizeof(buf), 0);
|
|
if (r == 0) break;
|
|
if (r < 0)
|
|
{
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
|
|
break;
|
|
}
|
|
plain.clear();
|
|
if (sess.Decrypt((const BYTE*)buf, r, plain) < 0)
|
|
break; // 对端 close_notify / 解密失败
|
|
if (!plain.empty() && !SendAll(backend, plain.data(), (int)plain.size()))
|
|
break;
|
|
}
|
|
if (FD_ISSET(backend, &rd))
|
|
{
|
|
int r = recv(backend, buf, sizeof(buf), 0);
|
|
if (r == 0) break;
|
|
if (r < 0)
|
|
{
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
|
|
break;
|
|
}
|
|
wire.clear();
|
|
if (sess.Encrypt((const BYTE*)buf, r, wire) < 0)
|
|
break;
|
|
if (!wire.empty() && !SendAll(client, wire.data(), (int)wire.size()))
|
|
break;
|
|
}
|
|
}
|
|
}
|