从 sgcWebSockets 4.4.7 起,支持使用 SChannel 作为加密库进行证书身份验证。
SChannel 实现支持 2 种证书身份验证类型:
1. 使用 PFX 证书
2. 设置已安装在 Windows 系统中证书的哈希指纹
PFX 证书
PFX 证书是包含证书和私钥的文件。如果您的证书为 PEM 格式,使用前必须将其转换为 PFX。
使用以下 openssl 命令将 PEM 证书转换为 PFX:
openssl pkcs12 -inkey certificate-pem.key -in certificate-pem.crt -export -out certificate.pfx
证书转换为 PFX 格式后,只需部署证书并在 TLSOptions.Certificate 属性中设置其路径即可。
TLSOptions.IOHandler := iohSChannel; TLSOptions.CertFile := '<certificate path>'; TLSOptions.Password := '<certificate optional password>';
哈希指纹
如果证书已安装在 Windows 证书存储中,只需知道证书指纹并在 TLSOptions.SChannel_Options 属性中设置即可。
在 PowerShell 中,只需对证书容器运行 dir 命令即可轻松找到证书的哈希值。
dir cert:\localmachine\my
哈希值即为十六进制的指纹值。
Directory: Microsoft.PowerShell.Security\Certificate::localmachine\myThumbprint Subject---------- -------C12A8FC8AE668F866B48F23E753C93D357E9BE10 CN=*.mydomain.com
获得指纹值后,需在 TLSOptions.SChannel_Options 属性中设置哈希值和证书位置。
TLSOptions.IOHandler := iohSChannel; TLSOptions.SChannel_Options.CertHash := '<certificate thumbprint>'; TLSOptions.SChannel_Options.CertStoreName := '<certificate store name>'; TLSOptions.SChannel_Options.CertStorePath := '<certificate store path>'; TLSOptions.Password := '<certificate optional password>';
