From c4021e1fc92ef65b228cb90da6236a2ba29b2905 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 1 Nov 2026 15:39:54 +0100 Subject: [PATCH] Feature: Ship a built-in TLS certificate so encryption works out of the box MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- client/TlsChannel.cpp | 29 +- common/iniFile.h | 59 +++- server/2015Remote/2015RemoteDlg.cpp | 56 ++-- server/2015Remote/2015Remote_vs2015.vcxproj | 2 + .../2015Remote_vs2015.vcxproj.filters | 2 + server/2015Remote/BuiltinTlsCert.cpp | 282 ++++++++++++++++++ server/2015Remote/BuiltinTlsCert.h | 44 +++ server/2015Remote/McpSettingsDlg.cpp | 8 +- server/2015Remote/McpTlsProxy.cpp | 3 +- server/2015Remote/ServerTlsSession.cpp | 236 +++++++++++---- server/2015Remote/ServerTlsSession.h | 17 +- server/2015Remote/TlsCertificateDlg.cpp | 69 ++++- server/2015Remote/TlsCertificateDlg.h | 6 + server/2015Remote/lang/de_DE.ini | 5 + server/2015Remote/lang/en_US.ini | 5 + server/2015Remote/lang/zh_TW.ini | 5 + 16 files changed, 697 insertions(+), 131 deletions(-) create mode 100644 server/2015Remote/BuiltinTlsCert.cpp create mode 100644 server/2015Remote/BuiltinTlsCert.h diff --git a/client/TlsChannel.cpp b/client/TlsChannel.cpp index 2386652..586bbf2 100644 --- a/client/TlsChannel.cpp +++ b/client/TlsChannel.cpp @@ -50,32 +50,9 @@ std::string ResolveCertPath() return certPath; } -// 读取注册表 REG_SZ(动态扩容)。iniFile::GetStr 用 512 字节固定缓冲,会把 PEM 正文 -// (约 1.3~1.8KB)截断成空,故此处自行两段式 RegQueryValueEx 读取。 -// 返回 true 表示值存在且为 REG_SZ,out 为去掉末尾 '\0' 后的内容;否则 false。 -bool ReadRegistrySz(HKEY root, const std::string& subKey, const std::string& valueName, std::string& out) -{ - HKEY hKey = NULL; - if (RegOpenKeyExA(root, subKey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) - return false; - - DWORD type = 0, size = 0; - LONG r = RegQueryValueExA(hKey, valueName.c_str(), NULL, &type, NULL, &size); - if (r != ERROR_SUCCESS || type != REG_SZ) { - RegCloseKey(hKey); - return false; - } - std::vector buf(size ? size : 1, 0); - r = RegQueryValueExA(hKey, valueName.c_str(), NULL, &type, - reinterpret_cast(buf.data()), &size); - RegCloseKey(hKey); - if (r != ERROR_SUCCESS) - return false; - if (size > 0 && buf[size - 1] == '\0') - --size; // REG_SZ 的 size 含末尾 '\0' - out.assign(buf.data(), size); - return true; -} +// 读取注册表 REG_SZ(动态扩容,可区分「值不存在」与「值为空」):实现已并入 +// common/iniFile.h 的 ReadRegistrySz——服务端内置 TLS 证书同样要读 1~2KB 的长值, +// 放在公共头里两端共用,避免各写一份。 // 取信任锚 PEM 正文。优先注册表 Software\\settings\tls_cert(REG_SZ 直接存 // PEM 正文,便于后续服务端下发/撤销),其次 settings.ini [settings] tls_cert 路径、 diff --git a/common/iniFile.h b/common/iniFile.h index b130c60..4b992cb 100644 --- a/common/iniFile.h +++ b/common/iniFile.h @@ -160,6 +160,50 @@ inline void CloseUserRegistryKeyIfNeeded(HKEY hKey) (void)hKey; } +// 读取已打开子键下的 REG_SZ 值(两段式动态扩容)。 +// 长的配置值(PEM 正文、base64 证书等,1~2KB)用固定缓冲读会被截断成空/半截, +// 所以凡是「可能超过几百字节」的值一律走这里。 +// 返回 true 表示值存在且为 REG_SZ(内容可能为空串),false 表示不存在或类型不符。 +inline bool ReadRegistryValueSz(HKEY hKey, const char* valueName, std::string& out) +{ + if (hKey == NULL || valueName == NULL) + return false; + + for (int attempt = 0; attempt < 4; ++attempt) { + DWORD type = 0, size = 0; + LONG r = RegQueryValueExA(hKey, valueName, NULL, &type, NULL, &size); + if (r != ERROR_SUCCESS || type != REG_SZ) + return false; + + std::vector buf(size ? size : 1, '\0'); + DWORD got = size; + r = RegQueryValueExA(hKey, valueName, NULL, &type, + reinterpret_cast(buf.data()), &got); + if (r == ERROR_MORE_DATA) + continue; // 两次调用之间值变长了,重来 + if (r != ERROR_SUCCESS) + return false; + + if (got > 0 && buf[got - 1] == '\0') + --got; // REG_SZ 的 size 含末尾 '\0' + out.assign(buf.data(), got); + return true; + } + return false; +} + +// 按「根键 + 子键路径」读取 REG_SZ 值。语义同上(存在性可区分于「读不到」)。 +inline bool ReadRegistrySz(HKEY root, const std::string& subKey, const std::string& valueName, + std::string& out) +{ + HKEY hKey = NULL; + if (RegOpenKeyExA(root, subKey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) + return false; + bool ok = ReadRegistryValueSz(hKey, valueName.c_str(), out); + RegCloseKey(hKey); + return ok; +} + #else #define GetCurrentUserRegistryKey() HKEY_CURRENT_USER #define CloseUserRegistryKeyIfNeeded(hKey) @@ -344,22 +388,17 @@ public: static_cast(Data.size() + 1)) == ERROR_SUCCESS; } - // 读取字符串(使用缓存的键句柄) + // 读取字符串(使用缓存的键句柄)。动态扩容:固定 512 字节缓冲会把长值 + // (PEM 正文、base64 证书等)截断成空,长值一律走 ReadRegistryValueSz。 std::string GetStr(const std::string& MainKey, const std::string& SubKey, const std::string& def = "") override { HKEY hKey = GetCachedKey(MainKey); if (!hKey) return def; - char buffer[512] = { 0 }; - DWORD dwSize = sizeof(buffer); - DWORD dwType = REG_SZ; - - if (RegQueryValueExA(hKey, SubKey.c_str(), NULL, &dwType, - reinterpret_cast(buffer), &dwSize) == ERROR_SUCCESS && - dwType == REG_SZ) { - return std::string(buffer); - } + std::string val; + if (ReadRegistryValueSz(hKey, SubKey.c_str(), val)) + return val; return def; } diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index b8d8988..fb05fe4 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -52,6 +52,7 @@ #include "McpSettingsDlg.h" #include "TlsCertificateDlg.h" #include "ServerTlsSession.h" +#include "BuiltinTlsCert.h" #include "common/md5.h" #include #include @@ -355,7 +356,8 @@ static UINT Indicators[] = { // 状态栏短标签分区(TLS/MCP)的宽度:按当前语言下两种取值的实际文本宽度取大者, // 再留出左右边距。硬编码像素在不同语言/DPI 下容易被截断或浪费空间。 -static int CalcStatusFlagPaneWidth(const CStatusBar& bar, const CString& a, const CString& b) +static int CalcStatusFlagPaneWidth(const CStatusBar& bar, const CString& a, const CString& b, + const CString& c = CString()) { CClientDC dc((CWnd*)&bar); CFont* pOld = nullptr; @@ -377,9 +379,10 @@ static int CalcStatusFlagPaneWidth(const CStatusBar& bar, const CString& a, cons pOld = dc.SelectObject(pFont); CSize szA = dc.GetTextExtent(a); CSize szB = dc.GetTextExtent(b); + CSize szC = c.IsEmpty() ? CSize(0, 0) : dc.GetTextExtent(c); if (pOld != nullptr) dc.SelectObject(pOld); - return max(szA.cx, szB.cx) + 16; + return max(max(szA.cx, szB.cx), szC.cx) + 16; } std::string EventName() @@ -1390,7 +1393,7 @@ VOID CMy2015RemoteDlg::CreateSolidMenu() // ID_TOOL_PLUGIN_SETTINGS 二次 InsertMenu,后插者更贴近锚点,故排在 MCP 下方)。 if (!SHOULD_HIDE_MENU(HIDE_MENU_TLS_CERTIFICATE, MF_TLS_CERTIFICATE)) { UINT tlsFlags = MF_BYCOMMAND; - if (THIS_CFG.GetInt("tls", "Enabled", 0)) { + if (THIS_CFG.GetInt("tls", "Enabled", 1)) { tlsFlags |= MF_CHECKED; // 已启用 TLS 则菜单打勾 } pExtMenu->InsertMenu(ID_TOOL_PLUGIN_SETTINGS, tlsFlags, @@ -2525,8 +2528,12 @@ BOOL CMy2015RemoteDlg::OnInitDialog() if (THIS_CFG.GetInt("settings", "McpHttps", 0)) { std::string certPath = THIS_CFG.GetStr("settings", "McpCertPath", ""); std::string keyPath = THIS_CFG.GetStr("settings", "McpKeyPath", ""); - if (certPath.empty()) certPath = THIS_CFG.GetStr("tls", "CertPath", ""); - if (keyPath.empty()) keyPath = THIS_CFG.GetStr("tls", "KeyPath", ""); + // 设备端用内置证书时,[tls] CertPath 可能只是「切回自定义」保留的历史值,不能再 + // 拿来给 MCP 兜底:内置证书是自签的,公网 MCP 客户端(Claude 等)校验必然失败。 + if (!BuiltinTlsCert::UseBuiltin(THIS_CFG)) { + if (certPath.empty()) certPath = THIS_CFG.GetStr("tls", "CertPath", ""); + if (keyPath.empty()) keyPath = THIS_CFG.GetStr("tls", "KeyPath", ""); + } int internalPort = THIS_CFG.GetInt("settings", "McpInternalPort", 0); if (internalPort <= 0) internalPort = mcpPort + 1; McpServer().SetHttps(true, certPath, keyPath, internalPort); @@ -4125,8 +4132,13 @@ void CMy2015RemoteDlg::UpdateStatusBarStats() // === 分区1/2:TLS / MCP 状态 === // TLS:服务端身份已加载([tls] Enabled 且证书/私钥可用)。与协议切换等处用同一判定, - // 避免「菜单显示已启用但证书加载失败」时状态栏给出错误结论。 - CString strTls = ServerTlsIdentity::IsReady() ? _TR("TLS: 有") : _TR("TLS: 无"); + // 避免「菜单显示已启用但证书加载失败」时状态栏给出错误结论。三态:无 / 有 / 内置 + // (内置证书为程序自动生成的自签证书,与用户自备证书区分开)。 + CString strTls; + if (!ServerTlsIdentity::IsReady()) + strTls = _TR("TLS: 无"); + else + strTls = ServerTlsIdentity::IsBuiltin() ? _TR("TLS: 内置") : _TR("TLS: 有"); // MCP:读配置开关。启用后需重启才真正监听,此处反映的是「已启用」这一配置意图; // 启用时一并标出访问协议(McpHttps=1 走 TLS 前端,证书坏了启动时整体 fail-closed)。 CString strMcp; @@ -4141,7 +4153,8 @@ void CMy2015RemoteDlg::UpdateStatusBarStats() strMcpWidest = strMcp; } // 宽度取当前状态下的最大取值(关闭时收窄,开启时按最长协议串留足) - m_nPaneTlsWidth = CalcStatusFlagPaneWidth(m_StatusBar, _TR("TLS: 有"), _TR("TLS: 无")); + m_nPaneTlsWidth = CalcStatusFlagPaneWidth(m_StatusBar, _TR("TLS: 有"), _TR("TLS: 无"), + _TR("TLS: 内置")); m_nPaneMcpWidth = CalcStatusFlagPaneWidth(m_StatusBar, strMcp, strMcpWidest); m_StatusBar.SetPaneInfo(1, m_StatusBar.GetItemID(1), SBPS_NORMAL, m_nPaneTlsWidth); m_StatusBar.SetPaneText(1, strTls); @@ -5091,16 +5104,10 @@ void CMy2015RemoteDlg::OnExperimentTlsEnable() "(客户端立即生效并断线重连,正在进行的远程会话会中断)"), _T("提示"), MB_ICONQUESTION | MB_YESNO)) return; - // 读取服务端证书(信任锚),随启用指令一并下发;客户端写入注册表 settings\tls_cert。 - std::ifstream certFile(THIS_CFG.GetStr("tls", "CertPath", ""), std::ios::binary); - if (!certFile.is_open()) { - MessageBoxL(_T("读取服务端 TLS 证书失败,无法下发"), _T("提示"), MB_ICONWARNING); - return; - } - std::ostringstream ss; - ss << certFile.rdbuf(); - std::string certPem = ss.str(); - if (certPem.empty()) { + // 取服务端当前身份的证书(信任锚),随启用指令一并下发;客户端写入注册表 + // settings\tls_cert。内置证书与自定义证书(fullchain 全部)都从身份模块取。 + std::string certPem; + if (!ServerTlsIdentity::AnchorPem(certPem) || certPem.empty()) { MessageBoxL(_T("读取服务端 TLS 证书失败,无法下发"), _T("提示"), MB_ICONWARNING); return; } @@ -5194,12 +5201,7 @@ VOID CMy2015RemoteDlg::SwitchSelectedProtocol(UINT nID) MessageBoxL(_T("服务端未启用 TLS 证书,无法切换到 WSS"), _T("提示"), MB_ICONWARNING); return; } - std::ifstream certFile(THIS_CFG.GetStr("tls", "CertPath", ""), std::ios::binary); - if (certFile.is_open()) { - std::ostringstream ss; - ss << certFile.rdbuf(); - certPem = ss.str(); - } + ServerTlsIdentity::AnchorPem(certPem); if (certPem.empty()) { MessageBoxL(_T("读取服务端 TLS 证书失败,无法切换到 WSS"), _T("提示"), MB_ICONWARNING); return; @@ -12669,13 +12671,13 @@ void CMy2015RemoteDlg::OnTlsCertificate() CTlsCertificateDlg dlg(this); if (dlg.DoModal() != IDOK) return; - // 对话框内已写入 [tls] 三键并调 ServerTlsIdentity::Load(THIS_CFG) 热重载, - // 对新建连接即时生效(设计文档 §7.1)。 + // 对话框内已写入 [tls] 四键(Enabled/UseBuiltIn/CertPath/KeyPath)并调 + // ServerTlsIdentity::Load(THIS_CFG) 热重载,对新建连接即时生效(设计文档 §7.1)。 // 菜单在启动时只构建一次(CreateSolidMenu),勾选状态不会自动刷新, // 这里同步「扩展」菜单上「TLS证书」的勾选,使其与 [tls] Enabled 一致。 if (CMenu* pExtMenu = m_MainMenu.GetSubMenu(3)) { pExtMenu->CheckMenuItem(ID_TLS_CERTIFICATE, MF_BYCOMMAND | - (THIS_CFG.GetInt("tls", "Enabled", 0) ? MF_CHECKED : MF_UNCHECKED)); + (THIS_CFG.GetInt("tls", "Enabled", 1) ? MF_CHECKED : MF_UNCHECKED)); } // 状态栏 TLS 分区立即跟随切换结果(对话框内已热重载身份,不等 60s 定时器) diff --git a/server/2015Remote/2015Remote_vs2015.vcxproj b/server/2015Remote/2015Remote_vs2015.vcxproj index e321af9..6d79e36 100644 --- a/server/2015Remote/2015Remote_vs2015.vcxproj +++ b/server/2015Remote/2015Remote_vs2015.vcxproj @@ -334,6 +334,7 @@ + @@ -405,6 +406,7 @@ + NotUsing NotUsing diff --git a/server/2015Remote/2015Remote_vs2015.vcxproj.filters b/server/2015Remote/2015Remote_vs2015.vcxproj.filters index fe8deb9..95b1d47 100644 --- a/server/2015Remote/2015Remote_vs2015.vcxproj.filters +++ b/server/2015Remote/2015Remote_vs2015.vcxproj.filters @@ -96,6 +96,7 @@ + @@ -212,6 +213,7 @@ + diff --git a/server/2015Remote/BuiltinTlsCert.cpp b/server/2015Remote/BuiltinTlsCert.cpp new file mode 100644 index 0000000..ffbaec3 --- /dev/null +++ b/server/2015Remote/BuiltinTlsCert.cpp @@ -0,0 +1,282 @@ +#include "stdafx.h" +#include "BuiltinTlsCert.h" + +#include "common/iniFile.h" // config / iniFile(GetStr/SetStr/GetInt) + +#include +#include + +#pragma comment(lib, "ncrypt.lib") +#pragma comment(lib, "crypt32.lib") + +namespace { + +const char* kCertValue = "BuiltinCert"; // [tls] 单行 base64(DER 证书) +const char* kKeyValue = "BuiltinKey"; // [tls] 单行 base64(PKCS#8 DER 私钥) + +// 生成期间用的 CNG 持久化键名:导出 PKCS#8 后立刻删除,不留在系统里(真正的私钥 +// 存在配置里,由 ServerTlsSession 按需导入成 KSP 持久化键「YamaServerTlsKey-<指纹>」)。 +const wchar_t* kGenKeyName = L"YamaBuiltinTlsKeyGen"; + +// 注意导出类型用 NCRYPT_PKCS8_PRIVATE_KEY_BLOB(L"PKCS8_PRIVATEKEY"),与导入侧 +// (ServerTlsSession 的 NCryptImportKey) 完全对称。不要改成文献里常见的 +// NCRYPT_PKCS8_PRIV_KEY_INFO_BLOB(L"PKCS8_PRIV_KEY_INFO"):Microsoft Software KSP +// 不支持它,实测返回 NTE_INVALID_PARAMETER(0x80090027),且本 SDK 的 ncrypt.h 也没这个宏。 + +// base64 编码(单行,无换行):INI 后端的值不能含换行,统一用无换行形式。 +std::string EncodeB64(const BYTE* data, DWORD len) +{ + DWORD chars = 0; + if (!CryptBinaryToStringA(data, len, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &chars) || chars == 0) + return std::string(); + + std::vector buf(chars, '\0'); + if (!CryptBinaryToStringA(data, len, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, buf.data(), &chars)) + return std::string(); + + // 去掉可能的末尾 '\0' + size_t n = chars; + while (n > 0 && buf[n - 1] == '\0') + --n; + return std::string(buf.data(), n); +} + +bool DecodeB64(const std::string& b64, std::vector& der) +{ + if (b64.empty()) + return false; + + DWORD len = 0; + if (!CryptStringToBinaryA(b64.data(), (DWORD)b64.size(), CRYPT_STRING_BASE64, NULL, &len, NULL, NULL) || len == 0) + return false; + + der.resize(len); + if (!CryptStringToBinaryA(b64.data(), (DWORD)b64.size(), CRYPT_STRING_BASE64, der.data(), &len, NULL, NULL)) + return false; + der.resize(len); + return !der.empty(); +} + +// 按天数平移 SYSTEMTIME(不能用 wYear += 10:遇上 2 月 29 会得到非法日期)。 +void ShiftDays(const SYSTEMTIME& in, LONGLONG days, SYSTEMTIME& out) +{ + FILETIME ft = {}; + if (!SystemTimeToFileTime(&in, &ft)) { + out = in; + return; + } + ULARGE_INTEGER u; + u.LowPart = ft.dwLowDateTime; + u.HighPart = ft.dwHighDateTime; + u.QuadPart += (ULONGLONG)days * 86400ULL * 10000000ULL; + + ft.dwLowDateTime = u.LowPart; + ft.dwHighDateTime = u.HighPart; + if (!FileTimeToSystemTime(&ft, &out)) + out = in; +} + +// 生成一对自签证书 + PKCS#8 私钥(均为单行 base64 的 DER)。 +// 参数选择对齐 docs/TransportTLS_Design.md §9.1 的 `openssl req -x509`: +// RSA-2048、SHA-256、CN=yama-server、CA:TRUE、10 年有效期(提前 1 天生效,容时钟偏差)。 +bool GenerateBlobs(std::string& certB64, std::string& keyB64) +{ + NCRYPT_PROV_HANDLE hProv = 0; + SECURITY_STATUS st = NCryptOpenStorageProvider(&hProv, MS_KEY_STORAGE_PROVIDER, 0); + if (st != ERROR_SUCCESS) { + Mprintf("[TLS] builtin cert: NCryptOpenStorageProvider failed 0x%08x\n", (unsigned)st); + return false; + } + + NCRYPT_KEY_HANDLE hKey = 0; + st = NCryptCreatePersistedKey(hProv, &hKey, NCRYPT_RSA_ALGORITHM, kGenKeyName, 0, + NCRYPT_OVERWRITE_KEY_FLAG); + if (st == ERROR_SUCCESS) { + DWORD bits = 2048; + st = NCryptSetProperty(hKey, NCRYPT_LENGTH_PROPERTY, (PBYTE)&bits, sizeof(bits), 0); + } + if (st == ERROR_SUCCESS) { + // 私钥要以 PKCS#8 导出后存进配置,故必须允许导出。 + DWORD policy = NCRYPT_ALLOW_EXPORT_FLAG | NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG; + st = NCryptSetProperty(hKey, NCRYPT_EXPORT_POLICY_PROPERTY, (PBYTE)&policy, sizeof(policy), 0); + } + if (st == ERROR_SUCCESS) + st = NCryptFinalizeKey(hKey, 0); + if (st != ERROR_SUCCESS) { + Mprintf("[TLS] builtin cert: RSA-2048 key creation failed 0x%08x\n", (unsigned)st); + if (hKey) + NCryptFreeObject(hKey); + NCryptFreeObject(hProv); + return false; + } + + // 主体:CN=yama-server + CERT_NAME_BLOB subject = {}; + DWORD cbName = 0; + bool ok = CertStrToNameW(X509_ASN_ENCODING, L"CN=yama-server", CERT_X500_NAME_STR, + NULL, NULL, &cbName, NULL) != FALSE; + std::vector nameBuf; + if (ok) { + nameBuf.resize(cbName); + subject.pbData = nameBuf.data(); + subject.cbData = cbName; + ok = CertStrToNameW(X509_ASN_ENCODING, L"CN=yama-server", CERT_X500_NAME_STR, + NULL, subject.pbData, &cbName, NULL) != FALSE; + subject.cbData = cbName; + } + if (!ok) { + Mprintf("[TLS] builtin cert: failed to encode subject name (%u)\n", GetLastError()); + NCryptDeleteKey(hKey, 0); + NCryptFreeObject(hProv); + return false; + } + + // BasicConstraints: cA=TRUE → DER: 30 03 01 01 FF + // 客户端用「只信任锚」的独占根链引擎校验,CA:TRUE 让这张自签证书干净地充当根, + // 避免 CERT_TRUST_INVALID_BASIC_CONSTRAINTS。 + BYTE bcValue[] = { 0x30, 0x03, 0x01, 0x01, 0xFF }; + CERT_EXTENSION ext = {}; + ext.pszObjId = (LPSTR)szOID_BASIC_CONSTRAINTS2; + ext.fCritical = TRUE; + ext.Value.cbData = sizeof(bcValue); + ext.Value.pbData = bcValue; + CERT_EXTENSIONS exts = {}; + exts.cExtension = 1; + exts.rgExtension = &ext; + + CRYPT_ALGORITHM_IDENTIFIER sigAlg = {}; + sigAlg.pszObjId = (LPSTR)szOID_RSA_SHA256RSA; + + SYSTEMTIME now = {}; + GetSystemTime(&now); + SYSTEMTIME startTime = {}, endTime = {}; + ShiftDays(now, -1, startTime); // 提前 1 天生效,容两端时钟偏差 + ShiftDays(now, 3650, endTime); // 10 年 + + // 直接把 CNG 私钥句柄交给 CertCreateSelfSignCertificate 签名(它的形参类型就是 + // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE),省去 CRYPT_KEY_PROV_INFO 的按名引用。 + PCCERT_CONTEXT pCert = CertCreateSelfSignCertificate( + (HCRYPTPROV_OR_NCRYPT_KEY_HANDLE)hKey, &subject, 0, NULL, &sigAlg, + &startTime, &endTime, &exts); + if (pCert == NULL) { + Mprintf("[TLS] builtin cert: CertCreateSelfSignCertificate failed (%u)\n", GetLastError()); + NCryptDeleteKey(hKey, 0); + NCryptFreeObject(hProv); + return false; + } + + certB64 = EncodeB64(pCert->pbCertEncoded, pCert->cbCertEncoded); + CertFreeCertificateContext(pCert); + + // 私钥:PKCS#8 DER → base64。与用户自定义证书走同一条导入路径(ServerTlsSession + // 的 BuildIdentityFromDer 只认 PKCS#8)。 + DWORD cbKey = 0; + st = NCryptExportKey(hKey, 0, NCRYPT_PKCS8_PRIVATE_KEY_BLOB, NULL, NULL, 0, &cbKey, 0); + std::vector keyDer; + if (st == ERROR_SUCCESS && cbKey > 0) { + keyDer.resize(cbKey); + st = NCryptExportKey(hKey, 0, NCRYPT_PKCS8_PRIVATE_KEY_BLOB, NULL, + keyDer.data(), cbKey, &cbKey, 0); + } + if (st != ERROR_SUCCESS || cbKey == 0) { + Mprintf("[TLS] builtin cert: NCryptExportKey(PKCS#8) failed 0x%08x\n", (unsigned)st); + NCryptDeleteKey(hKey, 0); + NCryptFreeObject(hProv); + return false; + } + keyDer.resize(cbKey); + keyB64 = EncodeB64(keyDer.data(), (DWORD)keyDer.size()); + + // 生成用键只活在本次调用里,立刻删掉,避免在 KSP 里留一份私钥。 + NCryptDeleteKey(hKey, 0); + NCryptFreeObject(hProv); + + if (certB64.empty() || keyB64.empty()) { + Mprintf("[TLS] builtin cert: base64 encoding failed\n"); + return false; + } + return true; +} + +} // namespace + +bool BuiltinTlsCert::UseBuiltin(config& cfg) +{ + // 显式键优先;未设置时按「有自定义证书路径就不用内置」推导——老部署一定填了 + // CertPath/KeyPath,升级后保持原样;全新部署两项都空,自动落到内置证书。 + int v = cfg.GetInt("tls", "UseBuiltIn", -1); + if (v >= 0) + return v != 0; + + return cfg.GetStr("tls", "CertPath", "").empty() && + cfg.GetStr("tls", "KeyPath", "").empty(); +} + +bool BuiltinTlsCert::ReadDer(config& cfg, std::vector& certDer, std::vector& keyDer) +{ + return DecodeB64(cfg.GetStr("tls", kCertValue, ""), certDer) && + DecodeB64(cfg.GetStr("tls", kKeyValue, ""), keyDer); +} + +bool BuiltinTlsCert::Ensure(config& cfg) +{ + std::vector certDer, keyDer; + if (ReadDer(cfg, certDer, keyDer)) + return true; + + std::string certB64, keyB64; + if (!GenerateBlobs(certB64, keyB64)) + return false; + + if (!cfg.SetStr("tls", kCertValue, certB64) || !cfg.SetStr("tls", kKeyValue, keyB64)) { + Mprintf("[TLS] builtin cert: failed to persist certificate/key into config\n"); + return false; + } + + return ReadDer(cfg, certDer, keyDer); +} + +std::string BuiltinTlsCert::CertPem(config& cfg) +{ + std::vector certDer, keyDer; + if (!ReadDer(cfg, certDer, keyDer)) + return std::string(); + + // CRYPT_STRING_BASE64HEADER 直接产出标准 PEM(头尾行 + 64 列换行), + // 客户端与 Linux 端的信任锚文件都吃这种格式。 + DWORD chars = 0; + if (!CryptBinaryToStringA(certDer.data(), (DWORD)certDer.size(), CRYPT_STRING_BASE64HEADER, NULL, &chars) || chars == 0) + return std::string(); + + std::vector buf(chars, '\0'); + if (!CryptBinaryToStringA(certDer.data(), (DWORD)certDer.size(), CRYPT_STRING_BASE64HEADER, buf.data(), &chars)) + return std::string(); + + size_t n = chars; + while (n > 0 && buf[n - 1] == '\0') + --n; + return std::string(buf.data(), n); +} + +std::string BuiltinTlsCert::Describe(config& cfg) +{ + std::vector certDer, keyDer; + if (!ReadDer(cfg, certDer, keyDer)) + return std::string(); + + PCCERT_CONTEXT cert = CertCreateCertificateContext( + X509_ASN_ENCODING, certDer.data(), (DWORD)certDer.size()); + if (cert == NULL) + return std::string(); + + // CERT_INFO 的 NotAfter 是 FILETIME,转成 SYSTEMTIME 才好取年月日。 + SYSTEMTIME na = {}; + if (!FileTimeToSystemTime(&cert->pCertInfo->NotAfter, &na)) { + CertFreeCertificateContext(cert); + return std::string(); + } + char text[32] = { 0 }; + sprintf_s(text, "%04u-%02u-%02u", na.wYear, na.wMonth, na.wDay); + CertFreeCertificateContext(cert); + return text; +} diff --git a/server/2015Remote/BuiltinTlsCert.h b/server/2015Remote/BuiltinTlsCert.h new file mode 100644 index 0000000..1ebaa26 --- /dev/null +++ b/server/2015Remote/BuiltinTlsCert.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include + +class config; + +// 内置 TLS 证书 +// +// 目的:让服务端 TLS「开箱即用」——全新部署不再需要 openssl 手工生成证书/私钥, +// 首次启用加密时自动生成一张自签证书(RSA-2048 / SHA-256 / CN=yama-server / +// BasicConstraints CA:TRUE / 有效期 10 年)。 +// +// 存储:证书与私钥以【单行 base64(DER)】写进服务端配置的 [tls] 节(BuiltinCert / +// BuiltinKey)。单行是因为服务端配置有两种后端:Release 走注册表 +// (HKCU\Software\YAMA),Debug 的 master 实例走 settings.ini——INI 的值不能含换行, +// 存成单行两种后端都能用同一套读写代码(长值读取见 common/iniFile.h 的 +// ReadRegistryValueSz / iniFile::GetStr)。 +// +// 信任锚:客户端严格 pin 服务端证书(见 client/TlsChannel.cpp 的 VerifyServerCert), +// 所以内置证书无法「自动被信任」——仍由既有「启用 TLS」下发指令把本证书的 PEM 推给 +// 客户端(ServerTlsIdentity::AnchorPem)。证书丢失(注册表被清、换 Windows 用户)会 +// 重新生成一张新的,已下发的客户端必须重新下发信任锚。 +namespace BuiltinTlsCert +{ +// 是否使用内置证书:显式键 [tls] UseBuiltIn 优先;未设置时按「有自定义证书路径即用 +// 自定义、否则用内置」推导(对应「用户已设置证书路径和私钥则不勾选内置证书」)。 +bool UseBuiltin(config& cfg); + +// 确保配置里有一对可解析的内置证书/私钥;缺失或损坏时生成并写回。 +// 首次调用要生成 RSA-2048 并自签,约几百毫秒。失败返回 false(调用方对 TLS 握手 +// fail-closed)。 +bool Ensure(config& cfg); + +// 读取已存的内置证书/私钥(解码为 DER)。任一缺失或损坏返回 false。 +bool ReadDer(config& cfg, std::vector& certDer, std::vector& keyDer); + +// 内置证书的 PEM 文本(下发客户端信任锚用)。未生成时返回空串。 +std::string CertPem(config& cfg); + +// 对话框展示用:证书到期日 "2036-11-01"。未生成/无法解析时返回空串。 +std::string Describe(config& cfg); +} diff --git a/server/2015Remote/McpSettingsDlg.cpp b/server/2015Remote/McpSettingsDlg.cpp index 39a18a8..12e58dd 100644 --- a/server/2015Remote/McpSettingsDlg.cpp +++ b/server/2015Remote/McpSettingsDlg.cpp @@ -3,6 +3,7 @@ #include "LangManager.h" // _TR / MessageBoxL #include "McpServer.h" // GenerateRandomToken #include "2015Remote.h" // THIS_CFG (GetThisCfg) +#include "BuiltinTlsCert.h" // 设备端是否用内置证书(决定 MCP 证书回落) #include #include @@ -244,8 +245,11 @@ BOOL CMcpSettingsDlg::OnInitDialog() 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", ""); + // 设备端用内置证书(自签)时不回落——公网 MCP 客户端校验会失败。 + if (!BuiltinTlsCert::UseBuiltin(THIS_CFG)) { + 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())); diff --git a/server/2015Remote/McpTlsProxy.cpp b/server/2015Remote/McpTlsProxy.cpp index 3d37f42..0d83300 100644 --- a/server/2015Remote/McpTlsProxy.cpp +++ b/server/2015Remote/McpTlsProxy.cpp @@ -6,7 +6,8 @@ namespace { -// MCP 身份在 CNG KSP 里的键名,与设备端口的 YamaServerTlsKey 区分,避免互相覆盖。 +// MCP 身份在 CNG KSP 里的键名基名,与设备端口的 YamaServerTlsKey 区分,避免互相覆盖。 +// 实际键名还会拼上证书指纹(见 BuildIdentityFromDer)。 const wchar_t* kMcpKeyName = L"YamaMcpTlsKey"; const size_t kMaxConns = 64; // 并发连接上限(MCP 客户端数量很少,防资源耗尽) diff --git a/server/2015Remote/ServerTlsSession.cpp b/server/2015Remote/ServerTlsSession.cpp index 34eb013..11e4018 100644 --- a/server/2015Remote/ServerTlsSession.cpp +++ b/server/2015Remote/ServerTlsSession.cpp @@ -5,6 +5,7 @@ // 以便与 Go 服务端(server/go 的 crypto/tls,YAMA_TLS_CERT/KEY)共用同一对密钥。 #include "StdAfx.h" #include "ServerTlsSession.h" +#include "BuiltinTlsCert.h" // 内置证书(缺省证书来源) #include "Server.h" // CONTEXT_OBJECT::FreeTls() #include "common/iniFile.h" // config(GetInt/GetStr) @@ -26,13 +27,16 @@ std::mutex g_identityMtx; bool g_enabled = false; bool g_ready = false; bool g_loadError = false; +bool g_builtin = false; // 当前身份来自内置证书(状态栏「TLS: 内置」用) CredHandle g_cred = {}; bool g_credValid = false; PCCERT_CONTEXT g_cert = NULL; // 与入站凭据同生命周期,FreeIdentity 时释放 NCRYPT_KEY_HANDLE g_persistedKey = 0; // 已持久化到 CNG KSP 的私钥句柄,FreeIdentity 时删除 +std::string g_anchorPem; // 当前身份的信任锚 PEM(下发给客户端) -// 持久化私钥在 CNG KSP 中的名字。用固定名 + NCRYPT_OVERWRITE_KEY_FLAG,重启时覆盖旧键, -// 避免残留;同名键的密钥材料与 server.key 相同,覆盖无副作用。 +// 持久化私钥在 CNG KSP 中的名字基名。实际键名还会拼上证书指纹(见 +// BuildIdentityFromDer),所以换证书不会撞名;同一张证书始终对应同一个键, +// 配合 NCRYPT_OVERWRITE_KEY_FLAG 重启时覆盖旧键,避免残留。 const wchar_t* g_keyName = L"YamaServerTlsKey"; // 一次性读文件为 std::string(二进制安全)。 @@ -131,7 +135,10 @@ bool ServerTlsIdentity::Load(config& cfg) { std::lock_guard lk(g_identityMtx); - bool enabled = cfg.GetInt("tls", "Enabled", 0) != 0; + // 缺省启用:全新部署没有 [tls] Enabled 键时按启用处理,此时证书来源默认是内置证书 + // (首次启用时自动生成)。明文客户端不受影响——设备端口按 ClientHello 嗅探分流。 + bool enabled = cfg.GetInt("tls", "Enabled", 1) != 0; + const bool builtin = BuiltinTlsCert::UseBuiltin(cfg); std::string certPath = cfg.GetStr("tls", "CertPath", ""); std::string keyPath = cfg.GetStr("tls", "KeyPath", ""); @@ -142,6 +149,8 @@ bool ServerTlsIdentity::Load(config& cfg) FreeIdentity(); g_ready = false; g_loadError = false; + g_builtin = false; + g_anchorPem.clear(); if (!enabled) { @@ -149,6 +158,29 @@ bool ServerTlsIdentity::Load(config& cfg) return true; } + if (builtin) + { + std::vector certDer, keyDer; + if (!BuiltinTlsCert::Ensure(cfg) || !BuiltinTlsCert::ReadDer(cfg, certDer, keyDer)) + { + g_loadError = true; + Mprintf("[TLS] FATAL: [tls] builtin certificate unavailable; " + "refusing TLS ClientHello (fail-closed)\n"); + return false; + } + if (!LoadIdentityFromDer(certDer, keyDer)) + { + g_loadError = true; + return false; + } + // 下发给客户端的信任锚 = 内置自签证书本身(客户端严格 pin 这一张)。 + g_anchorPem = BuiltinTlsCert::CertPem(cfg); + g_builtin = true; + g_ready = true; + Mprintf("[TLS] server TLS enabled: builtin certificate\n"); + return true; + } + if (certPath.empty() || keyPath.empty()) { g_loadError = true; @@ -163,6 +195,10 @@ bool ServerTlsIdentity::Load(config& cfg) return false; } + // 信任锚 = 证书文件全文:fullchain 场景下中间证书也要一并下发,客户端才能建链。 + if (!ReadFileText(certPath, g_anchorPem)) + g_anchorPem.clear(); + g_ready = true; Mprintf("[TLS] server TLS enabled: cert=%s key=%s\n", certPath.c_str(), keyPath.c_str()); return true; @@ -190,33 +226,23 @@ SECURITY_STATUS AcquireInboundCredFor(PCCERT_CONTEXT cert, CredHandle& cred) NULL, &sc, NULL, NULL, &cred, &tsExpiry); } -// 按给定 PEM(证书链 + PKCS#8 私钥)与 CNG 持久化键名构造一份入站身份。 -// 设备端口(ServerTlsIdentity,键名 YamaServerTlsKey)与 MCP 前端(独立键名)共用这段 -// 逻辑;键名不同则两份身份可并存,互不覆盖。 +// 按「DER 证书链 + PKCS#8 私钥 DER」与 CNG 持久化键名构造一份入站身份。 +// 文件证书(BuildIdentity)、内置证书(BuiltinTlsCert)与 MCP 前端(独立键名)共用这段 +// 逻辑;键名不同则多份身份可并存,互不覆盖。source 仅用于日志定位。 // 成功时 outCred / outCert / outKey 均有效,生命周期由调用方释放。 -bool BuildIdentity(const std::string& certPath, const std::string& keyPath, - const wchar_t* keyName, - CredHandle& outCred, PCCERT_CONTEXT& outCert, NCRYPT_KEY_HANDLE& outKey) +bool BuildIdentityFromDer(const std::vector>& certDers, + const std::vector& keyDer, + const char* source, + const wchar_t* keyName, + CredHandle& outCred, PCCERT_CONTEXT& outCert, NCRYPT_KEY_HANDLE& outKey) { - std::string certPem, keyPem; - if (!ReadFileText(certPath, certPem)) + if (certDers.empty()) { - Mprintf("[TLS] FATAL: cannot read cert file %s\n", certPath.c_str()); - return false; - } - if (!ReadFileText(keyPath, keyPem)) - { - Mprintf("[TLS] FATAL: cannot read key file %s\n", keyPath.c_str()); + Mprintf("[TLS] FATAL: empty certificate for %s\n", source ? source : "?"); return false; } - // 1) 证书:PEM → DER → PCCERT_CONTEXT(第一块为叶证书)。 - std::vector> certDers = PemToDerList(certPem); - if (certDers.empty()) - { - Mprintf("[TLS] FATAL: cert file %s is not a valid PEM\n", certPath.c_str()); - return false; - } + // 1) 证书:DER → PCCERT_CONTEXT(第一块为叶证书)。 PCCERT_CONTEXT cert = CertCreateCertificateContext(X509_ASN_ENCODING, certDers[0].data(), (DWORD)certDers[0].size()); if (cert == NULL) @@ -237,7 +263,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, { Mprintf("[TLS] WARN: chain cert #%u in %s is unreadable (%u); " "handshake will only send the certificates already in the store\n", - (unsigned)i, certPath.c_str(), GetLastError()); + (unsigned)i, source ? source : "?", GetLastError()); continue; } static HCERTSTORE s_caStore = NULL; // 进程级;关掉反而让 Schannel 后续取不到 @@ -255,12 +281,45 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, { Mprintf("[TLS] WARN: cannot add chain cert #%u from %s to the user CA store (%u); " "handshake may not send the full chain\n", - (unsigned)i, certPath.c_str(), GetLastError()); + (unsigned)i, source ? source : "?", GetLastError()); } CertFreeCertificateContext(chainCert); } - // 2) 私钥:PEM → DER → 持久化到 CNG KSP(仅支持 PKCS#8;PKCS#1 给出可操作的转换提示)。 + // 持久化键名 = 调用方给的基名 + 证书指纹前缀。 + // + // 为什么不能只用固定键名:私钥是按名绑到证书上的(下面的 CERT_KEY_PROV_INFO), + // Schannel 每次握手都经 CryptAcquireCertificatePrivateKey 用这个名去 KSP 重开私钥。 + // 同一个 Windows 用户下同时跑 Debug 与 Release 两个服务端实例(证书各不相同)时, + // 后加载的实例会把同名键覆盖掉,先加载的实例下一次握手就拿到一把与证书不匹配的私钥, + // 握手直接失败。键名带上证书指纹后,不同证书各占一个键;同一张证书(多实例共用一份 + // PEM,例如内置证书与自定义证书指向同一对文件)仍然共用同一个键,与既有部署一致。 + std::wstring effectiveKeyName(keyName); + { + BYTE hash[20] = {}; + DWORD cbHash = sizeof(hash); + if (CertGetCertificateContextProperty(cert, CERT_SHA1_HASH_PROP_ID, hash, &cbHash) && + cbHash >= 6) + { + static const WCHAR kHex[] = L"0123456789abcdef"; + effectiveKeyName += L'-'; + for (int i = 0; i < 6; ++i) + { + effectiveKeyName += kHex[hash[i] >> 4]; + effectiveKeyName += kHex[hash[i] & 0x0F]; + } + } + else + { + // 拿不到指纹就退回固定键名:只是失去多实例隔离,不影响单实例工作。 + Mprintf("[TLS] WARN: cannot read cert thumbprint for %s (%u); " + "using fixed key name %ls\n", + source ? source : "?", GetLastError(), keyName); + } + } + + // 2) 私钥:PKCS#8 DER → 持久化到 CNG KSP(文件版的 PEM→DER 与 PKCS#1 提示在 + // BuildIdentity 里做,内置证书本来就是 PKCS#8 DER)。 // // 关键:Schannel 入站凭据(AcquireCredentialsHandle)需要「能按名重开」的持久化私钥。 // 匿名临时键不行:绑定 CERT_NCRYPT_KEY_HANDLE_PROP_ID 或 CERT_KEY_CONTEXT_PROP_ID @@ -278,7 +337,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, { Mprintf("[TLS] FATAL: cert %s uses unsupported public key type (%s); " "RSA and ECDSA (P-256/P-384/P-521) are supported\n", - certPath.c_str(), certKeyOid ? certKeyOid : "unknown"); + source ? source : "?", certKeyOid ? certKeyOid : "unknown"); CertFreeCertificateContext(cert); return false; } @@ -299,21 +358,14 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, { Mprintf("[TLS] FATAL: cert %s uses an ECDSA curve other than P-256/P-384/P-521 " "(or explicit curve parameters); convert it to an RSA or named-curve key\n", - certPath.c_str()); + source ? source : "?"); CertFreeCertificateContext(cert); return false; } } - std::vector keyDer; NCRYPT_KEY_HANDLE persistedKey = 0; - if (DetectKeyFormat(keyPem) == KF_PKCS8) + if (!keyDer.empty()) { - if (!PemToDer(keyPem, keyDer)) - { - Mprintf("[TLS] FATAL: key file %s is not a valid PEM\n", keyPath.c_str()); - CertFreeCertificateContext(cert); - return false; - } // 显式打开 Microsoft Software Key Storage Provider。hProvider 传 NULL 会让 // NCryptImportKey 走「默认提供者」解析,实测返回 NTE_INVALID_HANDLE(0x80090026), // 故必须显式取得 KSP 句柄(临时键独立于 hProv,导入后即可释放)。 @@ -336,7 +388,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, // PKCS#8)→ 写入持久化键 → finalize。 if (certKeyIsEc) { - std::wstring nameW(keyName); + std::wstring nameW(effectiveKeyName); NCryptBuffer nameBuf = {}; nameBuf.cbBuffer = (ULONG)((nameW.size() + 1) * sizeof(WCHAR)); nameBuf.BufferType = NCRYPTBUFFER_PKCS_KEY_NAME; @@ -347,14 +399,14 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, desc.pBuffers = &nameBuf; st = NCryptImportKey(hProv, NULL, NCRYPT_PKCS8_PRIVATE_KEY_BLOB, &desc, - &persistedKey, keyDer.data(), (DWORD)keyDer.size(), + &persistedKey, const_cast(keyDer.data()), (DWORD)keyDer.size(), NCRYPT_OVERWRITE_KEY_FLAG); } else { NCRYPT_KEY_HANDLE tmp = 0; st = NCryptImportKey(hProv, NULL, NCRYPT_PKCS8_PRIVATE_KEY_BLOB, NULL, - &tmp, keyDer.data(), (DWORD)keyDer.size(), 0); + &tmp, const_cast(keyDer.data()), (DWORD)keyDer.size(), 0); if (st == ERROR_SUCCESS) { DWORD policy = NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG | NCRYPT_ALLOW_EXPORT_FLAG; @@ -374,7 +426,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, if (st == ERROR_SUCCESS) { st = NCryptCreatePersistedKey(hProv, &persistedKey, BCRYPT_RSA_ALGORITHM, - keyName, AT_KEYEXCHANGE, + effectiveKeyName.c_str(), AT_KEYEXCHANGE, NCRYPT_OVERWRITE_KEY_FLAG); } if (st == ERROR_SUCCESS) @@ -393,7 +445,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, if (st != ERROR_SUCCESS) { Mprintf("[TLS] FATAL: failed to persist PKCS#8 key for %s (0x%08x)\n", - keyPath.c_str(), (unsigned)st); + source ? source : "?", (unsigned)st); if (persistedKey) NCryptFreeObject(persistedKey); CertFreeCertificateContext(cert); @@ -402,10 +454,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, } else { - // PKCS#1(RSA/EC PRIVATE KEY)或未知格式:本次不支持,给转换命令。 - Mprintf("[TLS] FATAL: key file %s must be PKCS#8 (-----BEGIN PRIVATE KEY-----). " - "Convert with: openssl pkcs8 -topk8 -nocrypt -in %s -out %s\n", - keyPath.c_str(), keyPath.c_str(), keyPath.c_str()); + Mprintf("[TLS] FATAL: empty private key for %s\n", source ? source : "?"); CertFreeCertificateContext(cert); return false; } @@ -414,7 +463,7 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, // 在 MS_KEY_STORAGE_PROVIDER 里用 pwszContainerName 重开该键。 // 4) 换入站凭据(单向 TLS,不请求客户端证书)。 CRYPT_KEY_PROV_INFO provInfo = {}; - provInfo.pwszContainerName = (LPWSTR)keyName; + provInfo.pwszContainerName = const_cast(effectiveKeyName.c_str()); provInfo.pwszProvName = (LPWSTR)MS_KEY_STORAGE_PROVIDER; provInfo.dwProvType = 0; // 0 = CNG(无 CAPI provider type) provInfo.dwKeySpec = certKeyIsEc ? 0 : AT_KEYEXCHANGE; // ECDSA 键无 legacy key spec @@ -441,11 +490,68 @@ bool BuildIdentity(const std::string& certPath, const std::string& keyPath, return false; } + // 键名进日志:排查「两个实例抢同一个持久化键」时,一眼能看出各自用的是哪个键。 + Mprintf("[TLS] identity ready: %s (CNG key %ls)\n", + source ? source : "?", effectiveKeyName.c_str()); + outCert = cert; outKey = persistedKey; outCred = cred; return true; } + +// 文件版:读证书/私钥 PEM(证书可以是 fullchain),转 DER 后走上面的共用构造逻辑。 +bool BuildIdentity(const std::string& certPath, const std::string& keyPath, + const wchar_t* keyName, + CredHandle& outCred, PCCERT_CONTEXT& outCert, NCRYPT_KEY_HANDLE& outKey) +{ + std::string certPem, keyPem; + if (!ReadFileText(certPath, certPem)) + { + Mprintf("[TLS] FATAL: cannot read cert file %s\n", certPath.c_str()); + return false; + } + if (!ReadFileText(keyPath, keyPem)) + { + Mprintf("[TLS] FATAL: cannot read key file %s\n", keyPath.c_str()); + return false; + } + + std::vector> certDers = PemToDerList(certPem); + if (certDers.empty()) + { + Mprintf("[TLS] FATAL: cert file %s is not a valid PEM\n", certPath.c_str()); + return false; + } + + // 仅支持 PKCS#8;PKCS#1 给出可操作的转换提示。 + if (DetectKeyFormat(keyPem) != KF_PKCS8) + { + Mprintf("[TLS] FATAL: key file %s must be PKCS#8 (-----BEGIN PRIVATE KEY-----). " + "Convert with: openssl pkcs8 -topk8 -nocrypt -in %s -out %s\n", + keyPath.c_str(), keyPath.c_str(), keyPath.c_str()); + return false; + } + std::vector keyDer; + if (!PemToDer(keyPem, keyDer)) + { + Mprintf("[TLS] FATAL: key file %s is not a valid PEM\n", keyPath.c_str()); + return false; + } + + return BuildIdentityFromDer(certDers, keyDer, certPath.c_str(), keyName, outCred, outCert, outKey); +} + +// 把构造好的身份交给进程级生命周期(FreeIdentity 按序释放)。 +void AdoptIdentity(CredHandle cred, PCCERT_CONTEXT cert, NCRYPT_KEY_HANDLE key) +{ + // 证书上下文的生命周期交给 g_cert,随 FreeIdentity 释放, + // 保证在 FreeCredentialsHandle 之前私钥一直可用。 + g_cred = cred; + g_cert = cert; + g_persistedKey = key; + g_credValid = true; +} } // namespace bool ServerTlsIdentity::LoadIdentity(const std::string& certPath, const std::string& keyPath) @@ -456,12 +562,21 @@ bool ServerTlsIdentity::LoadIdentity(const std::string& certPath, const std::str if (!BuildIdentity(certPath, keyPath, g_keyName, cred, cert, key)) return false; - // 成功:把证书上下文生命周期交给进程级 g_cert,随 FreeIdentity 释放, - // 保证在 FreeCredentialsHandle 之前私钥一直可用。 - g_cred = cred; - g_cert = cert; - g_persistedKey = key; - g_credValid = true; + AdoptIdentity(cred, cert, key); + return true; +} + +bool ServerTlsIdentity::LoadIdentityFromDer(const std::vector& certDer, + const std::vector& keyDer) +{ + CredHandle cred = {}; + PCCERT_CONTEXT cert = NULL; + NCRYPT_KEY_HANDLE key = 0; + std::vector> certDers(1, certDer); + if (!BuildIdentityFromDer(certDers, keyDer, "builtin", g_keyName, cred, cert, key)) + return false; + + AdoptIdentity(cred, cert, key); return true; } @@ -483,12 +598,27 @@ bool ServerTlsIdentity::IsLoadError() return g_loadError; } +bool ServerTlsIdentity::IsBuiltin() +{ + std::lock_guard lk(g_identityMtx); + return g_ready && g_credValid && g_builtin; +} + CredHandle ServerTlsIdentity::Cred() { std::lock_guard lk(g_identityMtx); return g_cred; } +bool ServerTlsIdentity::AnchorPem(std::string& out) +{ + std::lock_guard lk(g_identityMtx); + if (!g_ready || g_anchorPem.empty()) + return false; + out = g_anchorPem; + return true; +} + // ===================================================================================== // ServerTlsSession —— 单连接会话(握手 + 加解密) // ===================================================================================== diff --git a/server/2015Remote/ServerTlsSession.h b/server/2015Remote/ServerTlsSession.h index 1482680..286fa5f 100644 --- a/server/2015Remote/ServerTlsSession.h +++ b/server/2015Remote/ServerTlsSession.h @@ -37,21 +37,29 @@ class config; // common/iniFile.h(仅需引用,避免把 ini 头拖进 Ser class ServerTlsIdentity { public: - // 从 cfg(THIS_CFG)读 [tls] 节:Enabled / CertPath / KeyPath,并加载身份。 + // 从 cfg(THIS_CFG)读 [tls] 节:Enabled(缺省启用)/ UseBuiltIn / CertPath / KeyPath, + // 并加载身份。证书来源两种:内置证书(服务端首次启用时自动生成,见 BuiltinTlsCert.h) + // 或用户指定的证书/私钥文件。 // 返回 true 表示「配置已生效」(开关关、或开关开且身份加载成功);开关开但加载 // 失败时返回 false 且 IsLoadError()==true,服务端须对 TLS ClientHello fail-closed。 static bool Load(config& cfg); - static bool IsEnabled(); // [tls] Enabled != 0 + static bool IsEnabled(); // [tls] Enabled != 0(缺省为启用) static bool IsReady(); // 开关开 && 身份加载成功(有可用入站凭据) static bool IsLoadError(); // 开关开 && 身份加载失败(fail-closed) + static bool IsBuiltin(); // 就绪且当前身份来自内置证书(状态栏「TLS: 内置」) // 入站凭据(AcquireCredentialsHandle 产出)。返回拷贝;句柄值本身可安全拷贝。 static CredHandle Cred(); + // 当前身份的信任锚 PEM(自定义证书时为证书文件全文,fullchain 一并带上; + // 内置证书时为自签证书的 PEM)。用于「启用 TLS / 切换到 WSS」指令下发给客户端。 + static bool AnchorPem(std::string& out); + private: static void FreeIdentity(); static bool LoadIdentity(const std::string& certPath, const std::string& keyPath); + static bool LoadIdentityFromDer(const std::vector& certDer, const std::vector& keyDer); }; // ---- 额外入站身份(MCP 等第二套证书)---- @@ -67,8 +75,9 @@ struct ServerTlsExtraIdentity ULONG_PTR key = 0; // NCrypt 持久化私钥句柄(NCRYPT_KEY_HANDLE) }; -// 从 PEM(证书链 + PKCS#8 私钥)构造一份独立入站身份。keyName 为 CNG 持久化键名, -// 须与其它身份不同(例如 L"YamaServerTlsKey" / L"YamaMcpTlsKey")。 +// 从 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); diff --git a/server/2015Remote/TlsCertificateDlg.cpp b/server/2015Remote/TlsCertificateDlg.cpp index 873566f..b9ee99d 100644 --- a/server/2015Remote/TlsCertificateDlg.cpp +++ b/server/2015Remote/TlsCertificateDlg.cpp @@ -3,6 +3,7 @@ #include "LangManager.h" // _TR / MessageBoxL #include "2015Remote.h" // THIS_CFG (GetThisCfg) #include "ServerTlsSession.h" // ServerTlsIdentity::Load / IsLoadError +#include "BuiltinTlsCert.h" // 内置证书状态与有效期 #include "common/iniFile.h" // config::GetInt/GetStr/SetInt/SetStr #include #include @@ -53,6 +54,7 @@ void BuildDialogTemplate(std::vector& out, LPCWSTR caption, short cx, shor BEGIN_MESSAGE_MAP(CTlsCertificateDlg, CDialog) ON_BN_CLICKED(IDC_TLS_CERT_BROWSE, &CTlsCertificateDlg::OnBrowseCert) ON_BN_CLICKED(IDC_TLS_KEY_BROWSE, &CTlsCertificateDlg::OnBrowseKey) + ON_BN_CLICKED(IDC_TLS_BUILTIN, &CTlsCertificateDlg::OnBuiltinToggle) END_MESSAGE_MAP() CTlsCertificateDlg::CTlsCertificateDlg(CWnd* parent) @@ -82,6 +84,11 @@ BOOL CTlsCertificateDlg::OnInitDialog() m_btnEnable.Create(_TR("启用加密"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, r0, this, IDC_TLS_ENABLE); + m_btnBuiltin.Create(_TR("内置证书"), + WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, + r0, this, IDC_TLS_BUILTIN); + m_lblBuiltinInfo.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_LEFT, + r0, this, IDC_TLS_BUILTIN_INFO); 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_TLS_CERT); @@ -101,6 +108,8 @@ BOOL CTlsCertificateDlg::OnInitDialog() HFONT hFont = (HFONT)::SendMessage(GetSafeHwnd(), WM_GETFONT, 0, 0); if (hFont) { m_btnEnable.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0)); + m_btnBuiltin.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0)); + m_lblBuiltinInfo.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)); @@ -111,41 +120,74 @@ BOOL CTlsCertificateDlg::OnInitDialog() m_btnCancel.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0)); } - // 回填配置 - int enabled = THIS_CFG.GetInt("tls", "Enabled", 0); + // 回填配置(Enabled 缺省为启用,与 ServerTlsIdentity::Load 保持一致) + int enabled = THIS_CFG.GetInt("tls", "Enabled", 1); std::string cert = THIS_CFG.GetStr("tls", "CertPath", ""); std::string key = THIS_CFG.GetStr("tls", "KeyPath", ""); m_btnEnable.SetCheck(enabled ? BST_CHECKED : BST_UNCHECKED); + m_btnBuiltin.SetCheck(BuiltinTlsCert::UseBuiltin(THIS_CFG) ? BST_CHECKED : BST_UNCHECKED); m_editCert.SetWindowText(CString(cert.c_str())); m_editKey.SetWindowText(CString(key.c_str())); LayoutControls(cli.Width(), cli.Height()); + UpdateBuiltinUi(); return TRUE; } +// 勾选/取消「内置证书」:自定义证书/私钥控件随之置灰/恢复。路径本身不清空, +// 切回自定义证书时原来填的还在。 +void CTlsCertificateDlg::OnBuiltinToggle() +{ + UpdateBuiltinUi(); +} + +void CTlsCertificateDlg::UpdateBuiltinUi() +{ + const BOOL builtin = (m_btnBuiltin.GetCheck() == BST_CHECKED); + + m_lblCert.EnableWindow(!builtin); + m_editCert.EnableWindow(!builtin); + m_btnCertBrowse.EnableWindow(!builtin); + m_lblKey.EnableWindow(!builtin); + m_editKey.EnableWindow(!builtin); + m_btnKeyBrowse.EnableWindow(!builtin); + + CString info; + if (builtin) { + std::string notAfter = BuiltinTlsCert::Describe(THIS_CFG); + info = notAfter.empty() + ? _TR("首次启用时自动生成") + : _TR("自签证书,有效期至") + _T(" ") + CString(notAfter.c_str()); + } + m_lblBuiltinInfo.SetWindowText(info); +} + void CTlsCertificateDlg::OnOK() { CString sCert, sKey; m_editCert.GetWindowText(sCert); m_editKey.GetWindowText(sKey); bool enabled = (m_btnEnable.GetCheck() == BST_CHECKED); + bool builtin = (m_btnBuiltin.GetCheck() == BST_CHECKED); std::string cert = CT2A(sCert); std::string key = CT2A(sKey); - // 校验:启用加密时必须填写证书/私钥路径 - if (enabled && cert.empty()) { + // 校验:启用加密且用自定义证书时,必须填写证书/私钥路径 + if (enabled && !builtin && cert.empty()) { MessageBoxL("请填写证书路径", "提示", MB_ICONWARNING); return; } - if (enabled && key.empty()) { + if (enabled && !builtin && key.empty()) { MessageBoxL("请填写私钥路径", "提示", MB_ICONWARNING); return; } - // 落盘 [tls] 三键 + // 落盘 [tls] 四键。UseBuiltIn 显式写入,避免「路径非空」推断在清空路径时误判; + // 用内置证书时 CertPath/KeyPath 原样保留,方便之后切回自定义证书。 THIS_CFG.SetInt("tls", "Enabled", enabled ? 1 : 0); + THIS_CFG.SetInt("tls", "UseBuiltIn", builtin ? 1 : 0); THIS_CFG.SetStr("tls", "CertPath", cert); THIS_CFG.SetStr("tls", "KeyPath", key); @@ -154,7 +196,10 @@ void CTlsCertificateDlg::OnOK() // 未来热切换需引用计数 + 惰性释放(见 ServerTlsSession.cpp FreeIdentity 注释),本次不做。 ServerTlsIdentity::Load(THIS_CFG); - if (ServerTlsIdentity::IsLoadError()) { + if (ServerTlsIdentity::IsLoadError() && builtin) { + MessageBoxL("内置证书生成/加载失败,加密功能未生效。请查看日志,或取消勾选「内置证书」后改用本地证书与私钥。", + "提示", MB_ICONWARNING); + } else if (ServerTlsIdentity::IsLoadError()) { MessageBoxL("证书/私钥加载失败,加密功能未生效。请检查文件路径与格式(证书 PEM、私钥 PKCS#8)。", "提示", MB_ICONWARNING); } else { @@ -195,7 +240,9 @@ void CTlsCertificateDlg::OnBrowseKey() void CTlsCertificateDlg::LayoutControls(int cx, int cy) { const int margin = 14; - const int labelW = 72; + // 标签宽按英文 "Private Key Path" 取(~80px):72px 时英文标签会折成两行, + // 中文/繁中都是 4 个字,绰绰有余。 + const int labelW = 84; const int rowH = 24; const int gap = 10; const int browseW = 64; @@ -206,6 +253,12 @@ void CTlsCertificateDlg::LayoutControls(int cx, int cy) m_btnEnable.MoveWindow(margin, y, cx - margin * 2, 22); y += 30; + // 「内置证书」与它右侧的说明同排 + const int builtinW = 92; + m_btnBuiltin.MoveWindow(margin, y, builtinW, 22); + m_lblBuiltinInfo.MoveWindow(margin + builtinW + 4, y + 4, cx - margin * 2 - builtinW - 4, rowH); + y += rowH + gap; + int editW = cx - margin * 2 - labelW - browseW - gap; m_lblCert.MoveWindow(margin, y, labelW, rowH); diff --git a/server/2015Remote/TlsCertificateDlg.h b/server/2015Remote/TlsCertificateDlg.h index 895b0d1..3e79170 100644 --- a/server/2015Remote/TlsCertificateDlg.h +++ b/server/2015Remote/TlsCertificateDlg.h @@ -27,9 +27,13 @@ private: IDC_TLS_CERT_BROWSE = 2003, // 证书路径「浏览...」按钮 IDC_TLS_KEY = 2004, // 私钥路径编辑框 IDC_TLS_KEY_BROWSE = 2005, // 私钥路径「浏览...」按钮 + IDC_TLS_BUILTIN = 2006, // 「内置证书」复选框(缺省勾选) + IDC_TLS_BUILTIN_INFO= 2007, // 内置证书说明(只读静态文本) }; CButton m_btnEnable; + CButton m_btnBuiltin; + CStatic m_lblBuiltinInfo; CStatic m_lblCert, m_lblKey; CEdit m_editCert, m_editKey; CButton m_btnCertBrowse, m_btnKeyBrowse; @@ -40,4 +44,6 @@ private: void LayoutControls(int cx, int cy); void OnBrowseCert(); void OnBrowseKey(); + void OnBuiltinToggle(); // 勾选状态变化:置灰/恢复自定义证书控件 + void UpdateBuiltinUi(); // 同步内置证书说明文本 }; diff --git a/server/2015Remote/lang/de_DE.ini b/server/2015Remote/lang/de_DE.ini index 73411ab..c41369c 100644 --- a/server/2015Remote/lang/de_DE.ini +++ b/server/2015Remote/lang/de_DE.ini @@ -2123,6 +2123,10 @@ TLS֤ ÆôÓüÓÃÜ=Verschluesselung aktivieren ֤Êé·¾¶=Zertifikatspfad ˽Կ·¾¶=Pfad zum privaten Schluessel +ÄÚÖÃ֤Êé=Integriertes Zertifikat +×Ôǩ֤Ê飬ÓÐЧÆÚÖÁ=Selbstsigniertes Zertifikat, gueltig bis +Ê״ÎÆôÓÃʱ×Զ¯Éú³É=Wird bei der ersten Verwendung automatisch erzeugt +ÄÚÖÃ֤ÊéÉú³É/¼ÓÔØʧ°ܣ¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Çë²鿴ÈÕ־£¬»òȡÏû¹´ѡ¡¸ÄÚÖÃ֤Ê项ºó¸ÄÓñ¾µØ֤ÊéÓë˽Կ¡£=Integriertes Zertifikat konnte nicht erzeugt oder geladen werden; Verschluesselung ist inaktiv. Bitte Log pruefen oder "Integriertes Zertifikat" abwaehlen und ein lokales Zertifikat mit privatem Schluessel verwenden. ÇëÌîд֤Êé·¾¶=Bitte Zertifikatspfad angeben ÇëÌîд˽Կ·¾¶=Bitte Pfad zum privaten Schluessel angeben TLS ÅäÖÃÒѱ£´档=TLS-Einstellungen gespeichert. @@ -2171,6 +2175,7 @@ Y ; ===== Status Bar TLS/MCP ===== TLS: ÓÐ=TLS: Ja TLS: ÎÞ=TLS: Nein +TLS: ÄÚÖÃ=TLS: Integriert MCP: ¿ª=MCP: An MCP: ¹Ø=MCP: Aus ; ===== MCP Settings (file write / HTTPS) ===== diff --git a/server/2015Remote/lang/en_US.ini b/server/2015Remote/lang/en_US.ini index 607eb52..6cfa5b4 100644 --- a/server/2015Remote/lang/en_US.ini +++ b/server/2015Remote/lang/en_US.ini @@ -2138,6 +2138,10 @@ TLS֤ ÆôÓüÓÃÜ=Enable Encryption ֤Êé·¾¶=Certificate Path ˽Կ·¾¶=Private Key Path +ÄÚÖÃ֤Êé=Built-in Certificate +×Ôǩ֤Ê飬ÓÐЧÆÚÖÁ=Self-signed certificate, valid until +Ê״ÎÆôÓÃʱ×Զ¯Éú³É=Generated automatically on first use +ÄÚÖÃ֤ÊéÉú³É/¼ÓÔØʧ°ܣ¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Çë²鿴ÈÕ־£¬»òȡÏû¹´ѡ¡¸ÄÚÖÃ֤Ê项ºó¸ÄÓñ¾µØ֤ÊéÓë˽Կ¡£=Failed to generate or load the built-in certificate; encryption is not active. Check the log, or clear Built-in Certificate and use a local certificate and private key instead. ÇëÌîд֤Êé·¾¶=Please specify the certificate path ÇëÌîд˽Կ·¾¶=Please specify the private key path TLS ÅäÖÃÒѱ£´档=TLS settings saved. @@ -2187,6 +2191,7 @@ WSS ; ===== Status Bar TLS/MCP ===== TLS: ÓÐ=TLS: Yes TLS: ÎÞ=TLS: No +TLS: ÄÚÖÃ=TLS: Built-in MCP: ¿ª=MCP: On MCP: ¹Ø=MCP: Off ; ===== MCP Settings (file write / HTTPS) ===== diff --git a/server/2015Remote/lang/zh_TW.ini b/server/2015Remote/lang/zh_TW.ini index 2301657..016e1f7 100644 --- a/server/2015Remote/lang/zh_TW.ini +++ b/server/2015Remote/lang/zh_TW.ini @@ -2131,6 +2131,10 @@ TLS֤ ÆôÓüÓÃÜ=†¢ÓüÓÃÜ ֤Êé·¾¶=‘{×C·�½ ˽Կ·¾¶=˽耷�½ +ÄÚÖÃ֤Êé=ƒȽ¨‘{×C +×Ôǩ֤Ê飬ÓÐЧÆÚÖÁ=×Ժž‘{×C£¬ÓÐЧÆÚÏÞÖÁ +Ê״ÎÆôÓÃʱ×Զ¯Éú³É=Ê״Ά¢ÓÕr×ԄӮaÉú +ÄÚÖÃ֤ÊéÉú³É/¼ÓÔØʧ°ܣ¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Çë²鿴ÈÕ־£¬»òȡÏû¹´ѡ¡¸ÄÚÖÃ֤Ê项ºó¸ÄÓñ¾µØ֤ÊéÓë˽Կ¡£=ƒȽ¨‘{×C®aÉú»òÝdÈëʧ”¡£¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Ո²鿴ӛ䛣¬»òȡÏû¹´ßxƒȽ¨‘{×Cáá¸ÄÓñ¾™C‘{×CÅc˽耡£ ÇëÌîд֤Êé·¾¶=ՈÌ‘{×C·�½ ÇëÌîд˽Կ·¾¶=ՈÌ˽耷�½ TLS ÅäÖÃÒѱ£´档=TLS ÔO¶¨Òу¦´档 @@ -2180,6 +2184,7 @@ WSS ; ===== Status Bar TLS/MCP ===== TLS: ÓÐ=TLS: ÓÐ TLS: ÎÞ=TLS: Ÿo +TLS: ÄÚÖÃ=TLS: ƒȽ¨ MCP: ¿ª=MCP: é_ MCP: ¹Ø=MCP: êP ; ===== MCP Settings (file write / HTTPS) =====