Files
SimpleRemoter/server/2015Remote/BuiltinTlsCert.h
T
yuanyuanxianganddeepseek-flash cc9ce35c4d 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
2026-11-06 12:13:07 +01:00

49 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <Windows.h>
#include <string>
#include <vector>
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<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);
}