Debian Codegraph @

message_send_types.go

raw
1 package proton
2
3 import (
4 "encoding/base64"
5 "fmt"
6
7 "github.com/ProtonMail/gluon/rfc822"
8 "github.com/ProtonMail/gopenpgp/v2/crypto"
9 )
10
11 type EncryptionScheme int
12
13 const (
14 InternalScheme EncryptionScheme = 1 << iota
15 EncryptedOutsideScheme
16 ClearScheme
17 PGPInlineScheme
18 PGPMIMEScheme
19 ClearMIMEScheme
20 )
21
22 type SignatureType int
23
24 const (
25 NoSignature SignatureType = iota
26 DetachedSignature
27 AttachedSignature
28 )
29
30 type MessageRecipient struct {
31 Type EncryptionScheme
32 Signature SignatureType
33
34 BodyKeyPacket string `json:",omitempty"`
35 AttachmentKeyPackets map[string]string `json:",omitempty"`
36 }
37
38 type MessagePackage struct {
39 Addresses map[string]*MessageRecipient
40 MIMEType rfc822.MIMEType
41 Type EncryptionScheme
42 Body string
43
44 BodyKey *SessionKey `json:",omitempty"`
45 AttachmentKeys map[string]*SessionKey `json:",omitempty"`
46 }
47
48 func newMessagePackage(mimeType rfc822.MIMEType, encBodyData []byte) *MessagePackage {
49 return &MessagePackage{
50 Addresses: make(map[string]*MessageRecipient),
51 MIMEType: mimeType,
52 Body: base64.StdEncoding.EncodeToString(encBodyData),
53
54 AttachmentKeys: make(map[string]*SessionKey),
55 }
56 }
57
58 type SessionKey struct {
59 Key string
60 Algorithm string
61 }
62
63 func newSessionKey(key *crypto.SessionKey) *SessionKey {
64 return &SessionKey{
65 Key: key.GetBase64Key(),
66 Algorithm: key.Algo,
67 }
68 }
69
70 type SendPreferences struct {
71 // Encrypt indicates whether the email should be encrypted or not.
72 // If it's encrypted, we need to know which public key to use.
73 Encrypt bool
74
75 // PubKey contains an OpenPGP key that can be used for encryption.
76 PubKey *crypto.KeyRing
77
78 // SignatureType indicates how the email should be signed.
79 SignatureType SignatureType
80
81 // EncryptionScheme indicates if we should encrypt body and attachments separately and
82 // what MIME format to give the final encrypted email. The two standard PGP
83 // schemes are PGP/MIME and PGP/Inline. However we use a custom scheme for
84 // internal emails (including the so-called encrypted-to-outside emails,
85 // which even though meant for external users, they don't really get out of
86 // our platform). If the email is sent unencrypted, no PGP scheme is needed.
87 EncryptionScheme EncryptionScheme
88
89 // MIMEType is the MIME type to use for formatting the body of the email
90 // (before encryption/after decryption). The standard possibilities are the
91 // enriched HTML format, text/html, and plain text, text/plain. But it's
92 // also possible to have a multipart/mixed format, which is typically used
93 // for PGP/MIME encrypted emails, where attachments go into the body too.
94 // Because of this, this option is sometimes called MIME format.
95 MIMEType rfc822.MIMEType
96 }
97
98 type SendDraftReq struct {
99 Packages []*MessagePackage
100 }
101
102 func (req *SendDraftReq) AddMIMEPackage(
103 kr *crypto.KeyRing,
104 mimeBody string,
105 prefs map[string]SendPreferences,
106 ) error {
107 for _, prefs := range prefs {
108 if prefs.MIMEType != rfc822.MultipartMixed {
109 return fmt.Errorf("invalid MIME type for MIME package: %s", prefs.MIMEType)
110 }
111 }
112
113 pkg, err := newMIMEPackage(kr, mimeBody, prefs)
114 if err != nil {
115 return err
116 }
117
118 req.Packages = append(req.Packages, pkg)
119
120 return nil
121 }
122
123 func (req *SendDraftReq) AddTextPackage(
124 kr *crypto.KeyRing,
125 body string,
126 mimeType rfc822.MIMEType,
127 prefs map[string]SendPreferences,
128 attKeys map[string]*crypto.SessionKey,
129 ) error {
130 pkg, err := newTextPackage(kr, body, mimeType, prefs, attKeys)
131 if err != nil {
132 return err
133 }
134
135 req.Packages = append(req.Packages, pkg)
136
137 return nil
138 }
139
140 func newMIMEPackage(
141 kr *crypto.KeyRing,
142 mimeBody string,
143 prefs map[string]SendPreferences,
144 ) (*MessagePackage, error) {
145 decBodyKey, encBodyData, err := encSplit(kr, mimeBody)
146 if err != nil {
147 return nil, fmt.Errorf("failed to encrypt MIME body: %w", err)
148 }
149
150 pkg := newMessagePackage(rfc822.MultipartMixed, encBodyData)
151
152 for addr, prefs := range prefs {
153 if prefs.MIMEType != rfc822.MultipartMixed {
154 return nil, fmt.Errorf("invalid MIME type for MIME package: %s", prefs.MIMEType)
155 }
156
157 if prefs.SignatureType != DetachedSignature {
158 return nil, fmt.Errorf("invalid signature type for MIME package: %d", prefs.SignatureType)
159 }
160
161 recipient := &MessageRecipient{
162 Type: prefs.EncryptionScheme,
163 Signature: prefs.SignatureType,
164 }
165
166 switch prefs.EncryptionScheme {
167 case PGPMIMEScheme:
168 if prefs.PubKey == nil {
169 return nil, fmt.Errorf("missing public key for %s", addr)
170 }
171
172 encBodyKey, err := prefs.PubKey.EncryptSessionKey(decBodyKey)
173 if err != nil {
174 return nil, fmt.Errorf("failed to encrypt session key: %w", err)
175 }
176
177 recipient.BodyKeyPacket = base64.StdEncoding.EncodeToString(encBodyKey)
178
179 case ClearMIMEScheme:
180 pkg.BodyKey = &SessionKey{
181 Key: decBodyKey.GetBase64Key(),
182 Algorithm: decBodyKey.Algo,
183 }
184
185 default:
186 return nil, fmt.Errorf("invalid encryption scheme for MIME package: %d", prefs.EncryptionScheme)
187 }
188
189 pkg.Addresses[addr] = recipient
190 pkg.Type |= prefs.EncryptionScheme
191 }
192
193 return pkg, nil
194 }
195
196 func newTextPackage(
197 kr *crypto.KeyRing,
198 body string,
199 mimeType rfc822.MIMEType,
200 prefs map[string]SendPreferences,
201 attKeys map[string]*crypto.SessionKey,
202 ) (*MessagePackage, error) {
203 if mimeType != rfc822.TextPlain && mimeType != rfc822.TextHTML {
204 return nil, fmt.Errorf("invalid MIME type for package: %s", mimeType)
205 }
206
207 decBodyKey, encBodyData, err := encSplit(kr, body)
208 if err != nil {
209 return nil, fmt.Errorf("failed to encrypt message body: %w", err)
210 }
211
212 pkg := newMessagePackage(mimeType, encBodyData)
213
214 for addr, prefs := range prefs {
215 if prefs.MIMEType != mimeType {
216 return nil, fmt.Errorf("invalid MIME type for package: %s", prefs.MIMEType)
217 }
218
219 if prefs.SignatureType == DetachedSignature && !prefs.Encrypt {
220 if prefs.EncryptionScheme == PGPInlineScheme {
221 return nil, fmt.Errorf("invalid encryption scheme for %s: %d", addr, prefs.EncryptionScheme)
222 }
223
224 if prefs.EncryptionScheme == ClearScheme && mimeType != rfc822.TextPlain {
225 return nil, fmt.Errorf("invalid MIME type for clear package: %s", mimeType)
226 }
227 }
228
229 if prefs.EncryptionScheme == InternalScheme && !prefs.Encrypt {
230 return nil, fmt.Errorf("internal packages must be encrypted")
231 }
232
233 if prefs.EncryptionScheme == PGPInlineScheme && mimeType != rfc822.TextPlain {
234 return nil, fmt.Errorf("invalid MIME type for PGP inline package: %s", mimeType)
235 }
236
237 switch prefs.EncryptionScheme {
238 case ClearScheme:
239 pkg.BodyKey = newSessionKey(decBodyKey)
240
241 for attID, attKey := range attKeys {
242 pkg.AttachmentKeys[attID] = newSessionKey(attKey)
243 }
244
245 case InternalScheme, PGPInlineScheme:
246 // ...
247
248 default:
249 return nil, fmt.Errorf("invalid encryption scheme for package: %d", prefs.EncryptionScheme)
250 }
251
252 recipient := &MessageRecipient{
253 Type: prefs.EncryptionScheme,
254 Signature: prefs.SignatureType,
255 AttachmentKeyPackets: make(map[string]string),
256 }
257
258 if prefs.Encrypt {
259 if prefs.PubKey == nil {
260 return nil, fmt.Errorf("missing public key for %s", addr)
261 }
262
263 if prefs.SignatureType != DetachedSignature {
264 return nil, fmt.Errorf("invalid signature type for package: %d", prefs.SignatureType)
265 }
266
267 encBodyKey, err := prefs.PubKey.EncryptSessionKey(decBodyKey)
268 if err != nil {
269 return nil, fmt.Errorf("failed to encrypt session key: %w", err)
270 }
271
272 recipient.BodyKeyPacket = base64.StdEncoding.EncodeToString(encBodyKey)
273
274 for attID, attKey := range attKeys {
275 encAttKey, err := prefs.PubKey.EncryptSessionKey(attKey)
276 if err != nil {
277 return nil, fmt.Errorf("failed to encrypt attachment key: %w", err)
278 }
279
280 recipient.AttachmentKeyPackets[attID] = base64.StdEncoding.EncodeToString(encAttKey)
281 }
282 }
283
284 pkg.Addresses[addr] = recipient
285 pkg.Type |= prefs.EncryptionScheme
286 }
287
288 return pkg, nil
289 }
290
291 func encSplit(kr *crypto.KeyRing, body string) (*crypto.SessionKey, []byte, error) {
292 encBody, err := kr.Encrypt(crypto.NewPlainMessageFromString(body), kr)
293 if err != nil {
294 return nil, nil, fmt.Errorf("failed to encrypt MIME body: %w", err)
295 }
296
297 splitEncBody, err := encBody.SplitMessage()
298 if err != nil {
299 return nil, nil, fmt.Errorf("failed to split message: %w", err)
300 }
301
302 decBodyKey, err := kr.DecryptSessionKey(splitEncBody.GetBinaryKeyPacket())
303 if err != nil {
304 return nil, nil, fmt.Errorf("failed to decrypt session key: %w", err)
305 }
306
307 return decBodyKey, splitEncBody.GetBinaryDataPacket(), nil
308 }