Fix a race regarding client keyPairs caching

This commit is contained in:
Qian Wang 2019-01-01 14:37:47 +00:00
parent f3f3042c81
commit 59c18437e4
3 changed files with 7 additions and 15 deletions

View File

@ -38,15 +38,17 @@ func MakeSessionTicket(sta *State) []byte {
// for encrypting the UID // for encrypting the UID
tthInterval := sta.Now().Unix() / int64(sta.TicketTimeHint) tthInterval := sta.Now().Unix() / int64(sta.TicketTimeHint)
ec := ecdh.NewCurve25519ECDH() ec := ecdh.NewCurve25519ECDH()
ephKP := sta.getKeyPair(tthInterval) sta.keyPairsM.Lock()
ephKP := sta.keyPairs[tthInterval]
if ephKP == nil { if ephKP == nil {
ephPv, ephPub, _ := ec.GenerateKey(rand.Reader) ephPv, ephPub, _ := ec.GenerateKey(rand.Reader)
ephKP = &keyPair{ ephKP = &keyPair{
ephPv, ephPv,
ephPub, ephPub,
} }
sta.putKeyPair(tthInterval, ephKP) sta.keyPairs[tthInterval] = ephKP
} }
sta.keyPairsM.Unlock()
ticket := make([]byte, 192) ticket := make([]byte, 192)
copy(ticket[0:32], ec.Marshal(ephKP.PublicKey)) copy(ticket[0:32], ec.Marshal(ephKP.PublicKey))
key, _ := ec.GenerateSharedSecret(ephKP.PrivateKey, sta.staticPub) key, _ := ec.GenerateSharedSecret(ephKP.PrivateKey, sta.staticPub)

View File

@ -123,15 +123,3 @@ func (sta *State) ParseConfig(conf string) (err error) {
sta.staticPub = pub sta.staticPub = pub
return nil return nil
} }
func (sta *State) getKeyPair(tthInterval int64) *keyPair {
sta.keyPairsM.Lock()
defer sta.keyPairsM.Unlock()
return sta.keyPairs[tthInterval]
}
func (sta *State) putKeyPair(tthInterval int64, kp *keyPair) {
sta.keyPairsM.Lock()
sta.keyPairs[tthInterval] = kp
sta.keyPairsM.Unlock()
}

View File

@ -346,7 +346,8 @@ func (up *Userpanel) syncMemFromDB(UID []byte) error {
return nil return nil
} }
// the following functions will return err==nil if user is not active // the following functions will update the db entries first, then if the
// user is active, it will update it in memory.
func (up *Userpanel) setSessionsCap(UID []byte, cap uint32) error { func (up *Userpanel) setSessionsCap(UID []byte, cap uint32) error {
err := up.updateDBEntryUint32(UID, "SessionsCap", cap) err := up.updateDBEntryUint32(UID, "SessionsCap", cap)
@ -446,6 +447,7 @@ func (up *Userpanel) addUpCredit(UID []byte, delta int64) error {
u.addUpCredit(delta) u.addUpCredit(delta)
return nil return nil
} }
func (up *Userpanel) addDownCredit(UID []byte, delta int64) error { func (up *Userpanel) addDownCredit(UID []byte, delta int64) error {
err := up.db.Update(func(tx *bolt.Tx) error { err := up.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(UID) b := tx.Bucket(UID)