2016-12-01 128 views
4

我正在嘗試配置TLS服務器,以便在連接時返回證書你如何在Go中建立tls.Certficate鏈?

我想創建一個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 

假設我的鏈是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() 

我失去了一些東西明顯?訂單是否重要?

+0

訂單確實重要;試試:'[] tls.Certificates {server,inter,root}'。 –

回答

3

您server.crt這文件可以包含整個鏈條[加你不想你的服務器有間或根鍵],在server.crt這可以有

-----BEGIN CERTIFICATE----- 
[server cert] 
-----END CERT----- 
----BEGIN CERTIFICATE----- 
[inter cert] 
-----END CERT----- 

的根證書不應該不在服務器提供的鏈中,只有服務器+中間[s]。

+0

謝謝,這也是我最終理解的。 – phtrivier