diff --git a/server/server.go b/server/server.go index 79a0f006a..1295b0a19 100644 --- a/server/server.go +++ b/server/server.go @@ -3,16 +3,11 @@ package server import ( "context" "crypto/tls" - "encoding/asn1" "fmt" - "math/big" "net" "net/http" "net/url" - "strings" - "time" - "github.com/btcsuite/btcutil/base58" "go.uber.org/zap" "github.com/status-im/status-go/logutils" @@ -122,77 +117,6 @@ func (s *Server) SetHandlers(handlers HandlerPatternMap) { s.handlers = handlers } -// MakeQRData generates a string required for generating a secure connection to another Status device. -// -// The returned string will look like below: -// - "4FHRnp:H6G:6jpbvo2ucrtrnpXXF4DQYuysh697isH9ppd2aT8uSRDh:eQUriVtGtkWhPJFeLZjF" -// -// Format bytes encoded into a base58 string, delimited by ":" -// - net.IP -// - port -// - ecdsa.PrivateKey D field -// - asn1.Marshal time.Time -func (s *PairingServer) MakeQRData() (string, error) { - switch { - case s.cert == nil: - return "", fmt.Errorf("server has no cert set") - case s.cert.Leaf == nil: - return "", fmt.Errorf("server cert has no Leaf set") - case s.cert.Leaf.NotBefore.IsZero(): - return "", fmt.Errorf("server cert Leaf has a zero value NotBefore") - } - - netIP := net.ParseIP(s.hostname) - if netIP == nil { - return "", fmt.Errorf("invalid ip address given '%s'", s.hostname) - } - - ip := base58.Encode(netIP) - p := base58.Encode(new(big.Int).SetInt64(int64(s.port)).Bytes()) - k := base58.Encode(s.pk.D.Bytes()) - - tb, err := asn1.Marshal(s.cert.Leaf.NotBefore.UTC()) - if err != nil { - return "", err - } - t := base58.Encode(tb) - - return fmt.Sprintf("%s:%s:%s:%s", ip, p, k, t), nil -} - -// ParseQRData parses a QR code data string required for to securely connect to another Status device. -// This function take a connection string generated by MakeQRData and returns a *url.URL and encoded pem.Block -func ParseQRData(data string) (*url.URL, []byte, error) { - // TODO should probably be a function of the future Client struct - sData := strings.Split(data, ":") - if len(sData) != 4 { - return nil, nil, fmt.Errorf("expected data '%s' to have length of '%d', received '%d'", data, 4, len(sData)) - } - - // Decode and build URL data - ip := net.IP(base58.Decode(sData[0])) - p := new(big.Int).SetBytes(base58.Decode(sData[1])).Int64() - u := &url.URL{ - Scheme: "https", - Host: fmt.Sprintf("%s:%d", ip, p), - } - - // Decode and build timestamp data - t := time.Time{} - _, err := asn1.Unmarshal(base58.Decode(sData[3]), &t) - if err != nil { - return nil, nil, err - } - - // Decode and build ecdsa.PrivateKey, generate cert PEM - _, pem, err := GenerateCertFromKey(ToECDSA(base58.Decode(sData[2])), t, ip.String()) - if err != nil { - return nil, nil, err - } - - return u, pem, nil -} - func (s *Server) MakeBaseURL() *url.URL { return &url.URL{ Scheme: "https", diff --git a/server/server_test.go b/server/server_test.go index c9fc0f045..a8edcfea4 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -1,12 +1,8 @@ package server import ( - "crypto/ecdsa" - "crypto/x509" - "encoding/pem" "testing" - "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/suite" ) @@ -17,7 +13,6 @@ func TestServerURLSuite(t *testing.T) { type ServerURLSuite struct { suite.Suite TestKeyComponents - TestCertComponents server *MediaServer serverNoPort *MediaServer @@ -26,10 +21,6 @@ type ServerURLSuite struct { func (s *ServerURLSuite) SetupSuite() { s.SetupKeyComponents(s.T()) - s.SetupCertComponents(s.T()) - - cert, _, err := GenerateCertFromKey(s.PK, s.NotBefore, defaultIP.String()) - s.Require().NoError(err) s.server = &MediaServer{Server: Server{ hostname: defaultIP.String(), @@ -38,47 +29,6 @@ func (s *ServerURLSuite) SetupSuite() { s.serverNoPort = &MediaServer{Server: Server{ hostname: defaultIP.String(), }} - s.pairingServer = &PairingServer{ - Server: Server{cert: &cert, hostname: defaultIP.String()}, - pk: s.PK, - } -} - -func (s *ServerURLSuite) TestServer_MakeQRData() { - qr, err := s.pairingServer.MakeQRData() - s.Require().NoError(err) - - s.Require().Regexp( - "^4FHRnp:[1-9|A-Z|a-z]{1,4}:6jpbvo2ucrtrnpXXF4DQYuysh697isH9ppd2aT8uSRDh:eQUriVtGtkWhPJFeLZjF$", - qr) -} - -func (s *ServerURLSuite) TestServer_ParseQRData() { - u, c, err := ParseQRData("4FHRnp:H6G:6jpbvo2ucrtrnpXXF4DQYuysh697isH9ppd2aT8uSRDh:eQUriVtGtkWhPJFeLZjF") - s.Require().NoError(err) - - s.Require().Equal("https://127.0.0.1:54129", u.String()) - s.Require().Equal(defaultIP.String(), u.Hostname()) - s.Require().Equal("54129", u.Port()) - - // Parse cert PEM into x509 cert - block, _ := pem.Decode(c) - s.Require().NotNil(block) - cert, err := x509.ParseCertificate(block.Bytes) - s.Require().NoError(err) - - // Compare cert values - cl := s.server.cert.Leaf - s.Require().NotEqual(cl.Signature, cert.Signature) - s.Require().Zero(cl.PublicKey.(*ecdsa.PublicKey).X.Cmp(cert.PublicKey.(*ecdsa.PublicKey).X)) - s.Require().Zero(cl.PublicKey.(*ecdsa.PublicKey).Y.Cmp(cert.PublicKey.(*ecdsa.PublicKey).Y)) - s.Require().Equal(cl.Version, cert.Version) - s.Require().Zero(cl.SerialNumber.Cmp(cert.SerialNumber)) - s.Require().Exactly(cl.NotBefore, cert.NotBefore) - s.Require().Exactly(cl.NotAfter, cert.NotAfter) - s.Require().Exactly(cl.IPAddresses, cert.IPAddresses) - - spew.Dump(cl, cert) } func (s *ServerURLSuite) TestServer_MakeBaseURL() {