2022-08-29 19:57:16 -04:00
|
|
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package hash
|
|
|
|
|
|
|
|
import (
|
2022-10-15 14:58:47 -04:00
|
|
|
"bytes"
|
2023-04-28 11:26:32 -04:00
|
|
|
"context"
|
2022-08-29 19:57:16 -04:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2023-04-14 12:44:45 -04:00
|
|
|
"fmt"
|
2022-08-29 19:57:16 -04:00
|
|
|
"hash"
|
|
|
|
"hash/crc32"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio/internal/hash/sha256"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2023-04-28 11:26:32 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2022-08-29 19:57:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// MinIOMultipartChecksum is as metadata on multipart uploads to indicate checksum type.
|
|
|
|
const MinIOMultipartChecksum = "x-minio-multipart-checksum"
|
|
|
|
|
|
|
|
// ChecksumType contains information about the checksum type.
|
|
|
|
type ChecksumType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
// ChecksumTrailing indicates the checksum will be sent in the trailing header.
|
|
|
|
// Another checksum type will be set.
|
|
|
|
ChecksumTrailing ChecksumType = 1 << iota
|
|
|
|
|
|
|
|
// ChecksumSHA256 indicates a SHA256 checksum.
|
|
|
|
ChecksumSHA256
|
|
|
|
// ChecksumSHA1 indicates a SHA-1 checksum.
|
|
|
|
ChecksumSHA1
|
|
|
|
// ChecksumCRC32 indicates a CRC32 checksum with IEEE table.
|
|
|
|
ChecksumCRC32
|
|
|
|
// ChecksumCRC32C indicates a CRC32 checksum with Castagnoli table.
|
|
|
|
ChecksumCRC32C
|
|
|
|
// ChecksumInvalid indicates an invalid checksum.
|
|
|
|
ChecksumInvalid
|
2023-04-14 12:44:45 -04:00
|
|
|
// ChecksumMultipart indicates the checksum is from a multipart upload.
|
|
|
|
ChecksumMultipart
|
2023-04-28 11:26:32 -04:00
|
|
|
// ChecksumIncludesMultipart indicates the checksum also contains part checksums.
|
|
|
|
ChecksumIncludesMultipart
|
2022-08-29 19:57:16 -04:00
|
|
|
|
|
|
|
// ChecksumNone indicates no checksum.
|
|
|
|
ChecksumNone ChecksumType = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
// Checksum is a type and base 64 encoded value.
|
|
|
|
type Checksum struct {
|
|
|
|
Type ChecksumType
|
|
|
|
Encoded string
|
2022-10-15 14:58:47 -04:00
|
|
|
Raw []byte
|
2022-08-29 19:57:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is returns if c is all of t.
|
|
|
|
func (c ChecksumType) Is(t ChecksumType) bool {
|
|
|
|
if t == ChecksumNone {
|
|
|
|
return c == ChecksumNone
|
|
|
|
}
|
|
|
|
return c&t == t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns the header key.
|
|
|
|
// returns empty string if invalid or none.
|
|
|
|
func (c ChecksumType) Key() string {
|
|
|
|
switch {
|
|
|
|
case c.Is(ChecksumCRC32):
|
|
|
|
return xhttp.AmzChecksumCRC32
|
|
|
|
case c.Is(ChecksumCRC32C):
|
|
|
|
return xhttp.AmzChecksumCRC32C
|
|
|
|
case c.Is(ChecksumSHA1):
|
|
|
|
return xhttp.AmzChecksumSHA1
|
|
|
|
case c.Is(ChecksumSHA256):
|
|
|
|
return xhttp.AmzChecksumSHA256
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawByteLen returns the size of the un-encoded checksum.
|
|
|
|
func (c ChecksumType) RawByteLen() int {
|
|
|
|
switch {
|
|
|
|
case c.Is(ChecksumCRC32):
|
|
|
|
return 4
|
|
|
|
case c.Is(ChecksumCRC32C):
|
|
|
|
return 4
|
|
|
|
case c.Is(ChecksumSHA1):
|
|
|
|
return sha1.Size
|
|
|
|
case c.Is(ChecksumSHA256):
|
|
|
|
return sha256.Size
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet returns whether the type is valid and known.
|
|
|
|
func (c ChecksumType) IsSet() bool {
|
|
|
|
return !c.Is(ChecksumInvalid) && !c.Is(ChecksumNone)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChecksumType returns a checksum type based on the algorithm string.
|
|
|
|
func NewChecksumType(alg string) ChecksumType {
|
|
|
|
switch strings.ToUpper(alg) {
|
|
|
|
case "CRC32":
|
|
|
|
return ChecksumCRC32
|
|
|
|
case "CRC32C":
|
|
|
|
return ChecksumCRC32C
|
|
|
|
case "SHA1":
|
|
|
|
return ChecksumSHA1
|
|
|
|
case "SHA256":
|
|
|
|
return ChecksumSHA256
|
|
|
|
case "":
|
|
|
|
return ChecksumNone
|
|
|
|
}
|
|
|
|
return ChecksumInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the type as a string.
|
|
|
|
func (c ChecksumType) String() string {
|
|
|
|
switch {
|
|
|
|
case c.Is(ChecksumCRC32):
|
|
|
|
return "CRC32"
|
|
|
|
case c.Is(ChecksumCRC32C):
|
|
|
|
return "CRC32C"
|
|
|
|
case c.Is(ChecksumSHA1):
|
|
|
|
return "SHA1"
|
|
|
|
case c.Is(ChecksumSHA256):
|
|
|
|
return "SHA256"
|
|
|
|
case c.Is(ChecksumNone):
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hasher returns a hasher corresponding to the checksum type.
|
|
|
|
// Returns nil if no checksum.
|
|
|
|
func (c ChecksumType) Hasher() hash.Hash {
|
|
|
|
switch {
|
|
|
|
case c.Is(ChecksumCRC32):
|
|
|
|
return crc32.NewIEEE()
|
|
|
|
case c.Is(ChecksumCRC32C):
|
|
|
|
return crc32.New(crc32.MakeTable(crc32.Castagnoli))
|
|
|
|
case c.Is(ChecksumSHA1):
|
|
|
|
return sha1.New()
|
|
|
|
case c.Is(ChecksumSHA256):
|
|
|
|
return sha256.New()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trailing return whether the checksum is traling.
|
|
|
|
func (c ChecksumType) Trailing() bool {
|
|
|
|
return c.Is(ChecksumTrailing)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChecksumFromData returns a new checksum from specified algorithm and base64 encoded value.
|
|
|
|
func NewChecksumFromData(t ChecksumType, data []byte) *Checksum {
|
|
|
|
if !t.IsSet() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h := t.Hasher()
|
|
|
|
h.Write(data)
|
2022-10-15 14:58:47 -04:00
|
|
|
raw := h.Sum(nil)
|
|
|
|
c := Checksum{Type: t, Encoded: base64.StdEncoding.EncodeToString(raw), Raw: raw}
|
2022-08-29 19:57:16 -04:00
|
|
|
if !c.Valid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadCheckSums will read checksums from b and return them.
|
2023-04-28 11:26:32 -04:00
|
|
|
func ReadCheckSums(b []byte, part int) map[string]string {
|
2022-08-29 19:57:16 -04:00
|
|
|
res := make(map[string]string, 1)
|
|
|
|
for len(b) > 0 {
|
|
|
|
t, n := binary.Uvarint(b)
|
|
|
|
if n < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
b = b[n:]
|
|
|
|
|
|
|
|
typ := ChecksumType(t)
|
|
|
|
length := typ.RawByteLen()
|
|
|
|
if length == 0 || len(b) < length {
|
|
|
|
break
|
|
|
|
}
|
2023-04-14 12:44:45 -04:00
|
|
|
cs := base64.StdEncoding.EncodeToString(b[:length])
|
2022-08-29 19:57:16 -04:00
|
|
|
b = b[length:]
|
2023-04-14 12:44:45 -04:00
|
|
|
if typ.Is(ChecksumMultipart) {
|
|
|
|
t, n := binary.Uvarint(b)
|
|
|
|
if n < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cs = fmt.Sprintf("%s-%d", cs, t)
|
|
|
|
b = b[n:]
|
2023-04-28 11:26:32 -04:00
|
|
|
if part > 0 {
|
|
|
|
cs = ""
|
|
|
|
}
|
|
|
|
if typ.Is(ChecksumIncludesMultipart) {
|
|
|
|
wantLen := int(t) * length
|
|
|
|
if len(b) < wantLen {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Read part checksum
|
|
|
|
if part > 0 && uint64(part) <= t {
|
|
|
|
offset := (part - 1) * length
|
|
|
|
partCs := b[offset:]
|
|
|
|
cs = base64.StdEncoding.EncodeToString(partCs[:length])
|
|
|
|
}
|
|
|
|
b = b[wantLen:]
|
|
|
|
}
|
|
|
|
} else if part > 1 {
|
|
|
|
// For non-multipart, checksum is part 1.
|
|
|
|
cs = ""
|
|
|
|
}
|
|
|
|
if cs != "" {
|
|
|
|
res[typ.String()] = cs
|
2023-04-14 12:44:45 -04:00
|
|
|
}
|
2022-08-29 19:57:16 -04:00
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
res = nil
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:58:47 -04:00
|
|
|
// NewChecksumWithType is similar to NewChecksumString but expects input algo of ChecksumType.
|
|
|
|
func NewChecksumWithType(alg ChecksumType, value string) *Checksum {
|
|
|
|
if !alg.IsSet() {
|
2022-08-29 19:57:16 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-15 14:58:47 -04:00
|
|
|
bvalue, err := base64.StdEncoding.DecodeString(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := Checksum{Type: alg, Encoded: value, Raw: bvalue}
|
2022-08-29 19:57:16 -04:00
|
|
|
if !c.Valid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:58:47 -04:00
|
|
|
// NewChecksumString returns a new checksum from specified algorithm and base64 encoded value.
|
|
|
|
func NewChecksumString(alg, value string) *Checksum {
|
|
|
|
return NewChecksumWithType(NewChecksumType(alg), value)
|
|
|
|
}
|
|
|
|
|
2022-08-29 19:57:16 -04:00
|
|
|
// AppendTo will append the checksum to b.
|
2023-04-14 12:44:45 -04:00
|
|
|
// 'parts' is used when checksum has ChecksumMultipart set.
|
2022-08-29 19:57:16 -04:00
|
|
|
// ReadCheckSums reads the values back.
|
2023-04-28 11:26:32 -04:00
|
|
|
func (c *Checksum) AppendTo(b []byte, parts []byte) []byte {
|
2022-08-31 11:13:23 -04:00
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-29 19:57:16 -04:00
|
|
|
var tmp [binary.MaxVarintLen32]byte
|
|
|
|
n := binary.PutUvarint(tmp[:], uint64(c.Type))
|
2022-10-15 14:58:47 -04:00
|
|
|
crc := c.Raw
|
2022-08-29 19:57:16 -04:00
|
|
|
if len(crc) != c.Type.RawByteLen() {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
b = append(b, tmp[:n]...)
|
|
|
|
b = append(b, crc...)
|
2023-04-14 12:44:45 -04:00
|
|
|
if c.Type.Is(ChecksumMultipart) {
|
2023-04-28 11:26:32 -04:00
|
|
|
var checksums int
|
|
|
|
// Ensure we don't divide by 0:
|
|
|
|
if c.Type.RawByteLen() == 0 || len(parts)%c.Type.RawByteLen() != 0 {
|
|
|
|
logger.LogIf(context.Background(), fmt.Errorf("internal error: Unexpected checksum length: %d, each checksum %d", len(parts), c.Type.RawByteLen()))
|
|
|
|
checksums = 0
|
|
|
|
parts = nil
|
|
|
|
} else {
|
|
|
|
checksums = len(parts) / c.Type.RawByteLen()
|
2023-04-14 12:44:45 -04:00
|
|
|
}
|
2023-04-28 11:26:32 -04:00
|
|
|
if !c.Type.Is(ChecksumIncludesMultipart) {
|
|
|
|
parts = nil
|
|
|
|
}
|
|
|
|
n := binary.PutUvarint(tmp[:], uint64(checksums))
|
2023-04-14 12:44:45 -04:00
|
|
|
b = append(b, tmp[:n]...)
|
2023-04-28 11:26:32 -04:00
|
|
|
if len(parts) > 0 {
|
|
|
|
b = append(b, parts...)
|
|
|
|
}
|
2023-04-14 12:44:45 -04:00
|
|
|
}
|
2022-08-29 19:57:16 -04:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns whether checksum is valid.
|
|
|
|
func (c Checksum) Valid() bool {
|
|
|
|
if c.Type == ChecksumInvalid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(c.Encoded) == 0 || c.Type.Is(ChecksumTrailing) {
|
|
|
|
return c.Type.Is(ChecksumNone) || c.Type.Is(ChecksumTrailing)
|
|
|
|
}
|
2022-10-15 14:58:47 -04:00
|
|
|
raw := c.Raw
|
2022-08-29 19:57:16 -04:00
|
|
|
return c.Type.RawByteLen() == len(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches returns whether given content matches c.
|
|
|
|
func (c Checksum) Matches(content []byte) error {
|
|
|
|
if len(c.Encoded) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hasher := c.Type.Hasher()
|
|
|
|
_, err := hasher.Write(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-15 14:58:47 -04:00
|
|
|
sum := hasher.Sum(nil)
|
|
|
|
if !bytes.Equal(sum, c.Raw) {
|
2022-08-29 19:57:16 -04:00
|
|
|
return ChecksumMismatch{
|
|
|
|
Want: c.Encoded,
|
2022-10-15 14:58:47 -04:00
|
|
|
Got: base64.StdEncoding.EncodeToString(sum),
|
2022-08-29 19:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsMap returns the
|
|
|
|
func (c *Checksum) AsMap() map[string]string {
|
|
|
|
if c == nil || !c.Valid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return map[string]string{c.Type.String(): c.Encoded}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransferChecksumHeader will transfer any checksum value that has been checked.
|
|
|
|
func TransferChecksumHeader(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t, s := getContentChecksum(r)
|
|
|
|
if !t.IsSet() || t.Is(ChecksumTrailing) {
|
|
|
|
// TODO: Add trailing when we can read it.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set(t.Key(), s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddChecksumHeader will transfer any checksum value that has been checked.
|
|
|
|
func AddChecksumHeader(w http.ResponseWriter, c map[string]string) {
|
|
|
|
for k, v := range c {
|
2022-10-15 14:58:47 -04:00
|
|
|
cksum := NewChecksumString(k, v)
|
|
|
|
if cksum == nil {
|
2022-08-29 19:57:16 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-10-15 14:58:47 -04:00
|
|
|
if cksum.Valid() {
|
|
|
|
w.Header().Set(cksum.Type.Key(), v)
|
2022-08-29 19:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContentChecksum returns content checksum.
|
|
|
|
// Returns ErrInvalidChecksum if so.
|
|
|
|
// Returns nil, nil if no checksum.
|
|
|
|
func GetContentChecksum(r *http.Request) (*Checksum, error) {
|
|
|
|
t, s := getContentChecksum(r)
|
|
|
|
if t == ChecksumNone {
|
|
|
|
if s == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, ErrInvalidChecksum
|
|
|
|
}
|
2022-10-15 14:58:47 -04:00
|
|
|
cksum := NewChecksumWithType(t, s)
|
|
|
|
if cksum == nil {
|
2022-08-29 19:57:16 -04:00
|
|
|
return nil, ErrInvalidChecksum
|
|
|
|
}
|
2022-10-15 14:58:47 -04:00
|
|
|
return cksum, nil
|
2022-08-29 19:57:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// getContentChecksum returns content checksum type and value.
|
|
|
|
// Returns ChecksumInvalid if so.
|
|
|
|
func getContentChecksum(r *http.Request) (t ChecksumType, s string) {
|
|
|
|
t = ChecksumNone
|
|
|
|
alg := r.Header.Get(xhttp.AmzChecksumAlgo)
|
|
|
|
if alg != "" {
|
|
|
|
t |= NewChecksumType(alg)
|
|
|
|
if t.IsSet() {
|
|
|
|
hdr := t.Key()
|
|
|
|
if s = r.Header.Get(hdr); s == "" {
|
|
|
|
if strings.EqualFold(r.Header.Get(xhttp.AmzTrailer), hdr) {
|
|
|
|
t |= ChecksumTrailing
|
|
|
|
} else {
|
|
|
|
t = ChecksumInvalid
|
|
|
|
}
|
|
|
|
return ChecksumNone, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t, s
|
|
|
|
}
|
|
|
|
checkType := func(c ChecksumType) {
|
|
|
|
if got := r.Header.Get(c.Key()); got != "" {
|
|
|
|
// If already set, invalid
|
|
|
|
if t != ChecksumNone {
|
|
|
|
t = ChecksumInvalid
|
|
|
|
s = ""
|
|
|
|
} else {
|
|
|
|
t = c
|
|
|
|
s = got
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkType(ChecksumCRC32)
|
|
|
|
checkType(ChecksumCRC32C)
|
|
|
|
checkType(ChecksumSHA1)
|
|
|
|
checkType(ChecksumSHA256)
|
|
|
|
return t, s
|
|
|
|
}
|