aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/gpg/reader_test.go
blob: 06cf7a3f0a63d1b4c827b7429174abca460e7b58 (plain) (blame)
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
package gpg

import (
	"bytes"
	"io"
	"strings"
	"testing"

	"git.sr.ht/~rjarry/aerc/lib/crypto/gpg/gpgbin"
	"git.sr.ht/~rjarry/aerc/models"
)

func importSecretKey() {
	r := strings.NewReader(testPrivateKeyArmored)
	gpgbin.Import(r)
}

func importPublicKey() {
	r := strings.NewReader(testPublicKeyArmored)
	gpgbin.Import(r)
}

func TestReader_encryptedSignedPGPMIME(t *testing.T) {
	initGPGtest(t)

	var expect = models.MessageDetails{
		IsEncrypted:        true,
		IsSigned:           true,
		SignedBy:           "John Doe (This is a test key) <john.doe@example.org>",
		SignedByKeyId:      3490876580878068068,
		SignatureError:     "",
		DecryptedWith:      "John Doe (This is a test key) <john.doe@example.org>",
		DecryptedWithKeyId: 3490876580878068068,
		Body:               strings.NewReader(testEncryptedBody),
		Micalg:             "pgp-sha512",
	}

	importSecretKey()
	sr := strings.NewReader(testPGPMIMEEncryptedSigned)
	r, err := Read(sr)
	if err != nil {
		t.Fatalf("pgpmail.Read() = %v", err)
	}

	deepEqual(t, r.MessageDetails, &expect)
}

func TestReader_signedPGPMIME(t *testing.T) {
	initGPGtest(t)

	var expect = models.MessageDetails{
		IsEncrypted:        false,
		IsSigned:           true,
		SignedBy:           "John Doe (This is a test key) <john.doe@example.org>",
		SignedByKeyId:      3490876580878068068,
		SignatureError:     "",
		DecryptedWith:      "",
		DecryptedWithKeyId: 0,
		Body:               strings.NewReader(testSignedBody),
		Micalg:             "pgp-sha256",
	}

	importSecretKey()
	importPublicKey()
	sr := strings.NewReader(testPGPMIMESigned)
	r, err := Read(sr)
	if err != nil {
		t.Fatalf("pgpmail.Read() = %v", err)
	}

	deepEqual(t, r.MessageDetails, &expect)
}

func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) {
	initGPGtest(t)

	var expect = models.MessageDetails{
		IsEncrypted:        true,
		IsSigned:           true,
		SignedBy:           "John Doe (This is a test key) <john.doe@example.org>",
		SignedByKeyId:      3490876580878068068,
		SignatureError:     "",
		DecryptedWith:      "John Doe (This is a test key) <john.doe@example.org>",
		DecryptedWithKeyId: 3490876580878068068,
		Body:               strings.NewReader(testSignedBody),
		Micalg:             "pgp-sha256",
	}

	importSecretKey()
	importPublicKey()
	sr := strings.NewReader(testPGPMIMEEncryptedSignedEncapsulated)
	r, err := Read(sr)
	if err != nil {
		t.Fatalf("pgpmail.Read() = %v", err)
	}

	deepEqual(t, r.MessageDetails, &expect)

	var buf bytes.Buffer
	if _, err := io.Copy(&buf, r.MessageDetails.Body); err != nil {
		t.Fatalf("io.Copy() = %v", err)
	}
}
func TestReader_signedPGPMIMEInvalid(t *testing.T) {
	initGPGtest(t)

	var expect = models.MessageDetails{
		IsEncrypted:        false,
		IsSigned:           true,
		SignedBy:           "John Doe (This is a test key) <john.doe@example.org>",
		SignedByKeyId:      3490876580878068068,
		SignatureError:     "gpg: invalid signature",
		DecryptedWith:      "",
		DecryptedWithKeyId: 0,
		Body:               strings.NewReader(testSignedInvalidBody),
		Micalg:             "",
	}

	importSecretKey()
	importPublicKey()
	sr := strings.NewReader(testPGPMIMESignedInvalid)
	r, err := Read(sr)
	if err != nil {
		t.Fatalf("pgpmail.Read() = %v", err)
	}
	deepEqual(t, r.MessageDetails, &expect)
}

func TestReader_plaintext(t *testing.T) {
	initGPGtest(t)

	sr := strings.NewReader(testPlaintext)
	r, err := Read(sr)
	if err != nil {
		t.Fatalf("pgpmail.Read() = %v", err)
	}

	var buf bytes.Buffer
	if _, err := io.Copy(&buf, r.MessageDetails.Body); err != nil {
		t.Fatalf("io.Copy() = %v", err)
	}

	if r.MessageDetails.IsEncrypted {
		t.Errorf("MessageDetails.IsEncrypted != false")
	}
	if r.MessageDetails.IsSigned {
		t.Errorf("MessageDetails.IsSigned != false")
	}

	if s := buf.String(); s != testPlaintext {
		t.Errorf("MessagesDetails.UnverifiedBody = \n%v\n but want \n%v", s, testPlaintext)
	}
}

var testEncryptedBody = toCRLF(`Content-Type: text/plain

This is an encrypted message!
`)

var testSignedBody = toCRLF(`Content-Type: text/plain

This is a signed message!
`)

var testSignedInvalidBody = toCRLF(`Content-Type: text/plain

This is a signed message, but the signature is invalid.
`)

var testPGPMIMEEncryptedSigned = toCRLF(`From: John Doe <john.doe@example.org>
To: John Doe <john.doe@example.org>
Mime-Version: 1.0
Content-Type: multipart/encrypted; boundary=foo;
   protocol="application/pgp-encrypted"

--foo
Content-Type: application/pgp-encrypted

Version: 1

--foo
Content-Type: application/octet-stream

-----BEGIN PGP MESSAGE-----

hQEMAxF0jxulHQ8+AQf/SBK2FIIgMA4OkCvlqty/1GmAumWq6J0T+pRLppXHvYFb
jbXRzz2h3pE/OoouI6vWzBwb8xU/5f8neen+fvdsF1N6PyLjZcHRB91oPvP8TuHA
0vEpiQDbP+0wlQ8BmMnnV06HokWJoKXGmIle0L4QszT/QCbrT80UgKrqXNVHKQtN
DUcytFsUCmolZRj074FEpEetjH6QGEX5hAYNBUJziXmOv7vdd4AFgNbbgC5j5ezz
h8tCAKUqeUiproYaAMrI0lfqh/t8bacJNkljI2LOxYfdJ/2317Npwly0OqpCM3YT
Q4dHuuGM6IuZHtIc9sneIBRhKf8WnWt14hLkHUT80dLA/AHKl0jGYqO34Dxd9JNB
EEwQ4j6rxauOEbKLAuYYaEqCzNYBasBrPmpNb4Fx2syWkCoYzwvzv7nj4I8vIBmm
FGsAQLX4c18qtZI4XaG4FPUvFQ01Y0rjTxAV3u51lrYjCxFuI5ZEtiT0J/Tv2Unw
R6xwtARkEf3W0agegmohEjjkAexKNxGrlulLiPk2j9/dnlAxeGpOuhYuYU2kYbKq
x3TkcVYRs1FkmCX0YHNJ2zVWLfDYd2f3UVkXINe7mODGx2A2BxvK9Ig7NMuNmWZE
ELiLSIvQk9jlgqWUMwSGPQKaHPrac02EjcBHef2zCoFbTg0TXQeDr5SV7yguX8jB
zZnoNs+6+GR1gA6poKzFdiG4NRr0SNgEHazPPkXp3P2KyOINyFJ7SA+HX8iegTqL
CTPYPK7UNRmb5s2u5B4e9NiQB9L85W4p7p7uemCSu9bxjs8rkCJpvx9Kb8jzPW17
wnEUe10A4JNDBhxiMg+Fm5oM2VxQVy+eDVFOOq7pDYVcSmZc36wO+EwAKph9shby
O4sDS4l/8eQTEYUxTavdtQ9O9ZMXvf/L3Rl1uFJXw1lFwPReXwtpA485e031/A==
=P0jf
-----END PGP MESSAGE-----

--foo--
`)

var testPGPMIMEEncryptedSignedEncapsulated = toCRLF(`From: John Doe <john.doe@example.org>
To: John Doe <john.doe@example.org>
Mime-Version: 1.0
Content-Type: multipart/encrypted; boundary=foo;
   protocol="application/pgp-encrypted"

--foo
Content-Type: application/pgp-encrypted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QBfn/sQhRytQtg2wr
U1Thy6RujlrrrdUryo3Mi+xc9Ot1o35JszCjNQGL6BCFsGi9fx5pjWM+lLiJ15aJ
jh02mSd/8j7IaJCGgTuyq6uK45EoVqWd1WRSYl4s5tg1g1jckigYYjJdAKNnU/rZ
iTk5F8GSyv30EXnqvrs=
=Ibxd
-----END PGP MESSAGE-----

--foo--
`)

var testPGPMIMESigned = toCRLF(`From: John Doe <john.doe@example.org>
To: John Doe <john.doe@example.org>
Mime-Version: 1.0
Content-Type: multipart/signed; boundary=bar; micalg=pgp-sha256;
   protocol="application/pgp-signature"

--bar
Content-Type: text/plain

This is a signed message!

--bar
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----

iQEzBAABCAAdFiEEsahmk1QVO3mfIhe/MHIVwT33qWQFAl5FRLgACgkQMHIVwT33
qWSEQQf/YgRlKlQzSyvm6A52lGIRU3F/z9EGjhCryxj+hSdPlk8O7iZFIjnco4Ea
7QIlsOj6D4AlLdhyK6c8IZV7rZoTNE5rc6I5UZjM4Qa0XoyLjao28zR252TtwwWJ
e4+wrTQKcVhCyHO6rkvcCpru4qF5CU+Mi8+sf8CNJJyBgw1Pri35rJWMdoTPTqqz
kcIGN1JySaI8bbVitJQmnm0FtFTiB7zznv94rMBCiPmPUWd9BSpSBJteJoBLZ+K7
Y7ws2Dzp2sBo/RLUM18oXd0N9PLXvFGI3IuF8ey1SPzQH3QbBdJSTmLzRlPjK7A1
HVHFb3vTjd71z9j5IGQQ3Awdw30zMg==
=gOul
-----END PGP SIGNATURE-----

--bar--
`)

var testPGPMIMESignedInvalid = toCRLF(`From: John Doe <john.doe@example.org>
To: John Doe <john.doe@example.org>
Mime-Version: 1.0
Content-Type: multipart/signed; boundary=bar; micalg=pgp-sha256;
   protocol="application/pgp-signature"

--bar
Content-Type: text/plain

This is a signed message, but the signature is invalid.

--bar
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----

iQEzBAABCAAdFiEEsahmk1QVO3mfIhe/MHIVwT33qWQFAl5FRLgACgkQMHIVwT33
qWSEQQf/YgRlKlQzSyvm6A52lGIRU3F/z9EGjhCryxj+hSdPlk8O7iZFIjnco4Ea
7QIlsOj6D4AlLdhyK6c8IZV7rZoTNE5rc6I5UZjM4Qa0XoyLjao28zR252TtwwWJ
e4+wrTQKcVhCyHO6rkvcCpru4qF5CU+Mi8+sf8CNJJyBgw1Pri35rJWMdoTPTqqz
kcIGN1JySaI8bbVitJQmnm0FtFTiB7zznv94rMBCiPmPUWd9BSpSBJteJoBLZ+K7
Y7ws2Dzp2sBo/RLUM18oXd0N9PLXvFGI3IuF8ey1SPzQH3QbBdJSTmLzRlPjK7A1
HVHFb3vTjd71z9j5IGQQ3Awdw30zMg==
=gOul
-----END PGP SIGNATURE-----

--bar--
`)

var testPlaintext = toCRLF(`From: John Doe <john.doe@example.org>
To: John Doe <john.doe@example.org>
Mime-Version: 1.0
Content-Type: text/plain

This is a plaintext message!
`)