155 lines
4.9 KiB
Go
155 lines
4.9 KiB
Go
// Package signedoci fetches OCI artifacts whose first layer is a statement
|
|
// signed by a distribution source's key. The extension catalogue and releases
|
|
// are both published this way.
|
|
package signedoci
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
"oras.land/oras-go/v2/registry/remote"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/distribution"
|
|
)
|
|
|
|
const maxManifestBytes = 256 << 10
|
|
|
|
// Artifact is a fetched manifest whose statement layer verified against the
|
|
// source's key. Its other layers are read through Blob, which checks each
|
|
// against the digest the manifest names.
|
|
type Artifact struct {
|
|
Manifest ocispec.Manifest
|
|
Statement []byte
|
|
|
|
repository *remote.Repository
|
|
}
|
|
|
|
// Fetch resolves tag in the source's repository and verifies the statement
|
|
// layer: annotation on the manifest must be the key's signature over the
|
|
// statement bytes. maxStatement bounds the statement read.
|
|
func Fetch(ctx context.Context, source distribution.Source, tag, annotation string, maxStatement int) (Artifact, error) {
|
|
if len(source.Key) != ed25519.PublicKeySize {
|
|
return Artifact{}, errors.New("no valid signing key")
|
|
}
|
|
reference := source.Reference + ":" + tag
|
|
repository, err := remote.NewRepository(reference)
|
|
if err != nil {
|
|
return Artifact{}, err
|
|
}
|
|
repository.PlainHTTP = Loopback(reference)
|
|
_, stream, err := repository.FetchReference(ctx, reference)
|
|
if err != nil {
|
|
return Artifact{}, fmt.Errorf("%s: %w", reference, err)
|
|
}
|
|
raw, err := readBounded(stream, maxManifestBytes)
|
|
if err != nil {
|
|
return Artifact{}, err
|
|
}
|
|
var manifest ocispec.Manifest
|
|
if err := json.Unmarshal(raw, &manifest); err != nil {
|
|
return Artifact{}, fmt.Errorf("%s manifest: %w", reference, err)
|
|
}
|
|
if len(manifest.Layers) == 0 {
|
|
return Artifact{}, fmt.Errorf("%s has no statement layer", reference)
|
|
}
|
|
layer, err := repository.Fetch(ctx, manifest.Layers[0])
|
|
if err != nil {
|
|
return Artifact{}, fmt.Errorf("%s statement: %w", reference, err)
|
|
}
|
|
statement, err := readBounded(layer, maxStatement)
|
|
if err != nil {
|
|
return Artifact{}, err
|
|
}
|
|
if err := Verify(manifest, statement, source.Key, annotation); err != nil {
|
|
return Artifact{}, fmt.Errorf("%s: %w", reference, err)
|
|
}
|
|
return Artifact{Manifest: manifest, Statement: statement, repository: repository}, nil
|
|
}
|
|
|
|
// Verify checks statement is the first layer the manifest names and that key
|
|
// signed exactly those bytes under annotation.
|
|
func Verify(manifest ocispec.Manifest, statement []byte, key ed25519.PublicKey, annotation string) error {
|
|
if len(manifest.Layers) == 0 || Digest(statement) != manifest.Layers[0].Digest.String() {
|
|
return errors.New("statement does not match its manifest digest")
|
|
}
|
|
signature, err := base64.StdEncoding.DecodeString(manifest.Annotations[annotation])
|
|
if err != nil || !ed25519.Verify(key, statement, signature) {
|
|
return errors.New("signature does not verify")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Blob copies the layer named digest to out, refusing one the manifest does
|
|
// not list or whose bytes do not hash to it.
|
|
func (a Artifact) Blob(ctx context.Context, digest string, out io.Writer, limit int64) error {
|
|
for _, layer := range a.Manifest.Layers {
|
|
if layer.Digest.String() != digest {
|
|
continue
|
|
}
|
|
if layer.Size > limit {
|
|
return fmt.Errorf("layer %s exceeds the size limit", digest)
|
|
}
|
|
stream, err := a.repository.Fetch(ctx, layer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = stream.Close() }()
|
|
hash := sha256.New()
|
|
n, err := io.Copy(io.MultiWriter(out, hash), io.LimitReader(stream, limit+1))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n > limit {
|
|
return fmt.Errorf("layer %s exceeds the size limit", digest)
|
|
}
|
|
if "sha256:"+hex.EncodeToString(hash.Sum(nil)) != digest {
|
|
return fmt.Errorf("layer %s does not match its digest", digest)
|
|
}
|
|
return nil
|
|
}
|
|
return fmt.Errorf("artifact has no layer %s", digest)
|
|
}
|
|
|
|
// Digest is the OCI content digest of data.
|
|
func Digest(data []byte) string {
|
|
sum := sha256.Sum256(data)
|
|
return "sha256:" + hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// Loopback reports whether reference names a registry on this host, the
|
|
// only one reached over plain HTTP. The signature, not the transport, is
|
|
// what makes an artifact trustworthy.
|
|
func Loopback(reference string) bool {
|
|
host, _, ok := strings.Cut(reference, "/")
|
|
if !ok {
|
|
return false
|
|
}
|
|
if name, _, err := net.SplitHostPort(host); err == nil {
|
|
host = name
|
|
}
|
|
host = strings.Trim(host, "[]")
|
|
return host == "localhost" || net.ParseIP(host).IsLoopback()
|
|
}
|
|
|
|
func readBounded(stream io.ReadCloser, limit int) ([]byte, error) {
|
|
defer func() { _ = stream.Close() }()
|
|
data, err := io.ReadAll(io.LimitReader(stream, int64(limit)+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(data) > limit {
|
|
return nil, errors.New("artifact content exceeds the size limit")
|
|
}
|
|
return data, nil
|
|
}
|