From 453a0c38516496052c5f570691c74516c8675e2d Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Tue, 16 Mar 2021 16:29:24 +0100 Subject: added additional basic client commands --- client/cmd/get-entries/main.go | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 client/cmd/get-entries/main.go (limited to 'client/cmd/get-entries') diff --git a/client/cmd/get-entries/main.go b/client/cmd/get-entries/main.go new file mode 100644 index 0000000..f32fdbf --- /dev/null +++ b/client/cmd/get-entries/main.go @@ -0,0 +1,83 @@ +package main + +import ( + "context" + "flag" + "fmt" + + "encoding/base64" + + "github.com/golang/glog" + "github.com/google/trillian/merkle/rfc6962" + "github.com/system-transparency/stfe/client" + "github.com/system-transparency/stfe/types" +) + +var ( + start = flag.Uint64("start", 0, "inclusive start index to download") + end = flag.Uint64("end", 0, "inclusive stop index to download") +) + +func main() { + flag.Parse() + defer glog.Flush() + + client, err := client.NewClientFromFlags() + if err != nil { + glog.Errorf("NewClientFromFlags: %v", err) + return + } + items, err := getRange(client, *start, *end) + if err != nil { + glog.Errorf("getRange: %v", err) + return + } + if err := printRange(items); err != nil { + glog.Errorf("printRange: %v", err) + return + } +} + +func getRange(client *client.Client, start, end uint64) ([]*types.StItem, error) { + items := make([]*types.StItem, 0, end-start+1) + for len(items) != cap(items) { + rsp, err := client.GetEntries(context.Background(), start, end) + if err != nil { + return nil, fmt.Errorf("fetching entries failed: %v", err) + } + items = append(items, rsp...) + start += uint64(len(rsp)) + } + return items, nil +} + +func printRange(items []*types.StItem) error { + for i, item := range items { + var status string + msg, err := types.Marshal(item.SignedChecksumV1.Data) + if err != nil { + return fmt.Errorf("Marshal data failed: %v", err) + } + sig := item.SignedChecksumV1.Signature.Signature + namespace := &item.SignedChecksumV1.Signature.Namespace + if err := namespace.Verify(msg, sig); err != nil { + status = "unverified signature" + } else { + status = "verified signature" + } + serializedNamespace, err := types.Marshal(*namespace) + if err != nil { + return fmt.Errorf("Marshal namespace failed: %v", err) + } + serializedLeaf, err := types.Marshal(*item) + if err != nil { + return fmt.Errorf("Marshal item on index %d: %v", *start+uint64(i), err) + } + fmt.Printf("Index(%d) - %s\n", *start+uint64(i), status) + fmt.Printf("-> Namespace: %s\n", base64.StdEncoding.EncodeToString(serializedNamespace)) + fmt.Printf("-> Identifier: %s\n", string(item.SignedChecksumV1.Data.Identifier)) + fmt.Printf("-> Checksum: %s\n", base64.StdEncoding.EncodeToString(item.SignedChecksumV1.Data.Checksum)) + fmt.Printf("-> Leaf hash: %s\n", base64.StdEncoding.EncodeToString(rfc6962.DefaultHasher.HashLeaf(serializedLeaf))) + } + return nil +} -- cgit v1.2.3