|
| 1 | +package mail |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "crypto/cipher" |
| 6 | + "crypto/rand" |
| 7 | + "encoding/base64" |
| 8 | + "errors" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "sync" |
| 14 | + |
| 15 | + "mu/internal/app" |
| 16 | +) |
| 17 | + |
| 18 | +const encPrefix = "enc:" // prefix to identify encrypted fields |
| 19 | + |
| 20 | +var ( |
| 21 | + encKey []byte |
| 22 | + encOnce sync.Once |
| 23 | + encEnabled bool |
| 24 | +) |
| 25 | + |
| 26 | +// initEncryption loads or generates the encryption key |
| 27 | +func initEncryption() { |
| 28 | + encOnce.Do(func() { |
| 29 | + // Try env var first |
| 30 | + if keyStr := os.Getenv("MU_ENCRYPTION_KEY"); keyStr != "" { |
| 31 | + decoded, err := base64.StdEncoding.DecodeString(keyStr) |
| 32 | + if err == nil && len(decoded) == 32 { |
| 33 | + encKey = decoded |
| 34 | + encEnabled = true |
| 35 | + app.Log("mail", "Encryption enabled (from MU_ENCRYPTION_KEY)") |
| 36 | + return |
| 37 | + } |
| 38 | + app.Log("mail", "WARNING: MU_ENCRYPTION_KEY invalid (must be 32 bytes, base64-encoded)") |
| 39 | + } |
| 40 | + |
| 41 | + // Try key file |
| 42 | + keyDir := os.ExpandEnv("$HOME/.mu/keys") |
| 43 | + keyFile := filepath.Join(keyDir, "encryption.key") |
| 44 | + |
| 45 | + if data, err := os.ReadFile(keyFile); err == nil { |
| 46 | + decoded, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(data))) |
| 47 | + if err == nil && len(decoded) == 32 { |
| 48 | + encKey = decoded |
| 49 | + encEnabled = true |
| 50 | + app.Log("mail", "Encryption enabled (from %s)", keyFile) |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Generate new key |
| 56 | + key := make([]byte, 32) |
| 57 | + if _, err := io.ReadFull(rand.Reader, key); err != nil { |
| 58 | + app.Log("mail", "WARNING: Failed to generate encryption key: %v", err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + os.MkdirAll(keyDir, 0700) |
| 63 | + encoded := base64.StdEncoding.EncodeToString(key) |
| 64 | + if err := os.WriteFile(keyFile, []byte(encoded), 0600); err != nil { |
| 65 | + app.Log("mail", "WARNING: Failed to save encryption key: %v", err) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + encKey = key |
| 70 | + encEnabled = true |
| 71 | + app.Log("mail", "Encryption enabled (new key generated at %s)", keyFile) |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +// encrypt encrypts plaintext using AES-256-GCM |
| 76 | +func encrypt(plaintext string) (string, error) { |
| 77 | + if !encEnabled || plaintext == "" { |
| 78 | + return plaintext, nil |
| 79 | + } |
| 80 | + |
| 81 | + block, err := aes.NewCipher(encKey) |
| 82 | + if err != nil { |
| 83 | + return "", err |
| 84 | + } |
| 85 | + |
| 86 | + gcm, err := cipher.NewGCM(block) |
| 87 | + if err != nil { |
| 88 | + return "", err |
| 89 | + } |
| 90 | + |
| 91 | + nonce := make([]byte, gcm.NonceSize()) |
| 92 | + if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
| 93 | + return "", err |
| 94 | + } |
| 95 | + |
| 96 | + ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil) |
| 97 | + return encPrefix + base64.StdEncoding.EncodeToString(ciphertext), nil |
| 98 | +} |
| 99 | + |
| 100 | +// decrypt decrypts ciphertext using AES-256-GCM |
| 101 | +func decrypt(ciphertext string) (string, error) { |
| 102 | + if !encEnabled || ciphertext == "" { |
| 103 | + return ciphertext, nil |
| 104 | + } |
| 105 | + |
| 106 | + // Not encrypted — return as-is (handles pre-encryption messages) |
| 107 | + if !strings.HasPrefix(ciphertext, encPrefix) { |
| 108 | + return ciphertext, nil |
| 109 | + } |
| 110 | + |
| 111 | + data, err := base64.StdEncoding.DecodeString(ciphertext[len(encPrefix):]) |
| 112 | + if err != nil { |
| 113 | + return ciphertext, err |
| 114 | + } |
| 115 | + |
| 116 | + block, err := aes.NewCipher(encKey) |
| 117 | + if err != nil { |
| 118 | + return "", err |
| 119 | + } |
| 120 | + |
| 121 | + gcm, err := cipher.NewGCM(block) |
| 122 | + if err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + |
| 126 | + nonceSize := gcm.NonceSize() |
| 127 | + if len(data) < nonceSize { |
| 128 | + return "", errors.New("ciphertext too short") |
| 129 | + } |
| 130 | + |
| 131 | + nonce, ciphertextBytes := data[:nonceSize], data[nonceSize:] |
| 132 | + plaintext, err := gcm.Open(nil, nonce, ciphertextBytes, nil) |
| 133 | + if err != nil { |
| 134 | + return "", err |
| 135 | + } |
| 136 | + |
| 137 | + return string(plaintext), nil |
| 138 | +} |
| 139 | + |
| 140 | +// encryptMessage encrypts sensitive fields in a message for storage |
| 141 | +func encryptMessage(m *Message) error { |
| 142 | + if !encEnabled { |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + var err error |
| 147 | + |
| 148 | + if m.Subject != "" && !strings.HasPrefix(m.Subject, encPrefix) { |
| 149 | + m.Subject, err = encrypt(m.Subject) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if m.Body != "" && !strings.HasPrefix(m.Body, encPrefix) { |
| 156 | + m.Body, err = encrypt(m.Body) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if m.To != "" && !strings.HasPrefix(m.To, encPrefix) { |
| 163 | + m.To, err = encrypt(m.To) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if m.ToID != "" && !strings.HasPrefix(m.ToID, encPrefix) { |
| 170 | + m.ToID, err = encrypt(m.ToID) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if m.RawHeaders != "" && !strings.HasPrefix(m.RawHeaders, encPrefix) { |
| 177 | + m.RawHeaders, err = encrypt(m.RawHeaders) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +// decryptMessage decrypts sensitive fields in a message after loading |
| 187 | +func decryptMessage(m *Message) error { |
| 188 | + if !encEnabled { |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + var err error |
| 193 | + |
| 194 | + if strings.HasPrefix(m.Subject, encPrefix) { |
| 195 | + m.Subject, err = decrypt(m.Subject) |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if strings.HasPrefix(m.Body, encPrefix) { |
| 202 | + m.Body, err = decrypt(m.Body) |
| 203 | + if err != nil { |
| 204 | + return err |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if strings.HasPrefix(m.To, encPrefix) { |
| 209 | + m.To, err = decrypt(m.To) |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + if strings.HasPrefix(m.ToID, encPrefix) { |
| 216 | + m.ToID, err = decrypt(m.ToID) |
| 217 | + if err != nil { |
| 218 | + return err |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + if strings.HasPrefix(m.RawHeaders, encPrefix) { |
| 223 | + m.RawHeaders, err = decrypt(m.RawHeaders) |
| 224 | + if err != nil { |
| 225 | + return err |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + return nil |
| 230 | +} |
0 commit comments