Skip to content

Commit

Permalink
Add InsecureSkipTimeVerify (#174)
Browse files Browse the repository at this point in the history
* add `InsecureSkipTimeVerify`

* fix the cache verification when `InsecureServerNameToVerify` set

* better description of `InsecureSkipTimeVerify`

Co-authored-by: Gaukas Wang <i@gauk.as>

* minimize the change made + wrap the modified section

* fix: use tab replace space indentation

---------

Co-authored-by: Gaukas Wang <i@gauk.as>
  • Loading branch information
molon and gaukas committed Mar 12, 2023
1 parent dae72ad commit 17e2929
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 8 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,13 @@ type Config struct {
// testing or in combination with VerifyConnection or VerifyPeerCertificate.
InsecureSkipVerify bool

// InsecureSkipTimeVerify controls whether a client verifies the server's
// certificate chain against time. If InsecureSkipTimeVerify is true,
// crypto/tls accepts the certificate even when it is expired.
//
// This field is ignored when InsecureSkipVerify is true.
InsecureSkipTimeVerify bool // [uTLS]

// InsecureServerNameToVerify is used to verify the hostname on the returned
// certificates. It is intended to use with spoofed ServerName.
// If InsecureServerNameToVerify is "*", crypto/tls will do normal
Expand Down Expand Up @@ -821,6 +828,7 @@ func (c *Config) Clone() *Config {
ClientAuth: c.ClientAuth,
ClientCAs: c.ClientCAs,
InsecureSkipVerify: c.InsecureSkipVerify,
InsecureSkipTimeVerify: c.InsecureSkipTimeVerify,
InsecureServerNameToVerify: c.InsecureServerNameToVerify,
CipherSuites: c.CipherSuites,
PreferServerCipherSuites: c.PreferServerCipherSuites,
Expand Down
28 changes: 22 additions & 6 deletions handshake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,26 @@ func (c *Conn) loadSession(hello *clientHelloMsg) (cacheKey string,
return cacheKey, nil, nil, nil, nil
}
serverCert := session.serverCertificates[0]
if c.config.time().After(serverCert.NotAfter) {
// Expired certificate, delete the entry.
c.config.ClientSessionCache.Put(cacheKey, nil)
return cacheKey, nil, nil, nil, nil
// [UTLS SECTION START]
if !c.config.InsecureSkipTimeVerify {
if c.config.time().After(serverCert.NotAfter) {
// Expired certificate, delete the entry.
c.config.ClientSessionCache.Put(cacheKey, nil)
return cacheKey, nil, nil, nil, nil
}
}
if err := serverCert.VerifyHostname(c.config.ServerName); err != nil {
return cacheKey, nil, nil, nil, nil
var dnsName string
if len(c.config.InsecureServerNameToVerify) == 0 {
dnsName = c.config.ServerName
} else if c.config.InsecureServerNameToVerify != "*" {
dnsName = c.config.InsecureServerNameToVerify
}
if len(dnsName) > 0 {
if err := serverCert.VerifyHostname(dnsName); err != nil {
return cacheKey, nil, nil, nil, nil
}
}
// [UTLS SECTION END]
}

if session.vers != VersionTLS13 {
Expand Down Expand Up @@ -895,6 +907,10 @@ func (c *Conn) verifyServerCertificate(certificates [][]byte) error {
Intermediates: x509.NewCertPool(),
}

if c.config.InsecureSkipTimeVerify {
opts.CurrentTime = certs[0].NotAfter
}

if len(c.config.InsecureServerNameToVerify) == 0 {
opts.DNSName = c.config.ServerName
} else if c.config.InsecureServerNameToVerify != "*" {
Expand Down
2 changes: 1 addition & 1 deletion tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ func TestCloneNonFuncFields(t *testing.T) {
f.Set(reflect.ValueOf("b"))
case "ClientAuth":
f.Set(reflect.ValueOf(VerifyClientCertIfGiven))
case "InsecureSkipVerify", "SessionTicketsDisabled", "DynamicRecordSizingDisabled", "PreferServerCipherSuites":
case "InsecureSkipVerify", "InsecureSkipTimeVerify", "SessionTicketsDisabled", "DynamicRecordSizingDisabled", "PreferServerCipherSuites":
f.Set(reflect.ValueOf(true))
case "InsecureServerNameToVerify":
f.Set(reflect.ValueOf("c"))
Expand Down

0 comments on commit 17e2929

Please sign in to comment.