如何创建证书链

时间:2019-09-18 14:57:42

标签: 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 将获取证书添加到要旨

1 个答案:

答案 0 :(得分:3)

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

  

RootCA定义了客户端在验证服务器证书时使用的一组根证书颁发机构。

您可以更改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
 }
相关问题