| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"io" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/ProtonMail/gluon/rfc822" |
| 10 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestEncryptMessage_Simple(t *testing.T) { |
| 16 |
const message = `From: Nathaniel Borenstein <[email protected]> |
| 17 |
To: Ned Freed <[email protected]> |
| 18 |
Subject: Sample message (import 2) |
| 19 |
MIME-Version: 1.0 |
| 20 |
Content-type: text/plain |
| 21 |
|
| 22 |
This is explicitly typed plain ASCII text. |
| 23 |
` |
| 24 |
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0) |
| 25 |
require.NoError(t, err) |
| 26 |
|
| 27 |
kr, err := crypto.NewKeyRing(key) |
| 28 |
require.NoError(t, err) |
| 29 |
|
| 30 |
encryptedMessage, err := EncryptRFC822(kr, []byte(message)) |
| 31 |
require.NoError(t, err) |
| 32 |
|
| 33 |
section := rfc822.Parse(encryptedMessage) |
| 34 |
|
| 35 |
// Check root header: |
| 36 |
header, err := section.ParseHeader() |
| 37 |
require.NoError(t, err) |
| 38 |
|
| 39 |
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>") |
| 40 |
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>") |
| 41 |
assert.Equal(t, header.Get("Subject"), "Sample message (import 2)") |
| 42 |
assert.Equal(t, header.Get("MIME-Version"), "1.0") |
| 43 |
|
| 44 |
// Read the body. |
| 45 |
body, err := section.DecodedBody() |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
// Unarmor the PGP message. |
| 49 |
enc, err := crypto.NewPGPMessageFromArmored(string(body)) |
| 50 |
require.NoError(t, err) |
| 51 |
|
| 52 |
// Decrypt the PGP message. |
| 53 |
dec, err := kr.Decrypt(enc, nil, crypto.GetUnixTime()) |
| 54 |
require.NoError(t, err) |
| 55 |
require.Equal(t, "This is explicitly typed plain ASCII text.\n", dec.GetString()) |
| 56 |
} |
| 57 |
|
| 58 |
func TestEncryptMessage_MultipleTextParts(t *testing.T) { |
| 59 |
const message = `From: Nathaniel Borenstein <[email protected]> |
| 60 |
To: Ned Freed <[email protected]> |
| 61 |
Subject: Sample message (import 2) |
| 62 |
MIME-Version: 1.0 |
| 63 |
Content-type: multipart/mixed; boundary="simple boundary" |
| 64 |
Received: from mail.protonmail.ch by mail.protonmail.ch; Tue, 25 Nov 2016 |
| 65 |
|
| 66 |
This is the preamble. It is to be ignored, though it |
| 67 |
is a handy place for mail composers to include an |
| 68 |
explanatory note to non-MIME compliant readers. |
| 69 |
--simple boundary |
| 70 |
|
| 71 |
This is implicitly typed plain ASCII text. |
| 72 |
It does NOT end with a linebreak. |
| 73 |
--simple boundary |
| 74 |
Content-type: text/plain; charset=us-ascii |
| 75 |
|
| 76 |
This is explicitly typed plain ASCII text. |
| 77 |
It DOES end with a linebreak. |
| 78 |
|
| 79 |
--simple boundary-- |
| 80 |
This is the epilogue. It is also to be ignored. |
| 81 |
` |
| 82 |
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0) |
| 83 |
require.NoError(t, err) |
| 84 |
|
| 85 |
kr, err := crypto.NewKeyRing(key) |
| 86 |
require.NoError(t, err) |
| 87 |
|
| 88 |
encryptedMessage, err := EncryptRFC822(kr, []byte(message)) |
| 89 |
require.NoError(t, err) |
| 90 |
|
| 91 |
section := rfc822.Parse(encryptedMessage) |
| 92 |
|
| 93 |
{ |
| 94 |
// Check root header: |
| 95 |
header, err := section.ParseHeader() |
| 96 |
require.NoError(t, err) |
| 97 |
|
| 98 |
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>") |
| 99 |
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>") |
| 100 |
assert.Equal(t, header.Get("Subject"), "Sample message (import 2)") |
| 101 |
assert.Equal(t, header.Get("MIME-Version"), "1.0") |
| 102 |
assert.Equal(t, header.Get("Received"), "from mail.protonmail.ch by mail.protonmail.ch; Tue, 25 Nov 2016") |
| 103 |
|
| 104 |
mediaType, params, err := rfc822.ParseMediaType(header.Get("Content-Type")) |
| 105 |
require.NoError(t, err) |
| 106 |
assert.Equal(t, "multipart/encrypted", mediaType) |
| 107 |
assert.Equal(t, "application/pgp-encrypted", params["protocol"]) |
| 108 |
assert.NotEmpty(t, params["boundary"]) |
| 109 |
} |
| 110 |
|
| 111 |
children, err := section.Children() |
| 112 |
require.NoError(t, err) |
| 113 |
require.Equal(t, 2, len(children)) |
| 114 |
|
| 115 |
{ |
| 116 |
// check first child. |
| 117 |
child := children[0] |
| 118 |
header, err := child.ParseHeader() |
| 119 |
require.NoError(t, err) |
| 120 |
|
| 121 |
assert.Equal(t, header.Get("Content-Description"), "PGP/MIME version identification") |
| 122 |
assert.Equal(t, header.Get("Content-Type"), "application/pgp-encrypted") |
| 123 |
|
| 124 |
assert.Equal(t, []byte("Version: 1"), child.Body()) |
| 125 |
} |
| 126 |
|
| 127 |
{ |
| 128 |
// check second child. |
| 129 |
child := children[1] |
| 130 |
header, err := child.ParseHeader() |
| 131 |
require.NoError(t, err) |
| 132 |
|
| 133 |
assert.Equal(t, header.Get("Content-Description"), "OpenPGP encrypted message") |
| 134 |
assert.Equal(t, header.Get("Content-Disposition"), "inline; filename=encrypted.asc") |
| 135 |
assert.Equal(t, header.Get("Content-type"), "application/octet-stream; name=encrypted.asc") |
| 136 |
|
| 137 |
body := child.Body() |
| 138 |
assert.True(t, bytes.HasPrefix(body, []byte("-----BEGIN PGP MESSAGE-----"))) |
| 139 |
assert.True(t, bytes.HasSuffix(body, []byte("-----END PGP MESSAGE-----"))) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestEncryptMessage_Attachment(t *testing.T) { |
| 144 |
const message = `From: Nathaniel Borenstein <[email protected]> |
| 145 |
To: Ned Freed <[email protected]> |
| 146 |
Subject: Sample message (import 2) |
| 147 |
MIME-Version: 1.0 |
| 148 |
Content-type: multipart/mixed; boundary="simple boundary" |
| 149 |
|
| 150 |
--simple boundary |
| 151 |
Content-type: text/plain; charset=us-ascii |
| 152 |
|
| 153 |
Hello world |
| 154 |
|
| 155 |
--simple boundary |
| 156 |
Content-Type: application/pdf; name="test.pdf" |
| 157 |
Content-Disposition: attachment; filename="test.pdf" |
| 158 |
Content-Transfer-Encoding: base64 |
| 159 |
|
| 160 |
SGVsbG8gQXR0YWNobWVudA== |
| 161 |
|
| 162 |
--simple boundary-- |
| 163 |
` |
| 164 |
key, err := crypto.GenerateKey("foobar", "[email protected]", "x25519", 0) |
| 165 |
require.NoError(t, err) |
| 166 |
|
| 167 |
kr, err := crypto.NewKeyRing(key) |
| 168 |
require.NoError(t, err) |
| 169 |
|
| 170 |
encryptedMessage, err := EncryptRFC822(kr, []byte(message)) |
| 171 |
require.NoError(t, err) |
| 172 |
|
| 173 |
section := rfc822.Parse(encryptedMessage) |
| 174 |
|
| 175 |
{ |
| 176 |
// Check root header: |
| 177 |
header, err := section.ParseHeader() |
| 178 |
require.NoError(t, err) |
| 179 |
|
| 180 |
assert.Equal(t, header.Get("From"), "Nathaniel Borenstein <[email protected]>") |
| 181 |
assert.Equal(t, header.Get("To"), "Ned Freed <[email protected]>") |
| 182 |
assert.Equal(t, header.Get("Subject"), "Sample message (import 2)") |
| 183 |
assert.Equal(t, header.Get("MIME-Version"), "1.0") |
| 184 |
|
| 185 |
mediaType, params, err := rfc822.ParseMediaType(header.Get("Content-Type")) |
| 186 |
require.NoError(t, err) |
| 187 |
assert.Equal(t, "multipart/mixed", mediaType) |
| 188 |
assert.NotEmpty(t, params["boundary"]) |
| 189 |
} |
| 190 |
|
| 191 |
children, err := section.Children() |
| 192 |
require.NoError(t, err) |
| 193 |
require.Equal(t, 2, len(children)) |
| 194 |
|
| 195 |
{ |
| 196 |
// check first child. |
| 197 |
child := children[0] |
| 198 |
header, err := child.ParseHeader() |
| 199 |
require.NoError(t, err) |
| 200 |
|
| 201 |
header.Entries(func(key, value string) { |
| 202 |
// Old header should be deleted. |
| 203 |
assert.NotEqual(t, key, "Content-type") |
| 204 |
assert.NotEqual(t, value, "text/plain; charset=us-ascii") |
| 205 |
}) |
| 206 |
|
| 207 |
assert.Equal(t, header.Get("Content-Type"), "text/plain; charset=utf-8") |
| 208 |
} |
| 209 |
|
| 210 |
{ |
| 211 |
// check second child. |
| 212 |
child := children[1] |
| 213 |
header, err := child.ParseHeader() |
| 214 |
require.NoError(t, err) |
| 215 |
|
| 216 |
assert.Equal(t, header.Get("Content-Transfer-Encoding"), "base64") |
| 217 |
assert.Equal(t, header.Get("Content-Disposition"), `attachment; filename="test.pdf"`) |
| 218 |
assert.Equal(t, header.Get("Content-type"), `application/pdf; name="test.pdf"`) |
| 219 |
|
| 220 |
body := child.Body() |
| 221 |
|
| 222 |
// Read the body. |
| 223 |
bodyDecoded, err := io.ReadAll(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(body))) |
| 224 |
require.NoError(t, err) |
| 225 |
|
| 226 |
// Unarmor the PGP message. |
| 227 |
enc := crypto.NewPGPMessage(bodyDecoded) |
| 228 |
|
| 229 |
// Decrypt the PGP message. |
| 230 |
dec, err := kr.Decrypt(enc, nil, crypto.GetUnixTime()) |
| 231 |
require.NoError(t, err) |
| 232 |
require.Equal(t, "Hello Attachment", dec.GetString()) |
| 233 |
} |
| 234 |
} |