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
This commit is contained in:
1 parent
6a51116873
commit
cc9ce35c4d
7 files changed
+133
-2
No files matched your search
@@ -57,6 +57,35 @@ bool DecodeB64(const std::string& b64, std::vector<BYTE>& der)
|
||||
return !der.empty();
|
||||
}
|
||||
|
||||
// DER → PEM(64 列换行 + 自定义标签)。CRYPT_STRING_BASE64HEADER 只会写死
|
||||
// "CERTIFICATE" 标签,私钥得自己包 "PRIVATE KEY"(与 PKCS#8 对应)。
|
||||
std::string DerToPem(const std::vector<BYTE>& 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<char> 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<BYTE> 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<BYTE> certDer, keyDer;
|
||||
|
||||
@@ -39,6 +39,10 @@ bool ReadDer(config& cfg, std::vector<BYTE>& certDer, std::vector<BYTE>& 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);
|
||||
}
|
||||
@@ -49,12 +49,24 @@ void BuildDialogTemplate(std::vector<BYTE>& 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;
|
||||
|
||||
@@ -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(); // 同步内置证书说明文本
|
||||
};
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2154,6 +2154,11 @@ TLS֤
|
||||
内置证书=內建憑證
|
||||
自签证书,有效期至=自簽憑證,有效期限至
|
||||
首次启用时自动生成=首次啟用時自動產生
|
||||
导出证书=匯出憑證
|
||||
内置证书尚未生成或已损坏,无法导出。=內建憑證尚未產生或已損毀,無法匯出。
|
||||
导出失败:无法写入文件,请检查目录是否可写。=匯出失敗:無法寫入檔案,請檢查目錄是否可寫。
|
||||
证书与私钥已导出:=憑證與私鑰已匯出:
|
||||
私钥为明文,请妥善保管。=私鑰為明文,請妥善保管。
|
||||
内置证书生成/加载失败,加密功能未生效。请查看日志,或取消勾选「内置证书」后改用本地证书与私钥。=內建憑證產生或載入失敗,加密功能未生效。請查看記錄,或取消勾選內建憑證後改用本機憑證與私鑰。
|
||||
请填写证书路径=請填寫憑證路徑
|
||||
请填写私钥路径=請填寫私鑰路徑
|
||||
|
||||
Reference in new issue
Block a user