Cloak/internal/server/auth.go

74 lines
1.9 KiB
Go
Raw Normal View History

2018-10-09 15:07:54 +00:00
package server
import (
"bytes"
2019-08-16 22:56:46 +00:00
"crypto"
2018-10-09 15:07:54 +00:00
"encoding/binary"
2019-08-02 14:45:33 +00:00
"errors"
"fmt"
"github.com/cbeuw/Cloak/internal/ecdh"
2018-10-14 19:32:54 +00:00
"github.com/cbeuw/Cloak/internal/util"
"time"
2018-10-09 15:07:54 +00:00
)
2019-08-12 13:21:42 +00:00
type ClientInfo struct {
UID []byte
SessionId uint32
ProxyMethod string
EncryptionMethod byte
2019-08-12 21:43:16 +00:00
Unordered bool
2019-08-12 13:21:42 +00:00
}
2019-08-12 21:43:16 +00:00
const (
UNORDERED_FLAG = 0x01 // 0000 0001
)
2019-08-02 14:45:33 +00:00
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")
2019-08-16 22:56:46 +00:00
func touchStone(ch *ClientHello, staticPv crypto.PrivateKey, now func() time.Time) (info ClientInfo, sharedSecret []byte, err error) {
2019-08-03 10:49:05 +00:00
ephPub, ok := ecdh.Unmarshal(ch.random)
2019-08-02 00:01:19 +00:00
if !ok {
2019-08-02 14:45:33 +00:00
err = ErrInvalidPubKey
2019-08-02 00:01:19 +00:00
return
}
2019-08-16 22:56:46 +00:00
sharedSecret = ecdh.GenerateSharedSecret(staticPv, ephPub)
2019-08-02 14:45:33 +00:00
var keyShare []byte
keyShare, err = parseKeyShare(ch.extensions[[2]byte{0x00, 0x33}])
2019-08-02 00:01:19 +00:00
if err != nil {
2019-06-09 11:05:41 +00:00
return
2018-10-20 20:41:01 +00:00
}
2019-08-02 14:45:33 +00:00
ciphertext := append(ch.sessionId, keyShare...)
2019-08-02 00:01:19 +00:00
if len(ciphertext) != 64 {
2019-08-02 14:45:33 +00:00
err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(ciphertext))
2019-08-02 00:01:19 +00:00
return
}
2019-08-02 14:45:33 +00:00
var plaintext []byte
2019-08-03 10:49:05 +00:00
plaintext, err = util.AESGCMDecrypt(ch.random[0:12], sharedSecret, ciphertext)
2019-08-02 00:01:19 +00:00
if err != nil {
2019-06-09 14:03:28 +00:00
return
}
2019-08-02 00:01:19 +00:00
2019-08-14 10:48:32 +00:00
info = ClientInfo{
2019-08-12 13:21:42 +00:00
UID: plaintext[0:16],
SessionId: 0,
ProxyMethod: string(bytes.Trim(plaintext[16:28], "\x00")),
EncryptionMethod: plaintext[28],
2019-08-12 21:43:16 +00:00
Unordered: plaintext[41]&UNORDERED_FLAG != 0,
2019-08-12 13:21:42 +00:00
}
2019-08-02 00:01:19 +00:00
timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37]))
clientTime := time.Unix(timestamp, 0)
2019-08-16 22:56:46 +00:00
serverTime := now()
if !(clientTime.After(serverTime.Truncate(TIMESTAMP_TOLERANCE)) && clientTime.Before(serverTime.Add(TIMESTAMP_TOLERANCE))) {
2019-08-02 14:45:33 +00:00
err = fmt.Errorf("%v: received timestamp %v", ErrTimestampOutOfWindow, timestamp)
2019-06-09 11:05:41 +00:00
return
2018-10-09 15:07:54 +00:00
}
2019-08-12 13:21:42 +00:00
info.SessionId = binary.BigEndian.Uint32(plaintext[37:41])
2018-11-07 21:16:13 +00:00
return
2018-10-09 15:07:54 +00:00
}