forked from google/keytransparency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyserver.go
362 lines (328 loc) · 12.6 KB
/
keyserver.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2016 Google Inc. 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 keyserver implements a transparent key server for End to End.
package keyserver
import (
"context"
"github.com/google/keytransparency/core/crypto/vrf"
"github.com/google/keytransparency/core/crypto/vrf/p256"
"github.com/google/keytransparency/core/directory"
"github.com/google/keytransparency/core/mutator"
"github.com/google/keytransparency/core/mutator/entry"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/google/keytransparency/core/api/v1/keytransparency_go_proto"
tpb "github.com/google/trillian"
)
// Server holds internal state for the key server.
type Server struct {
tlog tpb.TrillianLogClient
tmap tpb.TrillianMapClient
logAdmin tpb.TrillianAdminClient
mapAdmin tpb.TrillianAdminClient
mutator mutator.Func
directories directory.Storage
logs mutator.MutationLogs
mutations mutator.MutationStorage
indexFunc indexFunc
}
// New creates a new instance of the key server.
func New(tlog tpb.TrillianLogClient,
tmap tpb.TrillianMapClient,
logAdmin tpb.TrillianAdminClient,
mapAdmin tpb.TrillianAdminClient,
mutator mutator.Func,
directories directory.Storage,
logs mutator.MutationLogs,
mutations mutator.MutationStorage) *Server {
return &Server{
tlog: tlog,
tmap: tmap,
logAdmin: logAdmin,
mapAdmin: mapAdmin,
mutator: mutator,
directories: directories,
logs: logs,
mutations: mutations,
indexFunc: indexFromVRF,
}
}
// GetEntry returns a user's profile and proof that there is only one object for
// this user and that it is the same one being provided to everyone else.
// GetEntry also supports querying past values by setting the epoch field.
func (s *Server) GetEntry(ctx context.Context, in *pb.GetEntryRequest) (*pb.GetEntryResponse, error) {
directoryID := in.GetDirectoryId()
if directoryID == "" {
return nil, status.Errorf(codes.InvalidArgument, "Please specify a directory_id")
}
// Lookup log and map info.
d, err := s.directories.Read(ctx, directoryID, false)
if err != nil {
glog.Errorf("adminstorage.Read(%v): %v", directoryID, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info")
}
// Fetch latest revision.
sth, consistencyProof, err := s.latestLogRootProof(ctx, d, in.GetFirstTreeSize())
if err != nil {
return nil, err
}
revision, err := mapRevisionFor(sth)
if err != nil {
glog.Errorf("latestRevision(log %v, sth%v): %v", d.LogID, sth, err)
return nil, err
}
entryProof, err := s.getEntryByRevision(ctx, sth, d, in.UserId, in.AppId, revision)
if err != nil {
return nil, err
}
resp := &pb.GetEntryResponse{
LogRoot: sth,
LogConsistency: consistencyProof.GetHashes(),
}
proto.Merge(resp, entryProof)
return resp, nil
}
// getEntryByRevision returns an entry and its proofs.
// getEntryByRevision does NOT populate the following fields:
// - LogRoot
// - LogConsistency
func (s *Server) getEntryByRevision(ctx context.Context, sth *tpb.SignedLogRoot, d *directory.Directory,
userID, appID string, mapRevision int64) (*pb.GetEntryResponse, error) {
if mapRevision < 0 {
return nil, status.Errorf(codes.InvalidArgument,
"Revision is %v, want >= 0", mapRevision)
}
index, proof, err := s.indexFunc(ctx, d, appID, userID)
if err != nil {
return nil, err
}
getResp, err := s.tmap.GetLeavesByRevision(ctx, &tpb.GetMapLeavesByRevisionRequest{
MapId: d.MapID,
Index: [][]byte{index[:]},
Revision: mapRevision,
})
if err != nil {
glog.Errorf("GetLeavesByRevision(%v, rev: %v): %v", d.MapID, mapRevision, err)
return nil, status.Errorf(codes.Internal, "Failed fetching map leaf")
}
if got, want := len(getResp.MapLeafInclusion), 1; got != want {
glog.Errorf("GetLeavesByRevision() len: %v, want %v", got, want)
return nil, status.Errorf(codes.Internal, "Failed fetching map leaf")
}
neighbors := getResp.MapLeafInclusion[0].GetInclusion()
leaf := getResp.MapLeafInclusion[0].GetLeaf().GetLeafValue()
extraData := getResp.MapLeafInclusion[0].GetLeaf().GetExtraData()
var committed *pb.Committed
if leaf != nil {
if extraData == nil {
return nil, status.Errorf(codes.Internal, "Missing commitment data")
}
committed = &pb.Committed{}
if err := proto.Unmarshal(extraData, committed); err != nil {
return nil, status.Errorf(codes.Internal, "Cannot read committed value")
}
}
// SignedMapHead to SignedLogRoot inclusion proof.
secondTreeSize := sth.GetTreeSize()
logInclusion, err := s.tlog.GetInclusionProof(ctx,
&tpb.GetInclusionProofRequest{
LogId: d.LogID,
// SignedMapRoot must be placed in the log at MapRevision.
// MapRevisions start at 0. Log leaves start at 0.
LeafIndex: mapRevision,
TreeSize: secondTreeSize,
})
if err != nil {
glog.Errorf("tlog.GetInclusionProof(%v, %v, %v): %v", d.LogID, mapRevision, secondTreeSize, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch log inclusion proof")
}
return &pb.GetEntryResponse{
VrfProof: proof,
Committed: committed,
LeafProof: &tpb.MapLeafInclusion{
Inclusion: neighbors,
Leaf: &tpb.MapLeaf{
LeafValue: leaf,
},
},
MapRoot: getResp.GetMapRoot(),
LogInclusion: logInclusion.GetProof().GetHashes(),
}, nil
}
// ListEntryHistory returns a list of EntryProofs covering a period of time.
func (s *Server) ListEntryHistory(ctx context.Context, in *pb.ListEntryHistoryRequest) (*pb.ListEntryHistoryResponse, error) {
// Lookup log and map info.
directoryID := in.GetDirectoryId()
if directoryID == "" {
return nil, status.Errorf(codes.InvalidArgument, "Please specify a directory_id")
}
d, err := s.directories.Read(ctx, directoryID, false)
if err != nil {
glog.Errorf("adminstorage.Read(%v): %v", directoryID, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info")
}
// Fetch latest revision.
sth, consistencyProof, err := s.latestLogRootProof(ctx, d, in.GetFirstTreeSize())
if err != nil {
return nil, err
}
currentEpoch, err := mapRevisionFor(sth)
if err != nil {
glog.Errorf("latestRevision(log %v, sth%v): %v", d.LogID, sth, err)
return nil, err
}
if err := validateListEntryHistoryRequest(in, currentEpoch); err != nil {
glog.Errorf("validateListEntryHistoryRequest(%v, %v): %v", in, currentEpoch, err)
return nil, status.Errorf(codes.InvalidArgument, "Invalid request")
}
// TODO(gbelvin): fetch all history from trillian at once.
// Get all GetEntryResponse for all epochs in the range [start, start + in.PageSize].
responses := make([]*pb.GetEntryResponse, in.PageSize)
for i := range responses {
resp, err := s.getEntryByRevision(ctx, sth, d, in.UserId, in.AppId, in.Start+int64(i))
if err != nil {
glog.Errorf("getEntry failed for epoch %v: %v", in.Start+int64(i), err)
return nil, status.Errorf(codes.Internal, "GetEntry failed")
}
proto.Merge(resp, &pb.GetEntryResponse{
LogRoot: sth,
// TODO(gbelvin): This is redundant and wasteful. Refactor response API.
LogConsistency: consistencyProof.GetHashes(),
})
responses[i] = resp
}
nextStart := in.Start + int64(len(responses))
if nextStart > currentEpoch {
nextStart = 0
}
return &pb.ListEntryHistoryResponse{
Values: responses,
NextStart: nextStart,
}, nil
}
// QueueEntryUpdate updates a user's profile. If the user does not exist, a new
// profile will be created.
func (s *Server) QueueEntryUpdate(ctx context.Context, in *pb.UpdateEntryRequest) (*empty.Empty, error) {
if in.DirectoryId == "" {
return nil, status.Errorf(codes.InvalidArgument, "Please specify a directory_id")
}
// Lookup log and map info.
directory, err := s.directories.Read(ctx, in.DirectoryId, false)
if err != nil {
glog.Errorf("adminstorage.Read(%v): %v", in.DirectoryId, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info")
}
vrfPriv, err := p256.NewFromWrappedKey(ctx, directory.VRFPriv)
if err != nil {
return nil, err
}
// Verify:
// - Index to Key equality in SignedKV.
// - Correct profile commitment.
// - Correct key formats.
if err := validateUpdateEntryRequest(in, vrfPriv); err != nil {
glog.Warningf("Invalid UpdateEntryRequest: %v", err)
return nil, status.Errorf(codes.InvalidArgument, "Invalid request")
}
// Query for the current epoch.
req := &pb.GetEntryRequest{
DirectoryId: in.DirectoryId,
UserId: in.UserId,
AppId: in.AppId,
//EpochStart: in.GetEntryUpdate().EpochStart,
}
resp, err := s.GetEntry(ctx, req)
if err != nil {
glog.Errorf("GetEntry failed: %v", err)
return nil, status.Errorf(codes.Internal, "Read failed")
}
// Catch errors early. Perform mutation verification.
// Read at the current value. Assert the following:
// - Correct signatures from previous epoch.
// - Correct signatures internal to the update.
// - Hash of current data matches the expectation in the mutation.
// The very first mutation will have resp.LeafProof.MapLeaf.LeafValue=nil.
oldLeafB := resp.GetLeafProof().GetLeaf().GetLeafValue()
oldEntry, err := entry.FromLeafValue(oldLeafB)
if err != nil {
glog.Errorf("entry.FromLeafValue: %v", err)
return nil, status.Errorf(codes.InvalidArgument, "invalid previous leaf value")
}
if _, err := s.mutator.Mutate(oldEntry, in.GetEntryUpdate().GetMutation()); err == mutator.ErrReplay {
glog.Warningf("Discarding request due to replay")
return nil, status.Errorf(codes.FailedPrecondition,
"The request contains a reference to old data. Please regenerate request and try again")
} else if err != nil {
glog.Warningf("Invalid mutation: %v", err)
return nil, status.Errorf(codes.InvalidArgument, "Invalid mutation")
}
// Save mutation to the database.
if err := s.logs.Send(ctx, directory.DirectoryID, in.GetEntryUpdate()); err != nil {
glog.Errorf("mutations.Write failed: %v", err)
return nil, status.Errorf(codes.Internal, "Mutation write error")
}
return &empty.Empty{}, nil
}
// GetDirectory returns all info tied to the specified directory.
//
// This API to get all necessary data needed to verify a particular
// key-server. Data contains for instance the tree-info, like for instance the
// log/map-id and the corresponding public-keys.
func (s *Server) GetDirectory(ctx context.Context, in *pb.GetDirectoryRequest) (*pb.Directory, error) {
// Lookup log and map info.
if in.DirectoryId == "" {
return nil, status.Errorf(codes.InvalidArgument, "Please specify a directory_id")
}
directory, err := s.directories.Read(ctx, in.DirectoryId, false)
if status.Code(err) == codes.NotFound {
glog.Errorf("adminstorage.Read(%v): %v", in.DirectoryId, err)
return nil, status.Errorf(codes.NotFound, "Directory %v not found", in.DirectoryId)
} else if err != nil {
glog.Errorf("adminstorage.Read(%v): %v", in.DirectoryId, err)
return nil, status.Errorf(codes.Internal, "Cannot fetch directory info for %v", in.DirectoryId)
}
logTree, err := s.logAdmin.GetTree(ctx, &tpb.GetTreeRequest{TreeId: directory.LogID})
if err != nil {
return nil, status.Errorf(codes.Internal,
"Cannot fetch log info for %v: %v", in.DirectoryId, err)
}
mapTree, err := s.mapAdmin.GetTree(ctx, &tpb.GetTreeRequest{TreeId: directory.MapID})
if err != nil {
return nil, status.Errorf(codes.Internal,
"Cannot fetch map info for %v: %v", in.DirectoryId, err)
}
return &pb.Directory{
DirectoryId: directory.DirectoryID,
Log: logTree,
Map: mapTree,
Vrf: directory.VRF,
MinInterval: ptypes.DurationProto(directory.MinInterval),
MaxInterval: ptypes.DurationProto(directory.MaxInterval),
}, nil
}
// indexFunc computes an index and proof for directory/app/user
type indexFunc func(ctx context.Context, d *directory.Directory, appID, userID string) ([32]byte, []byte, error)
// index returns the index and proof for directory/app/user
func indexFromVRF(ctx context.Context, d *directory.Directory, appID, userID string) ([32]byte, []byte, error) {
vrfPriv, err := p256.NewFromWrappedKey(ctx, d.VRFPriv)
if err != nil {
return [32]byte{}, nil, err
}
index, proof := vrfPriv.Evaluate(vrf.UniqueID(userID, appID))
return index, proof, nil
}