-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity.go
212 lines (172 loc) · 5.18 KB
/
identity.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package berghain
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"net/netip"
"sync"
"github.com/dropmorepackets/haproxy-go/pkg/buffer"
)
// 03|3778206500000000|1edb6858c727c3519825ac8a8777d94282fe476c4d3e0b6a7247dc5fa2d4ed7f
// uint8 + uint64 + sha256 (32byte) = 41 byte
// this encoded into hex = 82 byte
// adding two spacers = 2 byte
// total = 82 bytes
const encodedCookieSize = 84
var cookieBufferPool = sync.Pool{
New: func() any {
return buffer.NewSliceBuffer(encodedCookieSize)
},
}
func AcquireCookieBuffer() *buffer.SliceBuffer {
return cookieBufferPool.Get().(*buffer.SliceBuffer)
}
func ReleaseCookieBuffer(b *buffer.SliceBuffer) {
b.Reset()
cookieBufferPool.Put(b)
}
type RequestIdentifier struct {
SrcAddr netip.Addr
Host []byte
Level uint8
}
func (ri RequestIdentifier) WriteTo(h io.Writer) (int64, error) {
raw := AcquireCookieBuffer()
defer ReleaseCookieBuffer(raw)
var written int64
// Write Host to the hash
n, err := h.Write(ri.Host)
written += int64(n)
if err != nil {
return written, err
}
// Write SrcAddr first to the buffer and then to the hash.
// netip.AsSlice does an allocation we want to avoid.
addrSlice := raw.WriteBytes()[:0] // reset length to zero
addrSlice = ri.SrcAddr.AppendTo(addrSlice)
n, err = h.Write(addrSlice)
written += int64(n)
if err != nil {
return written, err
}
raw.Reset()
// Write Level to the buffer and to the hash
raw.WriteNBytes(1)[0] = ri.Level
n, err = h.Write(raw.ReadBytes())
written += int64(n)
if err != nil {
return written, err
}
return written, nil
}
func (ri RequestIdentifier) ToCookie(b *Berghain, enc *buffer.SliceBuffer) error {
raw := AcquireCookieBuffer()
defer ReleaseCookieBuffer(raw)
h := b.acquireHMAC()
defer b.releaseHMAC(h)
// Write Host to the hash
if _, err := h.Write(ri.Host); err != nil {
return err
}
// Write SrcAddr first to the buffer and then to the hash.
// netip.AsSlice does an allocation we want to avoid.
addrSlice := raw.WriteBytes()[:0] // reset length to zero
addrSlice = ri.SrcAddr.AppendTo(addrSlice)
if _, err := h.Write(addrSlice); err != nil {
return err
}
raw.Reset()
// Write Level to the buffer and to the hash
raw.WriteNBytes(1)[0] = ri.Level
if _, err := h.Write(raw.ReadBytes()); err != nil {
return err
}
// Write the hex encoded level to the buffer and append this to the output.
levelArea := enc.WriteNBytes(hex.EncodedLen(raw.Len()))
hex.Encode(levelArea, raw.ReadBytes())
raw.Reset()
// Write a spacer to the output.
enc.WriteNBytes(1)[0] = '|'
// Calculate the expiration of the cookie, write it to the buffer and hash.
expireAt := tc.Now().Add(b.LevelConfig(ri.Level).Duration)
binary.LittleEndian.PutUint64(raw.WriteNBytes(8), uint64(expireAt.Unix()))
if _, err := h.Write(raw.ReadBytes()); err != nil {
return err
}
// Write the hex encoded expiration to the output.
expireArea := enc.WriteNBytes(hex.EncodedLen(raw.Len()))
hex.Encode(expireArea, raw.ReadBytes())
raw.Reset()
// Write another spacer to the output.
enc.WriteNBytes(1)[0] = '|'
// Finally generate the sum and write that with hex encoding to the output.
sumArea := enc.WriteNBytes(hex.EncodedLen(h.Size()))
hex.Encode(sumArea, h.Sum(nil))
return nil
}
var (
ErrInvalidLength = fmt.Errorf("invalid length")
ErrLevelTooLow = fmt.Errorf("cookie level too low")
ErrExpired = fmt.Errorf("expired")
ErrInvalidHMAC = fmt.Errorf("invalid hmac")
)
func (b *Berghain) IsValidCookie(ri RequestIdentifier, cookie []byte) error {
if len(cookie) != encodedCookieSize {
return ErrInvalidLength
}
dec := AcquireCookieBuffer()
defer ReleaseCookieBuffer(dec)
h := b.acquireHMAC()
defer b.releaseHMAC(h)
if _, err := h.Write(ri.Host); err != nil {
return err
}
// Write SrcAddr first to the buffer and then to the hash.
// netip.AsSlice does an allocation we want to avoid.
addrSlice := dec.WriteBytes()[:0] // reset capacity to zero
addrSlice = ri.SrcAddr.AppendTo(addrSlice)
if _, err := h.Write(addrSlice); err != nil {
return err
}
dec.Reset()
cookieBuf := buffer.NewSliceBufferWithSlice(cookie)
cookieLevel := cookieBuf.ReadNBytes(hex.EncodedLen(1))
cookieBuf.AdvanceR(1) // Separator
levelArea := dec.WriteNBytes(hex.DecodedLen(len(cookieLevel)))
if _, err := hex.Decode(levelArea, cookieLevel); err != nil {
return err
}
// Untrusted input is compared!
if ri.Level > dec.ReadBytes()[0] {
return ErrLevelTooLow
}
if _, err := h.Write(dec.ReadBytes()); err != nil {
return err
}
dec.Reset()
cookieExpiration := cookieBuf.ReadNBytes(hex.EncodedLen(8))
cookieBuf.AdvanceR(1) // Separator
expirArea := dec.WriteNBytes(hex.DecodedLen(len(cookieExpiration)))
if _, err := hex.Decode(expirArea, cookieExpiration); err != nil {
return err
}
// Untrusted input is decoded and compared!
if uint64(tc.Now().Unix()) > binary.LittleEndian.Uint64(dec.ReadBytes()) {
return ErrExpired
}
if _, err := h.Write(dec.ReadBytes()); err != nil {
return err
}
dec.Reset()
cookieSum := cookieBuf.ReadBytes()
sumArea := dec.WriteNBytes(hex.DecodedLen(len(cookieSum)))
if _, err := hex.Decode(sumArea, cookieSum); err != nil {
return err
}
if !bytes.Equal(h.Sum(nil), dec.ReadBytes()) {
return ErrInvalidHMAC
}
return nil
}