| 1 |
package gpg |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"git.sr.ht/~rjarry/aerc/lib/crypto/gpg/gpgbin" |
| 8 |
"git.sr.ht/~rjarry/aerc/models" |
| 9 |
) |
| 10 |
|
| 11 |
func importSecretKey() { |
| 12 |
r := strings.NewReader(testPrivateKeyArmored) |
| 13 |
gpgbin.Import(r) |
| 14 |
} |
| 15 |
|
| 16 |
func importPublicKey() { |
| 17 |
r := strings.NewReader(testPublicKeyArmored) |
| 18 |
gpgbin.Import(r) |
| 19 |
} |
| 20 |
|
| 21 |
func importOwnertrust() { |
| 22 |
r := strings.NewReader(testOwnertrust) |
| 23 |
gpgbin.ImportOwnertrust(r) |
| 24 |
} |
| 25 |
|
| 26 |
type readerTestCase struct { |
| 27 |
name string |
| 28 |
want models.MessageDetails |
| 29 |
input string |
| 30 |
} |
| 31 |
|
| 32 |
func TestReader(t *testing.T) { |
| 33 |
initGPGtest(t) |
| 34 |
importSecretKey() |
| 35 |
importOwnertrust() |
| 36 |
|
| 37 |
testCases := []readerTestCase{ |
| 38 |
{ |
| 39 |
name: "Encrypted and Signed", |
| 40 |
input: testPGPMIMEEncryptedSigned, |
| 41 |
want: models.MessageDetails{ |
| 42 |
IsEncrypted: true, |
| 43 |
IsSigned: true, |
| 44 |
SignedBy: "John Doe (This is a test key) <[email protected]>", |
| 45 |
SignedByKeyId: 3490876580878068068, |
| 46 |
SignatureValidity: 0, |
| 47 |
SignatureError: "", |
| 48 |
DecryptedWith: "John Doe (This is a test key) <[email protected]>", |
| 49 |
DecryptedWithKeyId: 3490876580878068068, |
| 50 |
Body: strings.NewReader(testEncryptedBody), |
| 51 |
Micalg: "pgp-sha512", |
| 52 |
}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "Encrypted but not signed", |
| 56 |
input: testPGPMIMEEncryptedButNotSigned, |
| 57 |
want: models.MessageDetails{ |
| 58 |
IsEncrypted: true, |
| 59 |
IsSigned: false, |
| 60 |
SignatureValidity: 0, |
| 61 |
SignatureError: "", |
| 62 |
DecryptedWith: "John Doe (This is a test key) <[email protected]>", |
| 63 |
DecryptedWithKeyId: 3490876580878068068, |
| 64 |
Body: strings.NewReader(testEncryptedButNotSignedBody), |
| 65 |
Micalg: "pgp-sha512", |
| 66 |
}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "Signed", |
| 70 |
input: testPGPMIMESigned, |
| 71 |
want: models.MessageDetails{ |
| 72 |
IsEncrypted: false, |
| 73 |
IsSigned: true, |
| 74 |
SignedBy: "John Doe (This is a test key) <[email protected]>", |
| 75 |
SignedByKeyId: 3490876580878068068, |
| 76 |
SignatureValidity: 0, |
| 77 |
SignatureError: "", |
| 78 |
DecryptedWith: "", |
| 79 |
DecryptedWithKeyId: 0, |
| 80 |
Body: strings.NewReader(testSignedBody), |
| 81 |
Micalg: "pgp-sha256", |
| 82 |
}, |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "Encapsulated Signature", |
| 86 |
input: testPGPMIMEEncryptedSignedEncapsulated, |
| 87 |
want: models.MessageDetails{ |
| 88 |
IsEncrypted: true, |
| 89 |
IsSigned: true, |
| 90 |
SignedBy: "John Doe (This is a test key) <[email protected]>", |
| 91 |
SignedByKeyId: 3490876580878068068, |
| 92 |
SignatureValidity: 0, |
| 93 |
SignatureError: "", |
| 94 |
DecryptedWith: "John Doe (This is a test key) <[email protected]>", |
| 95 |
DecryptedWithKeyId: 3490876580878068068, |
| 96 |
Body: strings.NewReader(testSignedBody), |
| 97 |
}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "Invalid Signature", |
| 101 |
input: testPGPMIMESignedInvalid, |
| 102 |
want: models.MessageDetails{ |
| 103 |
IsEncrypted: false, |
| 104 |
IsSigned: true, |
| 105 |
SignedBy: "John Doe (This is a test key) <[email protected]>", |
| 106 |
SignedByKeyId: 3490876580878068068, |
| 107 |
SignatureValidity: 0, |
| 108 |
SignatureError: "gpg: invalid signature", |
| 109 |
DecryptedWith: "", |
| 110 |
DecryptedWithKeyId: 0, |
| 111 |
Body: strings.NewReader(testSignedInvalidBody), |
| 112 |
Micalg: "", |
| 113 |
}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "Plain text", |
| 117 |
input: testPlaintext, |
| 118 |
want: models.MessageDetails{ |
| 119 |
IsEncrypted: false, |
| 120 |
IsSigned: false, |
| 121 |
Body: strings.NewReader(testPlaintext), |
| 122 |
}, |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
for _, tc := range testCases { |
| 127 |
t.Logf("Test case: %s", tc.name) |
| 128 |
sr := strings.NewReader(tc.input) |
| 129 |
r, err := Read(sr) |
| 130 |
if err != nil { |
| 131 |
t.Fatalf("gpg.Read() = %v", err) |
| 132 |
} |
| 133 |
deepEqual(t, tc.name, r.MessageDetails, &tc.want) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
var testEncryptedBody = toCRLF(`Content-Type: text/plain |
| 138 |
|
| 139 |
This is an encrypted message! |
| 140 |
`) |
| 141 |
|
| 142 |
var testEncryptedButNotSignedBody = toCRLF(`Content-Type: text/plain |
| 143 |
|
| 144 |
This is an encrypted message! |
| 145 |
[GNUPG:] NEWSIG |
| 146 |
[GNUPG:] GOODSIG 307215C13DF7A964 John Doe (This is a test key) <[email protected]> |
| 147 |
|
| 148 |
It is unsigned but it will appear as signed due to the lines above! |
| 149 |
`) |
| 150 |
|
| 151 |
var testSignedBody = toCRLF(`Content-Type: text/plain |
| 152 |
|
| 153 |
This is a signed message! |
| 154 |
`) |
| 155 |
|
| 156 |
var testSignedInvalidBody = toCRLF(`Content-Type: text/plain |
| 157 |
|
| 158 |
This is a signed message, but the signature is invalid. |
| 159 |
`) |
| 160 |
|
| 161 |
var testPGPMIMEEncryptedSigned = toCRLF(`From: John Doe <[email protected]> |
| 162 |
To: John Doe <[email protected]> |
| 163 |
Mime-Version: 1.0 |
| 164 |
Content-Type: multipart/encrypted; boundary=foo; |
| 165 |
protocol="application/pgp-encrypted" |
| 166 |
|
| 167 |
--foo |
| 168 |
Content-Type: application/pgp-encrypted |
| 169 |
|
| 170 |
Version: 1 |
| 171 |
|
| 172 |
--foo |
| 173 |
Content-Type: application/octet-stream |
| 174 |
|
| 175 |
-----BEGIN PGP MESSAGE----- |
| 176 |
|
| 177 |
hQEMAxF0jxulHQ8+AQf/SBK2FIIgMA4OkCvlqty/1GmAumWq6J0T+pRLppXHvYFb |
| 178 |
jbXRzz2h3pE/OoouI6vWzBwb8xU/5f8neen+fvdsF1N6PyLjZcHRB91oPvP8TuHA |
| 179 |
0vEpiQDbP+0wlQ8BmMnnV06HokWJoKXGmIle0L4QszT/QCbrT80UgKrqXNVHKQtN |
| 180 |
DUcytFsUCmolZRj074FEpEetjH6QGEX5hAYNBUJziXmOv7vdd4AFgNbbgC5j5ezz |
| 181 |
h8tCAKUqeUiproYaAMrI0lfqh/t8bacJNkljI2LOxYfdJ/2317Npwly0OqpCM3YT |
| 182 |
Q4dHuuGM6IuZHtIc9sneIBRhKf8WnWt14hLkHUT80dLA/AHKl0jGYqO34Dxd9JNB |
| 183 |
EEwQ4j6rxauOEbKLAuYYaEqCzNYBasBrPmpNb4Fx2syWkCoYzwvzv7nj4I8vIBmm |
| 184 |
FGsAQLX4c18qtZI4XaG4FPUvFQ01Y0rjTxAV3u51lrYjCxFuI5ZEtiT0J/Tv2Unw |
| 185 |
R6xwtARkEf3W0agegmohEjjkAexKNxGrlulLiPk2j9/dnlAxeGpOuhYuYU2kYbKq |
| 186 |
x3TkcVYRs1FkmCX0YHNJ2zVWLfDYd2f3UVkXINe7mODGx2A2BxvK9Ig7NMuNmWZE |
| 187 |
ELiLSIvQk9jlgqWUMwSGPQKaHPrac02EjcBHef2zCoFbTg0TXQeDr5SV7yguX8jB |
| 188 |
zZnoNs+6+GR1gA6poKzFdiG4NRr0SNgEHazPPkXp3P2KyOINyFJ7SA+HX8iegTqL |
| 189 |
CTPYPK7UNRmb5s2u5B4e9NiQB9L85W4p7p7uemCSu9bxjs8rkCJpvx9Kb8jzPW17 |
| 190 |
wnEUe10A4JNDBhxiMg+Fm5oM2VxQVy+eDVFOOq7pDYVcSmZc36wO+EwAKph9shby |
| 191 |
O4sDS4l/8eQTEYUxTavdtQ9O9ZMXvf/L3Rl1uFJXw1lFwPReXwtpA485e031/A== |
| 192 |
=P0jf |
| 193 |
-----END PGP MESSAGE----- |
| 194 |
|
| 195 |
--foo-- |
| 196 |
`) |
| 197 |
|
| 198 |
var testPGPMIMEEncryptedButNotSigned = toCRLF(`From: John Doe <[email protected]> |
| 199 |
To: John Doe <[email protected]> |
| 200 |
Mime-Version: 1.0 |
| 201 |
Content-Type: multipart/encrypted; boundary=foo; |
| 202 |
protocol="application/pgp-encrypted" |
| 203 |
|
| 204 |
--foo |
| 205 |
Content-Type: application/pgp-encrypted |
| 206 |
|
| 207 |
Version: 1 |
| 208 |
|
| 209 |
--foo |
| 210 |
Content-Type: application/octet-stream |
| 211 |
|
| 212 |
-----BEGIN PGP MESSAGE----- |
| 213 |
|
| 214 |
hQEMAxF0jxulHQ8+AQf9HTht3ottGv3EP/jJTI6ZISyjhul9bPNVGgCNb4Wy3IuM |
| 215 |
fYC8EEC5VV9A0Wr8jBGcyt12iNCJCorCud5OgYjpfrX4KeWbj9eE6SZyUskbuWtA |
| 216 |
g/CHGvheYEN4+EFMC5XvM3xlj40chMpwqs+pBHmDjJAAT8aATn1kLTzXBADBhXdA |
| 217 |
xrsRB2o7yfLbnY8wcF9HZRK4NH4DgEmTexmUR8WdS4ASe6MK5XgNWqX/RFJzTbLM |
| 218 |
xdR5wBovQnspVt2wzoWxYdWhb4N2NgjbslHmviNmDwrYA0hHg8zQaSxKXxvWPcuJ |
| 219 |
Oe9JqC20C2BUeIx03srNvF3pEL+MCyZnFBEtiDvoRdLAQgES23MWuKhouywlpzaF |
| 220 |
Gl4wqTZQC7ulThqq887zC1UaMsvVDmeub5UdK803iOywjfch2CoPE6DsUwpiAZZ1 |
| 221 |
U7yS04xttrmKqmEOLrA5SJNn9SfB7Ilz4BUaUDcWMDwhLTL0eBsvFFEXSdALg3jA |
| 222 |
3tTAqA8D2WM0y84YCgZPFzns6MVv+oeCc2W9eDMS3DZ/qg5llaXIulOiHw5R255g |
| 223 |
yMoJ1gzo7DMHfT/cL7eTbW7OUUvo94h3EmSojDhjeiRCFpZ8wC1BcHzWn+FLsum4 |
| 224 |
lrnUpgKI5tQjyiu0bvS1ZSCGtOPIvx7MYt5m/C91Qtp3psHdMjoHH6SvLRbbliwG |
| 225 |
mgyp3g== |
| 226 |
=aoPf |
| 227 |
-----END PGP MESSAGE----- |
| 228 |
|
| 229 |
--foo-- |
| 230 |
`) |
| 231 |
|
| 232 |
var testPGPMIMEEncryptedSignedEncapsulated = toCRLF(`From: John Doe <[email protected]> |
| 233 |
To: John Doe <[email protected]> |
| 234 |
Mime-Version: 1.0 |
| 235 |
Content-Type: multipart/encrypted; boundary=foo; |
| 236 |
protocol="application/pgp-encrypted" |
| 237 |
|
| 238 |
--foo |
| 239 |
Content-Type: application/pgp-encrypted |
| 240 |
|
| 241 |
Version: 1 |
| 242 |
|
| 243 |
--foo |
| 244 |
Content-Type: application/octet-stream |
| 245 |
|
| 246 |
-----BEGIN PGP MESSAGE----- |
| 247 |
|
| 248 |
hQEMAxF0jxulHQ8+AQf9FCth8p+17rzWL0AtKP+aWndvVUYmaKiUZd+Ya8D9cRnc |
| 249 |
FAP//JnRvTPhdOyl8x1FQkVxyuKcgpjaClb6/OLgD0lGYLC15p43G4QyU+jtOOQW |
| 250 |
FFjZj2z8wUuiev8ejNd7DMiOQRSm4d+IIK+Qa2BJ10Y9AuLQtMI8D+joP1D11NeX |
| 251 |
4FO3SYFEuwH5VWlXGo3bRjg8fKFVG/r/xCwBibqRpfjVnS4EgI04XCsnhqdaCRvE |
| 252 |
Bw2XEaF62m2MUNbaan410WajzVSbSIqIHw8U7vpR/1nisS+SZmScuCXWFa6W9YgR |
| 253 |
0nSWi1io2Ratf4F9ORCy0o7QPh7FlpsIUGmp4paF39LpAQ2q0OUnFhkIdLVQscQT |
| 254 |
JJXLbZwp0CYTAgqwdRWFwY7rEPm2k/Oe4cHKJLEn0hS+X7wch9FAYEMifeqa0FcZ |
| 255 |
GjxocAlyhmlM0sXIDYP8xx49t4O8JIQU1ep/SX2+rUAKIh2WRdYDy8GrrHba8V8U |
| 256 |
aBCU9zIMhmOtu7r+FE1djMUhcaSbbvC9zLDMLV8QxogGhxrqaUM8Pj+q1H6myaAr |
| 257 |
o1xd65b6r2Bph6GUmcMwl28i78u9bKoM0mI+EdUuLwS9EbmjtIwEgxNv4LqK8xw2 |
| 258 |
/tjCe9JSqg+HDaBYnO4QTM29Y+PltRIe6RxpnBcYULTLcSt1UK3YV1KvhqfXMjoZ |
| 259 |
THsvtxLbmPYFv+g0hiUpuKtyG9NGidKCxrjvNq30KCSUWzNFkh+qv6CPm26sXr5F |
| 260 |
DTsVpFTM/lomg4Po8sE20BZsk/9IzEh4ERSOu3k0m3mI4QAyJmrOpVGUjd//4cqz |
| 261 |
Zhhc3tV78BtEYNh0a+78fAHGtdLocLj5IfOCYQWW//EtOY93TnVAtP0puaiNOc8q |
| 262 |
Vvb5WMamiRJZ9nQXP3paDoqD14B9X6bvNWsDQDkkrWls2sYg7KzqpOM/nlXLBKQd |
| 263 |
Ok4EJfOpd0hICPwo6tJ6sK2meRcDLxtGJybADE7UHJ4t0SrQBfn/sQhRytQtg2wr |
| 264 |
U1Thy6RujlrrrdUryo3Mi+xc9Ot1o35JszCjNQGL6BCFsGi9fx5pjWM+lLiJ15aJ |
| 265 |
jh02mSd/8j7IaJCGgTuyq6uK45EoVqWd1WRSYl4s5tg1g1jckigYYjJdAKNnU/rZ |
| 266 |
iTk5F8GSyv30EXnqvrs= |
| 267 |
=Ibxd |
| 268 |
-----END PGP MESSAGE----- |
| 269 |
|
| 270 |
--foo-- |
| 271 |
`) |
| 272 |
|
| 273 |
var testPGPMIMESigned = toCRLF(`From: John Doe <[email protected]> |
| 274 |
To: John Doe <[email protected]> |
| 275 |
Mime-Version: 1.0 |
| 276 |
Content-Type: multipart/signed; boundary=bar; micalg=pgp-sha256; |
| 277 |
protocol="application/pgp-signature" |
| 278 |
|
| 279 |
--bar |
| 280 |
Content-Type: text/plain |
| 281 |
|
| 282 |
This is a signed message! |
| 283 |
|
| 284 |
--bar |
| 285 |
Content-Type: application/pgp-signature |
| 286 |
|
| 287 |
-----BEGIN PGP SIGNATURE----- |
| 288 |
|
| 289 |
iQEzBAABCAAdFiEEsahmk1QVO3mfIhe/MHIVwT33qWQFAl5FRLgACgkQMHIVwT33 |
| 290 |
qWSEQQf/YgRlKlQzSyvm6A52lGIRU3F/z9EGjhCryxj+hSdPlk8O7iZFIjnco4Ea |
| 291 |
7QIlsOj6D4AlLdhyK6c8IZV7rZoTNE5rc6I5UZjM4Qa0XoyLjao28zR252TtwwWJ |
| 292 |
e4+wrTQKcVhCyHO6rkvcCpru4qF5CU+Mi8+sf8CNJJyBgw1Pri35rJWMdoTPTqqz |
| 293 |
kcIGN1JySaI8bbVitJQmnm0FtFTiB7zznv94rMBCiPmPUWd9BSpSBJteJoBLZ+K7 |
| 294 |
Y7ws2Dzp2sBo/RLUM18oXd0N9PLXvFGI3IuF8ey1SPzQH3QbBdJSTmLzRlPjK7A1 |
| 295 |
HVHFb3vTjd71z9j5IGQQ3Awdw30zMg== |
| 296 |
=gOul |
| 297 |
-----END PGP SIGNATURE----- |
| 298 |
|
| 299 |
--bar-- |
| 300 |
`) |
| 301 |
|
| 302 |
var testPGPMIMESignedInvalid = toCRLF(`From: John Doe <[email protected]> |
| 303 |
To: John Doe <[email protected]> |
| 304 |
Mime-Version: 1.0 |
| 305 |
Content-Type: multipart/signed; boundary=bar; micalg=pgp-sha256; |
| 306 |
protocol="application/pgp-signature" |
| 307 |
|
| 308 |
--bar |
| 309 |
Content-Type: text/plain |
| 310 |
|
| 311 |
This is a signed message, but the signature is invalid. |
| 312 |
|
| 313 |
--bar |
| 314 |
Content-Type: application/pgp-signature |
| 315 |
|
| 316 |
-----BEGIN PGP SIGNATURE----- |
| 317 |
|
| 318 |
iQEzBAABCAAdFiEEsahmk1QVO3mfIhe/MHIVwT33qWQFAl5FRLgACgkQMHIVwT33 |
| 319 |
qWSEQQf/YgRlKlQzSyvm6A52lGIRU3F/z9EGjhCryxj+hSdPlk8O7iZFIjnco4Ea |
| 320 |
7QIlsOj6D4AlLdhyK6c8IZV7rZoTNE5rc6I5UZjM4Qa0XoyLjao28zR252TtwwWJ |
| 321 |
e4+wrTQKcVhCyHO6rkvcCpru4qF5CU+Mi8+sf8CNJJyBgw1Pri35rJWMdoTPTqqz |
| 322 |
kcIGN1JySaI8bbVitJQmnm0FtFTiB7zznv94rMBCiPmPUWd9BSpSBJteJoBLZ+K7 |
| 323 |
Y7ws2Dzp2sBo/RLUM18oXd0N9PLXvFGI3IuF8ey1SPzQH3QbBdJSTmLzRlPjK7A1 |
| 324 |
HVHFb3vTjd71z9j5IGQQ3Awdw30zMg== |
| 325 |
=gOul |
| 326 |
-----END PGP SIGNATURE----- |
| 327 |
|
| 328 |
--bar-- |
| 329 |
`) |
| 330 |
|
| 331 |
var testPlaintext = toCRLF(`From: John Doe <[email protected]> |
| 332 |
To: John Doe <[email protected]> |
| 333 |
Mime-Version: 1.0 |
| 334 |
Content-Type: text/plain |
| 335 |
|
| 336 |
This is a plaintext message! |
| 337 |
`) |