forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_send_types.go
308 lines (244 loc) · 8.26 KB
/
message_send_types.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
package proton
import (
"encoding/base64"
"fmt"
"github.com/ProtonMail/gluon/rfc822"
"github.com/ProtonMail/gopenpgp/v2/crypto"
)
type EncryptionScheme int
const (
InternalScheme EncryptionScheme = 1 << iota
EncryptedOutsideScheme
ClearScheme
PGPInlineScheme
PGPMIMEScheme
ClearMIMEScheme
)
type SignatureType int
const (
NoSignature SignatureType = iota
DetachedSignature
AttachedSignature
)
type MessageRecipient struct {
Type EncryptionScheme
Signature SignatureType
BodyKeyPacket string `json:",omitempty"`
AttachmentKeyPackets map[string]string `json:",omitempty"`
}
type MessagePackage struct {
Addresses map[string]*MessageRecipient
MIMEType rfc822.MIMEType
Type EncryptionScheme
Body string
BodyKey *SessionKey `json:",omitempty"`
AttachmentKeys map[string]*SessionKey `json:",omitempty"`
}
func newMessagePackage(mimeType rfc822.MIMEType, encBodyData []byte) *MessagePackage {
return &MessagePackage{
Addresses: make(map[string]*MessageRecipient),
MIMEType: mimeType,
Body: base64.StdEncoding.EncodeToString(encBodyData),
AttachmentKeys: make(map[string]*SessionKey),
}
}
type SessionKey struct {
Key string
Algorithm string
}
func newSessionKey(key *crypto.SessionKey) *SessionKey {
return &SessionKey{
Key: key.GetBase64Key(),
Algorithm: key.Algo,
}
}
type SendPreferences struct {
// Encrypt indicates whether the email should be encrypted or not.
// If it's encrypted, we need to know which public key to use.
Encrypt bool
// PubKey contains an OpenPGP key that can be used for encryption.
PubKey *crypto.KeyRing
// SignatureType indicates how the email should be signed.
SignatureType SignatureType
// EncryptionScheme indicates if we should encrypt body and attachments separately and
// what MIME format to give the final encrypted email. The two standard PGP
// schemes are PGP/MIME and PGP/Inline. However we use a custom scheme for
// internal emails (including the so-called encrypted-to-outside emails,
// which even though meant for external users, they don't really get out of
// our platform). If the email is sent unencrypted, no PGP scheme is needed.
EncryptionScheme EncryptionScheme
// MIMEType is the MIME type to use for formatting the body of the email
// (before encryption/after decryption). The standard possibilities are the
// enriched HTML format, text/html, and plain text, text/plain. But it's
// also possible to have a multipart/mixed format, which is typically used
// for PGP/MIME encrypted emails, where attachments go into the body too.
// Because of this, this option is sometimes called MIME format.
MIMEType rfc822.MIMEType
}
type SendDraftReq struct {
Packages []*MessagePackage
}
func (req *SendDraftReq) AddMIMEPackage(
kr *crypto.KeyRing,
mimeBody string,
prefs map[string]SendPreferences,
) error {
for _, prefs := range prefs {
if prefs.MIMEType != rfc822.MultipartMixed {
return fmt.Errorf("invalid MIME type for MIME package: %s", prefs.MIMEType)
}
}
pkg, err := newMIMEPackage(kr, mimeBody, prefs)
if err != nil {
return err
}
req.Packages = append(req.Packages, pkg)
return nil
}
func (req *SendDraftReq) AddTextPackage(
kr *crypto.KeyRing,
body string,
mimeType rfc822.MIMEType,
prefs map[string]SendPreferences,
attKeys map[string]*crypto.SessionKey,
) error {
pkg, err := newTextPackage(kr, body, mimeType, prefs, attKeys)
if err != nil {
return err
}
req.Packages = append(req.Packages, pkg)
return nil
}
func newMIMEPackage(
kr *crypto.KeyRing,
mimeBody string,
prefs map[string]SendPreferences,
) (*MessagePackage, error) {
decBodyKey, encBodyData, err := encSplit(kr, mimeBody)
if err != nil {
return nil, fmt.Errorf("failed to encrypt MIME body: %w", err)
}
pkg := newMessagePackage(rfc822.MultipartMixed, encBodyData)
for addr, prefs := range prefs {
if prefs.MIMEType != rfc822.MultipartMixed {
return nil, fmt.Errorf("invalid MIME type for MIME package: %s", prefs.MIMEType)
}
if prefs.SignatureType != DetachedSignature {
return nil, fmt.Errorf("invalid signature type for MIME package: %d", prefs.SignatureType)
}
recipient := &MessageRecipient{
Type: prefs.EncryptionScheme,
Signature: prefs.SignatureType,
}
switch prefs.EncryptionScheme {
case PGPMIMEScheme:
if prefs.PubKey == nil {
return nil, fmt.Errorf("missing public key for %s", addr)
}
encBodyKey, err := prefs.PubKey.EncryptSessionKey(decBodyKey)
if err != nil {
return nil, fmt.Errorf("failed to encrypt session key: %w", err)
}
recipient.BodyKeyPacket = base64.StdEncoding.EncodeToString(encBodyKey)
case ClearMIMEScheme:
pkg.BodyKey = &SessionKey{
Key: decBodyKey.GetBase64Key(),
Algorithm: decBodyKey.Algo,
}
default:
return nil, fmt.Errorf("invalid encryption scheme for MIME package: %d", prefs.EncryptionScheme)
}
pkg.Addresses[addr] = recipient
pkg.Type |= prefs.EncryptionScheme
}
return pkg, nil
}
func newTextPackage(
kr *crypto.KeyRing,
body string,
mimeType rfc822.MIMEType,
prefs map[string]SendPreferences,
attKeys map[string]*crypto.SessionKey,
) (*MessagePackage, error) {
if mimeType != rfc822.TextPlain && mimeType != rfc822.TextHTML {
return nil, fmt.Errorf("invalid MIME type for package: %s", mimeType)
}
decBodyKey, encBodyData, err := encSplit(kr, body)
if err != nil {
return nil, fmt.Errorf("failed to encrypt message body: %w", err)
}
pkg := newMessagePackage(mimeType, encBodyData)
for addr, prefs := range prefs {
if prefs.MIMEType != mimeType {
return nil, fmt.Errorf("invalid MIME type for package: %s", prefs.MIMEType)
}
if prefs.SignatureType == DetachedSignature && !prefs.Encrypt {
if prefs.EncryptionScheme == PGPInlineScheme {
return nil, fmt.Errorf("invalid encryption scheme for %s: %d", addr, prefs.EncryptionScheme)
}
if prefs.EncryptionScheme == ClearScheme && mimeType != rfc822.TextPlain {
return nil, fmt.Errorf("invalid MIME type for clear package: %s", mimeType)
}
}
if prefs.EncryptionScheme == InternalScheme && !prefs.Encrypt {
return nil, fmt.Errorf("internal packages must be encrypted")
}
if prefs.EncryptionScheme == PGPInlineScheme && mimeType != rfc822.TextPlain {
return nil, fmt.Errorf("invalid MIME type for PGP inline package: %s", mimeType)
}
switch prefs.EncryptionScheme {
case ClearScheme:
pkg.BodyKey = newSessionKey(decBodyKey)
for attID, attKey := range attKeys {
pkg.AttachmentKeys[attID] = newSessionKey(attKey)
}
case InternalScheme, PGPInlineScheme:
// ...
default:
return nil, fmt.Errorf("invalid encryption scheme for package: %d", prefs.EncryptionScheme)
}
recipient := &MessageRecipient{
Type: prefs.EncryptionScheme,
Signature: prefs.SignatureType,
AttachmentKeyPackets: make(map[string]string),
}
if prefs.Encrypt {
if prefs.PubKey == nil {
return nil, fmt.Errorf("missing public key for %s", addr)
}
if prefs.SignatureType != DetachedSignature {
return nil, fmt.Errorf("invalid signature type for package: %d", prefs.SignatureType)
}
encBodyKey, err := prefs.PubKey.EncryptSessionKey(decBodyKey)
if err != nil {
return nil, fmt.Errorf("failed to encrypt session key: %w", err)
}
recipient.BodyKeyPacket = base64.StdEncoding.EncodeToString(encBodyKey)
for attID, attKey := range attKeys {
encAttKey, err := prefs.PubKey.EncryptSessionKey(attKey)
if err != nil {
return nil, fmt.Errorf("failed to encrypt attachment key: %w", err)
}
recipient.AttachmentKeyPackets[attID] = base64.StdEncoding.EncodeToString(encAttKey)
}
}
pkg.Addresses[addr] = recipient
pkg.Type |= prefs.EncryptionScheme
}
return pkg, nil
}
func encSplit(kr *crypto.KeyRing, body string) (*crypto.SessionKey, []byte, error) {
encBody, err := kr.Encrypt(crypto.NewPlainMessageFromString(body), kr)
if err != nil {
return nil, nil, fmt.Errorf("failed to encrypt MIME body: %w", err)
}
splitEncBody, err := encBody.SplitMessage()
if err != nil {
return nil, nil, fmt.Errorf("failed to split message: %w", err)
}
decBodyKey, err := kr.DecryptSessionKey(splitEncBody.GetBinaryKeyPacket())
if err != nil {
return nil, nil, fmt.Errorf("failed to decrypt session key: %w", err)
}
return decBodyKey, splitEncBody.GetBinaryDataPacket(), nil
}