forked from msgboxio/ike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
46 lines (36 loc) · 945 Bytes
/
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
package ike
import (
"crypto/rand"
"crypto/sha1"
stderr "errors"
"math/big"
"net"
"github.com/msgboxio/ike/protocol"
)
// An implementation of COOKIE as specified in
// 2.6. IKE SA SPIs and Cookies
var errMissingCookie = stderr.New("NEED COOKIE")
var errInvalidCookie = stderr.New("INVALID COOKIE")
type peerRequestsCookieError struct {
Cookie *protocol.NotifyPayload
}
func (e peerRequestsCookieError) Error() string {
return "Rx Cookie"
}
// Version for COOKIE
var cookieVersion []byte
// Secret for COOKIE
var cookieSecret [64]byte
func init() {
cookieVersion = []byte{0, 0}
rand.Read(cookieSecret[:])
}
func getCookie(no *big.Int, spiI []byte, remote net.Addr) []byte {
// Cookie = <VersionIDofSecret> | Hash(Ni | IPi | SPIi | <secret>)
digest := sha1.New()
digest.Write(no.Bytes())
digest.Write(spiI)
digest.Write(AddrToIp(remote))
digest.Write(cookieSecret[:])
return append(cookieVersion, digest.Sum(nil)...)
}