gpt4 book ai didi

go - 如何创建证书链

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:02 27 4
gpt4 key购买 nike

我正在研究用于自动化浏览器集成测试的代理服务器。我已经到了可以创建根 CA 证书然后创建自签名证书的地方。

但是,我失败的地方是将它们“加入”到一个有效的证书链中,然后提供该证书链。我觉得我遗漏了一些非常微不足道的东西,因为 CA 已正确创建并且自签名证书正在由 CA 签名,但是在浏览器中查看生成的证书时,证书链从不显示 CA。

我意识到这是一个有点神秘的问题,但让我知道如何才能更清楚地说明这一点。

谢谢大家!

func Server(cn net.Conn, p ServerParam) *ServerConn {
conf := new(tls.Config)
if p.TLSConfig != nil {
*conf = *p.TLSConfig
}
sc := new(ServerConn)
// this is the CA certificate that is performing the signing and I had though would show as the "root" certificate in the chain
conf.RootCAs = buildBoolFromCA(p.CA)
conf.GetCertificate = func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
sc.ServerName = hello.ServerName
// the self signed cert that is generated (the root CA however is not part of the chain
return getCert(p.CA, hello.ServerName)
}
sc.Conn = tls.Server(cn, conf)
return sc
}

为了减少这篇文章的大小,我在这里创建了一个小要点来展示我在哪里生成 CA 和自签名证书:https://gist.github.com/jredl-va/d5df26877fc85095115731d98ea5ff33

更新 1添加获取证书到要点

最佳答案

Go 不会神奇地将 CA 证书包含在 TLS 握手中。如果您认为 RootCA 会导致这种情况,那您就错了。它与服务器无关:

RootCAs defines the set of root certificate authorities that clients use when verifying server certificates.

您可以更改 GenerateCert 以返回整个链:

--- cert.go.orig        2019-09-18 17:35:29.408807334 +0200
+++ cert.go 2019-09-18 17:35:45.028779955 +0200
@@ -46,11 +46,11 @@
x, err := x509.CreateCertificate(rand.Reader, template, ca.Leaf, key.Public(), ca.PrivateKey)
if err != nil {
return nil, err
}
cert := new(tls.Certificate)
- cert.Certificate = append(cert.Certificate, x)
+ cert.Certificate = append(cert.Certificate, x, ca.Leaf.Raw)
cert.PrivateKey = key
cert.Leaf, _ = x509.ParseCertificate(x)
return cert, nil
}

...或者让 getCert 以类似的方式附加 CA 证书:

--- cert.go.orig        2019-09-18 18:07:45.924405370 +0200
+++ cert.go 2019-09-18 18:08:11.998359456 +0200
@@ -61,7 +61,8 @@
func getCert(ca *tls.Certificate, host string) (*tls.Certificate, error) {
cert, err := GenerateCert(ca, host)
if err != nil {
return nil, err
}
+ cert.Certificate = append(cert.Certificate, ca.Leaf.Raw)
return cert, nil
}

关于go - 如何创建证书链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57995716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com