2023-01-06 12:21:14 +00:00
|
|
|
package pairing
|
2022-06-15 14:49:31 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-15 15:36:17 +00:00
|
|
|
"crypto/ecdsa"
|
2022-08-31 11:44:12 +00:00
|
|
|
"crypto/elliptic"
|
2022-08-19 12:45:50 +00:00
|
|
|
"crypto/rand"
|
2023-03-23 11:44:15 +00:00
|
|
|
"encoding/json"
|
2022-06-15 15:36:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-08-31 11:44:12 +00:00
|
|
|
"time"
|
2022-08-19 12:45:50 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
2023-03-23 11:44:15 +00:00
|
|
|
"go.uber.org/zap"
|
2022-08-31 12:47:16 +00:00
|
|
|
|
2023-02-17 13:02:42 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2022-10-21 12:15:39 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-02-17 13:02:42 +00:00
|
|
|
"github.com/status-im/status-go/server"
|
2022-06-15 14:49:31 +00:00
|
|
|
)
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| type BaseServer struct {
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
type BaseServer struct {
|
|
|
|
server.Server
|
2022-08-19 12:45:50 +00:00
|
|
|
|
|
|
|
cookieStore *sessions.CookieStore
|
2023-03-23 11:44:15 +00:00
|
|
|
encryptor *PayloadEncryptor
|
|
|
|
|
|
|
|
pk *ecdsa.PublicKey
|
|
|
|
ek []byte
|
|
|
|
// TODO remove mode from pairing process
|
|
|
|
// https://github.com/status-im/status-go/issues/3301
|
|
|
|
mode Mode
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
// NewBaseServer returns a *BaseServer init from the given *SenderServerConfig
|
|
|
|
func NewBaseServer(logger *zap.Logger, e *PayloadEncryptor, config *ServerConfig) (*BaseServer, error) {
|
|
|
|
cs, err := makeCookieStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-01 15:37:53 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
bs := &BaseServer{
|
|
|
|
Server: server.NewServer(
|
|
|
|
config.Cert,
|
|
|
|
config.Hostname,
|
|
|
|
nil,
|
|
|
|
logger,
|
|
|
|
),
|
|
|
|
encryptor: e,
|
|
|
|
cookieStore: cs,
|
|
|
|
pk: config.PK,
|
|
|
|
ek: config.EK,
|
|
|
|
mode: config.Mode,
|
|
|
|
}
|
|
|
|
bs.SetTimeout(config.Timeout)
|
|
|
|
return bs, nil
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:36:17 +00:00
|
|
|
// MakeConnectionParams generates a *ConnectionParams based on the Server's current state
|
2023-03-23 11:44:15 +00:00
|
|
|
func (s *BaseServer) MakeConnectionParams() (*ConnectionParams, error) {
|
2023-01-06 12:21:14 +00:00
|
|
|
hostname := s.GetHostname()
|
|
|
|
netIP := net.ParseIP(hostname)
|
2022-06-15 15:36:17 +00:00
|
|
|
if netIP == nil {
|
2023-01-06 12:21:14 +00:00
|
|
|
return nil, fmt.Errorf("invalid ip address given '%s'", hostname)
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
netIP4 := netIP.To4()
|
|
|
|
if netIP4 != nil {
|
|
|
|
netIP = netIP4
|
|
|
|
}
|
|
|
|
|
2022-10-12 10:00:14 +00:00
|
|
|
return NewConnectionParams(netIP, s.MustGetPort(), s.pk, s.ek, s.mode), nil
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
func MakeServerConfig(config *ServerConfig) error {
|
|
|
|
tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2022-08-31 12:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
AESKey := make([]byte, 32)
|
|
|
|
_, err = rand.Read(AESKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
outboundIP, err := server.GetOutboundIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsCert, _, err := GenerateCertFromKey(tlsKey, time.Now(), outboundIP.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.PK = &tlsKey.PublicKey
|
|
|
|
config.EK = AESKey
|
|
|
|
config.Cert = &tlsCert
|
|
|
|
config.Hostname = outboundIP.String()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| type SenderServer struct {
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| With AccountPayloadMounter, RawMessagePayloadMounter and InstallationPayloadMounterReceiver
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
type SenderServer struct {
|
|
|
|
*BaseServer
|
|
|
|
accountMounter PayloadMounter
|
|
|
|
rawMessageMounter *RawMessagePayloadMounter
|
|
|
|
installationMounter *InstallationPayloadMounterReceiver
|
2023-03-16 14:15:07 +00:00
|
|
|
|
|
|
|
challengeGiver *ChallengeGiver
|
2023-03-23 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSenderServer returns a *SenderServer init from the given *SenderServerConfig
|
|
|
|
func NewSenderServer(backend *api.GethStatusBackend, config *SenderServerConfig) (*SenderServer, error) {
|
|
|
|
logger := logutils.ZapLogger().Named("SenderServer")
|
|
|
|
e := NewPayloadEncryptor(config.ServerConfig.EK)
|
|
|
|
|
|
|
|
bs, err := NewBaseServer(logger, e, config.ServerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
am, rmm, imr, err := NewPayloadMounters(logger, e, backend, config.SenderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-16 14:15:07 +00:00
|
|
|
cg, err := NewChallengeGiver(e, logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
return &SenderServer{
|
|
|
|
BaseServer: bs,
|
|
|
|
accountMounter: am,
|
|
|
|
rawMessageMounter: rmm,
|
|
|
|
installationMounter: imr,
|
2023-03-16 14:15:07 +00:00
|
|
|
challengeGiver: cg,
|
2023-03-23 11:44:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SenderServer) startSendingData() error {
|
2023-01-06 12:21:14 +00:00
|
|
|
s.SetHandlers(server.HandlerPatternMap{
|
2023-03-16 14:15:07 +00:00
|
|
|
pairingChallenge: handlePairingChallenge(s.challengeGiver),
|
|
|
|
pairingSendAccount: middlewareChallenge(s.challengeGiver, handleSendAccount(s, s.accountMounter)),
|
|
|
|
pairingSendSyncDevice: middlewareChallenge(s.challengeGiver, handlePairingSyncDeviceSend(s, s.rawMessageMounter)),
|
2023-03-23 11:44:15 +00:00
|
|
|
// TODO implement refactor of installation data exchange to follow the send/receive pattern of
|
|
|
|
// the other handlers.
|
|
|
|
// https://github.com/status-im/status-go/issues/3304
|
2023-02-28 12:32:45 +00:00
|
|
|
// receive installation data from receiver
|
2023-03-16 14:15:07 +00:00
|
|
|
pairingReceiveInstallation: middlewareChallenge(s.challengeGiver, handleReceiveInstallation(s, s.installationMounter)),
|
2022-08-19 12:45:50 +00:00
|
|
|
})
|
2022-06-10 15:32:15 +00:00
|
|
|
return s.Start()
|
|
|
|
}
|
2022-08-31 11:44:12 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
// MakeFullSenderServer generates a fully configured and randomly seeded SenderServer
|
|
|
|
func MakeFullSenderServer(backend *api.GethStatusBackend, mode Mode, config *SenderServerConfig) (*SenderServer, error) {
|
|
|
|
err := MakeServerConfig(config.ServerConfig)
|
2022-08-31 11:44:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
config.SenderConfig.DB = backend.GetMultiaccountDB()
|
|
|
|
return NewSenderServer(backend, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartUpSenderServer generates a SenderServer, starts the sending server in the correct mode
|
|
|
|
// and returns the ConnectionParams string to allow a ReceiverClient to make a successful connection.
|
|
|
|
func StartUpSenderServer(backend *api.GethStatusBackend, mode Mode, configJSON string) (string, error) {
|
|
|
|
conf := NewSenderServerConfig()
|
|
|
|
err := json.Unmarshal([]byte(configJSON), conf)
|
2022-08-31 11:44:12 +00:00
|
|
|
if err != nil {
|
2023-03-23 11:44:15 +00:00
|
|
|
return "", err
|
2022-08-31 11:44:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
ps, err := MakeFullSenderServer(backend, mode, conf)
|
2022-08-31 11:44:12 +00:00
|
|
|
if err != nil {
|
2023-03-23 11:44:15 +00:00
|
|
|
return "", err
|
2022-08-31 11:44:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
err = ps.startSendingData()
|
2022-08-31 11:44:12 +00:00
|
|
|
if err != nil {
|
2023-03-23 11:44:15 +00:00
|
|
|
return "", err
|
2022-08-31 11:44:12 +00:00
|
|
|
}
|
2023-02-28 12:32:45 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
cp, err := ps.MakeConnectionParams()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2023-02-28 12:32:45 +00:00
|
|
|
}
|
2023-03-23 11:44:15 +00:00
|
|
|
|
|
|
|
return cp.ToString(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| ReceiverServer
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| With AccountPayloadReceiver, RawMessagePayloadReceiver, InstallationPayloadMounterReceiver
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
type ReceiverServer struct {
|
|
|
|
*BaseServer
|
|
|
|
accountReceiver PayloadReceiver
|
|
|
|
rawMessageReceiver PayloadReceiver
|
|
|
|
installationReceiver PayloadMounterReceiver
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReceiverServer returns a *SenderServer init from the given *ReceiverServerConfig
|
|
|
|
func NewReceiverServer(backend *api.GethStatusBackend, config *ReceiverServerConfig) (*ReceiverServer, error) {
|
|
|
|
logger := logutils.ZapLogger().Named("SenderServer")
|
|
|
|
e := NewPayloadEncryptor(config.ServerConfig.EK)
|
|
|
|
|
|
|
|
bs, err := NewBaseServer(logger, e, config.ServerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-02-28 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
ar, rmr, imr, err := NewPayloadReceivers(logger, e, backend, config.ReceiverConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-31 11:44:12 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
return &ReceiverServer{
|
|
|
|
BaseServer: bs,
|
|
|
|
accountReceiver: ar,
|
|
|
|
rawMessageReceiver: rmr,
|
|
|
|
installationReceiver: imr,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-31 11:44:12 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
func (s *ReceiverServer) startReceivingData() error {
|
|
|
|
s.SetHandlers(server.HandlerPatternMap{
|
|
|
|
pairingReceiveAccount: handleReceiveAccount(s, s.accountReceiver),
|
|
|
|
pairingReceiveSyncDevice: handleParingSyncDeviceReceive(s, s.rawMessageReceiver),
|
|
|
|
// TODO implement refactor of installation data exchange to follow the send/receive pattern of
|
|
|
|
// the other handlers.
|
|
|
|
// https://github.com/status-im/status-go/issues/3304
|
|
|
|
// send installation data back to sender
|
|
|
|
pairingSendInstallation: handleSendInstallation(s, s.installationReceiver),
|
2022-08-31 11:44:12 +00:00
|
|
|
})
|
2023-03-23 11:44:15 +00:00
|
|
|
return s.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeFullReceiverServer generates a fully configured and randomly seeded ReceiverServer
|
|
|
|
func MakeFullReceiverServer(backend *api.GethStatusBackend, mode Mode, config *ReceiverServerConfig) (*ReceiverServer, error) {
|
|
|
|
err := MakeServerConfig(config.ServerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
activeAccount, _ := backend.GetActiveAccount()
|
|
|
|
if activeAccount != nil {
|
|
|
|
config.ReceiverConfig.LoggedInKeyUID = activeAccount.KeyUID
|
|
|
|
}
|
|
|
|
config.ReceiverConfig.DB = backend.GetMultiaccountDB()
|
|
|
|
|
|
|
|
return NewReceiverServer(backend, config)
|
2022-08-31 11:44:12 +00:00
|
|
|
}
|
2022-08-31 12:47:16 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
// StartUpReceiverServer generates a ReceiverServer, starts the sending server in the correct mode
|
|
|
|
// and returns the ConnectionParams string to allow a SenderClient to make a successful connection.
|
|
|
|
func StartUpReceiverServer(backend *api.GethStatusBackend, mode Mode, configJSON string) (string, error) {
|
|
|
|
conf := NewReceiverServerConfig()
|
|
|
|
err := json.Unmarshal([]byte(configJSON), conf)
|
2022-08-31 12:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
ps, err := MakeFullReceiverServer(backend, mode, conf)
|
2022-08-31 12:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
err = ps.startReceivingData()
|
2022-08-31 12:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
cp, err := ps.MakeConnectionParams()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cp.ToString(), nil
|
|
|
|
}
|