status-go/protocol/push_notification_server/push_notification_server.go

124 lines
2.9 KiB
Go
Raw Normal View History

package protocol
import (
"errors"
"fmt"
2020-06-30 14:55:24 +00:00
"io"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
2020-06-30 14:55:24 +00:00
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/eth-node/crypto/ecies"
"github.com/status-im/status-go/protocol/protobuf"
)
const encryptedPayloadKeyLength = 16
const nonceLength = 12
2020-06-30 14:55:24 +00:00
var ErrInvalidPushNotificationPreferencesVersion = errors.New("invalid version")
var ErrEmptyPushNotificationPreferencesPayload = errors.New("empty payload")
var ErrEmptyPushNotificationPreferencesPublicKey = errors.New("no public key")
var ErrCouldNotUnmarshalPushNotificationPreferences = errors.New("could not unmarshal preferences")
var ErrInvalidCiphertextLength = errors.New("invalid cyphertext length")
type Config struct {
// Identity is our identity key
Identity *ecdsa.PrivateKey
// GorushUrl is the url for the gorush service
GorushURL string
}
type Server struct {
persistence *Persistence
config *Config
}
func New(persistence *Persistence) *Server {
return &Server{persistence: persistence}
}
2020-06-30 14:55:24 +00:00
func (p *Server) generateSharedKey(publicKey *ecdsa.PublicKey) ([]byte, error) {
return ecies.ImportECDSA(p.config.Identity).GenerateShared(
ecies.ImportECDSAPublic(publicKey),
encryptedPayloadKeyLength,
encryptedPayloadKeyLength,
)
}
func (p *Server) ValidateRegistration(previousPreferences *protobuf.PushNotificationPreferences, publicKey *ecdsa.PublicKey, payload []byte) error {
if payload == nil {
2020-06-30 14:55:24 +00:00
return ErrEmptyPushNotificationPreferencesPayload
}
if publicKey == nil {
2020-06-30 14:55:24 +00:00
return ErrEmptyPushNotificationPreferencesPublicKey
}
2020-06-30 14:55:24 +00:00
sharedKey, err := p.generateSharedKey(publicKey)
if err != nil {
return err
}
decryptedPayload, err := decrypt(payload, sharedKey)
if err != nil {
return err
}
2020-06-30 14:55:24 +00:00
preferences := &protobuf.PushNotificationPreferences{}
if err := proto.Unmarshal(decryptedPayload, preferences); err != nil {
return ErrCouldNotUnmarshalPushNotificationPreferences
}
if preferences.Version < 1 {
return ErrInvalidPushNotificationPreferencesVersion
}
fmt.Println(decryptedPayload)
/*if newRegistration.Version < 1 {
2020-06-30 14:55:24 +00:00
return ErrInvalidPushNotificationPreferencesVersion
}*/
return nil
}
func decrypt(cyphertext []byte, key []byte) ([]byte, error) {
if len(cyphertext) < nonceLength {
2020-06-30 14:55:24 +00:00
return nil, ErrInvalidCiphertextLength
}
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := cyphertext[:nonceLength]
2020-06-30 14:55:24 +00:00
return gcm.Open(nil, nonce, cyphertext[nonceLength:], nil)
}
func encrypt(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, plaintext, nil), nil
}