2014-11-19 21:23:01 +00:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2015-02-20 01:55:05 +00:00
|
|
|
"crypto/sha512"
|
2014-11-19 21:23:01 +00:00
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
|
|
|
)
|
|
|
|
|
2014-11-19 22:59:05 +00:00
|
|
|
// Verifier presents a general verification interface to be used with message
|
|
|
|
// digests and other byte stream verifications. Users instantiate a Verifier
|
|
|
|
// from one of the various methods, write the data under test to it then check
|
|
|
|
// the result with the Verified method.
|
2014-11-19 21:23:01 +00:00
|
|
|
type Verifier interface {
|
|
|
|
io.Writer
|
|
|
|
|
|
|
|
// Verified will return true if the content written to Verifier matches
|
|
|
|
// the digest.
|
|
|
|
Verified() bool
|
|
|
|
|
|
|
|
// Planned methods:
|
|
|
|
// Err() error
|
|
|
|
// Reset()
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:59:05 +00:00
|
|
|
// NewDigestVerifier returns a verifier that compares the written bytes
|
|
|
|
// against a passed in digest.
|
|
|
|
func NewDigestVerifier(d Digest) Verifier {
|
2014-11-19 21:23:01 +00:00
|
|
|
alg := d.Algorithm()
|
|
|
|
switch alg {
|
2015-02-20 01:55:05 +00:00
|
|
|
case "sha256", "sha384", "sha512":
|
2014-11-19 21:23:01 +00:00
|
|
|
return hashVerifier{
|
|
|
|
hash: newHash(alg),
|
|
|
|
digest: d,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Assume we have a tarsum.
|
|
|
|
version, err := tarsum.GetVersionFromTarsum(string(d))
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Always assume valid tarsum at this point.
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
|
|
|
|
// TODO(stevvooe): We may actually want to ban the earlier versions of
|
|
|
|
// tarsum. That decision may not be the place of the verifier.
|
|
|
|
|
|
|
|
ts, err := tarsum.NewTarSum(pr, true, version)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(sday): Ick! A goroutine per digest verification? We'll have to
|
|
|
|
// get the tarsum library to export an io.Writer variant.
|
|
|
|
go func() {
|
|
|
|
io.Copy(ioutil.Discard, ts)
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &tarsumVerifier{
|
|
|
|
digest: d,
|
|
|
|
ts: ts,
|
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:59:05 +00:00
|
|
|
// NewLengthVerifier returns a verifier that returns true when the number of
|
|
|
|
// read bytes equals the expected parameter.
|
|
|
|
func NewLengthVerifier(expected int64) Verifier {
|
2014-11-19 21:23:01 +00:00
|
|
|
return &lengthVerifier{
|
|
|
|
expected: expected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lengthVerifier struct {
|
|
|
|
expected int64 // expected bytes read
|
|
|
|
len int64 // bytes read
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lv *lengthVerifier) Write(p []byte) (n int, err error) {
|
|
|
|
n = len(p)
|
|
|
|
lv.len += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lv *lengthVerifier) Verified() bool {
|
|
|
|
return lv.expected == lv.len
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHash(name string) hash.Hash {
|
|
|
|
switch name {
|
|
|
|
case "sha256":
|
|
|
|
return sha256.New()
|
2015-02-20 01:55:05 +00:00
|
|
|
case "sha384":
|
|
|
|
return sha512.New384()
|
|
|
|
case "sha512":
|
|
|
|
return sha512.New()
|
2014-11-19 21:23:01 +00:00
|
|
|
default:
|
|
|
|
panic("unsupport algorithm: " + name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type hashVerifier struct {
|
|
|
|
digest Digest
|
|
|
|
hash hash.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hv hashVerifier) Write(p []byte) (n int, err error) {
|
|
|
|
return hv.hash.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hv hashVerifier) Verified() bool {
|
|
|
|
return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
type tarsumVerifier struct {
|
|
|
|
digest Digest
|
|
|
|
ts tarsum.TarSum
|
|
|
|
pr *io.PipeReader
|
|
|
|
pw *io.PipeWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tv *tarsumVerifier) Write(p []byte) (n int, err error) {
|
|
|
|
return tv.pw.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tv *tarsumVerifier) Verified() bool {
|
|
|
|
return tv.digest == Digest(tv.ts.Sum(nil))
|
|
|
|
}
|