2017-12-29 04:16:20 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
// This is where we record which signatures we've previously checked
|
|
|
|
// It's so we can avoid needlessly checking them again
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type sigManager struct {
|
2018-01-04 22:37:51 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
checked map[sigBytes]knownSig
|
|
|
|
lastCleaned time.Time
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type knownSig struct {
|
2018-01-04 22:37:51 +00:00
|
|
|
bs []byte
|
|
|
|
time time.Time
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sigManager) init() {
|
2018-01-04 22:37:51 +00:00
|
|
|
m.checked = make(map[sigBytes]knownSig)
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sigManager) check(key *sigPubKey, sig *sigBytes, bs []byte) bool {
|
2018-01-04 22:37:51 +00:00
|
|
|
if m.isChecked(sig, bs) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
verified := verify(key, bs, sig)
|
|
|
|
if verified {
|
|
|
|
m.putChecked(sig, bs)
|
|
|
|
}
|
|
|
|
return verified
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sigManager) isChecked(sig *sigBytes, bs []byte) bool {
|
2018-01-04 22:37:51 +00:00
|
|
|
m.mutex.RLock()
|
|
|
|
defer m.mutex.RUnlock()
|
|
|
|
k, isIn := m.checked[*sig]
|
|
|
|
if !isIn {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(bs) != len(k.bs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(bs); idx++ {
|
|
|
|
if bs[idx] != k.bs[idx] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
k.time = time.Now()
|
|
|
|
return true
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sigManager) putChecked(newsig *sigBytes, bs []byte) {
|
2018-01-04 22:37:51 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
if time.Since(m.lastCleaned) > 60*time.Second {
|
|
|
|
// Since we have the write lock anyway, do some cleanup
|
|
|
|
for s, k := range m.checked {
|
|
|
|
if time.Since(k.time) > 60*time.Second {
|
|
|
|
delete(m.checked, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.lastCleaned = now
|
|
|
|
}
|
|
|
|
k := knownSig{bs: bs, time: now}
|
|
|
|
m.checked[*newsig] = k
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|