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

Closed
rolandshoemaker opened this issue May 12, 2020 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rolandshoemaker
Copy link
Member

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
Contributor

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
@katiehockman katiehockman added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 12, 2020
@gopherbot
Copy link

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

@networkimprov
Copy link

Backport candidate?

@katiehockman
Copy link
Contributor

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

@golang golang locked and limited conversation to collaborators Jun 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants