我希望连接一个服务器和一些客户端使用mTLS在golang .在我的服务器上,我希望能够生成证书以放在所有客户端上,以便客户端可以与服务器通信,但客户端不能相互通信.然而,我的客户端在端口80上公开Golang http服务器,并且我的服务器通过API请求与客户端通信,而不是反过来(从技术上讲,您可能会质疑我所称的服务器是否真的是客户端,但它是生成证书的单一来源,并且向客户端提供内容,因此为了简单起见,我将坚持使用这种命名).这怎么可能设置成这样呢?需要生成哪些证书?是否可以让客户端使用从单个服务器生成的证书监听请求?
我很可能会使用SmallStep来生成证书,SmallStep示例会很有用,但一般的方法也很好,然后可以单独研究如何使用SmallStep复制它.
我已经查看了一些现有的Golang示例,但它们往往在不同的设置下进行操作:
https://smallstep.com/hello-mtls/doc/server/go个
https://venilnoronha.io/a-step-by-step-guide-to-mtls-in-go个
以下是目前的代码,我非常确定它错误地使用了证书:
客户端:
step ca certificate "localhost" client.crt client.key
个
在下面,cert和key变量分别是client.crt和client.key.
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(cert))
// Read the key pair to create certificate
keyPair, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
log.Fatal(err)
}
transport = &http.Transport{
IdleConnTimeout: transportIdleConnTimeout,
MaxIdleConnsPerHost: transportMaxIdleConnsPerHost,
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{keyPair},
},
服务器:
step ca certificate "pm1" server.crt server.key
个
下面,cert和key变量分别为Server.crt和Server.key.
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(cert))
cert, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
log.Fatal("server: loadkeys: ", err)
}
tlsConfig := tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS13,
}
我现在根本没有使用ca.crt(step ca root ca.crt
).