Skip to content

Commit

Permalink
Merge pull request #63 from jbenet/fix/identify-test_handshake-issue-61
Browse files Browse the repository at this point in the history
fix(identify) Handshake
  • Loading branch information
Brian Tiger Chow committed Sep 13, 2014
2 parents 5a41a2a + 24b7703 commit 9c3e8d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
34 changes: 27 additions & 7 deletions identify/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -185,6 +203,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey
for {
data, ok := <-in
if !ok {
close(secureIn)
return
}

Expand All @@ -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)

Expand All @@ -220,6 +239,7 @@ func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMK
for {
data, ok := <-secureOut
if !ok {
close(out)
return
}

Expand Down
4 changes: 2 additions & 2 deletions swarm/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9c3e8d7

Please sign in to comment.