go - 如何创建证书链

标签 go ssl http-proxy

我正在开发一个代理服务器来自动执行浏览器集成测试。我已经到达可以创建根 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/

相关文章:

android - 在 Android 上使用 Volley 获取 SSLHandshakeException

javascript - npm 安装错误/问题

ruby - Ruby 中的 HTTPS URL 服务器证书

重命名后的golang相关包导入

json - 如何创建一个无限嵌套的 json 并在 go lang 数据结构中访问它?

docker - Docker没有看到main.go

c++ - 断开连接后正确杀死 asio steady_timer

java - 通过代理网络(如果可用)重定向所有 Android 应用程序网络(使用库)

git - 无法克隆 Git 存储库 : Invalid file descriptor

go - 无法在travis中构建go项目