builtin.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package kms
  15. import (
  16. "crypto/aes"
  17. "crypto/cipher"
  18. "crypto/rand"
  19. "crypto/sha256"
  20. "encoding/hex"
  21. "errors"
  22. "io"
  23. sdkkms "github.com/sftpgo/sdk/kms"
  24. )
  25. var (
  26. errMalformedCiphertext = errors.New("malformed ciphertext")
  27. )
  28. type builtinSecret struct {
  29. BaseSecret
  30. }
  31. func init() {
  32. RegisterSecretProvider(sdkkms.SchemeBuiltin, sdkkms.SecretStatusAES256GCM, newBuiltinSecret)
  33. }
  34. func newBuiltinSecret(base BaseSecret, url, masterKey string) SecretProvider {
  35. return &builtinSecret{
  36. BaseSecret: base,
  37. }
  38. }
  39. func (s *builtinSecret) Name() string {
  40. return "Builtin"
  41. }
  42. func (s *builtinSecret) IsEncrypted() bool {
  43. return s.Status == sdkkms.SecretStatusAES256GCM
  44. }
  45. func (s *builtinSecret) deriveKey(key []byte) []byte {
  46. var combined []byte
  47. combined = append(combined, key...)
  48. if s.AdditionalData != "" {
  49. combined = append(combined, []byte(s.AdditionalData)...)
  50. }
  51. combined = append(combined, key...)
  52. hash := sha256.Sum256(combined)
  53. return hash[:]
  54. }
  55. func (s *builtinSecret) Encrypt() error {
  56. if s.Payload == "" {
  57. return ErrInvalidSecret
  58. }
  59. switch s.Status {
  60. case sdkkms.SecretStatusPlain:
  61. key := make([]byte, 32)
  62. if _, err := io.ReadFull(rand.Reader, key); err != nil {
  63. return err
  64. }
  65. block, err := aes.NewCipher(s.deriveKey(key))
  66. if err != nil {
  67. return err
  68. }
  69. gcm, err := cipher.NewGCM(block)
  70. if err != nil {
  71. return err
  72. }
  73. nonce := make([]byte, gcm.NonceSize())
  74. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  75. return err
  76. }
  77. var aad []byte
  78. if s.AdditionalData != "" {
  79. aad = []byte(s.AdditionalData)
  80. }
  81. ciphertext := gcm.Seal(nonce, nonce, []byte(s.Payload), aad)
  82. s.Key = hex.EncodeToString(key)
  83. s.Payload = hex.EncodeToString(ciphertext)
  84. s.Status = sdkkms.SecretStatusAES256GCM
  85. return nil
  86. default:
  87. return ErrWrongSecretStatus
  88. }
  89. }
  90. func (s *builtinSecret) Decrypt() error {
  91. switch s.Status {
  92. case sdkkms.SecretStatusAES256GCM:
  93. encrypted, err := hex.DecodeString(s.Payload)
  94. if err != nil {
  95. return err
  96. }
  97. key, err := hex.DecodeString(s.Key)
  98. if err != nil {
  99. return err
  100. }
  101. block, err := aes.NewCipher(s.deriveKey(key))
  102. if err != nil {
  103. return err
  104. }
  105. gcm, err := cipher.NewGCM(block)
  106. if err != nil {
  107. return err
  108. }
  109. nonceSize := gcm.NonceSize()
  110. if len(encrypted) < nonceSize {
  111. return errMalformedCiphertext
  112. }
  113. nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
  114. var aad []byte
  115. if s.AdditionalData != "" {
  116. aad = []byte(s.AdditionalData)
  117. }
  118. plaintext, err := gcm.Open(nil, nonce, ciphertext, aad)
  119. if err != nil {
  120. return err
  121. }
  122. s.Status = sdkkms.SecretStatusPlain
  123. s.Payload = string(plaintext)
  124. s.Key = ""
  125. s.AdditionalData = ""
  126. return nil
  127. default:
  128. return ErrWrongSecretStatus
  129. }
  130. }
  131. func (s *builtinSecret) Clone() SecretProvider {
  132. baseSecret := BaseSecret{
  133. Status: s.Status,
  134. Payload: s.Payload,
  135. Key: s.Key,
  136. AdditionalData: s.AdditionalData,
  137. Mode: s.Mode,
  138. }
  139. return newBuiltinSecret(baseSecret, "", "")
  140. }