Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Asra Ali <[email protected]>
  • Loading branch information
asraa committed Sep 7, 2022
1 parent e04074f commit ffec83c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 96 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/gofuzz v1.2.0
github.com/secure-systems-lab/go-securesystemslib v0.4.0
github.com/stretchr/testify v1.8.0
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
Expand All @@ -20,7 +21,6 @@ require (
github.com/kr/text v0.1.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions pkg/deprecated/deprecated_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/secure-systems-lab/go-securesystemslib/cjson"
repo "github.com/theupdateframework/go-tuf"
"github.com/theupdateframework/go-tuf/data"
_ "github.com/theupdateframework/go-tuf/pkg/deprecated/set_ecdsa"
"github.com/theupdateframework/go-tuf/pkg/keys"
. "gopkg.in/check.v1"
)
Expand Down
84 changes: 0 additions & 84 deletions pkg/deprecated/ecdsa_test.go

This file was deleted.

23 changes: 23 additions & 0 deletions pkg/deprecated/set_ecdsa/set_ecdsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package set_ecdsa

import (
"errors"

"github.com/theupdateframework/go-tuf/data"
"github.com/theupdateframework/go-tuf/pkg/keys"
)

/*
Importing this package will allow support for both hex-encoded ECDSA
verifiers and PEM-encoded ECDSA verifiers.
Note that this package imports "github.com/theupdateframework/go-tuf/pkg/keys"
and overrides the ECDSA verifier loaded at init time in that package.
*/

func init() {
_, ok := keys.VerifierMap.Load(data.KeyTypeECDSA_SHA2_P256)
if !ok {
panic(errors.New("expected to override previously loaded PEM-only ECDSA verifier"))
}
keys.VerifierMap.Store(data.KeyTypeECDSA_SHA2_P256, keys.NewDeprecatedEcdsaVerifier)
}
16 changes: 5 additions & 11 deletions pkg/deprecated/ecdsa.go → pkg/keys/deprecated_ecdsa.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package deprecated
package keys

import (
"bytes"
Expand All @@ -12,28 +12,22 @@ import (
"os"

"github.com/theupdateframework/go-tuf/data"
"github.com/theupdateframework/go-tuf/pkg/keys"
)

func init() {
keys.VerifierMap.Delete(data.KeyTypeECDSA_SHA2_P256)
keys.VerifierMap.Store(data.KeyTypeECDSA_SHA2_P256, NewDeprecatedEcdsaVerifier)
}

func NewDeprecatedEcdsaVerifier() keys.Verifier {
func NewDeprecatedEcdsaVerifier() Verifier {
return &ecdsaVerifierWithDeprecatedSupport{}
}

type ecdsaVerifierWithDeprecatedSupport struct {
key *data.PublicKey
// This will switch based on whether this is a PEM-encoded key
// or a deprecated hex-encoded key.
keys.Verifier
Verifier
}

func (p *ecdsaVerifierWithDeprecatedSupport) UnmarshalPublicKey(key *data.PublicKey) error {
p.key = key
pemVerifier := &keys.EcdsaVerifier{}
pemVerifier := &EcdsaVerifier{}
if err := pemVerifier.UnmarshalPublicKey(key); err != nil {
// Try the deprecated hex-encoded verifier
hexVerifier := &deprecatedP256Verifier{}
Expand Down Expand Up @@ -85,7 +79,7 @@ func (p *deprecatedP256Verifier) MarshalPublicKey() *data.PublicKey {

func (p *deprecatedP256Verifier) UnmarshalPublicKey(key *data.PublicKey) error {
// Prepare decoder limited to 512Kb
dec := json.NewDecoder(io.LimitReader(bytes.NewReader(key.Value), keys.MaxJSONKeySize))
dec := json.NewDecoder(io.LimitReader(bytes.NewReader(key.Value), MaxJSONKeySize))

// Unmarshal key value
if err := dec.Decode(p); err != nil {
Expand Down
129 changes: 129 additions & 0 deletions pkg/keys/deprecated_ecdsa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package keys

import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"

"github.com/theupdateframework/go-tuf/data"
. "gopkg.in/check.v1"
)

type DeprecatedECDSASuite struct{}

var _ = Suite(DeprecatedECDSASuite{})

type deprecatedEcdsaSigner struct {
*ecdsa.PrivateKey
}

type deprecatedEcdsaPublic struct {
PublicKey data.HexBytes `json:"public"`
}

func (s deprecatedEcdsaSigner) PublicData() *data.PublicKey {
pub := s.Public().(*ecdsa.PublicKey)
keyValBytes, _ := json.Marshal(deprecatedEcdsaPublic{
PublicKey: elliptic.Marshal(pub.Curve, pub.X, pub.Y)})
return &data.PublicKey{
Type: data.KeyTypeECDSA_SHA2_P256,
Scheme: data.KeySchemeECDSA_SHA2_P256,
Algorithms: data.HashAlgorithms,
Value: keyValBytes,
}
}

func (s deprecatedEcdsaSigner) SignMessage(message []byte) ([]byte, error) {
hash := sha256.Sum256(message)
return s.PrivateKey.Sign(rand.Reader, hash[:], crypto.SHA256)
}

func (s deprecatedEcdsaSigner) ContainsID(id string) bool {
return s.PublicData().ContainsID(id)
}

func (deprecatedEcdsaSigner) MarshalPrivateKey() (*data.PrivateKey, error) {
return nil, errors.New("not implemented for test")
}

func (deprecatedEcdsaSigner) UnmarshalPrivateKey(key *data.PrivateKey) error {
return errors.New("not implemented for test")
}

func generatedDeprecatedSigner() (*deprecatedEcdsaSigner, error) {
privkey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
return &deprecatedEcdsaSigner{privkey}, nil
}

func (DeprecatedECDSASuite) TestSignVerifyDeprecatedFormat(c *C) {
// Create an ecdsa key with a deprecated format.
signer, err := generatedDeprecatedSigner()
c.Assert(err, IsNil)
msg := []byte("foo")
sig, err := signer.SignMessage(msg)
c.Assert(err, IsNil)

pub := signer.PublicKey

keyValBytes, err := json.Marshal(&deprecatedP256Verifier{PublicKey: elliptic.Marshal(pub.Curve, pub.X, pub.Y)})
c.Assert(err, IsNil)
publicData := &data.PublicKey{
Type: data.KeyTypeECDSA_SHA2_P256,
Scheme: data.KeySchemeECDSA_SHA2_P256,
Algorithms: data.HashAlgorithms,
Value: keyValBytes,
}

deprecatedEcdsa := NewDeprecatedEcdsaVerifier()
err = deprecatedEcdsa.UnmarshalPublicKey(publicData)
c.Assert(err, IsNil)
c.Assert(deprecatedEcdsa.Verify(msg, sig), IsNil)
}

func (DeprecatedECDSASuite) TestECDSAVerifyMismatchMessage(c *C) {
signer, err := generatedDeprecatedSigner()
c.Assert(err, IsNil)
msg := []byte("foo")
sig, err := signer.SignMessage(msg)
c.Assert(err, IsNil)
publicData := signer.PublicData()
deprecatedEcdsa := NewDeprecatedEcdsaVerifier()
err = deprecatedEcdsa.UnmarshalPublicKey(publicData)
c.Assert(err, IsNil)
c.Assert(deprecatedEcdsa.Verify([]byte("notfoo"), sig), ErrorMatches, "tuf: deprecated ecdsa signature verification failed")
}

func (DeprecatedECDSASuite) TestECDSAVerifyMismatchPubKey(c *C) {
signer, err := generatedDeprecatedSigner()
c.Assert(err, IsNil)
msg := []byte("foo")
sig, err := signer.SignMessage(msg)
c.Assert(err, IsNil)

signerNew, err := generatedDeprecatedSigner()
c.Assert(err, IsNil)
deprecatedEcdsa := NewDeprecatedEcdsaVerifier()
err = deprecatedEcdsa.UnmarshalPublicKey(signerNew.PublicData())
c.Assert(err, IsNil)
c.Assert(deprecatedEcdsa.Verify([]byte("notfoo"), sig), ErrorMatches, "tuf: deprecated ecdsa signature verification failed")
}

func (DeprecatedECDSASuite) TestMarshalUnmarshalPublicKey(c *C) {
signer, err := generatedDeprecatedSigner()
c.Assert(err, IsNil)

pub := signer.PublicData()

deprecatedEcdsa := NewDeprecatedEcdsaVerifier()
err = deprecatedEcdsa.UnmarshalPublicKey(pub)
c.Assert(err, IsNil)

c.Assert(deprecatedEcdsa.MarshalPublicKey(), DeepEquals, pub)
}

0 comments on commit ffec83c

Please sign in to comment.