登录
首页 >  Golang >  Go问答

多个云服务之间使用随机 IP 的 TLS 证书

来源:stackoverflow

时间:2024-03-04 13:00:25 295浏览 收藏

你在学习Golang相关的知识吗?本文《多个云服务之间使用随机 IP 的 TLS 证书》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

作为一个 tls 新手,我真的不知道我应该在这里做什么。

我在 go 中有一个使用 listenandservetls 的客户端应用程序,它向 go 中也使用 listenandservetls 的服务器应用程序发送请求。我可以从客户端向服务器发送请求并验证本地生成的证书。

当我更新客户端以与 azure 中的代理(c#)通信时,会打开一个到连接到本地计算机上的服务器的 azure 中继服务的 sslstream。我收到以下错误。 get https://:8080/hello: x509: 无法验证 的证书,因为它不包含任何 ip sans

我使用的 ip 地址是动态的,没有主机或 dns。如果 ip 随我发出的每个请求而变化,我如何通过服务器验证我的证书。

// server code
func hellohandler(w http.responsewriter, r *http.request) {
    // write "hello, world!" to the response body
    io.writestring(w, "hello, world!\n")
}

func main() {
    // create a ca certificate pool and add cert.pem to it
    cacert, err := ioutil.readfile("../certs/cert.pem")
    if err != nil {
        log.fatal(err)
    }
    cacertpool := x509.newcertpool()
    cacertpool.appendcertsfrompem(cacert)
    // create the tls config with the ca pool and enable client certificate validation
    tlsconfig := &tls.config{
        clientcas:  cacertpool,
        clientauth: tls.requireandverifyclientcert,
    }
    tlsconfig.buildnametocertificate()

    server := &http.server{
        addr:      ":8080",
        tlsconfig: tlsconfig,
    }
    // set up a /hello resource handler
    http.handlefunc("/hello", hellohandler)

    // listen to port 8080 and wait
    log.fatal(server.listenandservetls("../certs/cert.pem", "../certs/key.pem"))
}
// Client Code
func main() {
    cert, err := tls.LoadX509KeyPair("../certs/cert.pem", "../certs/key.pem")
    if err != nil {
        log.Fatal(err)
    }

    // Create a CA certificate pool and add cert.pem to it
    caCert, err := ioutil.ReadFile("../certs/cert.pem")
    if err != nil {
        log.Fatal(err)
    }
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                // InsecureSkipVerify: true,
                RootCAs:      caCertPool,
                Certificates: []tls.Certificate{cert},
            },
        },
    }

    // Request /hello over port 8080 via the GET method
    // r, err := http.Get("http://localhost:8080/hello")
    // Request /hello over HTTPS port 8443 via the GET method
    r, err := client.Get("https://:8080/hello")
    if err != nil {
        log.Fatal(err)
    }

    // Read the response body
    defer r.Body.Close()
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        log.Fatal(err)
    }

    // Print the response body to stdout
    fmt.Printf("%s\n", body)
}

解决方案


首先,澄清一个误解:服务器不验证证书。证书是身份的加密证明,由客户端使用,以便他们可以确保他们连接到了他们想要连接的服务器。 IE。他们希望确保网络连接没有被意外或恶意地定向到不同的服务器。因此,如果证书验证失败,几乎总是因为客户端不满意,而不是服务器。

当您向 https://192.0.2.1:8080/hello 发出 HTTP 请求时,默认情况下客户端会收到一个在主题备用名称 (SAN) 中包含 192.0.2.1 的证书,因为这将是请求的 Host 字段的值(如上所述,客户端希望确保它确实连接到该服务器)。

如果您事先不知道 IP 地址,则无法颁发此类证书,您唯一的选择就是改变客户的期望。这就是 ServerName field in tls.Config 的用途。只需将其设置为证书的通用名称(应该是一个域)。

今天关于《多个云服务之间使用随机 IP 的 TLS 证书》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>