TLS/SSL令牌失效日期不正确

yyhrrdl8  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(122)

我试图建立一个函数来获取证书信息。当使用带有"www"的顶级域时,每个人都工作得很好,但当排除它或查询三级域时,它提供了不正确的"不晚于"和"不早于"日期。
我想知道有没有人有补救或建议,谢谢!

package main

import (
    "crypto/tls"
    "fmt"
    "log"
)

func main() {

    conf := &tls.Config{
        InsecureSkipVerify: true,
    }

    conn, err := tls.Dial("tcp", "www.google.com:443", conf)
    if err != nil {
        log.Println("Error when Dialing", err)
        return
    }

    defer conn.Close()

    certs := conn.ConnectionState().PeerCertificates

    for _, cert := range certs {
        fmt.Printf("FQDNs: %s\n\n", cert.DNSNames)
        fmt.Printf("Issuer Name: %s\n\n", cert.Issuer)
        fmt.Printf("Issued: %v\n\n", cert.NotBefore.Format("Jan 2, 2006 3:04 PM"))
        fmt.Printf("Expires: %s\n\n", cert.NotAfter.Format("Jan 2, 2006 3:04 PM"))
        fmt.Printf("Issuer Common Name: %s\n\n", cert.Issuer.CommonName)
        fmt.Println("-----------------------------------------")
    }
}
yzuktlbb

yzuktlbb1#

func main() {

    address := "google.com"
    conn, err := tls.Dial("tcp", address+":443", nil)
    if err != nil {
        panic("Server doesn't support SSL certificate err: " + err.Error())
    }

    err = conn.VerifyHostname(address)
    if err != nil {
        panic("Hostname doesn't match with certificate: " + err.Error())
    }
    certs := conn.ConnectionState().PeerCertificates

    for _, cert := range certs {
        fmt.Printf("FQDNs: %s\n\n", cert.DNSNames)
        fmt.Printf("Issuer Name: %s\n\n", cert.Issuer)
        fmt.Printf("Issued: %v\n\n", cert.NotBefore.Format("Jan 2, 2006 3:04 PM"))
        fmt.Printf("Expires: %s\n\n", cert.NotAfter.Format("Jan 2, 2006 3:04 PM"))
        fmt.Printf("Issuer Common Name: %s\n\n", cert.Issuer.CommonName)
        fmt.Println("-----------------------------------------")
    }
    
}

相关问题