diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 8b4fde6239..e04f8b5b8d 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -16,7 +16,7 @@ }, { "ImportPath": "github.com/endophage/go-tuf", - "Rev": "913d6f239a809f317bf3642019bc480d18e80cfc" + "Rev": "69d16ac22ea8a8310775803a498f7f4d83208d1c" }, { "ImportPath": "github.com/gorilla/context", diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/client/client_test.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/client/client_test.go index a0083acde6..83669fd01d 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/client/client_test.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/client/client_test.go @@ -11,11 +11,11 @@ import ( "time" "github.com/flynn/go-tuf" + . "gopkg.in/check.v1" "github.com/flynn/go-tuf/data" "github.com/flynn/go-tuf/keys" "github.com/flynn/go-tuf/signed" "github.com/flynn/go-tuf/util" - . "gopkg.in/check.v1" ) // Hook up gocheck into the "go test" runner. diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/client/interop_test.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/client/interop_test.go index 9f28f67f9c..8dcc2e65ad 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/client/interop_test.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/client/interop_test.go @@ -12,11 +12,11 @@ import ( "path/filepath" "strings" - "github.com/agl/ed25519" "github.com/flynn/go-tuf" + "github.com/agl/ed25519" + . "gopkg.in/check.v1" "github.com/flynn/go-tuf/data" "github.com/flynn/go-tuf/util" - . "gopkg.in/check.v1" ) type InteropSuite struct{} diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/client/testdata/generate/generate.py b/Godeps/_workspace/src/github.com/endophage/go-tuf/client/testdata/generate/generate.py old mode 100755 new mode 100644 diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/client/testdata/generate/generate.sh b/Godeps/_workspace/src/github.com/endophage/go-tuf/client/testdata/generate/generate.sh old mode 100755 new mode 100644 diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/main.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/main.go index 1d9d8e1b88..22587430e7 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/main.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/main.go @@ -5,9 +5,9 @@ import ( "log" "github.com/endophage/go-tuf" + "github.com/flynn/go-docopt" "github.com/endophage/go-tuf/store" "github.com/endophage/go-tuf/util" - "github.com/flynn/go-docopt" ) func main() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/meta.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/meta.go index 139cc7d55b..d1a8fc049d 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/meta.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tools/meta.go @@ -7,8 +7,8 @@ import ( "os" "github.com/endophage/go-tuf" - "github.com/endophage/go-tuf/util" "github.com/flynn/go-docopt" + "github.com/endophage/go-tuf/util" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/add.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/add.go index f862b2d759..e131fce70c 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/add.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/add.go @@ -3,8 +3,8 @@ package main import ( // "encoding/json" - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/clean.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/clean.go index 40814a059f..a120f29abe 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/clean.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/clean.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/commit.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/commit.go index 764d432cbb..ddfdf34e74 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/commit.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/commit.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/gen_key.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/gen_key.go index 6df24a71b9..f89332a994 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/gen_key.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/gen_key.go @@ -3,8 +3,8 @@ package main import ( "fmt" - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/init.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/init.go index af328acca9..aca7c9167e 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/init.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/init.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/main.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/main.go index 2404c31eb7..86f21150df 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/main.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/main.go @@ -11,11 +11,11 @@ import ( "strings" "time" - "github.com/docker/docker/pkg/term" "github.com/endophage/go-tuf/signed" "github.com/endophage/go-tuf/store" - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/docker/docker/pkg/term" + "github.com/flynn/go-docopt" "github.com/flynn/go-tuf/util" ) diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/regenerate.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/regenerate.go index 70d5ec1dad..c45e3a17eb 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/regenerate.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/regenerate.go @@ -3,8 +3,8 @@ package main import ( "log" - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/remove.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/remove.go index 2871cfd16f..8232b662b1 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/remove.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/remove.go @@ -3,8 +3,8 @@ package main import ( "errors" - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/revoke_key.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/revoke_key.go index e243a702db..62dcde98bc 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/revoke_key.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/revoke_key.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/root_keys.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/root_keys.go index ec6a59ff82..b49a53ee85 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/root_keys.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/root_keys.go @@ -4,8 +4,8 @@ import ( "encoding/json" "os" - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/sign.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/sign.go index bcd9d0c4b8..09860558b3 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/sign.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/sign.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/snapshot.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/snapshot.go index ba88c3172e..6e9e98f2fe 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/snapshot.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/snapshot.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/timestamp.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/timestamp.go index 581a6b1998..7c184ed6ee 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/timestamp.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/cmd/tuf/timestamp.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flynn/go-docopt" "github.com/flynn/go-tuf" + "github.com/flynn/go-docopt" ) func init() { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/repo.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/repo.go index 0210a101c9..6b04348e63 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/repo.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/repo.go @@ -9,6 +9,8 @@ import ( "strings" "time" + cjson "github.com/tent/canonical-json-go" + "github.com/endophage/go-tuf/data" "github.com/endophage/go-tuf/errors" "github.com/endophage/go-tuf/keys" @@ -32,20 +34,27 @@ var topLevelManifests = []string{ "timestamp.json", } +// snapshotManifests is the list of default filenames that should be included in the +// snapshots.json. If using delegated targets, additional, dynamic files should also +// be included in snapshots. var snapshotManifests = []string{ "root.json", "targets.json", } +// Repo represents an instance of a TUF repo type Repo struct { - trust signed.Signer + trust *signed.Signer local store.LocalStore hashAlgorithms []string meta map[string]json.RawMessage } -func NewRepo(trust *signed.Signer, local store.LocalStore, hashAlgorithms ...string) (*Repo, error) { - r := &Repo{trust: *trust, local: local, hashAlgorithms: hashAlgorithms} +// NewRepo is a factory function for instantiating new TUF repos objects. +// If the local store is already populated, local.GetMeta() will initialise +// the Repo with the appropriate state. +func NewRepo(trust signed.TrustService, local store.LocalStore, hashAlgorithms ...string) (*Repo, error) { + r := &Repo{trust: signed.NewSigner(trust), local: local, hashAlgorithms: hashAlgorithms} var err error r.meta, err = local.GetMeta() @@ -55,6 +64,8 @@ func NewRepo(trust *signed.Signer, local store.LocalStore, hashAlgorithms ...str return r, nil } +// Init attempts to initialize a brand new TUF repo. It will fail if +// an existing targets file is detected. func (r *Repo) Init(consistentSnapshot bool) error { t, err := r.targets() if err != nil { @@ -112,7 +123,7 @@ func (r *Repo) snapshot() (*data.Snapshot, error) { if err := json.Unmarshal(snapshotJSON, s); err != nil { return nil, err } - snapshot := &data.Snapshot{} + snapshot := data.NewSnapshot() if err := json.Unmarshal(s.Signed, snapshot); err != nil { return nil, err } @@ -128,7 +139,7 @@ func (r *Repo) targets() (*data.Targets, error) { if err := json.Unmarshal(targetsJSON, s); err != nil { return nil, err } - targets := &data.Targets{} + targets := data.NewTargets() if err := json.Unmarshal(s.Signed, targets); err != nil { return nil, err } @@ -144,7 +155,7 @@ func (r *Repo) timestamp() (*data.Timestamp, error) { if err := json.Unmarshal(timestampJSON, s); err != nil { return nil, err } - timestamp := &data.Timestamp{} + timestamp := data.NewTimestamp() if err := json.Unmarshal(s.Signed, timestamp); err != nil { return nil, err } @@ -169,7 +180,7 @@ func (r *Repo) GenKeyWithExpires(keyRole string, expires time.Time) (string, err return "", err } - key, err := r.trust.NewKey() + key, err := r.trust.Create() if err != nil { return "", err } @@ -267,11 +278,16 @@ func (r *Repo) setMeta(name string, meta interface{}) error { if err != nil { return err } - s, err := r.trust.Marshal(meta, keys...) + b, err := cjson.Marshal(meta) if err != nil { return err } - b, err := json.Marshal(s) + s := &data.Signed{Signed: b} + err = r.trust.Sign(s, keys...) + if err != nil { + return err + } + b, err = json.Marshal(s) if err != nil { return err } diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/repo_test.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/repo_test.go index 6557b3cff5..0a80efff7c 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/repo_test.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/repo_test.go @@ -11,9 +11,9 @@ import ( "time" "github.com/agl/ed25519" + . "gopkg.in/check.v1" "github.com/endophage/go-tuf/data" "github.com/endophage/go-tuf/store" - . "gopkg.in/check.v1" // "github.com/endophage/go-tuf/encrypted" tuferr "github.com/endophage/go-tuf/errors" "github.com/endophage/go-tuf/signed" @@ -29,7 +29,6 @@ var _ = Suite(&RepoSuite{}) func (RepoSuite) TestNewRepo(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) meta := map[string]json.RawMessage{ "root.json": []byte(`{ @@ -78,7 +77,7 @@ func (RepoSuite) TestNewRepo(c *C) { local.SetMeta(k, v) } - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) root, err := r.root() @@ -112,7 +111,6 @@ func (RepoSuite) TestNewRepo(c *C) { func (RepoSuite) TestInit(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) db := util.GetSqliteDB() defer util.FlushDB(db) @@ -123,7 +121,7 @@ func (RepoSuite) TestInit(c *C) { ) local.AddBlob("/foo.txt", util.SampleMeta()) - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) // Init() sets root.ConsistentSnapshot @@ -147,12 +145,11 @@ func genKey(c *C, r *Repo, role string) string { func (RepoSuite) TestGenKey(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) sqldb := util.GetSqliteDB() defer util.FlushDB(sqldb) local := store.DBStore(sqldb, "") - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) // generate a key for an unknown role @@ -279,12 +276,11 @@ func (RepoSuite) TestGenKey(c *C) { func (RepoSuite) TestRevokeKey(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) db := util.GetSqliteDB() defer util.FlushDB(db) local := store.DBStore(db, "") - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) // revoking a key for an unknown role returns ErrInvalidRole @@ -332,14 +328,13 @@ func (RepoSuite) TestRevokeKey(c *C) { func (RepoSuite) TestSign(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) baseMeta := map[string]json.RawMessage{"root.json": []byte(`{"signed":{},"signatures":[]}`)} db := util.GetSqliteDB() defer util.FlushDB(db) local := store.DBStore(db, "") local.SetMeta("root.json", baseMeta["root.json"]) - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) // signing with no keys returns ErrInsufficientKeys @@ -363,7 +358,7 @@ func (RepoSuite) TestSign(c *C) { } // signing with an available key generates a signature - //key, err := signer.NewKey() + //key, err := signer.Create() kID, err := r.GenKey("root") c.Assert(err, IsNil) //c.Assert(local.SaveKey("root", key.SerializePrivate()), IsNil) @@ -375,7 +370,7 @@ func (RepoSuite) TestSign(c *C) { checkSigIDs(kID) // signing with a new available key generates another signature - //newKey, err := signer.NewKey() + //newKey, err := signer.Create() newkID, err := r.GenKey("root") c.Assert(err, IsNil) //c.Assert(local.SaveKey("root", newKey.SerializePrivate()), IsNil) @@ -385,13 +380,12 @@ func (RepoSuite) TestSign(c *C) { func (RepoSuite) TestCommit(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) //files := map[string][]byte{"/foo.txt": []byte("foo"), "/bar.txt": []byte("bar")} db := util.GetSqliteDB() defer util.FlushDB(db) local := store.DBStore(db, "") - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) // commit without root.json @@ -532,10 +526,9 @@ func (t *tmpDir) readFile(path string) []byte { func (RepoSuite) TestCommitFileSystem(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) tmp := newTmpDir(c) local := store.FileSystemStore(tmp.path, nil) - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) // don't use consistent snapshots to make the checks simpler @@ -612,10 +605,9 @@ func (RepoSuite) TestCommitFileSystem(c *C) { func (RepoSuite) TestConsistentSnapshot(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) tmp := newTmpDir(c) local := store.FileSystemStore(tmp.path, nil) - r, err := NewRepo(signer, local, "sha512", "sha256") + r, err := NewRepo(trust, local, "sha512", "sha256") c.Assert(err, IsNil) genKey(c, r, "root") @@ -659,7 +651,7 @@ func (RepoSuite) TestConsistentSnapshot(c *C) { tmp.assertNotExist("repository/targets/foo.txt") // targets should be returned by new repo - newRepo, err := NewRepo(signer, local, "sha512", "sha256") + newRepo, err := NewRepo(trust, local, "sha512", "sha256") c.Assert(err, IsNil) t, err := newRepo.targets() c.Assert(err, IsNil) @@ -671,13 +663,12 @@ func (RepoSuite) TestConsistentSnapshot(c *C) { func (RepoSuite) TestExpiresAndVersion(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) //files := map[string][]byte{"/foo.txt": []byte("foo")} db := util.GetSqliteDB() defer util.FlushDB(db) local := store.DBStore(db, "") - r, err := NewRepo(signer, local, "sha256") + r, err := NewRepo(trust, local, "sha256") c.Assert(err, IsNil) past := time.Now().Add(-1 * time.Second) @@ -762,7 +753,6 @@ func (RepoSuite) TestExpiresAndVersion(c *C) { func (RepoSuite) TestHashAlgorithm(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) //files := map[string][]byte{"/foo.txt": []byte("foo")} db := util.GetSqliteDB() @@ -778,7 +768,7 @@ func (RepoSuite) TestHashAlgorithm(c *C) { {args: []string{"sha512", "sha256"}}, } { // generate metadata with specific hash functions - r, err := NewRepo(signer, local, test.args...) + r, err := NewRepo(trust, local, test.args...) c.Assert(err, IsNil) genKey(c, r, "root") genKey(c, r, "targets") @@ -882,10 +872,9 @@ func testPassphraseFunc(p []byte) util.PassphraseFunc { func (RepoSuite) TestManageMultipleTargets(c *C) { trust := signed.NewEd25519() - signer := signed.NewSigner(trust) tmp := newTmpDir(c) local := store.FileSystemStore(tmp.path, nil) - r, err := NewRepo(signer, local) + r, err := NewRepo(trust, local) c.Assert(err, IsNil) // don't use consistent snapshots to make the checks simpler c.Assert(r.Init(false), IsNil) diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign.go index 2b27f2ebc4..cf379f4ad3 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign.go @@ -1,33 +1,10 @@ package signed import ( - cjson "github.com/tent/canonical-json-go" - "github.com/endophage/go-tuf/data" "github.com/endophage/go-tuf/keys" ) -// Sign takes a data.Signed and a key, calculated and adds the signature -// to the data.Signed -//func Sign(s *data.Signed, k *data.Key) { -// id := k.ID() -// signatures := make([]data.Signature, 0, len(s.Signatures)+1) -// for _, sig := range s.Signatures { -// if sig.KeyID == id { -// continue -// } -// signatures = append(signatures, sig) -// } -// priv := [ed25519.PrivateKeySize]byte{} -// copy(priv[:], k.Value.Private) -// sig := ed25519.Sign(&priv, s.Signed) -// s.Signatures = append(signatures, data.Signature{ -// KeyID: id, -// Method: "ed25519", -// Signature: sig[:], -// }) -//} - // Signer encapsulates a signing service with some convenience methods to // interface between TUF keys and the generic service interface type Signer struct { @@ -63,16 +40,20 @@ func (signer *Signer) Sign(s *data.Signed, keys ...*keys.PublicKey) error { return nil } -func (signer *Signer) Marshal(v interface{}, keys ...*keys.PublicKey) (*data.Signed, error) { - b, err := cjson.Marshal(v) - if err != nil { - return nil, err - } - s := &data.Signed{Signed: b} - err = signer.Sign(s, keys...) - return s, err // err may be nil but there's no point in checking, just return it -} +//func (signer *Signer) Marshal(v interface{}, keys ...*keys.PublicKey) (*data.Signed, error) { +// b, err := cjson.Marshal(v) +// if err != nil { +// return nil, err +// } +// s := &data.Signed{Signed: b} +// err = signer.Sign(s, keys...) +// return s, err // err may be nil but there's no point in checking, just return it +//} -func (signer *Signer) NewKey() (*keys.PublicKey, error) { +func (signer *Signer) Create() (*keys.PublicKey, error) { return signer.service.Create() } + +func (signer *Signer) PublicKeys(keyIDs ...string) (map[string]*keys.PublicKey, error) { + return signer.service.PublicKeys(keyIDs...) +} diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign_test.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign_test.go index dae242c6b8..2262d7b380 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign_test.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/sign_test.go @@ -8,21 +8,23 @@ import ( ) type MockTrustService struct { - testSig data.Signature testKey keys.PublicKey } -func (mts *MockTrustService) Sign(keyIDs []string, data []byte) ([]data.Signature, error) { - sigs := []data.Signature{mts.testSig} +func (mts *MockTrustService) Sign(keyIDs []string, _ []byte) ([]data.Signature, error) { + sigs := make([]data.Signature, 0, len(keyIDs)) + for _, keyID := range keyIDs { + sigs = append(sigs, data.Signature{KeyID: keyID}) + } return sigs, nil } -func (mts *MockTrustService) Create(keyType string) (keys.PublicKey, error) { - return keys.PublicKey{mts.testKey}, nil +func (mts *MockTrustService) Create() (*keys.PublicKey, error) { + return &mts.testKey, nil } -func (mts *MockTrustService) PublicKeys(keyIDs ...string) (map[string]keys.PublicKey, error) { - keys := map[string]keys.PublicKey{"testID": mts.testKey} +func (mts *MockTrustService) PublicKeys(keyIDs ...string) (map[string]*keys.PublicKey, error) { + keys := map[string]*keys.PublicKey{"testID": &mts.testKey} return keys, nil } @@ -31,13 +33,15 @@ var _ TrustService = &MockTrustService{} // Test signing and ensure the expected signature is added func TestBasicSign(t *testing.T) { signer := Signer{&MockTrustService{ - testSig: data.Signature{KeyID: "testID"}, - testKey: keys.PublicKey{}, + testKey: keys.PublicKey{ID: "testID"}, }} - key := keys.PublicKey{} + key, err := signer.Create() + if err != nil { + t.Fatal(err) + } testData := data.Signed{} - signer.Sign(&testData, &key) + signer.Sign(&testData, key) if len(testData.Signatures) != 1 { t.Fatalf("Incorrect number of signatures: %d", len(testData.Signatures)) @@ -54,7 +58,6 @@ func TestBasicSign(t *testing.T) { // should be cleaning previous signatures by the KeyID when asked to sign again) func TestReSign(t *testing.T) { signer := Signer{&MockTrustService{ - testSig: data.Signature{KeyID: "testID"}, testKey: keys.PublicKey{}, }} key := keys.PublicKey{ID: "testID"} @@ -74,10 +77,7 @@ func TestReSign(t *testing.T) { } func TestMultiSign(t *testing.T) { - signer := Signer{&MockTrustService{ - testSig: data.Signature{KeyID: "testID"}, - testKey: keys.PublicKey{}, - }} + signer := Signer{&MockTrustService{}} key := keys.PublicKey{ID: "testID1"} testData := data.Signed{} @@ -99,14 +99,16 @@ func TestMultiSign(t *testing.T) { } -func TestNewKey(t *testing.T) { +func TestCreate(t *testing.T) { signer := Signer{&MockTrustService{ - testSig: data.Signature{}, testKey: keys.PublicKey{ID: "testID"}, }} - key := signer.NewKey("testType") + key, err := signer.Create() + if err != nil { + t.Fatal(err) + } if key.ID != "testID" { t.Fatalf("Expected key ID not found: %s", key.ID) } diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify.go index 2dad6d1d7d..670c61021a 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify.go @@ -7,9 +7,9 @@ import ( "time" "github.com/agl/ed25519" + "github.com/tent/canonical-json-go" "github.com/endophage/go-tuf/data" "github.com/endophage/go-tuf/keys" - "github.com/tent/canonical-json-go" ) var ( diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify_test.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify_test.go index 468a80680a..d9a9033470 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify_test.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/signed/verify_test.go @@ -5,10 +5,11 @@ import ( "time" "github.com/agl/ed25519" + cjson "github.com/tent/canonical-json-go" + . "gopkg.in/check.v1" + "github.com/endophage/go-tuf/data" "github.com/endophage/go-tuf/keys" - - . "gopkg.in/check.v1" ) // Hook up gocheck into the "go test" runner. @@ -19,9 +20,11 @@ type VerifySuite struct{} var _ = Suite(&VerifySuite{}) func (VerifySuite) Test(c *C) { + trust := NewEd25519() + signer := NewSigner(trust) type test struct { name string - keys []*data.Key + keys []*keys.PublicKey roles map[string]*data.Role s *data.Signed ver int @@ -76,9 +79,9 @@ func (VerifySuite) Test(c *C) { { name: "more than enough signatures", mut: func(t *test) { - k, _ := keys.NewKey() - Sign(t.s, k.SerializePrivate()) - t.keys = append(t.keys, k.Serialize()) + k, _ := signer.Create() + signer.Sign(t.s, k) + t.keys = append(t.keys, k) t.roles["root"].KeyIDs = append(t.roles["root"].KeyIDs, k.ID) }, }, @@ -93,15 +96,15 @@ func (VerifySuite) Test(c *C) { { name: "unknown key", mut: func(t *test) { - k, _ := keys.NewKey() - Sign(t.s, k.Serialize()) + k, _ := signer.Create() + signer.Sign(t.s, k) }, }, { name: "unknown key below threshold", mut: func(t *test) { - k, _ := keys.NewKey() - Sign(t.s, k.Serialize()) + k, _ := signer.Create() + signer.Sign(t.s, k) t.roles["root"].Threshold = 2 }, err: ErrRoleThreshold, @@ -109,17 +112,17 @@ func (VerifySuite) Test(c *C) { { name: "unknown keys in db", mut: func(t *test) { - k, _ := keys.NewKey() - Sign(t.s, k.Serialize()) - t.keys = append(t.keys, k.Serialize()) + k, _ := signer.Create() + signer.Sign(t.s, k) + t.keys = append(t.keys, k) }, }, { name: "unknown keys in db below threshold", mut: func(t *test) { - k, _ := keys.NewKey() - Sign(t.s, k.Serialize()) - t.keys = append(t.keys, k.Serialize()) + k, _ := signer.Create() + signer.Sign(t.s, k) + t.keys = append(t.keys, k) t.roles["root"].Threshold = 2 }, err: ErrRoleThreshold, @@ -155,14 +158,20 @@ func (VerifySuite) Test(c *C) { t.typ = t.role } if t.keys == nil && t.s == nil { - k, _ := keys.NewKey() - t.s, _ = Marshal(&signedMeta{Type: t.typ, Version: t.ver, Expires: *t.exp}, k.SerializePrivate()) - t.keys = []*data.Key{k.Serialize()} + k, _ := signer.Create() + meta := &signedMeta{Type: t.typ, Version: t.ver, Expires: *t.exp} + + b, err := cjson.Marshal(meta) + c.Assert(err, IsNil) + s := &data.Signed{Signed: b} + signer.Sign(s, k) + t.s = s + t.keys = []*keys.PublicKey{k} } if t.roles == nil { t.roles = map[string]*data.Role{ "root": &data.Role{ - KeyIDs: []string{t.keys[0].ID()}, + KeyIDs: []string{t.keys[0].ID}, Threshold: 1, }, } @@ -173,7 +182,7 @@ func (VerifySuite) Test(c *C) { db := keys.NewDB() for _, k := range t.keys { - err := db.AddKey(k.ID(), k) + err := db.AddKey(k) c.Assert(err, IsNil) } for n, r := range t.roles { diff --git a/Godeps/_workspace/src/github.com/endophage/go-tuf/util/util_test.go b/Godeps/_workspace/src/github.com/endophage/go-tuf/util/util_test.go index 8a7a3ac14e..a3da6d3eea 100644 --- a/Godeps/_workspace/src/github.com/endophage/go-tuf/util/util_test.go +++ b/Godeps/_workspace/src/github.com/endophage/go-tuf/util/util_test.go @@ -5,8 +5,8 @@ import ( "encoding/hex" "testing" - "github.com/endophage/go-tuf/data" . "gopkg.in/check.v1" + "github.com/endophage/go-tuf/data" ) // Hook up gocheck into the "go test" runner.