From cc9ce35c4d37193dd5c2cebee5e1d3a93ef4de65 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Fri, 6 Nov 2026 12:13:07 +0100 Subject: [PATCH] Feature: export the built-in TLS certificate from the settings dialog The built-in certificate lives only inside the server config as base64 DER, so there was no way to take it elsewhere -- to install it as a custom certificate on another machine, or to hand it to a proxy. The TLS certificate dialog now has an Export Certificate button next to the Built-in Certificate checkbox. It is enabled only while that box is checked. Clicking it opens a save dialog and writes two PEM files: the chosen .crt and a .key with the same base name in the same directory. The key is PKCS#8 and unencrypted; the success message says so. If the certificate has not been generated yet (box checked but the dialog not applied), the export generates it first, the same way OK does, then refreshes the expiry text. If the key cannot be written, the certificate written just before it is deleted, so no half pair is left. Translations added for en_US, zh_TW and de_DE; en_US and zh_TW are also embedded resources, so the server was rebuilt. Verified: Debug x64 server builds clean. Drove the dialog on a running Debug instance over window messages -- the button, its label and its enabled state track the checkbox. The PEM writer was checked against OpenSSL (x509 -subject, pkey -check) using the same CryptoAPI flags. The save dialog itself was not automated end to end. Co-Authored-By: deepseek-flash --- server/2015Remote/BuiltinTlsCert.cpp | 40 ++++++++++++++ server/2015Remote/BuiltinTlsCert.h | 4 ++ server/2015Remote/TlsCertificateDlg.cpp | 73 ++++++++++++++++++++++++- server/2015Remote/TlsCertificateDlg.h | 3 + server/2015Remote/lang/de_DE.ini | 5 ++ server/2015Remote/lang/en_US.ini | 5 ++ server/2015Remote/lang/zh_TW.ini | 5 ++ 7 files changed, 133 insertions(+), 2 deletions(-) diff --git a/server/2015Remote/BuiltinTlsCert.cpp b/server/2015Remote/BuiltinTlsCert.cpp index ffbaec3..6e2b115 100644 --- a/server/2015Remote/BuiltinTlsCert.cpp +++ b/server/2015Remote/BuiltinTlsCert.cpp @@ -57,6 +57,35 @@ bool DecodeB64(const std::string& b64, std::vector& der) return !der.empty(); } +// DER → PEM(64 列æ�¢è¡Œ + 自定义标签)。CRYPT_STRING_BASE64HEADER å�ªä¼šå†™æ­» +// "CERTIFICATE" 标签,ç§�钥得自己包 "PRIVATE KEY"(与 PKCS#8 对应)。 +std::string DerToPem(const std::vector& der, const char* label) +{ + if (der.empty() || label == NULL) + return std::string(); + + DWORD chars = 0; + // CRYPT_STRING_BASE64:无头尾,按 64 列æ�’å…¥ CRLF(未带 NOCRLF 时) + if (!CryptBinaryToStringA(der.data(), (DWORD)der.size(), CRYPT_STRING_BASE64, NULL, &chars) || chars == 0) + return std::string(); + + std::vector buf(chars, '\0'); + if (!CryptBinaryToStringA(der.data(), (DWORD)der.size(), CRYPT_STRING_BASE64, buf.data(), &chars)) + return std::string(); + + size_t n = chars; + while (n > 0 && buf[n - 1] == '\0') + --n; + std::string body(buf.data(), n); + if (body.empty()) + return std::string(); + if (body.back() != '\n') + body += "\r\n"; + + return std::string("-----BEGIN ") + label + "-----\r\n" + body + + "-----END " + label + "-----\r\n"; +} + // 按天数平移 SYSTEMTIME(ä¸�能用 wYear += 10:é�‡ä¸Š 2 月 29 会得到é�žæ³•日期)。 void ShiftDays(const SYSTEMTIME& in, LONGLONG days, SYSTEMTIME& out) { @@ -258,6 +287,17 @@ std::string BuiltinTlsCert::CertPem(config& cfg) return std::string(buf.data(), n); } +bool BuiltinTlsCert::ExportPem(config& cfg, std::string& certPem, std::string& keyPem) +{ + std::vector certDer, keyDer; + if (!ReadDer(cfg, certDer, keyDer)) + return false; + + certPem = DerToPem(certDer, "CERTIFICATE"); + keyPem = DerToPem(keyDer, "PRIVATE KEY"); + return !certPem.empty() && !keyPem.empty(); +} + std::string BuiltinTlsCert::Describe(config& cfg) { std::vector certDer, keyDer; diff --git a/server/2015Remote/BuiltinTlsCert.h b/server/2015Remote/BuiltinTlsCert.h index 1ebaa26..da50d10 100644 --- a/server/2015Remote/BuiltinTlsCert.h +++ b/server/2015Remote/BuiltinTlsCert.h @@ -39,6 +39,10 @@ bool ReadDer(config& cfg, std::vector& certDer, std::vector& keyDer) // 内置è¯�书的 PEM 文本(下å�‘客户端信任锚用)。未生æˆ�时返回空串。 std::string CertPem(config& cfg); +// 导出用的两份 PEM:è¯�书(BEGIN CERTIFICATE)与ç§�钥(PKCS#8,BEGIN PRIVATE KEY)。 +// 任一缺失或无法编ç �时返回 false。ç§�钥以明文导出,调用方负责æ��示用户ä¿�管。 +bool ExportPem(config& cfg, std::string& certPem, std::string& keyPem); + // 对è¯�框展示用:è¯�书到期日 "2036-11-01"。未生æˆ�/无法解æž�时返回空串。 std::string Describe(config& cfg); } diff --git a/server/2015Remote/TlsCertificateDlg.cpp b/server/2015Remote/TlsCertificateDlg.cpp index b9ee99d..ef3baba 100644 --- a/server/2015Remote/TlsCertificateDlg.cpp +++ b/server/2015Remote/TlsCertificateDlg.cpp @@ -49,12 +49,24 @@ void BuildDialogTemplate(std::vector& out, LPCWSTR caption, short cx, shor while (out.size() % 4) out.push_back(0); // DWORD align } +// 以二进制写入。路径æ�¥è‡ªæ–‡ä»¶å¯¹è¯�框(TCHAR),与项目其余文件æ“�作一样走 ANSI 路径。 +bool WriteTextFile(const CString& path, const std::string& text) +{ + FILE* fp = NULL; + if (_tfopen_s(&fp, path, _T("wb")) != 0 || fp == NULL) + return false; + const size_t n = fwrite(text.data(), 1, text.size(), fp); + fclose(fp); + return n == text.size(); +} + } // namespace 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) + ON_BN_CLICKED(IDC_TLS_BUILTIN_EXPORT, &CTlsCertificateDlg::OnExportBuiltin) END_MESSAGE_MAP() CTlsCertificateDlg::CTlsCertificateDlg(CWnd* parent) @@ -87,6 +99,9 @@ BOOL CTlsCertificateDlg::OnInitDialog() m_btnBuiltin.Create(_TR("内置è¯�书"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, r0, this, IDC_TLS_BUILTIN); + m_btnExport.Create(_TR("导出è¯�书"), + WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, + r0, this, IDC_TLS_BUILTIN_EXPORT); 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); @@ -109,6 +124,7 @@ BOOL CTlsCertificateDlg::OnInitDialog() if (hFont) { m_btnEnable.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0)); m_btnBuiltin.SendMessage(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0)); + m_btnExport.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)); @@ -152,6 +168,8 @@ void CTlsCertificateDlg::UpdateBuiltinUi() m_lblKey.EnableWindow(!builtin); m_editKey.EnableWindow(!builtin); m_btnKeyBrowse.EnableWindow(!builtin); + // 导出的是内置è¯�书,没选用它时没有å�¯å¯¼å‡ºçš„对象 + m_btnExport.EnableWindow(builtin); CString info; if (builtin) { @@ -163,6 +181,54 @@ void CTlsCertificateDlg::UpdateBuiltinUi() m_lblBuiltinInfo.SetWindowText(info); } +// 把内置è¯�书导出为 <å��å­—>.crt + <å��å­—>.key(与è¯�书å�Œç›®å½•ã€�å�Œä¸»å��)。 +// 勾了「内置è¯�书ã€�但还没点确定的场å�ˆè¯�书å�¯èƒ½å°šæœªç”Ÿæˆ�,这里按需生æˆ�一次, +// 与 OnOK èµ°å�Œä¸€ä¸ª Ensure å…¥å�£ã€‚ +void CTlsCertificateDlg::OnExportBuiltin() +{ + std::string certPem, keyPem; + if (!BuiltinTlsCert::ExportPem(THIS_CFG, certPem, keyPem)) { + if (!BuiltinTlsCert::Ensure(THIS_CFG) || + !BuiltinTlsCert::ExportPem(THIS_CFG, certPem, keyPem)) { + MessageBoxL("内置è¯�书尚未生æˆ�或已æ�Ÿå��,无法导出。", "æ��示", MB_ICONWARNING); + return; + } + UpdateBuiltinUi(); // 刚生æˆ�:说明文本从「首次å�¯ç”¨æ—¶è‡ªåŠ¨ç”Ÿæˆ�ã€�æ�¢æˆ�有效期 + } + + CFileDialog dlg(FALSE, _T("crt"), _T("yama-server.crt"), + OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, + _T("Certificate Files (*.crt)|*.crt|All Files (*.*)|*.*||"), this); + dlg.m_ofn.lpstrTitle = _T("Export Built-in Certificate"); + if (dlg.DoModal() != IDOK) + return; + + CString certPath = dlg.GetPathName(); + CString keyPath = certPath; + int dot = keyPath.ReverseFind(_T('.')); + int slash = keyPath.ReverseFind(_T('\\')); + if (dot > slash) + keyPath = keyPath.Left(dot); + keyPath += _T(".key"); + + if (!WriteTextFile(certPath, certPem)) { + MessageBoxL("导出失败:无法写入文件,请检查目录是å�¦å�¯å†™ã€‚", "æ��示", MB_ICONERROR); + return; + } + if (!WriteTextFile(keyPath, keyPem)) { + // å�ªè�½ä¸€å�Šä¼šè¢«è¯¯å½“æˆ�一对完整的è¯�书/ç§�钥,删掉刚写出的è¯�书 + DeleteFile(certPath); + MessageBoxL("导出失败:无法写入文件,请检查目录是å�¦å�¯å†™ã€‚", "æ��示", MB_ICONERROR); + return; + } + + CString msg = _TR("è¯�书与ç§�钥已导出:"); + msg += _T("\r\n") + certPath; + msg += _T("\r\n") + keyPath; + msg += _T("\r\n\r\n") + _TR("ç§�钥为明文,请妥善ä¿�管。"); + MessageBox(msg, _TR("æ��示"), MB_ICONINFORMATION); +} + void CTlsCertificateDlg::OnOK() { CString sCert, sKey; @@ -253,10 +319,13 @@ void CTlsCertificateDlg::LayoutControls(int cx, int cy) m_btnEnable.MoveWindow(margin, y, cx - margin * 2, 22); y += 30; - // 「内置è¯�书ã€�与它å�³ä¾§çš„说明å�ŒæŽ’ + // 「内置è¯�书ã€�+「导出è¯�书ã€�å�Œä¸€æŽ’,说明文本å� å‰©ä½™å®½åº¦ const int builtinW = 92; + const int exportW = 96; m_btnBuiltin.MoveWindow(margin, y, builtinW, 22); - m_lblBuiltinInfo.MoveWindow(margin + builtinW + 4, y + 4, cx - margin * 2 - builtinW - 4, rowH); + m_btnExport.MoveWindow(cx - margin - exportW, y - 2, exportW, rowH); + m_lblBuiltinInfo.MoveWindow(margin + builtinW + 4, y + 4, + cx - margin * 2 - builtinW - exportW - 12, rowH); y += rowH + gap; int editW = cx - margin * 2 - labelW - browseW - gap; diff --git a/server/2015Remote/TlsCertificateDlg.h b/server/2015Remote/TlsCertificateDlg.h index 3e79170..ca3317c 100644 --- a/server/2015Remote/TlsCertificateDlg.h +++ b/server/2015Remote/TlsCertificateDlg.h @@ -29,10 +29,12 @@ private: IDC_TLS_KEY_BROWSE = 2005, // ç§�钥路径「æµ�览...ã€�按钮 IDC_TLS_BUILTIN = 2006, // 「内置è¯�书ã€�å¤�选框(缺çœ�勾选) IDC_TLS_BUILTIN_INFO= 2007, // 内置è¯�书说明(å�ªè¯»é�™æ€�文本) + IDC_TLS_BUILTIN_EXPORT = 2008, // 「导出è¯�书ã€�按钮(勾选内置è¯�书时å�¯ç”¨ï¼‰ }; CButton m_btnEnable; CButton m_btnBuiltin; + CButton m_btnExport; CStatic m_lblBuiltinInfo; CStatic m_lblCert, m_lblKey; CEdit m_editCert, m_editKey; @@ -45,5 +47,6 @@ private: void OnBrowseCert(); void OnBrowseKey(); void OnBuiltinToggle(); // 勾选状æ€�å�˜åŒ–:置ç�°/æ�¢å¤�自定义è¯�书控件 + void OnExportBuiltin(); // 把内置è¯�书导出为 .crt + .key void UpdateBuiltinUi(); // å�Œæ­¥å†…ç½®è¯�书说明文本 }; diff --git a/server/2015Remote/lang/de_DE.ini b/server/2015Remote/lang/de_DE.ini index 04d244b..cc1ffec 100644 --- a/server/2015Remote/lang/de_DE.ini +++ b/server/2015Remote/lang/de_DE.ini @@ -2146,6 +2146,11 @@ TLSÖ¤ ÄÚÖÃÖ¤Êé=Integriertes Zertifikat ×Ôǩ֤Ê飬ÓÐЧÆÚÖÁ=Selbstsigniertes Zertifikat, gueltig bis Ê×´ÎÆôÓÃʱ×Ô¶¯Éú³É=Wird bei der ersten Verwendung automatisch erzeugt +µ¼³öÖ¤Êé=Zertifikat exportieren +ÄÚÖÃÖ¤ÊéÉÐδÉú³É»òÒÑË𻵣¬ÎÞ·¨µ¼³ö¡£=Das integrierte Zertifikat wurde noch nicht erzeugt oder ist beschaedigt und kann nicht exportiert werden. +µ¼³öʧ°Ü£ºÎÞ·¨Ð´ÈëÎļþ£¬Çë¼ì²éĿ¼ÊÇ·ñ¿Éд¡£=Export fehlgeschlagen: Die Datei konnte nicht geschrieben werden; bitte pruefen, ob das Verzeichnis schreibbar ist. +Ö¤ÊéÓë˽ԿÒѵ¼³ö£º=Zertifikat und privater Schluessel exportiert: +˽ԿΪÃ÷ÎÄ£¬ÇëÍ×ÉÆ±£¹Ü¡£=Der private Schluessel liegt im Klartext vor; bitte sicher aufbewahren. ÄÚÖÃÖ¤ÊéÉú³É/¼ÓÔØÊ§°Ü£¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Çë²é¿´ÈÕÖ¾£¬»òÈ¡Ïû¹´Ñ¡¡¸ÄÚÖÃÖ¤Ê项ºó¸ÄÓñ¾µØÖ¤ÊéÓë˽Կ¡£=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 diff --git a/server/2015Remote/lang/en_US.ini b/server/2015Remote/lang/en_US.ini index d744817..c036cc1 100644 --- a/server/2015Remote/lang/en_US.ini +++ b/server/2015Remote/lang/en_US.ini @@ -2161,6 +2161,11 @@ TLSÖ¤ ÄÚÖÃÖ¤Êé=Built-in Certificate ×Ôǩ֤Ê飬ÓÐЧÆÚÖÁ=Self-signed certificate, valid until Ê×´ÎÆôÓÃʱ×Ô¶¯Éú³É=Generated automatically on first use +µ¼³öÖ¤Êé=Export Certificate +ÄÚÖÃÖ¤ÊéÉÐδÉú³É»òÒÑË𻵣¬ÎÞ·¨µ¼³ö¡£=The built-in certificate has not been generated or is corrupt, so it cannot be exported. +µ¼³öʧ°Ü£ºÎÞ·¨Ð´ÈëÎļþ£¬Çë¼ì²éĿ¼ÊÇ·ñ¿Éд¡£=Export failed: the file could not be written; check that the directory is writable. +Ö¤ÊéÓë˽ԿÒѵ¼³ö£º=Certificate and private key exported: +˽ԿΪÃ÷ÎÄ£¬ÇëÍ×ÉÆ±£¹Ü¡£=The private key is stored in plain text; keep it safe. ÄÚÖÃÖ¤ÊéÉú³É/¼ÓÔØÊ§°Ü£¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Çë²é¿´ÈÕÖ¾£¬»òÈ¡Ïû¹´Ñ¡¡¸ÄÚÖÃÖ¤Ê项ºó¸ÄÓñ¾µØÖ¤ÊéÓë˽Կ¡£=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 diff --git a/server/2015Remote/lang/zh_TW.ini b/server/2015Remote/lang/zh_TW.ini index 9096f2f..fdddf46 100644 --- a/server/2015Remote/lang/zh_TW.ini +++ b/server/2015Remote/lang/zh_TW.ini @@ -2154,6 +2154,11 @@ TLSÖ¤ ÄÚÖÃÖ¤Êé=ƒÈ½¨‘{×C ×Ôǩ֤Ê飬ÓÐЧÆÚÖÁ=×Ôºž‘{×C£¬ÓÐЧÆÚÏÞÖÁ Ê×´ÎÆôÓÃʱ×Ô¶¯Éú³É=Ê״Ά¢ÓÕr×Ô„Ó®aÉú +µ¼³öÖ¤Êé=…R³ö‘{×C +ÄÚÖÃÖ¤ÊéÉÐδÉú³É»òÒÑË𻵣¬ÎÞ·¨µ¼³ö¡£=ƒÈ½¨‘{×CÉÐδ®aÉú»òÒÑ“p𧣬Ÿo·¨…R³ö¡£ +µ¼³öʧ°Ü£ºÎÞ·¨Ð´ÈëÎļþ£¬Çë¼ì²éĿ¼ÊÇ·ñ¿Éд¡£=…R³öʧ”¡£ºŸo·¨Œ‘Èë™n°¸£¬Õˆ™z²éÄ¿ä›ÊÇ·ñ¿ÉŒ‘¡£ +Ö¤ÊéÓë˽ԿÒѵ¼³ö£º=‘{×CÅc˽è€ÒÑ…R³ö£º +˽ԿΪÃ÷ÎÄ£¬ÇëÍ×ÉÆ±£¹Ü¡£=˽耞éÃ÷ÎÄ£¬ÕˆÍ×ÉÆ±£¹Ü¡£ ÄÚÖÃÖ¤ÊéÉú³É/¼ÓÔØÊ§°Ü£¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Çë²é¿´ÈÕÖ¾£¬»òÈ¡Ïû¹´Ñ¡¡¸ÄÚÖÃÖ¤Ê项ºó¸ÄÓñ¾µØÖ¤ÊéÓë˽Կ¡£=ƒÈ½¨‘{×C®aÉú»òÝdÈëʧ”¡£¬¼ÓÃܹ¦ÄÜδÉúЧ¡£Õˆ²é¿´Ó›ä›£¬»òÈ¡Ïû¹´ßxƒÈ½¨‘{×Cáá¸ÄÓñ¾™C‘{×CÅc˽耡£ ÇëÌîд֤Êé·¾¶=ÕˆÌ‘{×C·�½ ÇëÌîд˽Կ·¾¶=ÕˆÌ˽è€Â·�½