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;
|
||||
|
||||
Reference in new issue
Block a user