@@ -7,14 +7,12 @@ import (
77 "math/big"
88
99 "go.uber.org/zap"
10-
11- "github.com/gotd/td/internal/proto"
12-
1310 "golang.org/x/xerrors"
1411
1512 "github.com/gotd/td/bin"
1613 "github.com/gotd/td/internal/crypto"
1714 "github.com/gotd/td/internal/mt"
15+ "github.com/gotd/td/internal/proto"
1816)
1917
2018// Run runs client-side flow.
@@ -41,6 +39,7 @@ func (c ClientExchange) Run(ctx context.Context) (ClientExchangeResult, error) {
4139 if res .Nonce != nonce {
4240 return ClientExchangeResult {}, xerrors .New ("ResPQ nonce mismatch" )
4341 }
42+ serverNonce := res .ServerNonce
4443
4544 // Selecting first public key that match fingerprint.
4645 var selectedPubKey * rsa.PublicKey
8988 Pq : res .Pq ,
9089 Nonce : nonce ,
9190 NewNonce : newNonce ,
92- ServerNonce : res . ServerNonce ,
91+ ServerNonce : serverNonce ,
9392 P : pBytes ,
9493 Q : qBytes ,
9594 }
@@ -105,7 +104,7 @@ Loop:
105104 }
106105 reqDHParams := & mt.ReqDHParamsRequest {
107106 Nonce : nonce ,
108- ServerNonce : res . ServerNonce ,
107+ ServerNonce : serverNonce ,
109108 P : pBytes ,
110109 Q : qBytes ,
111110 PublicKeyFingerprint : crypto .RSAFingerprint (selectedPubKey ),
@@ -138,8 +137,11 @@ Loop:
138137 if p .Nonce != nonce {
139138 return ClientExchangeResult {}, xerrors .New ("ServerDHParamsOk nonce mismatch" )
140139 }
140+ if p .ServerNonce != serverNonce {
141+ return ClientExchangeResult {}, xerrors .New ("ServerDHParamsOk server nonce mismatch" )
142+ }
141143
142- key , iv := crypto .TempAESKeys (newNonce .BigInt (), res . ServerNonce .BigInt ())
144+ key , iv := crypto .TempAESKeys (newNonce .BigInt (), serverNonce .BigInt ())
143145 // Decrypting inner data.
144146 data , err := crypto .DecryptExchangeAnswer (p .EncryptedAnswer , key , iv )
145147 if err != nil {
@@ -151,6 +153,12 @@ Loop:
151153 if err := innerData .Decode (b ); err != nil {
152154 return ClientExchangeResult {}, err
153155 }
156+ if innerData .Nonce != nonce {
157+ return ClientExchangeResult {}, xerrors .New ("ServerDHInnerData nonce mismatch" )
158+ }
159+ if innerData .ServerNonce != serverNonce {
160+ return ClientExchangeResult {}, xerrors .New ("ServerDHInnerData server nonce mismatch" )
161+ }
154162
155163 dhPrime := big .NewInt (0 ).SetBytes (innerData .DhPrime )
156164 g := big .NewInt (int64 (innerData .G ))
@@ -215,6 +223,13 @@ Loop:
215223 }
216224 switch v := dhSetRes .(type ) {
217225 case * mt.DhGenOk : // dh_gen_ok#3bcbf734
226+ if v .Nonce != nonce {
227+ return ClientExchangeResult {}, xerrors .New ("DhGenOk nonce mismatch" )
228+ }
229+ if v .ServerNonce != serverNonce {
230+ return ClientExchangeResult {}, xerrors .New ("DhGenOk server nonce mismatch" )
231+ }
232+
218233 var key crypto.Key
219234 authKey .FillBytes (key [:])
220235 authKeyID := key .ID ()
0 commit comments