builtin.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2019 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. "github.com/drakkan/sftpgo/v2/internal/util"
  25. )
  26. var (
  27. errMalformedCiphertext = errors.New("malformed ciphertext")
  28. )
  29. type builtinSecret struct {
  30. BaseSecret
  31. }
  32. func init() {
  33. RegisterSecretProvider(sdkkms.SchemeBuiltin, sdkkms.SecretStatusAES256GCM, newBuiltinSecret)
  34. }
  35. func newBuiltinSecret(base BaseSecret, _, _ string) SecretProvider {
  36. return &builtinSecret{
  37. BaseSecret: base,
  38. }
  39. }
  40. func (s *builtinSecret) Name() string {
  41. return "Builtin"
  42. }
  43. func (s *builtinSecret) IsEncrypted() bool {
  44. return s.Status == sdkkms.SecretStatusAES256GCM
  45. }
  46. func (s *builtinSecret) deriveKey(key []byte) []byte {
  47. var combined []byte
  48. combined = append(combined, key...)
  49. if s.AdditionalData != "" {
  50. combined = append(combined, []byte(s.AdditionalData)...)
  51. }
  52. combined = append(combined, key...)
  53. hash := sha256.Sum256(combined)
  54. return hash[:]
  55. }
  56. func (s *builtinSecret) Encrypt() error {
  57. if s.Payload == "" {
  58. return ErrInvalidSecret
  59. }
  60. switch s.Status {
  61. case sdkkms.SecretStatusPlain:
  62. key := make([]byte, 32)
  63. if _, err := io.ReadFull(rand.Reader, key); err != nil {
  64. return err
  65. }
  66. block, err := aes.NewCipher(s.deriveKey(key))
  67. if err != nil {
  68. return err
  69. }
  70. gcm, err := cipher.NewGCM(block)
  71. if err != nil {
  72. return err
  73. }
  74. nonce := make([]byte, gcm.NonceSize())
  75. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  76. return err
  77. }
  78. var aad []byte
  79. if s.AdditionalData != "" {
  80. aad = []byte(s.AdditionalData)
  81. }
  82. ciphertext := gcm.Seal(nonce, nonce, []byte(s.Payload), aad)
  83. s.Key = hex.EncodeToString(key)
  84. s.Payload = hex.EncodeToString(ciphertext)
  85. s.Status = sdkkms.SecretStatusAES256GCM
  86. return nil
  87. default:
  88. return ErrWrongSecretStatus
  89. }
  90. }
  91. func (s *builtinSecret) Decrypt() error {
  92. switch s.Status {
  93. case sdkkms.SecretStatusAES256GCM:
  94. encrypted, err := hex.DecodeString(s.Payload)
  95. if err != nil {
  96. return err
  97. }
  98. key, err := hex.DecodeString(s.Key)
  99. if err != nil {
  100. return err
  101. }
  102. block, err := aes.NewCipher(s.deriveKey(key))
  103. if err != nil {
  104. return err
  105. }
  106. gcm, err := cipher.NewGCM(block)
  107. if err != nil {
  108. return err
  109. }
  110. nonceSize := gcm.NonceSize()
  111. if len(encrypted) < nonceSize {
  112. return errMalformedCiphertext
  113. }
  114. nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
  115. var aad []byte
  116. if s.AdditionalData != "" {
  117. aad = []byte(s.AdditionalData)
  118. }
  119. plaintext, err := gcm.Open(nil, nonce, ciphertext, aad)
  120. if err != nil {
  121. return err
  122. }
  123. s.Status = sdkkms.SecretStatusPlain
  124. s.Payload = util.BytesToString(plaintext)
  125. s.Key = ""
  126. s.AdditionalData = ""
  127. return nil
  128. default:
  129. return ErrWrongSecretStatus
  130. }
  131. }
  132. func (s *builtinSecret) Clone() SecretProvider {
  133. baseSecret := BaseSecret{
  134. Status: s.Status,
  135. Payload: s.Payload,
  136. Key: s.Key,
  137. AdditionalData: s.AdditionalData,
  138. Mode: s.Mode,
  139. }
  140. return newBuiltinSecret(baseSecret, "", "")
  141. }