Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto/x509: cannot parse certificate IP & net/http cannot ignore this certificate error #65829

Closed
staverne opened this issue Feb 20, 2024 · 1 comment

Comments

@staverne
Copy link

staverne commented Feb 20, 2024

Go version

go version go1.22.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN='/home/sylvain/projects/sandboxes/go/bin'
GOCACHE='/home/sylvain/.cache/go-build'
GOENV='/home/sylvain/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/sylvain/projects/sandboxes/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/sylvain/projects/sandboxes/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/sylvain/projects/sandboxes/go/src/project_desktop/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/sylvain/projects/sandboxes/go/src/project_desktop/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/sylvain/projects/sandboxes/go/src/project_desktop/test2/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build374461103=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://go.dev/play/


package main

import (
  "log"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AfaoJh/c9E/yIjLyYNU6thlvfMJqiFsX
pmCvuE/7kbadKko3ZfBs50fo+iJrDD6LXIL5oSnQeWLt6L56YHo1PZM/tdLmJ1VH
6wXlmX6DsONrfBcnwGZmZXEPuZyDqgMc9KOzUkToR5hl81GO9o9iHenD
-----END CERTIFICATE-----`

func main() {

  log.Println("Decode certificate")
  block, _ := pem.Decode([]byte(certPEM))
  if block == nil || block.Type != "CERTIFICATE" {
      log.Fatal("failed to decode PEM block containing the certificate")
  }
  cert, err := x509.ParseCertificate(block.Bytes)
  if err != nil {
      log.Fatalf("failed to parse certificate: %v", err)
  }
  caCertPool := x509.NewCertPool()
  caCertPool.AddCert(cert)

}

What did you see happen ?

I've a HTTP server with a bad PEM certificate.
If i do an HTTP request on this host, even if i set InsecureSkipVerify: true it will fail due to this certificate decoding error.

 req, err := http.NewRequest("https://localhost", "")
 if err != nil {
   log.Println("Err request 1", err);
   return ""
 tr := &http.Transport{
   TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
 }
 client := &http.Client{Transport: tr}

i will get the error:
Error: "failed to parse certificate: x509: cannot parse IP address of length 9"

What did you expect to see?

If I use InsecureSkipVerify i expect http library to skip the verification of the server's SSL certificate.
But it check the SSL certificate by parsing the PEM certificate, so it's failing.
I'm not expecting GOLANG to parse the PEM certificate with the InsecureSkipVerify option.

Note about PEM validity

If i open my certificate with gnome "gcr-viewer" the IP read works:

$ gcr-viewer cert.pem

Capture d’écran du 2024-02-20 17-09-45

but not with openSSL, it say IP address is invalid:

$ openssl x509 -text -noout -in cert.pem

    X509v3 extensions:
        X509v3 Subject Alternative Name: 
            DNS:localhost, DNS:Ludovic, DNS:ludovic, DNS:Ludovic, DNS:ludovic, IP Address:<invalid length=9>, IP Address:<invalid length=39>
@staverne staverne changed the title crypto/x509: cannot parse certificate with ipv6 IP 0000:0000:0000:0000:0000:0000:0000:0001 crypto/x509: cannot parse certificate IP & net/http cannot ignore this certificate error Feb 20, 2024
@seankhliao
Copy link
Member

I think this is working as expected (invalid cert returns an error).

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants