TLS with selfsigned certificate

前端 未结 6 2025
再見小時候
再見小時候 2020-12-07 17:10

I\'m trying to establish a TLS connection with the use of a self signed server certificate.

I generated the certificate with this example code: http://golang.org/src

6条回答
  •  情话喂你
    2020-12-07 18:07

    I saw the same error when using mysql client in Go:

    Failed to connect to database:  x509: cannot validate certificate for 10.111.202.229 because it doesn't contain any IP SANs
    

    and setting InsecureSkipVerify to true (to skip verification of certificate) resolved it for me:

    https://godoc.org/crypto/tls#Config

    The following code worked for me:

    package main
    
    import (
     "fmt"
     "github.com/go-sql-driver/mysql"
     "github.com/jinzhu/gorm"
     "crypto/tls"
     "crypto/x509"
     "io/ioutil"
     "log"
    )
    
    func main() {
        rootCertPool := x509.NewCertPool()
        pem, err := ioutil.ReadFile("/usr/local/share/ca-certificates/ccp-root-ca.crt")
        if err != nil {
                log.Fatal(err)
        }
        if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
            log.Fatal("Failed to append root CA cert at /usr/local/share/ca-certificates/ccp-root-ca.crt.")
        }
        mysql.RegisterTLSConfig("custom", &tls.Config{
            RootCAs: rootCertPool,
            InsecureSkipVerify: true,
        })
    
        db, err := gorm.Open("mysql", "ccp-user:I6qnD6zNDmqdDLXYg3HqVAk2P@tcp(10.111.202.229:3306)/ccp?tls=custom")
        defer db.Close()
    }
    

提交回复
热议问题