Commit 9c3e8d7b authored by Brian Tiger Chow's avatar Brian Tiger Chow

Merge pull request #63 from jbenet/fix/identify-test_handshake-issue-61

fix(identify) Handshake
parents 5a41a2ac 24b7703f
...@@ -33,11 +33,14 @@ var ErrUnsupportedKeyType = errors.New("unsupported key type") ...@@ -33,11 +33,14 @@ var ErrUnsupportedKeyType = errors.New("unsupported key type")
// Performs initial communication with this peer to share node ID's and // Performs initial communication with this peer to share node ID's and
// initiate communication. (secureIn, secureOut, error) // initiate communication. (secureIn, secureOut, error)
func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan []byte, error) { func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
// Generate and send Hello packet. // Generate and send Hello packet.
// Hello = (rand, PublicKey, Supported) // Hello = (rand, PublicKey, Supported)
nonce := make([]byte, 16) nonce := make([]byte, 16)
rand.Read(nonce) _, err := rand.Read(nonce)
if err != nil {
return nil, nil, err
}
hello := new(Hello) hello := new(Hello)
...@@ -95,6 +98,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan ...@@ -95,6 +98,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
} }
epubkey, done, err := ci.GenerateEKeyPair(exchange) // Generate EphemeralPubKey epubkey, done, err := ci.GenerateEKeyPair(exchange) // Generate EphemeralPubKey
if err != nil {
return nil, nil, err
}
var handshake bytes.Buffer // Gather corpus to sign. var handshake bytes.Buffer // Gather corpus to sign.
handshake.Write(encoded) handshake.Write(encoded)
...@@ -110,6 +116,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan ...@@ -110,6 +116,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
} }
exEncoded, err := proto.Marshal(exPacket) exEncoded, err := proto.Marshal(exPacket)
if err != nil {
return nil, nil, err
}
out <- exEncoded out <- exEncoded
...@@ -124,9 +133,18 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan ...@@ -124,9 +133,18 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
} }
var theirHandshake bytes.Buffer var theirHandshake bytes.Buffer
theirHandshake.Write(resp) _, err = theirHandshake.Write(resp)
theirHandshake.Write(encoded) if err != nil {
theirHandshake.Write(exchangeResp.GetEpubkey()) return nil, nil, err
}
_, err = theirHandshake.Write(encoded)
if err != nil {
return nil, nil, err
}
_, err = theirHandshake.Write(exchangeResp.GetEpubkey())
if err != nil {
return nil, nil, err
}
ok, err := remote.PubKey.Verify(theirHandshake.Bytes(), exchangeResp.GetSignature()) ok, err := remote.PubKey.Verify(theirHandshake.Bytes(), exchangeResp.GetSignature())
if err != nil { if err != nil {
...@@ -176,7 +194,7 @@ func makeMac(hashType string, key []byte) (hash.Hash, int) { ...@@ -176,7 +194,7 @@ func makeMac(hashType string, key []byte) (hash.Hash, int) {
} }
} }
func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey []byte) { func secureInProxy(in <-chan []byte, secureIn chan<- []byte, hashType string, tIV, tCKey, tMKey []byte) {
theirBlock, _ := aes.NewCipher(tCKey) theirBlock, _ := aes.NewCipher(tCKey)
theirCipher := cipher.NewCTR(theirBlock, tIV) theirCipher := cipher.NewCTR(theirBlock, tIV)
...@@ -185,6 +203,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey ...@@ -185,6 +203,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey
for { for {
data, ok := <-in data, ok := <-in
if !ok { if !ok {
close(secureIn)
return return
} }
...@@ -211,7 +230,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey ...@@ -211,7 +230,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey
} }
} }
func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMKey []byte) { func secureOutProxy(out chan<- []byte, secureOut <-chan []byte, hashType string, mIV, mCKey, mMKey []byte) {
myBlock, _ := aes.NewCipher(mCKey) myBlock, _ := aes.NewCipher(mCKey)
myCipher := cipher.NewCTR(myBlock, mIV) myCipher := cipher.NewCTR(myBlock, mIV)
...@@ -220,6 +239,7 @@ func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMK ...@@ -220,6 +239,7 @@ func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMK
for { for {
data, ok := <-secureOut data, ok := <-secureOut
if !ok { if !ok {
close(out)
return return
} }
......
...@@ -25,8 +25,8 @@ type Conn struct { ...@@ -25,8 +25,8 @@ type Conn struct {
Closed chan bool Closed chan bool
Outgoing *msgio.Chan Outgoing *msgio.Chan
Incoming *msgio.Chan Incoming *msgio.Chan
secIn chan []byte secIn <-chan []byte
secOut chan []byte secOut chan<- []byte
} }
// ConnMap maps Keys (Peer.IDs) to Connections. // ConnMap maps Keys (Peer.IDs) to Connections.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment