Feature: Terminate TLS for the MCP endpoint inside YAMA

MCP was served by a plain httplib::Server, so remote clients could only use
cleartext HTTP. YAMA now terminates TLS itself: a small front-end owns the
MCP port, sniffs the first byte and either runs a Schannel handshake and
forwards the decrypted bytes or passes plaintext through, with httplib
moving to a loopback-only internal port. No OpenSSL, no frp change, existing
local HTTP clients keep working, and an unusable certificate fails closed
instead of falling back to cleartext.

The identity loader grew the two things a public certificate needs. ECDSA
(P-256/P-384/P-521) keys are persisted by importing PKCS#8 with an
NCRYPTBUFFER_PKCS_KEY_NAME parameter, the only route MS Software KSP
supports for Schannel; RSA keeps its old export-and-repersist path.
Fullchains are accepted, and the intermediates are added to the current
user's CA store so Schannel, which builds chains only from system stores,
actually sends them. The shared handshake also now buffers a full TLS
record before the first AcceptSecurityContext, which a partial ClientHello
would otherwise poison (SEC_E_UNKNOWN_CREDENTIALS).

Session resumption exposed a second trap: when a client resumes a session,
ChangeCipherSpec, Finished and the first application record arrive in one
segment and AcceptSecurityContext leaves the application record pending in
the session. Nothing drained it, because both callers only decrypt when new
ciphertext arrives, so the request waited until the peer gave up (the MCP
front-end saw no request at all and the client reported "fetch failed
(other side closed)"; the device port had the same hole behind its
occasional login timeouts). Decrypt now drains pending data on len == 0, and
both the front-end and the device port's handshake thread use it before
entering their receive loops.

Configuration is McpHttps, McpCertPath, McpKeyPath and McpInternalPort, the
first three editable in the MCP settings dialog (the certificate rows stay
greyed out until HTTPS is enabled and saving rejects missing files).

Verified with a real Let's Encrypt ECDSA fullchain (53 tools over TLS, 401
with no or bad token), a local three-level chain whose intermediate was
absent from the store, Node 24's fetch on a resumed session (initialize,
notifications/initialized and tools/list all complete), and the device
port's RSA handshake as a regression check. Details in docs/Mcp_Design.md
section 13.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-26 13:56:41 +01:00
1 parent b4cb34fea9
commit a7dd86ff0f
15 files changed
+1218 -96

No files matched your search

+28 -1
View File
@@ -931,11 +931,38 @@ DWORD WINAPI IOCPServer::TlsHandshakeThreadProc(LPVOID lParam)
bool ok = (ctx->tls != NULL) &&
ctx->tls->Handshake(ctx->sClientSocket, initial.data(), (int)initial.size());
// 握手结束时会话里可能已经躺着首条应用数据:客户端复用 TLS 会话时会把
// ChangeCipherSpec + Finished + 首个业务包挤在同一批字节里,握手只消费前两个。
// 必须在交还 IOCP 之前取出来喂给解析路径——接收路径只在有新密文到达时才调
// Decrypt,等客户端再发一包就是等一个永远不来的包(客户端正等我们回应),
// 表现为随机登录超时/重连。明文连接从未有此问题。
std::vector<BYTE> pendingPlain;
if (ok && ctx->tls->Decrypt(NULL, 0, pendingPlain) < 0)
ok = false;
if (ok) {
// 响应必须走加密通道,故先置 Ready 再解析首包。
ctx->tlsState.store(TLS_Ready, std::memory_order_release);
}
// 先释放握手线程持有的引用,再触发任何可能 RemoveStaleContext 的动作(#215 顺序)。
// 注意顺序:解析仍在本线程持有引用期间完成(对象不会在这段窗口里被回收);
// 释放引用之后才允许任何可能 RemoveStaleContext 的动作(#215)。
if (ok && !pendingPlain.empty()) {
// 与工作线程同样的解压上下文,只是现建现用(首包只解析这一批)。
ZSTD_DCtx* dctx = ZSTD_createDCtx();
z_stream z = {};
inflateInit2(&z, 15);
ok = ctx->IsRemoved.load(std::memory_order_acquire) ? TRUE
: ParseReceivedData(ctx, pendingPlain.data(), (DWORD)pendingPlain.size(),
This->m_NotifyProc, dctx, &z);
inflateEnd(&z);
ZSTD_freeDCtx(dctx);
if (!ok) {
Mprintf("[TLS] pending first packet parse failed for %s (closing)\n",
ctx->GetPeerName().c_str());
}
}
ctx->IoRefCount.fetch_sub(1);
if (ok) {