2021-05-10 21:15:11 -04:00
|
|
|
// Copyright (c) 2015-2021 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 kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-05-07 19:55:37 -04:00
|
|
|
"net/http"
|
2022-08-02 19:58:09 -04:00
|
|
|
"sync"
|
2024-04-15 03:42:50 -04:00
|
|
|
"time"
|
|
|
|
|
2024-03-01 16:09:42 -05:00
|
|
|
"github.com/minio/kms-go/kes"
|
2024-05-07 19:55:37 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2022-03-18 18:14:10 -04:00
|
|
|
)
|
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
type kesConn struct {
|
|
|
|
defaultKeyID string
|
|
|
|
client *kes.Client
|
|
|
|
}
|
2021-05-10 21:15:11 -04:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) Version(ctx context.Context) (string, error) {
|
|
|
|
return c.client.Version(ctx)
|
2021-05-10 21:15:11 -04:00
|
|
|
}
|
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) APIs(ctx context.Context) ([]madmin.KMSAPI, error) {
|
|
|
|
APIs, err := c.client.APIs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return nil, ErrPermission
|
|
|
|
}
|
|
|
|
return nil, Error{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
APICode: "kms:InternalError",
|
|
|
|
Err: "failed to list KMS APIs",
|
|
|
|
Cause: err,
|
|
|
|
}
|
2021-05-10 21:15:11 -04:00
|
|
|
}
|
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
list := make([]madmin.KMSAPI, 0, len(APIs))
|
|
|
|
for _, api := range APIs {
|
|
|
|
list = append(list, madmin.KMSAPI{
|
|
|
|
Method: api.Method,
|
|
|
|
Path: api.Path,
|
|
|
|
MaxBody: api.MaxBody,
|
|
|
|
Timeout: int64(api.Timeout.Truncate(time.Second).Seconds()),
|
2023-02-14 10:19:20 -05:00
|
|
|
})
|
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
return list, nil
|
|
|
|
}
|
2022-03-25 18:01:41 -04:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
// Stat returns the current KES status containing a
|
|
|
|
// list of KES endpoints and the default key ID.
|
|
|
|
func (c *kesConn) Status(ctx context.Context) (map[string]madmin.ItemState, error) {
|
|
|
|
if len(c.client.Endpoints) == 1 {
|
|
|
|
if _, err := c.client.Status(ctx); err != nil {
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return nil, err
|
2022-12-05 14:18:50 -05:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return nil, ErrPermission
|
2022-12-05 14:18:50 -05:00
|
|
|
}
|
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
return map[string]madmin.ItemState{
|
|
|
|
c.client.Endpoints[0]: madmin.ItemOffline,
|
|
|
|
}, nil
|
2022-08-02 19:58:09 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
return map[string]madmin.ItemState{
|
|
|
|
c.client.Endpoints[0]: madmin.ItemOnline,
|
|
|
|
}, nil
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
type Result struct {
|
|
|
|
Endpoint string
|
|
|
|
ItemState madmin.ItemState
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|
2021-05-10 21:15:11 -04:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
results := make([]Result, len(c.client.Endpoints))
|
|
|
|
for i := range c.client.Endpoints {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
2021-05-10 21:15:11 -04:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
client := kes.Client{
|
|
|
|
Endpoints: []string{c.client.Endpoints[i]},
|
|
|
|
HTTPClient: c.client.HTTPClient,
|
|
|
|
}
|
2023-02-16 11:42:24 -05:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
var item madmin.ItemState
|
|
|
|
if _, err := client.Status(ctx); err == nil {
|
|
|
|
item = madmin.ItemOnline
|
|
|
|
} else {
|
|
|
|
item = madmin.ItemOffline
|
|
|
|
}
|
|
|
|
results[i] = Result{
|
|
|
|
Endpoint: c.client.Endpoints[i],
|
|
|
|
ItemState: item,
|
|
|
|
}
|
|
|
|
}(i)
|
2021-05-18 10:22:31 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
wg.Wait()
|
2023-02-21 20:43:01 -05:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
status := make(map[string]madmin.ItemState, len(results))
|
|
|
|
for _, r := range results {
|
|
|
|
if r.ItemState == madmin.ItemOnline {
|
|
|
|
status[r.Endpoint] = madmin.ItemOnline
|
|
|
|
} else {
|
|
|
|
status[r.Endpoint] = madmin.ItemOffline
|
2023-02-21 20:43:01 -05:00
|
|
|
}
|
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
return status, nil
|
2022-07-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2024-08-18 02:43:03 -04:00
|
|
|
func (c *kesConn) ListKeys(ctx context.Context, req *ListRequest) ([]madmin.KMSKeyInfo, string, error) {
|
|
|
|
names, continueAt, err := c.client.ListKeys(ctx, req.Prefix, req.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
keyInfos := make([]madmin.KMSKeyInfo, len(names))
|
|
|
|
for i := range names {
|
|
|
|
keyInfos[i].Name = names[i]
|
|
|
|
}
|
|
|
|
return keyInfos, continueAt, nil
|
2022-10-12 15:08:03 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 21:15:11 -04:00
|
|
|
// CreateKey tries to create a new key at the KMS with the
|
|
|
|
// given key ID.
|
|
|
|
//
|
|
|
|
// If the a key with the same keyID already exists then
|
|
|
|
// CreateKey returns kes.ErrKeyExists.
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) CreateKey(ctx context.Context, req *CreateKeyRequest) error {
|
|
|
|
if err := c.client.CreateKey(ctx, req.Name); err != nil {
|
|
|
|
if errors.Is(err, kes.ErrKeyExists) {
|
|
|
|
return ErrKeyExists
|
|
|
|
}
|
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return ErrPermission
|
|
|
|
}
|
|
|
|
return errKeyCreationFailed(err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-10 21:15:11 -04:00
|
|
|
}
|
|
|
|
|
2022-09-19 14:04:40 -04:00
|
|
|
// DeleteKey deletes a key at the KMS with the given key ID.
|
|
|
|
// Please note that is a dangerous operation.
|
|
|
|
// Once a key has been deleted all data that has been encrypted with it cannot be decrypted
|
|
|
|
// anymore, and therefore, is lost.
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) DeleteKey(ctx context.Context, req *DeleteKeyRequest) error {
|
|
|
|
if err := c.client.DeleteKey(ctx, req.Name); err != nil {
|
|
|
|
if errors.Is(err, kes.ErrKeyNotFound) {
|
|
|
|
return ErrKeyNotFound
|
|
|
|
}
|
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return ErrPermission
|
|
|
|
}
|
|
|
|
return errKeyDeletionFailed(err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-09-19 14:04:40 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 21:15:11 -04:00
|
|
|
// GenerateKey generates a new data encryption key using
|
|
|
|
// the key at the KES server referenced by the key ID.
|
|
|
|
//
|
|
|
|
// The default key ID will be used if keyID is empty.
|
|
|
|
//
|
|
|
|
// The context is associated and tied to the generated DEK.
|
|
|
|
// The same context must be provided when the generated
|
|
|
|
// key should be decrypted.
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) GenerateKey(ctx context.Context, req *GenerateKeyRequest) (DEK, error) {
|
|
|
|
aad, err := req.AssociatedData.MarshalText()
|
2021-05-10 21:15:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return DEK{}, err
|
|
|
|
}
|
2022-12-04 05:34:24 -05:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
name := req.Name
|
|
|
|
if name == "" {
|
|
|
|
name = c.defaultKeyID
|
|
|
|
}
|
|
|
|
|
|
|
|
dek, err := c.client.GenerateKey(ctx, name, aad)
|
2021-05-10 21:15:11 -04:00
|
|
|
if err != nil {
|
2024-05-07 19:55:37 -04:00
|
|
|
if errors.Is(err, kes.ErrKeyNotFound) {
|
|
|
|
return DEK{}, ErrKeyNotFound
|
|
|
|
}
|
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return DEK{}, ErrPermission
|
|
|
|
}
|
|
|
|
return DEK{}, errKeyGenerationFailed(err)
|
2021-05-10 21:15:11 -04:00
|
|
|
}
|
|
|
|
return DEK{
|
2024-05-07 19:55:37 -04:00
|
|
|
KeyID: name,
|
2021-05-10 21:15:11 -04:00
|
|
|
Plaintext: dek.Plaintext,
|
|
|
|
Ciphertext: dek.Ciphertext,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-19 14:04:40 -04:00
|
|
|
// ImportKey imports a cryptographic key into the KMS.
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) ImportKey(ctx context.Context, keyID string, bytes []byte) error {
|
2024-03-01 16:09:42 -05:00
|
|
|
return c.client.ImportKey(ctx, keyID, &kes.ImportKeyRequest{
|
2023-08-19 10:37:53 -04:00
|
|
|
Key: bytes,
|
|
|
|
})
|
2022-09-19 14:04:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptKey Encrypts and authenticates a (small) plaintext with the cryptographic key
|
|
|
|
// The plaintext must not exceed 1 MB
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) EncryptKey(keyID string, plaintext []byte, ctx Context) ([]byte, error) {
|
2022-09-19 14:04:40 -04:00
|
|
|
ctxBytes, err := ctx.MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-01 16:09:42 -05:00
|
|
|
return c.client.Encrypt(context.Background(), keyID, plaintext, ctxBytes)
|
2022-09-19 14:04:40 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 21:15:11 -04:00
|
|
|
// DecryptKey decrypts the ciphertext with the key at the KES
|
|
|
|
// server referenced by the key ID. The context must match the
|
|
|
|
// context value used to generate the ciphertext.
|
2024-05-07 19:55:37 -04:00
|
|
|
func (c *kesConn) Decrypt(ctx context.Context, req *DecryptRequest) ([]byte, error) {
|
|
|
|
aad, err := req.AssociatedData.MarshalText()
|
2021-05-10 21:15:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-19 14:04:40 -04:00
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
plaintext, err := c.client.Decrypt(context.Background(), req.Name, req.Ciphertext, aad)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, kes.ErrKeyNotFound) {
|
|
|
|
return nil, ErrKeyNotFound
|
2023-07-13 02:50:38 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
if errors.Is(err, kes.ErrDecrypt) {
|
|
|
|
return nil, ErrDecrypt
|
2023-07-13 02:50:38 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return nil, ErrPermission
|
2023-07-13 02:50:38 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
return nil, errDecryptionFailed(err)
|
2023-07-13 02:50:38 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
return plaintext, nil
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|
|
|
|
|
2024-05-07 19:55:37 -04:00
|
|
|
// MAC generates the checksum of the given req.Message using the key
|
|
|
|
// with the req.Name at the KMS.
|
|
|
|
func (c *kesConn) MAC(ctx context.Context, req *MACRequest) ([]byte, error) {
|
|
|
|
mac, err := c.client.HMAC(context.Background(), req.Name, req.Message)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, kes.ErrKeyNotFound) {
|
|
|
|
return nil, ErrKeyNotFound
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
if errors.Is(err, kes.ErrNotAllowed) {
|
|
|
|
return nil, ErrPermission
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
if kErr, ok := err.(kes.Error); ok && kErr.Status() == http.StatusNotImplemented {
|
|
|
|
return nil, ErrNotSupported
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|
|
|
|
}
|
2024-05-07 19:55:37 -04:00
|
|
|
return mac, nil
|
2024-04-15 03:42:50 -04:00
|
|
|
}
|