package publicrelease import ( "crypto/ed25519" "encoding/base64" "errors" "regexp" "strings" ) // ErrSums is every way a checksum list can fail VerifySums. var ErrSums = errors.New("publicrelease: signed checksum list refused") var sumLine = regexp.MustCompile(`^([0-9a-f]{64}) (\S+)$`) // VerifySums checks a release's SHA256SUMS.txt against its .sig with pubKey // (base64 ed25519, over the raw sums bytes, as Feed.Sums checks it) and // returns asset name to sha256. Unlike Feed.Sums it is strict, because its // caller signs what it returns into deployment authority: every line must be // sha256sum's text form, "<64 lowercase hex> ", each name once, and a // list with no lines is refused. It reads nothing from the network. func VerifySums(pubKey string, sums []byte, sig string) (map[string]string, error) { pub, err := base64.StdEncoding.DecodeString(pubKey) if err != nil || len(pub) != ed25519.PublicKeySize { return nil, ErrSums } raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(sig)) if err != nil || len(raw) != ed25519.SignatureSize || !ed25519.Verify(ed25519.PublicKey(pub), sums, raw) { return nil, ErrSums } text := string(sums) if !strings.HasSuffix(text, "\n") { return nil, ErrSums } out := map[string]string{} for _, line := range strings.Split(strings.TrimSuffix(text, "\n"), "\n") { m := sumLine.FindStringSubmatch(line) if m == nil { return nil, ErrSums } if _, dup := out[m[2]]; dup { return nil, ErrSums } out[m[2]] = m[1] } return out, nil }