6

I'm trying to skip certificate verification while trying to do a https request to a server. The client.Do() fails with the following error:

tls: failed to parse certificate from server: asn1: syntax error: PrintableString contains invalid character

Code Snippet:

    var jsonStr = []byte(`{"grant_type":"client_credentials"}`)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    fmt.Println("req:>", req)

    //req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.SetBasicAuth("opennms", "test123~")

    tr := &http.Transport{
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}

    //client := &http.Client{}
    resp, err := client.Do(req)
    fmt.Println("resp:>", resp)
    if err != nil {
            panic(err)
    }
    defer resp.Body.Close()

    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))

What am I missing here?

Himanshu
  • 12,071
  • 7
  • 46
  • 61
dino123
  • 89
  • 2
  • 5
  • 2
    Probably nothing. A pure server error. Skimming crypto/tls for options about how to not read ASN.1 at all might be helpful. – Volker Jun 06 '18 at 09:26
  • Most likely the problem is that the server certificate is not properly encoded. A common problem seems to be that some packages either use the wrong or a faulty encoding, e.g using _ as part PrintableString for CN which is illegal. See also issue https://github.com/golang/go/issues/36044 - As far as I know you can't and you shouldn't bypass this error on the client side. – Marcus Feb 02 '23 at 16:37

0 Answers0