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

+27
View File
@@ -54,6 +54,27 @@ private:
static bool LoadIdentity(const std::string& certPath, const std::string& keyPath);
};
// ---- 额外入站身份(MCP 等第二套证书)----
//
// 进程级身份 ServerTlsIdentity 只服务设备端口,证书由 [tls] 指定(通常是自签
// CN=localhost)。MCP 要暴露给公网客户端,必须用公共 CA 签发的证书,故需要第二份
// 身份。两者可并存:证书/私钥各自加载,持久化私钥用不同的键名(keyName)分开存放,
// 不会互相覆盖。调用方负责在停止时调用 ServerTlsFreeIdentity。
struct ServerTlsExtraIdentity
{
CredHandle cred = {}; // AcquireCredentialsHandle 产出的入站凭据
PCCERT_CONTEXT cert = NULL; // 凭据按名引用的证书上下文
ULONG_PTR key = 0; // NCrypt 持久化私钥句柄(NCRYPT_KEY_HANDLE)
};
// 从 PEM(证书链 + PKCS#8 私钥)构造一份独立入站身份。keyName 为 CNG 持久化键名,
// 须与其它身份不同(例如 L"YamaServerTlsKey" / L"YamaMcpTlsKey")。
bool ServerTlsLoadIdentityFromPem(const std::string& certPath, const std::string& keyPath,
const wchar_t* keyName, ServerTlsExtraIdentity& out);
// 释放 ServerTlsLoadIdentityFromPem 构造的身份(先凭据、再证书、最后删除持久化私钥)。
void ServerTlsFreeIdentity(ServerTlsExtraIdentity& id);
// ---- 单连接服务端 TLS 会话(握手 + 加解密)----
class ServerTlsSession
{
@@ -69,7 +90,13 @@ public:
// 成功返回 true,失败 false(调用方关闭连接,fail-closed)。
bool Handshake(SOCKET sock, const BYTE* initial, int initialLen);
// 同上,但使用调用方提供的入站凭据(MCP TLS 前端用独立的第二份身份,
// 见 ServerTlsLoadIdentityFromPem)。
bool HandshakeWithCred(SOCKET sock, const BYTE* initial, int initialLen, CredHandle cred);
// 解密:把本次 WSARecv 收到的密文 cipher[0..len] 喂入,把能解出的明文追加到 plain。
// len == 0 表示只解出会话里已缓存的密文(握手结束后必须先取一次,否则紧随 Finished
// 到达的首条应用数据会一直躺着,直到客户端再发一包)。
// 返回 0 = 成功(可能无明文);-1 = 错误 / 对端 close_notify(调用方关闭连接)。
int Decrypt(const BYTE* cipher, int len, std::vector<BYTE>& plain);