forked from mirror/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
6.1 KiB
225 lines
6.1 KiB
10 years ago
|
package p2p
|
||
|
|
||
|
import (
|
||
10 years ago
|
"bytes"
|
||
10 years ago
|
"crypto/ecdsa"
|
||
10 years ago
|
"crypto/rand"
|
||
10 years ago
|
"net"
|
||
10 years ago
|
"reflect"
|
||
10 years ago
|
"testing"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/crypto"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
func TestPublicKeyEncoding(t *testing.T) {
|
||
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
||
|
pub0 := &prv0.PublicKey
|
||
|
pub0s := crypto.FromECDSAPub(pub0)
|
||
10 years ago
|
pub1, err := importPublicKey(pub0s)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("%v", err)
|
||
|
}
|
||
|
eciesPub1 := ecies.ImportECDSAPublic(pub1)
|
||
|
if eciesPub1 == nil {
|
||
|
t.Errorf("invalid ecdsa public key")
|
||
|
}
|
||
10 years ago
|
pub1s, err := exportPublicKey(pub1)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("%v", err)
|
||
|
}
|
||
|
if len(pub1s) != 64 {
|
||
|
t.Errorf("wrong length expect 64, got", len(pub1s))
|
||
|
}
|
||
10 years ago
|
pub2, err := importPublicKey(pub1s)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("%v", err)
|
||
|
}
|
||
10 years ago
|
pub2s, err := exportPublicKey(pub2)
|
||
10 years ago
|
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 :(")
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func TestCryptoHandshake(t *testing.T) {
|
||
10 years ago
|
testCryptoHandshake(newkey(), newkey(), nil, t)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func TestCryptoHandshakeWithToken(t *testing.T) {
|
||
|
sessionToken := make([]byte, shaLen)
|
||
|
rand.Read(sessionToken)
|
||
|
testCryptoHandshake(newkey(), newkey(), sessionToken, t)
|
||
10 years ago
|
}
|
||
|
|
||
|
func testCryptoHandshake(prv0, prv1 *ecdsa.PrivateKey, sessionToken []byte, t *testing.T) {
|
||
10 years ago
|
var err error
|
||
10 years ago
|
// pub0 := &prv0.PublicKey
|
||
10 years ago
|
pub1 := &prv1.PublicKey
|
||
10 years ago
|
|
||
10 years ago
|
// pub0s := crypto.FromECDSAPub(pub0)
|
||
10 years ago
|
pub1s := crypto.FromECDSAPub(pub1)
|
||
10 years ago
|
|
||
10 years ago
|
// simulate handshake by feeding output to input
|
||
10 years ago
|
// initiator sends handshake 'auth'
|
||
10 years ago
|
auth, initNonce, randomPrivKey, err := authMsg(prv0, pub1s, sessionToken)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("%v", err)
|
||
|
}
|
||
10 years ago
|
// t.Logf("-> %v", hexkey(auth))
|
||
10 years ago
|
|
||
10 years ago
|
// receiver reads auth and responds with response
|
||
10 years ago
|
response, remoteRecNonce, remoteInitNonce, _, remoteRandomPrivKey, remoteInitRandomPubKey, err := authResp(auth, sessionToken, prv1)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("%v", err)
|
||
|
}
|
||
10 years ago
|
// t.Logf("<- %v\n", hexkey(response))
|
||
10 years ago
|
|
||
10 years ago
|
// initiator reads receiver's response and the key exchange completes
|
||
10 years ago
|
recNonce, remoteRandomPubKey, _, err := completeHandshake(response, prv0)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
t.Errorf("completeHandshake error: %v", err)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// now both parties should have the same session parameters
|
||
10 years ago
|
initSessionToken, err := newSession(initNonce, recNonce, randomPrivKey, remoteRandomPubKey)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
t.Errorf("newSession error: %v", err)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
recSessionToken, err := newSession(remoteInitNonce, remoteRecNonce, remoteRandomPrivKey, remoteInitRandomPubKey)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
t.Errorf("newSession error: %v", err)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// fmt.Printf("\nauth (%v) %x\n\nresp (%v) %x\n\n", len(auth), auth, len(response), response)
|
||
10 years ago
|
|
||
|
// 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)
|
||
10 years ago
|
|
||
10 years ago
|
if !bytes.Equal(initNonce, remoteInitNonce) {
|
||
|
t.Errorf("nonces do not match")
|
||
|
}
|
||
|
if !bytes.Equal(recNonce, remoteRecNonce) {
|
||
|
t.Errorf("receiver nonces do not match")
|
||
|
}
|
||
10 years ago
|
if !bytes.Equal(initSessionToken, recSessionToken) {
|
||
10 years ago
|
t.Errorf("session tokens do not match")
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
func TestEncHandshake(t *testing.T) {
|
||
10 years ago
|
defer testlog(t).detach()
|
||
|
|
||
10 years ago
|
prv0, _ := crypto.GenerateKey()
|
||
|
prv1, _ := crypto.GenerateKey()
|
||
|
pub0s, _ := exportPublicKey(&prv0.PublicKey)
|
||
|
pub1s, _ := exportPublicKey(&prv1.PublicKey)
|
||
|
rw0, rw1 := net.Pipe()
|
||
|
tokens := make(chan []byte)
|
||
10 years ago
|
|
||
|
go func() {
|
||
10 years ago
|
token, err := outboundEncHandshake(rw0, prv0, pub1s, nil)
|
||
|
if err != nil {
|
||
|
t.Errorf("outbound side error: %v", err)
|
||
|
}
|
||
|
tokens <- token
|
||
10 years ago
|
}()
|
||
|
go func() {
|
||
10 years ago
|
token, remotePubkey, err := inboundEncHandshake(rw1, prv1, nil)
|
||
|
if err != nil {
|
||
|
t.Errorf("inbound side error: %v", err)
|
||
|
}
|
||
|
if !bytes.Equal(remotePubkey, pub0s) {
|
||
|
t.Errorf("inbound side returned wrong remote pubkey\n got: %x\n want: %x", remotePubkey, pub0s)
|
||
|
}
|
||
|
tokens <- token
|
||
10 years ago
|
}()
|
||
10 years ago
|
|
||
|
t1, t2 := <-tokens, <-tokens
|
||
|
if !bytes.Equal(t1, t2) {
|
||
|
t.Error("session token mismatch")
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
func TestSetupConn(t *testing.T) {
|
||
|
prv0, _ := crypto.GenerateKey()
|
||
|
prv1, _ := crypto.GenerateKey()
|
||
|
node0 := &discover.Node{
|
||
|
ID: discover.PubkeyID(&prv0.PublicKey),
|
||
|
IP: net.IP{1, 2, 3, 4},
|
||
|
TCPPort: 33,
|
||
|
}
|
||
|
node1 := &discover.Node{
|
||
|
ID: discover.PubkeyID(&prv1.PublicKey),
|
||
|
IP: net.IP{5, 6, 7, 8},
|
||
|
TCPPort: 44,
|
||
|
}
|
||
|
hs0 := &protoHandshake{
|
||
|
Version: baseProtocolVersion,
|
||
|
ID: node0.ID,
|
||
|
Caps: []Cap{{"a", 0}, {"b", 2}},
|
||
|
}
|
||
|
hs1 := &protoHandshake{
|
||
|
Version: baseProtocolVersion,
|
||
|
ID: node1.ID,
|
||
|
Caps: []Cap{{"c", 1}, {"d", 3}},
|
||
|
}
|
||
|
fd0, fd1 := net.Pipe()
|
||
|
|
||
|
done := make(chan struct{})
|
||
|
go func() {
|
||
|
defer close(done)
|
||
|
conn0, err := setupConn(fd0, prv0, hs0, node1)
|
||
|
if err != nil {
|
||
|
t.Errorf("outbound side error: %v", err)
|
||
|
return
|
||
|
}
|
||
|
if conn0.ID != node1.ID {
|
||
|
t.Errorf("outbound conn id mismatch: got %v, want %v", conn0.ID, node1.ID)
|
||
|
}
|
||
|
if !reflect.DeepEqual(conn0.Caps, hs1.Caps) {
|
||
|
t.Errorf("outbound caps mismatch: got %v, want %v", conn0.Caps, hs1.Caps)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
conn1, err := setupConn(fd1, prv1, hs1, nil)
|
||
|
if err != nil {
|
||
|
t.Fatalf("inbound side error: %v", err)
|
||
|
}
|
||
|
if conn1.ID != node0.ID {
|
||
|
t.Errorf("inbound conn id mismatch: got %v, want %v", conn1.ID, node0.ID)
|
||
|
}
|
||
|
if !reflect.DeepEqual(conn1.Caps, hs0.Caps) {
|
||
|
t.Errorf("inbound caps mismatch: got %v, want %v", conn1.Caps, hs0.Caps)
|
||
|
}
|
||
|
|
||
|
<-done
|
||
|
}
|