2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// 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, version 3.
|
|
|
|
//
|
|
|
|
// 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
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
package kms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-05 16:23:40 +00:00
|
|
|
"crypto/sha256"
|
2020-11-30 20:46:34 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
|
2022-01-06 10:54:43 +00:00
|
|
|
sdkkms "github.com/sftpgo/sdk/kms"
|
2020-11-30 20:46:34 +00:00
|
|
|
"gocloud.dev/secrets/localsecrets"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
|
|
|
)
|
|
|
|
|
2021-07-13 19:17:21 +00:00
|
|
|
func init() {
|
2022-01-06 09:11:47 +00:00
|
|
|
RegisterSecretProvider(sdkkms.SchemeLocal, sdkkms.SecretStatusSecretBox, NewLocalSecret)
|
2021-07-13 19:17:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
type localSecret struct {
|
2022-01-04 15:07:41 +00:00
|
|
|
BaseSecret
|
2020-11-30 20:46:34 +00:00
|
|
|
masterKey string
|
|
|
|
}
|
|
|
|
|
2021-07-13 19:17:21 +00:00
|
|
|
// NewLocalSecret returns a SecretProvider that use a locally provided symmetric key
|
2022-01-04 15:07:41 +00:00
|
|
|
func NewLocalSecret(base BaseSecret, url, masterKey string) SecretProvider {
|
2020-11-30 20:46:34 +00:00
|
|
|
return &localSecret{
|
2021-07-13 19:17:21 +00:00
|
|
|
BaseSecret: base,
|
2020-11-30 20:46:34 +00:00
|
|
|
masterKey: masterKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localSecret) Name() string {
|
2021-07-13 19:17:21 +00:00
|
|
|
return "Local"
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localSecret) IsEncrypted() bool {
|
2022-01-06 09:11:47 +00:00
|
|
|
return s.Status == sdkkms.SecretStatusSecretBox
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localSecret) Encrypt() error {
|
2022-01-06 09:11:47 +00:00
|
|
|
if s.Status != sdkkms.SecretStatusPlain {
|
2022-01-04 15:07:41 +00:00
|
|
|
return ErrWrongSecretStatus
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
if s.Payload == "" {
|
2022-01-04 15:07:41 +00:00
|
|
|
return ErrInvalidSecret
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
secretKey, err := localsecrets.NewRandomKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-01 21:18:16 +00:00
|
|
|
key, err := s.deriveKey(secretKey[:], false)
|
2020-11-30 20:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keeper := localsecrets.NewKeeper(key)
|
|
|
|
defer keeper.Close()
|
|
|
|
|
|
|
|
ciphertext, err := keeper.Encrypt(context.Background(), []byte(s.Payload))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Key = hex.EncodeToString(secretKey[:])
|
|
|
|
s.Payload = base64.StdEncoding.EncodeToString(ciphertext)
|
2022-01-06 09:11:47 +00:00
|
|
|
s.Status = sdkkms.SecretStatusSecretBox
|
2020-12-01 21:18:16 +00:00
|
|
|
s.Mode = s.getEncryptionMode()
|
2020-11-30 20:46:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *localSecret) Decrypt() error {
|
|
|
|
if !s.IsEncrypted() {
|
2022-01-04 15:07:41 +00:00
|
|
|
return ErrWrongSecretStatus
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
encrypted, err := base64.StdEncoding.DecodeString(s.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
secretKey, err := hex.DecodeString(s.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-01 21:18:16 +00:00
|
|
|
key, err := s.deriveKey(secretKey[:], true)
|
2020-11-30 20:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keeper := localsecrets.NewKeeper(key)
|
|
|
|
defer keeper.Close()
|
|
|
|
|
|
|
|
plaintext, err := keeper.Decrypt(context.Background(), encrypted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 09:11:47 +00:00
|
|
|
s.Status = sdkkms.SecretStatusPlain
|
2020-11-30 20:46:34 +00:00
|
|
|
s.Payload = string(plaintext)
|
|
|
|
s.Key = ""
|
|
|
|
s.AdditionalData = ""
|
2020-12-01 21:18:16 +00:00
|
|
|
s.Mode = 0
|
2020-11-30 20:46:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:18:16 +00:00
|
|
|
func (s *localSecret) deriveKey(key []byte, isForDecryption bool) ([32]byte, error) {
|
2020-11-30 20:46:34 +00:00
|
|
|
var masterKey []byte
|
2020-12-01 21:18:16 +00:00
|
|
|
if s.masterKey == "" || (isForDecryption && s.Mode == 0) {
|
2020-11-30 20:46:34 +00:00
|
|
|
var combined []byte
|
|
|
|
combined = append(combined, key...)
|
|
|
|
if s.AdditionalData != "" {
|
|
|
|
combined = append(combined, []byte(s.AdditionalData)...)
|
|
|
|
}
|
|
|
|
combined = append(combined, key...)
|
|
|
|
hash := sha256.Sum256(combined)
|
|
|
|
masterKey = hash[:]
|
|
|
|
} else {
|
|
|
|
masterKey = []byte(s.masterKey)
|
|
|
|
}
|
|
|
|
var derivedKey [32]byte
|
|
|
|
var info []byte
|
|
|
|
if s.AdditionalData != "" {
|
|
|
|
info = []byte(s.AdditionalData)
|
|
|
|
}
|
|
|
|
kdf := hkdf.New(sha256.New, masterKey, key, info)
|
|
|
|
if _, err := io.ReadFull(kdf, derivedKey[:]); err != nil {
|
|
|
|
return derivedKey, err
|
|
|
|
}
|
|
|
|
return derivedKey, nil
|
|
|
|
}
|
2020-12-01 21:18:16 +00:00
|
|
|
|
|
|
|
func (s *localSecret) getEncryptionMode() int {
|
|
|
|
if s.masterKey == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
2021-07-13 19:17:21 +00:00
|
|
|
|
2022-01-04 15:07:41 +00:00
|
|
|
func (s *localSecret) Clone() SecretProvider {
|
|
|
|
baseSecret := BaseSecret{
|
2021-07-13 19:17:21 +00:00
|
|
|
Status: s.Status,
|
|
|
|
Payload: s.Payload,
|
|
|
|
Key: s.Key,
|
|
|
|
AdditionalData: s.AdditionalData,
|
|
|
|
Mode: s.Mode,
|
|
|
|
}
|
|
|
|
return NewLocalSecret(baseSecret, "", s.masterKey)
|
|
|
|
}
|