-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.go
162 lines (132 loc) · 2.76 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package prekeyserver
import (
"sync"
"time"
"github.com/otrv4/ed448"
"github.com/otrv4/gotrx"
)
type sessionManager struct {
s map[string]*realSession
sync.RWMutex
}
type session interface {
save(*gotrx.Keypair, ed448.Point, uint32, *gotrx.ClientProfile)
instanceTag() uint32
macKey() []byte
sharedSecret() []byte
clientProfile() *gotrx.ClientProfile
pointI() ed448.Point
keypairS() *gotrx.Keypair
hasExpired(time.Duration) bool
}
type realSession struct {
tag uint32
s *gotrx.Keypair
i ed448.Point
cp *gotrx.ClientProfile
storedMac []byte
sk []byte
lastTouched time.Time
sync.Mutex
}
// expects the lock to be held
func (s *realSession) touch() {
s.lastTouched = time.Now()
}
func (s *realSession) save(kp *gotrx.Keypair, i ed448.Point, tag uint32, cp *gotrx.ClientProfile) {
s.Lock()
defer s.Unlock()
s.touch()
s.s = kp
s.i = i
s.tag = tag
s.cp = cp
}
func (s *realSession) instanceTag() uint32 {
s.Lock()
defer s.Unlock()
s.touch()
return s.tag
}
func (s *realSession) clientProfile() *gotrx.ClientProfile {
s.Lock()
defer s.Unlock()
s.touch()
return s.cp
}
func (s *realSession) pointI() ed448.Point {
s.Lock()
defer s.Unlock()
s.touch()
return s.i
}
func (s *realSession) keypairS() *gotrx.Keypair {
s.Lock()
defer s.Unlock()
s.touch()
return s.s
}
func (s *realSession) macKey() []byte {
s.Lock()
defer s.Unlock()
s.touch()
if s.storedMac != nil {
return s.storedMac
}
return gotrx.KdfPrekeyServer(usagePreMACKey, 64, gotrx.KdfPrekeyServer(usageSK, skLength, gotrx.SerializePoint(ed448.PointScalarMul(s.i, s.s.Priv.K()))))
}
func (s *realSession) sharedSecret() []byte {
s.Lock()
defer s.Unlock()
s.touch()
if s.sk != nil {
return s.sk
}
return gotrx.KdfPrekeyServer(usageSK, skLength, gotrx.SerializePoint(ed448.PointScalarMul(s.i, s.s.Priv.K())))
}
func (s *realSession) hasExpired(timeout time.Duration) bool {
s.Lock()
defer s.Unlock()
return s.lastTouched.Add(timeout).Before(time.Now())
}
func newSessionManager() *sessionManager {
return &sessionManager{
s: make(map[string]*realSession),
}
}
func (sm *sessionManager) get(name string) session {
sm.RLock()
s, ok := sm.s[name]
sm.RUnlock()
if !ok {
s = &realSession{}
sm.Lock()
sm.s[name] = s
sm.Unlock()
}
return s
}
func (sm *sessionManager) complete(name string) {
sm.Lock()
defer sm.Unlock()
delete(sm.s, name)
}
func (sm *sessionManager) has(name string) bool {
sm.RLock()
defer sm.RUnlock()
_, ok := sm.s[name]
return ok
}
func (sm *sessionManager) cleanup(timeout time.Duration) {
sm.Lock()
defer sm.Unlock()
toRemove := []string{}
for nm, s := range sm.s {
if s.hasExpired(timeout) {
toRemove = append(toRemove, nm)
}
}
for _, nm := range toRemove {
delete(sm.s, nm)
}
}