|
| 1 | +package deprecated |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto" |
| 5 | + "crypto/elliptic" |
| 6 | + "crypto/rand" |
| 7 | + "crypto/sha256" |
| 8 | + "encoding/json" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/secure-systems-lab/go-securesystemslib/cjson" |
| 12 | + repo "github.com/theupdateframework/go-tuf" |
| 13 | + "github.com/theupdateframework/go-tuf/data" |
| 14 | + _ "github.com/theupdateframework/go-tuf/pkg/deprecated/set_ecdsa" |
| 15 | + "github.com/theupdateframework/go-tuf/pkg/keys" |
| 16 | + . "gopkg.in/check.v1" |
| 17 | +) |
| 18 | + |
| 19 | +func Test(t *testing.T) { TestingT(t) } |
| 20 | + |
| 21 | +type RepoSuite struct{} |
| 22 | + |
| 23 | +var _ = Suite(&RepoSuite{}) |
| 24 | + |
| 25 | +func genKey(c *C, r *repo.Repo, role string) []string { |
| 26 | + keyids, err := r.GenKey(role) |
| 27 | + c.Assert(err, IsNil) |
| 28 | + c.Assert(len(keyids) > 0, Equals, true) |
| 29 | + return keyids |
| 30 | +} |
| 31 | + |
| 32 | +// Deprecated ecdsa key support: Support verification against roots that were |
| 33 | +// signed with hex-encoded ecdsa keys. |
| 34 | +func (rs *RepoSuite) TestDeprecatedHexEncodedKeysSucceed(c *C) { |
| 35 | + files := map[string][]byte{"foo.txt": []byte("foo")} |
| 36 | + local := repo.MemoryStore(make(map[string]json.RawMessage), files) |
| 37 | + r, err := repo.NewRepo(local) |
| 38 | + c.Assert(err, IsNil) |
| 39 | + |
| 40 | + r.Init(false) |
| 41 | + // Add a root key with hex-encoded ecdsa format |
| 42 | + signer, err := keys.GenerateEcdsaKey() |
| 43 | + c.Assert(err, IsNil) |
| 44 | + type deprecatedP256Verifier struct { |
| 45 | + PublicKey data.HexBytes `json:"public"` |
| 46 | + } |
| 47 | + pub := signer.PublicKey |
| 48 | + keyValBytes, err := json.Marshal(&deprecatedP256Verifier{PublicKey: elliptic.Marshal(pub.Curve, pub.X, pub.Y)}) |
| 49 | + c.Assert(err, IsNil) |
| 50 | + publicData := &data.PublicKey{ |
| 51 | + Type: data.KeyTypeECDSA_SHA2_P256, |
| 52 | + Scheme: data.KeySchemeECDSA_SHA2_P256, |
| 53 | + Algorithms: data.HashAlgorithms, |
| 54 | + Value: keyValBytes, |
| 55 | + } |
| 56 | + err = r.AddVerificationKey("root", publicData) |
| 57 | + c.Assert(err, IsNil) |
| 58 | + // Add other keys as normal |
| 59 | + genKey(c, r, "targets") |
| 60 | + genKey(c, r, "snapshot") |
| 61 | + genKey(c, r, "timestamp") |
| 62 | + c.Assert(r.AddTarget("foo.txt", nil), IsNil) |
| 63 | + |
| 64 | + // Sign the root role manually |
| 65 | + rootMeta, err := r.SignedMeta("root.json") |
| 66 | + c.Assert(err, IsNil) |
| 67 | + rootCanonical, err := cjson.EncodeCanonical(rootMeta.Signed) |
| 68 | + c.Assert(err, IsNil) |
| 69 | + hash := sha256.Sum256(rootCanonical) |
| 70 | + rootSig, err := signer.PrivateKey.Sign(rand.Reader, hash[:], crypto.SHA256) |
| 71 | + c.Assert(err, IsNil) |
| 72 | + for _, id := range publicData.IDs() { |
| 73 | + c.Assert(r.AddOrUpdateSignature("root.json", data.Signature{ |
| 74 | + KeyID: id, |
| 75 | + Signature: rootSig}), IsNil) |
| 76 | + } |
| 77 | + |
| 78 | + // Committing should succeed because the deprecated key pkg is added. |
| 79 | + c.Assert(r.Snapshot(), IsNil) |
| 80 | + c.Assert(r.Timestamp(), IsNil) |
| 81 | + c.Assert(r.Commit(), IsNil) |
| 82 | +} |
0 commit comments