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 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
|
package client
import (
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/secure-systems-lab/go-securesystemslib/cjson"
"github.com/stretchr/testify/assert"
tuf "github.com/theupdateframework/go-tuf"
"github.com/theupdateframework/go-tuf/data"
"github.com/theupdateframework/go-tuf/internal/sets"
"github.com/theupdateframework/go-tuf/pkg/keys"
"github.com/theupdateframework/go-tuf/sign"
"github.com/theupdateframework/go-tuf/util"
"github.com/theupdateframework/go-tuf/verify"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type ClientSuite struct {
store tuf.LocalStore
repo *tuf.Repo
local LocalStore
remote RemoteStore
expiredTime time.Time
keyIDs map[string][]string
useFileStore bool
// Only used with FileStore
tmpDir string
}
var _ = Suite(&ClientSuite{useFileStore: false})
var _ = Suite(&ClientSuite{useFileStore: true})
func newFakeRemoteStore() *fakeRemoteStore {
return &fakeRemoteStore{
meta: make(map[string]*fakeFile),
targets: make(map[string]*fakeFile),
}
}
type fakeRemoteStore struct {
meta map[string]*fakeFile
targets map[string]*fakeFile
}
func (f *fakeRemoteStore) GetMeta(name string) (io.ReadCloser, int64, error) {
return f.get(name, f.meta)
}
func (f *fakeRemoteStore) GetTarget(path string) (io.ReadCloser, int64, error) {
return f.get(path, f.targets)
}
func (f *fakeRemoteStore) get(name string, store map[string]*fakeFile) (io.ReadCloser, int64, error) {
file, ok := store[name]
if !ok {
return nil, 0, ErrNotFound{name}
}
return file, file.size, nil
}
// These are helper methods for manipulating the internals of the Stores
// because the set/delete methods are not part of the Interface, we need to
// switch on the underlying implementation.
// Also readMeta method is convenience for ease of testing.
func (s *ClientSuite) setRemoteMeta(path string, data []byte) error {
switch impl := s.remote.(type) {
case *fakeRemoteStore:
impl.meta[path] = newFakeFile(data)
return nil
case *FileRemoteStore:
return impl.addMeta(path, data)
default:
return fmt.Errorf("non-supoprted RemoteStore, got %+v", impl)
}
}
func (s *ClientSuite) setRemoteTarget(path string, data []byte) error {
switch impl := s.remote.(type) {
case *fakeRemoteStore:
impl.targets[path] = newFakeFile(data)
return nil
case *FileRemoteStore:
return impl.addTarget(path, data)
default:
return fmt.Errorf("non-supoprted RemoteStore, got %+v", impl)
}
}
func (s *ClientSuite) deleteMeta(path string) error {
switch impl := s.remote.(type) {
case *fakeRemoteStore:
delete(impl.meta, path)
return nil
case *FileRemoteStore:
return impl.deleteMeta(path)
default:
return fmt.Errorf("non-supported RemoteStore, got %+v", impl)
}
}
func (s *ClientSuite) deleteTarget(path string) error {
switch impl := s.remote.(type) {
case *fakeRemoteStore:
delete(impl.targets, path)
return nil
case *FileRemoteStore:
return impl.deleteTarget(path)
default:
return fmt.Errorf("non-supported RemoteStore, got %+v", impl)
}
}
func (s *ClientSuite) readMeta(name string) ([]byte, error) {
stream, _, err := s.remote.GetMeta(name)
if err != nil {
return nil, err
}
return io.ReadAll(stream)
}
func newFakeFile(b []byte) *fakeFile {
return &fakeFile{buf: bytes.NewReader(b), size: int64(len(b))}
}
type fakeFile struct {
buf *bytes.Reader
bytesRead int
size int64
}
func (f *fakeFile) Read(p []byte) (int, error) {
n, err := f.buf.Read(p)
f.bytesRead += n
return n, err
}
func (f *fakeFile) Close() error {
f.buf.Seek(0, io.SeekStart)
return nil
}
var targetFiles = map[string][]byte{
"foo.txt": []byte("foo"),
"bar.txt": []byte("bar"),
"baz.txt": []byte("baz"),
}
func (s *ClientSuite) SetUpTest(c *C) {
s.store = tuf.MemoryStore(nil, targetFiles)
// create a valid repo containing foo.txt
var err error
s.repo, err = tuf.NewRepo(s.store)
c.Assert(err, IsNil)
// don't use consistent snapshots to make testing easier (consistent
// snapshots are tested explicitly elsewhere)
c.Assert(s.repo.Init(false), IsNil)
s.keyIDs = map[string][]string{
"root": s.genKey(c, "root"),
"targets": s.genKey(c, "targets"),
"snapshot": s.genKey(c, "snapshot"),
"timestamp": s.genKey(c, "timestamp"),
}
c.Assert(s.repo.AddTarget("foo.txt", nil), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
// create a remote store containing valid repo files
if s.useFileStore {
s.remote, s.tmpDir, err = newTestFileStoreFS()
if err != nil {
c.Fatalf("failed to create new FileStore: %v", err)
}
} else {
s.remote = newFakeRemoteStore()
}
s.syncRemote(c)
for path, data := range targetFiles {
s.setRemoteTarget(path, data)
}
s.expiredTime = time.Now().Add(time.Hour)
}
func (s *ClientSuite) TearDownTest(c *C) {
if s.tmpDir != "" {
rmrf(s.tmpDir, c.Logf)
}
}
func (s *ClientSuite) genKey(c *C, role string) []string {
ids, err := s.repo.GenKey(role)
c.Assert(err, IsNil)
return ids
}
func (s *ClientSuite) genKeyExpired(c *C, role string) []string {
ids, err := s.repo.GenKeyWithExpires(role, s.expiredTime)
c.Assert(err, IsNil)
return ids
}
// withMetaExpired sets signed.IsExpired throughout the invocation of f so that
// any metadata marked to expire at s.expiredTime will be expired (this avoids
// the need to sleep in the tests).
func (s *ClientSuite) withMetaExpired(f func()) {
e := verify.IsExpired
defer func() { verify.IsExpired = e }()
verify.IsExpired = func(t time.Time) bool {
return t.Unix() == s.expiredTime.Round(time.Second).Unix()
}
f()
}
func (s *ClientSuite) syncLocal(c *C) {
meta, err := s.store.GetMeta()
c.Assert(err, IsNil)
for k, v := range meta {
c.Assert(s.local.SetMeta(k, v), IsNil)
}
}
func (s *ClientSuite) syncRemote(c *C) {
meta, err := s.store.GetMeta()
c.Assert(err, IsNil)
for name, data := range meta {
if err := s.setRemoteMeta(name, data); err != nil {
panic(fmt.Sprintf("setMetadata failed: %v", err))
}
}
}
func (s *ClientSuite) addRemoteTarget(c *C, name string) {
c.Assert(s.repo.AddTarget(name, nil), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
}
func (s *ClientSuite) rootMeta(c *C) []byte {
meta, err := s.repo.GetMeta()
c.Assert(err, IsNil)
rootMeta, ok := meta["root.json"]
c.Assert(ok, Equals, true)
return rootMeta
}
func (s *ClientSuite) newClient(c *C) *Client {
s.local = MemoryLocalStore()
client := NewClient(s.local, s.remote)
c.Assert(client.Init(s.rootMeta(c)), IsNil)
return client
}
func (s *ClientSuite) updatedClient(c *C) *Client {
client := s.newClient(c)
_, err := client.Update()
c.Assert(err, IsNil)
return client
}
func assertFile(c *C, file data.TargetFileMeta, name string) {
target, ok := targetFiles[name]
if !ok {
c.Fatalf("unknown target %s", name)
}
meta, err := util.GenerateTargetFileMeta(bytes.NewReader(target), file.HashAlgorithms()...)
c.Assert(err, IsNil)
c.Assert(util.TargetFileMetaEqual(file, meta), IsNil)
}
func assertFiles(c *C, files data.TargetFiles, names []string) {
c.Assert(files, HasLen, len(names))
for _, name := range names {
file, ok := files[name]
if !ok {
c.Fatalf("expected files to contain %s", name)
}
assertFile(c, file, name)
}
}
func assertWrongHash(c *C, err error) {
// just test the type of err rather using DeepEquals as it contains
// hashes we don't necessarily need to check.
e, ok := err.(ErrDownloadFailed)
if !ok {
c.Fatalf("expected err to have type ErrDownloadFailed, got %T", err)
}
if _, ok := e.Err.(util.ErrWrongHash); !ok {
c.Fatalf("expected err.Err to have type util.ErrWrongHash, got %T", err)
}
}
func (s *ClientSuite) assertErrExpired(c *C, err error, file string) {
decodeErr, ok := err.(ErrDecodeFailed)
if !ok {
c.Fatalf("expected err to have type ErrDecodeFailed, got %T", err)
}
c.Assert(decodeErr.File, Equals, file)
expiredErr, ok := decodeErr.Err.(verify.ErrExpired)
if !ok {
c.Fatalf("expected err.Err to have type signed.ErrExpired, got %T", err)
}
c.Assert(expiredErr.Expired.Unix(), Equals, s.expiredTime.Round(time.Second).Unix())
}
func (s *ClientSuite) TestInitAllowsExpired(c *C) {
s.genKeyExpired(c, "targets")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
client := NewClient(MemoryLocalStore(), s.remote)
bytes, err := s.readMeta("root.json")
c.Assert(err, IsNil)
s.withMetaExpired(func() {
c.Assert(client.Init(bytes), IsNil)
})
}
func (s *ClientSuite) TestInit(c *C) {
client := NewClient(MemoryLocalStore(), s.remote)
bytes, err := s.readMeta("root.json")
c.Assert(err, IsNil)
dataSigned := &data.Signed{}
c.Assert(json.Unmarshal(bytes, dataSigned), IsNil)
root := &data.Root{}
c.Assert(json.Unmarshal(dataSigned.Signed, root), IsNil)
// check Update() returns ErrNoRootKeys when uninitialized
_, err = client.Update()
c.Assert(err, Equals, ErrNoRootKeys)
// check Init() returns ErrRoleThreshold when the root's signature is
// invalid
// modify root and marshal without regenerating signatures
root.Version = root.Version + 1
rootBytes, err := json.Marshal(root)
c.Assert(err, IsNil)
dataSigned.Signed = rootBytes
dataBytes, err := json.Marshal(dataSigned)
c.Assert(err, IsNil)
c.Assert(client.Init(dataBytes), Equals, verify.ErrRoleThreshold{
Expected: 1, Actual: 0})
// check Update() does not return ErrNoRootKeys after initialization
c.Assert(client.Init(bytes), IsNil)
_, err = client.Update()
c.Assert(err, IsNil)
}
// This is a regression test for https://github.com/theupdateframework/go-tuf/issues/370
// where a single invalid signature resulted in an early return.
// Instead, the client should have continued and counted the number
// of valid signatures, ignoring the incorrect one.
func (s *ClientSuite) TestExtraRootSignaturesOnInit(c *C) {
client := NewClient(MemoryLocalStore(), s.remote)
bytes, err := s.readMeta("root.json")
c.Assert(err, IsNil)
dataSigned := &data.Signed{}
c.Assert(json.Unmarshal(bytes, dataSigned), IsNil)
// check Init() succeeds when an extra invalid signature was
// added to the root.
dataSigned.Signatures = append(dataSigned.Signatures,
data.Signature{
KeyID: dataSigned.Signatures[0].KeyID,
Signature: make([]byte, ed25519.SignatureSize),
})
dataBytes, err := json.Marshal(dataSigned)
c.Assert(err, IsNil)
c.Assert(client.Init(dataBytes), IsNil)
}
func (s *ClientSuite) TestFirstUpdate(c *C) {
files, err := s.newClient(c).Update()
c.Assert(err, IsNil)
c.Assert(files, HasLen, 1)
assertFiles(c, files, []string{"foo.txt"})
}
func (s *ClientSuite) TestMissingRemoteMetadata(c *C) {
client := s.newClient(c)
s.deleteMeta("targets.json")
_, err := client.Update()
c.Assert(err, Equals, ErrMissingRemoteMetadata{"targets.json"})
s.deleteMeta("timestamp.json")
_, err = client.Update()
c.Assert(err, Equals, ErrMissingRemoteMetadata{"timestamp.json"})
}
func (s *ClientSuite) TestNoChangeUpdate(c *C) {
client := s.newClient(c)
_, err := client.Update()
c.Assert(err, IsNil)
_, err = client.Update()
c.Assert(err, IsNil)
}
func (s *ClientSuite) TestNewTimestamp(c *C) {
client := s.updatedClient(c)
version := client.timestampVer
c.Assert(version > 0, Equals, true)
c.Assert(s.repo.Timestamp(), IsNil)
s.syncRemote(c)
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.timestampVer > version, Equals, true)
}
func (s *ClientSuite) TestNewRoot(c *C) {
client := s.newClient(c)
// replace all keys
newKeyIDs := make(map[string][]string)
for role, ids := range s.keyIDs {
c.Assert(len(ids) > 0, Equals, true)
c.Assert(s.repo.RevokeKey(role, ids[0]), IsNil)
newKeyIDs[role] = s.genKey(c, role)
}
// update metadata
c.Assert(s.repo.Sign("targets.json"), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update gets new root version
c.Assert(client.getLocalMeta(), IsNil)
version := client.rootVer
c.Assert(version > 0, Equals, true)
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.rootVer > version, Equals, true)
// check old keys are not in db
for _, ids := range s.keyIDs {
c.Assert(len(ids) > 0, Equals, true)
for _, id := range ids {
_, err := client.db.GetVerifier(id)
c.Assert(err, NotNil)
}
}
// check new keys are in db
for name, ids := range newKeyIDs {
c.Assert(len(ids) > 0, Equals, true)
for _, id := range ids {
verifier, err := client.db.GetVerifier(id)
c.Assert(err, IsNil)
c.Assert(verifier.MarshalPublicKey().IDs(), DeepEquals, ids)
}
role := client.db.GetRole(name)
c.Assert(role, NotNil)
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(ids))
}
}
// This is a regression test for https://github.com/theupdateframework/go-tuf/issues/370
// where a single invalid signature resulted in an early return.
// Instead, the client should have continued and counted the number
// of valid signatures, ignoring the incorrect one.
func (s *ClientSuite) TestExtraSignaturesOnRootUpdate(c *C) {
client := s.newClient(c)
// Add an extra root key to update the root to a new version.
s.genKey(c, "root")
// update metadata
c.Assert(s.repo.Sign("targets.json"), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// Add an extra signature to the new remote root.
bytes, err := s.readMeta("root.json")
c.Assert(err, IsNil)
dataSigned := &data.Signed{}
c.Assert(json.Unmarshal(bytes, dataSigned), IsNil)
dataSigned.Signatures = append(dataSigned.Signatures,
data.Signature{
KeyID: dataSigned.Signatures[0].KeyID,
Signature: make([]byte, ed25519.SignatureSize),
})
dataBytes, err := json.Marshal(dataSigned)
c.Assert(err, IsNil)
s.setRemoteMeta("root.json", dataBytes)
s.setRemoteMeta("2.root.json", dataBytes)
// check Update() succeeds when an extra invalid signature was
// added to the root.
_, err = client.Update()
c.Assert(err, IsNil)
c.Assert(client.rootVer, Equals, int64(2))
}
// startTUFRepoServer starts a HTTP server to serve a TUF Repo.
func startTUFRepoServer(baseDir string, relPath string) (net.Listener, error) {
serverDir := filepath.Join(baseDir, relPath)
l, err := net.Listen("tcp", "127.0.0.1:0")
go http.Serve(l, http.FileServer(http.Dir(serverDir)))
return l, err
}
// newClientWithMeta creates new client and sets the root metadata for it.
func newClientWithMeta(baseDir string, relPath string, serverAddr string) (*Client, error) {
initialStateDir := filepath.Join(baseDir, relPath)
opts := &HTTPRemoteOptions{
MetadataPath: "metadata",
TargetsPath: "targets",
}
remote, err := HTTPRemoteStore(fmt.Sprintf("http://%s/", serverAddr), opts, nil)
if err != nil {
return nil, err
}
c := NewClient(MemoryLocalStore(), remote)
for _, m := range []string{"root.json", "snapshot.json", "timestamp.json", "targets.json"} {
if _, err := os.Stat(initialStateDir + "/" + m); err == nil {
metadataJSON, err := os.ReadFile(initialStateDir + "/" + m)
if err != nil {
return nil, err
}
c.local.SetMeta(m, metadataJSON)
}
}
return c, nil
}
func initRootTest(c *C, baseDir string) (*Client, func() error) {
l, err := startTUFRepoServer(baseDir, "server")
c.Assert(err, IsNil)
tufClient, err := newClientWithMeta(baseDir, "client/metadata/current", l.Addr().String())
c.Assert(err, IsNil)
return tufClient, l.Close
}
func (s *ClientSuite) TestUpdateRoots(c *C) {
var tests = []struct {
fixturePath string
expectedError error
expectedVersions map[string]int64
}{
// Succeeds when there is no root update.
{"testdata/Published1Time", nil, map[string]int64{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds when client only has root.json
{"testdata/Published1Time_client_root_only", nil, map[string]int64{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 1 to version 2.
{"testdata/Published2Times_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 1 to version 2 when the client's initial root version is expired.
{"testdata/Published2Times_keyrotated_initialrootexpired", nil, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 1 to version 3 when versions 1 and 2 are expired.
{"testdata/Published3Times_keyrotated_initialrootsexpired", nil, map[string]int64{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 2 to version 3.
{"testdata/Published3Times_keyrotated_initialrootsexpired_clientversionis2", nil, map[string]int64{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Fails updating root from version 1 to version 3 when versions 1 and 3 are expired but version 2 is not expired.
{"testdata/Published3Times_keyrotated_latestrootexpired", ErrDecodeFailed{File: "root.json", Err: verify.ErrExpired{}}, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Fails updating root from version 1 to version 2 when old root 1 did not sign off on it (nth root didn't sign off n+1).
// TODO(asraa): This testcase should have revoked the old key!
// https://github.com/theupdateframework/go-tuf/issues/417
{"testdata/Published2Times_keyrotated_invalidOldRootSignature", nil, map[string]int64{}},
// Fails updating root from version 1 to version 2 when the new root 2 did not sign itself (n+1th root didn't sign off n+1)
{"testdata/Published2Times_keyrotated_invalidNewRootSignature", verify.ErrRoleThreshold{Expected: 1, Actual: 0}, map[string]int64{}},
// Fails updating root to 2.root.json when the value of the version field inside it is 1 (rollback attack prevention).
{"testdata/Published1Time_backwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 1, Expected: 2}), map[string]int64{}},
// Fails updating root to 2.root.json when the value of the version field inside it is 3 (rollforward attack prevention).
{"testdata/Published3Times_keyrotated_forwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 3, Expected: 2}), map[string]int64{}},
// Fails updating when there is no local trusted root.
{"testdata/Published1Time_client_no_root", errors.New("tuf: no root keys found in local meta store"), map[string]int64{}},
// snapshot role key rotation increase the snapshot and timestamp.
{"testdata/Published2Times_snapshot_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 1}},
// targets role key rotation increase the snapshot, timestamp, and targets.
{"testdata/Published2Times_targets_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 2}},
// timestamp role key rotation increase the timestamp.
{"testdata/Published2Times_timestamp_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 1, "targets": 1}},
//root file size > defaultRootDownloadLimit
{"testdata/Published2Times_roottoolarge", ErrMetaTooLarge{Name: "2.root.json", Size: defaultRootDownloadLimit + 1, MaxSize: defaultRootDownloadLimit}, map[string]int64{}},
}
for _, test := range tests {
tufClient, closer := initRootTest(c, test.fixturePath)
_, err := tufClient.Update()
if test.expectedError == nil {
c.Assert(err, IsNil)
// Check if the root.json is being saved in non-volatile storage.
tufClient.getLocalMeta()
versionMethods := map[string]int64{"root": tufClient.rootVer,
"timestamp": tufClient.timestampVer,
"snapshot": tufClient.snapshotVer,
"targets": tufClient.targetsVer}
for m, v := range test.expectedVersions {
assert.Equal(c, v, versionMethods[m])
}
} else {
// For backward compatibility, the update root returns
// ErrDecodeFailed that wraps the verify.ErrExpired.
if _, ok := test.expectedError.(ErrDecodeFailed); ok {
decodeErr, ok := err.(ErrDecodeFailed)
c.Assert(ok, Equals, true)
c.Assert(decodeErr.File, Equals, "root.json")
_, ok = decodeErr.Err.(verify.ErrExpired)
c.Assert(ok, Equals, true)
} else {
assert.Equal(c, test.expectedError, err)
}
}
closer()
}
}
func (s *ClientSuite) TestFastForwardAttackRecovery(c *C) {
var tests = []struct {
fixturePath string
expectMetaDeleted map[string]bool
}{
// Each of the following test cases each has a two sets of TUF metadata:
// (1) client's initial, and (2) server's current.
// The naming format is PublishedTwiceMultiKeysadd_X_revoke_Y_threshold_Z_ROLE
// The client includes TUF metadata before key rotation for TUF ROLE with X keys.
// The server includes updated TUF metadata after key rotation. The
// rotation involves revoking Y keys from the initial keys.
// For each test, the TUF client's will be initialized to the client files.
// The test checks whether the client is able to update itself properly.
// Fast-forward recovery is not needed if less than threshold keys are revoked.
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_2_threshold_4_root",
map[string]bool{"root.json": false, "timestamp.json": false, "snapshot.json": false, "targets.json": false}},
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_2_threshold_4_snapshot",
map[string]bool{"root.json": false, "timestamp.json": false, "snapshot.json": false, "targets.json": false}},
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_2_threshold_4_targets",
map[string]bool{"root.json": false, "timestamp.json": false, "snapshot.json": false, "targets.json": false}},
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_2_threshold_4_timestamp",
map[string]bool{"root.json": false, "timestamp.json": false, "snapshot.json": false, "targets.json": false}},
// Fast-forward recovery not needed if root keys are revoked, even when the threshold number of root keys are revoked.
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_4_threshold_4_root",
map[string]bool{"root.json": false, "timestamp.json": false, "snapshot.json": false, "targets.json": false}},
// Delete snapshot and timestamp metadata if a threshold number of snapshot keys are revoked.
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_4_threshold_4_snapshot",
map[string]bool{"root.json": false, "timestamp.json": true, "snapshot.json": true, "targets.json": false}},
// Delete targets and snapshot metadata if a threshold number of targets keys are revoked.
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_4_threshold_4_targets",
map[string]bool{"root.json": false, "timestamp.json": false, "snapshot.json": true, "targets.json": true}},
// Delete timestamp metadata if a threshold number of timestamp keys are revoked.
{"testdata/PublishedTwiceMultiKeysadd_9_revoke_4_threshold_4_timestamp",
map[string]bool{"root.json": false, "timestamp.json": true, "snapshot.json": false, "targets.json": false}},
}
for _, test := range tests {
tufClient, closer := initRootTest(c, test.fixturePath)
c.Assert(tufClient.UpdateRoots(), IsNil)
m, err := tufClient.local.GetMeta()
c.Assert(err, IsNil)
for md, deleted := range test.expectMetaDeleted {
if deleted {
if _, ok := m[md]; ok {
c.Fatalf("Metadata %s is not deleted!", md)
}
} else {
if _, ok := m[md]; !ok {
c.Fatalf("Metadata %s deleted!", md)
}
}
}
closer()
}
}
func (s *ClientSuite) TestUpdateRace(c *C) {
// Tests race condition for the client update. You need to run the test with -race flag:
// go test -race
for i := 0; i < 2; i++ {
go func() {
c := NewClient(MemoryLocalStore(), newFakeRemoteStore())
c.Update()
}()
}
}
func (s *ClientSuite) TestNewTargets(c *C) {
client := s.newClient(c)
files, err := client.Update()
c.Assert(err, IsNil)
assertFiles(c, files, []string{"foo.txt"})
s.addRemoteTarget(c, "bar.txt")
s.addRemoteTarget(c, "baz.txt")
files, err = client.Update()
c.Assert(err, IsNil)
assertFiles(c, files, []string{"bar.txt", "baz.txt"})
// Adding the same exact file should not lead to an update
s.addRemoteTarget(c, "bar.txt")
files, err = client.Update()
c.Assert(err, IsNil)
c.Assert(files, HasLen, 0)
}
func (s *ClientSuite) TestNewTimestampKey(c *C) {
client := s.newClient(c)
// replace key
oldIDs := s.keyIDs["timestamp"]
c.Assert(s.repo.RevokeKey("timestamp", oldIDs[0]), IsNil)
newIDs := s.genKey(c, "timestamp")
// generate new snapshot (because root has changed) and timestamp
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update gets new root and timestamp
c.Assert(client.getLocalMeta(), IsNil)
rootVer := client.rootVer
timestampVer := client.timestampVer
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.rootVer > rootVer, Equals, true)
c.Assert(client.timestampVer > timestampVer, Equals, true)
// check key has been replaced in db
for _, oldID := range oldIDs {
_, err := client.db.GetVerifier(oldID)
c.Assert(err, NotNil)
}
for _, newID := range newIDs {
verifier, err := client.db.GetVerifier(newID)
c.Assert(err, IsNil)
c.Assert(verifier.MarshalPublicKey().IDs(), DeepEquals, newIDs)
}
role := client.db.GetRole("timestamp")
c.Assert(role, NotNil)
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(newIDs))
}
func (s *ClientSuite) TestNewSnapshotKey(c *C) {
client := s.newClient(c)
// replace key
oldIDs := s.keyIDs["snapshot"]
c.Assert(s.repo.RevokeKey("snapshot", oldIDs[0]), IsNil)
newIDs := s.genKey(c, "snapshot")
// generate new snapshot and timestamp
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update gets new root, snapshot and timestamp
c.Assert(client.getLocalMeta(), IsNil)
rootVer := client.rootVer
snapshotVer := client.snapshotVer
timestampVer := client.timestampVer
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.rootVer > rootVer, Equals, true)
c.Assert(client.snapshotVer > snapshotVer, Equals, true)
c.Assert(client.timestampVer > timestampVer, Equals, true)
// check key has been replaced in db
for _, oldID := range oldIDs {
_, err := client.db.GetVerifier(oldID)
c.Assert(err, NotNil)
}
for _, newID := range newIDs {
verifier, err := client.db.GetVerifier(newID)
c.Assert(err, IsNil)
c.Assert(verifier.MarshalPublicKey().IDs(), DeepEquals, newIDs)
}
role := client.db.GetRole("snapshot")
c.Assert(role, NotNil)
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(newIDs))
}
func (s *ClientSuite) TestNewTargetsKey(c *C) {
client := s.newClient(c)
// replace key
oldIDs := s.keyIDs["targets"]
c.Assert(s.repo.RevokeKey("targets", oldIDs[0]), IsNil)
newIDs := s.genKey(c, "targets")
// re-sign targets and generate new snapshot and timestamp
c.Assert(s.repo.Sign("targets.json"), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update gets new metadata
c.Assert(client.getLocalMeta(), IsNil)
rootVer := client.rootVer
targetsVer := client.targetsVer
snapshotVer := client.snapshotVer
timestampVer := client.timestampVer
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.rootVer > rootVer, Equals, true)
c.Assert(client.targetsVer > targetsVer, Equals, true)
c.Assert(client.snapshotVer > snapshotVer, Equals, true)
c.Assert(client.timestampVer > timestampVer, Equals, true)
// check key has been replaced in db
for _, oldID := range oldIDs {
_, err := client.db.GetVerifier(oldID)
c.Assert(err, NotNil)
}
for _, newID := range newIDs {
verifier, err := client.db.GetVerifier(newID)
c.Assert(err, IsNil)
c.Assert(verifier.MarshalPublicKey().IDs(), DeepEquals, newIDs)
}
role := client.db.GetRole("targets")
c.Assert(role, NotNil)
c.Assert(role.KeyIDs, DeepEquals, sets.StringSliceToSet(newIDs))
}
func (s *ClientSuite) TestOfflineSignatureFlow(c *C) {
client := s.newClient(c)
// replace key
oldIDs := s.keyIDs["targets"]
c.Assert(s.repo.RevokeKey("targets", oldIDs[0]), IsNil)
_ = s.genKey(c, "targets")
// re-sign targets using offline flow and generate new snapshot and timestamp
payload, err := s.repo.Payload("targets.json")
c.Assert(err, IsNil)
signed := data.Signed{Signed: payload}
_, err = s.repo.SignPayload("targets", &signed)
c.Assert(err, IsNil)
for _, sig := range signed.Signatures {
// This method checks that the signature verifies!
err = s.repo.AddOrUpdateSignature("targets.json", sig)
c.Assert(err, IsNil)
}
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update gets new metadata
c.Assert(client.getLocalMeta(), IsNil)
_, err = client.Update()
c.Assert(err, IsNil)
}
func (s *ClientSuite) TestLocalExpired(c *C) {
client := s.newClient(c)
// locally expired timestamp.json is ok
version := client.timestampVer
c.Assert(s.repo.TimestampWithExpires(s.expiredTime), IsNil)
s.syncLocal(c)
s.withMetaExpired(func() {
c.Assert(client.getLocalMeta(), IsNil)
c.Assert(client.timestampVer > version, Equals, true)
})
// locally expired snapshot.json is ok
version = client.snapshotVer
c.Assert(s.repo.SnapshotWithExpires(s.expiredTime), IsNil)
s.syncLocal(c)
s.withMetaExpired(func() {
c.Assert(client.getLocalMeta(), IsNil)
c.Assert(client.snapshotVer > version, Equals, true)
})
// locally expired targets.json is ok
version = client.targetsVer
c.Assert(s.repo.AddTargetWithExpires("foo.txt", nil, s.expiredTime), IsNil)
s.syncLocal(c)
s.withMetaExpired(func() {
c.Assert(client.getLocalMeta(), IsNil)
c.Assert(client.targetsVer > version, Equals, true)
})
// locally expired root.json is not ok
version = client.rootVer
s.genKeyExpired(c, "targets")
s.syncLocal(c)
s.withMetaExpired(func() {
err := client.getLocalMeta()
if _, ok := err.(verify.ErrExpired); !ok {
c.Fatalf("expected err to have type signed.ErrExpired, got %T", err)
}
c.Assert(client.rootVer, Equals, version)
})
}
func (s *ClientSuite) TestTimestampTooLarge(c *C) {
s.setRemoteMeta("timestamp.json", make([]byte, defaultTimestampDownloadLimit+1))
_, err := s.newClient(c).Update()
c.Assert(err, Equals, ErrMetaTooLarge{"timestamp.json", defaultTimestampDownloadLimit + 1, defaultTimestampDownloadLimit})
}
func (s *ClientSuite) TestUpdateLocalRootExpired(c *C) {
client := s.newClient(c)
// add soon to expire root.json to local storage
s.genKeyExpired(c, "timestamp")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncLocal(c)
// add far expiring root.json to remote storage
s.genKey(c, "timestamp")
s.addRemoteTarget(c, "bar.txt")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
const expectedRootVersion = int64(3)
// check the update downloads the non expired remote root.json and
// restarts itself, thus successfully updating
s.withMetaExpired(func() {
err := client.getLocalMeta()
if _, ok := err.(verify.ErrExpired); !ok {
c.Fatalf("expected err to have type signed.ErrExpired, got %T", err)
}
_, err = client.Update()
c.Assert(err, IsNil)
c.Assert(client.rootVer, Equals, expectedRootVersion)
})
}
func (s *ClientSuite) TestUpdateRemoteExpired(c *C) {
client := s.updatedClient(c)
// expired remote metadata should always be rejected
c.Assert(s.repo.TimestampWithExpires(s.expiredTime), IsNil)
s.syncRemote(c)
s.withMetaExpired(func() {
_, err := client.Update()
s.assertErrExpired(c, err, "timestamp.json")
})
c.Assert(s.repo.SnapshotWithExpires(s.expiredTime), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
s.withMetaExpired(func() {
_, err := client.Update()
s.assertErrExpired(c, err, "snapshot.json")
})
c.Assert(s.repo.AddTargetWithExpires("bar.txt", nil, s.expiredTime), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
s.syncRemote(c)
s.withMetaExpired(func() {
_, err := client.Update()
s.assertErrExpired(c, err, "targets.json")
})
s.genKeyExpired(c, "timestamp")
c.Assert(s.repo.RemoveTarget("bar.txt"), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
s.withMetaExpired(func() {
_, err := client.Update()
s.assertErrExpired(c, err, "root.json")
})
}
func (s *ClientSuite) TestUpdateLocalRootExpiredKeyChange(c *C) {
client := s.newClient(c)
// add soon to expire root.json to local storage
s.genKeyExpired(c, "timestamp")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncLocal(c)
// replace all keys
newKeyIDs := make(map[string][]string)
for role, ids := range s.keyIDs {
if role != "snapshot" && role != "timestamp" && role != "targets" {
c.Assert(s.repo.RevokeKey(role, ids[0]), IsNil)
newKeyIDs[role] = s.genKey(c, role)
}
}
// update metadata
c.Assert(s.repo.Sign("targets.json"), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check the update downloads the non expired remote root.json and
// restarts itself, thus successfully updating
s.withMetaExpired(func() {
err := client.getLocalMeta()
c.Assert(err, FitsTypeOf, verify.ErrExpired{})
_, err = client.Update()
c.Assert(err, IsNil)
})
}
func (s *ClientSuite) TestUpdateMixAndMatchAttack(c *C) {
// generate metadata with an explicit expires so we can make predictable changes
expires := time.Now().Add(time.Hour)
c.Assert(s.repo.AddTargetWithExpires("foo.txt", nil, expires), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
client := s.updatedClient(c)
// grab the remote targets.json
oldTargets, err := s.readMeta("targets.json")
if err != nil {
c.Fatal("missing remote targets.json")
}
// generate new remote metadata, but replace targets.json with the old one
c.Assert(s.repo.AddTargetWithExpires("bar.txt", nil, expires), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
newTargets, err := s.readMeta("targets.json")
if err != nil {
c.Fatal("missing remote targets.json")
}
s.setRemoteMeta("targets.json", oldTargets)
// check update returns ErrWrongSize for targets.json
_, err = client.Update()
c.Assert(err, DeepEquals, ErrWrongSize{"targets.json", int64(len(oldTargets)), int64(len(newTargets))})
// do the same but keep the size the same
c.Assert(s.repo.RemoveTargetWithExpires("foo.txt", expires), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
s.syncRemote(c)
s.setRemoteMeta("targets.json", oldTargets)
// check update returns ErrWrongHash
_, err = client.Update()
assertWrongHash(c, err)
}
func (s *ClientSuite) TestUpdateReplayAttack(c *C) {
client := s.updatedClient(c)
// grab the remote timestamp.json
oldTimestamp, err := s.readMeta("timestamp.json")
if err != nil {
c.Fatal("missing remote timestamp.json")
}
// generate a new timestamp and sync with the client
version := client.timestampVer
c.Assert(version > 0, Equals, true)
c.Assert(s.repo.Timestamp(), IsNil)
s.syncRemote(c)
_, err = client.Update()
c.Assert(err, IsNil)
c.Assert(client.timestampVer > version, Equals, true)
// replace remote timestamp.json with the old one
s.setRemoteMeta("timestamp.json", oldTimestamp)
// check update returns ErrLowVersion
_, err = client.Update()
c.Assert(err, DeepEquals, ErrDecodeFailed{
File: "timestamp.json",
Err: verify.ErrLowVersion{
Actual: version,
Current: client.timestampVer,
},
})
}
func (s *ClientSuite) TestUpdateForkTimestamp(c *C) {
client := s.updatedClient(c)
// grab the remote timestamp.json
oldTimestamp, err := s.readMeta("timestamp.json")
if err != nil {
c.Fatal("missing remote timestamp.json")
}
// generate a new timestamp and sync with the client
version := client.timestampVer
c.Assert(version > 0, Equals, true)
c.Assert(s.repo.Timestamp(), IsNil)
s.syncRemote(c)
_, err = client.Update()
c.Assert(err, IsNil)
newVersion := client.timestampVer
c.Assert(newVersion > version, Equals, true)
// generate a new, different timestamp with the *same version*
s.setRemoteMeta("timestamp.json", oldTimestamp)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(client.timestampVer, Equals, newVersion) // double-check: same version?
s.syncRemote(c)
oldMeta, err := client.local.GetMeta()
c.Assert(err, IsNil)
_, err = client.Update()
c.Assert(err, IsNil) // no error: the targets.json version didn't change, so there was no update!
// Client shouldn't update!
newMeta, err := client.local.GetMeta()
c.Assert(err, IsNil)
c.Assert(oldMeta, DeepEquals, newMeta)
}
func (s *ClientSuite) TestUpdateTamperedTargets(c *C) {
client := s.newClient(c)
// get local targets.json
meta, err := s.store.GetMeta()
c.Assert(err, IsNil)
targetsJSON, ok := meta["targets.json"]
if !ok {
c.Fatal("missing targets.json")
}
type signedTargets struct {
Signed data.Targets `json:"signed"`
Signatures []data.Signature `json:"signatures"`
}
targets := &signedTargets{}
c.Assert(json.Unmarshal(targetsJSON, targets), IsNil)
// update remote targets.json to have different content but same size
targets.Signed.Type = "xxxxxxx"
tamperedJSON, err := json.Marshal(targets)
c.Assert(err, IsNil)
s.store.SetMeta("targets.json", tamperedJSON)
s.store.Commit(false, nil, nil)
s.syncRemote(c)
_, err = client.Update()
assertWrongHash(c, err)
// update remote targets.json to have the wrong size
targets.Signed.Type = "xxx"
tamperedJSON, err = json.Marshal(targets)
c.Assert(err, IsNil)
s.store.SetMeta("targets.json", tamperedJSON)
s.store.Commit(false, nil, nil)
c.Assert(s.repo.Timestamp(), IsNil) // unless timestamp changes, the client doesn't even look at "targets.json"
s.syncRemote(c)
_, err = client.Update()
c.Assert(err, DeepEquals, ErrWrongSize{"targets.json", int64(len(tamperedJSON)), int64(len(targetsJSON))})
}
func (s *ClientSuite) TestUpdateHTTP(c *C) {
tmp := c.MkDir()
// start file server
addr, cleanup := startFileServer(c, tmp)
defer cleanup()
for _, consistentSnapshot := range []bool{false, true} {
dir := fmt.Sprintf("consistent-snapshot-%t", consistentSnapshot)
// generate repository
repo := generateRepoFS(c, filepath.Join(tmp, dir), targetFiles, consistentSnapshot)
// initialize a client
remote, err := HTTPRemoteStore(fmt.Sprintf("http://%s/%s/repository", addr, dir), nil, nil)
c.Assert(err, IsNil)
client := NewClient(MemoryLocalStore(), remote)
rootMeta, err := repo.SignedMeta("root.json")
c.Assert(err, IsNil)
rootJsonBytes, err := json.Marshal(rootMeta)
c.Assert(err, IsNil)
c.Assert(client.Init(rootJsonBytes), IsNil)
// check update is ok
targets, err := client.Update()
c.Assert(err, IsNil)
assertFiles(c, targets, []string{"foo.txt", "bar.txt", "baz.txt"})
// check can download files
for name, data := range targetFiles {
var dest testDestination
c.Assert(client.Download(name, &dest), IsNil)
c.Assert(dest.deleted, Equals, false)
c.Assert(dest.String(), Equals, string(data))
}
}
}
// TestRollbackSnapshot tests a rollback version of snapshot.
func (s *ClientSuite) TestRollbackSnapshot(c *C) {
client := s.updatedClient(c)
// generate a new snapshot & timestamp v2 and sync with the client
version := client.snapshotVer
c.Assert(version > 0, Equals, true)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.snapshotVer > version, Equals, true)
// replace remote snapshot.json with old version and timestamp again.
s.repo.SetSnapshotVersion(version)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update returns ErrLowVersion
_, err = client.Update()
c.Assert(err, DeepEquals, verify.ErrLowVersion{
Actual: version,
Current: client.snapshotVer,
})
}
func (s *ClientSuite) TestRollbackTopLevelTargets(c *C) {
client := s.updatedClient(c)
// generate a new targets and sync with the client
version := client.targetsVer
c.Assert(version > 0, Equals, true)
s.addRemoteTarget(c, "bar.txt")
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.targetsVer > version, Equals, true)
// replace remote snapshot.json with old version and timestamp again.
s.repo.SetTargetsVersion(version)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// check update returns ErrLowVersion
_, err = client.Update()
c.Assert(err, DeepEquals, verify.ErrLowVersion{
Actual: version,
Current: client.targetsVer,
})
}
func (s *ClientSuite) TestRollbackDelegatedTargets(c *C) {
client := s.updatedClient(c)
// add a delegation
signer, err := keys.GenerateEd25519Key()
c.Assert(err, IsNil)
role := data.DelegatedRole{
Name: "role",
KeyIDs: signer.PublicData().IDs(),
Paths: []string{"bar.txt", "baz.txt"},
Threshold: 1,
}
s.store.SaveSigner("role", signer)
s.repo.AddDelegatedRole("targets", role, []*data.PublicKey{signer.PublicData()})
s.repo.AddTargetToPreferredRole("bar.txt", nil, "role")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// save v1 delegation
meta, err := s.store.GetMeta()
c.Assert(err, IsNil)
oldRole, ok := meta["role.json"]
if !ok {
c.Fatal("missing role.json")
}
// update client and verify download delegated target
_, err = client.Update()
c.Assert(err, IsNil)
var dest testDestination
c.Assert(client.Download("bar.txt", &dest), IsNil)
// update delegation to v2
s.repo.AddTargetToPreferredRole("baz.txt", nil, "role")
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
// update client and verify download v2 delegated target
_, err = client.Update()
c.Assert(err, IsNil)
c.Assert(dest.Delete(), IsNil)
c.Assert(client.Download("baz.txt", &dest), IsNil)
// rollback role.json version.
c.Assert(s.store.SetMeta("role.json", oldRole), IsNil)
repo, err := tuf.NewRepo(s.store)
c.Assert(err, IsNil)
c.Assert(repo.Snapshot(), IsNil)
c.Assert(repo.Timestamp(), IsNil)
c.Assert(repo.Commit(), IsNil)
s.syncRemote(c)
// check update returns ErrLowVersion
_, err = client.Update()
c.Assert(err, DeepEquals, verify.ErrLowVersion{
Actual: 1,
Current: 2,
})
}
type testDestination struct {
bytes.Buffer
deleted bool
}
func (t *testDestination) Delete() error {
t.deleted = true
return nil
}
func (s *ClientSuite) TestDownloadUnknownTarget(c *C) {
client := s.updatedClient(c)
var dest testDestination
c.Assert(client.Download("nonexistent", &dest), Equals, ErrUnknownTarget{Name: "nonexistent", SnapshotVersion: 1})
c.Assert(dest.deleted, Equals, true)
}
func (s *ClientSuite) TestDownloadNoExist(c *C) {
client := s.updatedClient(c)
s.deleteTarget("foo.txt")
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), Equals, ErrNotFound{"foo.txt"})
c.Assert(dest.deleted, Equals, true)
}
func (s *ClientSuite) TestDownloadOK(c *C) {
client := s.updatedClient(c)
// the filename is normalized if necessary
for _, name := range []string{"/foo.txt", "foo.txt"} {
var dest testDestination
c.Assert(client.Download(name, &dest), IsNil)
c.Assert(dest.deleted, Equals, false)
c.Assert(dest.String(), Equals, "foo")
}
}
func (s *ClientSuite) TestDownloadWrongSize(c *C) {
client := s.updatedClient(c)
// Update with a file that's incorrect size.
s.setRemoteTarget("foo.txt", []byte("wrong-size"))
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), DeepEquals, ErrWrongSize{"foo.txt", 10, 3})
c.Assert(dest.deleted, Equals, true)
}
func (s *ClientSuite) TestDownloadTargetTooLong(c *C) {
client := s.updatedClient(c)
s.setRemoteTarget("foo.txt", []byte("foo-ooo"))
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), DeepEquals, ErrWrongSize{"foo.txt", 7, 3})
c.Assert(dest.deleted, Equals, true)
}
func (s *ClientSuite) TestDownloadTargetTooShort(c *C) {
client := s.updatedClient(c)
s.setRemoteTarget("foo.txt", []byte("fo"))
var dest testDestination
c.Assert(client.Download("foo.txt", &dest), DeepEquals, ErrWrongSize{"foo.txt", 2, 3})
c.Assert(dest.deleted, Equals, true)
}
func (s *ClientSuite) TestDownloadTargetCorruptData(c *C) {
client := s.updatedClient(c)
s.setRemoteTarget("foo.txt", []byte("ooo"))
var dest testDestination
assertWrongHash(c, client.Download("foo.txt", &dest))
c.Assert(dest.deleted, Equals, true)
}
func (s *ClientSuite) TestAvailableTargets(c *C) {
client := s.updatedClient(c)
files, err := client.Targets()
c.Assert(err, IsNil)
assertFiles(c, files, []string{"foo.txt"})
s.addRemoteTarget(c, "bar.txt")
s.addRemoteTarget(c, "baz.txt")
_, err = client.Update()
c.Assert(err, IsNil)
files, err = client.Targets()
c.Assert(err, IsNil)
assertFiles(c, files, []string{"foo.txt", "bar.txt", "baz.txt"})
}
func (s *ClientSuite) TestAvailableTarget(c *C) {
client := s.updatedClient(c)
target, err := client.Target("foo.txt")
c.Assert(err, IsNil)
assertFile(c, target, "foo.txt")
target, err = client.Target("/foo.txt")
c.Assert(err, IsNil)
assertFile(c, target, "foo.txt")
_, err = client.Target("bar.txt")
c.Assert(err, Equals, ErrNotFound{"bar.txt"})
_, err = client.Target("/bar.txt")
c.Assert(err, Equals, ErrNotFound{"/bar.txt"})
}
func (s *ClientSuite) TestUnknownKeyIDs(c *C) {
// get local root.json
meta, err := s.store.GetMeta()
c.Assert(err, IsNil)
rootJSON, ok := meta["root.json"]
c.Assert(ok, Equals, true)
var root struct {
Signed data.Root `json:"signed"`
Signatures []data.Signature `json:"signatures"`
}
c.Assert(json.Unmarshal(rootJSON, &root), IsNil)
// update remote root.json to add a new key with an unknown id
signer, err := keys.GenerateEd25519Key()
c.Assert(err, IsNil)
root.Signed.Keys["unknown-key-id"] = signer.PublicData()
// re-sign the root metadata, then commit it back into the store.
signingKeys, err := s.store.GetSigners("root")
c.Assert(err, IsNil)
signedRoot, err := sign.Marshal(root.Signed, signingKeys...)
c.Assert(err, IsNil)
rootJSON, err = cjson.EncodeCanonical(signedRoot)
c.Assert(err, IsNil)
s.store.SetMeta("root.json", rootJSON)
s.store.Commit(false, nil, nil)
s.syncRemote(c)
// FIXME(TUF-0.9) We need this for now because the client still uses
// the TUF-0.9 update workflow, where we decide to update the root
// metadata when we observe a new root through the snapshot.
repo, err := tuf.NewRepo(s.store)
c.Assert(err, IsNil)
c.Assert(repo.Snapshot(), IsNil)
c.Assert(repo.Timestamp(), IsNil)
c.Assert(repo.Commit(), IsNil)
s.syncRemote(c)
// Make sure the client can update with the unknown keyid.
client := s.newClient(c)
_, err = client.Update()
c.Assert(err, IsNil)
}
func generateRepoFS(c *C, dir string, files map[string][]byte, consistentSnapshot bool) *tuf.Repo {
repo, err := tuf.NewRepo(tuf.FileSystemStore(dir, nil))
c.Assert(err, IsNil)
if !consistentSnapshot {
c.Assert(repo.Init(false), IsNil)
}
for _, role := range []string{"root", "snapshot", "targets", "timestamp"} {
_, err := repo.GenKey(role)
c.Assert(err, IsNil)
}
for file, data := range files {
path := filepath.Join(dir, "staged", "targets", file)
c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
c.Assert(os.WriteFile(path, data, 0644), IsNil)
c.Assert(repo.AddTarget(file, nil), IsNil)
}
c.Assert(repo.Snapshot(), IsNil)
c.Assert(repo.Timestamp(), IsNil)
c.Assert(repo.Commit(), IsNil)
return repo
}
func (s *ClientSuite) TestVerifyDigest(c *C) {
digest := "sha256:bc11b176a293bb341a0f2d0d226f52e7fcebd186a7c4dfca5fc64f305f06b94c"
hash := "bc11b176a293bb341a0f2d0d226f52e7fcebd186a7c4dfca5fc64f305f06b94c"
size := int64(42)
c.Assert(s.repo.AddTargetsWithDigest(hash, "sha256", size, digest, nil), IsNil)
c.Assert(s.repo.Snapshot(), IsNil)
c.Assert(s.repo.Timestamp(), IsNil)
c.Assert(s.repo.Commit(), IsNil)
s.syncRemote(c)
client := s.newClient(c)
_, err := client.Update()
c.Assert(err, IsNil)
c.Assert(client.VerifyDigest(hash, "sha256", size, digest), IsNil)
}
type StateLessSuite struct{}
var _ = Suite(&StateLessSuite{})
func (s *StateLessSuite) TestRejectsMultiSignaturesSameKeyDifferentIDs(c *C) {
// In this test Alice and Bob want to create a TUF repo
// where a root key rotation would require both their signatures.
// Alice uses an old version of Go-TUF where each key gets assigned several IDs.
// Bob uses a modern version of Go-TUF that does not produce the same list of IDs for a same key.
// This test checks that the TUF client
// will not accept a root rotation
// signed twice with Alice's key with different key IDs each time.
// This test was failing with https://github.com/theupdateframework/go-tuf/tree/ac7b5d7bce18cca5a84a28b021bd6372f450b35b
// because the signature verification code was assuming that the key IDs used in the metadata
// were the same as the one the TUF library of the client would generate,
// breaking the security of threshold signatures.
// The attack works just the same if Alice is malicious from the beginning
// and convinces Bob to sign an initial "root.json"
// with additional key IDs for her only key,
// but this scenario show that the vulnerability can even impact situations
// where Alice is not malicious at all,
// she was simply using an old client and an attacker stole her key.
// The purpose of threshold signatures in TUF is precisely
// to make sure that an attacker cannot forge signatures
// if they did not steal a large enough number of keys.
alice, err := keys.GenerateEd25519Key()
if err != nil {
panic(err)
}
root := data.NewRoot()
root.Version = 1
root.Roles["root"] = &data.Role{
KeyIDs: []string{},
Threshold: 2, // Note the threshold
}
// reproduces how IDs were computed in
// https://github.com/theupdateframework/go-tuf/blob/8e84384bebe3/data/types.go#L50
oldTUFIDs := func(k *data.PublicKey) []string {
bytes, _ := cjson.EncodeCanonical(k)
digest := sha256.Sum256(bytes)
ids := []string{hex.EncodeToString(digest[:])}
if k.Scheme != "" || len(k.Algorithms) != 0 {
bytes, _ = cjson.EncodeCanonical(&data.PublicKey{
Type: k.Type,
Value: k.Value,
})
digest = sha256.Sum256(bytes)
ids = append(ids, hex.EncodeToString(digest[:]))
}
return ids
}
// Alice adds her key using an old version of go-tuf
// which will use several IDs
for _, keyID := range oldTUFIDs(alice.PublicData()) {
root.Keys[keyID] = alice.PublicData()
root.Roles["root"].KeyIDs = append(root.Roles["root"].KeyIDs, keyID)
}
bob, err := keys.GenerateEd25519Key()
if err != nil {
panic(err)
}
root.AddKey(bob.PublicData())
root.Roles["root"].KeyIDs = append(
root.Roles["root"].KeyIDs,
bob.PublicData().IDs()...,
)
// signer for the other roles, not important
delegatedSigner, _ := keys.GenerateEd25519Key()
root.AddKey(delegatedSigner.PublicData())
for _, role := range []string{"targets", "snapshot", "timestamp"} {
root.Roles[role] = &data.Role{
KeyIDs: delegatedSigner.PublicData().IDs(),
Threshold: 1,
}
}
signedRoot, err := sign.Marshal(root, alice, bob)
c.Assert(err, IsNil)
rootJSON, err := json.Marshal(signedRoot)
c.Assert(err, IsNil)
// producing evil root using only Alice's key
evilRoot := root
evilRoot.Version = 2
canonical, err := cjson.EncodeCanonical(evilRoot)
c.Assert(err, IsNil)
sig, err := alice.SignMessage(canonical)
c.Assert(err, IsNil)
signedEvilRoot := &data.Signed{
Signed: canonical,
Signatures: make([]data.Signature, 0),
}
for _, keyID := range oldTUFIDs(alice.PublicData()) {
signedEvilRoot.Signatures = append(signedEvilRoot.Signatures, data.Signature{
Signature: sig,
KeyID: keyID,
})
}
evilRootJSON, err := json.Marshal(signedEvilRoot)
c.Assert(err, IsNil)
// checking that client does not accept root rotation
// to evil root
localStore := MemoryLocalStore()
err = localStore.SetMeta("root.json", rootJSON)
c.Assert(err, IsNil)
remoteStore := newFakeRemoteStore()
remoteStore.meta["2.root.json"] = newFakeFile(evilRootJSON)
client := NewClient(localStore, remoteStore)
err = client.UpdateRoots()
if err != nil {
c.Assert(err, DeepEquals, verify.ErrRoleThreshold{Expected: 2, Actual: 1})
} else {
c.Fatalf("client returned no error when updating with evil root")
}
}
|