| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"mime" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/ProtonMail/gluon/rfc822" |
| 12 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 13 |
"github.com/google/uuid" |
| 14 |
"golang.org/x/text/encoding/htmlindex" |
| 15 |
"golang.org/x/text/encoding/ianaindex" |
| 16 |
) |
| 17 |
|
| 18 |
// CharsetReader returns a charset decoder for the given charset. |
| 19 |
// If set, it will be used to decode non-utf8 encoded messages. |
| 20 |
var CharsetReader func(charset string, input io.Reader) (io.Reader, error) |
| 21 |
|
| 22 |
// EncryptRFC822 encrypts the given message literal as a PGP attachment. |
| 23 |
func EncryptRFC822(kr *crypto.KeyRing, literal []byte) ([]byte, error) { |
| 24 |
var buf bytes.Buffer |
| 25 |
|
| 26 |
if err := tryEncrypt(&buf, kr, rfc822.Parse(literal)); err != nil { |
| 27 |
return encryptFull(kr, literal) |
| 28 |
} |
| 29 |
|
| 30 |
return buf.Bytes(), nil |
| 31 |
} |
| 32 |
|
| 33 |
// tryEncrypt tries to encrypt the given message section. |
| 34 |
// It first checks if the message is encrypted/signed or has multiple text parts. |
| 35 |
// If so, it returns an error -- we need to encrypt the whole message as a PGP attachment. |
| 36 |
func tryEncrypt(w io.Writer, kr *crypto.KeyRing, s *rfc822.Section) error { |
| 37 |
var textCount int |
| 38 |
|
| 39 |
if err := s.Walk(func(s *rfc822.Section) error { |
| 40 |
// Ensure we can read the content type. |
| 41 |
contentType, _, err := s.ContentType() |
| 42 |
if err != nil { |
| 43 |
return fmt.Errorf("cannot read content type: %w", err) |
| 44 |
} |
| 45 |
|
| 46 |
// Ensure we can read the content disposition. |
| 47 |
if header, err := s.ParseHeader(); err != nil { |
| 48 |
return fmt.Errorf("cannot read header: %w", err) |
| 49 |
} else if header.Has("Content-Disposition") { |
| 50 |
if _, _, err := rfc822.ParseMediaType(header.Get("Content-Disposition")); err != nil { |
| 51 |
return fmt.Errorf("cannot read content disposition: %w", err) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Check if the message is already encrypted or signed. |
| 56 |
if contentType.SubType() == "encrypted" { |
| 57 |
return fmt.Errorf("already encrypted") |
| 58 |
} else if contentType.SubType() == "signed" { |
| 59 |
return fmt.Errorf("already signed") |
| 60 |
} |
| 61 |
|
| 62 |
if contentType.Type() != "text" { |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
if textCount++; textCount > 1 { |
| 67 |
return fmt.Errorf("multiple text parts") |
| 68 |
} |
| 69 |
|
| 70 |
return nil |
| 71 |
}); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
|
| 75 |
return encrypt(w, kr, s) |
| 76 |
} |
| 77 |
|
| 78 |
// encrypt encrypts the given message section with the given keyring and writes the result to w. |
| 79 |
func encrypt(w io.Writer, kr *crypto.KeyRing, s *rfc822.Section) error { |
| 80 |
contentType, contentParams, err := s.ContentType() |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
|
| 85 |
if contentType.IsMultiPart() { |
| 86 |
return encryptMultipart(w, kr, s, contentParams["boundary"]) |
| 87 |
} |
| 88 |
|
| 89 |
if contentType.Type() == "text" || contentType.Type() == "message" { |
| 90 |
return encryptText(w, kr, s) |
| 91 |
} |
| 92 |
|
| 93 |
return encryptAtt(w, kr, s) |
| 94 |
} |
| 95 |
|
| 96 |
// encryptMultipart encrypts the given multipart message section with the given keyring and writes the result to w. |
| 97 |
func encryptMultipart(w io.Writer, kr *crypto.KeyRing, s *rfc822.Section, boundary string) error { |
| 98 |
// Write the header. |
| 99 |
if _, err := w.Write(s.Header()); err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
|
| 103 |
// Create a new multipart writer with the boundary from the header. |
| 104 |
ww := rfc822.NewMultipartWriter(w, boundary) |
| 105 |
|
| 106 |
children, err := s.Children() |
| 107 |
if err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
// Encrypt each child part. |
| 112 |
for _, child := range children { |
| 113 |
if err := ww.AddPart(func(w io.Writer) error { |
| 114 |
return encrypt(w, kr, child) |
| 115 |
}); err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return ww.Done() |
| 121 |
} |
| 122 |
|
| 123 |
// encryptText encrypts the given text message section with the given keyring and writes the result to w. |
| 124 |
func encryptText(w io.Writer, kr *crypto.KeyRing, s *rfc822.Section) error { |
| 125 |
contentType, contentParams, err := s.ContentType() |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
|
| 130 |
header, err := s.ParseHeader() |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
body, err := s.DecodedBody() |
| 136 |
if err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
// Remove the Content-Transfer-Encoding header as we decode the body. |
| 141 |
header.Del("Content-Transfer-Encoding") |
| 142 |
|
| 143 |
// If the text part has a charset, decode it to UTF-8. |
| 144 |
if charset, ok := contentParams["charset"]; ok { |
| 145 |
decoder, err := getCharsetDecoder(bytes.NewReader(body), charset) |
| 146 |
if err != nil { |
| 147 |
return err |
| 148 |
} |
| 149 |
|
| 150 |
if body, err = io.ReadAll(decoder); err != nil { |
| 151 |
return err |
| 152 |
} |
| 153 |
|
| 154 |
// Remove old content type. |
| 155 |
header.Del("Content-Type") |
| 156 |
|
| 157 |
header.Set("Content-Type", mime.FormatMediaType( |
| 158 |
string(contentType), |
| 159 |
replace(contentParams, "charset", "utf-8")), |
| 160 |
) |
| 161 |
} |
| 162 |
|
| 163 |
// Encrypt the body. |
| 164 |
enc, err := kr.Encrypt(crypto.NewPlainMessage(body), nil) |
| 165 |
if err != nil { |
| 166 |
return err |
| 167 |
} |
| 168 |
|
| 169 |
// Armor the encrypted body. |
| 170 |
arm, err := enc.GetArmored() |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
|
| 175 |
// Write the header. |
| 176 |
if _, err := w.Write(header.Raw()); err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
// Write the armored body. |
| 181 |
if _, err := w.Write([]byte(arm)); err != nil { |
| 182 |
return err |
| 183 |
} |
| 184 |
|
| 185 |
return nil |
| 186 |
} |
| 187 |
|
| 188 |
// encryptAtt encrypts the given attachment section with the given keyring and writes the result to w. |
| 189 |
func encryptAtt(w io.Writer, kr *crypto.KeyRing, s *rfc822.Section) error { |
| 190 |
header, err := s.ParseHeader() |
| 191 |
if err != nil { |
| 192 |
return err |
| 193 |
} |
| 194 |
|
| 195 |
body, err := s.DecodedBody() |
| 196 |
if err != nil { |
| 197 |
return err |
| 198 |
} |
| 199 |
|
| 200 |
// Set the Content-Transfer-Encoding header to base64. |
| 201 |
header.Set("Content-Transfer-Encoding", "base64") |
| 202 |
|
| 203 |
// Encrypt the body. |
| 204 |
enc, err := kr.Encrypt(crypto.NewPlainMessage(body), nil) |
| 205 |
if err != nil { |
| 206 |
return err |
| 207 |
} |
| 208 |
|
| 209 |
// Write the header. |
| 210 |
if _, err := w.Write(header.Raw()); err != nil { |
| 211 |
return err |
| 212 |
} |
| 213 |
|
| 214 |
// Write the base64 body. |
| 215 |
if err := encodeBase64(w, enc.GetBinary()); err != nil { |
| 216 |
return err |
| 217 |
} |
| 218 |
|
| 219 |
return nil |
| 220 |
} |
| 221 |
|
| 222 |
// encryptFull builds a PGP/MIME encrypted message from the given literal. |
| 223 |
func encryptFull(kr *crypto.KeyRing, literal []byte) ([]byte, error) { |
| 224 |
enc, err := kr.Encrypt(crypto.NewPlainMessage(literal), kr) |
| 225 |
if err != nil { |
| 226 |
return nil, err |
| 227 |
} |
| 228 |
|
| 229 |
arm, err := enc.GetArmored() |
| 230 |
if err != nil { |
| 231 |
return nil, err |
| 232 |
} |
| 233 |
|
| 234 |
header, err := rfc822.Parse(literal).ParseHeader() |
| 235 |
if err != nil { |
| 236 |
return nil, err |
| 237 |
} |
| 238 |
|
| 239 |
buf := new(bytes.Buffer) |
| 240 |
boundary := strings.ReplaceAll(uuid.NewString(), "-", "") |
| 241 |
multipartWriter := rfc822.NewMultipartWriter(buf, boundary) |
| 242 |
|
| 243 |
{ |
| 244 |
newHeader := rfc822.NewEmptyHeader() |
| 245 |
|
| 246 |
if value, ok := header.GetChecked("Message-Id"); ok { |
| 247 |
newHeader.Set("Message-Id", value) |
| 248 |
} |
| 249 |
|
| 250 |
contentType := mime.FormatMediaType("multipart/encrypted", map[string]string{ |
| 251 |
"boundary": boundary, |
| 252 |
"protocol": "application/pgp-encrypted", |
| 253 |
}) |
| 254 |
newHeader.Set("Mime-version", "1.0") |
| 255 |
newHeader.Set("Content-Type", contentType) |
| 256 |
|
| 257 |
if value, ok := header.GetChecked("From"); ok { |
| 258 |
newHeader.Set("From", value) |
| 259 |
} |
| 260 |
|
| 261 |
if value, ok := header.GetChecked("To"); ok { |
| 262 |
newHeader.Set("To", value) |
| 263 |
} |
| 264 |
|
| 265 |
if value, ok := header.GetChecked("Subject"); ok { |
| 266 |
newHeader.Set("Subject", value) |
| 267 |
} |
| 268 |
|
| 269 |
if value, ok := header.GetChecked("Date"); ok { |
| 270 |
newHeader.Set("Date", value) |
| 271 |
} |
| 272 |
|
| 273 |
if value, ok := header.GetChecked("Received"); ok { |
| 274 |
newHeader.Set("Received", value) |
| 275 |
} |
| 276 |
|
| 277 |
buf.Write(newHeader.Raw()) |
| 278 |
} |
| 279 |
|
| 280 |
// Write PGP control data |
| 281 |
{ |
| 282 |
pgpControlHeader := rfc822.NewEmptyHeader() |
| 283 |
pgpControlHeader.Set("Content-Description", "PGP/MIME version identification") |
| 284 |
pgpControlHeader.Set("Content-Type", "application/pgp-encrypted") |
| 285 |
if err := multipartWriter.AddPart(func(writer io.Writer) error { |
| 286 |
if _, err := writer.Write(pgpControlHeader.Raw()); err != nil { |
| 287 |
return err |
| 288 |
} |
| 289 |
|
| 290 |
_, err := writer.Write([]byte("Version: 1")) |
| 291 |
|
| 292 |
return err |
| 293 |
}); err != nil { |
| 294 |
return nil, err |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
// write PGP attachment |
| 299 |
{ |
| 300 |
pgpAttachmentHeader := rfc822.NewEmptyHeader() |
| 301 |
contentType := mime.FormatMediaType("application/octet-stream", map[string]string{ |
| 302 |
"name": "encrypted.asc", |
| 303 |
}) |
| 304 |
pgpAttachmentHeader.Set("Content-Description", "OpenPGP encrypted message") |
| 305 |
pgpAttachmentHeader.Set("Content-Disposition", "inline; filename=encrypted.asc") |
| 306 |
pgpAttachmentHeader.Set("Content-Type", contentType) |
| 307 |
|
| 308 |
if err := multipartWriter.AddPart(func(writer io.Writer) error { |
| 309 |
if _, err := writer.Write(pgpAttachmentHeader.Raw()); err != nil { |
| 310 |
return err |
| 311 |
} |
| 312 |
|
| 313 |
_, err := writer.Write([]byte(arm)) |
| 314 |
return err |
| 315 |
}); err != nil { |
| 316 |
return nil, err |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// finish messsage |
| 321 |
if err := multipartWriter.Done(); err != nil { |
| 322 |
return nil, err |
| 323 |
} |
| 324 |
|
| 325 |
return buf.Bytes(), nil |
| 326 |
} |
| 327 |
|
| 328 |
func encodeBase64(writer io.Writer, b []byte) error { |
| 329 |
encoder := base64.NewEncoder(base64.StdEncoding, writer) |
| 330 |
defer encoder.Close() |
| 331 |
|
| 332 |
if _, err := encoder.Write(b); err != nil { |
| 333 |
return err |
| 334 |
} |
| 335 |
|
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
func getCharsetDecoder(r io.Reader, charset string) (io.Reader, error) { |
| 340 |
if CharsetReader != nil { |
| 341 |
if enc, err := CharsetReader(charset, r); err == nil { |
| 342 |
return enc, nil |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if enc, err := ianaindex.MIME.Encoding(strings.ToLower(charset)); err == nil { |
| 347 |
return enc.NewDecoder().Reader(r), nil |
| 348 |
} |
| 349 |
|
| 350 |
if enc, err := ianaindex.MIME.Encoding("cs" + strings.ToLower(charset)); err == nil { |
| 351 |
return enc.NewDecoder().Reader(r), nil |
| 352 |
} |
| 353 |
|
| 354 |
if enc, err := htmlindex.Get(strings.ToLower(charset)); err == nil { |
| 355 |
return enc.NewDecoder().Reader(r), nil |
| 356 |
} |
| 357 |
|
| 358 |
return nil, fmt.Errorf("unknown charset: %s", charset) |
| 359 |
} |
| 360 |
|
| 361 |
func replace[Key comparable, Value any](m map[Key]Value, key Key, value Value) map[Key]Value { |
| 362 |
m[key] = value |
| 363 |
return m |
| 364 |
} |