forked from notaryproject/notary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swizzler.go
757 lines (644 loc) · 21.6 KB
/
swizzler.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
package testutils
import (
"bytes"
"fmt"
"time"
"github.com/docker/go/canonical/json"
"github.com/theupdateframework/notary/cryptoservice"
"github.com/theupdateframework/notary/passphrase"
store "github.com/theupdateframework/notary/storage"
"github.com/theupdateframework/notary/trustmanager"
"github.com/theupdateframework/notary/tuf"
"github.com/theupdateframework/notary/tuf/data"
"github.com/theupdateframework/notary/tuf/signed"
)
// ErrNoKeyForRole returns an error when the cryptoservice provided to
// MetadataSwizzler has no key for a particular role
type ErrNoKeyForRole struct {
Role data.RoleName
}
func (e ErrNoKeyForRole) Error() string {
return "Swizzler's cryptoservice has no key for role " + e.Role.String()
}
// MetadataSwizzler fuzzes the metadata in a MetadataStore
type MetadataSwizzler struct {
Gun data.GUN
MetadataCache store.MetadataStore
CryptoService signed.CryptoService
Roles []data.RoleName // list of Roles in the metadataStore
}
func getPubKeys(cs signed.CryptoService, s *data.Signed, role data.RoleName) ([]data.PublicKey, error) {
var pubKeys []data.PublicKey
if role == data.CanonicalRootRole {
// if this is root metadata, we have to get the keys from the root because they
// are certs
root := &data.Root{}
if err := json.Unmarshal(*s.Signed, root); err != nil {
return nil, err
}
rootRole, ok := root.Roles[data.CanonicalRootRole]
if !ok || rootRole == nil {
return nil, tuf.ErrNotLoaded{}
}
for _, pubKeyID := range rootRole.KeyIDs {
pubKeys = append(pubKeys, root.Keys[pubKeyID])
}
} else {
pubKeyIDs := cs.ListKeys(role)
for _, pubKeyID := range pubKeyIDs {
pubKey := cs.GetKey(pubKeyID)
if pubKey != nil {
pubKeys = append(pubKeys, pubKey)
}
}
}
return pubKeys, nil
}
// signs the new metadata, replacing whatever signature was there
func serializeMetadata(cs signed.CryptoService, s *data.Signed, role data.RoleName,
pubKeys ...data.PublicKey) ([]byte, error) {
// delete the existing signatures
s.Signatures = []data.Signature{}
if len(pubKeys) < 1 {
return nil, ErrNoKeyForRole{role}
}
if err := signed.Sign(cs, s, pubKeys, 1, nil); err != nil {
if _, ok := err.(signed.ErrInsufficientSignatures); ok {
return nil, ErrNoKeyForRole{Role: role}
}
return nil, err
}
metaBytes, err := json.Marshal(s)
if err != nil {
return nil, err
}
return metaBytes, nil
}
// gets a Signed from the metadata store
func signedFromStore(cache store.MetadataStore, role data.RoleName) (*data.Signed, error) {
b, err := cache.GetSized(role.String(), store.NoSizeLimit)
if err != nil {
return nil, err
}
signed := &data.Signed{}
if err := json.Unmarshal(b, signed); err != nil {
return nil, err
}
return signed, nil
}
// NewMetadataSwizzler returns a new swizzler when given a gun,
// mapping of roles to initial metadata bytes, and a cryptoservice
func NewMetadataSwizzler(gun data.GUN, initialMetadata map[data.RoleName][]byte,
cryptoService signed.CryptoService) *MetadataSwizzler {
var roles []data.RoleName
for roleName := range initialMetadata {
roles = append(roles, roleName)
}
return &MetadataSwizzler{
Gun: gun,
MetadataCache: store.NewMemoryStore(initialMetadata),
CryptoService: cryptoService,
Roles: roles,
}
}
// SetInvalidJSON corrupts metadata into something that is no longer valid JSON
func (m *MetadataSwizzler) SetInvalidJSON(role data.RoleName) error {
metaBytes, err := m.MetadataCache.GetSized(role.String(), store.NoSizeLimit)
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes[5:])
}
// AddExtraSpace adds an extra space to the beginning and end of the serialized
// JSON bytes, which should not affect serialization, but will change the checksum
// of the file.
func (m *MetadataSwizzler) AddExtraSpace(role data.RoleName) error {
metaBytes, err := m.MetadataCache.GetSized(role.String(), store.NoSizeLimit)
if err != nil {
return err
}
newBytes := append(append([]byte{' '}, metaBytes...), ' ')
return m.MetadataCache.Set(role.String(), newBytes)
}
// SetInvalidSigned corrupts the metadata into something that is valid JSON,
// but not unmarshallable into signed JSON
func (m *MetadataSwizzler) SetInvalidSigned(role data.RoleName) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
metaBytes, err := json.MarshalCanonical(map[string]interface{}{
"signed": signedThing.Signed,
"signatures": "not list",
})
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// SetInvalidSignedMeta corrupts the metadata into something that is unmarshallable
// as a Signed object, but not unmarshallable into a SignedMeta object
func (m *MetadataSwizzler) SetInvalidSignedMeta(role data.RoleName) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, signedThing, role)
if err != nil {
return err
}
var unmarshalled map[string]interface{}
if err := json.Unmarshal(*signedThing.Signed, &unmarshalled); err != nil {
return err
}
unmarshalled["_type"] = []string{"not a string"}
unmarshalled["version"] = "string not int"
unmarshalled["expires"] = "cannot be parsed as time"
metaBytes, err := json.MarshalCanonical(unmarshalled)
if err != nil {
return err
}
signedThing.Signed = (*json.RawMessage)(&metaBytes)
metaBytes, err = serializeMetadata(m.CryptoService, signedThing, role, pubKeys...)
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// TODO: corrupt metadata in such a way that it can be unmarshalled as a
// SignedMeta, but not as a SignedRoot or SignedTarget, etc. (Signed*)
// SetInvalidMetadataType unmarshallable, but has the wrong metadata type (not
// actually a metadata type)
func (m *MetadataSwizzler) SetInvalidMetadataType(role data.RoleName) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
var unmarshalled map[string]interface{}
if err := json.Unmarshal(*signedThing.Signed, &unmarshalled); err != nil {
return err
}
unmarshalled["_type"] = "not_real"
metaBytes, err := json.MarshalCanonical(unmarshalled)
if err != nil {
return err
}
signedThing.Signed = (*json.RawMessage)(&metaBytes)
pubKeys, err := getPubKeys(m.CryptoService, signedThing, role)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, signedThing, role, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// InvalidateMetadataSignatures signs with the right key(s) but wrong hash
func (m *MetadataSwizzler) InvalidateMetadataSignatures(role data.RoleName) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
sigs := make([]data.Signature, len(signedThing.Signatures))
for i, origSig := range signedThing.Signatures {
sigs[i] = data.Signature{
KeyID: origSig.KeyID,
Signature: []byte("invalid signature"),
Method: origSig.Method,
}
}
signedThing.Signatures = sigs
metaBytes, err := json.Marshal(signedThing)
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// TODO: AddExtraSignedInfo - add an extra field to Signed that doesn't get
// unmarshalled, and the whole thing is correctly signed, so shouldn't cause
// problems there. Should this fail a canonical JSON check?
// RemoveMetadata deletes the metadata entirely
func (m *MetadataSwizzler) RemoveMetadata(role data.RoleName) error {
return m.MetadataCache.Remove(role.String())
}
// SignMetadataWithInvalidKey signs the metadata with the wrong key
func (m *MetadataSwizzler) SignMetadataWithInvalidKey(role data.RoleName) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
// create an invalid key, but not in the existing CryptoService
cs := cryptoservice.NewCryptoService(trustmanager.NewKeyMemoryStore(passphrase.ConstantRetriever("")))
key, err := CreateKey(cs, m.Gun, role, data.ECDSAKey)
if err != nil {
return err
}
metaBytes, err := serializeMetadata(cs, signedThing, "root", key)
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// OffsetMetadataVersion updates the metadata version
func (m *MetadataSwizzler) OffsetMetadataVersion(role data.RoleName, offset int) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
var unmarshalled map[string]interface{}
if err := json.Unmarshal(*signedThing.Signed, &unmarshalled); err != nil {
return err
}
if role == data.CanonicalRootRole {
// store old versions of roots accessible by version
version, ok := unmarshalled["version"].(float64)
if !ok {
version = float64(0) // just ignore the error and set it to 0
}
versionedRole := fmt.Sprintf("%d.%s", int(version), data.CanonicalRootRole)
pubKeys, err := getPubKeys(m.CryptoService, signedThing, role)
if err != nil {
return err
}
versionedMetaBytes, err := serializeMetadata(m.CryptoService, signedThing, role, pubKeys...)
if err != nil {
return err
}
err = m.MetadataCache.Set(versionedRole, versionedMetaBytes)
if err != nil {
return err
}
}
oldVersion, ok := unmarshalled["version"].(float64)
if !ok {
oldVersion = float64(0) // just ignore the error and set it to 0
}
unmarshalled["version"] = int(oldVersion) + offset
metaBytes, err := json.MarshalCanonical(unmarshalled)
if err != nil {
return err
}
signedThing.Signed = (*json.RawMessage)(&metaBytes)
pubKeys, err := getPubKeys(m.CryptoService, signedThing, role)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, signedThing, role, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// ExpireMetadata expires the metadata, which would make it invalid - don't do anything if
// we don't have the timestamp key
func (m *MetadataSwizzler) ExpireMetadata(role data.RoleName) error {
signedThing, err := signedFromStore(m.MetadataCache, role)
if err != nil {
return err
}
var unmarshalled map[string]interface{}
if err := json.Unmarshal(*signedThing.Signed, &unmarshalled); err != nil {
return err
}
unmarshalled["expires"] = time.Now().AddDate(-1, -1, -1)
metaBytes, err := json.MarshalCanonical(unmarshalled)
if err != nil {
return err
}
signedThing.Signed = (*json.RawMessage)(&metaBytes)
pubKeys, err := getPubKeys(m.CryptoService, signedThing, role)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, signedThing, role, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(role.String(), metaBytes)
}
// SetThreshold sets a threshold for a metadata role - can invalidate metadata for which
// the threshold is increased, if there aren't enough signatures or can be invalid because
// the threshold is 0
func (m *MetadataSwizzler) SetThreshold(role data.RoleName, newThreshold int) error {
roleSpecifier := data.CanonicalRootRole
if data.IsDelegation(role) {
roleSpecifier = role.Parent()
}
b, err := m.MetadataCache.GetSized(roleSpecifier.String(), store.NoSizeLimit)
if err != nil {
return err
}
signedThing := &data.Signed{}
if err := json.Unmarshal(b, signedThing); err != nil {
return err
}
if roleSpecifier == data.CanonicalRootRole {
signedRoot, err := data.RootFromSigned(signedThing)
if err != nil {
return err
}
signedRoot.Signed.Roles[role].Threshold = newThreshold
if signedThing, err = signedRoot.ToSigned(); err != nil {
return err
}
} else {
signedTargets, err := data.TargetsFromSigned(signedThing, roleSpecifier)
if err != nil {
return err
}
for _, roleObject := range signedTargets.Signed.Delegations.Roles {
if roleObject.Name == role {
roleObject.Threshold = newThreshold
break
}
}
if signedThing, err = signedTargets.ToSigned(); err != nil {
return err
}
}
var metaBytes []byte
pubKeys, err := getPubKeys(m.CryptoService, signedThing, roleSpecifier)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, signedThing, roleSpecifier, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(roleSpecifier.String(), metaBytes)
}
// RotateKey rotates the key for a role - this can invalidate that role's metadata
// if it is not signed by that key. Particularly if the key being rotated is the
// root key, because it is not signed by the new key, only the old key.
func (m *MetadataSwizzler) RotateKey(role data.RoleName, key data.PublicKey) error {
roleSpecifier := data.CanonicalRootRole
if data.IsDelegation(role) {
roleSpecifier = role.Parent()
}
b, err := m.MetadataCache.GetSized(roleSpecifier.String(), store.NoSizeLimit)
if err != nil {
return err
}
signedThing := &data.Signed{}
if err := json.Unmarshal(b, signedThing); err != nil {
return err
}
// get keys before the keys are rotated
pubKeys, err := getPubKeys(m.CryptoService, signedThing, roleSpecifier)
if err != nil {
return err
}
if roleSpecifier == data.CanonicalRootRole {
signedRoot, err := data.RootFromSigned(signedThing)
if err != nil {
return err
}
signedRoot.Signed.Roles[role].KeyIDs = []string{key.ID()}
signedRoot.Signed.Keys[key.ID()] = key
if signedThing, err = signedRoot.ToSigned(); err != nil {
return err
}
} else {
signedTargets, err := data.TargetsFromSigned(signedThing, roleSpecifier)
if err != nil {
return err
}
for _, roleObject := range signedTargets.Signed.Delegations.Roles {
if roleObject.Name == role {
roleObject.KeyIDs = []string{key.ID()}
break
}
}
signedTargets.Signed.Delegations.Keys[key.ID()] = key
if signedThing, err = signedTargets.ToSigned(); err != nil {
return err
}
}
metaBytes, err := serializeMetadata(m.CryptoService, signedThing, roleSpecifier, pubKeys...)
if err != nil {
return err
}
return m.MetadataCache.Set(roleSpecifier.String(), metaBytes)
}
// ChangeRootKey swaps out the root key with a new key, and re-signs the metadata
// with the new key
func (m *MetadataSwizzler) ChangeRootKey() error {
key, err := CreateKey(m.CryptoService, m.Gun, data.CanonicalRootRole, data.ECDSAKey)
if err != nil {
return err
}
b, err := m.MetadataCache.GetSized(data.CanonicalRootRole.String(), store.NoSizeLimit)
if err != nil {
return err
}
signedRoot := &data.SignedRoot{}
if err := json.Unmarshal(b, signedRoot); err != nil {
return err
}
signedRoot.Signed.Keys[key.ID()] = key
signedRoot.Signed.Roles[data.CanonicalRootRole].KeyIDs = []string{key.ID()}
var signedThing *data.Signed
if signedThing, err = signedRoot.ToSigned(); err != nil {
return err
}
var metaBytes []byte
pubKeys, err := getPubKeys(m.CryptoService, signedThing, data.CanonicalRootRole)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, signedThing, data.CanonicalRootRole, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalRootRole.String(), metaBytes)
}
// UpdateSnapshotHashes updates the snapshot to reflect the latest hash changes, to
// ensure that failure isn't because the snapshot has the wrong hash.
func (m *MetadataSwizzler) UpdateSnapshotHashes(roles ...data.RoleName) error {
var (
metaBytes []byte
snapshotSigned *data.Signed
err error
)
if metaBytes, err = m.MetadataCache.GetSized(data.CanonicalSnapshotRole.String(), store.NoSizeLimit); err != nil {
return err
}
snapshot := data.SignedSnapshot{}
if err = json.Unmarshal(metaBytes, &snapshot); err != nil {
return err
}
// just rebuild everything if roles is not specified
if len(roles) == 0 {
roles = m.Roles
}
for _, role := range roles {
if role != data.CanonicalSnapshotRole && role != data.CanonicalTimestampRole {
if metaBytes, err = m.MetadataCache.GetSized(role.String(), store.NoSizeLimit); err != nil {
return err
}
meta, err := data.NewFileMeta(bytes.NewReader(metaBytes), data.NotaryDefaultHashes...)
if err != nil {
return err
}
snapshot.Signed.Meta[role.String()] = meta
}
}
if snapshotSigned, err = snapshot.ToSigned(); err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, snapshotSigned, data.CanonicalSnapshotRole)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, snapshotSigned, data.CanonicalSnapshotRole, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalSnapshotRole.String(), metaBytes)
}
// UpdateTimestampHash updates the timestamp to reflect the latest snapshot changes, to
// ensure that failure isn't because the timestamp has the wrong hash.
func (m *MetadataSwizzler) UpdateTimestampHash() error {
var (
metaBytes []byte
timestamp = &data.SignedTimestamp{}
timestampSigned *data.Signed
err error
)
if metaBytes, err = m.MetadataCache.GetSized(data.CanonicalTimestampRole.String(), store.NoSizeLimit); err != nil {
return err
}
// we can't just create a new timestamp, because then the expiry would be
// different
if err = json.Unmarshal(metaBytes, timestamp); err != nil {
return err
}
if metaBytes, err = m.MetadataCache.GetSized(data.CanonicalSnapshotRole.String(), store.NoSizeLimit); err != nil {
return err
}
snapshotMeta, err := data.NewFileMeta(bytes.NewReader(metaBytes), data.NotaryDefaultHashes...)
if err != nil {
return err
}
timestamp.Signed.Meta[data.CanonicalSnapshotRole.String()] = snapshotMeta
timestampSigned, err = timestamp.ToSigned()
if err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, timestampSigned, data.CanonicalTimestampRole)
if err == nil {
metaBytes, err = serializeMetadata(m.CryptoService, timestampSigned, data.CanonicalTimestampRole, pubKeys...)
}
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalTimestampRole.String(), metaBytes)
}
// MutateRoot takes a function that mutates the root metadata - once done, it
// serializes the root again
func (m *MetadataSwizzler) MutateRoot(mutate func(*data.Root)) error {
signedThing, err := signedFromStore(m.MetadataCache, data.CanonicalRootRole)
if err != nil {
return err
}
var root data.Root
if err := json.Unmarshal(*signedThing.Signed, &root); err != nil {
return err
}
// get the original keys, in case the mutation messes with the signing keys
oldPubKeys, err := getPubKeys(m.CryptoService, signedThing, data.CanonicalRootRole)
if err != nil {
return err
}
mutate(&root)
sRoot := &data.SignedRoot{Signed: root, Signatures: signedThing.Signatures}
signedThing, err = sRoot.ToSigned()
if err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, signedThing, data.CanonicalRootRole)
if err != nil || len(pubKeys) == 0 { // we have to sign it somehow - might as well use the old keys
pubKeys = oldPubKeys
}
metaBytes, err := serializeMetadata(m.CryptoService, signedThing, data.CanonicalRootRole, pubKeys...)
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalRootRole.String(), metaBytes)
}
// MutateTimestamp takes a function that mutates the timestamp metadata - once done, it
// serializes the timestamp again
func (m *MetadataSwizzler) MutateTimestamp(mutate func(*data.Timestamp)) error {
signedThing, err := signedFromStore(m.MetadataCache, data.CanonicalTimestampRole)
if err != nil {
return err
}
var timestamp data.Timestamp
if err := json.Unmarshal(*signedThing.Signed, ×tamp); err != nil {
return err
}
mutate(×tamp)
sTimestamp := &data.SignedTimestamp{Signed: timestamp, Signatures: signedThing.Signatures}
signedThing, err = sTimestamp.ToSigned()
if err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, signedThing, data.CanonicalTimestampRole)
if err != nil {
return err
}
metaBytes, err := serializeMetadata(m.CryptoService, signedThing, data.CanonicalTimestampRole, pubKeys...)
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalTimestampRole.String(), metaBytes)
}
// MutateSnapshot takes a function that mutates the snapshot metadata - once done, it
// serializes the snapshot again
func (m *MetadataSwizzler) MutateSnapshot(mutate func(*data.Snapshot)) error {
signedThing, err := signedFromStore(m.MetadataCache, data.CanonicalSnapshotRole)
if err != nil {
return err
}
var snapshot data.Snapshot
if err := json.Unmarshal(*signedThing.Signed, &snapshot); err != nil {
return err
}
mutate(&snapshot)
sSnapshot := &data.SignedSnapshot{Signed: snapshot, Signatures: signedThing.Signatures}
signedThing, err = sSnapshot.ToSigned()
if err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, signedThing, data.CanonicalSnapshotRole)
if err != nil {
return err
}
metaBytes, err := serializeMetadata(m.CryptoService, signedThing, data.CanonicalSnapshotRole, pubKeys...)
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalSnapshotRole.String(), metaBytes)
}
// MutateTargets takes a function that mutates the targets metadata - once done, it
// serializes the targets again
func (m *MetadataSwizzler) MutateTargets(mutate func(*data.Targets)) error {
signedThing, err := signedFromStore(m.MetadataCache, data.CanonicalTargetsRole)
if err != nil {
return err
}
var targets data.Targets
if err := json.Unmarshal(*signedThing.Signed, &targets); err != nil {
return err
}
mutate(&targets)
sTargets := &data.SignedTargets{Signed: targets, Signatures: signedThing.Signatures}
signedThing, err = sTargets.ToSigned()
if err != nil {
return err
}
pubKeys, err := getPubKeys(m.CryptoService, signedThing, data.CanonicalTargetsRole)
if err != nil {
return err
}
metaBytes, err := serializeMetadata(m.CryptoService, signedThing, data.CanonicalTargetsRole, pubKeys...)
if err != nil {
return err
}
return m.MetadataCache.Set(data.CanonicalTargetsRole.String(), metaBytes)
}