2023-01-06 12:21:14 +00:00
|
|
|
package pairing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-21 12:36:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestPairingServerComponents struct {
|
|
|
|
EphemeralPK *ecdsa.PrivateKey
|
|
|
|
EphemeralAES []byte
|
2023-08-22 16:18:14 +00:00
|
|
|
IPAddresses []net.IP
|
2023-01-06 12:21:14 +00:00
|
|
|
Cert tls.Certificate
|
2023-03-23 11:44:15 +00:00
|
|
|
SS *SenderServer
|
|
|
|
RS *ReceiverServer
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tpsc *TestPairingServerComponents) SetupPairingServerComponents(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Get 4 key components for tls.cert generation
|
|
|
|
// 1) Ephemeral private key
|
|
|
|
tpsc.EphemeralPK, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2) AES encryption key
|
|
|
|
tpsc.EphemeralAES, err = common.MakeECDHSharedKey(tpsc.EphemeralPK, &tpsc.EphemeralPK.PublicKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
// 3) Device IP address
|
|
|
|
tpsc.IPAddresses, err = server.GetLocalAddressesForPairingServer()
|
2023-01-06 12:21:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Generate tls.Certificate and Server
|
2023-08-22 16:18:14 +00:00
|
|
|
tpsc.Cert, _, err = GenerateCertFromKey(tpsc.EphemeralPK, time.Now(), tpsc.IPAddresses, []string{})
|
2023-01-06 12:21:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
sc := &ServerConfig{
|
2023-08-22 16:18:14 +00:00
|
|
|
PK: &tpsc.EphemeralPK.PublicKey,
|
|
|
|
EK: tpsc.EphemeralAES,
|
|
|
|
Cert: &tpsc.Cert,
|
|
|
|
IPAddresses: tpsc.IPAddresses,
|
|
|
|
ListenIP: net.IPv4zero,
|
2023-03-23 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tpsc.SS, err = NewSenderServer(nil, &SenderServerConfig{ServerConfig: sc, SenderConfig: &SenderConfig{}})
|
|
|
|
require.NoError(t, err)
|
2024-08-14 20:10:19 +00:00
|
|
|
tpsc.RS, err = NewReceiverServer(nil, &ReceiverServerConfig{
|
|
|
|
ServerConfig: sc,
|
|
|
|
ReceiverConfig: &ReceiverConfig{},
|
|
|
|
})
|
2023-01-06 12:21:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
type MockPayloadReceiver struct {
|
|
|
|
encryptor *PayloadEncryptor
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
func NewMockPayloadReceiver(aesKey []byte) *MockPayloadReceiver {
|
|
|
|
return &MockPayloadReceiver{NewPayloadEncryptor(aesKey)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockPayloadReceiver) Receive(data []byte) error {
|
|
|
|
return m.encryptor.decrypt(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockPayloadReceiver) Received() []byte {
|
|
|
|
return m.encryptor.getDecrypted()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockPayloadReceiver) LockPayload() {}
|
2023-01-06 12:21:14 +00:00
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
type MockPayloadMounter struct {
|
|
|
|
encryptor *PayloadEncryptor
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
func NewMockPayloadMounter(aesKey []byte) *MockPayloadMounter {
|
|
|
|
return &MockPayloadMounter{NewPayloadEncryptor(aesKey)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockPayloadMounter) Mount() error {
|
2023-01-06 12:21:14 +00:00
|
|
|
// Make a random payload
|
|
|
|
data := make([]byte, 32)
|
|
|
|
_, err := rand.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
return m.encryptor.encrypt(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockPayloadMounter) ToSend() []byte {
|
|
|
|
return m.encryptor.getEncrypted()
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:44:15 +00:00
|
|
|
func (m *MockPayloadMounter) LockPayload() {
|
|
|
|
m.encryptor.lockPayload()
|
2023-01-06 12:21:14 +00:00
|
|
|
}
|