mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
p2p/discover: add some more code comments
This commit is contained in:
parent
e33008c1c1
commit
99e3ada571
2 changed files with 16 additions and 2 deletions
|
|
@ -321,10 +321,16 @@ func (c *wireCodec) encodeEncrypted(toID enode.ID, toAddr string, packet packetV
|
||||||
// encodeAuthHeader creates the auth header on a call packet following WHOAREYOU.
|
// encodeAuthHeader creates the auth header on a call packet following WHOAREYOU.
|
||||||
func (c *wireCodec) makeAuthHeader(nonce []byte, challenge *whoareyouV5) (*authHeaderList, *handshakeSecrets, error) {
|
func (c *wireCodec) makeAuthHeader(nonce []byte, challenge *whoareyouV5) (*authHeaderList, *handshakeSecrets, error) {
|
||||||
resp := &authResponse{Version: 5}
|
resp := &authResponse{Version: 5}
|
||||||
|
|
||||||
|
// Add our record to response if it's newer than what remote
|
||||||
|
// side has.
|
||||||
ln := c.localnode.Node()
|
ln := c.localnode.Node()
|
||||||
if challenge.RecordSeq < ln.Seq() {
|
if challenge.RecordSeq < ln.Seq() {
|
||||||
resp.Record = ln.Record()
|
resp.Record = ln.Record()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the ephemeral key. This needs to be first because the
|
||||||
|
// key is part of the ID nonce signature.
|
||||||
var remotePubkey = new(ecdsa.PublicKey)
|
var remotePubkey = new(ecdsa.PublicKey)
|
||||||
if err := challenge.node.Load((*enode.Secp256k1)(remotePubkey)); err != nil {
|
if err := challenge.node.Load((*enode.Secp256k1)(remotePubkey)); err != nil {
|
||||||
return nil, nil, fmt.Errorf("can't find secp256k1 key for recipient")
|
return nil, nil, fmt.Errorf("can't find secp256k1 key for recipient")
|
||||||
|
|
@ -334,17 +340,21 @@ func (c *wireCodec) makeAuthHeader(nonce []byte, challenge *whoareyouV5) (*authH
|
||||||
return nil, nil, fmt.Errorf("can't generate ephemeral key")
|
return nil, nil, fmt.Errorf("can't generate ephemeral key")
|
||||||
}
|
}
|
||||||
ephpubkey := encodePubkey(&ephkey.PublicKey)
|
ephpubkey := encodePubkey(&ephkey.PublicKey)
|
||||||
|
|
||||||
|
// Add ID nonce signature to response.
|
||||||
idsig, err := c.signIDNonce(challenge.IDNonce[:], ephpubkey[:])
|
idsig, err := c.signIDNonce(challenge.IDNonce[:], ephpubkey[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("can't sign: %v", err)
|
return nil, nil, fmt.Errorf("can't sign: %v", err)
|
||||||
}
|
}
|
||||||
resp.Signature = idsig
|
resp.Signature = idsig
|
||||||
|
|
||||||
// Encrypt the authentication response.
|
// Create session keys.
|
||||||
sec := c.deriveKeys(c.localnode.ID(), challenge.node.ID(), ephkey, remotePubkey, challenge)
|
sec := c.deriveKeys(c.localnode.ID(), challenge.node.ID(), ephkey, remotePubkey, challenge)
|
||||||
if sec == nil {
|
if sec == nil {
|
||||||
return nil, nil, fmt.Errorf("key derivation failed")
|
return nil, nil, fmt.Errorf("key derivation failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encrypt the authentication response and assemble the auth header.
|
||||||
respRLP, err := rlp.EncodeToBytes(resp)
|
respRLP, err := rlp.EncodeToBytes(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("can't encode auth response: %v", err)
|
return nil, nil, fmt.Errorf("can't encode auth response: %v", err)
|
||||||
|
|
@ -492,7 +502,7 @@ func (c *wireCodec) decodeAuth(fromID enode.ID, fromAddr string, head *authHeade
|
||||||
|
|
||||||
// decodeAuthResp decodes and verifies an authentication response.
|
// decodeAuthResp decodes and verifies an authentication response.
|
||||||
func (c *wireCodec) decodeAuthResp(fromID enode.ID, fromAddr string, head *authHeaderList, challenge *whoareyouV5) (*handshakeSecrets, *enode.Node, error) {
|
func (c *wireCodec) decodeAuthResp(fromID enode.ID, fromAddr string, head *authHeaderList, challenge *whoareyouV5) (*handshakeSecrets, *enode.Node, error) {
|
||||||
// Decrypt / decode.
|
// Decrypt / decode the response.
|
||||||
if head.Scheme != authSchemeName {
|
if head.Scheme != authSchemeName {
|
||||||
return nil, nil, errUnknownAuthScheme
|
return nil, nil, errUnknownAuthScheme
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -529,8 +529,12 @@ func (t *UDPv5) sendNextCall(id enode.ID) {
|
||||||
// This performs a handshake if needed.
|
// This performs a handshake if needed.
|
||||||
func (t *UDPv5) sendCall(c *callV5) {
|
func (t *UDPv5) sendCall(c *callV5) {
|
||||||
if len(c.authTag) > 0 {
|
if len(c.authTag) > 0 {
|
||||||
|
// The call already has an authTag from a previous handshake attempt. Remove the
|
||||||
|
// entry for the authTag because we're about to generate a new authTag for this
|
||||||
|
// call.
|
||||||
delete(t.activeCallByAuth, string(c.authTag))
|
delete(t.activeCallByAuth, string(c.authTag))
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := &net.UDPAddr{IP: c.node.IP(), Port: c.node.UDP()}
|
addr := &net.UDPAddr{IP: c.node.IP(), Port: c.node.UDP()}
|
||||||
newTag, _ := t.send(c.node.ID(), addr, c.packet, c.challenge)
|
newTag, _ := t.send(c.node.ID(), addr, c.packet, c.challenge)
|
||||||
c.authTag = newTag
|
c.authTag = newTag
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue