mirror of https://github.com/cbeuw/Cloak
Make authentication info arrays
This commit is contained in:
parent
fe8b2d78ef
commit
fa1c109d90
|
|
@ -36,7 +36,7 @@ func (TLS) handshake(clientHello []byte, privateKey crypto.PrivateKey, originalC
|
||||||
|
|
||||||
finisher = func(sessionKey []byte) (preparedConn net.Conn, err error) {
|
finisher = func(sessionKey []byte) (preparedConn net.Conn, err error) {
|
||||||
preparedConn = originalConn
|
preparedConn = originalConn
|
||||||
reply, err := composeReply(ch, ai.sharedSecret, sessionKey)
|
reply, err := composeReply(ch, ai.sharedSecret[:], sessionKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to compose TLS reply: %v", err)
|
err = fmt.Errorf("failed to compose TLS reply: %v", err)
|
||||||
return
|
return
|
||||||
|
|
@ -54,24 +54,25 @@ func (TLS) handshake(clientHello []byte, privateKey crypto.PrivateKey, originalC
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalClientHello(ch *ClientHello, staticPv crypto.PrivateKey) (ai authenticationInfo, err error) {
|
func unmarshalClientHello(ch *ClientHello, staticPv crypto.PrivateKey) (ai authenticationInfo, err error) {
|
||||||
ai.randPubKey = ch.random
|
copy(ai.randPubKey[:], ch.random)
|
||||||
ephPub, ok := ecdh.Unmarshal(ai.randPubKey)
|
ephPub, ok := ecdh.Unmarshal(ai.randPubKey[:])
|
||||||
if !ok {
|
if !ok {
|
||||||
err = ErrInvalidPubKey
|
err = ErrInvalidPubKey
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ai.sharedSecret = ecdh.GenerateSharedSecret(staticPv, ephPub)
|
copy(ai.sharedSecret[:], ecdh.GenerateSharedSecret(staticPv, ephPub))
|
||||||
var keyShare []byte
|
var keyShare []byte
|
||||||
keyShare, err = parseKeyShare(ch.extensions[[2]byte{0x00, 0x33}])
|
keyShare, err = parseKeyShare(ch.extensions[[2]byte{0x00, 0x33}])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ai.ciphertextWithTag = append(ch.sessionId, keyShare...)
|
ctxTag := append(ch.sessionId, keyShare...)
|
||||||
if len(ai.ciphertextWithTag) != 64 {
|
if len(ctxTag) != 64 {
|
||||||
err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(ai.ciphertextWithTag))
|
err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(ctxTag))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
copy(ai.ciphertextWithTag[:], ctxTag)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ type ClientInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type authenticationInfo struct {
|
type authenticationInfo struct {
|
||||||
sharedSecret []byte
|
sharedSecret [32]byte
|
||||||
randPubKey []byte
|
randPubKey [32]byte
|
||||||
ciphertextWithTag []byte
|
ciphertextWithTag [64]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -37,7 +37,7 @@ var ErrUnreconisedProtocol = errors.New("unreconised protocol")
|
||||||
// touchStone checks if a the authenticationInfo are valid. It doesn't check if the UID is authorised
|
// touchStone checks if a the authenticationInfo are valid. It doesn't check if the UID is authorised
|
||||||
func touchStone(ai authenticationInfo, now func() time.Time) (info ClientInfo, err error) {
|
func touchStone(ai authenticationInfo, now func() time.Time) (info ClientInfo, err error) {
|
||||||
var plaintext []byte
|
var plaintext []byte
|
||||||
plaintext, err = util.AESGCMDecrypt(ai.randPubKey[0:12], ai.sharedSecret, ai.ciphertextWithTag)
|
plaintext, err = util.AESGCMDecrypt(ai.randPubKey[0:12], ai.sharedSecret[:], ai.ciphertextWithTag[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -232,12 +232,10 @@ func (sta *State) UsedRandomCleaner() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sta *State) registerRandom(r []byte) bool {
|
func (sta *State) registerRandom(r [32]byte) bool {
|
||||||
var random [32]byte
|
|
||||||
copy(random[:], r)
|
|
||||||
sta.usedRandomM.Lock()
|
sta.usedRandomM.Lock()
|
||||||
_, used := sta.usedRandom[random]
|
_, used := sta.usedRandom[r]
|
||||||
sta.usedRandom[random] = sta.Now().Unix()
|
sta.usedRandom[r] = sta.Now().Unix()
|
||||||
sta.usedRandomM.Unlock()
|
sta.usedRandomM.Unlock()
|
||||||
return used
|
return used
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ func (WebSocket) handshake(reqPacket []byte, privateKey crypto.PrivateKey, origi
|
||||||
rand.Read(nonce)
|
rand.Read(nonce)
|
||||||
|
|
||||||
// reply: [12 bytes nonce][32 bytes encrypted session key][16 bytes authentication tag]
|
// reply: [12 bytes nonce][32 bytes encrypted session key][16 bytes authentication tag]
|
||||||
encryptedKey, err := util.AESGCMEncrypt(nonce, ai.sharedSecret, sessionKey) // 32 + 16 = 48 bytes
|
encryptedKey, err := util.AESGCMEncrypt(nonce, ai.sharedSecret[:], sessionKey) // 32 + 16 = 48 bytes
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to encrypt reply: %v", err)
|
err = fmt.Errorf("failed to encrypt reply: %v", err)
|
||||||
return
|
return
|
||||||
|
|
@ -74,19 +74,20 @@ func unmarshalHidden(hidden []byte, staticPv crypto.PrivateKey) (ai authenticati
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ai.randPubKey = hidden[0:32]
|
copy(ai.randPubKey[:], hidden[0:32])
|
||||||
ephPub, ok := ecdh.Unmarshal(ai.randPubKey)
|
ephPub, ok := ecdh.Unmarshal(ai.randPubKey[:])
|
||||||
if !ok {
|
if !ok {
|
||||||
err = ErrInvalidPubKey
|
err = ErrInvalidPubKey
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ai.sharedSecret = ecdh.GenerateSharedSecret(staticPv, ephPub)
|
copy(ai.sharedSecret[:], ecdh.GenerateSharedSecret(staticPv, ephPub))
|
||||||
|
|
||||||
ai.ciphertextWithTag = hidden[32:]
|
if len(hidden[32:]) != 64 {
|
||||||
if len(ai.ciphertextWithTag) != 64 {
|
err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(hidden[32:]))
|
||||||
err = fmt.Errorf("%v: %v", ErrCiphertextLength, len(ai.ciphertextWithTag))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
copy(ai.ciphertextWithTag[:], hidden[32:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue