| 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 openpgp |
| 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[0], message, nil) |
| 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 := DetachSignText(out, kring[0], message, nil) |
| 42 |
if err != nil { |
| 43 |
t.Error(err) |
| 44 |
} |
| 45 |
|
| 46 |
testDetachedSignature(t, kring, out, signedInput, "check", testKey1KeyId) |
| 47 |
} |
| 48 |
|
| 49 |
func TestSignDetachedDSA(t *testing.T) { |
| 50 |
kring, _ := ReadKeyRing(readerFromHex(dsaTestKeyPrivateHex)) |
| 51 |
out := bytes.NewBuffer(nil) |
| 52 |
message := bytes.NewBufferString(signedInput) |
| 53 |
err := DetachSign(out, kring[0], message, nil) |
| 54 |
if err != nil { |
| 55 |
t.Error(err) |
| 56 |
} |
| 57 |
|
| 58 |
testDetachedSignature(t, kring, out, signedInput, "check", testKey3KeyId) |
| 59 |
} |
| 60 |
|
| 61 |
func TestSignDetachedP256(t *testing.T) { |
| 62 |
kring, _ := ReadKeyRing(readerFromHex(p256TestKeyPrivateHex)) |
| 63 |
if err := kring[0].PrivateKey.Decrypt([]byte("passphrase")); err != nil { |
| 64 |
t.Error(err) |
| 65 |
} |
| 66 |
|
| 67 |
out := bytes.NewBuffer(nil) |
| 68 |
message := bytes.NewBufferString(signedInput) |
| 69 |
err := DetachSign(out, kring[0], message, nil) |
| 70 |
if err != nil { |
| 71 |
t.Error(err) |
| 72 |
} |
| 73 |
|
| 74 |
testDetachedSignature(t, kring, out, signedInput, "check", testKeyP256KeyId) |
| 75 |
} |
| 76 |
|
| 77 |
func TestSignDetachedWithNotation(t *testing.T) { |
| 78 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 79 |
signature := bytes.NewBuffer(nil) |
| 80 |
message := bytes.NewBufferString(signedInput) |
| 81 |
config := &packet.Config{ |
| 82 |
SignatureNotations: []*packet.Notation{ |
| 83 |
{ |
| 84 |
Name: "[email protected]", |
| 85 |
Value: []byte("test"), |
| 86 |
IsHumanReadable: true, |
| 87 |
}, |
| 88 |
}, |
| 89 |
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), |
| 90 |
} |
| 91 |
err := DetachSign(signature, kring[0], message, config) |
| 92 |
if err != nil { |
| 93 |
t.Error(err) |
| 94 |
} |
| 95 |
|
| 96 |
signed := bytes.NewBufferString(signedInput) |
| 97 |
config = &packet.Config{} |
| 98 |
sig, signer, err := VerifyDetachedSignature(kring, signed, signature, config) |
| 99 |
if err != nil { |
| 100 |
t.Errorf("signature error: %s", err) |
| 101 |
return |
| 102 |
} |
| 103 |
if sig == nil { |
| 104 |
t.Errorf("sig is nil") |
| 105 |
return |
| 106 |
} |
| 107 |
if numNotations, numExpected := len(sig.Notations), 1; numNotations != numExpected { |
| 108 |
t.Fatalf("got %d Notation Data subpackets, expected %d", numNotations, numExpected) |
| 109 |
} |
| 110 |
if sig.Notations[0].IsHumanReadable != true { |
| 111 |
t.Fatalf("got false, expected true") |
| 112 |
} |
| 113 |
if sig.Notations[0].Name != "[email protected]" { |
| 114 |
t.Fatalf("got %s, expected [email protected]", sig.Notations[0].Name) |
| 115 |
} |
| 116 |
if string(sig.Notations[0].Value) != "test" { |
| 117 |
t.Fatalf("got %s, expected \"test\"", string(sig.Notations[0].Value)) |
| 118 |
} |
| 119 |
if signer == nil { |
| 120 |
t.Errorf("signer is nil") |
| 121 |
return |
| 122 |
} |
| 123 |
if signer.PrimaryKey.KeyId != testKey1KeyId { |
| 124 |
t.Errorf("wrong signer: got %x, expected %x", signer.PrimaryKey.KeyId, testKey1KeyId) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestSignDetachedWithCriticalNotation(t *testing.T) { |
| 129 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 130 |
signature := bytes.NewBuffer(nil) |
| 131 |
message := bytes.NewBufferString(signedInput) |
| 132 |
config := &packet.Config{ |
| 133 |
SignatureNotations: []*packet.Notation{ |
| 134 |
{ |
| 135 |
Name: "[email protected]", |
| 136 |
Value: []byte("test"), |
| 137 |
IsHumanReadable: true, |
| 138 |
IsCritical: true, |
| 139 |
}, |
| 140 |
}, |
| 141 |
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), |
| 142 |
} |
| 143 |
err := DetachSign(signature, kring[0], message, config) |
| 144 |
if err != nil { |
| 145 |
t.Error(err) |
| 146 |
} |
| 147 |
|
| 148 |
signed := bytes.NewBufferString(signedInput) |
| 149 |
config = &packet.Config{ |
| 150 |
KnownNotations: map[string]bool{ |
| 151 |
"[email protected]": true, |
| 152 |
}, |
| 153 |
} |
| 154 |
sig, signer, err := VerifyDetachedSignature(kring, signed, signature, config) |
| 155 |
if err != nil { |
| 156 |
t.Errorf("signature error: %s", err) |
| 157 |
return |
| 158 |
} |
| 159 |
if sig == nil { |
| 160 |
t.Errorf("sig is nil") |
| 161 |
return |
| 162 |
} |
| 163 |
if numNotations, numExpected := len(sig.Notations), 1; numNotations != numExpected { |
| 164 |
t.Fatalf("got %d Notation Data subpackets, expected %d", numNotations, numExpected) |
| 165 |
} |
| 166 |
if sig.Notations[0].IsHumanReadable != true { |
| 167 |
t.Fatalf("got false, expected true") |
| 168 |
} |
| 169 |
if sig.Notations[0].Name != "[email protected]" { |
| 170 |
t.Fatalf("got %s, expected [email protected]", sig.Notations[0].Name) |
| 171 |
} |
| 172 |
if string(sig.Notations[0].Value) != "test" { |
| 173 |
t.Fatalf("got %s, expected \"test\"", string(sig.Notations[0].Value)) |
| 174 |
} |
| 175 |
if signer == nil { |
| 176 |
t.Errorf("signer is nil") |
| 177 |
return |
| 178 |
} |
| 179 |
if signer.PrimaryKey.KeyId != testKey1KeyId { |
| 180 |
t.Errorf("wrong signer: got %x, expected %x", signer.PrimaryKey.KeyId, testKey1KeyId) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestNewEntity(t *testing.T) { |
| 185 |
// Check bit-length with no config. |
| 186 |
e, err := NewEntity("Test User", "test", "[email protected]", nil) |
| 187 |
if err != nil { |
| 188 |
t.Errorf("failed to create entity: %s", err) |
| 189 |
return |
| 190 |
} |
| 191 |
bl, err := e.PrimaryKey.BitLength() |
| 192 |
if err != nil { |
| 193 |
t.Errorf("failed to find bit length: %s", err) |
| 194 |
} |
| 195 |
defaultRSAKeyBits := 2048 |
| 196 |
if int(bl) != defaultRSAKeyBits { |
| 197 |
t.Errorf("BitLength %v, expected %v", int(bl), defaultRSAKeyBits) |
| 198 |
} |
| 199 |
|
| 200 |
// Check bit-length with a config. |
| 201 |
cfg := &packet.Config{RSABits: 1024} |
| 202 |
e, err = NewEntity("Test User", "test", "[email protected]", cfg) |
| 203 |
if err != nil { |
| 204 |
t.Errorf("failed to create entity: %s", err) |
| 205 |
return |
| 206 |
} |
| 207 |
bl, err = e.PrimaryKey.BitLength() |
| 208 |
if err != nil { |
| 209 |
t.Errorf("failed to find bit length: %s", err) |
| 210 |
} |
| 211 |
if int(bl) != cfg.RSABits { |
| 212 |
t.Errorf("BitLength %v, expected %v", bl, cfg.RSABits) |
| 213 |
} |
| 214 |
|
| 215 |
keySerializeConfig := &packet.Config{ |
| 216 |
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), |
| 217 |
} |
| 218 |
w := bytes.NewBuffer(nil) |
| 219 |
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { |
| 220 |
t.Errorf("failed to serialize entity: %s", err) |
| 221 |
return |
| 222 |
} |
| 223 |
serialized := w.Bytes() |
| 224 |
|
| 225 |
el, err := ReadKeyRing(w) |
| 226 |
if err != nil { |
| 227 |
t.Errorf("failed to rephrase entity: %s", err) |
| 228 |
return |
| 229 |
} |
| 230 |
|
| 231 |
if len(el) != 1 { |
| 232 |
t.Errorf("wrong number of entities found, got %d, want 1", len(el)) |
| 233 |
} |
| 234 |
|
| 235 |
w = bytes.NewBuffer(nil) |
| 236 |
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { |
| 237 |
t.Errorf("failed to serialize entity second time: %s", err) |
| 238 |
return |
| 239 |
} |
| 240 |
|
| 241 |
if !bytes.Equal(w.Bytes(), serialized) { |
| 242 |
t.Errorf("results differed") |
| 243 |
} |
| 244 |
|
| 245 |
if err := e.PrivateKey.Encrypt([]byte("password")); err != nil { |
| 246 |
t.Errorf("failed to encrypt private key: %s", err) |
| 247 |
} |
| 248 |
|
| 249 |
if err := e.PrivateKey.Decrypt([]byte("password")); err != nil { |
| 250 |
t.Errorf("failed to decrypt private key: %s", err) |
| 251 |
} |
| 252 |
|
| 253 |
w = bytes.NewBuffer(nil) |
| 254 |
if err := e.SerializePrivate(w, keySerializeConfig); err != nil { |
| 255 |
t.Errorf("failed to serialize after encryption round: %s", err) |
| 256 |
return |
| 257 |
} |
| 258 |
|
| 259 |
_, err = ReadKeyRing(w) |
| 260 |
if err != nil { |
| 261 |
t.Errorf("failed to re-parse entity after encryption round: %s", err) |
| 262 |
return |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestEncryptWithCompression(t *testing.T) { |
| 267 |
kring, _ := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 268 |
passphrase := []byte("passphrase") |
| 269 |
for _, entity := range kring { |
| 270 |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { |
| 271 |
err := entity.PrivateKey.Decrypt(passphrase) |
| 272 |
if err != nil { |
| 273 |
t.Errorf("failed to decrypt key: %s", err) |
| 274 |
} |
| 275 |
} |
| 276 |
for _, subkey := range entity.Subkeys { |
| 277 |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { |
| 278 |
err := subkey.PrivateKey.Decrypt(passphrase) |
| 279 |
if err != nil { |
| 280 |
t.Errorf("failed to decrypt subkey: %s", err) |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
buf := new(bytes.Buffer) |
| 287 |
var config = &packet.Config{ |
| 288 |
DefaultCompressionAlgo: packet.CompressionZIP, |
| 289 |
CompressionConfig: &packet.CompressionConfig{Level: -1}, |
| 290 |
} |
| 291 |
w, err := Encrypt(buf, kring[:1], nil, nil /* no hints */, config) |
| 292 |
if err != nil { |
| 293 |
t.Errorf("error in encrypting plaintext: %s", err) |
| 294 |
return |
| 295 |
} |
| 296 |
message := []byte("hello world") |
| 297 |
_, err = w.Write(message) |
| 298 |
|
| 299 |
if err != nil { |
| 300 |
t.Errorf("error writing plaintext: %s", err) |
| 301 |
return |
| 302 |
} |
| 303 |
err = w.Close() |
| 304 |
if err != nil { |
| 305 |
t.Errorf("error closing WriteCloser: %s", err) |
| 306 |
return |
| 307 |
} |
| 308 |
err = checkCompression(buf, kring[:1]) |
| 309 |
if err != nil { |
| 310 |
t.Errorf("compression check failed: %s", err) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
func TestSymmetricEncryption(t *testing.T) { |
| 315 |
modesS2K := map[string]s2k.Mode{ |
| 316 |
"Iterated": s2k.IteratedSaltedS2K, |
| 317 |
"Argon2": s2k.Argon2S2K, |
| 318 |
} |
| 319 |
for s2kName, s2ktype := range modesS2K { |
| 320 |
t.Run(s2kName, func(t *testing.T) { |
| 321 |
config := &packet.Config{ |
| 322 |
S2KConfig: &s2k.Config{S2KMode: s2ktype}, |
| 323 |
} |
| 324 |
buf := new(bytes.Buffer) |
| 325 |
plaintext, err := SymmetricallyEncrypt(buf, []byte("testing"), nil, config) |
| 326 |
if err != nil { |
| 327 |
t.Errorf("error writing headers: %s", err) |
| 328 |
return |
| 329 |
} |
| 330 |
message := []byte("hello world\n") |
| 331 |
_, err = plaintext.Write(message) |
| 332 |
if err != nil { |
| 333 |
t.Errorf("error writing to plaintext writer: %s", err) |
| 334 |
} |
| 335 |
err = plaintext.Close() |
| 336 |
if err != nil { |
| 337 |
t.Errorf("error closing plaintext writer: %s", err) |
| 338 |
} |
| 339 |
|
| 340 |
md, err := ReadMessage(buf, nil, func(keys []Key, symmetric bool) ([]byte, error) { |
| 341 |
return []byte("testing"), nil |
| 342 |
}, nil) |
| 343 |
if err != nil { |
| 344 |
t.Errorf("error rereading message: %s", err) |
| 345 |
} |
| 346 |
messageBuf := bytes.NewBuffer(nil) |
| 347 |
_, err = io.Copy(messageBuf, md.UnverifiedBody) |
| 348 |
if err != nil { |
| 349 |
t.Errorf("error rereading message: %s", err) |
| 350 |
} |
| 351 |
if !bytes.Equal(message, messageBuf.Bytes()) { |
| 352 |
t.Errorf("recovered message incorrect got '%s', want '%s'", messageBuf.Bytes(), message) |
| 353 |
} |
| 354 |
}) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
func TestSymmetricEncryptionSEIPDv2RandomizeSlow(t *testing.T) { |
| 359 |
modesS2K := map[int]s2k.Mode{ |
| 360 |
0: s2k.IteratedSaltedS2K, |
| 361 |
1: s2k.Argon2S2K, |
| 362 |
} |
| 363 |
aeadConf := packet.AEADConfig{ |
| 364 |
DefaultMode: aeadModes[mathrand.Intn(len(aeadModes))], |
| 365 |
} |
| 366 |
config := &packet.Config{AEADConfig: &aeadConf, S2KConfig: &s2k.Config{S2KMode: modesS2K[mathrand.Intn(2)]}} |
| 367 |
buf := new(bytes.Buffer) |
| 368 |
passphrase := make([]byte, mathrand.Intn(maxPassLen)) |
| 369 |
_, err := rand.Read(passphrase) |
| 370 |
if err != nil { |
| 371 |
panic(err) |
| 372 |
} |
| 373 |
plaintext, err := SymmetricallyEncrypt(buf, passphrase, nil, config) |
| 374 |
if err != nil { |
| 375 |
t.Errorf("error writing headers: %s", err) |
| 376 |
return |
| 377 |
} |
| 378 |
message := make([]byte, mathrand.Intn(maxPlaintextLen)) |
| 379 |
_, errR := rand.Read(message) |
| 380 |
if errR != nil { |
| 381 |
panic(errR) |
| 382 |
} |
| 383 |
_, err = plaintext.Write(message) |
| 384 |
if err != nil { |
| 385 |
t.Errorf("error writing to plaintext writer: %s", err) |
| 386 |
} |
| 387 |
err = plaintext.Close() |
| 388 |
if err != nil { |
| 389 |
t.Errorf("error closing plaintext writer: %s", err) |
| 390 |
} |
| 391 |
|
| 392 |
// Check if the packet is AEADEncrypted |
| 393 |
copiedCiph := make([]byte, len(buf.Bytes())) |
| 394 |
copy(copiedCiph, buf.Bytes()) |
| 395 |
copiedBuf := bytes.NewBuffer(copiedCiph) |
| 396 |
packets := packet.NewReader(copiedBuf) |
| 397 |
// First a SymmetricKeyEncrypted packet |
| 398 |
p, err := packets.Next() |
| 399 |
if err != nil { |
| 400 |
t.Fatal(err) |
| 401 |
} |
| 402 |
switch tp := p.(type) { |
| 403 |
case *packet.SymmetricKeyEncrypted: |
| 404 |
default: |
| 405 |
t.Errorf("Didn't find a SymmetricKeyEncrypted packet (found %T instead)", tp) |
| 406 |
} |
| 407 |
// Then an SymmetricallyEncrypted packet version 2 |
| 408 |
p, err = packets.Next() |
| 409 |
if err != nil { |
| 410 |
t.Fatal(err) |
| 411 |
} |
| 412 |
switch tp := p.(type) { |
| 413 |
case *packet.SymmetricallyEncrypted: |
| 414 |
if tp.Version != 2 { |
| 415 |
t.Errorf("Wrong packet version, expected 2, found %d", tp.Version) |
| 416 |
} |
| 417 |
default: |
| 418 |
t.Errorf("Didn't find an SymmetricallyEncrypted packet (found %T instead)", tp) |
| 419 |
} |
| 420 |
|
| 421 |
promptFunc := func(keys []Key, symmetric bool) ([]byte, error) { |
| 422 |
return passphrase, nil |
| 423 |
} |
| 424 |
md, err := ReadMessage(buf, nil, promptFunc, config) |
| 425 |
if err != nil { |
| 426 |
t.Errorf("error rereading message: %s", err) |
| 427 |
} |
| 428 |
messageBuf := bytes.NewBuffer(nil) |
| 429 |
_, err = io.Copy(messageBuf, md.UnverifiedBody) |
| 430 |
if err != nil { |
| 431 |
t.Errorf("error rereading message: %s", err) |
| 432 |
} |
| 433 |
if !bytes.Equal(message, messageBuf.Bytes()) { |
| 434 |
t.Errorf("recovered message incorrect got '%s', want '%s'", |
| 435 |
messageBuf.Bytes(), message) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
var testEncryptionTests = map[string]struct { |
| 440 |
keyRingHex string |
| 441 |
isSigned bool |
| 442 |
okV6 bool |
| 443 |
}{ |
| 444 |
"Simple": { |
| 445 |
testKeys1And2PrivateHex, |
| 446 |
false, |
| 447 |
true, |
| 448 |
}, |
| 449 |
"Simple_signed": { |
| 450 |
testKeys1And2PrivateHex, |
| 451 |
true, |
| 452 |
true, |
| 453 |
}, |
| 454 |
"DSA_ElGamal": { |
| 455 |
dsaElGamalTestKeysHex, |
| 456 |
false, |
| 457 |
false, |
| 458 |
}, |
| 459 |
"DSA_ElGamal_signed": { |
| 460 |
dsaElGamalTestKeysHex, |
| 461 |
true, |
| 462 |
false, |
| 463 |
}, |
| 464 |
"v6_Ed25519_ML-KEM-768+X25519": { |
| 465 |
v6Ed25519Mlkem768X25519PrivateHex, |
| 466 |
false, |
| 467 |
true, |
| 468 |
}, |
| 469 |
"v6_Ed25519_ML-KEM-768+X25519_signed": { |
| 470 |
v6Ed25519Mlkem768X25519PrivateHex, |
| 471 |
true, |
| 472 |
true, |
| 473 |
}, |
| 474 |
"v4_Ed25519_ML-KEM-768+X25519": { |
| 475 |
v4Ed25519Mlkem768X25519PrivateHex, |
| 476 |
false, |
| 477 |
true, |
| 478 |
}, |
| 479 |
"v4_Ed25519_ML-KEM-768+X25519_signed": { |
| 480 |
v4Ed25519Mlkem768X25519PrivateHex, |
| 481 |
true, |
| 482 |
true, |
| 483 |
}, |
| 484 |
"v4_Ed25519_X25519,ML-KEM-768+X25519": { |
| 485 |
v4Ed25519X25519Mlkem768X25519PrivateHex, |
| 486 |
false, |
| 487 |
true, |
| 488 |
}, |
| 489 |
"v6_ML-DSA-67+Ed25519_ML-KEM-768+X25519_signed": { |
| 490 |
v6Mldsa65Ed25519Mlkem768X25519PrivateHex, |
| 491 |
true, |
| 492 |
true, |
| 493 |
}, |
| 494 |
"v6_ML-DSA-87+Ed448_ML-KEM-1024+X448": { |
| 495 |
v6Mldsa87Ed448Mlkem1024X448PrivateHex, |
| 496 |
false, |
| 497 |
true, |
| 498 |
}, |
| 499 |
"v6_ML-DSA-87+Ed448_ML-KEM-1024+X448_signed": { |
| 500 |
v6Mldsa87Ed448Mlkem1024X448PrivateHex, |
| 501 |
true, |
| 502 |
true, |
| 503 |
}, |
| 504 |
"v6_SLH-DSA-128s_ML-KEM-768+X25519_signed": { |
| 505 |
v6Slhdsa128sMlkem768X25519PrivateHex, |
| 506 |
true, |
| 507 |
true, |
| 508 |
}, |
| 509 |
"v6_SLH-DSA-128f_ML-KEM-768+X25519_signed": { |
| 510 |
v6Slhdsa128fMlkem768X25519PrivateHex, |
| 511 |
true, |
| 512 |
true, |
| 513 |
}, |
| 514 |
"v6_SLH-DSA-256s_ML-KEM-1024+X448_signed": { |
| 515 |
v6Slhdsa256sMlkem1024X448PrivateHex, |
| 516 |
true, |
| 517 |
true, |
| 518 |
}, |
| 519 |
} |
| 520 |
|
| 521 |
func TestEncryption(t *testing.T) { |
| 522 |
for name, test := range testEncryptionTests { |
| 523 |
t.Run(name, func(t *testing.T) { |
| 524 |
kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) |
| 525 |
|
| 526 |
passphrase := []byte("passphrase") |
| 527 |
for _, entity := range kring { |
| 528 |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { |
| 529 |
err := entity.PrivateKey.Decrypt(passphrase) |
| 530 |
if err != nil { |
| 531 |
t.Fatal("Failed to decrypt key") |
| 532 |
} |
| 533 |
} |
| 534 |
for _, subkey := range entity.Subkeys { |
| 535 |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { |
| 536 |
err := subkey.PrivateKey.Decrypt(passphrase) |
| 537 |
if err != nil { |
| 538 |
t.Fatal("Failed to decrypt subkey") |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
var signed *Entity |
| 545 |
if test.isSigned { |
| 546 |
signed = kring[0] |
| 547 |
} |
| 548 |
|
| 549 |
buf := new(bytes.Buffer) |
| 550 |
// randomized compression test |
| 551 |
compAlgos := []packet.CompressionAlgo{ |
| 552 |
packet.CompressionNone, |
| 553 |
packet.CompressionZIP, |
| 554 |
packet.CompressionZLIB, |
| 555 |
} |
| 556 |
compAlgo := compAlgos[mathrand.Intn(len(compAlgos))] |
| 557 |
level := mathrand.Intn(11) - 1 |
| 558 |
compConf := &packet.CompressionConfig{Level: level} |
| 559 |
var config = &packet.Config{ |
| 560 |
DefaultCompressionAlgo: compAlgo, |
| 561 |
CompressionConfig: compConf, |
| 562 |
DefaultCipher: packet.CipherAES256, |
| 563 |
} |
| 564 |
|
| 565 |
// Flip coin to enable AEAD mode |
| 566 |
if test.okV6 && (mathrand.Int()%2 == 0) { |
| 567 |
aeadConf := packet.AEADConfig{ |
| 568 |
DefaultMode: aeadModes[mathrand.Intn(len(aeadModes))], |
| 569 |
} |
| 570 |
config.AEADConfig = &aeadConf |
| 571 |
} |
| 572 |
|
| 573 |
w, err := Encrypt(buf, kring[:1], signed, nil /* no hints */, config) |
| 574 |
if err != nil { |
| 575 |
t.Fatalf("Error in Encrypt: %s", err) |
| 576 |
} |
| 577 |
|
| 578 |
const message = "testing" |
| 579 |
_, err = w.Write([]byte(message)) |
| 580 |
if err != nil { |
| 581 |
t.Fatalf("Error writing plaintext: %s", err) |
| 582 |
} |
| 583 |
err = w.Close() |
| 584 |
if err != nil { |
| 585 |
t.Fatalf("Error closing WriteCloser: %s", err) |
| 586 |
} |
| 587 |
|
| 588 |
md, err := ReadMessage(buf, kring, nil /* no prompt */, config) |
| 589 |
if err != nil { |
| 590 |
t.Fatalf("Error reading message: %s", err) |
| 591 |
} |
| 592 |
|
| 593 |
testTime, _ := time.Parse("2006-01-02", "2026-01-01") |
| 594 |
if test.isSigned { |
| 595 |
signKey, ok := kring[0].SigningKey(testTime) |
| 596 |
if !ok { |
| 597 |
t.Fatalf("#%s: No signing key found at %v", name, testTime) |
| 598 |
} |
| 599 |
expectedKeyId := signKey.PublicKey.KeyId |
| 600 |
if md.SignedByKeyId != expectedKeyId { |
| 601 |
t.Errorf("Message signed by wrong key id, got: %v, want: %v", *md.SignedBy, expectedKeyId) |
| 602 |
} |
| 603 |
if md.SignedBy == nil { |
| 604 |
t.Error("#Failed to find the signing Entity") |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
plaintext, err := io.ReadAll(md.UnverifiedBody) |
| 609 |
if err != nil { |
| 610 |
t.Fatalf("Error reading encrypted contents: %s", err) |
| 611 |
} |
| 612 |
|
| 613 |
encryptKey, ok := kring[0].EncryptionKey(testTime) |
| 614 |
if !ok { |
| 615 |
t.Fatalf("#%s: No encryption key found at %v", name, testTime) |
| 616 |
} |
| 617 |
expectedKeyId := encryptKey.PublicKey.KeyId |
| 618 |
if len(md.EncryptedToKeyIds) != 1 || md.EncryptedToKeyIds[0] != expectedKeyId { |
| 619 |
t.Errorf("Expected message to be encrypted to %v, but got %#v", expectedKeyId, md.EncryptedToKeyIds) |
| 620 |
} |
| 621 |
|
| 622 |
if string(plaintext) != message { |
| 623 |
t.Errorf("#Got: %s, want: %s", string(plaintext), message) |
| 624 |
} |
| 625 |
|
| 626 |
if test.isSigned { |
| 627 |
if md.SignatureError != nil { |
| 628 |
t.Errorf("Signature error: %s", md.SignatureError) |
| 629 |
} |
| 630 |
if md.Signature == nil { |
| 631 |
t.Error("Signature missing") |
| 632 |
} |
| 633 |
} |
| 634 |
}) |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
var testSigningTests = []struct { |
| 639 |
keyRingHex string |
| 640 |
}{ |
| 641 |
{ |
| 642 |
testKeys1And2PrivateHex, |
| 643 |
}, |
| 644 |
{ |
| 645 |
dsaElGamalTestKeysHex, |
| 646 |
}, |
| 647 |
{ |
| 648 |
ed25519wX25519Key, |
| 649 |
}, |
| 650 |
} |
| 651 |
|
| 652 |
func TestSigning(t *testing.T) { |
| 653 |
for i, test := range testSigningTests { |
| 654 |
kring, _ := ReadKeyRing(readerFromHex(test.keyRingHex)) |
| 655 |
|
| 656 |
passphrase := []byte("passphrase") |
| 657 |
for _, entity := range kring { |
| 658 |
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { |
| 659 |
err := entity.PrivateKey.Decrypt(passphrase) |
| 660 |
if err != nil { |
| 661 |
t.Errorf("#%d: failed to decrypt key", i) |
| 662 |
} |
| 663 |
} |
| 664 |
for _, subkey := range entity.Subkeys { |
| 665 |
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { |
| 666 |
err := subkey.PrivateKey.Decrypt(passphrase) |
| 667 |
if err != nil { |
| 668 |
t.Errorf("#%d: failed to decrypt subkey", i) |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
signed := kring[0] |
| 675 |
|
| 676 |
buf := new(bytes.Buffer) |
| 677 |
w, err := Sign(buf, signed, nil /* no hints */, nil) |
| 678 |
if err != nil { |
| 679 |
t.Errorf("#%d: error in Sign: %s", i, err) |
| 680 |
continue |
| 681 |
} |
| 682 |
|
| 683 |
const message = "testing" |
| 684 |
_, err = w.Write([]byte(message)) |
| 685 |
if err != nil { |
| 686 |
t.Errorf("#%d: error writing plaintext: %s", i, err) |
| 687 |
continue |
| 688 |
} |
| 689 |
err = w.Close() |
| 690 |
if err != nil { |
| 691 |
t.Errorf("#%d: error closing WriteCloser: %s", i, err) |
| 692 |
continue |
| 693 |
} |
| 694 |
|
| 695 |
md, err := ReadMessage(buf, kring, nil /* no prompt */, nil) |
| 696 |
if err != nil { |
| 697 |
t.Errorf("#%d: error reading message: %s", i, err) |
| 698 |
continue |
| 699 |
} |
| 700 |
|
| 701 |
testTime, _ := time.Parse("2006-01-02", "2022-12-01") |
| 702 |
signKey, _ := kring[0].SigningKey(testTime) |
| 703 |
expectedKeyId := signKey.PublicKey.KeyId |
| 704 |
if md.SignedByKeyId != expectedKeyId { |
| 705 |
t.Errorf("#%d: message signed by wrong key id, got: %v, want: %v", i, *md.SignedBy, expectedKeyId) |
| 706 |
} |
| 707 |
if md.SignedBy == nil { |
| 708 |
t.Errorf("#%d: failed to find the signing Entity", i) |
| 709 |
} |
| 710 |
|
| 711 |
plaintext, err := io.ReadAll(md.UnverifiedBody) |
| 712 |
if err != nil { |
| 713 |
t.Errorf("#%d: error reading contents: %v", i, err) |
| 714 |
continue |
| 715 |
} |
| 716 |
|
| 717 |
if string(plaintext) != message { |
| 718 |
t.Errorf("#%d: got: %q, want: %q", i, plaintext, message) |
| 719 |
} |
| 720 |
|
| 721 |
if md.SignatureError != nil { |
| 722 |
t.Errorf("#%d: signature error: %q", i, md.SignatureError) |
| 723 |
} |
| 724 |
if md.Signature == nil { |
| 725 |
t.Error("signature missing") |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
func checkCompression(r io.Reader, keyring KeyRing) (err error) { |
| 731 |
var p packet.Packet |
| 732 |
|
| 733 |
var symKeys []*packet.SymmetricKeyEncrypted |
| 734 |
var pubKeys []keyEnvelopePair |
| 735 |
// Integrity protected encrypted packet: SymmetricallyEncrypted or AEADEncrypted |
| 736 |
var edp packet.EncryptedDataPacket |
| 737 |
|
| 738 |
packets := packet.NewReader(r) |
| 739 |
config := &packet.Config{} |
| 740 |
|
| 741 |
// The message, if encrypted, starts with a number of packets |
| 742 |
// containing an encrypted decryption key. The decryption key is either |
| 743 |
// encrypted to a public key, or with a passphrase. This loop |
| 744 |
// collects these packets. |
| 745 |
ParsePackets: |
| 746 |
for { |
| 747 |
p, err = packets.Next() |
| 748 |
if err != nil { |
| 749 |
return err |
| 750 |
} |
| 751 |
switch p := p.(type) { |
| 752 |
case *packet.EncryptedKey: |
| 753 |
// This packet contains the decryption key encrypted to a public key. |
| 754 |
switch p.Algo { |
| 755 |
case packet.PubKeyAlgoRSA, packet.PubKeyAlgoRSAEncryptOnly, packet.PubKeyAlgoElGamal, packet.PubKeyAlgoECDH, |
| 756 |
packet.PubKeyAlgoMlkem768X25519, packet.PubKeyAlgoMlkem1024X448: |
| 757 |
break |
| 758 |
default: |
| 759 |
continue |
| 760 |
} |
| 761 |
var keys []Key |
| 762 |
if p.KeyId == 0 { |
| 763 |
keys = keyring.DecryptionKeys() |
| 764 |
} else { |
| 765 |
keys = keyring.KeysById(p.KeyId) |
| 766 |
} |
| 767 |
for _, k := range keys { |
| 768 |
pubKeys = append(pubKeys, keyEnvelopePair{k, p}) |
| 769 |
} |
| 770 |
case *packet.SymmetricallyEncrypted, *packet.AEADEncrypted: |
| 771 |
edp = p.(packet.EncryptedDataPacket) |
| 772 |
break ParsePackets |
| 773 |
case *packet.Compressed, *packet.LiteralData, *packet.OnePassSignature: |
| 774 |
// This message isn't encrypted. |
| 775 |
return errors.StructuralError("message not encrypted") |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
var candidates []Key |
| 780 |
var decrypted io.ReadCloser |
| 781 |
|
| 782 |
// Now that we have the list of encrypted keys we need to decrypt at |
| 783 |
// least one of them or, if we cannot, we need to call the prompt |
| 784 |
// function so that it can decrypt a key or give us a passphrase. |
| 785 |
FindKey: |
| 786 |
for { |
| 787 |
// See if any of the keys already have a private key available |
| 788 |
candidates = candidates[:0] |
| 789 |
candidateFingerprints := make(map[string]bool) |
| 790 |
|
| 791 |
for _, pk := range pubKeys { |
| 792 |
if pk.key.PrivateKey == nil { |
| 793 |
continue |
| 794 |
} |
| 795 |
if !pk.key.PrivateKey.Encrypted { |
| 796 |
if len(pk.encryptedKey.Key) == 0 { |
| 797 |
errDec := pk.encryptedKey.Decrypt(pk.key.PrivateKey, config) |
| 798 |
if errDec != nil { |
| 799 |
continue |
| 800 |
} |
| 801 |
} |
| 802 |
// Try to decrypt symmetrically encrypted |
| 803 |
decrypted, err = edp.Decrypt(pk.encryptedKey.CipherFunc, pk.encryptedKey.Key) |
| 804 |
if err != nil && err != errors.ErrKeyIncorrect { |
| 805 |
return err |
| 806 |
} |
| 807 |
if decrypted != nil { |
| 808 |
break FindKey |
| 809 |
} |
| 810 |
} else { |
| 811 |
fpr := string(pk.key.PublicKey.Fingerprint[:]) |
| 812 |
if v := candidateFingerprints[fpr]; v { |
| 813 |
continue |
| 814 |
} |
| 815 |
candidates = append(candidates, pk.key) |
| 816 |
candidateFingerprints[fpr] = true |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
if len(candidates) == 0 && len(symKeys) == 0 { |
| 821 |
return errors.ErrKeyIncorrect |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
decPackets, err := packet.Read(decrypted) |
| 826 |
if err != nil { |
| 827 |
return err |
| 828 |
} |
| 829 |
_, ok := decPackets.(*packet.Compressed) |
| 830 |
if !ok { |
| 831 |
return errors.StructuralError("No compressed packets found") |
| 832 |
} |
| 833 |
return nil |
| 834 |
} |