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/tls: VerifyConnection is called twice by tls 1.3 servers if connection is resumed #39012

Open
rolandshoemaker opened this issue May 12, 2020 · 4 comments
Assignees
Milestone

Comments

@rolandshoemaker
Copy link
Contributor

@rolandshoemaker rolandshoemaker commented May 12, 2020

When using session resumption on a TLS 1.3 server with VerifyConnection set it will be called twice when a session is resumed. This looks like it's happening because it gets called once in checkForResumption, which calls processCertsFromClient, and then again when readClientCertificate calls VerifyConnection if ClientAuth is set to ignore client certs (also because PSK resumption). This appears to work as expected in 1.2.

It seems like perhaps VerifyConnection should be decoupled from processCertsFromClient and handled somewhere else in handshake? (I don't have any concrete suggestion of where would be better though.)

Minimal(ish) repro:

package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"fmt"
	"math/big"
	"net"
	"net/http"
)

func main() {
	tlsVer := uint16(tls.VersionTLS13)
	clientConf := &tls.Config{
		ClientSessionCache: tls.NewLRUClientSessionCache(5),
		MinVersion:         tlsVer,
		MaxVersion:         tlsVer,
		InsecureSkipVerify: true,
	}
	tr := &http.Transport{
		TLSClientConfig: clientConf,
	}
	hc := &http.Client{Transport: tr}

	k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	if err != nil {
		panic(err)
	}
	tmpl := &x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject:      pkix.Name{CommonName: "localhost"},
		DNSNames:     []string{"localhost"},
	}
	c, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, k.Public(), k)
	if err != nil {
		panic(err)
	}

	serverConf := &tls.Config{
		MinVersion: tlsVer,
		MaxVersion: tlsVer,
		VerifyConnection: func(_ tls.ConnectionState) error {
			fmt.Println("server verify connection")
			return nil
		},
		Certificates: []tls.Certificate{
			{
				Certificate: [][]byte{c},
				PrivateKey:  k,
			},
		},
	}

	l, err := net.Listen("tcp", "localhost:8181")
	if err != nil {
		panic(err)
	}
	tl := tls.NewListener(l, serverConf)
	s := http.Server{Handler: http.NotFoundHandler()}

	go func() {
		s.Serve(tl)
	}()

	fmt.Println("first")
	hc.Get("https://localhost:8181")

	fmt.Println()
	fmt.Println("second")
	resp, err := hc.Get("https://localhost:8181")
	if err != nil {
		panic(err)
	}
	fmt.Println("resumed?", resp.TLS.DidResume)
}

cc @FiloSottile @katiehockman

@katiehockman
Copy link
Member

@katiehockman katiehockman commented May 12, 2020

Thanks for filing this @rolandshoemaker. Will take a look this week.

@katiehockman katiehockman self-assigned this May 12, 2020
@katiehockman katiehockman added this to the Go1.15 milestone May 12, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented May 13, 2020

Change https://golang.org/cl/233957 mentions this issue: crypto/tls: fix duplicate calls to VerifyConnection

@networkimprov
Copy link

@networkimprov networkimprov commented May 13, 2020

Backport candidate?

@katiehockman
Copy link
Member

@katiehockman katiehockman commented May 14, 2020

VerifyConnection was a new feature for 1.15, so this will just be merged to master and included in that release. No backport needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.