-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
121 lines (103 loc) · 2.97 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"errors"
"os"
"github.com/zmap/zgrab/ztools/zct"
"github.com/zmap/zgrab/ztools/zct/client"
)
var (
// ErrTreeHead if we can't get the tree head
ErrTreeHead = errors.New("Error to get tree head")
// ErrLogEntries if there's an issue getting log entries
ErrLogEntries = errors.New("Error ...")
// ErrCertificateNotFound if we cannot find a certificate
ErrCertificateNotFound = errors.New("Error certificate not found")
)
// LogServerConnection Struct containing the CT log connection and relevant data
type LogServerConnection struct {
logClient *client.LogClient
outputFile *os.File
treeSize int64
bucketSize int64
start int64
end int64
}
func merkleTreeSize(logClient *client.LogClient) (uint64, error) {
treeHead, err := logClient.GetSTH()
if err != nil {
return 0, err
}
return treeHead.TreeSize, nil
}
func leafCertificate(logEntry ct.LogEntry) ([]byte, error) {
if logEntry.Leaf.LeafType != ct.TimestampedEntryLeafType {
return nil, ErrCertificateNotFound
}
if logEntry.Leaf.TimestampedEntry.EntryType != ct.X509LogEntryType && logEntry.Leaf.TimestampedEntry.EntryType != ct.PrecertLogEntryType {
return nil, ErrCertificateNotFound
}
if logEntry.Leaf.TimestampedEntry.EntryType == ct.X509LogEntryType {
return logEntry.Leaf.TimestampedEntry.X509Entry, nil
}
return logEntry.Leaf.TimestampedEntry.PrecertEntry.TBSCertificate, nil
}
// New Create a new connection to server <uri>, downloading <bucketSize> entries at a time
func New(uri string, bucketSize int64) *LogServerConnection {
var c LogServerConnection
var err error
c.logClient = client.New(uri)
if err != nil {
log.Error(err)
return nil
}
treeSize, err := merkleTreeSize(c.logClient)
if err != nil {
log.Error(err)
return nil
}
c.treeSize = int64(treeSize)
//fmt.Fprintf(os.Stderr, "Connection with %s has size %d\n", uri, c.treeSize)
if bucketSize >= c.treeSize {
c.bucketSize = c.treeSize / 2
} else {
c.bucketSize = bucketSize
}
c.start = 0
c.end = c.bucketSize
return &c
}
// NewWithOffset Same as New, but starts at entry <offset> in the log
func NewWithOffset(uri string, bucketSize int64, start int64) *LogServerConnection {
c := New(uri, bucketSize)
if c == nil {
return nil
}
c.start = start
c.end = start + bucketSize
return c
}
func (c *LogServerConnection) slideBucket() {
if c.start == 0 {
c.start = 1
}
c.start += c.bucketSize
c.end += c.bucketSize
}
// GetLogEntries get one window's worth of entries, slide window
func (c *LogServerConnection) GetLogEntries() ([]ct.LogEntry, error) {
if c.end >= c.treeSize {
c.treeSize -= 1
c.end = c.treeSize
}
log.Info("Requesting Tree Range: %d-%d/%d\n", c.start, c.end, c.treeSize)
entries, err := c.logClient.GetEntries(c.start, c.end)
log.Info("Entries length: %d", len(entries))
if err != nil {
return nil, err
}
if len(entries) < int(c.bucketSize) && c.end != c.treeSize {
c.end = c.start + int64(len(entries))
c.bucketSize = int64(len(entries))
}
return entries, nil
}