| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package v2 |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/sha512" |
| 10 |
"encoding/base64" |
| 11 |
"encoding/hex" |
| 12 |
goerrors "errors" |
| 13 |
"io" |
| 14 |
"io/ioutil" |
| 15 |
"math/bits" |
| 16 |
"os" |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/ProtonMail/go-crypto/openpgp/armor" |
| 21 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 22 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 23 |
) |
| 24 |
|
| 25 |
func readerFromHex(s string) io.Reader { |
| 26 |
data, err := hex.DecodeString(s) |
| 27 |
if err != nil { |
| 28 |
panic("readerFromHex: bad input") |
| 29 |
} |
| 30 |
return bytes.NewBuffer(data) |
| 31 |
} |
| 32 |
|
| 33 |
func TestReadKeyRing(t *testing.T) { |
| 34 |
kring, err := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 35 |
if err != nil { |
| 36 |
t.Error(err) |
| 37 |
return |
| 38 |
} |
| 39 |
if len(kring) != 2 || uint32(kring[0].PrimaryKey.KeyId) != 0xC20C31BB || uint32(kring[1].PrimaryKey.KeyId) != 0x1E35246B { |
| 40 |
t.Errorf("bad keyring: %#v", kring) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestRereadKeyRing(t *testing.T) { |
| 45 |
kring, err := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 46 |
if err != nil { |
| 47 |
t.Errorf("error in initial parse: %s", err) |
| 48 |
return |
| 49 |
} |
| 50 |
out := new(bytes.Buffer) |
| 51 |
err = kring[0].Serialize(out) |
| 52 |
if err != nil { |
| 53 |
t.Errorf("error in serialization: %s", err) |
| 54 |
return |
| 55 |
} |
| 56 |
kring, err = ReadKeyRing(out) |
| 57 |
if err != nil { |
| 58 |
t.Errorf("error in second parse: %s", err) |
| 59 |
return |
| 60 |
} |
| 61 |
|
| 62 |
if len(kring) != 1 || uint32(kring[0].PrimaryKey.KeyId) != 0xC20C31BB { |
| 63 |
t.Errorf("bad keyring: %#v", kring) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestReadPrivateKeyRing(t *testing.T) { |
| 68 |
kring, err := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 69 |
if err != nil { |
| 70 |
t.Error(err) |
| 71 |
return |
| 72 |
} |
| 73 |
if len(kring) != 2 || uint32(kring[0].PrimaryKey.KeyId) != 0xC20C31BB || uint32(kring[1].PrimaryKey.KeyId) != 0x1E35246B || kring[0].PrimaryKey == nil { |
| 74 |
t.Errorf("bad keyring: %#v", kring) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestReadDSAKey(t *testing.T) { |
| 79 |
kring, err := ReadKeyRing(readerFromHex(dsaTestKeyHex)) |
| 80 |
if err != nil { |
| 81 |
t.Error(err) |
| 82 |
return |
| 83 |
} |
| 84 |
if len(kring) != 1 || uint32(kring[0].PrimaryKey.KeyId) != 0x0CCC0360 { |
| 85 |
t.Errorf("bad parse: %#v", kring) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestReadP256Key(t *testing.T) { |
| 90 |
kring, err := ReadKeyRing(readerFromHex(p256TestKeyHex)) |
| 91 |
if err != nil { |
| 92 |
t.Error(err) |
| 93 |
return |
| 94 |
} |
| 95 |
if len(kring) != 1 || uint32(kring[0].PrimaryKey.KeyId) != 0x5918513E { |
| 96 |
t.Errorf("bad parse: %#v", kring) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestDSAHashTruncatation(t *testing.T) { |
| 101 |
// dsaKeyWithSHA512 was generated with GnuPG and --cert-digest-algo |
| 102 |
// SHA512 in order to require DSA hash truncation to verify correctly. |
| 103 |
_, err := ReadKeyRing(readerFromHex(dsaKeyWithSHA512)) |
| 104 |
if err != nil { |
| 105 |
t.Error(err) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestGetKeyById(t *testing.T) { |
| 110 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 111 |
|
| 112 |
keys := kring.KeysById(0xa34d7e18c20c31bb) |
| 113 |
if len(keys) != 1 || keys[0].Entity != kring[0] { |
| 114 |
t.Errorf("bad result for 0xa34d7e18c20c31bb: %#v", keys) |
| 115 |
} |
| 116 |
|
| 117 |
keys = kring.KeysById(0xfd94408d4543314f) |
| 118 |
if len(keys) != 1 || keys[0].Entity != kring[0] { |
| 119 |
t.Errorf("bad result for 0xa34d7e18c20c31bb: %#v", keys) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func checkSignedMessage(t *testing.T, signedHex, expected string) { |
| 124 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 125 |
|
| 126 |
md, err := ReadMessage(readerFromHex(signedHex), kring, nil, &allowAllAlgorithmsConfig) |
| 127 |
if err != nil { |
| 128 |
t.Error(err) |
| 129 |
return |
| 130 |
} |
| 131 |
|
| 132 |
if len(md.SignatureCandidates) < 1 { |
| 133 |
t.Errorf("bad MessageDetails: %#v", md) |
| 134 |
} |
| 135 |
|
| 136 |
if !md.IsSigned || md.SignatureCandidates[0].IssuerKeyId != 0xa34d7e18c20c31bb || md.SignatureCandidates[0].SignedByEntity == nil || md.IsEncrypted || md.IsSymmetricallyEncrypted || len(md.EncryptedToKeyIds) != 0 || md.DecryptedWith.Entity != nil { |
| 137 |
t.Errorf("bad MessageDetails: %#v", md) |
| 138 |
} |
| 139 |
|
| 140 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 141 |
if err != nil { |
| 142 |
t.Errorf("error reading UnverifiedBody: %s", err) |
| 143 |
} |
| 144 |
if !md.IsVerified { |
| 145 |
t.Errorf("not verified despite all data read") |
| 146 |
} |
| 147 |
if string(contents) != expected { |
| 148 |
t.Errorf("bad UnverifiedBody got:%s want:%s", string(contents), expected) |
| 149 |
} |
| 150 |
if md.SignatureError != nil || md.Signature == nil { |
| 151 |
t.Errorf("failed to validate: %s", md.SignatureError) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestSignedMessage(t *testing.T) { |
| 156 |
checkSignedMessage(t, signedMessageHex, signedInput) |
| 157 |
} |
| 158 |
|
| 159 |
func TestTextSignedMessage(t *testing.T) { |
| 160 |
checkSignedMessage(t, signedTextMessageHex, signedTextInput) |
| 161 |
} |
| 162 |
|
| 163 |
// The reader should detect "compressed quines", which are compressed |
| 164 |
// packets that expand into themselves and cause an infinite recursive |
| 165 |
// parsing loop. |
| 166 |
// The packet in this test case comes from Taylor R. Campbell at |
| 167 |
// http://mumble.net/~campbell/misc/pgp-quine/ |
| 168 |
func TestCampbellQuine(t *testing.T) { |
| 169 |
md, err := ReadMessage(readerFromHex(campbellQuine), nil, nil, nil) |
| 170 |
if md != nil { |
| 171 |
t.Errorf("Reading a compressed quine should not return any data: %#v", md) |
| 172 |
} |
| 173 |
structural, ok := err.(errors.StructuralError) |
| 174 |
if !ok { |
| 175 |
t.Fatalf("Unexpected class of error: %T", err) |
| 176 |
} |
| 177 |
if !strings.Contains(string(structural), "too many layers of packets") { |
| 178 |
t.Fatalf("Unexpected error: %s", err) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestSignedEncryptedMessage(t *testing.T) { |
| 183 |
var signedEncryptedMessageTests = []struct { |
| 184 |
keyRingHex string |
| 185 |
messageHex string |
| 186 |
signedByKeyId uint64 |
| 187 |
encryptedToKeyId uint64 |
| 188 |
verifiedSigHex string |
| 189 |
unverifiedSigHex string |
| 190 |
}{ |
| 191 |
{ |
| 192 |
testKeys1And2PrivateHex, |
| 193 |
signedEncryptedMessageHex, |
| 194 |
0xa34d7e18c20c31bb, |
| 195 |
0x2a67d68660df41c7, |
| 196 |
verifiedSignatureEncryptedMessageHex, |
| 197 |
unverifiedSignatureEncryptedMessageHex, |
| 198 |
}, |
| 199 |
{ |
| 200 |
dsaElGamalTestKeysHex, |
| 201 |
signedEncryptedMessage2Hex, |
| 202 |
0x33af447ccd759b09, |
| 203 |
0xcf6a7abcd43e3673, |
| 204 |
signatureEncryptedMessage2Hex, |
| 205 |
"", |
| 206 |
}, |
| 207 |
} |
| 208 |
for i, test := range signedEncryptedMessageTests { |
| 209 |
expected := "Signed and encrypted message\n" |
| 210 |
kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) |
| 211 |
prompt := func(keys []Key, symmetric bool) ([]byte, error) { |
| 212 |
if symmetric { |
| 213 |
t.Errorf("prompt: message was marked as symmetrically encrypted") |
| 214 |
return nil, errors.ErrKeyIncorrect |
| 215 |
} |
| 216 |
|
| 217 |
if len(keys) == 0 { |
| 218 |
t.Error("prompt: no keys requested") |
| 219 |
return nil, errors.ErrKeyIncorrect |
| 220 |
} |
| 221 |
|
| 222 |
err := keys[0].PrivateKey.Decrypt([]byte("passphrase")) |
| 223 |
if err != nil { |
| 224 |
t.Errorf("prompt: error decrypting key: %s", err) |
| 225 |
return nil, errors.ErrKeyIncorrect |
| 226 |
} |
| 227 |
|
| 228 |
return nil, nil |
| 229 |
} |
| 230 |
|
| 231 |
md, err := ReadMessage(readerFromHex(test.messageHex), kring, prompt, &allowAllAlgorithmsConfig) |
| 232 |
if err != nil { |
| 233 |
t.Errorf("#%d: error reading message: %s", i, err) |
| 234 |
return |
| 235 |
} |
| 236 |
|
| 237 |
if len(md.SignatureCandidates) < 1 { |
| 238 |
t.Errorf("#%d: bad MessageDetails: %#v", i, md) |
| 239 |
} |
| 240 |
|
| 241 |
if !md.IsSigned || md.SignatureCandidates[0].IssuerKeyId != test.signedByKeyId || md.SignatureCandidates[0].SignedByEntity == nil || !md.IsEncrypted || md.IsSymmetricallyEncrypted || len(md.EncryptedToKeyIds) == 0 || md.EncryptedToKeyIds[0] != test.encryptedToKeyId { |
| 242 |
t.Errorf("#%d: bad MessageDetails: %#v", i, md) |
| 243 |
} |
| 244 |
|
| 245 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 246 |
if err != nil { |
| 247 |
t.Errorf("#%d: error reading UnverifiedBody: %s", i, err) |
| 248 |
} |
| 249 |
if !md.IsVerified { |
| 250 |
t.Errorf("not verified despite all data read") |
| 251 |
} |
| 252 |
if string(contents) != expected { |
| 253 |
t.Errorf("#%d: bad UnverifiedBody got:%s want:%s", i, string(contents), expected) |
| 254 |
} |
| 255 |
|
| 256 |
if md.SignatureError != nil || md.Signature == nil { |
| 257 |
t.Errorf("#%d: failed to validate: %s", i, md.SignatureError) |
| 258 |
} |
| 259 |
|
| 260 |
if test.verifiedSigHex != "" { |
| 261 |
var sig bytes.Buffer |
| 262 |
err = md.Signature.Serialize(&sig) |
| 263 |
if err != nil { |
| 264 |
t.Errorf("#%d: error serializing verified signature: %s", i, err) |
| 265 |
} |
| 266 |
|
| 267 |
sigData, err := io.ReadAll(&sig) |
| 268 |
if err != nil { |
| 269 |
t.Errorf("#%d: error reading verified signature: %s", i, err) |
| 270 |
} |
| 271 |
|
| 272 |
if hex.EncodeToString(sigData) != test.verifiedSigHex { |
| 273 |
t.Errorf("#%d: verified signature does not match: %s, %s", i, hex.EncodeToString(sigData), test.verifiedSigHex) |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func TestUnspecifiedRecipient(t *testing.T) { |
| 280 |
expected := "Recipient unspecified\n" |
| 281 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 282 |
|
| 283 |
md, err := ReadMessage(readerFromHex(recipientUnspecifiedHex), kring, nil, nil) |
| 284 |
if err != nil { |
| 285 |
t.Errorf("error reading message: %s", err) |
| 286 |
return |
| 287 |
} |
| 288 |
|
| 289 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 290 |
if err != nil { |
| 291 |
t.Errorf("error reading UnverifiedBody: %s", err) |
| 292 |
} |
| 293 |
if string(contents) != expected { |
| 294 |
t.Errorf("bad UnverifiedBody got:%s want:%s", string(contents), expected) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func TestSymmetricallyEncrypted(t *testing.T) { |
| 299 |
firstTimeCalled := true |
| 300 |
|
| 301 |
prompt := func(keys []Key, symmetric bool) ([]byte, error) { |
| 302 |
if len(keys) != 0 { |
| 303 |
t.Errorf("prompt: len(keys) = %d (want 0)", len(keys)) |
| 304 |
} |
| 305 |
|
| 306 |
if !symmetric { |
| 307 |
t.Errorf("symmetric is not set") |
| 308 |
} |
| 309 |
|
| 310 |
if firstTimeCalled { |
| 311 |
firstTimeCalled = false |
| 312 |
return []byte("wrongpassword"), nil |
| 313 |
} |
| 314 |
|
| 315 |
return []byte("password"), nil |
| 316 |
} |
| 317 |
|
| 318 |
md, err := ReadMessage(readerFromHex(symmetricallyEncryptedCompressedHex), nil, prompt, nil) |
| 319 |
if err != nil { |
| 320 |
t.Errorf("ReadMessage: %s", err) |
| 321 |
return |
| 322 |
} |
| 323 |
|
| 324 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 325 |
if err != nil { |
| 326 |
t.Errorf("ReadAll: %s", err) |
| 327 |
} |
| 328 |
|
| 329 |
expectedCreationTime := uint32(1555107469) |
| 330 |
if md.LiteralData.Time != expectedCreationTime { |
| 331 |
t.Errorf("LiteralData.Time is %d, want %d", md.LiteralData.Time, expectedCreationTime) |
| 332 |
} |
| 333 |
|
| 334 |
const expected = "Symmetrically encrypted.\r\n" |
| 335 |
if string(contents) != expected { |
| 336 |
t.Errorf("contents got: %s want: %s", string(contents), expected) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
func testDetachedSignature(t *testing.T, kring KeyRing, signature io.Reader, sigInput, tag string, expectedSignerKeyId uint64) { |
| 341 |
signed := bytes.NewBufferString(sigInput) |
| 342 |
_, signer, err := VerifyDetachedSignature(kring, signed, signature, &allowAllAlgorithmsConfig) |
| 343 |
if err != nil { |
| 344 |
t.Errorf("%s: signature error: %s", tag, err) |
| 345 |
return |
| 346 |
} |
| 347 |
if signer == nil { |
| 348 |
t.Errorf("%s: signer is nil", tag) |
| 349 |
return |
| 350 |
} |
| 351 |
if signer.PrimaryKey.KeyId != expectedSignerKeyId { |
| 352 |
t.Errorf("%s: wrong signer: got %x, expected %x", tag, signer.PrimaryKey.KeyId, expectedSignerKeyId) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
func TestDetachedSignature(t *testing.T) { |
| 357 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 358 |
testDetachedSignature(t, kring, readerFromHex(detachedSignatureHex), signedInput, "binary", testKey1KeyId) |
| 359 |
testDetachedSignature(t, kring, readerFromHex(detachedSignatureTextHex), signedInput, "text", testKey1KeyId) |
| 360 |
|
| 361 |
incorrectSignedInput := signedInput + "X" |
| 362 |
config := &packet.Config{} |
| 363 |
_, _, err := VerifyDetachedSignature(kring, bytes.NewBufferString(incorrectSignedInput), readerFromHex(detachedSignatureHex), config) |
| 364 |
if err == nil { |
| 365 |
t.Fatal("CheckDetachedSignature returned without error for bad signature") |
| 366 |
} |
| 367 |
if err == errors.ErrUnknownIssuer { |
| 368 |
t.Fatal("CheckDetachedSignature returned ErrUnknownIssuer when the signer was known, but the signature invalid") |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
func TestDetachedSignatureDSA(t *testing.T) { |
| 373 |
kring, _ := ReadKeyRing(readerFromHex(dsaTestKeyHex)) |
| 374 |
testDetachedSignature(t, kring, readerFromHex(detachedSignatureDSAHex), signedInput, "binary", testKey3KeyId) |
| 375 |
} |
| 376 |
|
| 377 |
func TestMultipleSignaturePacketsDSA(t *testing.T) { |
| 378 |
kring, _ := ReadKeyRing(readerFromHex(dsaTestKeyHex)) |
| 379 |
testDetachedSignature(t, kring, readerFromHex(missingHashFunctionHex+detachedSignatureDSAHex), signedInput, "binary", testKey3KeyId) |
| 380 |
} |
| 381 |
|
| 382 |
func TestDetachedSignatureP256(t *testing.T) { |
| 383 |
kring, _ := ReadKeyRing(readerFromHex(p256TestKeyHex)) |
| 384 |
testDetachedSignature(t, kring, readerFromHex(detachedSignatureP256Hex), signedInput, "binary", testKeyP256KeyId) |
| 385 |
} |
| 386 |
|
| 387 |
func testHashFunctionError(t *testing.T, signatureHex string) { |
| 388 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 389 |
config := &packet.Config{} |
| 390 |
_, _, err := VerifyDetachedSignature(kring, nil, readerFromHex(signatureHex), config) |
| 391 |
if err == nil { |
| 392 |
t.Fatal("Packet with bad hash type was correctly parsed") |
| 393 |
} |
| 394 |
if err != errors.ErrUnknownIssuer { |
| 395 |
t.Fatalf("Unexpected class of error: %s", err) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
func TestUnknownHashFunction(t *testing.T) { |
| 400 |
// unknownHashFunctionHex contains a signature packet with hash function type |
| 401 |
// 153 (which isn't a real hash function id). Since that's the only signature |
| 402 |
// packet we don't find any suitable packets and end up with ErrUnknownIssuer. |
| 403 |
testHashFunctionError(t, unknownHashFunctionHex) |
| 404 |
} |
| 405 |
|
| 406 |
func TestMissingHashFunction(t *testing.T) { |
| 407 |
// missingHashFunctionHex contains a signature packet that uses RIPEMD160, |
| 408 |
// which isn't compiled in. Since that's the only signature packet we don't |
| 409 |
// find any suitable packets and end up with ErrUnknownIssuer. |
| 410 |
testHashFunctionError(t, missingHashFunctionHex) |
| 411 |
} |
| 412 |
|
| 413 |
func TestRSASignatureBadMPILength(t *testing.T) { |
| 414 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2Hex)) |
| 415 |
config := &packet.Config{} |
| 416 |
_, _, err := VerifyDetachedSignature(kring, nil, readerFromHex(rsaSignatureBadMPIlength), config) |
| 417 |
if err == nil { |
| 418 |
t.Fatal("RSA Signature with malformed MPI was correctly parsed") |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
func TestDetachedSignatureExpiredCrossSig(t *testing.T) { |
| 423 |
kring, _ := ReadArmoredKeyRing(bytes.NewBufferString(keyWithExpiredCrossSig)) |
| 424 |
config := &packet.Config{} |
| 425 |
_, _, err := VerifyArmoredDetachedSignature(kring, bytes.NewBufferString("Hello World :)"), bytes.NewBufferString(sigFromKeyWithExpiredCrossSig), config) |
| 426 |
if err == nil { |
| 427 |
t.Fatal("Signature from key with expired subkey binding embedded signature was accepted") |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
func TestSignatureUnknownNotation(t *testing.T) { |
| 432 |
el, err := ReadArmoredKeyRing(bytes.NewBufferString(criticalNotationSigner)) |
| 433 |
if err != nil { |
| 434 |
t.Error(err) |
| 435 |
} |
| 436 |
raw, err := armor.Decode(strings.NewReader(signedMessageWithCriticalNotation)) |
| 437 |
if err != nil { |
| 438 |
t.Error(err) |
| 439 |
return |
| 440 |
} |
| 441 |
md, err := ReadMessage(raw.Body, el, nil, &allowAllAlgorithmsConfig) |
| 442 |
if err != nil { |
| 443 |
t.Error(err) |
| 444 |
return |
| 445 |
} |
| 446 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 447 |
if err != nil { |
| 448 |
t.Error(err) |
| 449 |
return |
| 450 |
} |
| 451 |
const expectedErr string = "openpgp: invalid signature: unknown critical notation: [email protected]" |
| 452 |
if md.SignatureError == nil || md.SignatureError.Error() != expectedErr { |
| 453 |
t.Errorf("Expected error '%s', but got error '%s'", expectedErr, md.SignatureError) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
func TestSignatureKnownNotation(t *testing.T) { |
| 458 |
el, err := ReadArmoredKeyRing(bytes.NewBufferString(criticalNotationSigner)) |
| 459 |
if err != nil { |
| 460 |
t.Error(err) |
| 461 |
} |
| 462 |
raw, err := armor.Decode(strings.NewReader(signedMessageWithCriticalNotation)) |
| 463 |
if err != nil { |
| 464 |
t.Error(err) |
| 465 |
return |
| 466 |
} |
| 467 |
config := allowAllAlgorithmsConfig |
| 468 |
config.KnownNotations = map[string]bool{ |
| 469 |
"[email protected]": true, |
| 470 |
} |
| 471 |
md, err := ReadMessage(raw.Body, el, nil, &config) |
| 472 |
if err != nil { |
| 473 |
t.Error(err) |
| 474 |
return |
| 475 |
} |
| 476 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 477 |
if err != nil { |
| 478 |
t.Error(err) |
| 479 |
return |
| 480 |
} |
| 481 |
if !md.IsVerified { |
| 482 |
t.Errorf("not verified despite all data read") |
| 483 |
} |
| 484 |
if md.SignatureError != nil { |
| 485 |
t.Error(md.SignatureError) |
| 486 |
return |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
func TestReadingArmoredPrivateKey(t *testing.T) { |
| 491 |
el, err := ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKeyBlock)) |
| 492 |
if err != nil { |
| 493 |
t.Error(err) |
| 494 |
} |
| 495 |
if len(el) != 1 { |
| 496 |
t.Errorf("got %d entities, wanted 1\n", len(el)) |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
func TestReadingArmoredPublicKey(t *testing.T) { |
| 501 |
el, err := ReadArmoredKeyRing(bytes.NewBufferString(e2ePublicKey)) |
| 502 |
if err != nil { |
| 503 |
t.Error(err) |
| 504 |
} |
| 505 |
if len(el) != 1 { |
| 506 |
t.Errorf("didn't get a valid entity") |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
func TestNoArmoredData(t *testing.T) { |
| 511 |
_, err := ReadArmoredKeyRing(bytes.NewBufferString("foo")) |
| 512 |
if _, ok := err.(errors.InvalidArgumentError); !ok { |
| 513 |
t.Errorf("error was not an InvalidArgumentError: %s", err) |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
func testReadMessageError(t *testing.T, messageHex string) { |
| 518 |
buf, err := hex.DecodeString(messageHex) |
| 519 |
if err != nil { |
| 520 |
t.Errorf("hex.DecodeString(): %v", err) |
| 521 |
} |
| 522 |
|
| 523 |
kr, err := ReadKeyRing(new(bytes.Buffer)) |
| 524 |
if err != nil { |
| 525 |
t.Errorf("ReadKeyring(): %v", err) |
| 526 |
} |
| 527 |
|
| 528 |
_, err = ReadMessage(bytes.NewBuffer(buf), kr, |
| 529 |
func([]Key, bool) ([]byte, error) { |
| 530 |
return []byte("insecure"), nil |
| 531 |
}, nil) |
| 532 |
|
| 533 |
if err == nil { |
| 534 |
t.Errorf("ReadMessage(): Unexpected nil error") |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
func TestIssue11503(t *testing.T) { |
| 539 |
testReadMessageError(t, "8c040402000aa430aa8228b9248b01fc899a91197130303030") |
| 540 |
} |
| 541 |
|
| 542 |
func TestIssue11504(t *testing.T) { |
| 543 |
testReadMessageError(t, "9303000130303030303030303030983002303030303030030000000130") |
| 544 |
} |
| 545 |
|
| 546 |
// TestSignatureV3Message tests the verification of V3 signature, generated |
| 547 |
// with a modern V4-style key. Some people have their clients set to generate |
| 548 |
// V3 signatures, so it's useful to be able to verify them. |
| 549 |
func TestSignatureV3Message(t *testing.T) { |
| 550 |
sig, err := armor.Decode(strings.NewReader(signedMessageV3)) |
| 551 |
if err != nil { |
| 552 |
t.Error(err) |
| 553 |
return |
| 554 |
} |
| 555 |
key, err := ReadArmoredKeyRing(strings.NewReader(keyV4forVerifyingSignedMessageV3)) |
| 556 |
if err != nil { |
| 557 |
t.Error(err) |
| 558 |
return |
| 559 |
} |
| 560 |
md, err := ReadMessage(sig.Body, key, nil, nil) |
| 561 |
if err != nil { |
| 562 |
t.Error(err) |
| 563 |
return |
| 564 |
} |
| 565 |
|
| 566 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 567 |
if err != nil { |
| 568 |
t.Error(err) |
| 569 |
return |
| 570 |
} |
| 571 |
if !md.IsVerified { |
| 572 |
t.Errorf("not verified despite all data read") |
| 573 |
} |
| 574 |
// We'll see a sig error here after reading in the UnverifiedBody above, |
| 575 |
// if there was one to see. |
| 576 |
if err = md.SignatureError; err == nil { |
| 577 |
t.Errorf("Expected a signature error") |
| 578 |
return |
| 579 |
} |
| 580 |
|
| 581 |
if md.Signature != nil { |
| 582 |
t.Errorf("Did not expect a signature V4 back") |
| 583 |
return |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
func TestSignatureOldStyleMessage(t *testing.T) { |
| 588 |
sig, err := armor.Decode(strings.NewReader(signedMessageOldStyle)) |
| 589 |
if err != nil { |
| 590 |
t.Error(err) |
| 591 |
return |
| 592 |
} |
| 593 |
key, err := ReadArmoredKeyRing(strings.NewReader(signedMessageOldStyleKey)) |
| 594 |
if err != nil { |
| 595 |
t.Error(err) |
| 596 |
return |
| 597 |
} |
| 598 |
md, err := ReadMessage(sig.Body, key, nil, nil) |
| 599 |
if err != nil { |
| 600 |
t.Error(err) |
| 601 |
return |
| 602 |
} |
| 603 |
|
| 604 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 605 |
if err != nil { |
| 606 |
t.Error(err) |
| 607 |
return |
| 608 |
} |
| 609 |
if !md.IsVerified { |
| 610 |
t.Errorf("not verified despite all data read") |
| 611 |
} |
| 612 |
// We'll see a sig error here after reading in the UnverifiedBody above, |
| 613 |
// if there was one to see. |
| 614 |
if err = md.SignatureError; err != nil { |
| 615 |
t.Error("Expected no signature error, got: ", err) |
| 616 |
return |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
func TestReadV6Messages(t *testing.T) { |
| 621 |
key, err := ReadArmoredKeyRing(strings.NewReader(v6PrivKey)) |
| 622 |
if err != nil { |
| 623 |
t.Error(err) |
| 624 |
return |
| 625 |
} |
| 626 |
msgReader, err := armor.Decode(strings.NewReader(v6PrivKeyMsg)) |
| 627 |
if err != nil { |
| 628 |
t.Error(err) |
| 629 |
return |
| 630 |
} |
| 631 |
md, err := ReadMessage(msgReader.Body, key, nil, nil) |
| 632 |
if err != nil { |
| 633 |
t.Error(err) |
| 634 |
return |
| 635 |
} |
| 636 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 637 |
if err != nil { |
| 638 |
t.Error(err) |
| 639 |
return |
| 640 |
} |
| 641 |
if string(contents) != "Hello, world!" { |
| 642 |
t.Errorf("decrypted message is wrong: %s", contents) |
| 643 |
} |
| 644 |
|
| 645 |
msgReader, err = armor.Decode(strings.NewReader(v6PrivKeyInlineSignMsg)) |
| 646 |
if err != nil { |
| 647 |
t.Error(err) |
| 648 |
return |
| 649 |
} |
| 650 |
md, err = ReadMessage(msgReader.Body, key, nil, nil) |
| 651 |
if err != nil { |
| 652 |
t.Error(err) |
| 653 |
return |
| 654 |
} |
| 655 |
contents, err = io.ReadAll(md.UnverifiedBody) |
| 656 |
if err != nil { |
| 657 |
t.Error(err) |
| 658 |
return |
| 659 |
} |
| 660 |
if !md.IsVerified { |
| 661 |
t.Errorf("not verified despite all data read") |
| 662 |
} |
| 663 |
if md.SignatureError != nil { |
| 664 |
t.Error("expected no signature error, got:", md.SignatureError) |
| 665 |
} |
| 666 |
if string(contents) != "What we need from the grocery store:\n\n- tofu\n- vegetables\n- noodles\n" { |
| 667 |
t.Errorf("inline message is wrong: %s", contents) |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
func TestSymmetricDecryptionArgon2(t *testing.T) { |
| 672 |
if bits.UintSize == 32 { |
| 673 |
// 32-bit platforms cannot allocate 2GiB of RAM |
| 674 |
// required by the test vector. |
| 675 |
t.Skip() |
| 676 |
} |
| 677 |
// Appendix IETF OpenPGP crypto refresh draft v08 A.8.1 |
| 678 |
file, err := os.Open("../test_data/argon2-sym-message.asc") |
| 679 |
if err != nil { |
| 680 |
t.Fatal(err) |
| 681 |
} |
| 682 |
armoredEncryptedMessage, err := io.ReadAll(file) |
| 683 |
if err != nil { |
| 684 |
t.Fatal(err) |
| 685 |
} |
| 686 |
testSymmetricDecryptionArgon2Run(t, armoredEncryptedMessage) |
| 687 |
} |
| 688 |
|
| 689 |
func TestSymmetricDecryptionArgon2LessMemory(t *testing.T) { |
| 690 |
armoredEncryptedMessage := []byte(`-----BEGIN PGP MESSAGE----- |
| 691 |
|
| 692 |
w0gGJgcCFATa3KMW/4/9RsPME+un+MBqAwQQljCpv3dPfmVTFJAcqn+YRcIFrbY4 |
| 693 |
iiVOkxM5uAKScyYn/T2su2j2fu+uPl/HpgLSWQIHAgx/1caHYWvwl7tyjJ/tSYwK |
| 694 |
m8OMKQHidSWi7UM88mN17ltnLCV/Wa3bLDIyAgJr9XKubHXeUK6/FqmtPxepd4y/ |
| 695 |
SXkqZq0XEafMIbynK2gH6JHjctFX |
| 696 |
-----END PGP MESSAGE-----`) |
| 697 |
testSymmetricDecryptionArgon2Run(t, armoredEncryptedMessage) |
| 698 |
} |
| 699 |
|
| 700 |
func testSymmetricDecryptionArgon2Run(t *testing.T, armoredEncryptedMessage []byte) { |
| 701 |
passphrase := []byte("password") |
| 702 |
// Unarmor string |
| 703 |
raw, err := armor.Decode(strings.NewReader(string(armoredEncryptedMessage))) |
| 704 |
if err != nil { |
| 705 |
t.Error(err) |
| 706 |
return |
| 707 |
} |
| 708 |
// Mock passphrase prompt |
| 709 |
promptFunc := func(keys []Key, symmetric bool) ([]byte, error) { |
| 710 |
return passphrase, nil |
| 711 |
} |
| 712 |
// Decrypt message |
| 713 |
md, err := ReadMessage(raw.Body, nil, promptFunc, nil) |
| 714 |
if err != nil { |
| 715 |
t.Error(err) |
| 716 |
return |
| 717 |
} |
| 718 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 719 |
if err != nil { |
| 720 |
t.Errorf("error reading UnverifiedBody: %s", err) |
| 721 |
} |
| 722 |
|
| 723 |
if string(contents) != "Hello, world!" { |
| 724 |
t.Fatal("Did not decrypt Argon message correctly") |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
func TestAsymmestricAeadOcbOpenPGPjsCompressedMessage(t *testing.T) { |
| 729 |
if packet.V5Disabled { |
| 730 |
t.Skip() |
| 731 |
} |
| 732 |
// Read key from file |
| 733 |
armored, err := os.Open("../test_data/aead-ocb-asym-key.asc") |
| 734 |
if err != nil { |
| 735 |
t.Fatal(err) |
| 736 |
} |
| 737 |
el, err := ReadArmoredKeyRing(armored) |
| 738 |
if err != nil { |
| 739 |
t.Fatal(err) |
| 740 |
} |
| 741 |
// Read ciphertext from file |
| 742 |
ciphertext, err := os.Open("../test_data/aead-ocb-asym-message.asc") |
| 743 |
if err != nil { |
| 744 |
t.Fatal(err) |
| 745 |
} |
| 746 |
armoredEncryptedMessage, err := io.ReadAll(ciphertext) |
| 747 |
if err != nil { |
| 748 |
t.Fatal(err) |
| 749 |
} |
| 750 |
// Unarmor string |
| 751 |
raw, err := armor.Decode(strings.NewReader(string(armoredEncryptedMessage))) |
| 752 |
if err != nil { |
| 753 |
t.Error(err) |
| 754 |
return |
| 755 |
} |
| 756 |
// Decrypt message |
| 757 |
md, err := ReadMessage(raw.Body, el, nil, nil) |
| 758 |
if err != nil { |
| 759 |
t.Error(err) |
| 760 |
return |
| 761 |
} |
| 762 |
// Read contents |
| 763 |
contents, err := io.ReadAll(md.UnverifiedBody) |
| 764 |
if err != nil && err != io.ErrUnexpectedEOF { |
| 765 |
t.Errorf("error reading UnverifiedBody: %s", err) |
| 766 |
} |
| 767 |
|
| 768 |
wantHash := modestProposalSha512 |
| 769 |
gotHashRaw := sha512.Sum512(contents) |
| 770 |
gotHash := base64.StdEncoding.EncodeToString(gotHashRaw[:]) |
| 771 |
|
| 772 |
if wantHash != gotHash { |
| 773 |
t.Fatal("Did not decrypt OpenPGPjs message correctly") |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
func TestSymmetricAeadEaxOpenPGPJsMessage(t *testing.T) { |
| 778 |
if packet.V5Disabled { |
| 779 |
t.Skip() |
| 780 |
} |
| 781 |
key := []byte{79, 41, 206, 112, 224, 133, 140, 223, 27, 61, 227, 57, 114, |
| 782 |
118, 64, 60, 177, 26, 42, 174, 151, 5, 186, 74, 226, 97, 214, 63, 114, 77, |
| 783 |
215, 121} |
| 784 |
|
| 785 |
file, err := os.Open("../test_data/aead-eax-packet.b64") |
| 786 |
if err != nil { |
| 787 |
t.Fatal(err) |
| 788 |
} |
| 789 |
fileBytes, err := io.ReadAll(file) |
| 790 |
if err != nil { |
| 791 |
t.Fatal(err) |
| 792 |
} |
| 793 |
// Decode from base 64 |
| 794 |
raw, err := base64.StdEncoding.DecodeString(string(fileBytes)) |
| 795 |
if err != nil { |
| 796 |
t.Fatal(err) |
| 797 |
} |
| 798 |
r := bytes.NewBuffer(raw) |
| 799 |
// Read packet |
| 800 |
p, err := packet.Read(r) |
| 801 |
if err != nil { |
| 802 |
panic(err) |
| 803 |
} |
| 804 |
|
| 805 |
// Decrypt with key |
| 806 |
edp := p.(*packet.AEADEncrypted) |
| 807 |
rc, err := edp.Decrypt(packet.CipherFunction(0), key) |
| 808 |
if err != nil { |
| 809 |
panic(err) |
| 810 |
} |
| 811 |
// Read literal data packet |
| 812 |
p, err = packet.Read(rc) |
| 813 |
if err != nil { |
| 814 |
t.Fatal(err) |
| 815 |
} |
| 816 |
ld := p.(*packet.LiteralData) |
| 817 |
|
| 818 |
// Read contents |
| 819 |
contents, err := io.ReadAll(ld.Body) |
| 820 |
if err != nil && err != io.ErrUnexpectedEOF { |
| 821 |
t.Errorf("error reading UnverifiedBody: %s", err) |
| 822 |
} |
| 823 |
|
| 824 |
wantHash := modestProposalSha512 |
| 825 |
gotHashRaw := sha512.Sum512(contents) |
| 826 |
gotHash := base64.StdEncoding.EncodeToString(gotHashRaw[:]) |
| 827 |
|
| 828 |
if wantHash != gotHash { |
| 829 |
t.Fatal("Did not decrypt OpenPGPjs message correctly") |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
func TestCorruptedMessageInvalidSigHeader(t *testing.T) { |
| 834 |
// Decrypt message with corrupted MDC and invalid one-pass-signature header |
| 835 |
// Expect parsing errors over unverified decrypted data to be opaque |
| 836 |
passphrase := []byte("password") |
| 837 |
file, err := os.Open("../test_data/sym-corrupted-message-invalid-sig-header.asc") |
| 838 |
if err != nil { |
| 839 |
t.Fatal(err) |
| 840 |
} |
| 841 |
armoredEncryptedMessage, err := io.ReadAll(file) |
| 842 |
if err != nil { |
| 843 |
t.Fatal(err) |
| 844 |
} |
| 845 |
// Unarmor string |
| 846 |
raw, err := armor.Decode(strings.NewReader(string(armoredEncryptedMessage))) |
| 847 |
if err != nil { |
| 848 |
t.Error(err) |
| 849 |
return |
| 850 |
} |
| 851 |
// Mock passphrase prompt |
| 852 |
promptFunc := func(keys []Key, symmetric bool) ([]byte, error) { |
| 853 |
return passphrase, nil |
| 854 |
} |
| 855 |
const expectedErr string = "openpgp: decryption with session key failed: parsing error" |
| 856 |
_, observedErr := ReadMessage(raw.Body, nil, promptFunc, nil) |
| 857 |
if observedErr.Error() != expectedErr { |
| 858 |
t.Errorf("Expected error '%s', but got error '%s'", expectedErr, observedErr) |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
func TestCorruptedMessageWrongLength(t *testing.T) { |
| 863 |
// Decrypt message with wrong length in Literal packet header (length too long) |
| 864 |
// Expect parsing errors over unverified decrypted data to be opaque |
| 865 |
passphrase := []byte("password") |
| 866 |
promptFunc := func(keys []Key, symmetric bool) ([]byte, error) { |
| 867 |
return passphrase, nil |
| 868 |
} |
| 869 |
const expectedErr string = "openpgp: decryption with session key failed: parsing error" |
| 870 |
|
| 871 |
file, err := os.Open("../test_data/sym-corrupted-message-long-length.asc") |
| 872 |
if err != nil { |
| 873 |
t.Fatal(err) |
| 874 |
} |
| 875 |
armoredEncryptedMessage, err := io.ReadAll(file) |
| 876 |
if err != nil { |
| 877 |
t.Fatal(err) |
| 878 |
} |
| 879 |
raw, err := armor.Decode(strings.NewReader(string(armoredEncryptedMessage))) |
| 880 |
if err != nil { |
| 881 |
t.Error(err) |
| 882 |
return |
| 883 |
} |
| 884 |
md, err := ReadMessage(raw.Body, nil, promptFunc, nil) |
| 885 |
if err != nil { |
| 886 |
t.Error(err) |
| 887 |
return |
| 888 |
} |
| 889 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 890 |
if err == nil { |
| 891 |
t.Fatal("Parsing error expected") |
| 892 |
} |
| 893 |
if err.Error() != expectedErr { |
| 894 |
t.Errorf("Expected error '%s', but got error '%s'", expectedErr, err) |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
func TestMessageWithoutMdc(t *testing.T) { |
| 899 |
armored, err := os.Open("../test_data/aead-ocb-asym-key.asc") |
| 900 |
if err != nil { |
| 901 |
t.Fatal(err) |
| 902 |
} |
| 903 |
defer armored.Close() |
| 904 |
|
| 905 |
el, err := ReadArmoredKeyRing(armored) |
| 906 |
if err != nil { |
| 907 |
t.Fatal(err) |
| 908 |
} |
| 909 |
|
| 910 |
armoredMessageWithoutMdc, err := ioutil.ReadFile("../test_data/sym-message-without-mdc.asc") |
| 911 |
if err != nil { |
| 912 |
t.Fatal(err) |
| 913 |
} |
| 914 |
|
| 915 |
t.Run("fails with InsecureAllowUnauthenticatedMessages disabled", func(t *testing.T) { |
| 916 |
messageWithoutMdc, err := armor.Decode(bytes.NewReader(armoredMessageWithoutMdc)) |
| 917 |
if err != nil { |
| 918 |
t.Fatal(err) |
| 919 |
} |
| 920 |
|
| 921 |
_, err = ReadMessage(messageWithoutMdc.Body, el, nil, nil) |
| 922 |
if err == nil { |
| 923 |
t.Fatal("reading the message should have failed") |
| 924 |
} |
| 925 |
}) |
| 926 |
|
| 927 |
t.Run("succeeds with InsecureAllowUnauthenticatedMessages enabled", func(t *testing.T) { |
| 928 |
messageWithoutMdc, err := armor.Decode(bytes.NewReader(armoredMessageWithoutMdc)) |
| 929 |
if err != nil { |
| 930 |
t.Fatal(err) |
| 931 |
} |
| 932 |
|
| 933 |
md, err := ReadMessage(messageWithoutMdc.Body, el, nil, &packet.Config{ |
| 934 |
InsecureAllowUnauthenticatedMessages: true, |
| 935 |
}) |
| 936 |
if err != nil { |
| 937 |
t.Fatal("reading the message should have worked") |
| 938 |
} |
| 939 |
|
| 940 |
b, err := io.ReadAll(md.UnverifiedBody) |
| 941 |
if err != nil { |
| 942 |
t.Fatal("reading the message should have worked") |
| 943 |
} |
| 944 |
|
| 945 |
if !bytes.Equal(b, []byte("message without mdc\n")) { |
| 946 |
t.Error("unexpected message content") |
| 947 |
} |
| 948 |
}) |
| 949 |
} |
| 950 |
|
| 951 |
func TestMultiSignedMessage(t *testing.T) { |
| 952 |
messageBlock, err := armor.Decode(strings.NewReader(multiSignMessage)) |
| 953 |
if err != nil { |
| 954 |
t.Fatal(err) |
| 955 |
} |
| 956 |
|
| 957 |
keyring, err := ReadArmoredKeyRing(strings.NewReader(multiSignMessageKey)) |
| 958 |
if err != nil { |
| 959 |
t.Fatal(err) |
| 960 |
} |
| 961 |
|
| 962 |
md, err := ReadMessage(messageBlock.Body, keyring, nil, nil) |
| 963 |
if err != nil { |
| 964 |
t.Fatal(err) |
| 965 |
} |
| 966 |
|
| 967 |
if len(md.SignatureCandidates) != 2 { |
| 968 |
t.Errorf("expected 2 signature candidates, got: %d", len(md.SignatureCandidates)) |
| 969 |
} |
| 970 |
|
| 971 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 972 |
if err != nil { |
| 973 |
t.Fatal(err) |
| 974 |
} |
| 975 |
if !md.IsVerified { |
| 976 |
t.Errorf("not verified despite all data read") |
| 977 |
} |
| 978 |
|
| 979 |
if md.Signature == nil { |
| 980 |
t.Error("expected a matched verified signatures, got: nil") |
| 981 |
} |
| 982 |
|
| 983 |
if md.SignatureError != nil { |
| 984 |
t.Error("expected no signature error, got: ", md.SignatureError) |
| 985 |
} |
| 986 |
|
| 987 |
if md.SignatureCandidates[0].SignatureError == nil { |
| 988 |
t.Error("First candidate should fail in verification, got: nil") |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
func TestMalformedMessage(t *testing.T) { |
| 993 |
keyring, err := ReadArmoredKeyRing(strings.NewReader(malformedKeyTest)) |
| 994 |
if err != nil { |
| 995 |
t.Fatal(err) |
| 996 |
} |
| 997 |
t.Run("Signature + Literal", func(t *testing.T) { |
| 998 |
testMalformedMessage(t, keyring, malformedLiteralSignature) |
| 999 |
}) |
| 1000 |
t.Run("Two literals, 1st compressed 4 times", func(t *testing.T) { |
| 1001 |
testMalformedMessage(t, keyring, malformedTwoLiteralsCompressed) |
| 1002 |
}) |
| 1003 |
t.Run("PKESK + Literal", func(t *testing.T) { |
| 1004 |
testMalformedMessage(t, keyring, malformedPKESKLiteral) |
| 1005 |
}) |
| 1006 |
t.Run("OPS + Literal", func(t *testing.T) { |
| 1007 |
testMalformedMessage(t, keyring, malformedOPSLiteral) |
| 1008 |
}) |
| 1009 |
} |
| 1010 |
|
| 1011 |
func testMalformedMessage(t *testing.T, keyring EntityList, message string) { |
| 1012 |
raw, err := armor.Decode(strings.NewReader(message)) |
| 1013 |
if err != nil { |
| 1014 |
t.Error(err) |
| 1015 |
return |
| 1016 |
} |
| 1017 |
md, err := ReadMessage(raw.Body, keyring, nil, nil) |
| 1018 |
if err != nil { |
| 1019 |
return |
| 1020 |
} |
| 1021 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 1022 |
if err == nil { |
| 1023 |
t.Error("Expected malformed message error") |
| 1024 |
return |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
func TestReadMessageWithSignOnly(t *testing.T) { |
| 1029 |
config := packet.Config{ |
| 1030 |
InsecureAllowDecryptionWithSigningKeys: true, |
| 1031 |
} |
| 1032 |
key, err := ReadArmoredKeyRing(strings.NewReader(rsaSignOnly)) |
| 1033 |
if err != nil { |
| 1034 |
t.Error(err) |
| 1035 |
return |
| 1036 |
} |
| 1037 |
// Success |
| 1038 |
msgReader, err := armor.Decode(strings.NewReader(armoredMessageRsaSignOnly)) |
| 1039 |
if err != nil { |
| 1040 |
t.Error(err) |
| 1041 |
return |
| 1042 |
} |
| 1043 |
md, err := ReadMessage(msgReader.Body, key, nil, &config) |
| 1044 |
if err != nil { |
| 1045 |
t.Error(err) |
| 1046 |
return |
| 1047 |
} |
| 1048 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 1049 |
if err != nil { |
| 1050 |
t.Error(err) |
| 1051 |
return |
| 1052 |
} |
| 1053 |
|
| 1054 |
// Fail |
| 1055 |
msgReader, err = armor.Decode(strings.NewReader(armoredMessageRsaSignOnly)) |
| 1056 |
if err != nil { |
| 1057 |
t.Error(err) |
| 1058 |
return |
| 1059 |
} |
| 1060 |
_, err = ReadMessage(msgReader.Body, key, nil, nil) |
| 1061 |
if err == nil { |
| 1062 |
t.Fatal("Should not decrypt") |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
func TestReadMessageCompressionLimit(t *testing.T) { |
| 1067 |
// Limit 1KB |
| 1068 |
max := int64(1024) |
| 1069 |
config := packet.Config{ |
| 1070 |
MaxDecompressedMessageSize: &max, |
| 1071 |
} |
| 1072 |
|
| 1073 |
msgReader, err := armor.Decode(strings.NewReader(compressed2KB)) |
| 1074 |
if err != nil { |
| 1075 |
t.Error(err) |
| 1076 |
return |
| 1077 |
} |
| 1078 |
md, err := ReadMessage(msgReader.Body, nil, nil, &config) |
| 1079 |
if err != nil { |
| 1080 |
t.Error(err) |
| 1081 |
return |
| 1082 |
} |
| 1083 |
// Should not be able to read all data |
| 1084 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 1085 |
if !goerrors.Is(err, errors.ErrMessageTooLarge) { |
| 1086 |
t.Errorf("Wrong error") |
| 1087 |
} |
| 1088 |
|
| 1089 |
// Limit 4KB |
| 1090 |
max = int64(4 * 1024) |
| 1091 |
config = packet.Config{ |
| 1092 |
MaxDecompressedMessageSize: &max, |
| 1093 |
} |
| 1094 |
msgReader, err = armor.Decode(strings.NewReader(compressed2KB)) |
| 1095 |
if err != nil { |
| 1096 |
t.Error(err) |
| 1097 |
return |
| 1098 |
} |
| 1099 |
md, err = ReadMessage(msgReader.Body, nil, nil, &config) |
| 1100 |
if err != nil { |
| 1101 |
t.Error(err) |
| 1102 |
return |
| 1103 |
} |
| 1104 |
// Should be able to read all data |
| 1105 |
if _, err = io.ReadAll(md.UnverifiedBody); err != nil { |
| 1106 |
t.Fatal(err) |
| 1107 |
} |
| 1108 |
} |