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:
1 parent
b4cb34fea9
commit
a7dd86ff0f
15 files changed
+1218
-96
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include "McpServer.h" // GenerateRandomToken
|
||||
#include "2015Remote.h" // THIS_CFG (GetThisCfg)
|
||||
#include <atlconv.h>
|
||||
#include <afxdlgs.h>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define new DEBUG_NEW
|
||||
@@ -95,6 +96,9 @@ std::string WhitelistForDisplay(const std::string& stored)
|
||||
} // namespace
|
||||
|
||||
BEGIN_MESSAGE_MAP(CMcpSettingsDlg, CDialog)
|
||||
ON_BN_CLICKED(IDC_MCP_HTTPS, &CMcpSettingsDlg::OnHttpsToggle)
|
||||
ON_BN_CLICKED(IDC_MCP_CERT_BROWSE, &CMcpSettingsDlg::OnBrowseCert)
|
||||
ON_BN_CLICKED(IDC_MCP_KEY_BROWSE, &CMcpSettingsDlg::OnBrowseKey)
|
||||
END_MESSAGE_MAP()
|
||||
|
||||
CMcpSettingsDlg::CMcpSettingsDlg(CWnd* parent)
|
||||
@@ -106,7 +110,7 @@ INT_PTR CMcpSettingsDlg::DoModal()
|
||||
{
|
||||
USES_CONVERSION;
|
||||
CString title = _TR("MCP设置");
|
||||
BuildDialogTemplate(m_Template, T2CW(title), 320, 540);
|
||||
BuildDialogTemplate(m_Template, T2CW(title), 320, 640);
|
||||
InitModalIndirect((LPCDLGTEMPLATE)m_Template.data());
|
||||
return CDialog::DoModal();
|
||||
}
|
||||
@@ -133,6 +137,19 @@ BOOL CMcpSettingsDlg::OnInitDialog()
|
||||
m_lblToken.Create(_TR("Token"), WS_CHILD | WS_VISIBLE, r0, this, (UINT)-1);
|
||||
m_editToken.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
|
||||
r0, this, IDC_MCP_TOKEN);
|
||||
m_btnHttps.Create(_TR("启用 HTTPS(本端口同时接受 TLS 连接)"),
|
||||
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX,
|
||||
r0, this, IDC_MCP_HTTPS);
|
||||
m_lblCert.Create(_TR("证书路径"), WS_CHILD | WS_VISIBLE, r0, this, (UINT)-1);
|
||||
m_editCert.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
|
||||
r0, this, IDC_MCP_CERT);
|
||||
m_btnCertBrowse.Create(_TR("浏览"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
|
||||
r0, this, IDC_MCP_CERT_BROWSE);
|
||||
m_lblKey.Create(_TR("私钥路径"), WS_CHILD | WS_VISIBLE, r0, this, (UINT)-1);
|
||||
m_editKey.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
|
||||
r0, this, IDC_MCP_KEY);
|
||||
m_btnKeyBrowse.Create(_TR("浏览"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
|
||||
r0, this, IDC_MCP_KEY_BROWSE);
|
||||
m_btnReadonly.Create(_TR("只读模式(禁命令执行)"),
|
||||
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX,
|
||||
r0, this, IDC_MCP_READONLY);
|
||||
@@ -179,6 +196,13 @@ BOOL CMcpSettingsDlg::OnInitDialog()
|
||||
m_editBind.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_lblToken.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_editToken.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_btnHttps.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_lblCert.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_editCert.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_btnCertBrowse.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_lblKey.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_editKey.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_btnKeyBrowse.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_btnReadonly.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_btnTerminal.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
m_btnRemoteControl.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
|
||||
@@ -210,6 +234,13 @@ BOOL CMcpSettingsDlg::OnInitDialog()
|
||||
int chat = THIS_CFG.GetInt("settings", "McpChat", 0);
|
||||
int keylog = THIS_CFG.GetInt("settings", "McpKeylog", 0);
|
||||
std::string whitelist = THIS_CFG.GetStr("settings", "McpCmdWhitelist", "");
|
||||
int https = THIS_CFG.GetInt("settings", "McpHttps", 0);
|
||||
std::string certPath = THIS_CFG.GetStr("settings", "McpCertPath", "");
|
||||
std::string keyPath = THIS_CFG.GetStr("settings", "McpKeyPath", "");
|
||||
// 未单独配置 MCP 证书时,按启动逻辑的同一回落规则预填设备端口的 [tls] 路径,
|
||||
// 用户改或直接用都可以(保存后即成为 MCP 自己的显式配置)。
|
||||
if (certPath.empty()) certPath = THIS_CFG.GetStr("tls", "CertPath", "");
|
||||
if (keyPath.empty()) keyPath = THIS_CFG.GetStr("tls", "KeyPath", "");
|
||||
|
||||
m_btnEnable.SetCheck(enabled ? BST_CHECKED : BST_UNCHECKED);
|
||||
m_editPort.SetWindowText(CString(std::to_string(port).c_str()));
|
||||
@@ -227,17 +258,66 @@ BOOL CMcpSettingsDlg::OnInitDialog()
|
||||
// 白名单存储为逗号分隔,展示为每行一条。
|
||||
m_editWhitelist.SetWindowText(CString(WhitelistForDisplay(whitelist).c_str()));
|
||||
|
||||
m_btnHttps.SetCheck(https ? BST_CHECKED : BST_UNCHECKED);
|
||||
m_editCert.SetWindowText(CString(certPath.c_str()));
|
||||
m_editKey.SetWindowText(CString(keyPath.c_str()));
|
||||
|
||||
LayoutControls(cli.Width(), cli.Height());
|
||||
UpdateHttpsState();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void CMcpSettingsDlg::UpdateHttpsState()
|
||||
{
|
||||
const BOOL on = (m_btnHttps.GetCheck() == BST_CHECKED);
|
||||
m_lblCert.EnableWindow(on);
|
||||
m_editCert.EnableWindow(on);
|
||||
m_btnCertBrowse.EnableWindow(on);
|
||||
m_lblKey.EnableWindow(on);
|
||||
m_editKey.EnableWindow(on);
|
||||
m_btnKeyBrowse.EnableWindow(on);
|
||||
}
|
||||
|
||||
void CMcpSettingsDlg::OnHttpsToggle()
|
||||
{
|
||||
UpdateHttpsState();
|
||||
}
|
||||
|
||||
void CMcpSettingsDlg::OnBrowseCert()
|
||||
{
|
||||
CString cur;
|
||||
m_editCert.GetWindowText(cur);
|
||||
CFileDialog dlg(TRUE, _T("pem"), cur.IsEmpty() ? nullptr : (LPCTSTR)cur,
|
||||
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
|
||||
_T("Certificate Files (*.pem;*.crt;*.cer)|*.pem;*.crt;*.cer|All Files (*.*)|*.*||"),
|
||||
this);
|
||||
dlg.m_ofn.lpstrTitle = _T("Select Certificate");
|
||||
if (dlg.DoModal() == IDOK)
|
||||
m_editCert.SetWindowText(dlg.GetPathName());
|
||||
}
|
||||
|
||||
void CMcpSettingsDlg::OnBrowseKey()
|
||||
{
|
||||
CString cur;
|
||||
m_editKey.GetWindowText(cur);
|
||||
CFileDialog dlg(TRUE, _T("key"), cur.IsEmpty() ? nullptr : (LPCTSTR)cur,
|
||||
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
|
||||
_T("Key Files (*.key;*.pem)|*.key;*.pem|All Files (*.*)|*.*||"),
|
||||
this);
|
||||
dlg.m_ofn.lpstrTitle = _T("Select Private Key");
|
||||
if (dlg.DoModal() == IDOK)
|
||||
m_editKey.SetWindowText(dlg.GetPathName());
|
||||
}
|
||||
|
||||
void CMcpSettingsDlg::OnOK()
|
||||
{
|
||||
CString sPort, sBind, sToken, sWhitelist;
|
||||
CString sPort, sBind, sToken, sWhitelist, sCert, sKey;
|
||||
m_editPort.GetWindowText(sPort);
|
||||
m_editBind.GetWindowText(sBind);
|
||||
m_editToken.GetWindowText(sToken);
|
||||
m_editWhitelist.GetWindowText(sWhitelist);
|
||||
m_editCert.GetWindowText(sCert);
|
||||
m_editKey.GetWindowText(sKey);
|
||||
bool enabled = (m_btnEnable.GetCheck() == BST_CHECKED);
|
||||
bool readonly = (m_btnReadonly.GetCheck() == BST_CHECKED);
|
||||
bool terminal = (m_btnTerminal.GetCheck() == BST_CHECKED);
|
||||
@@ -248,6 +328,7 @@ void CMcpSettingsDlg::OnOK()
|
||||
bool machineDangerous = (m_btnMachineDangerous.GetCheck() == BST_CHECKED);
|
||||
bool chat = (m_btnChat.GetCheck() == BST_CHECKED);
|
||||
bool keylog = (m_btnKeylog.GetCheck() == BST_CHECKED);
|
||||
bool https = (m_btnHttps.GetCheck() == BST_CHECKED);
|
||||
|
||||
// 危险子集依附于主机管理总开关:没开主机管理就没有工具可放开,直接清除并提示。
|
||||
if (machineDangerous && !machine) {
|
||||
@@ -273,6 +354,27 @@ void CMcpSettingsDlg::OnOK()
|
||||
return;
|
||||
}
|
||||
|
||||
// HTTPS:证书与私钥必须都在且存在(启动时加载失败会直接拒绝监听,故此处先挡住)
|
||||
std::string certPath = CT2A(sCert);
|
||||
std::string keyPath = CT2A(sKey);
|
||||
if (https) {
|
||||
if (certPath.empty()) {
|
||||
MessageBoxL(_TR("请填写证书路径"), _TR("提示"), MB_ICONWARNING);
|
||||
return;
|
||||
}
|
||||
if (keyPath.empty()) {
|
||||
MessageBoxL(_TR("请填写私钥路径"), _TR("提示"), MB_ICONWARNING);
|
||||
return;
|
||||
}
|
||||
DWORD ca = GetFileAttributesA(certPath.c_str());
|
||||
DWORD ka = GetFileAttributesA(keyPath.c_str());
|
||||
if (ca == INVALID_FILE_ATTRIBUTES || (ca & FILE_ATTRIBUTE_DIRECTORY) ||
|
||||
ka == INVALID_FILE_ATTRIBUTES || (ka & FILE_ATTRIBUTE_DIRECTORY)) {
|
||||
MessageBoxL(_TR("证书或私钥文件不存在,请检查路径。"), _TR("提示"), MB_ICONWARNING);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 落盘
|
||||
THIS_CFG.SetInt("settings", "McpEnabled", enabled ? 1 : 0);
|
||||
THIS_CFG.SetInt("settings", "McpPort", port);
|
||||
@@ -290,6 +392,9 @@ void CMcpSettingsDlg::OnOK()
|
||||
std::string whitelist = CT2A(sWhitelist);
|
||||
whitelist = NormalizeWhitelist(whitelist);
|
||||
THIS_CFG.SetStr("settings", "McpCmdWhitelist", whitelist);
|
||||
THIS_CFG.SetInt("settings", "McpHttps", https ? 1 : 0);
|
||||
THIS_CFG.SetStr("settings", "McpCertPath", certPath);
|
||||
THIS_CFG.SetStr("settings", "McpKeyPath", keyPath);
|
||||
|
||||
// 拆成多段可翻译的单行键,中间用 \r\n 连接(多行键无法在 INI 中表示)
|
||||
MessageBox(_TR("MCP 设置已保存。") + _T("\r\n") +
|
||||
@@ -297,7 +402,9 @@ void CMcpSettingsDlg::OnOK()
|
||||
_TR("持久终端、远程控制与管理操作仅在只读模式关闭时生效。") + _T("\r\n") +
|
||||
_TR("主机管理的写操作需只读模式关闭;冻结/解冻进程、停止/重启服务还需启用危险主机操作。") + _T("\r\n") +
|
||||
_TR("远程交谈开关的改动需重启程序生效。") + _T("\r\n") +
|
||||
_TR("键盘记录开关的改动需重启程序生效。"),
|
||||
_TR("键盘记录开关的改动需重启程序生效。") + _T("\r\n") +
|
||||
_TR("HTTPS 开关与证书路径的改动需重启程序生效。") + _T("\r\n") +
|
||||
_TR("证书须为 PEM 格式,私钥须为 PKCS#8 格式。"),
|
||||
_TR("提示"), MB_ICONINFORMATION);
|
||||
|
||||
CDialog::OnOK();
|
||||
@@ -328,6 +435,22 @@ void CMcpSettingsDlg::LayoutControls(int cx, int cy)
|
||||
m_editToken.MoveWindow(margin + labelW, y - 2, cx - margin * 2 - labelW, rowH);
|
||||
y += rowH + gap;
|
||||
|
||||
// HTTPS:勾选后证书/私钥两行才可编辑(未勾选时置灰,值仍保留)。
|
||||
const int browseW = 64;
|
||||
int editW = cx - margin * 2 - labelW - browseW - gap;
|
||||
m_btnHttps.MoveWindow(margin, y, cx - margin * 2, 22);
|
||||
y += 30;
|
||||
|
||||
m_lblCert.MoveWindow(margin, y, labelW, rowH);
|
||||
m_editCert.MoveWindow(margin + labelW, y - 2, editW, rowH);
|
||||
m_btnCertBrowse.MoveWindow(cx - margin - browseW, y - 2, browseW, rowH);
|
||||
y += rowH + gap;
|
||||
|
||||
m_lblKey.MoveWindow(margin, y, labelW, rowH);
|
||||
m_editKey.MoveWindow(margin + labelW, y - 2, editW, rowH);
|
||||
m_btnKeyBrowse.MoveWindow(cx - margin - browseW, y - 2, browseW, rowH);
|
||||
y += rowH + gap;
|
||||
|
||||
m_btnReadonly.MoveWindow(margin, y, cx - margin * 2, 22);
|
||||
y += 30;
|
||||
|
||||
|
||||
Reference in new issue
Block a user