Browse Source

tracking mod

0xdcarns 3 years ago
parent
commit
aa22afeb95
6 changed files with 15 additions and 5 deletions
  1. 3 1
      controllers/node_grpc.go
  2. 3 1
      database/database.go
  3. 4 3
      logic/traffic.go
  4. 3 0
      models/structs.go
  5. 1 0
      mq/util.go
  6. 1 0
      netclient/functions/join.go

+ 3 - 1
controllers/node_grpc.go

@@ -77,14 +77,16 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
 	}
 	// TODO consolidate functionality around files
 	node.NetworkSettings.DefaultServerAddrs = serverAddrs
-	key, keyErr := logic.RetrievePublicTrafficKey()
+	key, mod, keyErr := logic.RetrievePublicTrafficKey()
 	if keyErr != nil {
 		logger.Log(0, "error retrieving key: ", keyErr.Error())
 		return nil, keyErr
 	}
+	key.N = &mod
 
 	node.TrafficKeys = models.TrafficKeys{
 		Mine:   node.TrafficKeys.Mine,
+		Mod:    node.TrafficKeys.Mod,
 		Server: key,
 	}
 

+ 3 - 1
database/database.go

@@ -5,6 +5,7 @@ import (
 	"crypto/rsa"
 	"encoding/json"
 	"errors"
+	"fmt"
 	"time"
 
 	"github.com/google/uuid"
@@ -211,8 +212,9 @@ func initializeUUID() error {
 		return keyErr
 	}
 	var rsaPublicKey = &rsaPrivKey.PublicKey
+	fmt.Printf("found modulus: %d \n", rsaPublicKey.N)
 
-	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKeyPriv: *rsaPrivKey, TrafficKeyPub: *rsaPublicKey}
+	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKeyPriv: *rsaPrivKey, TrafficKeyPub: *rsaPublicKey, PubMod: *rsaPublicKey.N}
 	telJSON, err := json.Marshal(&telemetry)
 	if err != nil {
 		return err

+ 4 - 3
logic/traffic.go

@@ -3,6 +3,7 @@ package logic
 import (
 	"crypto/rsa"
 	"fmt"
+	"math/big"
 )
 
 // RetrievePrivateTrafficKey - retrieves private key of server
@@ -17,12 +18,12 @@ func RetrievePrivateTrafficKey() (rsa.PrivateKey, error) {
 }
 
 // RetrievePublicTrafficKey - retrieves public key of server
-func RetrievePublicTrafficKey() (rsa.PublicKey, error) {
+func RetrievePublicTrafficKey() (rsa.PublicKey, big.Int, error) {
 	var telRecord, err = fetchTelemetryRecord()
 	if err != nil {
-		return rsa.PublicKey{}, err
+		return rsa.PublicKey{}, big.Int{}, err
 	}
 	fmt.Printf("fetched pub key %v \n", telRecord.TrafficKeyPub)
 
-	return telRecord.TrafficKeyPub, nil
+	return telRecord.TrafficKeyPub, telRecord.PubMod, nil
 }

+ 3 - 0
models/structs.go

@@ -2,6 +2,7 @@ package models
 
 import (
 	"crypto/rsa"
+	"math/big"
 
 	jwt "github.com/golang-jwt/jwt/v4"
 )
@@ -174,6 +175,7 @@ type Telemetry struct {
 	LastSend       int64          `json:"lastsend" bson:"lastsend"`
 	TrafficKeyPriv rsa.PrivateKey `json:"traffickeypriv" bson:"traffickeypriv"`
 	TrafficKeyPub  rsa.PublicKey  `json:"traffickeypub" bson:"traffickeypub"`
+	PubMod         big.Int        `json:"pubmod" bson:"pubmod"`
 }
 
 // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not
@@ -185,5 +187,6 @@ type ServerAddr struct {
 // TrafficKeys - struct to hold public keys
 type TrafficKeys struct {
 	Mine   rsa.PublicKey `json:"mine" bson:"mine" yaml:"mine"`
+	Mod    big.Int       `json:"mod" bson:"mod" yaml:"mod"`
 	Server rsa.PublicKey `json:"server" bson:"server" yaml:"server"`
 }

+ 1 - 0
mq/util.go

@@ -18,6 +18,7 @@ func decryptMsg(msg []byte) ([]byte, error) {
 
 func encrypt(node *models.Node, dest string, msg []byte) ([]byte, error) {
 	fmt.Printf("original length: %d \n", len(msg))
+	node.TrafficKeys.Mine.N = &node.TrafficKeys.Mod
 	encrypted := ncutils.BuildMessage(msg, &node.TrafficKeys.Mine)
 	if encrypted == "" {
 		return nil, fmt.Errorf("could not encrypt message")

+ 1 - 0
netclient/functions/join.go

@@ -137,6 +137,7 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error {
 		UDPHolePunch:        cfg.Node.UDPHolePunch,
 		TrafficKeys: models.TrafficKeys{
 			Mine:   rsaPrivKey.PublicKey,
+			Mod:    *rsaPrivKey.PublicKey.N,
 			Server: rsa.PublicKey{},
 		},
 	}