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