mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 21:37:57 +08:00
more secio debugging
This commit is contained in:
parent
c49580044d
commit
16497ed7a3
@ -2,7 +2,6 @@
|
||||
package secio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
@ -26,24 +25,15 @@ type SessionGenerator struct {
|
||||
func (sg *SessionGenerator) NewSession(ctx context.Context,
|
||||
insecure io.ReadWriter) (Session, error) {
|
||||
|
||||
if sg.LocalID == "" {
|
||||
return nil, errors.New("no local id provided")
|
||||
}
|
||||
|
||||
if sg.PrivateKey == nil {
|
||||
return nil, errors.New("no local private key provided")
|
||||
}
|
||||
|
||||
if !sg.LocalID.MatchesPrivateKey(sg.PrivateKey) {
|
||||
return nil, errors.New("LocalID does not correspond to PrivateKey")
|
||||
ss, err := newSecureSession(sg.LocalID, sg.PrivateKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
ss := newSecureSession(sg.LocalID, sg.PrivateKey)
|
||||
if err := ss.handshake(ctx, insecure); err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
|
||||
@ -46,8 +46,19 @@ type secureSession struct {
|
||||
sharedSecret []byte
|
||||
}
|
||||
|
||||
func newSecureSession(local peer.ID, key ci.PrivKey) *secureSession {
|
||||
return &secureSession{localPeer: local, localKey: key}
|
||||
func newSecureSession(local peer.ID, key ci.PrivKey) (*secureSession, error) {
|
||||
s := &secureSession{localPeer: local, localKey: key}
|
||||
|
||||
switch {
|
||||
case s.localPeer == "":
|
||||
return nil, errors.New("no local id provided")
|
||||
case s.localKey == nil:
|
||||
return nil, errors.New("no local private key provided")
|
||||
case !s.localPeer.MatchesPrivateKey(s.localKey):
|
||||
return nil, fmt.Errorf("peer.ID does not match PrivateKey")
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// handsahke performs initial communication over insecure channel to share
|
||||
@ -55,10 +66,6 @@ func newSecureSession(local peer.ID, key ci.PrivKey) *secureSession {
|
||||
// requires the duplex channel to be a msgio.ReadWriter (for framed messaging)
|
||||
func (s *secureSession) handshake(ctx context.Context, insecure io.ReadWriter) error {
|
||||
|
||||
if !s.localPeer.MatchesPrivateKey(s.localKey) {
|
||||
return fmt.Errorf("peer.ID does not match PrivateKey")
|
||||
}
|
||||
|
||||
s.insecure = insecure
|
||||
s.insecureM = msgio.NewReadWriter(insecure)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user