Skip to content

Commit

Permalink
Added Periodic tasks function and fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tfn18001 committed Apr 25, 2022
1 parent 2101c7b commit 91be863
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
35 changes: 30 additions & 5 deletions CTng/gossip/gossip_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,39 @@ func Verify_gossip_pom(g Gossip_object, c *crypto.CryptoConfig) error {
if g.Type == GOSSIP_POM {
//gossip pom refers to Pom generated due to conflicting information
//From Finn's gossiper design, gossip poms are defaulted to have 2 non empty fields for signature and paypload
var err1, err2 error
if g.Signature[1] != "" && g.Payload[1] != "" {
if g.Signature[0] != g.Signature[1] {
//that means there are conflicting information
//the PoM is valid and the verification went through
sig1, sigerr1 := crypto.ThresholdSigFromString(g.Signature[0])
sig2, sigerr2 := crypto.ThresholdSigFromString(g.Signature[1])
err1 := c.ThresholdVerify(g.Payload[0], sig1)
err2 := c.ThresholdVerify(g.Payload[1], sig2)
//the PoM is valid and the verification went through.

// Next we need to figure out what type of signature is being used.
// First: try ThresholdSignature
thresSig1, sigerr1 := crypto.ThresholdSigFromString(g.Signature[0])
thresSig2, sigerr2 := crypto.ThresholdSigFromString(g.Signature[1])
// Verify the signatures were made successfully
if sigerr1 != nil || sigerr2 != nil && thresSig1.Sign != thresSig2.Sign {
err1 = c.ThresholdVerify(g.Payload[0], thresSig1)
err2 = c.ThresholdVerify(g.Payload[1], thresSig2)
} else {
// Second: try SigFragment
fragsig1, sigerr1 := crypto.SigFragmentFromString(g.Signature[0])
fragsig2, sigerr2 := crypto.SigFragmentFromString(g.Signature[1])
// Verify the signatures were made successfully
if sigerr1 != nil || sigerr2 != nil && !fragsig1.Sign.IsEqual(fragsig2.Sign) {
err1 = c.FragmentVerify(g.Payload[0], fragsig1)
err2 = c.FragmentVerify(g.Payload[1], fragsig2)
} else {
// Try RSASig
rsaSig1, sigerr1 := crypto.RSASigFromString(g.Signature[0])
rsaSig2, sigerr2 := crypto.RSASigFromString(g.Signature[1])
// Verify the signatures were made successfully
if sigerr1 != nil || sigerr2 != nil {
err1 = c.Verify([]byte(g.Payload[0]), rsaSig1)
err2 = c.Verify([]byte(g.Payload[1]), rsaSig2)
}
}
}
if err1 == nil && err2 == nil {
return nil
} else {
Expand Down
35 changes: 31 additions & 4 deletions CTng/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,17 @@ func QueryAuthorities(c *MonitorContext) {
//should be a monitor functionality
func AccuseEntity(c *MonitorContext, Accused string) {
// this should be a method for the monitor
// psedo code for now
// verify we havent accused during this MMD
// if we have, dont accuse again. This is a temporary fix.
if c.HasAccused {
// Could 'queue up' the accusation to try again next period with this line of code:
// time.AfterFunc(time.Duration(c.Config.Public.MMD)*time.Second, func() { AccuseEntity(c, Accused) })
// for now, to prevent flooding with accusations, we will just not accuse again.
return
}

msg := Accused
signature, _ := c.Config.Crypto.ThresholdSign(msg)

var sigarray [2]string
sigarray[0] = signature.String()
sigarray[1] = ""
Expand All @@ -109,7 +116,7 @@ func AccuseEntity(c *MonitorContext, Accused string) {
Timestamp: gossip.GetCurrentTimestamp(),
Payload: payloadarray,
}
// c.HasAccused[Accused] = true
c.HasAccused = true
Send_to_gossiper(c, accusation)
}

Expand All @@ -127,7 +134,7 @@ func Send_to_gossiper(c *MonitorContext, g gossip.Gossip_object) {
// Close the response, mentioned by http.Post
// Alernatively, we could return the response from this function.
defer resp.Body.Close()
fmt.Println("Owner responded with " + resp.Status)
fmt.Println(util.BLUE+"Sent Object to Gossiper, Recieved "+resp.Status, util.RESET)
}

}
Expand Down Expand Up @@ -160,3 +167,23 @@ func IsAuthority(c *MonitorContext, authURL string) bool {
}
return false
}

func PeriodicTasks(c *MonitorContext) {
// Immediately queue up the next task to run at next MMD
f := func() {
PeriodicTasks(c)
}

time.AfterFunc(time.Duration(c.Config.Public.MMD)*time.Second, f)
// Run the periodic tasks.
fmt.Println(util.GREEN + "Querying Loggers+CAs" + util.RESET)

// Reset accusation
c.HasAccused = false

c.SaveStorage()
//TODO: Switch storage directory to a new file

QueryLoggers(c)
// monitor.QueryAuthorities(c)
}
7 changes: 4 additions & 3 deletions CTng/monitor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type MonitorContext struct {
StorageDirectory string

// The below could be used to prevent a Monitor from sending duplicate Accusations,
// should be reset each MMD
// HasAccused map[string]bool
Client *http.Client
// Currently, if a monitor accuses two entities in the same Period, it will trigger a gossip PoM.
// Therefore, a monitor can only accuse once per Period. I believe this is a temporary solution.
HasAccused bool
Client *http.Client
}

func (c *MonitorContext) SaveStorage() error {
Expand Down
3 changes: 1 addition & 2 deletions CTng/server/Gossiper_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func handleGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http.Requ
err := json.NewDecoder(r.Body).Decode(&gossip_obj)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Verify the object is valid.
err = gossip_obj.Verify(c.Config.Crypto)
Expand All @@ -134,8 +135,6 @@ func handleGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http.Requ
err := gossip.ProcessDuplicateObject(c, gossip_obj, stored_obj)
if err != nil {
http.Error(w, err.Error(), http.StatusOK)
} else {
http.Error(w, "Duplicate object.", http.StatusOK)
}
http.Error(w, "Recieved Duplicate Object.", http.StatusOK)
return
Expand Down
14 changes: 2 additions & 12 deletions CTng/server/Monitor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package server
import (
"CTng/gossip"
"CTng/monitor"
"CTng/util"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"strings"
"time"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -186,15 +184,7 @@ func StartMonitorServer(c *monitor.MonitorContext) {
Transport: tr,
}
// Run a go routine to handle tasks that must occur every MMD
f := func() {
for {
fmt.Println(util.GREEN + "Querying Loggers+CAs" + util.RESET)
monitor.QueryLoggers(c)
// monitor.QueryAuthorities(c)
time.Sleep(time.Duration(c.Config.Public.MMD) * time.Second)
}
}
go f()
// Start HTTP server loop
go monitor.PeriodicTasks(c)
// Start HTTP server loop on the main thread
handleMonitorRequests(c)
}

0 comments on commit 91be863

Please sign in to comment.