Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion key_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type keySharePrivateKeys struct {
mlkem crypto.Decapsulator

mlkemEcdhe *ecdh.PrivateKey // [uTLS] seperate ecdhe key for pq keyshare in line with Chrome, instead of reusing ecdhe key like stdlib
// [uTLS] keys for classical key shares offered after the first (ecdhe)
additionalEcdhe []*ecdh.PrivateKey
}

// A keyExchange implements a TLS 1.3 KEM.
Expand Down Expand Up @@ -190,7 +192,7 @@ func (ke *ecdhKeyExchange) clientSharedSecretChooseEcdhe(priv *keySharePrivateKe
return nil, err
}
//[uTLS] SECTION START
ecdhe := priv.ecdhe
ecdhe := priv.ecdheFor(ke.curve)
if useSeperateEcdhe {
if priv.mlkemEcdhe == nil {
return nil, errors.New("tls: missing separate hybrid ECDHE private key")
Expand Down
16 changes: 16 additions & 0 deletions u_key_schedule.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package tls

import (
"crypto/ecdh"

"golang.org/x/crypto/sha3"
)

// ecdheFor returns the classical private key offered for curve, or nil.
// A client may offer several classical key shares and the server can pick any.
func (k *keySharePrivateKeys) ecdheFor(curve ecdh.Curve) *ecdh.PrivateKey {
if k.ecdhe != nil && k.ecdhe.Curve() == curve {
return k.ecdhe
}
for _, key := range k.additionalEcdhe {
if key.Curve() == curve {
return key
}
}
return nil
}

func kyberSharedSecret(c, K []byte) []byte {
// Package mlkem implements ML-KEM, which compared to Kyber removed a
// final hashing step. Compute SHAKE-256(K || SHA3-256(c), 32) to match Kyber.
Expand Down
76 changes: 76 additions & 0 deletions u_keyshare_selection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tls

import "testing"

// A server may select any group the client sent a key share for, not only the
// first classical one. The client must finish that handshake with the matching
// private key and without a HelloRetryRequest, as the browser it mimics would.
func TestServerSelectsAnyOfferedKeyShare(t *testing.T) {
threeClassical := ClientHelloSpec{
TLSVersMin: VersionTLS13,
TLSVersMax: VersionTLS13,
CipherSuites: []uint16{TLS_AES_128_GCM_SHA256},
CompressionMethods: []uint8{compressionNone},
Extensions: []TLSExtension{
&SNIExtension{},
&SupportedCurvesExtension{Curves: []CurveID{CurveP256, X25519, CurveP384}},
&SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: []SignatureScheme{
ECDSAWithP256AndSHA256, PSSWithSHA256, Ed25519,
}},
&KeyShareExtension{KeyShares: []KeyShare{{Group: CurveP256}, {Group: X25519}, {Group: CurveP384}}},
&SupportedVersionsExtension{Versions: []uint16{VersionTLS13}},
},
}

for _, tc := range []struct {
name string
client ClientHelloID
spec *ClientHelloSpec
server CurveID
}{
// Firefox offers X25519MLKEM768, X25519 (sharing the hybrid's X25519 key)
// and P-256.
{"Firefox/X25519MLKEM768", HelloFirefox_148, nil, X25519MLKEM768},
{"Firefox/X25519", HelloFirefox_148, nil, X25519},
{"Firefox/P-256", HelloFirefox_148, nil, CurveP256},
{"Custom/first", HelloCustom, &threeClassical, CurveP256},
{"Custom/second", HelloCustom, &threeClassical, X25519},
{"Custom/third", HelloCustom, &threeClassical, CurveP384},
} {
t.Run(tc.name, func(t *testing.T) {
clientConfig := testConfigClient.Clone()
clientConfig.ClientSessionCache = nil
serverConfig := testConfigServer.Clone()
serverConfig.MinVersion, serverConfig.MaxVersion = VersionTLS13, VersionTLS13
serverConfig.CurvePreferences = []CurveID{tc.server}
serverConfig.SessionTicketsDisabled = true

clientConn, serverConn := localPipe(t)
client := UClient(clientConn, clientConfig, tc.client)
if tc.spec != nil {
spec := tc.spec.clone()
if err := client.ApplyPreset(spec); err != nil {
t.Fatal(err)
}
}
server := Server(serverConn, serverConfig)
clientResult := make(chan error, 1)
go func() { clientResult <- client.Handshake() }()
serverErr := server.Handshake()
if serverErr != nil {
serverConn.Close()
}
if clientErr := <-clientResult; clientErr != nil || serverErr != nil {
t.Fatalf("handshake: client=%v, server=%v", clientErr, serverErr)
}

state := client.ConnectionState()
if state.CurveID != tc.server {
t.Errorf("negotiated %v, want %v", state.CurveID, tc.server)
}
if state.HelloRetryRequest {
t.Error("server sent a HelloRetryRequest for a group the client already offered")
}
})
}
}
6 changes: 6 additions & 0 deletions u_parrots.go
Original file line number Diff line number Diff line change
Expand Up @@ -3297,6 +3297,9 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error {
// only do this once for the first non-grease curve
uconn.HandshakeState.State13.KeyShareKeys.Ecdhe = reusedKey
preferredCurveIsSet = true
} else {
uconn.HandshakeState.State13.KeyShareKeys.AdditionalEcdhe = append(
uconn.HandshakeState.State13.KeyShareKeys.AdditionalEcdhe, reusedKey)
}
continue
}
Expand All @@ -3312,6 +3315,9 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error {
// only do this once for the first non-grease curve
uconn.HandshakeState.State13.KeyShareKeys.Ecdhe = ecdheKey
preferredCurveIsSet = true
} else {
uconn.HandshakeState.State13.KeyShareKeys.AdditionalEcdhe = append(
uconn.HandshakeState.State13.KeyShareKeys.AdditionalEcdhe, ecdheKey)
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions u_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,9 @@ type KeySharePrivateKeys struct {
Ecdhe *ecdh.PrivateKey
Mlkem crypto.Decapsulator
MlkemEcdhe *ecdh.PrivateKey
// AdditionalEcdhe holds the private keys of classical key shares offered
// after the first one (Ecdhe), so the server may select any of them.
AdditionalEcdhe []*ecdh.PrivateKey
}

func (ksp *KeySharePrivateKeys) ToPrivate() *keySharePrivateKeys {
Expand All @@ -936,9 +939,10 @@ func (ksp *KeySharePrivateKeys) ToPrivate() *keySharePrivateKeys {
}
return &keySharePrivateKeys{
// curveID: ksp.CurveID,
ecdhe: ksp.Ecdhe,
mlkem: ksp.Mlkem,
mlkemEcdhe: ksp.MlkemEcdhe,
ecdhe: ksp.Ecdhe,
mlkem: ksp.Mlkem,
mlkemEcdhe: ksp.MlkemEcdhe,
additionalEcdhe: ksp.AdditionalEcdhe,
}
}

Expand All @@ -948,8 +952,9 @@ func (ksp *keySharePrivateKeys) ToPublic() *KeySharePrivateKeys {
}
return &KeySharePrivateKeys{
// CurveID: ksp.curveID,
Ecdhe: ksp.ecdhe,
Mlkem: ksp.mlkem,
MlkemEcdhe: ksp.mlkemEcdhe,
Ecdhe: ksp.ecdhe,
Mlkem: ksp.mlkem,
MlkemEcdhe: ksp.mlkemEcdhe,
AdditionalEcdhe: ksp.additionalEcdhe,
}
}