Skip to content

Commit

Permalink
/protocol organizational refactor
Browse files Browse the repository at this point in the history
TODO:

* Use auditor.Auditor in /client and /auditlog
  • Loading branch information
vqhuy committed Aug 22, 2017
1 parent e27b750 commit 9c39ca2
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 282 deletions.
7 changes: 4 additions & 3 deletions client/coniksclient/internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/coniks-sys/coniks-go/client"
"github.com/coniks-sys/coniks-go/keyserver/testutil"
p "github.com/coniks-sys/coniks-go/protocol"
pclient "github.com/coniks-sys/coniks-go/protocol/client"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func init() {
func run(cmd *cobra.Command) {
isDebugging, _ := strconv.ParseBool(cmd.Flag("debug").Value.String())
conf := loadConfigOrExit(cmd)
cc := p.NewCC(nil, true, conf.SigningPubKey)
cc := pclient.New(nil, true, conf.SigningPubKey)

state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
Expand Down Expand Up @@ -109,7 +110,7 @@ func run(cmd *cobra.Command) {
}
}

func register(cc *p.ConsistencyChecks, conf *client.Config, name string, key string) string {
func register(cc *pclient.ConsistencyChecks, conf *client.Config, name string, key string) string {
req, err := client.CreateRegistrationMsg(name, []byte(key))
if err != nil {
return ("Couldn't marshal registration request!")
Expand Down Expand Up @@ -167,7 +168,7 @@ func register(cc *p.ConsistencyChecks, conf *client.Config, name string, key str
return ""
}

func keyLookup(cc *p.ConsistencyChecks, conf *client.Config, name string) string {
func keyLookup(cc *pclient.ConsistencyChecks, conf *client.Config, name string) string {
req, err := client.CreateKeyLookupMsg(name)
if err != nil {
return ("Couldn't marshal key lookup request!")
Expand Down
3 changes: 2 additions & 1 deletion client/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/coniks-sys/coniks-go/keyserver"
"github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/directory"
)

func TestUnmarshalErrorResponse(t *testing.T) {
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestUnmarshalMalformedErrorResponse(t *testing.T) {
}

func TestUnmarshalSampleMessage(t *testing.T) {
d, _ := protocol.NewTestDirectory(t, true)
d, _ := directory.NewTestDirectory(t, true)
res, _ := d.Register(&protocol.RegistrationRequest{
Username: "alice",
Key: []byte("key")})
Expand Down
5 changes: 3 additions & 2 deletions keyserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/coniks-sys/coniks-go/crypto/sign"
"github.com/coniks-sys/coniks-go/crypto/vrf"
"github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/directory"
"github.com/coniks-sys/coniks-go/utils"
)

Expand Down Expand Up @@ -84,7 +85,7 @@ type ConiksServer struct {
logger *utils.Logger

sync.RWMutex
dir *protocol.ConiksDirectory
dir *directory.ConiksDirectory

stop chan struct{}
waitStop sync.WaitGroup
Expand Down Expand Up @@ -145,7 +146,7 @@ func NewConiksServer(conf *ServerConfig) *ConiksServer {
// create server instance
server := new(ConiksServer)
server.logger = utils.NewLogger(conf.Logger)
server.dir = protocol.NewDirectory(
server.dir = directory.New(
conf.Policies.EpochDeadline,
conf.Policies.vrfKey,
conf.Policies.signKey,
Expand Down
66 changes: 35 additions & 31 deletions protocol/auditlog.go → protocol/auditlog/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
// An audit log is a mirror of many CONIKS key directories' STR history,
// allowing CONIKS clients to audit the CONIKS directories.

package protocol
package auditlog

import (
"github.com/coniks-sys/coniks-go/crypto"
"github.com/coniks-sys/coniks-go/crypto/sign"
p "github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/auditor"
)

type directoryHistory struct {
*AudState
*auditor.AudState
addr string
snapshots map[uint64]*DirSTR
snapshots map[uint64]*p.DirSTR
}

// caller validates that initSTR is for epoch 0
func newDirectoryHistory(addr string, signKey sign.PublicKey, initSTR *DirSTR) *directoryHistory {
a := NewAuditor(signKey, initSTR)
func newDirectoryHistory(addr string,
signKey sign.PublicKey,
initSTR *p.DirSTR) *directoryHistory {
a := auditor.New(signKey, initSTR)
h := &directoryHistory{
AudState: a,
addr: addr,
snapshots: make(map[uint64]*DirSTR),
snapshots: make(map[uint64]*p.DirSTR),
}
h.updateVerifiedSTR(initSTR)
return h
Expand All @@ -40,7 +44,7 @@ type ConiksAuditLog map[[crypto.HashSizeByte]byte]*directoryHistory

// updateVerifiedSTR inserts a new range of STRs into a directory history;
// assumes the STRs have been validated by the caller
func (h *directoryHistory) updateVerifiedSTR(newVerified *DirSTR) {
func (h *directoryHistory) updateVerifiedSTR(newVerified *p.DirSTR) {
h.Update(newVerified)
h.snapshots[newVerified.Epoch] = newVerified
}
Expand All @@ -54,15 +58,15 @@ func (h *directoryHistory) updateVerifiedSTR(newVerified *DirSTR) {
// finally updates the snapshots if the checks pass.
// Audit() is called when an auditor receives new STRs
// from a directory.
func (h *directoryHistory) Audit(msg *Response) error {
func (h *directoryHistory) Audit(msg *p.Response) error {
// TODO: Implement as part of the auditor-server protocol
return CheckPassed
return p.CheckPassed
}

// NewAuditLog constructs a new ConiksAuditLog. It creates an empty
// New constructs a new ConiksAuditLog. It creates an empty
// log; the auditor will add an entry for each CONIKS directory
// the first time it observes an STR for that directory.
func NewAuditLog() ConiksAuditLog {
func New() ConiksAuditLog {
return make(map[[crypto.HashSizeByte]byte]*directoryHistory)
}

Expand Down Expand Up @@ -96,20 +100,20 @@ func (l ConiksAuditLog) get(dirInitHash [crypto.HashSizeByte]byte) (*directoryHi
// masomel: will probably want to write a more generic function
// for "catching up" on a history in case an auditor misses epochs
func (l ConiksAuditLog) Insert(addr string, signKey sign.PublicKey,
snaps []*DirSTR) error {
snaps []*p.DirSTR) error {
// make sure we're getting an initial STR at the very least
if len(snaps) < 1 || snaps[0].Epoch != 0 {
return ErrMalformedDirectoryMessage
return p.ErrMalformedDirectoryMessage
}

// compute the hash of the initial STR
dirInitHash := ComputeDirectoryIdentity(snaps[0])
dirInitHash := auditor.ComputeDirectoryIdentity(snaps[0])

// error if we want to create a new entry for a directory
// we already know
h, ok := l.get(dirInitHash)
if ok {
return ErrAuditLog
return p.ErrAuditLog
}

// create the new directory history
Expand All @@ -124,14 +128,14 @@ func (l ConiksAuditLog) Insert(addr string, signKey sign.PublicKey,
for i := 1; i < len(snaps); i++ {
str := snaps[i]
if str == nil {
return ErrMalformedDirectoryMessage
return p.ErrMalformedDirectoryMessage
}

// verify the consistency of each new STR before inserting
// into the audit log
if err := h.verifySTRConsistency(h.VerifiedSTR(), str); err != nil {
return err
}
// if err := h.verifySTRConsistency(h.VerifiedSTR(), str); err != nil {
// return err
// }

h.updateVerifiedSTR(snaps[i])
}
Expand All @@ -150,18 +154,18 @@ func (l ConiksAuditLog) Insert(addr string, signKey sign.PublicKey,
// Update() returns ErrAuditLog if the audit log doesn't contain an
// entry for dirInitHash
// FIXME: pass Response message as param
func (l ConiksAuditLog) Update(dirInitHash [crypto.HashSizeByte]byte, newSTR *DirSTR) error {
func (l ConiksAuditLog) Update(dirInitHash [crypto.HashSizeByte]byte, newSTR *p.DirSTR) error {
// error if we want to update the entry for an addr we don't know
h, ok := l.get(dirInitHash)
if !ok {
return ErrAuditLog
return p.ErrAuditLog
}

// FIXME: remove this check --> caller calls Audit() before this
// function
if err := h.verifySTRConsistency(h.VerifiedSTR(), newSTR); err != nil {
return err
}
// if err := h.verifySTRConsistency(h.VerifiedSTR(), newSTR); err != nil {
// return err
// }

// update the latest STR
// FIXME: use STR slice from Response msg
Expand All @@ -187,25 +191,25 @@ func (l ConiksAuditLog) Update(dirInitHash [crypto.HashSizeByte]byte, newSTR *Di
// If the auditor doesn't have any history entries for the requested CONIKS
// directory, GetObservedSTRs() returns a
// message.NewErrorResponse(ReqUnknownDirectory) tuple.
func (l ConiksAuditLog) GetObservedSTRs(req *AuditingRequest) (*Response,
ErrorCode) {
func (l ConiksAuditLog) GetObservedSTRs(req *p.AuditingRequest) (*p.Response,
p.ErrorCode) {
// make sure we have a history for the requested directory in the log
h, ok := l.get(req.DirInitSTRHash)
if !ok {
return NewErrorResponse(ReqUnknownDirectory), ReqUnknownDirectory
return p.NewErrorResponse(p.ReqUnknownDirectory), p.ReqUnknownDirectory
}

// make sure the request is well-formed
if req.EndEpoch > h.VerifiedSTR().Epoch || req.StartEpoch > req.EndEpoch {
return NewErrorResponse(ErrMalformedClientMessage),
ErrMalformedClientMessage
return p.NewErrorResponse(p.ErrMalformedClientMessage),
p.ErrMalformedClientMessage
}

var strs []*DirSTR
var strs []*p.DirSTR
for ep := req.StartEpoch; ep <= req.EndEpoch; ep++ {
str := h.snapshots[ep]
strs = append(strs, str)
}

return NewSTRHistoryRange(strs)
return p.NewSTRHistoryRange(strs)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package protocol
package auditlog

import (
"github.com/coniks-sys/coniks-go/crypto"
"testing"

"github.com/coniks-sys/coniks-go/crypto"
. "github.com/coniks-sys/coniks-go/protocol"
. "github.com/coniks-sys/coniks-go/protocol/auditor"
)

func TestInsertEmptyHistory(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions protocol/auditlog/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package auditlog

import (
"testing"

p "github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/directory"
)

// NewTestAuditLog creates a ConiksAuditLog and corresponding
// ConiksDirectory used for testing auditor-side CONIKS operations.
// The new audit log can be initialized with the number of epochs
// indicating the length of the directory history with which to
// initialize the log; if numEpochs > 0, the history contains numEpochs+1
// STRs as it always includes the STR after the last directory update
func NewTestAuditLog(t *testing.T, numEpochs int) (
*directory.ConiksDirectory, ConiksAuditLog, []*p.DirSTR) {
d, pk := directory.NewTestDirectory(t, true)
aud := New()

var hist []*p.DirSTR
for ep := 0; ep < numEpochs; ep++ {
hist = append(hist, d.LatestSTR())
d.Update()
}
// always include the actual latest STR
hist = append(hist, d.LatestSTR())

err := aud.Insert("test-server", pk, hist)
if err != nil {
t.Fatalf("Error inserting a new history with %d STRs", numEpochs+1)
}

return d, aud, hist
}
Loading

0 comments on commit 9c39ca2

Please sign in to comment.