forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_encrypt_test.go
327 lines (255 loc) · 9.55 KB
/
message_encrypt_test.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
package proton_test
import (
"bytes"
"encoding/base64"
"io"
"testing"
"github.com/ProtonMail/gluon/rfc822"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/henrybear327/go-proton-api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncryptMessage_Simple(t *testing.T) {
const message = `From: Nathaniel Borenstein <[email protected]>
To: Ned Freed <[email protected]>
Subject: Sample message (import 2)
MIME-Version: 1.0
Content-type: text/plain
This is explicitly typed plain ASCII text.
`
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0)
require.NoError(t, err)
kr, err := crypto.NewKeyRing(key)
require.NoError(t, err)
encryptedMessage, err := proton.EncryptRFC822(kr, []byte(message))
require.NoError(t, err)
section := rfc822.Parse(encryptedMessage)
// Check root header:
header, err := section.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>")
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>")
assert.Equal(t, header.Get("Subject"), "Sample message (import 2)")
assert.Equal(t, header.Get("MIME-Version"), "1.0")
// Read the body.
body, err := section.DecodedBody()
require.NoError(t, err)
// Unarmor the PGP message.
enc, err := crypto.NewPGPMessageFromArmored(string(body))
require.NoError(t, err)
// Decrypt the PGP message.
dec, err := kr.Decrypt(enc, nil, crypto.GetUnixTime())
require.NoError(t, err)
require.Equal(t, "This is explicitly typed plain ASCII text.\n", dec.GetString())
}
func TestEncryptMessage_MultipleTextParts(t *testing.T) {
const message = `From: Nathaniel Borenstein <[email protected]>
To: Ned Freed <[email protected]>
Subject: Sample message (import 2)
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="simple boundary"
Received: from mail.protonmail.ch by mail.protonmail.ch; Tue, 25 Nov 2016
This is the preamble. It is to be ignored, though it
is a handy place for mail composers to include an
explanatory note to non-MIME compliant readers.
--simple boundary
This is implicitly typed plain ASCII text.
It does NOT end with a linebreak.
--simple boundary
Content-type: text/plain; charset=us-ascii
This is explicitly typed plain ASCII text.
It DOES end with a linebreak.
--simple boundary--
This is the epilogue. It is also to be ignored.
`
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0)
require.NoError(t, err)
kr, err := crypto.NewKeyRing(key)
require.NoError(t, err)
encryptedMessage, err := proton.EncryptRFC822(kr, []byte(message))
require.NoError(t, err)
section := rfc822.Parse(encryptedMessage)
{
// Check root header:
header, err := section.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>")
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>")
assert.Equal(t, header.Get("Subject"), "Sample message (import 2)")
assert.Equal(t, header.Get("MIME-Version"), "1.0")
assert.Equal(t, header.Get("Received"), "from mail.protonmail.ch by mail.protonmail.ch; Tue, 25 Nov 2016")
mediaType, params, err := rfc822.ParseMediaType(header.Get("Content-Type"))
require.NoError(t, err)
assert.Equal(t, "multipart/encrypted", mediaType)
assert.Equal(t, "application/pgp-encrypted", params["protocol"])
assert.NotEmpty(t, params["boundary"])
}
children, err := section.Children()
require.NoError(t, err)
require.Equal(t, 2, len(children))
{
// check first child.
child := children[0]
header, err := child.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("Content-Description"), "PGP/MIME version identification")
assert.Equal(t, header.Get("Content-Type"), "application/pgp-encrypted")
assert.Equal(t, []byte("Version: 1"), child.Body())
}
{
// check second child.
child := children[1]
header, err := child.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("Content-Description"), "OpenPGP encrypted message")
assert.Equal(t, header.Get("Content-Disposition"), "inline; filename=encrypted.asc")
assert.Equal(t, header.Get("Content-type"), "application/octet-stream; name=encrypted.asc")
body := child.Body()
assert.True(t, bytes.HasPrefix(body, []byte("-----BEGIN PGP MESSAGE-----")))
assert.True(t, bytes.HasSuffix(body, []byte("-----END PGP MESSAGE-----")))
}
}
func TestEncryptMessage_Attachment(t *testing.T) {
const message = `From: Nathaniel Borenstein <[email protected]>
To: Ned Freed <[email protected]>
Subject: Sample message (import 2)
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="simple boundary"
--simple boundary
Content-type: text/plain; charset=us-ascii
Hello world
--simple boundary
Content-Type: application/pdf; name="test.pdf"
Content-Disposition: attachment; filename="test.pdf"
Content-Transfer-Encoding: base64
SGVsbG8gQXR0YWNobWVudA==
--simple boundary--
`
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0)
require.NoError(t, err)
kr, err := crypto.NewKeyRing(key)
require.NoError(t, err)
encryptedMessage, err := proton.EncryptRFC822(kr, []byte(message))
require.NoError(t, err)
section := rfc822.Parse(encryptedMessage)
{
// Check root header:
header, err := section.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>")
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>")
assert.Equal(t, header.Get("Subject"), "Sample message (import 2)")
assert.Equal(t, header.Get("MIME-Version"), "1.0")
mediaType, params, err := rfc822.ParseMediaType(header.Get("Content-Type"))
require.NoError(t, err)
assert.Equal(t, "multipart/mixed", mediaType)
assert.NotEmpty(t, params["boundary"])
}
children, err := section.Children()
require.NoError(t, err)
require.Equal(t, 2, len(children))
{
// check first child.
child := children[0]
header, err := child.ParseHeader()
require.NoError(t, err)
header.Entries(func(key, value string) {
// Old header should be deleted.
assert.NotEqual(t, key, "Content-type")
assert.NotEqual(t, value, "text/plain; charset=us-ascii")
})
assert.Equal(t, header.Get("Content-Type"), "text/plain; charset=utf-8")
}
{
// check second child.
child := children[1]
header, err := child.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("Content-Transfer-Encoding"), "base64")
assert.Equal(t, header.Get("Content-Disposition"), `attachment; filename="test.pdf"`)
assert.Equal(t, header.Get("Content-type"), `application/pdf; name="test.pdf"`)
body := child.Body()
// Read the body.
bodyDecoded, err := io.ReadAll(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(body)))
require.NoError(t, err)
// Unarmor the PGP message.
enc := crypto.NewPGPMessage(bodyDecoded)
// Decrypt the PGP message.
dec, err := kr.Decrypt(enc, nil, crypto.GetUnixTime())
require.NoError(t, err)
require.Equal(t, "Hello Attachment", dec.GetString())
}
}
func TestEncryptMessage_Inline(t *testing.T) {
const message = `From: Nathaniel Borenstein <[email protected]>
To: Ned Freed <[email protected]>
Subject: Sample message (import inline)
MIME-Version: 1.0
Content-type: multipart/related; boundary="BOUNDARY"
--BOUNDARY
Content-type: text/plain; charset=us-ascii
Hello world
--BOUNDARY
Content-Type: image/gif; name="email-action-left.gif"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
Content-Disposition: inline; filename="email-action-left.gif"
SGVsbG8gQXR0YWNobWVudA==
--BOUNDARY--
`
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0)
require.NoError(t, err)
kr, err := crypto.NewKeyRing(key)
require.NoError(t, err)
encryptedMessage, err := proton.EncryptRFC822(kr, []byte(message))
require.NoError(t, err)
section := rfc822.Parse(encryptedMessage)
{
// Check root header:
header, err := section.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>")
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>")
assert.Equal(t, header.Get("Subject"), "Sample message (import inline)")
assert.Equal(t, header.Get("MIME-Version"), "1.0")
mediaType, params, err := rfc822.ParseMediaType(header.Get("Content-Type"))
require.NoError(t, err)
assert.Equal(t, "multipart/related", mediaType)
assert.NotEmpty(t, params["boundary"])
}
children, err := section.Children()
require.NoError(t, err)
require.Equal(t, 2, len(children))
{
// check first child.
child := children[0]
header, err := child.ParseHeader()
require.NoError(t, err)
header.Entries(func(key, value string) {
// Old header should be deleted.
assert.NotEqual(t, key, "Content-type")
assert.NotEqual(t, value, "text/plain; charset=us-ascii")
})
assert.Equal(t, header.Get("Content-Type"), "text/plain; charset=utf-8")
}
{
// check second child.
child := children[1]
header, err := child.ParseHeader()
require.NoError(t, err)
assert.Equal(t, header.Get("Content-Transfer-Encoding"), "base64")
assert.Equal(t, header.Get("Content-Disposition"), `inline; filename="email-action-left.gif"`)
assert.Equal(t, header.Get("Content-type"), `image/gif; name="email-action-left.gif"`)
body := child.Body()
// Read the body.
bodyDecoded, err := io.ReadAll(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(body)))
require.NoError(t, err)
// Unarmor the PGP message.
enc := crypto.NewPGPMessage(bodyDecoded)
// Decrypt the PGP message.
dec, err := kr.Decrypt(enc, nil, crypto.GetUnixTime())
require.NoError(t, err)
require.Equal(t, "Hello Attachment", dec.GetString())
}
}