| 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/rand" |
| 10 |
"io" |
| 11 |
mathrand "math/rand" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 16 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 17 |
"github.com/ProtonMail/go-crypto/openpgp/s2k" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
maxPlaintextLen = 1 << 12 |
| 22 |
maxPassLen = 1 << 6 |
| 23 |
) |
| 24 |
|
| 25 |
func TestSignDetached(t *testing.T) { |
| 26 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 27 |
out := bytes.NewBuffer(nil) |
| 28 |
message := bytes.NewBufferString(signedInput) |
| 29 |
err := DetachSign(out, kring[:1], message, &allowAllAlgorithmsConfig) |
| 30 |
if err != nil { |
| 31 |
t.Error(err) |
| 32 |
} |
| 33 |
|
| 34 |
testDetachedSignature(t, kring, out, signedInput, "check", testKey1KeyId) |
| 35 |
} |
| 36 |
|
| 37 |
func TestSignTextDetached(t *testing.T) { |
| 38 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 39 |
out := bytes.NewBuffer(nil) |
| 40 |
message := bytes.NewBufferString(signedInput) |
| 41 |
err := DetachSignWithParams(out, kring[:1], message, &SignParams{ |
| 42 |
TextSig: true, |
| 43 |
Config: &allowAllAlgorithmsConfig, |
| 44 |
}) |
| 45 |
if err != nil { |
| 46 |
t.Error(err) |
| 47 |
} |
| 48 |
|
| 49 |
testDetachedSignature(t, kring, out, signedInput, "check", testKey1KeyId) |
| 50 |
} |
| 51 |
|
| 52 |
func TestSignDetachedDSA(t *testing.T) { |
| 53 |
kring, _ := ReadKeyRing(readerFromHex(dsaTestKeyPrivateHex)) |
| 54 |
out := bytes.NewBuffer(nil) |
| 55 |
message := bytes.NewBufferString(signedInput) |
| 56 |
err := DetachSign(out, kring[:1], message, &allowAllAlgorithmsConfig) |
| 57 |
if err != nil { |
| 58 |
t.Error(err) |
| 59 |
} |
| 60 |
|
| 61 |
testDetachedSignature(t, kring, out, signedInput, "check", testKey3KeyId) |
| 62 |
} |
| 63 |
|
| 64 |
func TestSignDetachedP256(t *testing.T) { |
| 65 |
kring, _ := ReadKeyRing(readerFromHex(p256TestKeyPrivateHex)) |
| 66 |
if err := kring[0].PrivateKey.Decrypt([]byte("passphrase")); err != nil { |
| 67 |
t.Error(err) |
| 68 |
} |
| 69 |
|
| 70 |
out := bytes.NewBuffer(nil) |
| 71 |
message := bytes.NewBufferString(signedInput) |
| 72 |
err := DetachSign(out, kring[:1], message, nil) |
| 73 |
if err != nil { |
| 74 |
t.Error(err) |
| 75 |
} |
| 76 |
|
| 77 |
testDetachedSignature(t, kring, out, signedInput, "check", testKeyP256KeyId) |
| 78 |
} |
| 79 |
|
| 80 |
func TestSignDetachedWithNotation(t *testing.T) { |
| 81 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 82 |
signature := bytes.NewBuffer(nil) |
| 83 |
message := bytes.NewBufferString(signedInput) |
| 84 |
config := allowAllAlgorithmsConfig |
| 85 |
config.SignatureNotations = []*packet.Notation{ |
| 86 |
{ |
| 87 |
Name: "[email protected]", |
| 88 |
Value: []byte("test"), |
| 89 |
IsHumanReadable: true, |
| 90 |
}, |
| 91 |
} |
| 92 |
config.NonDeterministicSignaturesViaNotation = packet.BoolPointer(false) |
| 93 |
err := DetachSign(signature, kring[:1], message, &config) |
| 94 |
if err != nil { |
| 95 |
t.Error(err) |
| 96 |
} |
| 97 |
|
| 98 |
signed := bytes.NewBufferString(signedInput) |
| 99 |
sig, signer, err := VerifyDetachedSignature(kring, signed, signature, &allowAllAlgorithmsConfig) |
| 100 |
if err != nil { |
| 101 |
t.Errorf("signature error: %s", err) |
| 102 |
return |
| 103 |
} |
| 104 |
if sig == nil { |
| 105 |
t.Errorf("sig is nil") |
| 106 |
return |
| 107 |
} |
| 108 |
if numNotations, numExpected := len(sig.Notations), 1; numNotations != numExpected { |
| 109 |
t.Fatalf("got %d Notation Data subpackets, expected %d", numNotations, numExpected) |
| 110 |
} |
| 111 |
if sig.Notations[0].IsHumanReadable != true { |
| 112 |
t.Fatalf("got false, expected true") |
| 113 |
} |
| 114 |
if sig.Notations[0].Name != "[email protected]" { |
| 115 |
t.Fatalf("got %s, expected [email protected]", sig.Notations[0].Name) |
| 116 |
} |
| 117 |
if string(sig.Notations[0].Value) != "test" { |
| 118 |
t.Fatalf("got %s, expected \"test\"", string(sig.Notations[0].Value)) |
| 119 |
} |
| 120 |
if signer == nil { |
| 121 |
t.Errorf("signer is nil") |
| 122 |
return |
| 123 |
} |
| 124 |
if signer.PrimaryKey.KeyId != testKey1KeyId { |
| 125 |
t.Errorf("wrong signer: got %x, expected %x", signer.PrimaryKey.KeyId, testKey1KeyId) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestSignDetachedWithCriticalNotation(t *testing.T) { |
| 130 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 131 |
signature := bytes.NewBuffer(nil) |
| 132 |
message := bytes.NewBufferString(signedInput) |
| 133 |
config := allowAllAlgorithmsConfig |
| 134 |
config.SignatureNotations = []*packet.Notation{ |
| 135 |
{ |
| 136 |
Name: "[email protected]", |
| 137 |
Value: []byte("test"), |
| 138 |
IsHumanReadable: true, |
| 139 |
IsCritical: true, |
| 140 |
}, |
| 141 |
} |
| 142 |
config.NonDeterministicSignaturesViaNotation = packet.BoolPointer(false) |
| 143 |
err := DetachSign(signature, kring[:1], message, &config) |
| 144 |
if err != nil { |
| 145 |
t.Error(err) |
| 146 |
} |
| 147 |
|
| 148 |
signed := bytes.NewBufferString(signedInput) |
| 149 |
config = allowAllAlgorithmsConfig |
| 150 |
config.KnownNotations = map[string]bool{ |
| 151 |
"[email protected]": true, |
| 152 |
} |
| 153 |
sig, signer, err := VerifyDetachedSignature(kring, signed, signature, &config) |
| 154 |
if err != nil { |
| 155 |
t.Errorf("signature error: %s", err) |
| 156 |
return |
| 157 |
} |
| 158 |
if sig == nil { |
| 159 |
t.Errorf("sig is nil") |
| 160 |
return |
| 161 |
} |
| 162 |
if numNotations, numExpected := len(sig.Notations), 1; numNotations != numExpected { |
| 163 |
t.Fatalf("got %d Notation Data subpackets, expected %d", numNotations, numExpected) |
| 164 |
} |
| 165 |
if sig.Notations[0].IsHumanReadable != true { |
| 166 |
t.Fatalf("got false, expected true") |
| 167 |
} |
| 168 |
if sig.Notations[0].Name != "[email protected]" { |
| 169 |
t.Fatalf("got %s, expected [email protected]", sig.Notations[0].Name) |
| 170 |
} |
| 171 |
if string(sig.Notations[0].Value) != "test" { |
| 172 |
t.Fatalf("got %s, expected \"test\"", string(sig.Notations[0].Value)) |
| 173 |
} |
| 174 |
if signer == nil { |
| 175 |
t.Errorf("signer is nil") |
| 176 |
return |
| 177 |
} |
| 178 |
if signer.PrimaryKey.KeyId != testKey1KeyId { |
| 179 |
t.Errorf("wrong signer: got %x, expected %x", signer.PrimaryKey.KeyId, testKey1KeyId) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestNewEntity(t *testing.T) { |
| 184 |
// Check bit-length with no config. |
| 185 |
e, err := NewEntity("Test User", "test", "[email protected]", nil) |
| 186 |
if err != nil { |
| 187 |
t.Errorf("failed to create entity: %s", err) |
| 188 |
return |
| 189 |
} |
| 190 |
bl, err := e.PrimaryKey.BitLength() |
| 191 |
if err != nil { |
| 192 |
t.Errorf("failed to find bit length: %s", err) |
| 193 |
} |
| 194 |
defaultRSAKeyBits := 2048 |
| 195 |
if int(bl) != defaultRSAKeyBits { |
| 196 |
t.Errorf("BitLength %v, expected %v", int(bl), defaultRSAKeyBits) |
| 197 |
} |
| 198 |
|
| 199 |
// Check bit-length with a config. |
| 200 |
cfg := &packet.Config{RSABits: 1024} |
| 201 |
e, err = NewEntity("Test User", "test", "[email protected]", cfg) |
| 202 |
if err != nil { |
| 203 |
t.Errorf("failed to create entity: %s", err) |
| 204 |
return |
| 205 |
} |
| 206 |
bl, err = e.PrimaryKey.BitLength() |
| 207 |
if err != nil { |
| 208 |
t.Errorf("failed to find bit length: %s", err) |
| 209 |
} |
| 210 |
if int(bl) != cfg.RSABits { |
| 211 |
t.Errorf("BitLength %v, expected %v", bl, cfg.RSABits) |
| 212 |
} |
| 213 |
|
| 214 |
keySerializeConfig := &packet.Config{ |
| 215 |
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), |
| 216 |
} |
| 217 |
w := bytes.NewBuffer(nil) |
| 218 |
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { |
| 219 |
t.Errorf("failed to serialize entity: %s", err) |
| 220 |
return |
| 221 |
} |
| 222 |
serialized := w.Bytes() |
| 223 |
|
| 224 |
el, err := ReadKeyRing(w) |
| 225 |
if err != nil { |
| 226 |
t.Errorf("failed to reparse entity: %s", err) |
| 227 |
return |
| 228 |
} |
| 229 |
|
| 230 |
if len(el) != 1 { |
| 231 |
t.Errorf("wrong number of entities found, got %d, want 1", len(el)) |
| 232 |
} |
| 233 |
|
| 234 |
w = bytes.NewBuffer(nil) |
| 235 |
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { |
| 236 |
t.Errorf("failed to serialize entity second time: %s", err) |
| 237 |
return |
| 238 |
} |
| 239 |
|
| 240 |
if !bytes.Equal(w.Bytes(), serialized) { |
| 241 |
t.Errorf("results differed") |
| 242 |
} |
| 243 |
|
| 244 |
if err := e.PrivateKey.Encrypt([]byte("password")); err != nil { |
| 245 |
t.Errorf("failed to encrypt private key: %s", err) |
| 246 |
} |
| 247 |
|
| 248 |
if err := e.PrivateKey.Decrypt([]byte("password")); err != nil { |
| 249 |
t.Errorf("failed to decrypt private key: %s", err) |
| 250 |
} |
| 251 |
|
| 252 |
w = bytes.NewBuffer(nil) |
| 253 |
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { |
| 254 |
t.Errorf("failed to serialize after encryption round: %s", err) |
| 255 |
return |
| 256 |
} |
| 257 |
|
| 258 |
_, err = ReadKeyRing(w) |
| 259 |
if err != nil { |
| 260 |
t.Errorf("failed to reparse entity after encryption round: %s", err) |
| 261 |
return |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func TestEncryptWithCompression(t *testing.T) { |
| 266 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 267 |
passphrase := []byte("passphrase") |
| 268 |
for _, entity := range kring { |
| 269 |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { |
| 270 |
err := entity.PrivateKey.Decrypt(passphrase) |
| 271 |
if err != nil { |
| 272 |
t.Errorf("failed to decrypt key: %s", err) |
| 273 |
} |
| 274 |
} |
| 275 |
for _, subkey := range entity.Subkeys { |
| 276 |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { |
| 277 |
err := subkey.PrivateKey.Decrypt(passphrase) |
| 278 |
if err != nil { |
| 279 |
t.Errorf("failed to decrypt subkey: %s", err) |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
buf := new(bytes.Buffer) |
| 286 |
config := allowAllAlgorithmsConfig |
| 287 |
config.DefaultCompressionAlgo = packet.CompressionZIP |
| 288 |
config.CompressionConfig = &packet.CompressionConfig{Level: -1} |
| 289 |
w, err := Encrypt(buf, kring[:1], nil, nil, nil /* no hints */, &config) |
| 290 |
if err != nil { |
| 291 |
t.Errorf("error in encrypting plaintext: %s", err) |
| 292 |
return |
| 293 |
} |
| 294 |
message := []byte("hello world") |
| 295 |
_, err = w.Write(message) |
| 296 |
|
| 297 |
if err != nil { |
| 298 |
t.Errorf("error writing plaintext: %s", err) |
| 299 |
return |
| 300 |
} |
| 301 |
err = w.Close() |
| 302 |
if err != nil { |
| 303 |
t.Errorf("error closing WriteCloser: %s", err) |
| 304 |
return |
| 305 |
} |
| 306 |
err = checkCompression(buf, kring[:1]) |
| 307 |
if err != nil { |
| 308 |
t.Errorf("compression check failed: %s", err) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
func TestSymmetricEncryption(t *testing.T) { |
| 313 |
modesS2K := map[string]s2k.Mode{ |
| 314 |
"Iterated": s2k.IteratedSaltedS2K, |
| 315 |
"Argon2": s2k.Argon2S2K, |
| 316 |
} |
| 317 |
for s2kName, s2ktype := range modesS2K { |
| 318 |
t.Run(s2kName, func(t *testing.T) { |
| 319 |
config := &packet.Config{ |
| 320 |
S2KConfig: &s2k.Config{S2KMode: s2ktype}, |
| 321 |
} |
| 322 |
buf := new(bytes.Buffer) |
| 323 |
plaintext, err := SymmetricallyEncrypt(buf, []byte("testing"), nil, config) |
| 324 |
if err != nil { |
| 325 |
t.Errorf("error writing headers: %s", err) |
| 326 |
return |
| 327 |
} |
| 328 |
message := []byte("hello world\n") |
| 329 |
_, err = plaintext.Write(message) |
| 330 |
if err != nil { |
| 331 |
t.Errorf("error writing to plaintext writer: %s", err) |
| 332 |
} |
| 333 |
err = plaintext.Close() |
| 334 |
if err != nil { |
| 335 |
t.Errorf("error closing plaintext writer: %s", err) |
| 336 |
} |
| 337 |
|
| 338 |
md, err := ReadMessage(buf, nil, func(keys []Key, symmetric bool) ([]byte, error) { |
| 339 |
return []byte("testing"), nil |
| 340 |
}, nil) |
| 341 |
if err != nil { |
| 342 |
t.Errorf("error rereading message: %s", err) |
| 343 |
} |
| 344 |
messageBuf := bytes.NewBuffer(nil) |
| 345 |
_, err = io.Copy(messageBuf, md.UnverifiedBody) |
| 346 |
if err != nil { |
| 347 |
t.Errorf("error rereading message: %s", err) |
| 348 |
} |
| 349 |
if !bytes.Equal(message, messageBuf.Bytes()) { |
| 350 |
t.Errorf("recovered message incorrect got '%s', want '%s'", messageBuf.Bytes(), message) |
| 351 |
} |
| 352 |
}) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
func TestSymmetricEncryptionV5RandomizeSlow(t *testing.T) { |
| 357 |
modesS2K := map[int]s2k.Mode{ |
| 358 |
0: s2k.IteratedSaltedS2K, |
| 359 |
1: s2k.Argon2S2K, |
| 360 |
} |
| 361 |
aeadConf := packet.AEADConfig{ |
| 362 |
DefaultMode: aeadModes[mathrand.Intn(len(aeadModes))], |
| 363 |
} |
| 364 |
config := &packet.Config{AEADConfig: &aeadConf, S2KConfig: &s2k.Config{S2KMode: modesS2K[mathrand.Intn(2)]}} |
| 365 |
buf := new(bytes.Buffer) |
| 366 |
passphrase := make([]byte, mathrand.Intn(maxPassLen)) |
| 367 |
_, err := rand.Read(passphrase) |
| 368 |
if err != nil { |
| 369 |
panic(err) |
| 370 |
} |
| 371 |
plaintext, err := SymmetricallyEncrypt(buf, passphrase, nil, config) |
| 372 |
if err != nil { |
| 373 |
t.Errorf("error writing headers: %s", err) |
| 374 |
return |
| 375 |
} |
| 376 |
message := make([]byte, mathrand.Intn(maxPlaintextLen)) |
| 377 |
_, errR := rand.Read(message) |
| 378 |
if errR != nil { |
| 379 |
panic(errR) |
| 380 |
} |
| 381 |
_, err = plaintext.Write(message) |
| 382 |
if err != nil { |
| 383 |
t.Errorf("error writing to plaintext writer: %s", err) |
| 384 |
} |
| 385 |
err = plaintext.Close() |
| 386 |
if err != nil { |
| 387 |
t.Errorf("error closing plaintext writer: %s", err) |
| 388 |
} |
| 389 |
|
| 390 |
// Check if the packet is AEADEncrypted |
| 391 |
copiedCiph := make([]byte, len(buf.Bytes())) |
| 392 |
copy(copiedCiph, buf.Bytes()) |
| 393 |
copiedBuf := bytes.NewBuffer(copiedCiph) |
| 394 |
packets := packet.NewReader(copiedBuf) |
| 395 |
// First a SymmetricKeyEncrypted packet |
| 396 |
p, err := packets.Next() |
| 397 |
if err != nil { |
| 398 |
t.Errorf("error reading packet: %s", err) |
| 399 |
} |
| 400 |
switch tp := p.(type) { |
| 401 |
case *packet.SymmetricKeyEncrypted: |
| 402 |
default: |
| 403 |
t.Errorf("Didn't find a SymmetricKeyEncrypted packet (found %T instead)", tp) |
| 404 |
} |
| 405 |
// Then an SymmetricallyEncrypted packet version 2 |
| 406 |
p, err = packets.Next() |
| 407 |
if err != nil { |
| 408 |
t.Errorf("error reading packet: %s", err) |
| 409 |
} |
| 410 |
switch tp := p.(type) { |
| 411 |
case *packet.SymmetricallyEncrypted: |
| 412 |
if tp.Version != 2 { |
| 413 |
t.Errorf("Wrong packet version, expected 2, found %d", tp.Version) |
| 414 |
} |
| 415 |
default: |
| 416 |
t.Errorf("Didn't find an SymmetricallyEncrypted packet (found %T instead)", tp) |
| 417 |
} |
| 418 |
|
| 419 |
promptFunc := func(keys []Key, symmetric bool) ([]byte, error) { |
| 420 |
return passphrase, nil |
| 421 |
} |
| 422 |
md, err := ReadMessage(buf, nil, promptFunc, config) |
| 423 |
if err != nil { |
| 424 |
t.Errorf("error rereading message: %s", err) |
| 425 |
} |
| 426 |
messageBuf := bytes.NewBuffer(nil) |
| 427 |
_, err = io.Copy(messageBuf, md.UnverifiedBody) |
| 428 |
if err != nil { |
| 429 |
t.Errorf("error rereading message: %s", err) |
| 430 |
} |
| 431 |
if !bytes.Equal(message, messageBuf.Bytes()) { |
| 432 |
t.Errorf("recovered message incorrect got '%s', want '%s'", |
| 433 |
messageBuf.Bytes(), message) |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
func TestIntendedRecipientsEncryption(t *testing.T) { |
| 438 |
var config = &packet.Config{ |
| 439 |
V6Keys: true, |
| 440 |
AEADConfig: &packet.AEADConfig{}, |
| 441 |
Algorithm: packet.PubKeyAlgoEd25519, |
| 442 |
} |
| 443 |
sender, err := NewEntity("sender", "", "[email protected]", config) |
| 444 |
if err != nil { |
| 445 |
t.Errorf("failed to create entity: %s", err) |
| 446 |
return |
| 447 |
} |
| 448 |
|
| 449 |
publicRecipient, err := NewEntity("publicRecipient", "", "[email protected]", config) |
| 450 |
if err != nil { |
| 451 |
t.Errorf("failed to create entity: %s", err) |
| 452 |
return |
| 453 |
} |
| 454 |
|
| 455 |
hiddenRecipient, err := NewEntity("hiddenRecipient", "", "[email protected]", config) |
| 456 |
if err != nil { |
| 457 |
t.Errorf("failed to create entity: %s", err) |
| 458 |
return |
| 459 |
} |
| 460 |
|
| 461 |
outputBuffer := new(bytes.Buffer) |
| 462 |
pWriter, err := Encrypt(outputBuffer, []*Entity{publicRecipient}, []*Entity{hiddenRecipient}, []*Entity{sender}, nil, config) |
| 463 |
if err != nil { |
| 464 |
t.Errorf("error in encrypt: %s", err) |
| 465 |
} |
| 466 |
|
| 467 |
const message = "testing" |
| 468 |
_, err = pWriter.Write([]byte(message)) |
| 469 |
if err != nil { |
| 470 |
t.Errorf("error writing plaintext: %s", err) |
| 471 |
} |
| 472 |
|
| 473 |
err = pWriter.Close() |
| 474 |
if err != nil { |
| 475 |
t.Errorf("error closing WriteCloser: %s", err) |
| 476 |
} |
| 477 |
|
| 478 |
encryptedMessage := make([]byte, len(outputBuffer.Bytes())) |
| 479 |
copy(encryptedMessage, outputBuffer.Bytes()) |
| 480 |
|
| 481 |
md, err := ReadMessage(outputBuffer, EntityList{publicRecipient, sender}, nil /* no prompt */, config) |
| 482 |
if err != nil { |
| 483 |
t.Errorf("error reading message: %s", err) |
| 484 |
} |
| 485 |
|
| 486 |
// Check reading with public recipient |
| 487 |
if !md.CheckRecipients { |
| 488 |
t.Error("should check for intended recipient") |
| 489 |
} |
| 490 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 491 |
if err != nil { |
| 492 |
t.Errorf("error reading encrypted contents: %s", err) |
| 493 |
} |
| 494 |
|
| 495 |
if md.Signature == nil { |
| 496 |
t.Error("expected matching signature") |
| 497 |
} |
| 498 |
|
| 499 |
if len(md.Signature.IntendedRecipients) == 0 || |
| 500 |
!bytes.Equal(md.Signature.IntendedRecipients[0].Fingerprint, publicRecipient.PrimaryKey.Fingerprint) { |
| 501 |
t.Errorf("signature should contain %s as recipient", publicRecipient.PrimaryKey.Fingerprint) |
| 502 |
} |
| 503 |
|
| 504 |
// Check reading with hidden recipient |
| 505 |
outputBuffer = new(bytes.Buffer) |
| 506 |
outputBuffer.Write(encryptedMessage) |
| 507 |
md, err = ReadMessage(outputBuffer, EntityList{hiddenRecipient, sender}, nil /* no prompt */, config) |
| 508 |
if err != nil { |
| 509 |
t.Errorf("error reading message: %s", err) |
| 510 |
} |
| 511 |
if !md.CheckRecipients { |
| 512 |
t.Error("should check for intended recipient") |
| 513 |
} |
| 514 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 515 |
if err != nil { |
| 516 |
t.Errorf("error reading encrypted contents: %s", err) |
| 517 |
} |
| 518 |
if !md.IsVerified { |
| 519 |
t.Errorf("not verified despite all data read") |
| 520 |
} |
| 521 |
if _, ok := md.SignatureError.(errors.SignatureError); !ok { |
| 522 |
t.Error("hidden recipient should not be in the intended recipient list") |
| 523 |
} |
| 524 |
|
| 525 |
// Check reading with hidden recipient check disabled |
| 526 |
outputBuffer = new(bytes.Buffer) |
| 527 |
outputBuffer.Write(encryptedMessage) |
| 528 |
check := false |
| 529 |
config.CheckIntendedRecipients = &check |
| 530 |
md, err = ReadMessage(outputBuffer, EntityList{hiddenRecipient, sender}, nil /* no prompt */, config) |
| 531 |
if err != nil { |
| 532 |
t.Errorf("error reading message: %s", err) |
| 533 |
} |
| 534 |
if md.CheckRecipients { |
| 535 |
t.Error("should not check for intended recipient") |
| 536 |
} |
| 537 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 538 |
if err != nil { |
| 539 |
t.Errorf("error reading encrypted contents: %s", err) |
| 540 |
} |
| 541 |
if !md.IsVerified { |
| 542 |
t.Errorf("not verified despite all data read") |
| 543 |
} |
| 544 |
if md.SignatureError != nil { |
| 545 |
t.Error("signature verification should pass") |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
func TestMultiSignEncryption(t *testing.T) { |
| 550 |
recipient, err := NewEntity("sender", "", "[email protected]", nil) |
| 551 |
if err != nil { |
| 552 |
t.Errorf("failed to create entity: %s", err) |
| 553 |
return |
| 554 |
} |
| 555 |
|
| 556 |
v4Sign, err := NewEntity("signv4", "", "[email protected]", &packet.Config{ |
| 557 |
Algorithm: packet.PubKeyAlgoRSA, |
| 558 |
}) |
| 559 |
if err != nil { |
| 560 |
t.Errorf("failed to create entity: %s", err) |
| 561 |
return |
| 562 |
} |
| 563 |
|
| 564 |
v6Sign, err := NewEntity("signv6", "", "[email protected]", &packet.Config{ |
| 565 |
V6Keys: true, |
| 566 |
Algorithm: packet.PubKeyAlgoEd25519, |
| 567 |
}) |
| 568 |
if err != nil { |
| 569 |
t.Errorf("failed to create entity: %s", err) |
| 570 |
return |
| 571 |
} |
| 572 |
|
| 573 |
outputBuffer := new(bytes.Buffer) |
| 574 |
pWriter, err := Encrypt(outputBuffer, []*Entity{recipient}, nil, []*Entity{v4Sign, v6Sign}, nil, nil) |
| 575 |
if err != nil { |
| 576 |
t.Errorf("error in encrypt: %s", err) |
| 577 |
} |
| 578 |
|
| 579 |
const message = "testing" |
| 580 |
_, err = pWriter.Write([]byte(message)) |
| 581 |
if err != nil { |
| 582 |
t.Errorf("error writing plaintext: %s", err) |
| 583 |
} |
| 584 |
|
| 585 |
err = pWriter.Close() |
| 586 |
if err != nil { |
| 587 |
t.Errorf("error closing WriteCloser: %s", err) |
| 588 |
} |
| 589 |
|
| 590 |
encryptedMessage := make([]byte, len(outputBuffer.Bytes())) |
| 591 |
copy(encryptedMessage, outputBuffer.Bytes()) |
| 592 |
|
| 593 |
md, err := ReadMessage(outputBuffer, EntityList{recipient, v4Sign}, nil /* no prompt */, nil) |
| 594 |
if err != nil { |
| 595 |
t.Errorf("error reading message: %s", err) |
| 596 |
} |
| 597 |
|
| 598 |
// Check reading with v4 key |
| 599 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 600 |
if err != nil { |
| 601 |
t.Errorf("error reading encrypted contents: %s", err) |
| 602 |
} |
| 603 |
if !md.IsVerified { |
| 604 |
t.Errorf("not verified despite all data read") |
| 605 |
} |
| 606 |
if md.Signature == nil || md.SignatureError != nil { |
| 607 |
t.Error("expected matching signature") |
| 608 |
} |
| 609 |
|
| 610 |
// Check reading with v6 key |
| 611 |
outputBuffer = new(bytes.Buffer) |
| 612 |
outputBuffer.Write(encryptedMessage) |
| 613 |
md, err = ReadMessage(outputBuffer, EntityList{recipient, v6Sign}, nil /* no prompt */, nil) |
| 614 |
if err != nil { |
| 615 |
t.Errorf("error reading message: %s", err) |
| 616 |
} |
| 617 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 618 |
if err != nil { |
| 619 |
t.Errorf("error reading encrypted contents: %s", err) |
| 620 |
} |
| 621 |
if !md.IsVerified { |
| 622 |
t.Errorf("not verified despite all data read") |
| 623 |
} |
| 624 |
if md.Signature == nil || md.SignatureError != nil { |
| 625 |
t.Error("expected matching signature") |
| 626 |
} |
| 627 |
|
| 628 |
// Check reading with error |
| 629 |
outputBuffer = new(bytes.Buffer) |
| 630 |
outputBuffer.Write(encryptedMessage) |
| 631 |
md, err = ReadMessage(outputBuffer, EntityList{recipient}, nil /* no prompt */, nil) |
| 632 |
if err != nil { |
| 633 |
t.Errorf("error reading message: %s", err) |
| 634 |
} |
| 635 |
_, err = io.ReadAll(md.UnverifiedBody) |
| 636 |
if err != nil { |
| 637 |
t.Errorf("error reading encrypted contents: %s", err) |
| 638 |
} |
| 639 |
if !md.IsVerified { |
| 640 |
t.Errorf("not verified despite all data read") |
| 641 |
} |
| 642 |
if md.Signature != nil || md.SignatureError == nil { |
| 643 |
t.Error("expected error") |
| 644 |
} |
| 645 |
if md.SignatureError != errors.ErrUnknownIssuer { |
| 646 |
t.Error("expected unknown issuer error") |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
var testEncryptionTests = map[string]struct { |
| 651 |
keyRingHex string |
| 652 |
isSigned bool |
| 653 |
okV6 bool |
| 654 |
}{ |
| 655 |
"Simple": { |
| 656 |
testKeys1And2PrivateHex, |
| 657 |
false, |
| 658 |
true, |
| 659 |
}, |
| 660 |
"Simple_signed": { |
| 661 |
testKeys1And2PrivateHex, |
| 662 |
true, |
| 663 |
true, |
| 664 |
}, |
| 665 |
"DSA_ElGamal": { |
| 666 |
dsaElGamalTestKeysHex, |
| 667 |
false, |
| 668 |
false, |
| 669 |
}, |
| 670 |
"DSA_ElGamal_signed": { |
| 671 |
dsaElGamalTestKeysHex, |
| 672 |
true, |
| 673 |
false, |
| 674 |
}, |
| 675 |
"v6_Ed25519_ML-KEM-768+X25519": { |
| 676 |
v6Ed25519Mlkem768X25519PrivateHex, |
| 677 |
false, |
| 678 |
true, |
| 679 |
}, |
| 680 |
"v6_Ed25519_ML-KEM-768+X25519_signed": { |
| 681 |
v6Ed25519Mlkem768X25519PrivateHex, |
| 682 |
true, |
| 683 |
true, |
| 684 |
}, |
| 685 |
"v4_Ed25519_ML-KEM-768+X25519": { |
| 686 |
v4Ed25519Mlkem768X25519PrivateHex, |
| 687 |
false, |
| 688 |
true, |
| 689 |
}, |
| 690 |
"v4_Ed25519_ML-KEM-768+X25519_signed": { |
| 691 |
v4Ed25519Mlkem768X25519PrivateHex, |
| 692 |
true, |
| 693 |
true, |
| 694 |
}, |
| 695 |
"v4_Ed25519_X25519,ML-KEM-768+X25519": { |
| 696 |
v4Ed25519X25519Mlkem768X25519PrivateHex, |
| 697 |
false, |
| 698 |
true, |
| 699 |
}, |
| 700 |
"v6_ML-DSA-67+Ed25519_ML-KEM-768+X25519_signed": { |
| 701 |
v6Mldsa65Ed25519Mlkem768X25519PrivateHex, |
| 702 |
true, |
| 703 |
true, |
| 704 |
}, |
| 705 |
"v6_ML-DSA-87+Ed448_ML-KEM-1024+X448": { |
| 706 |
v6Mldsa87Ed448Mlkem1024X448PrivateHex, |
| 707 |
false, |
| 708 |
true, |
| 709 |
}, |
| 710 |
"v6_ML-DSA-87+Ed448_ML-KEM-1024+X448_signed": { |
| 711 |
v6Mldsa87Ed448Mlkem1024X448PrivateHex, |
| 712 |
true, |
| 713 |
true, |
| 714 |
}, |
| 715 |
"v6_SLH-DSA-128s_ML-KEM-768+X25519_signed": { |
| 716 |
v6Slhdsa128sMlkem768X25519PrivateHex, |
| 717 |
true, |
| 718 |
true, |
| 719 |
}, |
| 720 |
"v6_SLH-DSA-128f_ML-KEM-768+X25519_signed": { |
| 721 |
v6Slhdsa128fMlkem768X25519PrivateHex, |
| 722 |
true, |
| 723 |
true, |
| 724 |
}, |
| 725 |
"v6_SLH-DSA-256s_ML-KEM-1024+X448_signed": { |
| 726 |
v6Slhdsa256sMlkem1024X448PrivateHex, |
| 727 |
true, |
| 728 |
true, |
| 729 |
}, |
| 730 |
} |
| 731 |
|
| 732 |
func TestEncryption(t *testing.T) { |
| 733 |
for name, test := range testEncryptionTests { |
| 734 |
t.Run(name, func(t *testing.T) { |
| 735 |
kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) |
| 736 |
|
| 737 |
passphrase := []byte("passphrase") |
| 738 |
for _, entity := range kring { |
| 739 |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { |
| 740 |
err := entity.PrivateKey.Decrypt(passphrase) |
| 741 |
if err != nil { |
| 742 |
t.Fatal("Failed to decrypt key") |
| 743 |
} |
| 744 |
} |
| 745 |
for _, subkey := range entity.Subkeys { |
| 746 |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { |
| 747 |
err := subkey.PrivateKey.Decrypt(passphrase) |
| 748 |
if err != nil { |
| 749 |
t.Fatal("Failed to decrypt subkey") |
| 750 |
} |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
var signed []*Entity |
| 756 |
if test.isSigned { |
| 757 |
signed = kring[:1] |
| 758 |
} |
| 759 |
|
| 760 |
buf := new(bytes.Buffer) |
| 761 |
// randomized compression test |
| 762 |
compAlgos := []packet.CompressionAlgo{ |
| 763 |
packet.CompressionNone, |
| 764 |
packet.CompressionZIP, |
| 765 |
packet.CompressionZLIB, |
| 766 |
} |
| 767 |
compAlgo := compAlgos[mathrand.Intn(len(compAlgos))] |
| 768 |
level := mathrand.Intn(11) - 1 |
| 769 |
compConf := &packet.CompressionConfig{Level: level} |
| 770 |
config := allowAllAlgorithmsConfig |
| 771 |
config.DefaultCompressionAlgo = compAlgo |
| 772 |
config.CompressionConfig = compConf |
| 773 |
config.DefaultCipher = packet.CipherAES256 |
| 774 |
|
| 775 |
// Flip coin to enable AEAD mode |
| 776 |
if test.okV6 && (mathrand.Int()%2 == 0) { |
| 777 |
aeadConf := packet.AEADConfig{ |
| 778 |
DefaultMode: aeadModes[mathrand.Intn(len(aeadModes))], |
| 779 |
} |
| 780 |
config.AEADConfig = &aeadConf |
| 781 |
} |
| 782 |
|
| 783 |
w, err := Encrypt(buf, kring[:1], nil, signed, nil /* no hints */, &config) |
| 784 |
if (err != nil) == (test.okV6 && config.AEAD() != nil) { |
| 785 |
// ElGamal is not allowed with v6 |
| 786 |
return |
| 787 |
} |
| 788 |
|
| 789 |
if err != nil { |
| 790 |
t.Fatalf("Error in Encrypt: %s", err) |
| 791 |
} |
| 792 |
|
| 793 |
const message = "testing" |
| 794 |
_, err = w.Write([]byte(message)) |
| 795 |
if err != nil { |
| 796 |
t.Fatalf("Error writing plaintext: %s", err) |
| 797 |
} |
| 798 |
err = w.Close() |
| 799 |
if err != nil { |
| 800 |
t.Fatalf("Error closing WriteCloser: %s", err) |
| 801 |
} |
| 802 |
|
| 803 |
testTime, _ := time.Parse("2006-01-02", "2026-01-01") |
| 804 |
|
| 805 |
md, err := ReadMessage(buf, kring, nil /* no prompt */, &config) |
| 806 |
if err != nil { |
| 807 |
t.Fatalf("Error reading message: %s", err) |
| 808 |
} |
| 809 |
|
| 810 |
if test.isSigned { |
| 811 |
signKey, ok := kring[0].SigningKey(testTime, &allowAllAlgorithmsConfig) |
| 812 |
if !ok { |
| 813 |
t.Fatalf("#%s: No signing key found at %v", name, testTime) |
| 814 |
} |
| 815 |
expectedKeyId := signKey.PublicKey.KeyId |
| 816 |
if len(md.SignatureCandidates) < 1 { |
| 817 |
t.Errorf("#%s: no candidate signature found", name) |
| 818 |
} |
| 819 |
if md.SignatureCandidates[0].IssuerKeyId != expectedKeyId { |
| 820 |
t.Errorf("#%s: message signed by wrong key id, got: %v, want: %v", name, *md.SignatureCandidates[0].SignedBy, expectedKeyId) |
| 821 |
} |
| 822 |
if md.SignatureCandidates[0].SignedByEntity == nil { |
| 823 |
t.Errorf("#%s: failed to find the signing Entity", name) |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
plaintext, err := io.ReadAll(md.UnverifiedBody) |
| 828 |
if err != nil { |
| 829 |
t.Fatalf("Error reading encrypted contents: %s", err) |
| 830 |
} |
| 831 |
|
| 832 |
encryptKey, ok := kring[0].EncryptionKey(testTime, &allowAllAlgorithmsConfig) |
| 833 |
if !ok { |
| 834 |
t.Fatalf("#%s: No encryption key found at %v", name, testTime) |
| 835 |
} |
| 836 |
expectedKeyId := encryptKey.PublicKey.KeyId |
| 837 |
if len(md.EncryptedToKeyIds) != 1 || md.EncryptedToKeyIds[0] != expectedKeyId { |
| 838 |
t.Errorf("Expected message to be encrypted to %v, but got %#v", expectedKeyId, md.EncryptedToKeyIds) |
| 839 |
} |
| 840 |
|
| 841 |
if string(plaintext) != message { |
| 842 |
t.Errorf("#Got: %s, want: %s", string(plaintext), message) |
| 843 |
} |
| 844 |
|
| 845 |
if test.isSigned { |
| 846 |
if !md.IsVerified { |
| 847 |
t.Errorf("not verified despite all data read") |
| 848 |
} |
| 849 |
if md.SignatureError != nil { |
| 850 |
t.Errorf("Signature error: %s", md.SignatureError) |
| 851 |
} |
| 852 |
if md.Signature == nil { |
| 853 |
t.Error("Signature missing") |
| 854 |
} |
| 855 |
} |
| 856 |
}) |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
var testSigningTests = []struct { |
| 861 |
keyRingHex string |
| 862 |
}{ |
| 863 |
{ |
| 864 |
testKeys1And2PrivateHex, |
| 865 |
}, |
| 866 |
{ |
| 867 |
dsaElGamalTestKeysHex, |
| 868 |
}, |
| 869 |
{ |
| 870 |
ed25519wX25519Key, |
| 871 |
}, |
| 872 |
} |
| 873 |
|
| 874 |
func TestSigning(t *testing.T) { |
| 875 |
for i, test := range testSigningTests { |
| 876 |
kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) |
| 877 |
|
| 878 |
passphrase := []byte("passphrase") |
| 879 |
for _, entity := range kring { |
| 880 |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { |
| 881 |
err := entity.PrivateKey.Decrypt(passphrase) |
| 882 |
if err != nil { |
| 883 |
t.Errorf("#%d: failed to decrypt key", i) |
| 884 |
} |
| 885 |
} |
| 886 |
for _, subkey := range entity.Subkeys { |
| 887 |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { |
| 888 |
err := subkey.PrivateKey.Decrypt(passphrase) |
| 889 |
if err != nil { |
| 890 |
t.Errorf("#%d: failed to decrypt subkey", i) |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
signed := kring[0] |
| 897 |
|
| 898 |
buf := new(bytes.Buffer) |
| 899 |
w, err := Sign(buf, []*Entity{signed}, nil /* no hints */, &allowAllAlgorithmsConfig) |
| 900 |
if err != nil { |
| 901 |
t.Errorf("#%d: error in Sign: %s", i, err) |
| 902 |
continue |
| 903 |
} |
| 904 |
|
| 905 |
const message = "testing" |
| 906 |
_, err = w.Write([]byte(message)) |
| 907 |
if err != nil { |
| 908 |
t.Errorf("#%d: error writing plaintext: %s", i, err) |
| 909 |
continue |
| 910 |
} |
| 911 |
err = w.Close() |
| 912 |
if err != nil { |
| 913 |
t.Errorf("#%d: error closing WriteCloser: %s", i, err) |
| 914 |
continue |
| 915 |
} |
| 916 |
|
| 917 |
md, err := ReadMessage(buf, kring, nil /* no prompt */, &allowAllAlgorithmsConfig) |
| 918 |
if err != nil { |
| 919 |
t.Errorf("#%d: error reading message: %s", i, err) |
| 920 |
continue |
| 921 |
} |
| 922 |
|
| 923 |
testTime, _ := time.Parse("2006-01-02", "2022-12-01") |
| 924 |
signKey, _ := kring[0].SigningKey(testTime, &allowAllAlgorithmsConfig) |
| 925 |
expectedKeyId := signKey.PublicKey.KeyId |
| 926 |
if len(md.SignatureCandidates) < 1 { |
| 927 |
t.Error("expected a signature candidate") |
| 928 |
} |
| 929 |
if md.SignatureCandidates[0].IssuerKeyId != expectedKeyId { |
| 930 |
t.Errorf("#%d: message signed by wrong key id, got: %v, want: %v", i, *md.SignatureCandidates[0].SignedBy, expectedKeyId) |
| 931 |
} |
| 932 |
if md.SignatureCandidates[0].SignedByEntity == nil { |
| 933 |
t.Errorf("#%d: failed to find the signing Entity", i) |
| 934 |
} |
| 935 |
|
| 936 |
plaintext, err := io.ReadAll(md.UnverifiedBody) |
| 937 |
if err != nil { |
| 938 |
t.Errorf("#%d: error reading contents: %v", i, err) |
| 939 |
continue |
| 940 |
} |
| 941 |
|
| 942 |
if string(plaintext) != message { |
| 943 |
t.Errorf("#%d: got: %q, want: %q", i, plaintext, message) |
| 944 |
} |
| 945 |
|
| 946 |
if !md.IsVerified { |
| 947 |
t.Errorf("not verified despite all data read") |
| 948 |
} |
| 949 |
if md.SignatureError != nil { |
| 950 |
t.Errorf("#%d: signature error: %q", i, md.SignatureError) |
| 951 |
} |
| 952 |
if md.Signature == nil { |
| 953 |
t.Error("signature missing") |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
func checkCompression(r io.Reader, keyring KeyRing) (err error) { |
| 959 |
var p packet.Packet |
| 960 |
|
| 961 |
var symKeys []*packet.SymmetricKeyEncrypted |
| 962 |
var pubKeys []keyEnvelopePair |
| 963 |
// Integrity protected encrypted packet: SymmetricallyEncrypted or AEADEncrypted |
| 964 |
var edp packet.EncryptedDataPacket |
| 965 |
|
| 966 |
packets := packet.NewReader(r) |
| 967 |
config := &packet.Config{} |
| 968 |
|
| 969 |
// The message, if encrypted, starts with a number of packets |
| 970 |
// containing an encrypted decryption key. The decryption key is either |
| 971 |
// encrypted to a public key, or with a passphrase. This loop |
| 972 |
// collects these packets. |
| 973 |
ParsePackets: |
| 974 |
for { |
| 975 |
p, err = packets.Next() |
| 976 |
if err != nil { |
| 977 |
return err |
| 978 |
} |
| 979 |
switch p := p.(type) { |
| 980 |
case *packet.EncryptedKey: |
| 981 |
// This packet contains the decryption key encrypted to a public key. |
| 982 |
switch p.Algo { |
| 983 |
case packet.PubKeyAlgoRSA, packet.PubKeyAlgoRSAEncryptOnly, packet.PubKeyAlgoElGamal, packet.PubKeyAlgoECDH: |
| 984 |
break |
| 985 |
default: |
| 986 |
continue |
| 987 |
} |
| 988 |
keys := keyring.KeysById(p.KeyId) |
| 989 |
for _, k := range keys { |
| 990 |
pubKeys = append(pubKeys, keyEnvelopePair{k, p}) |
| 991 |
} |
| 992 |
case *packet.SymmetricallyEncrypted, *packet.AEADEncrypted: |
| 993 |
edp = p.(packet.EncryptedDataPacket) |
| 994 |
break ParsePackets |
| 995 |
case *packet.Compressed, *packet.LiteralData, *packet.OnePassSignature: |
| 996 |
// This message isn't encrypted. |
| 997 |
return errors.StructuralError("message not encrypted") |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
var candidates []Key |
| 1002 |
var decrypted io.ReadCloser |
| 1003 |
|
| 1004 |
// Now that we have the list of encrypted keys we need to decrypt at |
| 1005 |
// least one of them or, if we cannot, we need to call the prompt |
| 1006 |
// function so that it can decrypt a key or give us a passphrase. |
| 1007 |
FindKey: |
| 1008 |
for { |
| 1009 |
// See if any of the keys already have a private key available |
| 1010 |
candidates = candidates[:0] |
| 1011 |
candidateFingerprints := make(map[string]bool) |
| 1012 |
|
| 1013 |
for _, pk := range pubKeys { |
| 1014 |
if pk.key.PrivateKey == nil { |
| 1015 |
continue |
| 1016 |
} |
| 1017 |
if !pk.key.PrivateKey.Encrypted { |
| 1018 |
if len(pk.encryptedKey.Key) == 0 { |
| 1019 |
errDec := pk.encryptedKey.Decrypt(pk.key.PrivateKey, config) |
| 1020 |
if errDec != nil { |
| 1021 |
continue |
| 1022 |
} |
| 1023 |
} |
| 1024 |
// Try to decrypt symmetrically encrypted |
| 1025 |
decrypted, err = edp.Decrypt(pk.encryptedKey.CipherFunc, pk.encryptedKey.Key) |
| 1026 |
if err != nil && err != errors.ErrKeyIncorrect { |
| 1027 |
return err |
| 1028 |
} |
| 1029 |
if decrypted != nil { |
| 1030 |
break FindKey |
| 1031 |
} |
| 1032 |
} else { |
| 1033 |
fpr := string(pk.key.PublicKey.Fingerprint[:]) |
| 1034 |
if v := candidateFingerprints[fpr]; v { |
| 1035 |
continue |
| 1036 |
} |
| 1037 |
candidates = append(candidates, pk.key) |
| 1038 |
candidateFingerprints[fpr] = true |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
if len(candidates) == 0 && len(symKeys) == 0 { |
| 1043 |
return errors.ErrKeyIncorrect |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
decPackets, err := packet.Read(decrypted) |
| 1048 |
if err != nil { |
| 1049 |
return |
| 1050 |
} |
| 1051 |
_, ok := decPackets.(*packet.Compressed) |
| 1052 |
if !ok { |
| 1053 |
return errors.StructuralError("No compressed packets found") |
| 1054 |
} |
| 1055 |
return nil |
| 1056 |
} |