2022-05-03 14:50:40 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-06-10 15:32:15 +00:00
|
|
|
"bytes"
|
2022-05-10 09:34:53 +00:00
|
|
|
"crypto/ecdsa"
|
2022-05-03 14:50:40 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2022-08-31 14:01:45 +00:00
|
|
|
"encoding/json"
|
2022-08-07 22:14:33 +00:00
|
|
|
"encoding/pem"
|
2022-05-03 14:50:40 +00:00
|
|
|
"fmt"
|
2022-06-10 15:32:15 +00:00
|
|
|
"io/ioutil"
|
2022-05-03 14:50:40 +00:00
|
|
|
"net/http"
|
2022-08-19 12:45:50 +00:00
|
|
|
"net/http/cookiejar"
|
2022-05-03 14:50:40 +00:00
|
|
|
"net/url"
|
2022-08-19 12:45:50 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
2022-08-31 14:01:45 +00:00
|
|
|
|
2022-10-21 12:15:39 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2022-08-31 14:01:45 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2022-08-31 14:31:28 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2022-05-03 14:50:40 +00:00
|
|
|
)
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
type PairingClient struct {
|
2022-05-03 14:50:40 +00:00
|
|
|
*http.Client
|
2022-07-05 05:40:43 +00:00
|
|
|
PayloadManager
|
2022-05-03 14:50:40 +00:00
|
|
|
|
2022-08-19 12:45:50 +00:00
|
|
|
baseAddress *url.URL
|
|
|
|
certPEM []byte
|
|
|
|
serverPK *ecdsa.PublicKey
|
|
|
|
serverMode Mode
|
|
|
|
serverCert *x509.Certificate
|
|
|
|
serverChallenge []byte
|
2022-05-03 14:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:37:53 +00:00
|
|
|
func NewPairingClient(c *ConnectionParams, config *PairingPayloadManagerConfig) (*PairingClient, error) {
|
2022-08-07 22:14:33 +00:00
|
|
|
u, err := c.URL()
|
2022-05-03 14:50:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:14:33 +00:00
|
|
|
serverCert, err := getServerCert(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = verifyCert(serverCert, c.publicKey)
|
2022-05-03 14:50:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-07 22:14:33 +00:00
|
|
|
certPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: serverCert.Raw})
|
2022-05-03 14:50:40 +00:00
|
|
|
|
2022-08-07 22:14:33 +00:00
|
|
|
rootCAs, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-03 14:50:40 +00:00
|
|
|
if ok := rootCAs.AppendCertsFromPEM(certPem); !ok {
|
|
|
|
return nil, fmt.Errorf("failed to append certPem to rootCAs")
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
InsecureSkipVerify: false, // MUST BE FALSE
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:45:50 +00:00
|
|
|
cj, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-21 12:15:39 +00:00
|
|
|
pm, err := NewPairingPayloadManager(c.aesKey, config, logutils.ZapLogger().Named("PairingClient"))
|
2022-06-10 15:32:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PairingClient{
|
2022-08-19 12:45:50 +00:00
|
|
|
Client: &http.Client{Transport: tr, Jar: cj},
|
2022-07-01 15:37:53 +00:00
|
|
|
baseAddress: u,
|
|
|
|
certPEM: certPem,
|
2022-08-07 22:14:33 +00:00
|
|
|
serverCert: serverCert,
|
|
|
|
serverPK: c.publicKey,
|
2022-07-01 15:37:53 +00:00
|
|
|
serverMode: c.serverMode,
|
|
|
|
PayloadManager: pm,
|
2022-05-03 14:50:40 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
|
|
|
|
func (c *PairingClient) PairAccount() error {
|
|
|
|
switch c.serverMode {
|
|
|
|
case Receiving:
|
|
|
|
return c.sendAccountData()
|
|
|
|
case Sending:
|
2022-08-19 12:45:50 +00:00
|
|
|
err := c.getChallenge()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
return c.receiveAccountData()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unrecognised server mode '%d'", c.serverMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PairingClient) sendAccountData() error {
|
2022-08-31 12:47:16 +00:00
|
|
|
err := c.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
c.baseAddress.Path = pairingReceive
|
2022-08-19 12:45:50 +00:00
|
|
|
resp, err := c.Post(c.baseAddress.String(), "application/octet-stream", bytes.NewBuffer(c.PayloadManager.ToSend()))
|
2022-06-10 15:32:15 +00:00
|
|
|
if err != nil {
|
2022-10-21 12:15:39 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err.Error()})
|
2022-06-10 15:32:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:45:50 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2022-10-21 12:15:39 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err.Error()})
|
2022-08-19 12:45:50 +00:00
|
|
|
return fmt.Errorf("status not ok, received '%s'", resp.Status)
|
|
|
|
}
|
|
|
|
|
2022-08-31 14:31:28 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess})
|
2022-10-28 10:30:18 +00:00
|
|
|
|
|
|
|
c.PayloadManager.LockPayload()
|
2022-06-10 15:32:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PairingClient) receiveAccountData() error {
|
|
|
|
c.baseAddress.Path = pairingSend
|
2022-08-19 12:45:50 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, c.baseAddress.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.serverChallenge != nil {
|
|
|
|
ec, err := c.PayloadManager.EncryptPlain(c.serverChallenge)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set(sessionChallenge, base58.Encode(ec))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.Do(req)
|
2022-06-10 15:32:15 +00:00
|
|
|
if err != nil {
|
2022-10-21 12:15:39 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err.Error()})
|
2022-06-10 15:32:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:45:50 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2022-10-21 12:15:39 +00:00
|
|
|
err = fmt.Errorf("status not ok, received '%s'", resp.Status)
|
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err.Error()})
|
|
|
|
return err
|
2022-08-19 12:45:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
payload, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2022-10-21 12:15:39 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferError, Error: err.Error()})
|
2022-06-10 23:03:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-31 14:31:28 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventTransferSuccess})
|
2022-06-10 15:32:15 +00:00
|
|
|
|
2022-08-31 14:31:28 +00:00
|
|
|
err = c.PayloadManager.Receive(payload)
|
|
|
|
if err != nil {
|
2022-10-21 12:15:39 +00:00
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventProcessError, Error: err.Error()})
|
2022-08-31 14:31:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventProcessSuccess})
|
|
|
|
return nil
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|
2022-08-19 12:45:50 +00:00
|
|
|
|
|
|
|
func (c *PairingClient) getChallenge() error {
|
|
|
|
c.baseAddress.Path = pairingChallenge
|
|
|
|
resp, err := c.Get(c.baseAddress.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("status not ok, received '%s'", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.serverChallenge, err = ioutil.ReadAll(resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
2022-08-31 14:01:45 +00:00
|
|
|
|
|
|
|
func StartUpPairingClient(db *multiaccounts.Database, cs, configJSON string) error {
|
|
|
|
var conf PairingPayloadSourceConfig
|
|
|
|
err := json.Unmarshal([]byte(configJSON), &conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ccp := new(ConnectionParams)
|
|
|
|
err = ccp.FromString(cs)
|
2022-08-31 14:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-31 14:01:45 +00:00
|
|
|
|
|
|
|
c, err := NewPairingClient(ccp, &PairingPayloadManagerConfig{db, conf})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PairAccount()
|
|
|
|
}
|