-
Notifications
You must be signed in to change notification settings - Fork 706
/
secure_cookie.go
112 lines (103 loc) · 2.88 KB
/
secure_cookie.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
package web
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"errors"
"golang.org/x/crypto/pbkdf2"
"io"
"strings"
)
const (
pbkdf2Iterations = 64000
keySize = 32
)
var (
ErrMissingCookieSecret = errors.New("Secret Key for secure cookies has not been set. Assign one to web.Config.CookieSecret.")
ErrInvalidKey = errors.New("The keys for secure cookies have not been initialized. Ensure that a Run* method is being called")
)
func (ctx *Context) SetSecureCookie(name string, val string, age int64) error {
server := ctx.Server
if len(server.Config.CookieSecret) == 0 {
return ErrMissingCookieSecret
}
if len(server.encKey) == 0 || len(server.signKey) == 0 {
return ErrInvalidKey
}
ciphertext, err := encrypt([]byte(val), server.encKey)
if err != nil {
return err
}
sig := sign(ciphertext, server.signKey)
data := base64.StdEncoding.EncodeToString(ciphertext) + "|" + base64.StdEncoding.EncodeToString(sig)
ctx.SetCookie(NewCookie(name, data, age))
return nil
}
func (ctx *Context) GetSecureCookie(name string) (string, bool) {
for _, cookie := range ctx.Request.Cookies() {
if cookie.Name != name {
continue
}
parts := strings.SplitN(cookie.Value, "|", 2)
if len(parts) != 2 {
return "", false
}
ciphertext, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
return "", false
}
sig, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return "", false
}
expectedSig := sign([]byte(ciphertext), ctx.Server.signKey)
if !bytes.Equal(expectedSig, sig) {
return "", false
}
plaintext, err := decrypt(ciphertext, ctx.Server.encKey)
if err != nil {
return "", false
}
return string(plaintext), true
}
return "", false
}
func genKey(password string, salt string) []byte {
return pbkdf2.Key([]byte(password), []byte(salt), pbkdf2Iterations, keySize, sha512.New)
}
func encrypt(plaintext []byte, key []byte) ([]byte, error) {
aesCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCTR(aesCipher, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, nil
}
func decrypt(ciphertext []byte, key []byte) ([]byte, error) {
if len(ciphertext) <= aes.BlockSize {
return nil, errors.New("Invalid cipher text")
}
aesCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plaintext := make([]byte, len(ciphertext)-aes.BlockSize)
stream := cipher.NewCTR(aesCipher, ciphertext[:aes.BlockSize])
stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:])
return plaintext, nil
}
func sign(data []byte, key []byte) []byte {
mac := hmac.New(sha512.New, key)
mac.Write(data)
return mac.Sum(nil)
}