@@ -256,33 +256,32 @@ func (hs *serverHandshakeStateTLS13) processClientHello() error {
256256 }
257257 }
258258
259- // Pick the ECDHE group in server preference order, but give priority to
260- // groups with a key share, to avoid a HelloRetryRequest round-trip.
259+ // Pick group by server preference. In contrast to upstream Go, we will
260+ // send an HelloRetryRequest and accept an extra roundtrip if there is
261+ // a more preferred group, than those for which the client has sent
262+ // a keyshare in the initial ClientHello.
263+ // Cf. https://datatracker.ietf.org/doc/draft-davidben-tls-key-share-prediction/
261264 var selectedGroup CurveID
262265 var clientKeyShare * keyShare
263266GroupSelection:
264267 for _ , preferredGroup := range supportedCurves {
265- for _ , ks := range hs .clientHello .keyShares {
266- if ks .group == preferredGroup {
267- selectedGroup = ks .group
268- clientKeyShare = & ks
269- break GroupSelection
270- }
271- }
272- if selectedGroup != 0 {
273- continue
274- }
275268 for _ , group := range hs .clientHello .supportedCurves {
276269 if group == preferredGroup {
277270 selectedGroup = group
278- break
271+ break GroupSelection
279272 }
280273 }
281274 }
282275 if selectedGroup == 0 {
283276 c .sendAlert (alertHandshakeFailure )
284277 return errors .New ("tls: no ECDHE curve supported by both client and server" )
285278 }
279+ for _ , ks := range hs .clientHello .keyShares {
280+ if ks .group == selectedGroup {
281+ clientKeyShare = & ks
282+ break
283+ }
284+ }
286285 if clientKeyShare == nil {
287286 if err := hs .doHelloRetryRequest (selectedGroup ); err != nil {
288287 return err
0 commit comments