2015-01-19 03:24:09 +02:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-01-19 06:53:48 +02:00
|
|
|
"bytes"
|
2015-01-29 05:16:10 +02:00
|
|
|
"crypto/ecdsa"
|
2015-01-19 03:24:09 +02:00
|
|
|
"fmt"
|
2015-01-21 16:42:12 +02:00
|
|
|
"net"
|
2015-01-19 03:24:09 +02:00
|
|
|
"testing"
|
2015-01-21 18:22:49 +02:00
|
|
|
"time"
|
2015-01-19 03:24:09 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-01-20 18:47:46 +02:00
|
|
|
"github.com/obscuren/ecies"
|
2015-01-19 03:24:09 +02:00
|
|
|
)
|
|
|
|
|
2015-01-20 18:47:46 +02:00
|
|
|
func TestPublicKeyEncoding(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
pub0s := crypto.FromECDSAPub(pub0)
|
|
|
|
pub1, err := ImportPublicKey(pub0s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
eciesPub1 := ecies.ImportECDSAPublic(pub1)
|
|
|
|
if eciesPub1 == nil {
|
|
|
|
t.Errorf("invalid ecdsa public key")
|
|
|
|
}
|
|
|
|
pub1s, err := ExportPublicKey(pub1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if len(pub1s) != 64 {
|
|
|
|
t.Errorf("wrong length expect 64, got", len(pub1s))
|
|
|
|
}
|
|
|
|
pub2, err := ImportPublicKey(pub1s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
pub2s, err := ExportPublicKey(pub2)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(pub1s, pub2s) {
|
|
|
|
t.Errorf("exports dont match")
|
|
|
|
}
|
|
|
|
pub2sEC := crypto.FromECDSAPub(pub2)
|
|
|
|
if !bytes.Equal(pub0s, pub2sEC) {
|
|
|
|
t.Errorf("exports dont match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSharedSecret(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
pub1 := &prv1.PublicKey
|
|
|
|
|
|
|
|
ss0, err := ecies.ImportECDSA(prv0).GenerateShared(ecies.ImportECDSAPublic(pub1), sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ss1, err := ecies.ImportECDSA(prv1).GenerateShared(ecies.ImportECDSAPublic(pub0), sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("Secret:\n%v %x\n%v %x", len(ss0), ss0, len(ss0), ss1)
|
|
|
|
if !bytes.Equal(ss0, ss1) {
|
|
|
|
t.Errorf("dont match :(")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 03:24:09 +02:00
|
|
|
func TestCryptoHandshake(t *testing.T) {
|
2015-01-29 05:16:10 +02:00
|
|
|
testCryptoHandshakeWithGen(false, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenCryptoHandshake(t *testing.T) {
|
|
|
|
testCryptoHandshakeWithGen(true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetCryptoHandshake(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
tmpkeyF := keyF
|
|
|
|
keyF = detkeyF
|
|
|
|
tmpnonceF := nonceF
|
|
|
|
nonceF = detnonceF
|
|
|
|
testCryptoHandshakeWithGen(false, t)
|
|
|
|
keyF = tmpkeyF
|
|
|
|
nonceF = tmpnonceF
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetTokenCryptoHandshake(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
tmpkeyF := keyF
|
|
|
|
keyF = detkeyF
|
|
|
|
tmpnonceF := nonceF
|
|
|
|
nonceF = detnonceF
|
|
|
|
testCryptoHandshakeWithGen(true, t)
|
|
|
|
keyF = tmpkeyF
|
|
|
|
nonceF = tmpnonceF
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCryptoHandshakeWithGen(token bool, t *testing.T) {
|
|
|
|
fmt.Printf("init-private-key: ")
|
|
|
|
prv0, err := keyF()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("rec-private-key: ")
|
|
|
|
prv1, err := keyF()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var nonce []byte
|
|
|
|
if token {
|
|
|
|
fmt.Printf("session-token: ")
|
|
|
|
nonce = make([]byte, shaLen)
|
|
|
|
nonceF(nonce)
|
|
|
|
}
|
|
|
|
testCryptoHandshake(prv0, prv1, nonce, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCryptoHandshake(prv0, prv1 *ecdsa.PrivateKey, sessionToken []byte, t *testing.T) {
|
2015-01-19 03:24:09 +02:00
|
|
|
var err error
|
2015-01-20 01:42:13 +02:00
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
pub1 := &prv1.PublicKey
|
2015-01-19 03:24:09 +02:00
|
|
|
|
2015-01-26 18:16:23 +02:00
|
|
|
pub0s := crypto.FromECDSAPub(pub0)
|
|
|
|
pub1s := crypto.FromECDSAPub(pub1)
|
2015-01-19 03:24:09 +02:00
|
|
|
|
2015-01-20 01:42:13 +02:00
|
|
|
// simulate handshake by feeding output to input
|
2015-01-20 02:23:10 +02:00
|
|
|
// initiator sends handshake 'auth'
|
2015-01-26 18:16:23 +02:00
|
|
|
auth, initNonce, randomPrivKey, _, err := startHandshake(prv0, pub1s, sessionToken)
|
2015-01-20 18:47:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-01-29 05:16:10 +02:00
|
|
|
fmt.Printf("-> %v\n", hexkey(auth))
|
2015-01-20 18:47:46 +02:00
|
|
|
|
2015-01-20 02:23:10 +02:00
|
|
|
// receiver reads auth and responds with response
|
2015-01-26 18:16:23 +02:00
|
|
|
response, remoteRecNonce, remoteInitNonce, remoteRandomPrivKey, remoteInitRandomPubKey, err := respondToHandshake(auth, prv1, pub0s, sessionToken)
|
2015-01-20 18:47:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-01-29 05:16:10 +02:00
|
|
|
fmt.Printf("<- %v\n", hexkey(response))
|
2015-01-20 18:47:46 +02:00
|
|
|
|
2015-01-20 02:23:10 +02:00
|
|
|
// initiator reads receiver's response and the key exchange completes
|
2015-01-26 18:16:23 +02:00
|
|
|
recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prv0)
|
2015-01-20 18:47:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-01-19 06:53:48 +02:00
|
|
|
|
2015-01-20 02:23:10 +02:00
|
|
|
// now both parties should have the same session parameters
|
2015-01-26 18:16:23 +02:00
|
|
|
initSessionToken, initSecretRW, err := newSession(true, initNonce, recNonce, auth, randomPrivKey, remoteRandomPubKey)
|
2015-01-20 18:47:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:16:23 +02:00
|
|
|
recSessionToken, recSecretRW, err := newSession(false, remoteInitNonce, remoteRecNonce, auth, remoteRandomPrivKey, remoteInitRandomPubKey)
|
2015-01-20 18:47:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-01-19 06:53:48 +02:00
|
|
|
|
2015-01-29 05:16:10 +02:00
|
|
|
// fmt.Printf("\nauth (%v) %x\n\nresp (%v) %x\n\n", len(auth), auth, len(response), response)
|
2015-01-21 16:42:12 +02:00
|
|
|
|
|
|
|
// fmt.Printf("\nauth %x\ninitNonce %x\nresponse%x\nremoteRecNonce %x\nremoteInitNonce %x\nremoteRandomPubKey %x\nrecNonce %x\nremoteInitRandomPubKey %x\ninitSessionToken %x\n\n", auth, initNonce, response, remoteRecNonce, remoteInitNonce, remoteRandomPubKey, recNonce, remoteInitRandomPubKey, initSessionToken)
|
2015-01-19 06:53:48 +02:00
|
|
|
|
2015-01-20 02:41:45 +02:00
|
|
|
if !bytes.Equal(initNonce, remoteInitNonce) {
|
|
|
|
t.Errorf("nonces do not match")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(recNonce, remoteRecNonce) {
|
|
|
|
t.Errorf("receiver nonces do not match")
|
|
|
|
}
|
2015-01-20 01:42:13 +02:00
|
|
|
if !bytes.Equal(initSessionToken, recSessionToken) {
|
2015-01-19 06:53:48 +02:00
|
|
|
t.Errorf("session tokens do not match")
|
|
|
|
}
|
|
|
|
// aesSecret, macSecret, egressMac, ingressMac
|
2015-01-20 01:42:13 +02:00
|
|
|
if !bytes.Equal(initSecretRW.aesSecret, recSecretRW.aesSecret) {
|
2015-01-19 06:53:48 +02:00
|
|
|
t.Errorf("AES secrets do not match")
|
|
|
|
}
|
2015-01-20 01:42:13 +02:00
|
|
|
if !bytes.Equal(initSecretRW.macSecret, recSecretRW.macSecret) {
|
2015-01-19 06:53:48 +02:00
|
|
|
t.Errorf("macSecrets do not match")
|
|
|
|
}
|
2015-01-26 16:50:12 +02:00
|
|
|
if !bytes.Equal(initSecretRW.egressMac, recSecretRW.ingressMac) {
|
|
|
|
t.Errorf("initiator's egressMac do not match receiver's ingressMac")
|
2015-01-19 06:53:48 +02:00
|
|
|
}
|
2015-01-26 16:50:12 +02:00
|
|
|
if !bytes.Equal(initSecretRW.ingressMac, recSecretRW.egressMac) {
|
|
|
|
t.Errorf("initiator's inressMac do not match receiver's egressMac")
|
2015-01-19 06:53:48 +02:00
|
|
|
}
|
2015-01-19 03:24:09 +02:00
|
|
|
|
|
|
|
}
|
2015-01-21 16:42:12 +02:00
|
|
|
|
|
|
|
func TestPeersHandshake(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
var err error
|
|
|
|
// var sessionToken []byte
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
pub1 := &prv1.PublicKey
|
|
|
|
|
|
|
|
prv0s := crypto.FromECDSA(prv0)
|
|
|
|
pub0s := crypto.FromECDSAPub(pub0)
|
|
|
|
prv1s := crypto.FromECDSA(prv1)
|
|
|
|
pub1s := crypto.FromECDSAPub(pub1)
|
|
|
|
|
|
|
|
conn1, conn2 := net.Pipe()
|
|
|
|
initiator := newPeer(conn1, []Protocol{}, nil)
|
|
|
|
receiver := newPeer(conn2, []Protocol{}, nil)
|
|
|
|
initiator.dialAddr = &peerAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222, Pubkey: pub1s[1:]}
|
2015-01-26 18:16:23 +02:00
|
|
|
initiator.privateKey = prv0s
|
2015-01-21 16:42:12 +02:00
|
|
|
|
|
|
|
// this is cheating. identity of initiator/dialler not available to listener/receiver
|
|
|
|
// its public key should be looked up based on IP address
|
2015-01-26 18:16:23 +02:00
|
|
|
receiver.identity = &peerId{nil, pub0s}
|
|
|
|
receiver.privateKey = prv1s
|
2015-01-21 16:42:12 +02:00
|
|
|
|
|
|
|
initiator.pubkeyHook = func(*peerAddr) error { return nil }
|
|
|
|
receiver.pubkeyHook = func(*peerAddr) error { return nil }
|
|
|
|
|
|
|
|
initiator.cryptoHandshake = true
|
|
|
|
receiver.cryptoHandshake = true
|
|
|
|
errc0 := make(chan error, 1)
|
|
|
|
errc1 := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
_, err := initiator.loop()
|
|
|
|
errc0 <- err
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
_, err := receiver.loop()
|
|
|
|
errc1 <- err
|
|
|
|
}()
|
2015-01-21 18:22:49 +02:00
|
|
|
ready := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
<-initiator.cryptoReady
|
|
|
|
<-receiver.cryptoReady
|
|
|
|
close(ready)
|
|
|
|
}()
|
2015-01-26 16:50:12 +02:00
|
|
|
timeout := time.After(10 * time.Second)
|
2015-01-21 16:42:12 +02:00
|
|
|
select {
|
2015-01-21 18:22:49 +02:00
|
|
|
case <-ready:
|
|
|
|
case <-timeout:
|
|
|
|
t.Errorf("crypto handshake hanging for too long")
|
2015-01-21 16:42:12 +02:00
|
|
|
case err = <-errc0:
|
|
|
|
t.Errorf("peer 0 quit with error: %v", err)
|
|
|
|
case err = <-errc1:
|
|
|
|
t.Errorf("peer 1 quit with error: %v", err)
|
|
|
|
}
|
|
|
|
}
|