forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_build.go
318 lines (243 loc) · 7.03 KB
/
message_build.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
package proton
import (
"bufio"
"bytes"
"encoding/base64"
"io"
"mime"
"net/mail"
"strings"
"time"
"unicode/utf8"
"github.com/ProtonMail/gluon/rfc822"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/emersion/go-message"
"github.com/emersion/go-message/textproto"
"github.com/google/uuid"
)
func BuildRFC822(kr *crypto.KeyRing, msg Message, attData map[string][]byte) ([]byte, error) {
if msg.MIMEType == rfc822.MultipartMixed {
return buildPGPRFC822(kr, msg)
}
header, err := getMixedMessageHeader(msg)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
w, err := message.CreateWriter(buf, header)
if err != nil {
return nil, err
}
var (
inlineAtts []Attachment
inlineData [][]byte
attachAtts []Attachment
attachData [][]byte
)
for _, att := range msg.Attachments {
if att.Disposition == InlineDisposition {
inlineAtts = append(inlineAtts, att)
inlineData = append(inlineData, attData[att.ID])
} else {
attachAtts = append(attachAtts, att)
attachData = append(attachData, attData[att.ID])
}
}
if len(inlineAtts) > 0 {
if err := writeRelatedParts(w, kr, msg, inlineAtts, inlineData); err != nil {
return nil, err
}
} else if err := writeTextPart(w, kr, msg); err != nil {
return nil, err
}
for i, att := range attachAtts {
if err := writeAttachmentPart(w, kr, att, attachData[i]); err != nil {
return nil, err
}
}
if err := w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func writeTextPart(w *message.Writer, kr *crypto.KeyRing, msg Message) error {
dec, err := msg.Decrypt(kr)
if err != nil {
return err
}
part, err := w.CreatePart(getTextPartHeader(dec, msg.MIMEType))
if err != nil {
return err
}
if _, err := part.Write(dec); err != nil {
return err
}
return part.Close()
}
func writeAttachmentPart(w *message.Writer, kr *crypto.KeyRing, att Attachment, attData []byte) error {
kps, err := base64.StdEncoding.DecodeString(att.KeyPackets)
if err != nil {
return err
}
msg := crypto.NewPGPSplitMessage(kps, attData).GetPGPMessage()
dec, err := kr.Decrypt(msg, nil, crypto.GetUnixTime())
if err != nil {
return err
}
part, err := w.CreatePart(getAttachmentPartHeader(att))
if err != nil {
return err
}
if _, err := part.Write(dec.GetBinary()); err != nil {
return err
}
return part.Close()
}
func writeRelatedParts(w *message.Writer, kr *crypto.KeyRing, msg Message, atts []Attachment, attData [][]byte) error {
var header message.Header
header.SetContentType(string(rfc822.MultipartRelated), nil)
rel, err := w.CreatePart(header)
if err != nil {
return err
}
if err := writeTextPart(rel, kr, msg); err != nil {
return err
}
for i, att := range atts {
if err := writeAttachmentPart(rel, kr, att, attData[i]); err != nil {
return err
}
}
return rel.Close()
}
func buildPGPRFC822(kr *crypto.KeyRing, msg Message) ([]byte, error) {
raw, err := textproto.ReadHeader(bufio.NewReader(strings.NewReader(msg.Header)))
if err != nil {
return nil, err
}
dec, err := msg.Decrypt(kr)
if err != nil {
return nil, err
}
sigs, err := ExtractSignatures(kr, msg.Body)
if err != nil {
return nil, err
}
if len(sigs) > 0 {
return buildMultipartSignedRFC822(message.Header{Header: raw}, dec, sigs[0])
}
return buildMultipartEncryptedRFC822(message.Header{Header: raw}, dec)
}
func buildMultipartSignedRFC822(header message.Header, body []byte, sig Signature) ([]byte, error) {
buf := new(bytes.Buffer)
boundary := uuid.New().String()
header.SetContentType("multipart/signed", map[string]string{
"micalg": sig.Hash,
"protocol": "application/pgp-signature",
"boundary": boundary,
})
if err := textproto.WriteHeader(buf, header.Header); err != nil {
return nil, err
}
w := rfc822.NewMultipartWriter(buf, boundary)
bodyHeader, bodyData := rfc822.Split(body)
if err := w.AddPart(func(w io.Writer) error {
if _, err := w.Write(bodyHeader); err != nil {
return err
}
if _, err := w.Write(bodyData); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
var sigHeader message.Header
sigHeader.SetContentType("application/pgp-signature", map[string]string{"name": "OpenPGP_signature.asc"})
sigHeader.SetContentDisposition("attachment", map[string]string{"filename": "OpenPGP_signature"})
sigHeader.Set("Content-Description", "OpenPGP digital signature")
sigData, err := sig.Data.GetArmored()
if err != nil {
return nil, err
}
if err := w.AddPart(func(w io.Writer) error {
if err := textproto.WriteHeader(w, sigHeader.Header); err != nil {
return err
}
if _, err := w.Write([]byte(sigData)); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
if err := w.Done(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func buildMultipartEncryptedRFC822(header message.Header, body []byte) ([]byte, error) {
buf := new(bytes.Buffer)
bodyHeader, bodyData := rfc822.Split(body)
parsedHeader, err := rfc822.NewHeader(bodyHeader)
if err != nil {
return nil, err
}
parsedHeader.Entries(func(key, val string) {
header.Set(key, val)
})
if err := textproto.WriteHeader(buf, header.Header); err != nil {
return nil, err
}
if _, err := buf.Write(bodyData); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func getMixedMessageHeader(msg Message) (message.Header, error) {
raw, err := textproto.ReadHeader(bufio.NewReader(strings.NewReader(msg.Header)))
if err != nil {
return message.Header{}, err
}
header := message.Header{Header: raw}
header.SetContentType(string(rfc822.MultipartMixed), nil)
if date, err := mail.ParseDate(header.Get("Date")); err != nil || date.Before(time.Unix(0, 0)) {
if msgTime := time.Unix(msg.Time, 0); msgTime.After(time.Unix(0, 0)) {
header.Set("Date", msgTime.In(time.UTC).Format(time.RFC1123Z))
} else {
header.Del("Date")
}
header.Set("X-Original-Date", date.In(time.UTC).Format(time.RFC1123Z))
}
return header, nil
}
func getTextPartHeader(body []byte, mimeType rfc822.MIMEType) message.Header {
var header message.Header
params := make(map[string]string)
if utf8.Valid(body) {
params["charset"] = "utf-8"
}
header.SetContentType(string(mimeType), params)
// Use quoted-printable for all text/... parts
header.Set("Content-Transfer-Encoding", "quoted-printable")
return header
}
func getAttachmentPartHeader(att Attachment) message.Header {
var header message.Header
for _, key := range att.Headers.Order {
for _, val := range att.Headers.Values[key] {
header.Add(key, val)
}
}
// All attachments have a content type.
header.SetContentType(string(att.MIMEType), map[string]string{"name": mime.QEncoding.Encode("utf-8", att.Name)})
// All attachments have a content disposition.
header.SetContentDisposition(string(att.Disposition), map[string]string{"filename": mime.QEncoding.Encode("utf-8", att.Name)})
// Use base64 for all attachments except embedded RFC822 messages.
if att.MIMEType != rfc822.MessageRFC822 {
header.Set("Content-Transfer-Encoding", "base64")
} else {
header.Del("Content-Transfer-Encoding")
}
return header
}