2019-04-09 14:39:42 -04:00
|
|
|
// MinIO Cloud Storage, (C) 2015, 2016, 2017, 2018 MinIO, Inc.
|
2018-07-18 01:40:34 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
2021-02-24 12:00:15 -05:00
|
|
|
"crypto/sha256"
|
2018-07-18 01:40:34 -04:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/logger"
|
2021-04-15 11:47:33 -04:00
|
|
|
"github.com/minio/minio/pkg/kms"
|
2018-07-18 01:40:34 -04:00
|
|
|
"github.com/minio/sio"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Context is a list of key-value pairs cryptographically
|
|
|
|
// associated with a certain object.
|
2021-04-15 11:47:33 -04:00
|
|
|
type Context = kms.Context
|
2020-10-29 12:34:20 -04:00
|
|
|
|
2018-07-18 01:40:34 -04:00
|
|
|
// KMS represents an active and authenticted connection
|
|
|
|
// to a Key-Management-Service. It supports generating
|
|
|
|
// data key generation and unsealing of KMS-generated
|
|
|
|
// data keys.
|
2021-04-15 11:47:33 -04:00
|
|
|
type KMS = kms.KMS
|
2018-07-18 01:40:34 -04:00
|
|
|
|
|
|
|
type masterKeyKMS struct {
|
2019-10-08 01:47:56 -04:00
|
|
|
keyID string
|
2018-07-18 01:40:34 -04:00
|
|
|
masterKey [32]byte
|
|
|
|
}
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
// NewMasterKey returns a basic KMS implementation from a single 256 bit master key.
|
2018-07-18 01:40:34 -04:00
|
|
|
//
|
|
|
|
// The KMS accepts any keyID but binds the keyID and context cryptographically
|
|
|
|
// to the generated keys.
|
2019-10-08 01:47:56 -04:00
|
|
|
func NewMasterKey(keyID string, key [32]byte) KMS { return &masterKeyKMS{keyID: keyID, masterKey: key} }
|
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
func (m *masterKeyKMS) Stat() (kms.Status, error) {
|
|
|
|
return kms.Status{
|
|
|
|
Name: "MasterKey",
|
|
|
|
DefaultKey: m.keyID,
|
|
|
|
}, nil
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
2018-07-18 01:40:34 -04:00
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
func (m *masterKeyKMS) CreateKey(keyID string) error {
|
2020-07-08 21:50:43 -04:00
|
|
|
return errors.New("crypto: creating keys is not supported by a static master key")
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
func (m *masterKeyKMS) GenerateKey(keyID string, ctx Context) (kms.DEK, error) {
|
|
|
|
if keyID == "" {
|
|
|
|
keyID = m.keyID
|
|
|
|
}
|
|
|
|
|
|
|
|
var key [32]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, key[:]); err != nil {
|
2018-07-18 01:40:34 -04:00
|
|
|
logger.CriticalIf(context.Background(), errOutOfEntropy)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
buffer bytes.Buffer
|
2021-04-15 11:47:33 -04:00
|
|
|
derivedKey = m.deriveKey(keyID, ctx)
|
2018-07-18 01:40:34 -04:00
|
|
|
)
|
2018-07-25 16:35:54 -04:00
|
|
|
if n, err := sio.Encrypt(&buffer, bytes.NewReader(key[:]), sio.Config{Key: derivedKey[:]}); err != nil || n != 64 {
|
2018-07-18 01:40:34 -04:00
|
|
|
logger.CriticalIf(context.Background(), errors.New("KMS: unable to encrypt data key"))
|
|
|
|
}
|
2021-04-15 11:47:33 -04:00
|
|
|
return kms.DEK{
|
|
|
|
KeyID: m.keyID,
|
|
|
|
Plaintext: key[:],
|
|
|
|
Ciphertext: buffer.Bytes(),
|
|
|
|
}, nil
|
2018-07-18 01:40:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
func (m *masterKeyKMS) DecryptKey(keyID string, sealedKey []byte, ctx Context) ([]byte, error) {
|
|
|
|
var derivedKey = m.deriveKey(keyID, ctx)
|
2019-12-11 17:27:03 -05:00
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
var key [32]byte
|
2020-10-29 12:34:20 -04:00
|
|
|
out, err := sio.DecryptBuffer(key[:0], sealedKey, sio.Config{Key: derivedKey[:]})
|
|
|
|
if err != nil || len(out) != 32 {
|
2021-04-15 11:47:33 -04:00
|
|
|
return nil, err // TODO(aead): upgrade sio to use sio.Error
|
2018-07-18 01:40:34 -04:00
|
|
|
}
|
2021-04-15 11:47:33 -04:00
|
|
|
return key[:], nil
|
2018-07-18 01:40:34 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
func (m *masterKeyKMS) deriveKey(keyID string, context Context) (key [32]byte) {
|
2018-07-18 01:40:34 -04:00
|
|
|
if context == nil {
|
|
|
|
context = Context{}
|
|
|
|
}
|
2021-03-20 05:48:48 -04:00
|
|
|
ctxBytes, _ := context.MarshalText()
|
|
|
|
|
2021-04-15 11:47:33 -04:00
|
|
|
mac := hmac.New(sha256.New, m.masterKey[:])
|
2018-07-18 01:40:34 -04:00
|
|
|
mac.Write([]byte(keyID))
|
2021-03-20 05:48:48 -04:00
|
|
|
mac.Write(ctxBytes)
|
2018-07-18 01:40:34 -04:00
|
|
|
mac.Sum(key[:0])
|
|
|
|
return key
|
|
|
|
}
|