mirror of https://github.com/docker/docs.git
commit
b79d6d088b
|
@ -20,3 +20,21 @@ type ErrMissingMeta struct {
|
|||
func (e ErrMissingMeta) Error() string {
|
||||
return fmt.Sprintf("tuf: sha256 checksum required for %s", e.Role)
|
||||
}
|
||||
|
||||
// ErrInvalidChecksum is the error to be returned when checksum is invalid
|
||||
type ErrInvalidChecksum struct {
|
||||
alg string
|
||||
}
|
||||
|
||||
func (e ErrInvalidChecksum) Error() string {
|
||||
return fmt.Sprintf("%s checksum invalid", e.alg)
|
||||
}
|
||||
|
||||
// ErrMismatchedChecksum is the error to be returned when checksum is mismatched
|
||||
type ErrMismatchedChecksum struct {
|
||||
alg string
|
||||
}
|
||||
|
||||
func (e ErrMismatchedChecksum) Error() string {
|
||||
return fmt.Sprintf("%s checksum mismatched", e.alg)
|
||||
}
|
||||
|
|
|
@ -141,13 +141,13 @@ func CheckHashes(payload []byte, hashes Hashes) error {
|
|||
case notary.SHA256:
|
||||
checksum := sha256.Sum256(payload)
|
||||
if subtle.ConstantTimeCompare(checksum[:], v) == 0 {
|
||||
return fmt.Errorf("%s checksum mismatched", k)
|
||||
return ErrMismatchedChecksum{alg: notary.SHA256}
|
||||
}
|
||||
cnt++
|
||||
case notary.SHA512:
|
||||
checksum := sha512.Sum512(payload)
|
||||
if subtle.ConstantTimeCompare(checksum[:], v) == 0 {
|
||||
return fmt.Errorf("%s checksum mismatched", k)
|
||||
return ErrMismatchedChecksum{alg: notary.SHA512}
|
||||
}
|
||||
cnt++
|
||||
}
|
||||
|
@ -169,12 +169,12 @@ func CheckValidHashStructures(hashes Hashes) error {
|
|||
switch k {
|
||||
case notary.SHA256:
|
||||
if len(v) != sha256.Size {
|
||||
return fmt.Errorf("invalid %s checksum", notary.SHA256)
|
||||
return ErrInvalidChecksum{alg: notary.SHA256}
|
||||
}
|
||||
cnt++
|
||||
case notary.SHA512:
|
||||
if len(v) != sha512.Size {
|
||||
return fmt.Errorf("invalid %s checksum", notary.SHA512)
|
||||
return ErrInvalidChecksum{alg: notary.SHA512}
|
||||
}
|
||||
cnt++
|
||||
}
|
||||
|
|
|
@ -102,15 +102,13 @@ func TestCheckHashes(t *testing.T) {
|
|||
malicious256 := make(Hashes)
|
||||
malicious256["sha256"] = []byte("malicious data")
|
||||
err = CheckHashes(raw, malicious256)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "checksum mismatched")
|
||||
require.IsType(t, ErrMismatchedChecksum{}, err)
|
||||
|
||||
// Expected to fail due to the failure of sha512
|
||||
malicious512 := make(Hashes)
|
||||
malicious512["sha512"] = []byte("malicious data")
|
||||
err = CheckHashes(raw, malicious512)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "checksum mismatched")
|
||||
require.IsType(t, ErrMismatchedChecksum{}, err)
|
||||
|
||||
// Expected to fail because of the failure of sha512
|
||||
// even though the sha256 is OK.
|
||||
|
@ -120,8 +118,7 @@ func TestCheckHashes(t *testing.T) {
|
|||
doubleFace["sha512"], err = hex.DecodeString("d13e2b60d74c2e6f4f449b5e536814edf9a4827f5a9f4f957fc92e77609b9c92")
|
||||
require.NoError(t, err)
|
||||
err = CheckHashes(raw, doubleFace)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "checksum mismatched")
|
||||
require.IsType(t, ErrMismatchedChecksum{}, err)
|
||||
}
|
||||
|
||||
func TestCheckValidHashStructures(t *testing.T) {
|
||||
|
@ -158,6 +155,5 @@ func TestCheckValidHashStructures(t *testing.T) {
|
|||
// Should failed since the first '0' is missing.
|
||||
hashes["sha256"], err = hex.DecodeString("1234567890a4f2307e49160fa242db6fb95f071ad81a198eeb7d770e61cd6d8")
|
||||
err = CheckValidHashStructures(hashes)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "invalid")
|
||||
require.IsType(t, ErrInvalidChecksum{}, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue