aboutsummaryrefslogtreecommitdiff
path: root/client/client.go
blob: 0cbf4326979cc23d8af158277255a24fa6127bae (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package client

import (
	"bytes"
	"context"
	"fmt"

	"crypto/ed25519"
	"encoding/base64"
	"encoding/json"
	"io/ioutil"
	"net/http"

	"github.com/golang/glog"
	"github.com/google/trillian/merkle/rfc6962"
	"github.com/system-transparency/stfe"
	"github.com/system-transparency/stfe/descriptor"
	"github.com/system-transparency/stfe/namespace"
	"golang.org/x/net/context/ctxhttp"
)

// Client is an HTTP(S) client that talks to an ST log
type Client struct {
	Log        *descriptor.Log
	Client     *http.Client
	PrivateKey *ed25519.PrivateKey
	Namespace  *namespace.Namespace
	useHttp    bool
}

// NewClient returns a new log client.
//
// Note: private key can be ommied if no write APIs are used.
func NewClient(log *descriptor.Log, client *http.Client, useHttp bool, privateKey *ed25519.PrivateKey) (*Client, error) {
	c := &Client{
		Log:        log,
		Client:     client,
		PrivateKey: privateKey,
		useHttp:    useHttp,
	}
	if privateKey != nil {
		var err error
		c.Namespace, err = namespace.NewNamespaceEd25519V1([]byte(privateKey.Public().(ed25519.PublicKey)))
		if err != nil {
			return nil, fmt.Errorf("failed creating namespace: %v", err)
		}
	}
	return c, nil
}

// AddEntry creates, signs, and adds a new ChecksumV1 entry to the log
func (c *Client) AddEntry(ctx context.Context, name, checksum []byte) (*stfe.StItem, error) {
	leaf, err := stfe.NewChecksumV1(name, checksum, c.Namespace).Marshal()
	if err != nil {
		return nil, fmt.Errorf("failed marshaling StItem: %v", err)
	}
	data, err := json.Marshal(stfe.AddEntryRequest{
		Item:      leaf,
		Signature: ed25519.Sign(*c.PrivateKey, leaf),
	})
	if err != nil {
		return nil, fmt.Errorf("failed creating post data: %v", err)
	}
	glog.V(3).Infof("created post data: %s", string(data))

	url := stfe.EndpointAddEntry.Path(c.protocol() + c.Log.BaseUrl)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
	if err != nil {
		return nil, fmt.Errorf("failed creating http request: %v", err)
	}
	req.Header.Set("Content-Type", "application/json")
	glog.V(2).Infof("created http request: %s %s", req.Method, req.URL)

	item, err := c.doRequestWithStItemResponse(ctx, req)
	if err != nil {
		return nil, err
	}
	if item.Format != stfe.StFormatSignedDebugInfoV1 {
		return nil, fmt.Errorf("bad StItem format: %v", item.Format)
	}

	if ns, err := c.Log.Namespace(); err != nil {
		return nil, fmt.Errorf("invalid log namespace: %v", err)
	} else if err := ns.Verify(leaf, item.SignedDebugInfoV1.Signature); err != nil {
		return nil, fmt.Errorf("bad SignedDebugInfoV1 signature: %v", err)
	}
	return item, nil
}

// GetSth fetches and verifies the most recent STH.
func (c *Client) GetSth(ctx context.Context) (*stfe.StItem, error) {
	url := stfe.EndpointGetSth.Path(c.protocol() + c.Log.BaseUrl)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed creating http request: %v", err)
	}
	glog.V(2).Infof("created http request: %s %s", req.Method, req.URL)

	item, err := c.doRequestWithStItemResponse(ctx, req)
	if err != nil {
		return nil, err
	}
	if item.Format != stfe.StFormatSignedTreeHeadV1 {
		return nil, fmt.Errorf("bad StItem format: %v", item.Format)
	}
	th, err := item.SignedTreeHeadV1.TreeHead.Marshal()
	if err != nil {
		return nil, fmt.Errorf("failed marshaling tree head: %v", err)
	}

	if ns, err := c.Log.Namespace(); err != nil {
		return nil, fmt.Errorf("bad public key: %v", err)
	} else if err := ns.Verify(th, item.SignedTreeHeadV1.Signature); err != nil {
		return nil, fmt.Errorf("bad SignedTreeHeadV1 signature: %v", err)
	}
	return item, nil
}

// GetConsistencyProof fetches and verifies a consistency proof between two
// STHs.
func (c *Client) GetConsistencyProof(ctx context.Context, first, second *stfe.StItem) (*stfe.StItem, error) {
	url := stfe.EndpointGetConsistencyProof.Path(c.protocol() + c.Log.BaseUrl)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed creating http request: %v", err)
	}
	req.Header.Set("Content-Type", "application/json")
	q := req.URL.Query()
	q.Add("first", fmt.Sprintf("%d", first.SignedTreeHeadV1.TreeHead.TreeSize))
	q.Add("second", fmt.Sprintf("%d", second.SignedTreeHeadV1.TreeHead.TreeSize))
	req.URL.RawQuery = q.Encode()
	glog.V(2).Infof("created http request: %s %s", req.Method, req.URL)

	item, err := c.doRequestWithStItemResponse(ctx, req)
	if err != nil {
		return nil, err
	}
	if item.Format != stfe.StFormatConsistencyProofV1 {
		return nil, fmt.Errorf("bad StItem format: %v", item.Format)
	}
	if err := VerifyConsistencyProofV1(item, first, second); err != nil {
		return nil, fmt.Errorf("bad consistency proof: %v", err)
	}
	return item, nil
}

// GetProofByHash fetches and verifies an inclusion proof for a leaf against an
// STH.
func (c *Client) GetProofByHash(ctx context.Context, treeSize uint64, rootHash, leaf []byte) (*stfe.StItem, error) {
	leafHash := rfc6962.DefaultHasher.HashLeaf(leaf)
	url := stfe.EndpointGetProofByHash.Path(c.protocol() + c.Log.BaseUrl)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed creating http request: %v", err)
	}
	req.Header.Set("Content-Type", "application/json")
	q := req.URL.Query()
	q.Add("hash", base64.StdEncoding.EncodeToString(leafHash))
	q.Add("tree_size", fmt.Sprintf("%d", treeSize))
	req.URL.RawQuery = q.Encode()
	glog.V(2).Infof("created http request: %s %s", req.Method, req.URL)

	item, err := c.doRequestWithStItemResponse(ctx, req)
	if err != nil {
		return nil, err
	}
	if item.Format != stfe.StFormatInclusionProofV1 {
		return nil, fmt.Errorf("bad StItem format: %v", item.Format)
	}
	if err := VerifyInclusionProofV1(item, rootHash, leafHash); err != nil {
		return nil, fmt.Errorf("bad inclusion proof: %v", err)
	}
	return item, nil
}

// GetEntries fetches a range of entries from the log, verifying that they are
// of type checksum_v1 and signed by a valid certificate chain in the appendix.
// Fewer entries may be returned if too large range, in which case the end is
// truncated.
//
// Note that a certificate chain is considered valid if it is chained correctly.
// In other words, the caller may want to check whether the anchor is trusted.
func (c *Client) GetEntries(ctx context.Context, start, end uint64) ([]*stfe.GetEntryResponse, error) {
	url := stfe.EndpointGetEntries.Path(c.protocol() + c.Log.BaseUrl)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed creating http request: %v", err)
	}
	req.Header.Set("Content-Type", "application/json")
	q := req.URL.Query()
	q.Add("start", fmt.Sprintf("%d", start))
	q.Add("end", fmt.Sprintf("%d", end))
	req.URL.RawQuery = q.Encode()
	glog.V(2).Infof("created http request: %s %s", req.Method, req.URL)

	var rsp []*stfe.GetEntryResponse
	if err := c.doRequest(ctx, req, &rsp); err != nil {
		return nil, err
	}
	for _, entry := range rsp {
		var item stfe.StItem
		if err := item.Unmarshal(entry.Item); err != nil {
			return nil, fmt.Errorf("unmarshal failed: %v (%v)", err, entry)
		}
		if item.Format != stfe.StFormatChecksumV1 {
			return nil, fmt.Errorf("bad StFormat: %v (%v)", err, entry)
		}
		if err := item.ChecksumV1.Namespace.Verify(entry.Item, entry.Signature); err != nil { // TODO: only works if full vk in namespace
			return nil, fmt.Errorf("bad signature: %v (%v)", err, entry)
		}
	}
	return rsp, nil
}

// GetNamespaces fetches the log's trusted namespaces.
func (c *Client) GetNamespaces(ctx context.Context) ([][]byte, error) {
	url := stfe.EndpointGetAnchors.Path(c.protocol() + c.Log.BaseUrl) // TODO: update GetAnchors => GetNamespaces
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, fmt.Errorf("failed creating http request: %v", err)
	}
	var rsp [][]byte
	if err := c.doRequest(ctx, req, &rsp); err != nil {
		return nil, err
	}
	return rsp, nil
}

// doRequest sends an HTTP request and decodes the resulting json body into out
func (c *Client) doRequest(ctx context.Context, req *http.Request, out interface{}) error {
	rsp, err := ctxhttp.Do(ctx, c.Client, req)
	if err != nil {
		return fmt.Errorf("http request failed: %v", err)
	}
	body, err := ioutil.ReadAll(rsp.Body)
	rsp.Body.Close()
	if err != nil {
		return fmt.Errorf("http body read failed: %v", err)
	}
	if rsp.StatusCode != http.StatusOK {
		return fmt.Errorf("http status code not ok: %v", rsp.StatusCode)
	}
	if err := json.Unmarshal(body, out); err != nil {
		return fmt.Errorf("failed decoding json body: %v", err)
	}
	return nil
}

//
// doRequestWithStItemResponse sends an HTTP request and returns a decoded
// StItem that the resulting HTTP response contained json:ed and marshaled
func (c *Client) doRequestWithStItemResponse(ctx context.Context, req *http.Request) (*stfe.StItem, error) {
	var itemStr string
	if err := c.doRequest(ctx, req, &itemStr); err != nil {
		return nil, err
	}
	b, err := base64.StdEncoding.DecodeString(itemStr)
	if err != nil {
		return nil, fmt.Errorf("failed decoding base64 body: %v", err)
	}
	var item stfe.StItem
	if err := item.Unmarshal(b); err != nil {
		return nil, fmt.Errorf("failed decoding StItem: %v", err)
	}
	glog.V(3).Infof("got StItem: %s", item)
	return &item, nil
}

// protocol returns a protocol string that preceeds the log's base url
func (c *Client) protocol() string {
	if c.useHttp {
		return "http://"
	}
	return "https://"
}