2023-01-06 12:21:14 +00:00
|
|
|
package pairing
|
2022-06-15 15:36:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-08-07 22:14:33 +00:00
|
|
|
"crypto/elliptic"
|
2022-06-15 15:36:17 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
|
|
|
|
2023-04-17 08:54:03 +00:00
|
|
|
"github.com/status-im/status-go/server/pairing/versioning"
|
2022-06-15 15:36:17 +00:00
|
|
|
)
|
|
|
|
|
2022-10-05 09:57:12 +00:00
|
|
|
const (
|
|
|
|
connectionStringID = "cs"
|
|
|
|
)
|
|
|
|
|
2022-06-15 15:36:17 +00:00
|
|
|
type ConnectionParams struct {
|
2023-04-17 08:54:03 +00:00
|
|
|
version versioning.ConnectionParamVersion
|
2023-08-22 16:18:14 +00:00
|
|
|
netIPs []net.IP
|
2023-03-21 13:08:28 +00:00
|
|
|
port int
|
|
|
|
publicKey *ecdsa.PublicKey
|
|
|
|
aesKey []byte
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
func NewConnectionParams(netIPs []net.IP, port int, publicKey *ecdsa.PublicKey, aesKey []byte) *ConnectionParams {
|
2022-06-15 15:36:17 +00:00
|
|
|
cp := new(ConnectionParams)
|
2023-04-17 08:54:03 +00:00
|
|
|
cp.version = versioning.LatestConnectionParamVer
|
2023-08-22 16:18:14 +00:00
|
|
|
cp.netIPs = netIPs
|
2022-06-15 15:36:17 +00:00
|
|
|
cp.port = port
|
2022-08-07 22:14:33 +00:00
|
|
|
cp.publicKey = publicKey
|
|
|
|
cp.aesKey = aesKey
|
2022-06-15 15:36:17 +00:00
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToString generates a string required for generating a secure connection to another Status device.
|
|
|
|
//
|
|
|
|
// The returned string will look like below:
|
2023-03-21 13:08:28 +00:00
|
|
|
// - "cs2:4FHRnp:H6G:uqnnMwVUfJc2Fkcaojet8F1ufKC3hZdGEt47joyBx9yd:BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6"
|
2022-06-15 15:36:17 +00:00
|
|
|
//
|
|
|
|
// Format bytes encoded into a base58 string, delimited by ":"
|
2022-10-05 09:57:12 +00:00
|
|
|
// - string type identifier
|
2022-06-15 15:36:17 +00:00
|
|
|
// - version
|
|
|
|
// - net.IP
|
2023-08-22 16:18:14 +00:00
|
|
|
// - version 1: a single net.IP
|
|
|
|
// - version 2: array of IPs in next form:
|
|
|
|
// | 1 byte | 4*N bytes | 1 byte | 16*N bytes |
|
|
|
|
// | N | N * IPv4 | M | M * IPv6 |
|
2022-06-15 15:36:17 +00:00
|
|
|
// - port
|
2022-08-07 22:14:33 +00:00
|
|
|
// - ecdsa CompressedPublicKey
|
|
|
|
// - AES encryption key
|
|
|
|
func (cp *ConnectionParams) ToString() string {
|
2022-06-15 15:36:17 +00:00
|
|
|
v := base58.Encode(new(big.Int).SetInt64(int64(cp.version)).Bytes())
|
2023-08-22 16:18:14 +00:00
|
|
|
ips := base58.Encode(SerializeNetIps(cp.netIPs))
|
2022-06-15 15:36:17 +00:00
|
|
|
p := base58.Encode(new(big.Int).SetInt64(int64(cp.port)).Bytes())
|
2022-08-07 22:14:33 +00:00
|
|
|
k := base58.Encode(elliptic.MarshalCompressed(cp.publicKey.Curve, cp.publicKey.X, cp.publicKey.Y))
|
|
|
|
ek := base58.Encode(cp.aesKey)
|
2023-03-23 11:44:15 +00:00
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
return fmt.Sprintf("%s%s:%s:%s:%s:%s", connectionStringID, v, ips, p, k, ek)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SerializeNetIps(ips []net.IP) []byte {
|
|
|
|
var out []byte
|
|
|
|
var ipv4 []net.IP
|
|
|
|
var ipv6 []net.IP
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
if v := ip.To4(); v != nil {
|
|
|
|
ipv4 = append(ipv4, v)
|
|
|
|
} else {
|
|
|
|
ipv6 = append(ipv6, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arr := range [][]net.IP{ipv4, ipv6} {
|
|
|
|
out = append(out, uint8(len(arr)))
|
|
|
|
for _, ip := range arr {
|
|
|
|
out = append(out, ip...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseNetIps(in []byte) ([]net.IP, error) {
|
|
|
|
var out []net.IP
|
|
|
|
|
|
|
|
if len(in) < 1 {
|
|
|
|
return nil, fmt.Errorf("net.ip field is too short: '%d', at least 1 byte required", len(in))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ipLen := range []int{net.IPv4len, net.IPv6len} {
|
|
|
|
|
|
|
|
count := int(in[0])
|
|
|
|
in = in[1:]
|
|
|
|
|
|
|
|
if expectedLen := ipLen * count; len(in) < expectedLen {
|
|
|
|
return nil, fmt.Errorf("net.ip.ip%d field is too short, expected at least '%d' bytes, '%d' bytes found", ipLen, expectedLen, len(in))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
offset := i * ipLen
|
|
|
|
ip := in[offset : ipLen+offset]
|
|
|
|
out = append(out, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
in = in[ipLen*count:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromString parses a connection params string required for to securely connect to another Status device.
|
|
|
|
// This function parses a connection string generated by ToString
|
|
|
|
func (cp *ConnectionParams) FromString(s string) error {
|
2023-02-15 14:42:12 +00:00
|
|
|
|
|
|
|
if len(s) < 2 {
|
2023-08-22 16:18:14 +00:00
|
|
|
return fmt.Errorf("connection string is too short: '%s'", s)
|
2023-02-15 14:42:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 09:57:12 +00:00
|
|
|
if s[:2] != connectionStringID {
|
|
|
|
return fmt.Errorf("connection string doesn't begin with identifier '%s'", connectionStringID)
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:08:28 +00:00
|
|
|
requiredParams := 5
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2022-10-05 09:57:12 +00:00
|
|
|
sData := strings.Split(s[2:], ":")
|
2022-06-15 15:36:17 +00:00
|
|
|
if len(sData) != requiredParams {
|
|
|
|
return fmt.Errorf("expected data '%s' to have length of '%d', received '%d'", s, requiredParams, len(sData))
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:54:03 +00:00
|
|
|
cp.version = versioning.ConnectionParamVersion(new(big.Int).SetBytes(base58.Decode(sData[0])).Int64())
|
2023-08-22 16:18:14 +00:00
|
|
|
|
|
|
|
netIpsBytes := base58.Decode(sData[1])
|
|
|
|
switch cp.version {
|
|
|
|
case versioning.ConnectionParamsV1:
|
|
|
|
if len(netIpsBytes) != net.IPv4len {
|
|
|
|
return fmt.Errorf("invalid IP size: '%d' bytes, expected: '%d' bytes", len(netIpsBytes), net.IPv4len)
|
|
|
|
}
|
|
|
|
cp.netIPs = []net.IP{netIpsBytes}
|
|
|
|
case versioning.ConnectionParamsV2:
|
|
|
|
netIps, err := ParseNetIps(netIpsBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cp.netIPs = netIps
|
|
|
|
}
|
|
|
|
|
2022-06-15 15:36:17 +00:00
|
|
|
cp.port = int(new(big.Int).SetBytes(base58.Decode(sData[2])).Int64())
|
2022-08-07 22:14:33 +00:00
|
|
|
cp.publicKey = new(ecdsa.PublicKey)
|
|
|
|
cp.publicKey.X, cp.publicKey.Y = elliptic.UnmarshalCompressed(elliptic.P256(), base58.Decode(sData[3]))
|
|
|
|
cp.publicKey.Curve = elliptic.P256()
|
|
|
|
cp.aesKey = base58.Decode(sData[4])
|
2022-06-15 15:36:17 +00:00
|
|
|
|
|
|
|
return cp.validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *ConnectionParams) validate() error {
|
|
|
|
err := cp.validateVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cp.validateNetIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cp.validatePort()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:14:33 +00:00
|
|
|
err = cp.validatePublicKey()
|
2022-06-15 15:36:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:08:28 +00:00
|
|
|
return cp.validateAESKey()
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *ConnectionParams) validateVersion() error {
|
2023-04-17 08:54:03 +00:00
|
|
|
if cp.version <= versioning.LatestConnectionParamVer {
|
2022-06-15 15:36:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-17 08:54:03 +00:00
|
|
|
return fmt.Errorf("unsupported version '%d'", cp.version)
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *ConnectionParams) validateNetIP() error {
|
2023-08-22 16:18:14 +00:00
|
|
|
for _, ip := range cp.netIPs {
|
|
|
|
if ok := net.ParseIP(ip.String()); ok == nil {
|
|
|
|
return fmt.Errorf("invalid net ip '%s'", cp.netIPs)
|
|
|
|
}
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *ConnectionParams) validatePort() error {
|
|
|
|
if cp.port > 0 && cp.port < 0x10000 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("port '%d' outside of bounds of 1 - 65535", cp.port)
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:14:33 +00:00
|
|
|
func (cp *ConnectionParams) validatePublicKey() error {
|
2022-06-15 15:36:17 +00:00
|
|
|
switch {
|
2022-08-07 22:14:33 +00:00
|
|
|
case cp.publicKey.Curve == nil, cp.publicKey.Curve != elliptic.P256():
|
|
|
|
return fmt.Errorf("public key Curve not `elliptic.P256`")
|
|
|
|
case cp.publicKey.X == nil, cp.publicKey.X.Cmp(big.NewInt(0)) == 0:
|
2022-06-15 15:36:17 +00:00
|
|
|
return fmt.Errorf("public key X not set")
|
2022-08-07 22:14:33 +00:00
|
|
|
case cp.publicKey.Y == nil, cp.publicKey.Y.Cmp(big.NewInt(0)) == 0:
|
2022-06-15 15:36:17 +00:00
|
|
|
return fmt.Errorf("public key Y not set")
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:14:33 +00:00
|
|
|
func (cp *ConnectionParams) validateAESKey() error {
|
|
|
|
if len(cp.aesKey) != 32 {
|
|
|
|
return fmt.Errorf("AES key invalid length, expect length 32, received length '%d'", len(cp.aesKey))
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:18:14 +00:00
|
|
|
func (cp *ConnectionParams) URL(IPIndex int) (*url.URL, error) {
|
|
|
|
if IPIndex < 0 || IPIndex >= len(cp.netIPs) {
|
|
|
|
return nil, fmt.Errorf("invalid IP index '%d'", IPIndex)
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:50:40 +00:00
|
|
|
err := cp.validate()
|
|
|
|
if err != nil {
|
2022-08-07 22:14:33 +00:00
|
|
|
return nil, err
|
2022-05-03 14:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:36:17 +00:00
|
|
|
u := &url.URL{
|
|
|
|
Scheme: "https",
|
2023-08-22 16:18:14 +00:00
|
|
|
Host: fmt.Sprintf("%s:%d", cp.netIPs[IPIndex], cp.port),
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
2022-08-07 22:14:33 +00:00
|
|
|
return u, nil
|
2022-06-15 15:36:17 +00:00
|
|
|
}
|
2023-02-15 14:42:12 +00:00
|
|
|
|
|
|
|
func ValidateConnectionString(cs string) error {
|
|
|
|
ccp := ConnectionParams{}
|
|
|
|
err := ccp.FromString(cs)
|
|
|
|
return err
|
|
|
|
}
|