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")
// Performs initial communication with this peer to share node ID's and
// 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.
// Hello = (rand, PublicKey, Supported)
nonce := make([]byte, 16)
rand.Read(nonce)
_, err := rand.Read(nonce)
if err != nil {
return nil, nil, err
}
hello := new(Hello)
......@@ -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
if err != nil {
return nil, nil, err
}
var handshake bytes.Buffer // Gather corpus to sign.
handshake.Write(encoded)
......@@ -110,6 +116,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
}
exEncoded, err := proto.Marshal(exPacket)
if err != nil {
return nil, nil, err
}
out <- exEncoded
......@@ -124,9 +133,18 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
}
var theirHandshake bytes.Buffer
theirHandshake.Write(resp)
theirHandshake.Write(encoded)
theirHandshake.Write(exchangeResp.GetEpubkey())
_, err = theirHandshake.Write(resp)
if err != nil {
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())
if err != nil {
......@@ -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)
theirCipher := cipher.NewCTR(theirBlock, tIV)
......@@ -185,6 +203,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey
for {
data, ok := <-in
if !ok {
close(secureIn)
return
}
......@@ -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)
myCipher := cipher.NewCTR(myBlock, mIV)
......@@ -220,6 +239,7 @@ func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMK
for {
data, ok := <-secureOut
if !ok {
close(out)
return
}
......
......@@ -25,8 +25,8 @@ type Conn struct {
Closed chan bool
Outgoing *msgio.Chan
Incoming *msgio.Chan
secIn chan []byte
secOut chan []byte
secIn <-chan []byte
secOut chan<- []byte
}
// 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