Skip to content

Commit d4d586c

Browse files
JoTurkJulia-RomanJuliapixeltreejadey
committed
Export handshake Finished base keys
Co-authored-by: Julia <supa@juliaelena.ro> Co-authored-by: Julia <git@juliapixel.com> Co-authored-by: Jade <jadey@tutanota.com>
1 parent 0299894 commit d4d586c

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

internal/handshake/traffic_secrets.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,32 @@ func ensureMasterSecret(state *dtlsstate.State13) ([]byte, error) {
341341
return masterSecret, nil
342342
}
343343

344+
// ClientHandshakeFinishedBaseKey returns the client handshake traffic secret,
345+
// which is the TLS 1.3 Finished base key for the client's Finished message.
346+
func ClientHandshakeFinishedBaseKey(state *dtlsstate.State13) ([]byte, error) {
347+
if state == nil {
348+
return nil, dtlserrors.ErrCipherSuiteNotSet
349+
}
350+
if len(state.KeySchedule.HandshakeTraffic.Client) == 0 {
351+
return nil, dtlserrors.ErrLengthMismatch
352+
}
353+
354+
return state.KeySchedule.HandshakeTraffic.Client, nil
355+
}
356+
357+
// ServerHandshakeFinishedBaseKey returns the server handshake traffic secret,
358+
// which is the TLS 1.3 Finished base key for the server's Finished message.
359+
func ServerHandshakeFinishedBaseKey(state *dtlsstate.State13) ([]byte, error) {
360+
if state == nil {
361+
return nil, dtlserrors.ErrCipherSuiteNotSet
362+
}
363+
if len(state.KeySchedule.HandshakeTraffic.Server) == 0 {
364+
return nil, dtlserrors.ErrLengthMismatch
365+
}
366+
367+
return state.KeySchedule.HandshakeTraffic.Server, nil
368+
}
369+
344370
// CertificateVerifyInputFromTranscript returns the TLS 1.3 CertificateVerify
345371
// input for the current transcript snapshot.
346372
func CertificateVerifyInputFromTranscript(
@@ -462,8 +488,7 @@ func verifyFinishedData(hashFunc func() hash.Hash, baseKey, transcriptHash, veri
462488
}
463489

464490
// VerifyFinishedDataFromTranscript verifies TLS 1.3 Finished verify_data
465-
// against the current transcript snapshot. It does not commit the Finished
466-
// message to the transcript.
491+
// against the current transcript snapshot.
467492
func VerifyFinishedDataFromTranscript(
468493
hashFunc func() hash.Hash,
469494
baseKey []byte,

internal/handshake/transcript_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,29 @@ func TestCertificateVerifyInput13ServerAndClient(t *testing.T) {
865865
assert.NotEqual(t, serverInput, clientInput)
866866
}
867867

868+
func TestHandshakeFinishedBaseKeys13(t *testing.T) {
869+
clientSecret := bytes.Repeat([]byte{0x11}, sha256.Size)
870+
serverSecret := bytes.Repeat([]byte{0x22}, sha256.Size)
871+
state := newTestState13(false)
872+
state.KeySchedule.HandshakeTraffic = dtlsstate.TrafficSecrets{
873+
Client: clientSecret,
874+
Server: serverSecret,
875+
}
876+
877+
clientBaseKey, err := ClientHandshakeFinishedBaseKey(state)
878+
require.NoError(t, err)
879+
assert.Equal(t, clientSecret, clientBaseKey)
880+
881+
serverBaseKey, err := ServerHandshakeFinishedBaseKey(state)
882+
require.NoError(t, err)
883+
assert.Equal(t, serverSecret, serverBaseKey)
884+
885+
_, err = ClientHandshakeFinishedBaseKey(newTestState13(false))
886+
assert.ErrorIs(t, err, dtlserrors.ErrLengthMismatch)
887+
_, err = ServerHandshakeFinishedBaseKey(newTestState13(false))
888+
assert.ErrorIs(t, err, dtlserrors.ErrLengthMismatch)
889+
}
890+
868891
func TestFinishedVerifyData13(t *testing.T) {
869892
baseKey := bytes.Repeat([]byte{0x11}, sha256.Size)
870893
transcriptHash := bytes.Repeat([]byte{0x22}, sha256.Size)

0 commit comments

Comments
 (0)