1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +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
Id string
SnapshotId string
IsSnapshot bool
Timestamp int64
ReadKeyId string
Identity crypto.PubKey
Data []byte
Model interface{}
Signature []byte
// iterator helpers
visited bool
branchesFinished bool
Signature []byte
IsSnapshot bool
}
func NewChange(id string, identity crypto.PubKey, ch *treechangeproto.TreeChange, signature []byte) *Change {

View file

@ -3,6 +3,7 @@ package peer
import (
"context"
"errors"
"github.com/anytypeio/any-sync/util/crypto"
"github.com/libp2p/go-libp2p/core/sec"
"storj.io/drpc/drpcctx"
)
@ -43,6 +44,14 @@ func CtxIdentity(ctx context.Context) ([]byte, error) {
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
func CtxWithIdentity(ctx context.Context, identity []byte) context.Context {
return context.WithValue(ctx, contextKeyIdentity, identity)

View file

@ -38,8 +38,10 @@ func (p *peerSignVerifier) MakeCredentials(sc sec.SecureConn) *handshakeproto.Cr
if err != nil {
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{
Identity: p.account.Identity,
Identity: marshalled,
Sign: sign,
}
payload, _ := msg.Marshal()
@ -57,7 +59,7 @@ func (p *peerSignVerifier) CheckCredential(sc sec.SecureConn, cred *handshakepro
if err = msg.Unmarshal(cred.Payload); err != nil {
return nil, handshake.ErrUnexpectedPayload
}
pubKey, err := crypto.NewSigningEd25519PubKeyFromBytes(msg.Identity)
pubKey, err := crypto.UnmarshalEd25519PublicKeyProto(msg.Identity)
if err != nil {
return nil, handshake.ErrInvalidCredentials
}

View file

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

View file

@ -46,8 +46,9 @@ func TestHandshake(t *testing.T) {
require.NoError(t, err)
accId, err := peer.CtxIdentity(res.ctx)
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().Identity, accId)
assert.Equal(t, marshalledId, accId)
}
func newFixture(t *testing.T, nc *testnodeconf.Config, acc accountservice.Service) *fixture {

View file

@ -15,10 +15,9 @@ type configGetter interface {
}
type NodeConfig struct {
PeerId string `yaml:"peerId"`
Addresses []string `yaml:"address"`
EncryptionKey string `yaml:"encryptionPubKey,omitempty"`
Types []NodeType `yaml:"types,omitempty"`
PeerId string `yaml:"peerId"`
Addresses []string `yaml:"address"`
Types []NodeType `yaml:"types,omitempty"`
}
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/logger"
"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/libp2p/go-libp2p/core/peer"
)
@ -37,10 +34,9 @@ type service struct {
}
type Node struct {
Addresses []string
PeerId string
SigningKey signingkey.PubKey
EncryptionKey encryptionkey.PubKey
Addresses []string
PeerId string
SigningKey crypto.PubKey
}
func (n *Node) Id() string {
@ -127,18 +123,9 @@ func nodeFromConfigNode(n NodeConfig) (*Node, error) {
return nil, err
}
encPubKey, err := keys.DecodeKeyFromString(
n.EncryptionKey,
encryptionkey.NewEncryptionRsaPubKeyFromBytes,
nil)
if err != nil {
return nil, err
}
return &Node{
Addresses: n.Addresses,
PeerId: n.PeerId,
SigningKey: sigPubKey,
EncryptionKey: encPubKey,
Addresses: n.Addresses,
PeerId: n.PeerId,
SigningKey: sigPubKey,
}, nil
}

View file

@ -6,8 +6,6 @@ import (
"github.com/anytypeio/any-sync/commonspace/object/accountdata"
"github.com/anytypeio/any-sync/nodeconf"
"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"
)
@ -20,19 +18,10 @@ func (s *AccountTestService) Init(a *app.App) (err error) {
if s.acc != nil {
return
}
encKey, _, err := encryptionkey.GenerateRandomRSAKeyPair(2048)
if err != nil {
return
}
signKey, _, err := crypto.GenerateRandomEd25519KeyPair()
if err != nil {
return
}
ident, err := signKey.GetPublic().Raw()
if err != nil {
return
}
peerKey, _, err := crypto.GenerateRandomEd25519KeyPair()
if err != nil {
@ -44,11 +33,9 @@ func (s *AccountTestService) Init(a *app.App) (err error) {
return err
}
s.acc = &accountdata.AccountKeys{
Identity: ident,
PeerKey: peerKey,
SignKey: signKey,
EncKey: encKey,
PeerId: peerId.String(),
PeerKey: peerKey,
SignKey: signKey,
PeerId: peerId.String(),
}
return nil
}
@ -62,14 +49,9 @@ func (s *AccountTestService) Account() *accountdata.AccountKeys {
}
func (s *AccountTestService) NodeConf(addrs []string) nodeconf.NodeConfig {
encEk, err := keys.EncodeKeyToString(s.acc.EncKey.GetPublic())
if err != nil {
panic(err)
}
return nodeconf.NodeConfig{
PeerId: s.acc.PeerId,
Addresses: addrs,
EncryptionKey: encEk,
Types: []nodeconf.NodeType{nodeconf.NodeTypeTree},
PeerId: s.acc.PeerId,
Addresses: addrs,
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/strkey"
"github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p/core/crypto"
"io"
"sync"
)
@ -125,6 +126,12 @@ func (k *Ed25519PrivKey) Decrypt(msg []byte) ([]byte, error) {
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 {
res, _ := strkey.Encode(strkey.AccountAddressVersionByte, k.pubKey)
return res
@ -165,6 +172,7 @@ func (k *Ed25519PubKey) Verify(data []byte, sig []byte) (bool, error) {
return ed25519.Verify(k.pubKey, data, sig), nil
}
// Marshall marshalls the key into proto
func (k *Ed25519PubKey) Marshall() ([]byte, error) {
k.marshallOnce.Do(func() {
msg := &cryptoproto.Key{
@ -176,6 +184,11 @@ func (k *Ed25519PubKey) Marshall() ([]byte, error) {
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.
func UnmarshalEd25519PublicKey(data []byte) (PubKey, error) {
if len(data) != 32 {

View file

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

View file

@ -1,12 +1,12 @@
package peer
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/peer"
)
func IdFromSigningPubKey(pubKey signingkey.PubKey) (peer.ID, error) {
func IdFromSigningPubKey(pubKey utilcrypto.PubKey) (peer.ID, error) {
rawSigning, err := pubKey.Raw()
if err != nil {
return "", err