您如何在Go中建立tls.Certficate链?

pht*_*ier 5 ssl go

我正在尝试配置TLS服务器以在连接时返回证书

我想创建一个带有证书tls.Config

    // Certificates contains one or more certificate chains
    // to present to the other side of the connection.
    // Server configurations must include at least one certificate
    // or else set GetCertificate.
    Certificates []Certificate
Run Code Online (Sandbox Code Playgroud)

假设我的链为root -> inter -> server,我可以独立加载每个证书,并使用一个列表,但是只有serverCert发送到SSL客户端。

我正在按照以下方式进行操作:

root, err := tls.LoadX509KeyPair("root.crt", "root.key")
inter, err := tls.LoadX509KeyPair("inter.crt", "inter.key")
server, err := tls.LoadX509KeyPair("server.crt", "server.key")

config := tls.Config{
   Certificates : []tls.Certificates{root, inter, server}
}
config.BuildNameFromCertificates()
Run Code Online (Sandbox Code Playgroud)

我是否缺少明显的东西?顺序重要吗?

sup*_*ell 5

你的 server.crt 文件可以包含整个链[加上你不希望你的服务器有 inter 或 root 密钥],在 server.crt 中你可以有

-----BEGIN CERTIFICATE-----
[server cert]
-----END CERT-----
 ----BEGIN CERTIFICATE-----
[inter cert]
-----END CERT-----
Run Code Online (Sandbox Code Playgroud)

根证书不应该位于服务器提供的链中,而应该位于服务器+中间[s]中。