From 93fb733f2de330db673722a78262cb1b414f67cb Mon Sep 17 00:00:00 2001 From: Guillaume Louvigny Date: Wed, 28 Aug 2019 10:39:09 +0200 Subject: [PATCH] fix: signature for pushed events --- core/crypto/keypair/keypair.go | 7 ++++++- core/node/mainloop.go | 23 +++++++++++------------ core/node/p2papi.go | 16 +++++++++++----- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/core/crypto/keypair/keypair.go b/core/crypto/keypair/keypair.go index 5bcb08e636..6448dc21c5 100644 --- a/core/crypto/keypair/keypair.go +++ b/core/crypto/keypair/keypair.go @@ -95,7 +95,12 @@ func (m *Revocation) GetSignedValue() SignableValue { } func CheckSignature(signedValue SignedValue, pubKey crypto.PublicKey) error { - hashFunc, err := signedValue.GetSignature().SignatureAlgorithm.GetHashFunction() + sig := signedValue.GetSignature() + if sig == nil { + return errors.New("no signature found") + } + + hashFunc, err := sig.SignatureAlgorithm.GetHashFunction() if err != nil { return err diff --git a/core/node/mainloop.go b/core/node/mainloop.go index f113b42506..572af4d378 100644 --- a/core/node/mainloop.go +++ b/core/node/mainloop.go @@ -346,10 +346,19 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch, // tracer := tracing.EnterFunc(ctx, dispatch, envelope) // defer tracer.Finish() // ctx = tracer.Context() + var err error if dispatch.ContactID == n.UserID() { return nil } + + envCopy := *envelope + envCopy.ChannelID = dispatch.ContactID + envCopy.Signature, err = keypair.Sign(n.crypto, &envCopy) + if err != nil { + return errors.Wrap(err, "failed to sign envelope") + } + // Async subscribe to conversation // wait for 1s to simulate a sync subscription, // if too long, the task will be done in background @@ -358,16 +367,6 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch, tctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() - var err error - - envCopy := *envelope - envCopy.ChannelID = dispatch.ContactID - envCopy.Signature, err = keypair.Sign(n.crypto, &envCopy) - if err != nil { - n.LogBackgroundError(ctx, errors.Wrap(err, "failed to sign envelope")) - return - } - data, err := proto.Marshal(&envCopy) if err != nil { n.LogBackgroundWarn(ctx, errors.Wrap(err, "failed to marshal envelope")) @@ -384,7 +383,7 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch, n.LogBackgroundWarn(ctx, errors.Wrap(err, "failed to emit envelope on network")) // push the outgoing event on the client stream - go n.queuePushEvent(ctx, event, envelope) + go n.queuePushEvent(ctx, event, &envCopy) return } @@ -395,7 +394,7 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch, case <-done: case <-time.After(1 * time.Second): // push the outgoing event on the client stream - go n.queuePushEvent(ctx, event, envelope) + go n.queuePushEvent(ctx, event, &envCopy) } return nil } diff --git a/core/node/p2papi.go b/core/node/p2papi.go index fd854025ee..db827d0ec9 100644 --- a/core/node/p2papi.go +++ b/core/node/p2papi.go @@ -192,18 +192,24 @@ func (n *Node) pushEvent(ctx context.Context, event *entity.Event, envelope *ent return errorcodes.ErrPushBroadcast.Wrap(err) } + pushClient := pushService.NewPushServiceClient(conn) + for _, pushIdentifier := range pushIdentifiers { var pushMessages []*push.PushData logger().Info(fmt.Sprintf("connecting to push notification server on %s", pushServerAddr)) - pushClient := pushService.NewPushServiceClient(conn) - for _, c := range chunks { + envelopeData, err := c.Marshal() + if err != nil { + logger().Error("unable to marshal envelope") + break + } + pushData := &push.PushData{ Priority: event.PushPriority(), PushIdentifier: pushIdentifier.PushInfo, - Envelope: c.GetData(), + Envelope: envelopeData, } pushMessages = append(pushMessages, pushData) @@ -221,10 +227,10 @@ func (n *Node) pushEvent(ctx context.Context, event *entity.Event, envelope *ent logger().Error("error while pushing data to device", zap.Error(err)) continue } - - _ = conn.Close() } + _ = conn.Close() + return nil }