@cryptotaxi247 / kubo / commits / b249ebe84

fix(identify) error handling

* handle subroutine errors * further specify the type of channel

Brian Tiger Chow committed Sep 12, 2014 at 22:40 UTC b249ebe84e5a929e2b399944fde327b3445d8588
1 file changed +27 -7
identify/identify.go
+27 -7
@@ -33,11 +33,14 @@ var ErrUnsupportedKeyType = errors.New("unsupported key type")
33
34 // Performs initial communication with this peer to share node ID's and
35 // initiate communication. (secureIn, secureOut, error)
36 -func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan []byte, error) {
36 +func Handshake(self, remote *peer.Peer, in <-chan []byte, out chan<- []byte) (<-chan []byte, chan<- []byte, error) {
37 // Generate and send Hello packet.
38 // Hello = (rand, PublicKey, Supported)
39 nonce := make([]byte, 16)
40 - rand.Read(nonce)
40 + _, err := rand.Read(nonce)
41 + if err != nil {
42 + return nil, nil, err
43 + }
44
45 hello := new(Hello)
46
@@ -95,6 +98,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
98 }
99
100 epubkey, done, err := ci.GenerateEKeyPair(exchange) // Generate EphemeralPubKey
101 + if err != nil {
102 + return nil, nil, err
103 + }
104
105 var handshake bytes.Buffer // Gather corpus to sign.
106 handshake.Write(encoded)
@@ -110,6 +116,9 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
116 }
117
118 exEncoded, err := proto.Marshal(exPacket)
119 + if err != nil {
120 + return nil, nil, err
121 + }
122
123 out <- exEncoded
124
@@ -124,9 +133,18 @@ func Handshake(self, remote *peer.Peer, in, out chan []byte) (chan []byte, chan
133 }
134
135 var theirHandshake bytes.Buffer
127 - theirHandshake.Write(resp)
128 - theirHandshake.Write(encoded)
129 - theirHandshake.Write(exchangeResp.GetEpubkey())
136 + _, err = theirHandshake.Write(resp)
137 + if err != nil {
138 + return nil, nil, err
139 + }
140 + _, err = theirHandshake.Write(encoded)
141 + if err != nil {
142 + return nil, nil, err
143 + }
144 + _, err = theirHandshake.Write(exchangeResp.GetEpubkey())
145 + if err != nil {
146 + return nil, nil, err
147 + }
148
149 ok, err := remote.PubKey.Verify(theirHandshake.Bytes(), exchangeResp.GetSignature())
150 if err != nil {
@@ -176,7 +194,7 @@ func makeMac(hashType string, key []byte) (hash.Hash, int) {
194 }
195 }
196
179 -func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey []byte) {
197 +func secureInProxy(in <-chan []byte, secureIn chan<- []byte, hashType string, tIV, tCKey, tMKey []byte) {
198 theirBlock, _ := aes.NewCipher(tCKey)
199 theirCipher := cipher.NewCTR(theirBlock, tIV)
200
@@ -185,6 +203,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey
203 for {
204 data, ok := <-in
205 if !ok {
206 + close(secureIn)
207 return
208 }
209
@@ -211,7 +230,7 @@ func secureInProxy(in, secureIn chan []byte, hashType string, tIV, tCKey, tMKey
230 }
231 }
232
214 -func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMKey []byte) {
233 +func secureOutProxy(out chan<- []byte, secureOut <-chan []byte, hashType string, mIV, mCKey, mMKey []byte) {
234 myBlock, _ := aes.NewCipher(mCKey)
235 myCipher := cipher.NewCTR(myBlock, mIV)
236
@@ -220,6 +239,7 @@ func secureOutProxy(out, secureOut chan []byte, hashType string, mIV, mCKey, mMK
239 for {
240 data, ok := <-secureOut
241 if !ok {
242 + close(out)
243 return
244 }
245