doucaigai7176 2019-09-18 14:57
浏览 430
已采纳

如何创建证书链

I'm working on a proxy server for automating browser integration tests. I've gotten to a place where I can create a root CA cert and then my self signed cert.

However, where I'm failing is "joining" these together into a valid certificate chain that is then served. I feel like I'm missing something very trivial as the CA is created correctly and the self signed cert is being signed by the CA however the certificate chain never shows the CA when viewing the generated certificate within a browser.

I realize this is somewhat a cryptic question, but let me know how I can make this more clear.

Thanks everyone!

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
}

To reduce the size of this post I created a small gist here to show where I'm generating the CA and the self signed certificate: https://gist.github.com/jredl-va/d5df26877fc85095115731d98ea5ff33

Update 1 Added get cert to gist

  • 写回答

1条回答 默认 最新

  • du548397507 2019-09-18 15:39
    关注

    Go doesn't magically include the CA certificate in the TLS handshake. If you expected RootCAs to cause that, you are mistaken. It is irrelevant for servers:

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

    You can either change GenerateCert to return the whole chain:

    --- 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
     }
    

    ... or make getCert append the CA cert in a similar fashion:

    --- 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
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度