secretbox.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package email
  2. /*
  3. secretbox.go
  4. Seals account passwords, OAuth refresh tokens and OAuth client secrets
  5. with AES-256-GCM before they reach the database. The key is a single
  6. 0600 file next to the mail database, created on first use, so a copy of
  7. mail.db alone never discloses anyone's mailbox password.
  8. */
  9. import (
  10. "crypto/aes"
  11. "crypto/cipher"
  12. "crypto/rand"
  13. "encoding/base64"
  14. "errors"
  15. "io"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. )
  20. const keyFileName = "secret.key"
  21. // secretBox seals and opens short secrets.
  22. type secretBox struct {
  23. aead cipher.AEAD
  24. }
  25. // loadSecretBox reads the key from dir, generating one on first run.
  26. func loadSecretBox(dir string) (*secretBox, error) {
  27. if err := os.MkdirAll(dir, 0700); err != nil {
  28. return nil, err
  29. }
  30. keyFile := filepath.Join(dir, keyFileName)
  31. var key []byte
  32. if content, err := os.ReadFile(keyFile); err == nil {
  33. decoded, derr := base64.StdEncoding.DecodeString(strings.TrimSpace(string(content)))
  34. if derr == nil && len(decoded) == 32 {
  35. key = decoded
  36. }
  37. }
  38. if key == nil {
  39. //A missing or damaged key only costs users their saved passwords,
  40. //which the UI asks for again (the account reports an auth error).
  41. key = make([]byte, 32)
  42. if _, err := rand.Read(key); err != nil {
  43. return nil, err
  44. }
  45. if err := os.WriteFile(keyFile, []byte(base64.StdEncoding.EncodeToString(key)), 0600); err != nil {
  46. return nil, err
  47. }
  48. }
  49. return newSecretBox(key)
  50. }
  51. func newSecretBox(key []byte) (*secretBox, error) {
  52. block, err := aes.NewCipher(key)
  53. if err != nil {
  54. return nil, err
  55. }
  56. aead, err := cipher.NewGCM(block)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return &secretBox{aead: aead}, nil
  61. }
  62. // Seal encrypts plaintext and returns base64(nonce || ciphertext). An empty
  63. // plaintext seals to an empty string so "no secret" stays recognisable.
  64. func (s *secretBox) Seal(plaintext string) (string, error) {
  65. if plaintext == "" {
  66. return "", nil
  67. }
  68. nonce := make([]byte, s.aead.NonceSize())
  69. if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
  70. return "", err
  71. }
  72. sealed := s.aead.Seal(nonce, nonce, []byte(plaintext), nil)
  73. return base64.StdEncoding.EncodeToString(sealed), nil
  74. }
  75. // Open reverses Seal.
  76. func (s *secretBox) Open(encoded string) (string, error) {
  77. if encoded == "" {
  78. return "", nil
  79. }
  80. raw, err := base64.StdEncoding.DecodeString(encoded)
  81. if err != nil {
  82. return "", err
  83. }
  84. if len(raw) < s.aead.NonceSize() {
  85. return "", errors.New("sealed secret is truncated")
  86. }
  87. nonce, ciphertext := raw[:s.aead.NonceSize()], raw[s.aead.NonceSize():]
  88. plaintext, err := s.aead.Open(nil, nonce, ciphertext, nil)
  89. if err != nil {
  90. return "", errors.New("stored secret cannot be decrypted, please sign in again")
  91. }
  92. return string(plaintext), nil
  93. }