This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
debianupdate.go
458 lines (395 loc) · 13.2 KB
/
debianupdate.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package debianupdate
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"sort"
"sync"
"time"
"github.com/dedis/paper_chainiac/manage"
"github.com/dedis/paper_chainiac/skipchain"
"github.com/dedis/paper_chainiac/swupdate/protocol"
"github.com/dedis/paper_chainiac/timestamp"
"github.com/satori/go.uuid"
"gopkg.in/dedis/onet.v1"
"gopkg.in/dedis/onet.v1/log"
"gopkg.in/dedis/onet.v1/network"
"gopkg.in/dedis/onet.v1/simul/monitor"
)
// ServiceName is the name to refer to the CoSi service
const ServiceName = "DebianUpdate"
var debianUpdateService onet.ServiceID
var verifierID = skipchain.VerifierID(uuid.NewV5(uuid.NamespaceURL, ServiceName))
func init() {
onet.RegisterNewService(ServiceName, NewDebianUpdate)
debianUpdateService = onet.ServiceFactory.ServiceID(ServiceName)
network.RegisterMessage(&storage{})
skipchain.VerificationRegistration(verifierID, verifierFunc)
}
// DebianUpdate service
type DebianUpdate struct {
*onet.ServiceProcessor
path string
Storage *storage
skipchain *skipchain.Client
ReasonableTime time.Duration
sync.Mutex
}
type storage struct {
Timestamp *Timestamp
// the first block of the *string* repo
RepositoryChainGenesis map[string]*RepositoryChain
// the latest block
RepositoryChain map[string]*RepositoryChain
// the root skipchain
Root *skipchain.SkipBlock
// the interval between Timestamps
TSInterval time.Duration
}
func NewDebianUpdate(context *onet.Context) onet.Service {
service := &DebianUpdate{
ServiceProcessor: onet.NewServiceProcessor(context),
skipchain: skipchain.NewClient(),
Storage: &storage{
RepositoryChainGenesis: map[string]*RepositoryChain{},
RepositoryChain: map[string]*RepositoryChain{},
},
ReasonableTime: time.Hour,
}
err := service.RegisterHandlers(service.CreateRepository,
service.UpdateRepository, service.LatestBlocks,
service.LatestBlockFromName, service.LatestBlock)
/*service.TimeStampProof,
service.LatestBlocks,
service.TimestampProofs)*/
if err != nil {
log.ErrFatal(err, "Couldn't register messages")
}
return service
}
func (service *DebianUpdate) CreateRepository(cr *CreateRepository) (network.Message, onet.ClientError) {
repo := cr.Release.Repository
log.Lvlf3("%s Creating repository %s version %s", service,
repo.GetName(), repo.Version)
repoChain := &RepositoryChain{
Release: cr.Release,
Root: service.Storage.Root,
}
if service.Storage.Root == nil {
log.Lvl3("Creating Root-skipchain")
var err error
service.Storage.Root, err = service.skipchain.CreateRoster(cr.Roster,
cr.Base, cr.Height, skipchain.VerifyNone, nil)
if err != nil {
return nil, onet.NewClientError(err)
}
repoChain.Root = service.Storage.Root
}
log.Lvl3("Creating Data-skipchain")
var err error
repoChain.Root, repoChain.Data, err = service.skipchain.CreateData(
repoChain.Root, cr.Base, cr.Height, verifierID, cr.Release)
if err != nil {
log.Lvl2("error while adding the data in the skipchain")
return nil, onet.NewClientError(err)
}
service.Storage.RepositoryChainGenesis[repo.GetName()] = repoChain
if err := service.startPropagate(repo.GetName(), repoChain); err != nil {
return nil, onet.NewClientError(err)
}
service.timestamp(time.Now())
return &CreateRepositoryRet{repoChain}, nil
}
func (service *DebianUpdate) startPropagate(repo string,
repoChain *RepositoryChain) error {
roster := service.Storage.Root.Roster
log.Lvl2("Propagating repository", repo, repoChain, "to", roster.List)
replies, err := manage.PropagateStartAndWait(service.Context, roster,
repoChain, 120000, service.PropagateSkipBlock)
if err != nil {
return err
}
if replies != len(roster.List) {
log.Warn("Did only get", replies, "out of", len(roster.List))
}
return nil
}
func (service *DebianUpdate) PropagateSkipBlock(msg network.Message) {
repoChain, ok := msg.(*RepositoryChain)
if !ok {
log.Error("Couldn't convert to SkipBlock")
return
}
repo := repoChain.Release.Repository.GetName()
log.Lvl2("saving repositorychain for", repo)
// TODO: Verification
if _, exists := service.Storage.RepositoryChainGenesis[repo]; !exists {
service.Storage.RepositoryChainGenesis[repo] = repoChain
}
service.Storage.RepositoryChain[repo] = repoChain
}
// timestamp creates a merkle tree of all the latests skipblocks of each
// skipchains, run a timestamp protocol and store the results in
// service.latestTimestamps.
func (service *DebianUpdate) timestamp(time time.Time) {
//measure := monitor.NewTimeMeasure("debianupdate_timestamp")
// order all packets and marshal them
ids := service.orderedLatestSkipblocksID()
// create merkle tree + proofs and the final message
root, proofs := timestamp.ProofTree(HashFunc(), ids)
msg := MarshalPair(root, time.Unix())
// run protocol
signature := service.cosiSign(msg)
// TODO XXX Here in a non-academical world we should test if the
// signature contains enough participants.
service.updateTimestampInfo(root, proofs, time.Unix(), signature)
//measure.Record()
}
func (service *DebianUpdate) cosiSign(msg []byte) []byte {
sdaTree := service.Storage.Root.Roster.GenerateBinaryTree()
// TODO XXX Here we use the swupdate protocol (should we ?)
tni := service.NewTreeNodeInstance(sdaTree, sdaTree.Root, swupdate.ProtocolName)
pi, err := swupdate.NewCoSiUpdate(tni, service.cosiVerify)
if err != nil {
panic("Couldn't make new protocol: " + err.Error())
}
service.RegisterProtocolInstance(pi)
// measure the time the cothority takes to sign the root
measure := monitor.NewTimeMeasure("cothority_signing")
pi.SigningMessage(msg)
// Take the raw message (already expecting a hash for the timestamp service)
response := make(chan []byte)
pi.RegisterSignatureHook(func(sig []byte) {
response <- sig
})
go pi.Dispatch()
go pi.Start()
log.Lvl2("Waiting on cosi response ...")
res := <-response
measure.Record()
log.Lvl2("... DONE: Recieved cosi response")
return res
}
func (service *DebianUpdate) cosiVerify(msg []byte) bool {
signedRoot, signedTime := UnmarshalPair(msg)
// check timestamp
if time.Now().Sub(time.Unix(signedTime, 0)) > service.ReasonableTime {
log.Lvl2("Timestamp is too far in the past")
return false
}
// check merkle tree root
// order all packets and marshal them
ids := service.orderedLatestSkipblocksID()
// create merkle tree + proofs and the final message
root, _ := timestamp.ProofTree(HashFunc(), ids)
// root of merkle tree is not secret, no need to use constant time.
if !bytes.Equal(root, signedRoot) {
log.Lvl2("Root of merkle root does not match")
return false
}
log.Lvl3("DebianUpdate cosi signature verified")
return true
}
func (service *DebianUpdate) updateTimestampInfo(rootID timestamp.HashID,
proofs []timestamp.Proof, ts int64, sig []byte) {
service.Lock()
defer service.Unlock()
if service.Storage.Timestamp == nil {
service.Storage.Timestamp = &Timestamp{}
}
var t = service.Storage.Timestamp
t.Timestamp = ts
t.Root = rootID
t.Signature = sig
t.Proofs = proofs
}
// HashFunc used for the timestamp operations with the Merkle tree generation
// and verification.
func HashFunc() timestamp.HashFunc {
return sha256.New
}
// MarshalPair takes the root of a merkle tree (only a slice of bytes) and a
// unix timestamp and marshal them. UnmarshalPair do the opposite.
func MarshalPair(root timestamp.HashID, time int64) []byte {
var buff bytes.Buffer
if err := binary.Write(&buff, binary.BigEndian, time); err != nil {
panic(err)
}
return append(buff.Bytes(), []byte(root)...)
}
// UnmarshalPair takes a slice of bytes generated by MarshalPair and retrieve
// the root and the unix timestamp out of it.
func UnmarshalPair(buff []byte) (timestamp.HashID, int64) {
var reader = bytes.NewBuffer(buff)
var time int64
if err := binary.Read(reader, binary.BigEndian, &time); err != nil {
panic(err)
}
return reader.Bytes(), time
}
// orderedLatestSkipblocksID sorts the latests blocks of all skipchains and
// return all ids in an array of HashID
func (service *DebianUpdate) orderedLatestSkipblocksID() []timestamp.HashID {
keys := service.getOrderedRepositoryNames()
ids := make([]timestamp.HashID, 0)
chains := service.Storage.RepositoryChain
for _, key := range keys {
ids = append(ids, timestamp.HashID(chains[key].Data.Hash))
}
return ids
}
func (service *DebianUpdate) getOrderedRepositoryNames() []string {
keys := make([]string, 0)
for k := range service.Storage.RepositoryChain {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (service *DebianUpdate) UpdateRepository(ur *UpdateRepository) (network.Message, onet.ClientError) {
//addBlock := monitor.NewTimeMeasure("add_block")
//defer addBlock.Record()
repoChain := &RepositoryChain{
Release: ur.Release,
Root: ur.RepositoryChain.Root,
}
release := ur.Release
actualRoot := service.Storage.RepositoryChain[release.Repository.GetName()].
Release.RootID
// Check if the new block is different
if !bytes.Equal(actualRoot, release.RootID) {
log.Lvl1("Adding new data to the Data-skipchain")
ret, err := service.skipchain.ProposeData(ur.RepositoryChain.Root,
ur.RepositoryChain.Data, release)
if err != nil {
return nil, onet.NewClientError(err)
}
repoChain.Data = ret.Latest
if err := service.startPropagate(release.Repository.GetName(),
repoChain); err != nil {
return nil, onet.NewClientError(err)
}
} else {
log.Lvl1("The latest existing skipblock is the same," +
" only update the timestamp.")
repoChain = service.Storage.RepositoryChain[release.Repository.GetName()]
}
service.timestamp(time.Now())
return &UpdateRepositoryRet{repoChain}, nil
}
// NewProtocol initialize the Protocol
func (service *DebianUpdate) NewProtocol(tn *onet.TreeNodeInstance,
conf *onet.GenericConfig) (onet.ProtocolInstance, error) {
var pi onet.ProtocolInstance
var err error
switch tn.ProtocolName() {
case "Propagate":
log.Lvl2("DebianUpdate Service received New Protocol PROPAGATE event")
pi, err = manage.NewPropagateProtocol(tn)
if err != nil {
return nil, err
}
pi.(*manage.Propagate).RegisterOnData(service.PropagateSkipBlock)
default:
log.Lvl2("DebianUpdate Service received New Protocol COSI event")
pi, err = swupdate.NewCoSiUpdate(tn, service.cosiVerify)
if err != nil {
return nil, err
}
}
return pi, err
}
func verifierFunc(msg, data []byte) bool {
_, sbBuf, err := network.Unmarshal(data)
sb, ok := sbBuf.(*skipchain.SkipBlock)
if err != nil || !ok {
log.Error(err, ok)
return false
}
_, relBuf, err := network.Unmarshal(sb.Data)
release, ok := relBuf.(*Release)
if err != nil || !ok {
log.Error(err, ok)
return false
}
repo := release.Repository
if repo == nil {
log.Lvl2("The repository contained in the release is nil")
return false
}
root := release.RootID
if len(root) == 0 {
log.Lvl2("No root hash, has the Merkle-tree correctly been built ?")
return false
}
//ver := monitor.NewTimeMeasure("verification")
// measure the time the cothority takes to verify the merkle tree
// IC: moved measure before creation of hashes
measure := monitor.NewTimeMeasure("cothority_verify_proofs")
// build the merkle-tree for packages
hashes := make([]timestamp.HashID, len(repo.Packages))
for i, p := range repo.Packages {
hashes[i] = timestamp.HashID(p.Hash)
}
possibleRoot, _ := timestamp.ProofTree(HashFunc(), hashes)
if !bytes.Equal(possibleRoot, root) {
log.Lvl2("Wrong root hash")
return false
}
measure.Record()
return true
}
func (service *DebianUpdate) RepositorySC(rsc *RepositorySC) (network.Message, onet.ClientError) {
repoChain, ok := service.Storage.RepositoryChain[rsc.repositoryName]
if !ok {
return nil, onet.NewClientErrorCode(4200, "Does not exist.")
}
latestBlockRet, err := service.LatestBlock(&LatestBlock{repoChain.Data.Hash})
if err != nil {
return nil, onet.NewClientError(err)
}
update := latestBlockRet.(*LatestBlockRet).Update
return &RepositorySCRet{
First: service.Storage.RepositoryChainGenesis[rsc.repositoryName].Data,
Last: update[len(update)-1],
}, nil
}
func (service *DebianUpdate) LatestBlock(lb *LatestBlock) (network.Message, onet.ClientError) {
if service.Storage.Timestamp == nil {
return nil, onet.NewClientErrorCode(4200, "Timestamp-service missing!")
}
gucRet, err := service.skipchain.GetUpdateChain(service.Storage.Root,
lb.LastKnownSB)
if err != nil {
return nil, onet.NewClientError(err)
}
return &LatestBlockRet{service.Storage.Timestamp, gucRet.Update}, nil
}
func (service *DebianUpdate) LatestBlockFromName(lbr *LatestBlockRepo) (network.Message, onet.ClientError) {
repoName := lbr.RepoName
chain := service.Storage.RepositoryChain[repoName]
if chain == nil {
return nil, onet.NewClientErrorCode(4200, "skipchain not found for "+repoName)
}
return service.LatestBlock(&LatestBlock{chain.Data.Hash})
}
func (service *DebianUpdate) LatestBlocks(lbs *LatestBlocks) (network.Message, onet.ClientError) {
var updates []*skipchain.SkipBlock
var lengths []int64
var t *Timestamp
for _, id := range lbs.LastKnownSBs {
b, err := service.LatestBlock(&LatestBlock{id})
if err != nil {
return nil, onet.NewClientError(err)
}
lb := b.(*LatestBlockRet)
if len(lb.Update) > 1 {
updates = append(updates, lb.Update...)
lengths = append(lengths, int64(len(lb.Update)))
if t == nil {
t = lb.Timestamp
}
}
}
return &LatestBlocksRetInternal{t, updates, lengths}, nil
}