You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cloak/internal/server/auth.go

59 lines
1.3 KiB
Go

6 years ago
package server
import (
"bytes"
"encoding/binary"
"github.com/cbeuw/Cloak/internal/ecdh"
"github.com/cbeuw/Cloak/internal/util"
"log"
6 years ago
)
5 years ago
func TouchStone(ch *ClientHello, sta *State) (isCK bool, UID []byte, sessionID uint32, proxyMethod string, encryptionMethod byte, sharedSecret []byte) {
6 years ago
var random [32]byte
copy(random[:], ch.random)
sta.usedRandomM.Lock()
used := sta.usedRandom[random]
sta.usedRandom[random] = int(sta.Now().Unix())
sta.usedRandomM.Unlock()
6 years ago
if used != 0 {
log.Println("Replay! Duplicate random")
return
6 years ago
}
5 years ago
ephPub, ok := ecdh.Unmarshal(random[:])
if !ok {
return
}
5 years ago
sharedSecret = ecdh.GenerateSharedSecret(sta.staticPv, ephPub)
keyShare, err := parseKeyShare(ch.extensions[[2]byte{0x00, 0x33}])
if err != nil {
return
}
5 years ago
ciphertext := append(ch.sessionId, keyShare...)
5 years ago
if len(ciphertext) != 64 {
return
}
5 years ago
plaintext, err := util.AESGCMDecrypt(random[0:12], sharedSecret, ciphertext)
if err != nil {
return
}
5 years ago
UID = plaintext[0:16]
proxyMethod = string(bytes.Trim(plaintext[16:28], "\x00"))
encryptionMethod = plaintext[28]
timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37]))
if timestamp/int64(TIMESTAMP_WINDOW.Seconds()) != sta.Now().Unix()/int64(TIMESTAMP_WINDOW.Seconds()) {
isCK = false
return
6 years ago
}
5 years ago
sessionID = binary.BigEndian.Uint32(plaintext[37:41])
6 years ago
5 years ago
isCK = true
return
6 years ago
}