Skip to content

Commit 9376439

Browse files
committed
crypto/tls: expose local Certificates used in handshake via ConnectionState
If the local party didn't send a certificate in the handshake, leave the field nil. This information is predominantly useful when debugging. Fixes #24673
1 parent 3f451f2 commit 9376439

File tree

8 files changed

+16
-0
lines changed

8 files changed

+16
-0
lines changed

api/next/24673.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg crypto/tls, type ConnectionState struct, LocalCertificate *Certificate #24673
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make the local party's [Certificate] available via
2+
[ConnectionState.LocalCertificate] if provided during the handshake.

src/crypto/tls/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ type ConnectionState struct {
275275
// PeerCertificates and its contents should not be modified.
276276
PeerCertificates []*x509.Certificate
277277

278+
// LocalCertificate is the local certificate sent by this side of the
279+
// handshake. It's available both on the server and on the client side.
280+
// May be nil if a certificate wasn't exchanged by this party in the
281+
// handshake, e.g. a client opening a connection without providing a client
282+
// cert.
283+
LocalCertificate *Certificate
284+
278285
// VerifiedChains is a list of one or more chains where the first element is
279286
// PeerCertificates[0] and the last element is from Config.RootCAs (on the
280287
// client side) or Config.ClientCAs (on the server side).

src/crypto/tls/conn.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Conn struct {
5555
ocspResponse []byte // stapled OCSP response
5656
scts [][]byte // signed certificate timestamps from server
5757
peerCertificates []*x509.Certificate
58+
localCertificate *Certificate
5859
// verifiedChains contains the certificate chains that we built, as
5960
// opposed to the ones presented by the server.
6061
verifiedChains [][]*x509.Certificate
@@ -1619,6 +1620,7 @@ func (c *Conn) connectionStateLocked() ConnectionState {
16191620
state.ServerName = c.serverName
16201621
state.CipherSuite = c.cipherSuite
16211622
state.PeerCertificates = c.peerCertificates
1623+
state.LocalCertificate = c.localCertificate
16221624
state.VerifiedChains = c.verifiedChains
16231625
state.SignedCertificateTimestamps = c.scts
16241626
state.OCSPResponse = c.ocspResponse

src/crypto/tls/handshake_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ func (hs *clientHandshakeState) doFullHandshake() error {
834834
return err
835835
}
836836
}
837+
hs.c.localCertificate = chainToSend
837838

838839
signed := hs.finishedHash.hashForClientCertificate(sigType, sigHash)
839840
signOpts := crypto.SignerOpts(sigHash)

src/crypto/tls/handshake_client_tls13.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ func (hs *clientHandshakeStateTLS13) sendClientCertificate() error {
823823
if _, err := hs.c.writeHandshakeRecord(certVerifyMsg, hs.transcript); err != nil {
824824
return err
825825
}
826+
hs.c.localCertificate = cert
826827

827828
return nil
828829
}

src/crypto/tls/handshake_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ func (hs *serverHandshakeState) processClientHello() error {
283283
}
284284
return err
285285
}
286+
hs.c.localCertificate = hs.cert
286287
if hs.clientHello.scts {
287288
hs.hello.scts = hs.cert.SignedCertificateTimestamps
288289
}

src/crypto/tls/handshake_server_tls13.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ func (hs *serverHandshakeStateTLS13) pickCertificate() error {
533533
return err
534534
}
535535
hs.cert = certificate
536+
hs.c.localCertificate = hs.cert
536537

537538
return nil
538539
}

0 commit comments

Comments
 (0)