forked from bpowers/seshcookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseshcookie.go
381 lines (320 loc) · 9.44 KB
/
seshcookie.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2011 Bobby Powers. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package seshcookie
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/gob"
"errors"
"hash"
"io"
"log"
"net"
"net/http"
"sync"
"sync/atomic"
"time"
)
// we want 16 byte blocks, for AES-128
const blockSize = 16
var (
// if you don't need multiple independent seshcookie
// instances, you can use this RequestSessions instance to
// manage & access your sessions. Simply use it as the final
// parameter in your call to seshcookie.NewSessionHandler, and
// whenever you want to access the current session from an
// embedded http.Handler you can simply call:
//
// seshcookie.Session.Get(req)
Session = &RequestSessions{HttpOnly: true}
// Hash validation of the decrypted cookie failed. Most likely
// the session was encoded with a different cookie than we're
// using to decode it, but its possible the client (or someone
// else) tried to modify the session.
HashError = errors.New("Hash validation failed")
// The cookie is too short, so we must exit decoding early.
LenError = errors.New("Bad cookie length")
)
type sessionResponseWriter struct {
http.ResponseWriter
h *SessionHandler
req *http.Request
// int32 so we can use the sync/atomic functions on it
wroteHeader int32
}
type SessionHandler struct {
http.Handler
CookieName string // name of the cookie to store our session in
CookiePath string // resource path the cookie is valid for
RS *RequestSessions
encKey []byte
hmacKey []byte
}
type RequestSessions struct {
HttpOnly bool // don't allow javascript to access cookie
Secure bool // only send session over HTTPS
lk sync.Mutex
m map[*http.Request]map[string]interface{}
// stores a hash of the serialized session (the gob) that we
// received with the start of the request. Before setting a
// cookie for the reply, check to see if the session has
// actually changed. If it hasn't, then we don't need to send
// a new cookie.
hm map[*http.Request][]byte
}
func (rs *RequestSessions) Get(req *http.Request) map[string]interface{} {
rs.lk.Lock()
defer rs.lk.Unlock()
if rs.m == nil {
log.Print("seshcookie: warning! trying to get session " +
"data for unknown request. Perhaps your handler " +
"isn't wrapped by a SessionHandler?")
return nil
}
return rs.m[req]
}
func (rs *RequestSessions) getHash(req *http.Request) []byte {
rs.lk.Lock()
defer rs.lk.Unlock()
if rs.hm == nil {
return nil
}
return rs.hm[req]
}
func (rs *RequestSessions) Set(req *http.Request, val map[string]interface{}, gobHash []byte) {
rs.lk.Lock()
defer rs.lk.Unlock()
if rs.m == nil {
rs.m = map[*http.Request]map[string]interface{}{}
rs.hm = map[*http.Request][]byte{}
}
rs.m[req] = val
rs.hm[req] = gobHash
}
func (rs *RequestSessions) Clear(req *http.Request) {
rs.lk.Lock()
defer rs.lk.Unlock()
delete(rs.m, req)
delete(rs.hm, req)
}
func encodeGob(obj interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
err := enc.Encode(obj)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func decodeGob(encoded []byte) (map[string]interface{}, error) {
buf := bytes.NewBuffer(encoded)
dec := gob.NewDecoder(buf)
var out map[string]interface{}
err := dec.Decode(&out)
if err != nil {
return nil, err
}
return out, nil
}
// encode uses the given block cipher (in CTR mode) to encrypt the
// data, along with a hash, returning the iv and the ciphertext. What
// is returned looks like:
//
// encrypted(salt + sessionData) + iv + hmac
//
func encode(block cipher.Block, hmac hash.Hash, data []byte) ([]byte, error) {
buf := bytes.NewBuffer(nil)
salt := make([]byte, block.BlockSize())
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return nil, err
}
buf.Write(salt)
buf.Write(data)
session := buf.Bytes()
iv := make([]byte, block.BlockSize())
if _, err := rand.Read(iv); err != nil {
return nil, err
}
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(session, session)
buf.Write(iv)
hmac.Write(buf.Bytes())
buf.Write(hmac.Sum(nil))
return buf.Bytes(), nil
}
func encodeCookie(content interface{}, encKey, hmacKey []byte) (string, []byte, error) {
encodedGob, err := encodeGob(content)
if err != nil {
return "", nil, err
}
gobHash := sha1.New()
gobHash.Write(encodedGob)
aesCipher, err := aes.NewCipher(encKey)
if err != nil {
return "", nil, err
}
hmacHash := hmac.New(sha256.New, hmacKey)
sessionBytes, err := encode(aesCipher, hmacHash, encodedGob)
if err != nil {
return "", nil, err
}
return base64.StdEncoding.EncodeToString(sessionBytes), gobHash.Sum(nil), nil
}
// decode uses the given block cipher (in CTR mode) to decrypt the
// data, and validate the hash. If hash validation fails, an error is
// returned.
func decode(block cipher.Block, hmac hash.Hash, ciphertext []byte) ([]byte, error) {
if len(ciphertext) < 2*block.BlockSize()+hmac.Size() {
return nil, LenError
}
receivedHmac := ciphertext[len(ciphertext)-hmac.Size():]
ciphertext = ciphertext[:len(ciphertext)-hmac.Size()]
hmac.Write(ciphertext)
if subtle.ConstantTimeCompare(hmac.Sum(nil), receivedHmac) != 1 {
return nil, HashError
}
// split the iv and session bytes
iv := ciphertext[len(ciphertext)-block.BlockSize():]
session := ciphertext[:len(ciphertext)-block.BlockSize()]
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(session, session)
// skip past the iv
session = session[block.BlockSize():]
return session, nil
}
func decodeCookie(encoded string, encKey, hmacKey []byte) (map[string]interface{}, []byte, error) {
sessionBytes, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return nil, nil, err
}
aesCipher, err := aes.NewCipher(encKey)
if err != nil {
return nil, nil, err
}
hmacHash := hmac.New(sha256.New, hmacKey)
gobBytes, err := decode(aesCipher, hmacHash, sessionBytes)
if err != nil {
return nil, nil, err
}
gobHash := sha1.New()
gobHash.Write(gobBytes)
session, err := decodeGob(gobBytes)
if err != nil {
log.Printf("decodeGob: %s\n", err)
return nil, nil, err
}
return session, gobHash.Sum(nil), nil
}
func (s sessionResponseWriter) Write(data []byte) (int, error) {
s.WriteHeader(http.StatusOK)
return s.ResponseWriter.Write(data)
}
func (s sessionResponseWriter) WriteHeader(code int) {
if atomic.AddInt32(&s.wroteHeader, 1) == 1 {
origCookie, err := s.req.Cookie(s.h.CookieName)
var origCookieVal string
if err != nil {
origCookieVal = ""
} else {
origCookieVal = origCookie.Value
}
session := s.h.RS.Get(s.req)
if len(session) == 0 {
// if we have an empty session, but the
// request didn't start out that way, we
// assume the user wants us to clear the
// session
if origCookieVal != "" {
//log.Println("clearing cookie")
var cookie http.Cookie
cookie.Name = s.h.CookieName
cookie.Value = ""
cookie.Path = "/"
// a cookie is expired by setting it
// with an expiration time in the past
cookie.Expires = time.Unix(0, 0).UTC()
http.SetCookie(s, &cookie)
}
goto write
}
encoded, gobHash, err := encodeCookie(session, s.h.encKey, s.h.hmacKey)
if err != nil {
log.Printf("createCookie: %s\n", err)
goto write
}
if bytes.Equal(gobHash, s.h.RS.getHash(s.req)) {
//log.Println("not re-setting identical cookie")
goto write
}
var cookie http.Cookie
cookie.Name = s.h.CookieName
cookie.Value = encoded
cookie.Path = s.h.CookiePath
cookie.HttpOnly = s.h.RS.HttpOnly
cookie.Secure = s.h.RS.Secure
http.SetCookie(s, &cookie)
}
write:
s.ResponseWriter.WriteHeader(code)
}
func (s sessionResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, _ := s.ResponseWriter.(http.Hijacker)
return hijacker.Hijack()
}
func (h *SessionHandler) getCookieSession(req *http.Request) (map[string]interface{}, []byte) {
cookie, err := req.Cookie(h.CookieName)
if err != nil {
//log.Printf("getCookieSesh: '%#v' not found\n",
// h.CookieName)
return map[string]interface{}{}, nil
}
session, gobHash, err := decodeCookie(cookie.Value, h.encKey, h.hmacKey)
if err != nil {
// this almost always just means that the user doesn't
// have a valid login.
//log.Printf("decodeCookie: %s\n", err)
return map[string]interface{}{}, nil
}
return session, gobHash
}
func (h *SessionHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// get our session a little early, so that we can add our
// authentication information to it if we get some
session, gobHash := h.getCookieSession(req)
h.RS.Set(req, session, gobHash)
defer h.RS.Clear(req)
sessionWriter := sessionResponseWriter{rw, h, req, 0}
h.Handler.ServeHTTP(sessionWriter, req)
}
func NewSessionHandler(handler http.Handler, key string, rs *RequestSessions) *SessionHandler {
// sha1 sums are 20 bytes long. we use the first 16 bytes as
// the aes key.
encHash := sha1.New()
encHash.Write([]byte(key))
encHash.Write([]byte("-encryption"))
hmacHash := sha1.New()
hmacHash.Write([]byte(key))
hmacHash.Write([]byte("-hmac"))
// if the user hasn't specified a session handler, use the
// package's default one
if rs == nil {
rs = Session
}
return &SessionHandler{
Handler: handler,
CookieName: "session",
CookiePath: "/",
RS: rs,
encKey: encHash.Sum(nil)[:blockSize],
hmacKey: hmacHash.Sum(nil)[:blockSize],
}
}