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

60 lines
1.6 KiB
Go

6 years ago
package server
import (
"bytes"
"encoding/binary"
5 years ago
"errors"
"fmt"
"github.com/cbeuw/Cloak/internal/ecdh"
"github.com/cbeuw/Cloak/internal/util"
6 years ago
)
5 years ago
var ErrReplay = errors.New("duplicate random")
var ErrInvalidPubKey = errors.New("public key has invalid format")
var ErrCiphertextLength = errors.New("ciphertext has the wrong length")
var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window")
func TouchStone(ch *ClientHello, sta *State) (UID []byte, sessionID uint32, proxyMethod string, encryptionMethod byte, sharedSecret []byte, err error) {
if sta.registerRandom(ch.random) {
5 years ago
err = ErrReplay
return
6 years ago
}
ephPub, ok := ecdh.Unmarshal(ch.random)
5 years ago
if !ok {
5 years ago
err = ErrInvalidPubKey
5 years ago
return
}
5 years ago
sharedSecret = ecdh.GenerateSharedSecret(sta.staticPv, ephPub)
5 years ago
var keyShare []byte
keyShare, err = parseKeyShare(ch.extensions[[2]byte{0x00, 0x33}])
5 years ago
if err != nil {
return
}
5 years ago
ciphertext := append(ch.sessionId, keyShare...)
5 years ago
if len(ciphertext) != 64 {
5 years ago
err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(ciphertext))
5 years ago
return
}
5 years ago
var plaintext []byte
plaintext, err = util.AESGCMDecrypt(ch.random[0:12], sharedSecret, ciphertext)
5 years ago
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()) {
5 years ago
err = fmt.Errorf("%v: received timestamp %v", ErrTimestampOutOfWindow, timestamp)
return
6 years ago
}
5 years ago
sessionID = binary.BigEndian.Uint32(plaintext[37:41])
return
6 years ago
}