1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 14:07:02 +09:00

Fix handshake tests

This commit is contained in:
mcrakhman 2023-03-26 13:45:24 +02:00 committed by Mikhail Iudin
parent 52f462ff2c
commit 4efe189eec
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
11 changed files with 56 additions and 57 deletions

View file

@ -19,18 +19,17 @@ type Change struct {
AclHeadId string AclHeadId string
Id string Id string
SnapshotId string SnapshotId string
IsSnapshot bool
Timestamp int64 Timestamp int64
ReadKeyId string ReadKeyId string
Identity crypto.PubKey Identity crypto.PubKey
Data []byte Data []byte
Model interface{} Model interface{}
Signature []byte
// iterator helpers // iterator helpers
visited bool visited bool
branchesFinished bool branchesFinished bool
IsSnapshot bool
Signature []byte
} }
func NewChange(id string, identity crypto.PubKey, ch *treechangeproto.TreeChange, signature []byte) *Change { func NewChange(id string, identity crypto.PubKey, ch *treechangeproto.TreeChange, signature []byte) *Change {

View file

@ -3,6 +3,7 @@ package peer
import ( import (
"context" "context"
"errors" "errors"
"github.com/anytypeio/any-sync/util/crypto"
"github.com/libp2p/go-libp2p/core/sec" "github.com/libp2p/go-libp2p/core/sec"
"storj.io/drpc/drpcctx" "storj.io/drpc/drpcctx"
) )
@ -43,6 +44,14 @@ func CtxIdentity(ctx context.Context) ([]byte, error) {
return nil, ErrIdentityNotFoundInContext return nil, ErrIdentityNotFoundInContext
} }
// CtxPubKey returns identity unmarshalled from proto in crypto.PubKey model
func CtxPubKey(ctx context.Context) (crypto.PubKey, error) {
if identity, ok := ctx.Value(contextKeyIdentity).([]byte); ok {
return crypto.UnmarshalEd25519PublicKeyProto(identity)
}
return nil, ErrIdentityNotFoundInContext
}
// CtxWithIdentity sets identity in the context // CtxWithIdentity sets identity in the context
func CtxWithIdentity(ctx context.Context, identity []byte) context.Context { func CtxWithIdentity(ctx context.Context, identity []byte) context.Context {
return context.WithValue(ctx, contextKeyIdentity, identity) return context.WithValue(ctx, contextKeyIdentity, identity)

View file

@ -38,8 +38,10 @@ func (p *peerSignVerifier) MakeCredentials(sc sec.SecureConn) *handshakeproto.Cr
if err != nil { if err != nil {
log.Warn("can't sign identity credentials", zap.Error(err)) log.Warn("can't sign identity credentials", zap.Error(err))
} }
// this will actually be called only once
marshalled, _ := p.account.SignKey.GetPublic().Marshall()
msg := &handshakeproto.PayloadSignedPeerIds{ msg := &handshakeproto.PayloadSignedPeerIds{
Identity: p.account.Identity, Identity: marshalled,
Sign: sign, Sign: sign,
} }
payload, _ := msg.Marshal() payload, _ := msg.Marshal()
@ -57,7 +59,7 @@ func (p *peerSignVerifier) CheckCredential(sc sec.SecureConn, cred *handshakepro
if err = msg.Unmarshal(cred.Payload); err != nil { if err = msg.Unmarshal(cred.Payload); err != nil {
return nil, handshake.ErrUnexpectedPayload return nil, handshake.ErrUnexpectedPayload
} }
pubKey, err := crypto.NewSigningEd25519PubKeyFromBytes(msg.Identity) pubKey, err := crypto.UnmarshalEd25519PublicKeyProto(msg.Identity)
if err != nil { if err != nil {
return nil, handshake.ErrInvalidCredentials return nil, handshake.ErrInvalidCredentials
} }

View file

@ -17,6 +17,8 @@ import (
func TestPeerSignVerifier_CheckCredential(t *testing.T) { func TestPeerSignVerifier_CheckCredential(t *testing.T) {
a1 := newTestAccData(t) a1 := newTestAccData(t)
a2 := newTestAccData(t) a2 := newTestAccData(t)
identity1, _ := a1.SignKey.GetPublic().Marshall()
identity2, _ := a2.SignKey.GetPublic().Marshall()
cc1 := newPeerSignVerifier(a1) cc1 := newPeerSignVerifier(a1)
cc2 := newPeerSignVerifier(a2) cc2 := newPeerSignVerifier(a2)
@ -28,11 +30,11 @@ func TestPeerSignVerifier_CheckCredential(t *testing.T) {
cr2 := cc2.MakeCredentials(c2) cr2 := cc2.MakeCredentials(c2)
id1, err := cc1.CheckCredential(c1, cr2) id1, err := cc1.CheckCredential(c1, cr2)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, a2.Identity, id1) assert.Equal(t, identity2, id1)
id2, err := cc2.CheckCredential(c2, cr1) id2, err := cc2.CheckCredential(c2, cr1)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, a1.Identity, id2) assert.Equal(t, identity1, id2)
_, err = cc1.CheckCredential(c1, cr1) _, err = cc1.CheckCredential(c1, cr1)
assert.EqualError(t, err, handshake.ErrInvalidCredentials.Error()) assert.EqualError(t, err, handshake.ErrInvalidCredentials.Error())

View file

@ -46,8 +46,9 @@ func TestHandshake(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
accId, err := peer.CtxIdentity(res.ctx) accId, err := peer.CtxIdentity(res.ctx)
require.NoError(t, err) require.NoError(t, err)
marshalledId, _ := nc.GetAccountService(1).Account().SignKey.GetPublic().Marshall()
assert.Equal(t, nc.GetAccountService(1).Account().PeerId, peerId) assert.Equal(t, nc.GetAccountService(1).Account().PeerId, peerId)
assert.Equal(t, nc.GetAccountService(1).Account().Identity, accId) assert.Equal(t, marshalledId, accId)
} }
func newFixture(t *testing.T, nc *testnodeconf.Config, acc accountservice.Service) *fixture { func newFixture(t *testing.T, nc *testnodeconf.Config, acc accountservice.Service) *fixture {

View file

@ -15,10 +15,9 @@ type configGetter interface {
} }
type NodeConfig struct { type NodeConfig struct {
PeerId string `yaml:"peerId"` PeerId string `yaml:"peerId"`
Addresses []string `yaml:"address"` Addresses []string `yaml:"address"`
EncryptionKey string `yaml:"encryptionPubKey,omitempty"` Types []NodeType `yaml:"types,omitempty"`
Types []NodeType `yaml:"types,omitempty"`
} }
func (n NodeConfig) HasType(t NodeType) bool { func (n NodeConfig) HasType(t NodeType) bool {

View file

@ -5,9 +5,6 @@ import (
"github.com/anytypeio/any-sync/app" "github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/app/logger" "github.com/anytypeio/any-sync/app/logger"
"github.com/anytypeio/any-sync/util/crypto" "github.com/anytypeio/any-sync/util/crypto"
"github.com/anytypeio/any-sync/util/keys"
"github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey"
"github.com/anytypeio/any-sync/util/keys/asymmetric/signingkey"
"github.com/anytypeio/go-chash" "github.com/anytypeio/go-chash"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
) )
@ -37,10 +34,9 @@ type service struct {
} }
type Node struct { type Node struct {
Addresses []string Addresses []string
PeerId string PeerId string
SigningKey signingkey.PubKey SigningKey crypto.PubKey
EncryptionKey encryptionkey.PubKey
} }
func (n *Node) Id() string { func (n *Node) Id() string {
@ -127,18 +123,9 @@ func nodeFromConfigNode(n NodeConfig) (*Node, error) {
return nil, err return nil, err
} }
encPubKey, err := keys.DecodeKeyFromString(
n.EncryptionKey,
encryptionkey.NewEncryptionRsaPubKeyFromBytes,
nil)
if err != nil {
return nil, err
}
return &Node{ return &Node{
Addresses: n.Addresses, Addresses: n.Addresses,
PeerId: n.PeerId, PeerId: n.PeerId,
SigningKey: sigPubKey, SigningKey: sigPubKey,
EncryptionKey: encPubKey,
}, nil }, nil
} }

View file

@ -6,8 +6,6 @@ import (
"github.com/anytypeio/any-sync/commonspace/object/accountdata" "github.com/anytypeio/any-sync/commonspace/object/accountdata"
"github.com/anytypeio/any-sync/nodeconf" "github.com/anytypeio/any-sync/nodeconf"
"github.com/anytypeio/any-sync/util/crypto" "github.com/anytypeio/any-sync/util/crypto"
"github.com/anytypeio/any-sync/util/keys"
"github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey"
"github.com/anytypeio/any-sync/util/peer" "github.com/anytypeio/any-sync/util/peer"
) )
@ -20,19 +18,10 @@ func (s *AccountTestService) Init(a *app.App) (err error) {
if s.acc != nil { if s.acc != nil {
return return
} }
encKey, _, err := encryptionkey.GenerateRandomRSAKeyPair(2048)
if err != nil {
return
}
signKey, _, err := crypto.GenerateRandomEd25519KeyPair() signKey, _, err := crypto.GenerateRandomEd25519KeyPair()
if err != nil { if err != nil {
return return
} }
ident, err := signKey.GetPublic().Raw()
if err != nil {
return
}
peerKey, _, err := crypto.GenerateRandomEd25519KeyPair() peerKey, _, err := crypto.GenerateRandomEd25519KeyPair()
if err != nil { if err != nil {
@ -44,11 +33,9 @@ func (s *AccountTestService) Init(a *app.App) (err error) {
return err return err
} }
s.acc = &accountdata.AccountKeys{ s.acc = &accountdata.AccountKeys{
Identity: ident, PeerKey: peerKey,
PeerKey: peerKey, SignKey: signKey,
SignKey: signKey, PeerId: peerId.String(),
EncKey: encKey,
PeerId: peerId.String(),
} }
return nil return nil
} }
@ -62,14 +49,9 @@ func (s *AccountTestService) Account() *accountdata.AccountKeys {
} }
func (s *AccountTestService) NodeConf(addrs []string) nodeconf.NodeConfig { func (s *AccountTestService) NodeConf(addrs []string) nodeconf.NodeConfig {
encEk, err := keys.EncodeKeyToString(s.acc.EncKey.GetPublic())
if err != nil {
panic(err)
}
return nodeconf.NodeConfig{ return nodeconf.NodeConfig{
PeerId: s.acc.PeerId, PeerId: s.acc.PeerId,
Addresses: addrs, Addresses: addrs,
EncryptionKey: encEk, Types: []nodeconf.NodeType{nodeconf.NodeTypeTree},
Types: []nodeconf.NodeType{nodeconf.NodeTypeTree},
} }
} }

View file

@ -10,6 +10,7 @@ import (
"github.com/anytypeio/any-sync/util/crypto/cryptoproto" "github.com/anytypeio/any-sync/util/crypto/cryptoproto"
"github.com/anytypeio/any-sync/util/strkey" "github.com/anytypeio/any-sync/util/strkey"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p/core/crypto"
"io" "io"
"sync" "sync"
) )
@ -125,6 +126,12 @@ func (k *Ed25519PrivKey) Decrypt(msg []byte) ([]byte, error) {
return DecryptX25519(k.privCurve, k.pubCurve, msg) return DecryptX25519(k.privCurve, k.pubCurve, msg)
} }
// LibP2P converts the key to libp2p format
func (k *Ed25519PrivKey) LibP2P() (crypto.PrivKey, error) {
return crypto.UnmarshalEd25519PrivateKey(k.privKey)
}
// String returns string representation of key
func (k *Ed25519PubKey) String() string { func (k *Ed25519PubKey) String() string {
res, _ := strkey.Encode(strkey.AccountAddressVersionByte, k.pubKey) res, _ := strkey.Encode(strkey.AccountAddressVersionByte, k.pubKey)
return res return res
@ -165,6 +172,7 @@ func (k *Ed25519PubKey) Verify(data []byte, sig []byte) (bool, error) {
return ed25519.Verify(k.pubKey, data, sig), nil return ed25519.Verify(k.pubKey, data, sig), nil
} }
// Marshall marshalls the key into proto
func (k *Ed25519PubKey) Marshall() ([]byte, error) { func (k *Ed25519PubKey) Marshall() ([]byte, error) {
k.marshallOnce.Do(func() { k.marshallOnce.Do(func() {
msg := &cryptoproto.Key{ msg := &cryptoproto.Key{
@ -176,6 +184,11 @@ func (k *Ed25519PubKey) Marshall() ([]byte, error) {
return k.marshalled, k.marshallErr return k.marshalled, k.marshallErr
} }
// LibP2P converts the key to libp2p format
func (k *Ed25519PubKey) LibP2P() (crypto.PubKey, error) {
return crypto.UnmarshalEd25519PublicKey(k.pubKey)
}
// UnmarshalEd25519PublicKey returns a public key from input bytes. // UnmarshalEd25519PublicKey returns a public key from input bytes.
func UnmarshalEd25519PublicKey(data []byte) (PubKey, error) { func UnmarshalEd25519PublicKey(data []byte) (PubKey, error) {
if len(data) != 32 { if len(data) != 32 {

View file

@ -3,6 +3,7 @@ package crypto
import ( import (
"crypto/subtle" "crypto/subtle"
"errors" "errors"
"github.com/libp2p/go-libp2p/core/crypto"
) )
var ErrIncorrectKeyType = errors.New("incorrect key type") var ErrIncorrectKeyType = errors.New("incorrect key type")
@ -26,6 +27,8 @@ type PrivKey interface {
Sign([]byte) ([]byte, error) Sign([]byte) ([]byte, error)
// GetPublic returns the associated public key // GetPublic returns the associated public key
GetPublic() PubKey GetPublic() PubKey
// LibP2P returns libp2p model
LibP2P() (crypto.PrivKey, error)
} }
// PubKey is the public key used to verify the signatures and decrypt messages // PubKey is the public key used to verify the signatures and decrypt messages
@ -42,6 +45,8 @@ type PubKey interface {
Storage() []byte Storage() []byte
// String returns string representation // String returns string representation
String() string String() string
// LibP2P returns libp2p model
LibP2P() (crypto.PubKey, error)
} }
type SymKey interface { type SymKey interface {

View file

@ -1,12 +1,12 @@
package peer package peer
import ( import (
"github.com/anytypeio/any-sync/util/keys/asymmetric/signingkey" utilcrypto "github.com/anytypeio/any-sync/util/crypto"
"github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
) )
func IdFromSigningPubKey(pubKey signingkey.PubKey) (peer.ID, error) { func IdFromSigningPubKey(pubKey utilcrypto.PubKey) (peer.ID, error) {
rawSigning, err := pubKey.Raw() rawSigning, err := pubKey.Raw()
if err != nil { if err != nil {
return "", err return "", err