Skip to content

Commit

Permalink
ct: shift to a common ct.TimestampToTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddrysdale committed Mar 12, 2018
1 parent c6fb9ab commit 9b00c13
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
12 changes: 3 additions & 9 deletions client/ctclient/ctclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ var (
leafHash = flag.String("leaf_hash", "", "Leaf hash to retrieve (as hex string)")
)

func ctTimestampToTime(ts uint64) time.Time {
secs := int64(ts / 1000)
msecs := int64(ts % 1000)
return time.Unix(secs, msecs*1000000)
}

func signatureToString(signed *ct.DigitallySigned) string {
return fmt.Sprintf("Signature: Hash=%v Sign=%v Value=%x", signed.Algorithm.Hash, signed.Algorithm.Signature, signed.Signature)
}
Expand All @@ -68,7 +62,7 @@ func getSTH(ctx context.Context, logClient *client.LogClient) {
log.Fatal(err)
}
// Display the STH
when := ctTimestampToTime(sth.Timestamp)
when := ct.TimestampToTime(sth.Timestamp)
fmt.Printf("%v (timestamp %d): Got STH for %v log (size=%d) at %v, hash %x\n", when, sth.Timestamp, sth.Version, sth.TreeSize, *logURI, sth.SHA256RootHash)
fmt.Printf("%v\n", signatureToString(&sth.TreeHeadSignature))
}
Expand Down Expand Up @@ -128,7 +122,7 @@ func addChain(ctx context.Context, logClient *client.LogClient) {
leafHash := sha256.Sum256(append([]byte{merkletree.LeafPrefix}, leafData...))

// Display the SCT
when := ctTimestampToTime(sct.Timestamp)
when := ct.TimestampToTime(sct.Timestamp)
fmt.Printf("Uploaded chain of %d certs to %v log at %v, timestamp: %d (%v)\n", len(chain), sct.SCTVersion, *logURI, sct.Timestamp, when)
fmt.Printf("LogID: %x\n", sct.LogID.KeyID[:])
fmt.Printf("LeafHash: %x\n", leafHash)
Expand Down Expand Up @@ -164,7 +158,7 @@ func getEntries(ctx context.Context, logClient *client.LogClient) {
}
for _, entry := range entries {
ts := entry.Leaf.TimestampedEntry
when := ctTimestampToTime(ts.Timestamp)
when := ct.TimestampToTime(ts.Timestamp)
fmt.Printf("Index=%d Timestamp=%v ", entry.Index, when)
switch ts.EntryType {
case ct.X509LogEntryType:
Expand Down
11 changes: 3 additions & 8 deletions loglist/findlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/golang/glog"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/loglist"
)

Expand All @@ -50,12 +51,6 @@ func readPossibleURL(target string) ([]byte, error) {
return ioutil.ReadAll(rsp.Body)
}

func ctTimestampToTime(ts int) time.Time {
secs := int64(ts / 1000)
msecs := int64(ts % 1000)
return time.Unix(secs, msecs*1000000)
}

func main() {
flag.Parse()

Expand Down Expand Up @@ -93,13 +88,13 @@ func main() {
if log.FinalSTH != nil {
fmt.Printf(" FinalSTH:\n")
fmt.Printf(" TreeSize: %d\n", log.FinalSTH.TreeSize)
when := ctTimestampToTime(log.FinalSTH.Timestamp)
when := ct.TimestampToTime(uint64(log.FinalSTH.Timestamp))
fmt.Printf(" Timestamp: %d (%v)\n", log.FinalSTH.Timestamp, when)
fmt.Printf(" SHA256RootHash: %x\n", log.FinalSTH.SHA256RootHash)
fmt.Printf(" TreeHeadSignature: %x\n", log.FinalSTH.TreeHeadSignature)
}
if log.DisqualifiedAt > 0 {
when := ctTimestampToTime(log.DisqualifiedAt)
when := ct.TimestampToTime(uint64(log.DisqualifiedAt))
fmt.Printf(" Disqualified at: %v (%d)\n", when, log.DisqualifiedAt)
}
if log.DNSAPIEndpoint != "" {
Expand Down
9 changes: 9 additions & 0 deletions serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/google/certificate-transparency-go/tls"
"github.com/google/certificate-transparency-go/x509"
Expand Down Expand Up @@ -253,3 +254,11 @@ func LogEntryFromLeaf(index int64, leafEntry *LeafEntry) (*LogEntry, error) {
// err may hold a x509.NonFatalErrors object.
return &entry, err
}

// TimestampToTime converts a timestamp in the style of RFC 6962 (milliseconds
// since UNIX epoch) to a Go Time.
func TimestampToTime(ts uint64) time.Time {
secs := int64(ts / 1000)
msecs := int64(ts % 1000)
return time.Unix(secs, msecs*1000000)
}

0 comments on commit 9b00c13

Please sign in to comment.