/
client.go
258 lines (223 loc) · 7.33 KB
/
client.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
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
// Copyright 2020 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"github.com/golang/glog"
"github.com/google/trillian-examples/binary_transparency/firmware/api"
"github.com/transparency-dev/merkle/rfc6962"
"golang.org/x/mod/sumdb/note"
"google.golang.org/grpc/status"
)
// ReadonlyClient is an HTTP client for the FT personality.
//
// TODO(al): split this into Client and SubmitClient.
type ReadonlyClient struct {
// LogURL is the base URL for the FT log.
LogURL *url.URL
LogSigVerifier note.Verifier
}
// SubmitClient extends ReadonlyClient to also know how to submit entries
type SubmitClient struct {
*ReadonlyClient
}
// PublishFirmware sends a firmware manifest and corresponding image to the log server.
func (c SubmitClient) PublishFirmware(manifest, image []byte) error {
u, err := c.LogURL.Parse(api.HTTPAddFirmware)
if err != nil {
return err
}
glog.V(1).Infof("Submitting to %v", u.String())
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Write the manifest JSON part
mh := make(textproto.MIMEHeader)
mh.Set("Content-Type", "application/json")
partWriter, err := w.CreatePart(mh)
if err != nil {
return err
}
if _, err := io.Copy(partWriter, bytes.NewReader(manifest)); err != nil {
return err
}
// Write the binary FW image part
mh = make(textproto.MIMEHeader)
mh.Set("Content-Type", "application/octet-stream")
partWriter, err = w.CreatePart(mh)
if err != nil {
return err
}
if _, err := io.Copy(partWriter, bytes.NewReader(image)); err != nil {
return err
}
// Finish off the multipart request
if err := w.Close(); err != nil {
return err
}
// Turn this into an HTTP POST request
req, err := http.NewRequest("POST", u.String(), &b)
if err != nil {
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
// And finally, submit the request to the log
r, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to publish to log endpoint (%s): %w", u, err)
}
if r.Request.Method != "POST" {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#permanent_redirections
return fmt.Errorf("POST request to %q was converted to %s request to %q", u.String(), r.Request.Method, r.Request.URL)
}
if r.StatusCode != http.StatusOK {
return errFromResponse("failed to submit to log", r)
}
return nil
}
// PublishAnnotationMalware publishes the serialized annotation to the log.
func (c SubmitClient) PublishAnnotationMalware(stmt []byte) error {
u, err := c.LogURL.Parse(api.HTTPAddAnnotationMalware)
if err != nil {
return err
}
glog.V(1).Infof("Submitting to %v", u.String())
r, err := http.Post(u.String(), "application/json", bytes.NewBuffer(stmt))
if err != nil {
return fmt.Errorf("failed to publish to log endpoint (%s): %w", u, err)
}
if r.StatusCode != http.StatusOK {
return errFromResponse("failed to submit to log", r)
}
return nil
}
// GetCheckpoint returns a new LogCheckPoint from the server.
func (c ReadonlyClient) GetCheckpoint() (*api.LogCheckpoint, error) {
u, err := c.LogURL.Parse(api.HTTPGetRoot)
if err != nil {
return nil, err
}
r, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer func() {
if err := r.Body.Close(); err != nil {
glog.Errorf("r.Body.Close(): %v", err)
}
}()
if r.StatusCode != 200 {
return &api.LogCheckpoint{}, errFromResponse("failed to fetch checkpoint", r)
}
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %w", err)
}
return api.ParseCheckpoint(b, c.LogSigVerifier)
}
// GetInclusion returns an inclusion proof for the statement under the given checkpoint.
func (c ReadonlyClient) GetInclusion(statement []byte, cp api.LogCheckpoint) (api.InclusionProof, error) {
hash := rfc6962.DefaultHasher.HashLeaf(statement)
u, err := c.LogURL.Parse(fmt.Sprintf("%s/for-leaf-hash/%s/in-tree-of/%d", api.HTTPGetInclusion, base64.URLEncoding.EncodeToString(hash), cp.Size))
if err != nil {
return api.InclusionProof{}, err
}
glog.V(2).Infof("Fetching inclusion proof from %q", u.String())
r, err := http.Get(u.String())
if err != nil {
return api.InclusionProof{}, err
}
if r.StatusCode != 200 {
return api.InclusionProof{}, errFromResponse("failed to fetch inclusion proof", r)
}
var ip api.InclusionProof
err = json.NewDecoder(r.Body).Decode(&ip)
return ip, err
}
// GetManifestEntryAndProof returns the manifest and proof from the server, for given Index and TreeSize
// TODO(mhutchinson): Rename this as leaf values can also be annotations.
func (c ReadonlyClient) GetManifestEntryAndProof(request api.GetFirmwareManifestRequest) (*api.InclusionProof, error) {
url := fmt.Sprintf("%s/at/%d/in-tree-of/%d", api.HTTPGetManifestEntryAndProof, request.Index, request.TreeSize)
u, err := c.LogURL.Parse(url)
if err != nil {
return nil, err
}
r, err := http.Get(u.String())
if err != nil {
return nil, err
}
if r.StatusCode != 200 {
return nil, errFromResponse("failed to fetch entry and proof", r)
}
var mr api.InclusionProof
if err := json.NewDecoder(r.Body).Decode(&mr); err != nil {
return nil, err
}
return &mr, nil
}
// GetConsistencyProof returns the Consistency Proof from the server, for the two given snapshots
func (c ReadonlyClient) GetConsistencyProof(request api.GetConsistencyRequest) (*api.ConsistencyProof, error) {
url := fmt.Sprintf("%s/from/%d/to/%d", api.HTTPGetConsistency, request.From, request.To)
u, err := c.LogURL.Parse(url)
if err != nil {
return nil, err
}
r, err := http.Get(u.String())
if err != nil {
return nil, err
}
if r.StatusCode != 200 {
return nil, errFromResponse("failed to fetch consistency proof", r)
}
var cp api.ConsistencyProof
if err := json.NewDecoder(r.Body).Decode(&cp); err != nil {
return nil, err
}
return &cp, nil
}
// GetFirmwareImage returns the firmware image with the corresponding hash from the personality CAS.
func (c ReadonlyClient) GetFirmwareImage(hash []byte) ([]byte, error) {
url := fmt.Sprintf("%s/with-hash/%s", api.HTTPGetFirmwareImage, base64.URLEncoding.EncodeToString(hash))
u, err := c.LogURL.Parse(url)
if err != nil {
return nil, err
}
r, err := http.Get(u.String())
if err != nil {
return nil, err
}
if r.StatusCode != 200 {
return nil, errFromResponse("failed to fetch firmware image", r)
}
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("failed to read firmware image from response: %w", err)
}
return b, nil
}
func errFromResponse(m string, r *http.Response) error {
if r.StatusCode == 200 {
return nil
}
b, _ := io.ReadAll(r.Body) // Ignore any error, we want to ensure we return the right status code which we already know.
msg := fmt.Sprintf("%s: %s", m, string(b))
return status.New(codeFromHTTPResponse(r.StatusCode), msg).Err()
}