status-go/server/pairing/server.go

229 lines
5.8 KiB
Go
Raw Normal View History

package pairing
import (
"crypto/ecdsa"
2022-08-31 11:44:12 +00:00
"crypto/elliptic"
2022-08-19 12:45:50 +00:00
"crypto/rand"
"crypto/tls"
"fmt"
"net"
2022-08-31 11:44:12 +00:00
"time"
2022-08-19 12:45:50 +00:00
"github.com/gorilla/sessions"
"github.com/status-im/status-go/api"
2022-10-21 12:15:39 +00:00
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/server"
)
type Server struct {
server.Server
PayloadManager
rawMessagePayloadManager *RawMessagePayloadManager
installationPayloadManager *InstallationPayloadManager
pk *ecdsa.PublicKey
ek []byte
mode Mode
2022-08-19 12:45:50 +00:00
cookieStore *sessions.CookieStore
}
type Config struct {
2022-07-01 15:37:53 +00:00
// Connection fields
PK *ecdsa.PublicKey
EK []byte
Cert *tls.Certificate
Hostname string
Mode Mode
2022-07-01 15:37:53 +00:00
// AccountPayload management fields
*AccountPayloadManagerConfig
}
2022-08-19 12:45:50 +00:00
func makeCookieStore() (*sessions.CookieStore, error) {
auth := make([]byte, 64)
_, err := rand.Read(auth)
if err != nil {
return nil, err
}
enc := make([]byte, 32)
_, err = rand.Read(enc)
if err != nil {
return nil, err
}
return sessions.NewCookieStore(auth, enc), nil
}
// NewPairingServer returns a *Server init from the given *Config
func NewPairingServer(backend *api.GethStatusBackend, config *Config) (*Server, error) {
logger := logutils.ZapLogger().Named("Server")
accountPayloadManagerConfig := config.AccountPayloadManagerConfig
pm, err := NewAccountPayloadManager(config.EK, accountPayloadManagerConfig, logger)
if err != nil {
return nil, err
}
2022-08-19 12:45:50 +00:00
cs, err := makeCookieStore()
if err != nil {
return nil, err
}
rmpm, err := NewRawMessagePayloadManager(logger, pm.accountPayload, config.EK, backend, accountPayloadManagerConfig.GetNodeConfig(), accountPayloadManagerConfig.GetSettingCurrentNetwork(), accountPayloadManagerConfig.GetDeviceType())
if err != nil {
return nil, err
}
ipm, err := NewInstallationPayloadManager(logger, config.EK, backend, accountPayloadManagerConfig.GetDeviceType())
if err != nil {
return nil, err
}
s := &Server{Server: server.NewServer(
config.Cert,
config.Hostname,
nil,
2022-10-21 12:15:39 +00:00
logger,
),
pk: config.PK,
ek: config.EK,
mode: config.Mode,
PayloadManager: pm,
cookieStore: cs,
rawMessagePayloadManager: rmpm,
installationPayloadManager: ipm,
}
s.SetTimeout(config.GetTimeout())
return s, nil
}
// MakeConnectionParams generates a *ConnectionParams based on the Server's current state
func (s *Server) MakeConnectionParams() (*ConnectionParams, error) {
hostname := s.GetHostname()
netIP := net.ParseIP(hostname)
if netIP == nil {
return nil, fmt.Errorf("invalid ip address given '%s'", hostname)
}
netIP4 := netIP.To4()
if netIP4 != nil {
netIP = netIP4
}
return NewConnectionParams(netIP, s.MustGetPort(), s.pk, s.ek, s.mode), nil
}
func (s *Server) StartPairing() error {
switch s.mode {
case Receiving:
return s.startReceivingData()
case Sending:
return s.startSendingData()
default:
return fmt.Errorf("invalid server mode '%d'", s.mode)
}
}
func (s *Server) startReceivingData() error {
s.SetHandlers(server.HandlerPatternMap{
pairingReceiveAccount: handleReceiveAccount(s),
pairingChallenge: handlePairingChallenge(s),
pairingSyncDeviceReceive: handleParingSyncDeviceReceive(s),
// send installation data back to sender
pairingSendInstallation: handleSendInstallation(s),
2022-08-19 12:45:50 +00:00
})
return s.Start()
}
func (s *Server) startSendingData() error {
err := s.Mount()
if err != nil {
return err
}
s.SetHandlers(server.HandlerPatternMap{
pairingSendAccount: challengeMiddleware(s, handleSendAccount(s)),
pairingChallenge: handlePairingChallenge(s),
pairingSyncDeviceSend: challengeMiddleware(s, handlePairingSyncDeviceSend(s)),
// receive installation data from receiver
pairingReceiveInstallation: challengeMiddleware(s, handleReceiveInstallation(s)),
2022-08-19 12:45:50 +00:00
})
return s.Start()
}
2022-08-31 11:44:12 +00:00
// MakeFullPairingServer generates a fully configured and randomly seeded Server
func MakeFullPairingServer(backend *api.GethStatusBackend, mode Mode, storeConfig *PayloadSourceConfig) (*Server, error) {
2022-08-31 11:44:12 +00:00
tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
AESKey := make([]byte, 32)
_, err = rand.Read(AESKey)
if err != nil {
return nil, err
}
outboundIP, err := server.GetOutboundIP()
2022-08-31 11:44:12 +00:00
if err != nil {
return nil, err
}
tlsCert, _, err := GenerateCertFromKey(tlsKey, time.Now(), outboundIP.String())
if err != nil {
return nil, err
}
accountPayloadManagerConfig := &AccountPayloadManagerConfig{
// Things that can't be generated, but DO NOT come from app client
DB: backend.GetMultiaccountDB(),
// Things that can't be generated, but DO come from the app client
PayloadSourceConfig: storeConfig,
}
if mode == Receiving {
updateLoggedInKeyUID(accountPayloadManagerConfig, backend)
}
return NewPairingServer(backend, &Config{
// Things that can be generated, and CANNOT come from the app client (well they could be this is better)
2022-08-31 11:44:12 +00:00
PK: &tlsKey.PublicKey,
EK: AESKey,
Cert: &tlsCert,
Hostname: outboundIP.String(),
// Things that can't be generated, but DO come from the app client
2022-08-31 11:44:12 +00:00
Mode: mode,
AccountPayloadManagerConfig: accountPayloadManagerConfig,
2022-08-31 11:44:12 +00:00
})
}
// StartUpPairingServer generates a Server, starts the pairing server in the correct mode
// and returns the ConnectionParams string to allow a Client to make a successful connection.
func StartUpPairingServer(backend *api.GethStatusBackend, mode Mode, configJSON string) (string, error) {
conf, err := NewPayloadSourceForServer(configJSON, mode)
if err != nil {
return "", err
}
ps, err := MakeFullPairingServer(backend, mode, conf)
if err != nil {
return "", err
}
err = ps.StartPairing()
if err != nil {
return "", err
}
cp, err := ps.MakeConnectionParams()
if err != nil {
return "", err
}
return cp.ToString(), nil
}