客户端tls身份验证问题

时间:2018-02-18 19:09:39

标签: go

我在GoLang中通过证书遇到客户端身份验证问题。从跟踪中我看到服务器正在请求证书,但证书未在客户端响应中发送。

enter image description here

这是我的Go代码:

package main

import (
    "bytes"
    "crypto/tls"
    "crypto/x509"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {

    xml := `
    <Packet>
<Header>
 <ID>1231232132132354234</ID>
 </Header>
 <Body>
 <Item>
 <Content>SMS</Content>
 <Phone_no>+123123213</Phone_no>
 <Content>test</Content>
 <OriginAddress>Posiljatelj</OriginAddress>
 </Item>
 </Body>
</Packet>
    `

    // Load client cert
    cert, err := tls.LoadX509KeyPair("../cert/certNEW.pem", "../cert/serverNEW.key")
    if err != nil {
        log.Fatal(err)
    }

    // Load CA cert
    caCert, err := ioutil.ReadFile("../cert/cacerts.cer")
    if err != nil {
        log.Fatal(err)
    }
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    // Setup HTTPS client
    tlsConfig := &tls.Config{
        Certificates:       []tls.Certificate{cert},
        RootCAs:            caCertPool,
        InsecureSkipVerify: true,
    }

    tlsConfig.BuildNameToCertificate()
    transport := &http.Transport{TLSClientConfig: tlsConfig}
    client := &http.Client{Transport: transport}

    resp, err := client.Post("https://api.url", "text/xml", bytes.NewBuffer([]byte(xml)))

    if err != nil {
        fmt.Println(err)
    }
    contents, err := ioutil.ReadAll(resp.Body)
    fmt.Printf("%s\n", string(contents))
}

我从客户那里获得了pfx证书,我所做的是我取消了公共客户证书,私人证书和rootRA公共部分,我参与了Go代码。

1 个答案:

答案 0 :(得分:0)

您需要通过设置InsecureSkipVerify

来停用InsecureSkipVerify: false,

它导致链参数为nil https://golang.org/pkg/crypto/tls/

  // If normal verification is disabled by
    // setting InsecureSkipVerify, or (for a server) when ClientAuth is
    // RequestClientCert or RequireAnyClientCert, then this callback will
    // be considered but the verifiedChains argument will always be nil.