| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"crypto/dsa" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/rsa" |
| 9 |
"fmt" |
| 10 |
"math/big" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/ProtonMail/go-crypto/openpgp/armor" |
| 17 |
"github.com/ProtonMail/go-crypto/openpgp/ecdh" |
| 18 |
"github.com/ProtonMail/go-crypto/openpgp/ecdsa" |
| 19 |
"github.com/ProtonMail/go-crypto/openpgp/eddsa" |
| 20 |
"github.com/ProtonMail/go-crypto/openpgp/elgamal" |
| 21 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 22 |
"github.com/ProtonMail/go-crypto/openpgp/internal/algorithm" |
| 23 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 24 |
"github.com/ProtonMail/go-crypto/openpgp/s2k" |
| 25 |
) |
| 26 |
|
| 27 |
var hashes = []crypto.Hash{ |
| 28 |
crypto.SHA1, |
| 29 |
crypto.SHA224, |
| 30 |
crypto.SHA256, |
| 31 |
crypto.SHA384, |
| 32 |
crypto.SHA512, |
| 33 |
crypto.SHA3_256, |
| 34 |
crypto.SHA3_512, |
| 35 |
} |
| 36 |
|
| 37 |
var ciphers = []packet.CipherFunction{ |
| 38 |
packet.Cipher3DES, |
| 39 |
packet.CipherCAST5, |
| 40 |
packet.CipherAES128, |
| 41 |
packet.CipherAES192, |
| 42 |
packet.CipherAES256, |
| 43 |
} |
| 44 |
|
| 45 |
var aeadModes = []packet.AEADMode{ |
| 46 |
packet.AEADModeOCB, |
| 47 |
packet.AEADModeEAX, |
| 48 |
packet.AEADModeGCM, |
| 49 |
} |
| 50 |
|
| 51 |
var allowAllAlgorithmsConfig = packet.Config{ |
| 52 |
RejectPublicKeyAlgorithms: map[packet.PublicKeyAlgorithm]bool{}, |
| 53 |
RejectCurves: map[packet.Curve]bool{}, |
| 54 |
RejectMessageHashAlgorithms: map[crypto.Hash]bool{}, |
| 55 |
MinRSABits: 512, |
| 56 |
} |
| 57 |
|
| 58 |
func TestKeyExpiry(t *testing.T) { |
| 59 |
kring, err := ReadKeyRing(readerFromHex(expiringKeyHex)) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
entity := kring[0] |
| 64 |
|
| 65 |
const timeFormat = "2006-01-02" |
| 66 |
time1, _ := time.Parse(timeFormat, "2013-07-02") |
| 67 |
|
| 68 |
// The expiringKeyHex key is structured as: |
| 69 |
// |
| 70 |
// pub 1024R/5E237D8C created: 2013-07-01 expires: 2013-07-31 usage: SC |
| 71 |
// sub 1024R/1ABB25A0 created: 2013-07-01 23:11:07 +0200 CEST expires: 2013-07-08 usage: E |
| 72 |
// sub 1024R/96A672F5 created: 2013-07-01 23:11:23 +0200 CEST expires: 2013-07-31 usage: E |
| 73 |
// |
| 74 |
// So this should select the newest, non-expired encryption key. |
| 75 |
key, ok := entity.EncryptionKey(time1, nil) |
| 76 |
if !ok { |
| 77 |
t.Fatal("No encryption key found") |
| 78 |
} |
| 79 |
if id, expected := key.PublicKey.KeyIdShortString(), "CD3D39FF"; id != expected { |
| 80 |
t.Errorf("Expected key %s at time %s, but got key %s", expected, time1.Format(timeFormat), id) |
| 81 |
} |
| 82 |
|
| 83 |
// Once the first encryption subkey has expired, the second should be |
| 84 |
// selected. |
| 85 |
time2, _ := time.Parse(timeFormat, "2013-07-09") |
| 86 |
key, _ = entity.EncryptionKey(time2, nil) |
| 87 |
if id, expected := key.PublicKey.KeyIdShortString(), "CD3D39FF"; id != expected { |
| 88 |
t.Errorf("Expected key %s at time %s, but got key %s", expected, time2.Format(timeFormat), id) |
| 89 |
} |
| 90 |
|
| 91 |
// Once all the keys have expired, nothing should be returned. |
| 92 |
time3, _ := time.Parse(timeFormat, "2013-08-01") |
| 93 |
if key, ok := entity.EncryptionKey(time3, nil); ok { |
| 94 |
t.Errorf("Expected no key at time %s, but got key %s", time3.Format(timeFormat), key.PublicKey.KeyIdShortString()) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// https://tests.sequoia-pgp.org/#Certificate_expiration |
| 99 |
// P _ U f |
| 100 |
func TestExpiringPrimaryUIDKey(t *testing.T) { |
| 101 |
// P _ U f |
| 102 |
kring, err := ReadArmoredKeyRing(bytes.NewBufferString((expiringPrimaryUIDKey))) |
| 103 |
if err != nil { |
| 104 |
t.Fatal(err) |
| 105 |
} |
| 106 |
entity := kring[0] |
| 107 |
|
| 108 |
const timeFormat string = "2006-01-02" |
| 109 |
const expectedKeyID string = "015E7330" |
| 110 |
|
| 111 |
// Before the primary UID has expired, the primary key should be returned. |
| 112 |
time1, err := time.Parse(timeFormat, "2022-02-05") |
| 113 |
if err != nil { |
| 114 |
t.Fatal(err) |
| 115 |
} |
| 116 |
key, found := entity.SigningKey(time1, nil) |
| 117 |
if !found { |
| 118 |
t.Errorf("Signing subkey %s not found at time %s", expectedKeyID, time1.Format(timeFormat)) |
| 119 |
} else if observedKeyID := key.PublicKey.KeyIdShortString(); observedKeyID != expectedKeyID { |
| 120 |
t.Errorf("Expected key %s at time %s, but got key %s", expectedKeyID, time1.Format(timeFormat), observedKeyID) |
| 121 |
} |
| 122 |
|
| 123 |
// After the primary UID has expired, nothing should be returned. |
| 124 |
time2, err := time.Parse(timeFormat, "2022-02-06") |
| 125 |
if err != nil { |
| 126 |
t.Fatal(err) |
| 127 |
} |
| 128 |
if key, ok := entity.SigningKey(time2, nil); ok { |
| 129 |
t.Errorf("Expected no key at time %s, but got key %s", time2.Format(timeFormat), key.PublicKey.KeyIdShortString()) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestReturnFirstUnexpiredSigningSubkey(t *testing.T) { |
| 134 |
// Make a master key. |
| 135 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 136 |
if err != nil { |
| 137 |
t.Fatal(err) |
| 138 |
} |
| 139 |
|
| 140 |
// First signing subkey does not expire. |
| 141 |
err = entity.AddSigningSubkey(nil) |
| 142 |
if err != nil { |
| 143 |
t.Fatal(err) |
| 144 |
} |
| 145 |
// Get the first signing subkey (added after the default encryption subkey). |
| 146 |
subkey1 := entity.Subkeys[1] |
| 147 |
|
| 148 |
// Second signing subkey expires in a day. |
| 149 |
err = entity.AddSigningSubkey(&packet.Config{ |
| 150 |
Time: func() time.Time { |
| 151 |
return time.Now().Add(1 * time.Second) |
| 152 |
}, |
| 153 |
KeyLifetimeSecs: 24 * 60 * 60, |
| 154 |
}) |
| 155 |
if err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
// Get the second signing subkey. |
| 159 |
subkey2 := entity.Subkeys[2] |
| 160 |
|
| 161 |
// Before second signing subkey has expired, it should be returned. |
| 162 |
time1 := time.Now().Add(2 * time.Second) |
| 163 |
expected := subkey2.PublicKey.KeyIdShortString() |
| 164 |
subkey, found := entity.SigningKey(time1, nil) |
| 165 |
if !found { |
| 166 |
t.Errorf("Signing subkey %s not found at time %s", expected, time1.Format(time.UnixDate)) |
| 167 |
} |
| 168 |
observed := subkey.PublicKey.KeyIdShortString() |
| 169 |
if observed != expected { |
| 170 |
t.Errorf("Expected key %s at time %s, but got key %s", expected, time1.Format(time.UnixDate), observed) |
| 171 |
} |
| 172 |
|
| 173 |
// After the second signing subkey has expired, the first one should be returned. |
| 174 |
time2 := time1.AddDate(0, 0, 2) |
| 175 |
expected = subkey1.PublicKey.KeyIdShortString() |
| 176 |
subkey, found = entity.SigningKey(time2, nil) |
| 177 |
if !found { |
| 178 |
t.Errorf("Signing subkey %s not found at time %s", expected, time2.Format(time.UnixDate)) |
| 179 |
} |
| 180 |
observed = subkey.PublicKey.KeyIdShortString() |
| 181 |
if observed != expected { |
| 182 |
t.Errorf("Expected key %s at time %s, but got key %s", expected, time2.Format(time.UnixDate), observed) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func TestSignatureExpiry(t *testing.T) { |
| 187 |
// Make a master key, and attach it to a keyring. |
| 188 |
var keyring EntityList |
| 189 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 190 |
if err != nil { |
| 191 |
t.Fatal(err) |
| 192 |
} |
| 193 |
keyring = append(keyring, entity) |
| 194 |
|
| 195 |
// Make a signature that never expires. |
| 196 |
var signatureWriter1 bytes.Buffer |
| 197 |
const input string = "Hello, world!" |
| 198 |
message := strings.NewReader(input) |
| 199 |
err = ArmoredDetachSign(&signatureWriter1, []*Entity{entity}, message, nil) |
| 200 |
if err != nil { |
| 201 |
t.Fatal(err) |
| 202 |
} |
| 203 |
|
| 204 |
// Make a signature that expires in a day. |
| 205 |
var signatureWriter2 bytes.Buffer |
| 206 |
message = strings.NewReader(input) |
| 207 |
err = ArmoredDetachSign(&signatureWriter2, []*Entity{entity}, message, &SignParams{ |
| 208 |
Config: &packet.Config{ |
| 209 |
SigLifetimeSecs: 24 * 60 * 60, |
| 210 |
}, |
| 211 |
}) |
| 212 |
if err != nil { |
| 213 |
t.Fatal(err) |
| 214 |
} |
| 215 |
|
| 216 |
// Make a time that is day after tomorrow. |
| 217 |
futureTime := func() time.Time { |
| 218 |
return time.Now().AddDate(0, 0, 2) |
| 219 |
} |
| 220 |
|
| 221 |
// Make a signature that was created in the future. |
| 222 |
var signatureWriter3 bytes.Buffer |
| 223 |
message = strings.NewReader(input) |
| 224 |
err = ArmoredDetachSign(&signatureWriter3, []*Entity{entity}, message, &SignParams{ |
| 225 |
Config: &packet.Config{ |
| 226 |
Time: futureTime, |
| 227 |
}, |
| 228 |
}) |
| 229 |
if err != nil { |
| 230 |
t.Fatal(err) |
| 231 |
} |
| 232 |
|
| 233 |
// Check that the first signature has not expired day after tomorrow. |
| 234 |
message = strings.NewReader(input) |
| 235 |
signatureReader1 := strings.NewReader(signatureWriter1.String()) |
| 236 |
_, _, err = VerifyArmoredDetachedSignature(keyring, message, signatureReader1, &packet.Config{ |
| 237 |
Time: futureTime, |
| 238 |
}) |
| 239 |
if err != nil { |
| 240 |
t.Fatal(err) |
| 241 |
} |
| 242 |
|
| 243 |
// Check that the second signature has expired day after tomorrow. |
| 244 |
message = strings.NewReader(input) |
| 245 |
signatureReader2 := strings.NewReader(signatureWriter2.String()) |
| 246 |
const expectedErr string = "openpgp: signature expired" |
| 247 |
_, _, observedErr := VerifyArmoredDetachedSignature(keyring, message, signatureReader2, &packet.Config{ |
| 248 |
Time: futureTime, |
| 249 |
}) |
| 250 |
if observedErr.Error() != expectedErr { |
| 251 |
t.Errorf("Expected error '%s', but got error '%s'", expectedErr, observedErr) |
| 252 |
} |
| 253 |
|
| 254 |
// Check that the third signature is also considered expired even now. |
| 255 |
message = strings.NewReader(input) |
| 256 |
signatureReader3 := strings.NewReader(signatureWriter3.String()) |
| 257 |
_, _, observedErr = VerifyArmoredDetachedSignature(keyring, message, signatureReader3, nil) |
| 258 |
if observedErr.Error() != expectedErr { |
| 259 |
t.Errorf("Expected error '%s', but got error '%s'", expectedErr, observedErr) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func TestMissingCrossSignature(t *testing.T) { |
| 264 |
// This public key has a signing subkey, but the subkey does not |
| 265 |
// contain a cross-signature. |
| 266 |
keys, _ := ReadArmoredKeyRing(bytes.NewBufferString(missingCrossSignatureKey)) |
| 267 |
var config *packet.Config |
| 268 |
_, err := keys[0].Subkeys[0].Verify(config.Now(), config) |
| 269 |
if err == nil { |
| 270 |
t.Fatal("Failed to detect error in keyring with missing cross signature") |
| 271 |
} |
| 272 |
structural, ok := err.(errors.StructuralError) |
| 273 |
if !ok { |
| 274 |
t.Fatalf("Unexpected class of error: %T. Wanted StructuralError", err) |
| 275 |
} |
| 276 |
const expectedMsg = "no valid binding signature found for subkey" |
| 277 |
if !strings.Contains(string(structural), expectedMsg) { |
| 278 |
t.Fatalf("Unexpected error: %q. Expected it to contain %q", err, expectedMsg) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestInvalidCrossSignature(t *testing.T) { |
| 283 |
// This public key has a signing subkey, and the subkey has an |
| 284 |
// embedded cross-signature. However, the cross-signature does |
| 285 |
// not correctly validate over the primary and subkey. |
| 286 |
keys, _ := ReadArmoredKeyRing(bytes.NewBufferString(invalidCrossSignatureKey)) |
| 287 |
var config *packet.Config |
| 288 |
_, err := keys[0].Subkeys[0].Verify(config.Now(), config) |
| 289 |
if err == nil { |
| 290 |
t.Fatal("Failed to detect error in keyring with an invalid cross signature") |
| 291 |
} |
| 292 |
structural, ok := err.(errors.StructuralError) |
| 293 |
if !ok { |
| 294 |
t.Fatalf("Unexpected class of error: %T. Wanted StructuralError", err) |
| 295 |
} |
| 296 |
const expectedMsg = "no valid binding signature found for subkey" |
| 297 |
if !strings.Contains(string(structural), expectedMsg) { |
| 298 |
t.Fatalf("Unexpected error: %q. Expected it to contain %q", err, expectedMsg) |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func TestGoodCrossSignature(t *testing.T) { |
| 303 |
// This public key has a signing subkey, and the subkey has an |
| 304 |
// embedded cross-signature which correctly validates over the |
| 305 |
// primary and subkey. |
| 306 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(goodCrossSignatureKey)) |
| 307 |
if err != nil { |
| 308 |
t.Fatal(err) |
| 309 |
} |
| 310 |
if len(keys) != 1 { |
| 311 |
t.Errorf("Failed to accept key with good cross signature, %d", len(keys)) |
| 312 |
} |
| 313 |
if len(keys[0].Subkeys) != 1 { |
| 314 |
t.Errorf("Failed to accept good subkey, %d", len(keys[0].Subkeys)) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
func TestRevokedUserID(t *testing.T) { |
| 319 |
// This key contains 2 UIDs, one of which is revoked and has no valid self-signature: |
| 320 |
// [ultimate] (1) Golang Gopher <[email protected]> |
| 321 |
// [ revoked] (2) Golang Gopher <[email protected]> |
| 322 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(revokedUserIDKey)) |
| 323 |
if err != nil { |
| 324 |
t.Fatal(err) |
| 325 |
} |
| 326 |
|
| 327 |
if len(keys) != 1 { |
| 328 |
t.Fatal("Failed to read key with a revoked user id") |
| 329 |
} |
| 330 |
|
| 331 |
identities := keys[0].Identities |
| 332 |
|
| 333 |
if numIdentities, numExpected := len(identities), 2; numIdentities != numExpected { |
| 334 |
t.Errorf("obtained %d identities, expected %d", numIdentities, numExpected) |
| 335 |
} |
| 336 |
|
| 337 |
firstIdentity, found := identities["Golang Gopher <[email protected]>"] |
| 338 |
if !found { |
| 339 |
t.Errorf("missing first identity") |
| 340 |
} |
| 341 |
|
| 342 |
secondIdentity, found := identities["Golang Gopher <[email protected]>"] |
| 343 |
if !found { |
| 344 |
t.Errorf("missing second identity") |
| 345 |
} |
| 346 |
|
| 347 |
if firstIdentity.Revoked(nil, time.Now(), &packet.Config{}) { |
| 348 |
t.Errorf("expected first identity not to be revoked") |
| 349 |
} |
| 350 |
|
| 351 |
if !secondIdentity.Revoked(nil, time.Now(), &packet.Config{}) { |
| 352 |
t.Errorf("expected second identity to be revoked") |
| 353 |
} |
| 354 |
|
| 355 |
const timeFormat = "2006-01-02" |
| 356 |
time1, _ := time.Parse(timeFormat, "2020-01-01") |
| 357 |
|
| 358 |
if _, found := keys[0].SigningKey(time1, nil); !found { |
| 359 |
t.Errorf("Expected SigningKey to return a signing key when one User IDs is revoked") |
| 360 |
} |
| 361 |
|
| 362 |
if _, found := keys[0].EncryptionKey(time1, nil); !found { |
| 363 |
t.Errorf("Expected EncryptionKey to return an encryption key when one User IDs is revoked") |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
func TestFirstUserIDRevoked(t *testing.T) { |
| 368 |
// Same test as above, but with the User IDs reversed: |
| 369 |
// [ revoked] (1) Golang Gopher <[email protected]> |
| 370 |
// [ultimate] (2) Golang Gopher <[email protected]> |
| 371 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(keyWithFirstUserIDRevoked)) |
| 372 |
if err != nil { |
| 373 |
t.Fatal(err) |
| 374 |
} |
| 375 |
|
| 376 |
if len(keys) != 1 { |
| 377 |
t.Fatal("Failed to read key with a revoked user id") |
| 378 |
} |
| 379 |
|
| 380 |
identities := keys[0].Identities |
| 381 |
|
| 382 |
if numIdentities, numExpected := len(identities), 2; numIdentities != numExpected { |
| 383 |
t.Errorf("obtained %d identities, expected %d", numIdentities, numExpected) |
| 384 |
} |
| 385 |
|
| 386 |
firstIdentity, found := identities["Golang Gopher <[email protected]>"] |
| 387 |
if !found { |
| 388 |
t.Errorf("missing first identity") |
| 389 |
} |
| 390 |
|
| 391 |
secondIdentity, found := identities["Golang Gopher <[email protected]>"] |
| 392 |
if !found { |
| 393 |
t.Errorf("missing second identity") |
| 394 |
} |
| 395 |
|
| 396 |
if !firstIdentity.Revoked(nil, time.Now(), &packet.Config{}) { |
| 397 |
t.Errorf("expected first identity to be revoked") |
| 398 |
} |
| 399 |
|
| 400 |
if secondIdentity.Revoked(nil, time.Now(), &packet.Config{}) { |
| 401 |
t.Errorf("expected second identity not to be revoked") |
| 402 |
} |
| 403 |
|
| 404 |
const timeFormat = "2006-01-02" |
| 405 |
time1, _ := time.Parse(timeFormat, "2020-01-01") |
| 406 |
|
| 407 |
if _, found := keys[0].SigningKey(time1, nil); !found { |
| 408 |
t.Errorf("Expected SigningKey to return a signing key when first User IDs is revoked") |
| 409 |
} |
| 410 |
|
| 411 |
if _, found := keys[0].EncryptionKey(time1, nil); !found { |
| 412 |
t.Errorf("Expected EncryptionKey to return an encryption key when first User IDs is revoked") |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
func TestOnlyUserIDRevoked(t *testing.T) { |
| 417 |
// This key contains 1 UID which is revoked (but also has a self-signature) |
| 418 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(keyWithOnlyUserIDRevoked)) |
| 419 |
if err != nil { |
| 420 |
t.Fatal(err) |
| 421 |
} |
| 422 |
|
| 423 |
if len(keys) != 1 { |
| 424 |
t.Fatal("Failed to read key with a revoked user id") |
| 425 |
} |
| 426 |
|
| 427 |
identities := keys[0].Identities |
| 428 |
|
| 429 |
if numIdentities, numExpected := len(identities), 1; numIdentities != numExpected { |
| 430 |
t.Errorf("obtained %d identities, expected %d", numIdentities, numExpected) |
| 431 |
} |
| 432 |
|
| 433 |
identity, found := identities["Revoked Primary User ID <[email protected]>"] |
| 434 |
if !found { |
| 435 |
t.Errorf("missing identity") |
| 436 |
} |
| 437 |
|
| 438 |
if !identity.Revoked(nil, time.Now(), &packet.Config{}) { |
| 439 |
t.Errorf("expected identity to be revoked") |
| 440 |
} |
| 441 |
|
| 442 |
if _, found := keys[0].SigningKey(time.Now(), nil); found { |
| 443 |
t.Errorf("Expected SigningKey not to return a signing key when the only User IDs is revoked") |
| 444 |
} |
| 445 |
|
| 446 |
if _, found := keys[0].EncryptionKey(time.Now(), nil); found { |
| 447 |
t.Errorf("Expected EncryptionKey not to return an encryption key when the only User IDs is revoked") |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
func TestDummyPrivateKey(t *testing.T) { |
| 452 |
// This public key has a signing subkey, but has a dummy placeholder |
| 453 |
// instead of the real private key. It's used in scenarios where the |
| 454 |
// main private key is withheld and only signing is allowed (e.g. build |
| 455 |
// servers). |
| 456 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(onlySubkeyNoPrivateKey)) |
| 457 |
if err != nil { |
| 458 |
t.Fatal(err) |
| 459 |
} |
| 460 |
if len(keys) != 1 { |
| 461 |
t.Errorf("Failed to accept key with dummy private key, %d", len(keys)) |
| 462 |
} |
| 463 |
if !keys[0].PrivateKey.Dummy() { |
| 464 |
t.Errorf("Primary private key should be marked as a dummy key") |
| 465 |
} |
| 466 |
if len(keys[0].Subkeys) != 1 { |
| 467 |
t.Errorf("Failed to accept good subkey, %d", len(keys[0].Subkeys)) |
| 468 |
} |
| 469 |
|
| 470 |
// Test serialization of stub private key via entity.SerializePrivate(). |
| 471 |
var buf bytes.Buffer |
| 472 |
w, err := armor.EncodeWithChecksumOption(&buf, PrivateKeyType, nil, false) |
| 473 |
if err != nil { |
| 474 |
t.Errorf("Failed top initialise armored key writer") |
| 475 |
} |
| 476 |
err = keys[0].SerializePrivateWithoutSigning(w, nil) |
| 477 |
if err != nil { |
| 478 |
t.Errorf("Failed to serialize entity") |
| 479 |
} |
| 480 |
if w.Close() != nil { |
| 481 |
t.Errorf("Failed to close writer for armored key") |
| 482 |
} |
| 483 |
|
| 484 |
keys, err = ReadArmoredKeyRing(bytes.NewBufferString(buf.String())) |
| 485 |
if err != nil { |
| 486 |
t.Fatal(err) |
| 487 |
} |
| 488 |
if len(keys) != 1 { |
| 489 |
t.Errorf("Failed to accept key with dummy private key, %d", len(keys)) |
| 490 |
} |
| 491 |
if !keys[0].PrivateKey.Dummy() { |
| 492 |
t.Errorf("Primary private key should be marked as a dummy key after serialisation") |
| 493 |
} |
| 494 |
if len(keys[0].Subkeys) != 1 { |
| 495 |
t.Errorf("Failed to accept good subkey, %d", len(keys[0].Subkeys)) |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
// TestExternallyRevokableKey attempts to load and parse a key with a third party revocation permission. |
| 500 |
func TestExternallyRevocableKey(t *testing.T) { |
| 501 |
kring, err := ReadKeyRing(readerFromHex(subkeyUsageHex)) |
| 502 |
if err != nil { |
| 503 |
t.Fatal(err) |
| 504 |
} |
| 505 |
|
| 506 |
// The 0xA42704B92866382A key can be revoked by 0xBE3893CB843D0FE70C |
| 507 |
// according to this signature that appears within the key: |
| 508 |
// :signature packet: algo 1, keyid A42704B92866382A |
| 509 |
// version 4, created 1396409682, md5len 0, sigclass 0x1f |
| 510 |
// digest algo 2, begin of digest a9 84 |
| 511 |
// hashed subpkt 2 len 4 (sig created 2014-04-02) |
| 512 |
// hashed subpkt 12 len 22 (revocation key: c=80 a=1 f=CE094AA433F7040BB2DDF0BE3893CB843D0FE70C) |
| 513 |
// hashed subpkt 7 len 1 (not revocable) |
| 514 |
// subpkt 16 len 8 (issuer key ID A42704B92866382A) |
| 515 |
// data: [1024 bits] |
| 516 |
|
| 517 |
id := uint64(0xA42704B92866382A) |
| 518 |
keys := kring.KeysById(id) |
| 519 |
if len(keys) != 1 { |
| 520 |
t.Errorf("Expected to find key id %X, but got %d matches", id, len(keys)) |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
func TestKeyRevocation(t *testing.T) { |
| 525 |
kring, err := ReadKeyRing(readerFromHex(revokedKeyHex)) |
| 526 |
if err != nil { |
| 527 |
t.Fatal(err) |
| 528 |
} |
| 529 |
|
| 530 |
if len(kring) != 1 { |
| 531 |
t.Fatal("Failed to read key with a sub key") |
| 532 |
} |
| 533 |
|
| 534 |
// revokedKeyHex contains these keys: |
| 535 |
// pub 1024R/9A34F7C0 2014-03-25 [revoked: 2014-03-25] |
| 536 |
// sub 1024R/1BA3CD60 2014-03-25 [revoked: 2014-03-25] |
| 537 |
ids := []uint64{0xA401D9F09A34F7C0, 0x5CD3BE0A1BA3CD60} |
| 538 |
|
| 539 |
for _, id := range ids { |
| 540 |
keys := kring.KeysById(id) |
| 541 |
if len(keys) != 1 { |
| 542 |
t.Errorf("Expected KeysById to find revoked key %X, but got %d matches", id, len(keys)) |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
signingkey, found := kring[0].SigningKey(time.Now(), nil) |
| 547 |
if found { |
| 548 |
t.Errorf("Expected SigningKey not to return a signing key for a revoked key, got %X", signingkey.PublicKey.KeyId) |
| 549 |
} |
| 550 |
|
| 551 |
encryptionkey, found := kring[0].EncryptionKey(time.Now(), nil) |
| 552 |
if found { |
| 553 |
t.Errorf("Expected EncryptionKey not to return an encryption key for a revoked key, got %X", encryptionkey.PublicKey.KeyId) |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
func TestKeyWithRevokedSubKey(t *testing.T) { |
| 558 |
// This key contains a revoked sub key: |
| 559 |
// pub rsa1024/0x4CBD826C39074E38 2018-06-14 [SC] |
| 560 |
// Key fingerprint = 3F95 169F 3FFA 7D3F 2B47 6F0C 4CBD 826C 3907 4E38 |
| 561 |
// uid Golang Gopher <[email protected]> |
| 562 |
// sub rsa1024/0x945DB1AF61D85727 2018-06-14 [S] [revoked: 2018-06-14] |
| 563 |
|
| 564 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(keyWithSubKey)) |
| 565 |
if err != nil { |
| 566 |
t.Fatal(err) |
| 567 |
} |
| 568 |
|
| 569 |
if len(keys) != 1 { |
| 570 |
t.Fatal("Failed to read key with a sub key") |
| 571 |
} |
| 572 |
|
| 573 |
identity := keys[0].Identities["Golang Gopher <[email protected]>"] |
| 574 |
// Test for an issue where Subkey Binding Signatures (RFC 4880 5.2.1) were added to the identity |
| 575 |
// preceding the Subkey Packet if the Subkey Packet was followed by more than one signature. |
| 576 |
// For example, the current key has the following layout: |
| 577 |
// PUBKEY UID SELFSIG SUBKEY REV SELFSIG |
| 578 |
// The last SELFSIG would be added to the UID's signatures. This is wrong. |
| 579 |
if numSigs, numExpected := len(identity.SelfCertifications), 1; numSigs != numExpected { |
| 580 |
t.Fatalf("got %d signatures, expected %d", numSigs, numExpected) |
| 581 |
} |
| 582 |
|
| 583 |
if numSubKeys, numExpected := len(keys[0].Subkeys), 1; numSubKeys != numExpected { |
| 584 |
t.Fatalf("got %d subkeys, expected %d", numSubKeys, numExpected) |
| 585 |
} |
| 586 |
|
| 587 |
subKey := keys[0].Subkeys[0] |
| 588 |
if len(subKey.Bindings) == 0 { |
| 589 |
t.Fatalf("no binding subkey signature") |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
func TestSubkeyRevocation(t *testing.T) { |
| 594 |
kring, err := ReadKeyRing(readerFromHex(revokedSubkeyHex)) |
| 595 |
if err != nil { |
| 596 |
t.Fatal(err) |
| 597 |
} |
| 598 |
|
| 599 |
if len(kring) != 1 { |
| 600 |
t.Fatal("Failed to read key with a sub key") |
| 601 |
} |
| 602 |
|
| 603 |
// revokedSubkeyHex contains these keys: |
| 604 |
// pub 1024R/4EF7E4BECCDE97F0 2014-03-25 |
| 605 |
// sub 1024R/D63636E2B96AE423 2014-03-25 |
| 606 |
// sub 1024D/DBCE4EE19529437F 2014-03-25 |
| 607 |
// sub 1024R/677815E371C2FD23 2014-03-25 [revoked: 2014-03-25] |
| 608 |
validKeys := []uint64{0x4EF7E4BECCDE97F0, 0xD63636E2B96AE423, 0xDBCE4EE19529437F} |
| 609 |
encryptionKey := uint64(0xD63636E2B96AE423) |
| 610 |
revokedKey := uint64(0x677815E371C2FD23) |
| 611 |
|
| 612 |
for _, id := range validKeys { |
| 613 |
keys := kring.KeysById(id) |
| 614 |
if len(keys) != 1 { |
| 615 |
t.Errorf("Expected KeysById to find key %X, but got %d matches", id, len(keys)) |
| 616 |
} |
| 617 |
if id == encryptionKey { |
| 618 |
key, found := kring[0].EncryptionKey(time.Now(), &allowAllAlgorithmsConfig) |
| 619 |
if !found || key.PublicKey.KeyId != id { |
| 620 |
t.Errorf("Expected EncryptionKey to find key %X", id) |
| 621 |
} |
| 622 |
} else { |
| 623 |
_, found := kring[0].SigningKeyById(time.Now(), id, &allowAllAlgorithmsConfig) |
| 624 |
if !found { |
| 625 |
t.Errorf("Expected SigningKeyById to find key %X", id) |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
keys := kring.KeysById(revokedKey) |
| 631 |
if len(keys) != 1 { |
| 632 |
t.Errorf("Expected KeysById to find key %X, but got %d matches", revokedKey, len(keys)) |
| 633 |
} |
| 634 |
|
| 635 |
signingkey, found := kring[0].SigningKeyById(time.Now(), revokedKey, nil) |
| 636 |
if found { |
| 637 |
t.Errorf("Expected SigningKeyById not to return an encryption key for a revoked key, got %X", signingkey.PublicKey.KeyId) |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
func TestKeyWithSubKeyAndBadSelfSigOrder(t *testing.T) { |
| 642 |
// This key was altered so that the self signatures following the |
| 643 |
// subkey are in a sub-optimal order. |
| 644 |
// |
| 645 |
// Note: Should someone have to create a similar key again, look into |
| 646 |
// gpgsplit, gpg --dearmor, and gpg --enarmor. |
| 647 |
// |
| 648 |
// The packet ordering is the following: |
| 649 |
// PUBKEY UID UIDSELFSIG SUBKEY SELFSIG1 SELFSIG2 |
| 650 |
// |
| 651 |
// Where: |
| 652 |
// SELFSIG1 expires on 2018-06-14 and was created first |
| 653 |
// SELFSIG2 does not expire and was created after SELFSIG1 |
| 654 |
// |
| 655 |
// Test for RFC 4880 5.2.3.3: |
| 656 |
// > An implementation that encounters multiple self-signatures on the |
| 657 |
// > same object may resolve the ambiguity in any way it sees fit, but it |
| 658 |
// > is RECOMMENDED that priority be given to the most recent self- |
| 659 |
// > signature. |
| 660 |
// |
| 661 |
// This means that we should keep SELFSIG2. |
| 662 |
|
| 663 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(keyWithSubKeyAndBadSelfSigOrder)) |
| 664 |
if err != nil { |
| 665 |
t.Fatal(err) |
| 666 |
} |
| 667 |
|
| 668 |
if len(keys) != 1 { |
| 669 |
t.Fatal("Failed to read key with a sub key and a bad selfsig packet order") |
| 670 |
} |
| 671 |
|
| 672 |
key := keys[0] |
| 673 |
|
| 674 |
if numKeys, expected := len(key.Subkeys), 1; numKeys != expected { |
| 675 |
t.Fatalf("Read %d subkeys, expected %d", numKeys, expected) |
| 676 |
} |
| 677 |
|
| 678 |
subKey := key.Subkeys[0] |
| 679 |
var zeroTime time.Time |
| 680 |
var config *packet.Config |
| 681 |
selfSig, err := subKey.LatestValidBindingSignature(zeroTime, config) |
| 682 |
if err != nil { |
| 683 |
t.Fatal("expected a self signature to be found") |
| 684 |
} |
| 685 |
if lifetime := selfSig.KeyLifetimeSecs; lifetime != nil { |
| 686 |
t.Errorf("The signature has a key lifetime (%d), but it should be nil", *lifetime) |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
func TestIdVerification(t *testing.T) { |
| 691 |
kring, err := ReadKeyRing(readerFromHex(testKeys1And2PrivateHex)) |
| 692 |
if err != nil { |
| 693 |
t.Fatal(err) |
| 694 |
} |
| 695 |
if err := kring[1].PrivateKey.Decrypt([]byte("passphrase")); err != nil { |
| 696 |
t.Fatal(err) |
| 697 |
} |
| 698 |
|
| 699 |
const signedIdentity = "Test Key 1 (RSA)" |
| 700 |
const signerIdentity = "Test Key 2 (RSA, encrypted private key)" |
| 701 |
config := allowAllAlgorithmsConfig |
| 702 |
config.SigLifetimeSecs = 128 |
| 703 |
config.SigningIdentity = signerIdentity |
| 704 |
if err := kring[0].SignIdentity(signedIdentity, kring[1], &config); err != nil { |
| 705 |
t.Fatal(err) |
| 706 |
} |
| 707 |
|
| 708 |
ident, ok := kring[0].Identities[signedIdentity] |
| 709 |
if !ok { |
| 710 |
t.Fatal("signed identity missing from key after signing") |
| 711 |
} |
| 712 |
|
| 713 |
checked := false |
| 714 |
for _, sig := range ident.OtherCertifications { |
| 715 |
if sig.Packet.IssuerKeyId == nil || *sig.Packet.IssuerKeyId != kring[1].PrimaryKey.KeyId { |
| 716 |
continue |
| 717 |
} |
| 718 |
|
| 719 |
if err := kring[1].PrimaryKey.VerifyUserIdSignature(signedIdentity, kring[0].PrimaryKey, sig.Packet); err != nil { |
| 720 |
t.Fatalf("error verifying new identity signature: %s", err) |
| 721 |
} |
| 722 |
|
| 723 |
if sig.Packet.SignerUserId == nil || *sig.Packet.SignerUserId != signerIdentity { |
| 724 |
t.Fatalf("wrong or nil signer identity") |
| 725 |
} |
| 726 |
|
| 727 |
if sig.Packet.SigExpired(time.Now()) { |
| 728 |
t.Fatalf("signature is expired") |
| 729 |
} |
| 730 |
|
| 731 |
if !sig.Packet.SigExpired(time.Now().Add(129 * time.Second)) { |
| 732 |
t.Fatalf("signature has invalid expiration") |
| 733 |
} |
| 734 |
|
| 735 |
checked = true |
| 736 |
break |
| 737 |
} |
| 738 |
|
| 739 |
if !checked { |
| 740 |
t.Fatal("didn't find identity signature in Entity") |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
func TestNewEntityWithDefaultHash(t *testing.T) { |
| 745 |
for _, hash := range hashes { |
| 746 |
c := &packet.Config{ |
| 747 |
DefaultHash: hash, |
| 748 |
Algorithm: packet.PubKeyAlgoEdDSA, |
| 749 |
} |
| 750 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", c) |
| 751 |
if hash == crypto.SHA1 { |
| 752 |
if err == nil { |
| 753 |
t.Fatal("should fail on SHA1 key creation") |
| 754 |
} |
| 755 |
continue |
| 756 |
} |
| 757 |
|
| 758 |
if err != nil { |
| 759 |
t.Fatal(err) |
| 760 |
} |
| 761 |
|
| 762 |
for _, identity := range entity.Identities { |
| 763 |
var zeroTime time.Time |
| 764 |
selfSig, err := identity.LatestValidSelfCertification(zeroTime, c) |
| 765 |
if err != nil { |
| 766 |
t.Fatal("expected a self signature to be found ") |
| 767 |
} |
| 768 |
prefs := selfSig.PreferredHash |
| 769 |
if len(prefs) == 0 { |
| 770 |
t.Fatal("didn't find a preferred hash list in self signature") |
| 771 |
} |
| 772 |
ph := hashToHashId(c.DefaultHash) |
| 773 |
if c.DefaultHash != crypto.SHA224 && prefs[0] != ph { |
| 774 |
t.Fatalf("Expected preferred hash to be %d, got %d", ph, prefs[0]) |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
func TestNewEntityNilConfigPreferredHash(t *testing.T) { |
| 781 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 782 |
if err != nil { |
| 783 |
t.Fatal(err) |
| 784 |
} |
| 785 |
|
| 786 |
for _, identity := range entity.Identities { |
| 787 |
var zeroTime time.Time |
| 788 |
selfSig, err := identity.LatestValidSelfCertification(zeroTime, &packet.Config{}) |
| 789 |
if err != nil { |
| 790 |
t.Fatal("expected a self signature to be found ") |
| 791 |
} |
| 792 |
prefs := selfSig.PreferredHash |
| 793 |
if len(prefs) != 1 { |
| 794 |
t.Fatal("expected preferred hashes list to be [SHA256]") |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
func TestNewEntityCorrectName(t *testing.T) { |
| 800 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 801 |
if err != nil { |
| 802 |
t.Fatal(err) |
| 803 |
} |
| 804 |
if len(entity.Identities) != 1 { |
| 805 |
t.Fatalf("len(entity.Identities) = %d, want 1", len(entity.Identities)) |
| 806 |
} |
| 807 |
var got string |
| 808 |
for _, i := range entity.Identities { |
| 809 |
got = i.Name |
| 810 |
} |
| 811 |
want := "Golang Gopher (Test Key) <[email protected]>" |
| 812 |
if got != want { |
| 813 |
t.Fatalf("Identity.Name = %q, want %q", got, want) |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
func TestNewEntityWithDefaultCipher(t *testing.T) { |
| 818 |
for _, cipher := range ciphers { |
| 819 |
c := &packet.Config{ |
| 820 |
DefaultCipher: cipher, |
| 821 |
} |
| 822 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", c) |
| 823 |
if err != nil { |
| 824 |
t.Fatal(err) |
| 825 |
} |
| 826 |
|
| 827 |
for _, identity := range entity.Identities { |
| 828 |
var zeroTime time.Time |
| 829 |
selfSig, err := identity.LatestValidSelfCertification(zeroTime, c) |
| 830 |
if err != nil { |
| 831 |
t.Fatal("expected a self signature to be found ") |
| 832 |
} |
| 833 |
prefs := selfSig.PreferredSymmetric |
| 834 |
if len(prefs) == 0 { |
| 835 |
t.Fatal("didn't find a preferred cipher list") |
| 836 |
} |
| 837 |
if prefs[0] != uint8(c.DefaultCipher) { |
| 838 |
t.Fatalf("Expected preferred cipher to be %d, got %d", uint8(c.DefaultCipher), prefs[0]) |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
func TestNewEntityNilConfigPreferredSymmetric(t *testing.T) { |
| 845 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 846 |
if err != nil { |
| 847 |
t.Fatal(err) |
| 848 |
} |
| 849 |
|
| 850 |
for _, identity := range entity.Identities { |
| 851 |
var zeroTime time.Time |
| 852 |
selfSig, err := identity.LatestValidSelfCertification(zeroTime, &packet.Config{}) |
| 853 |
if err != nil { |
| 854 |
t.Fatal("expected a self signature to be found ") |
| 855 |
} |
| 856 |
prefs := selfSig.PreferredSymmetric |
| 857 |
if len(prefs) != 1 || prefs[0] != algorithm.AES128.Id() { |
| 858 |
t.Fatal("expected preferred ciphers list to be [AES128]") |
| 859 |
} |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
func TestNewEntityWithDefaultAead(t *testing.T) { |
| 864 |
for _, aeadMode := range aeadModes { |
| 865 |
cfg := &packet.Config{ |
| 866 |
AEADConfig: &packet.AEADConfig{ |
| 867 |
DefaultMode: aeadMode, |
| 868 |
}, |
| 869 |
} |
| 870 |
entity, err := NewEntity("Botvinnik", "1.e4", "[email protected]", cfg) |
| 871 |
if err != nil { |
| 872 |
t.Fatal(err) |
| 873 |
} |
| 874 |
|
| 875 |
for _, identity := range entity.Identities { |
| 876 |
var zeroTime time.Time |
| 877 |
selfSig, err := identity.LatestValidSelfCertification(zeroTime, cfg) |
| 878 |
if err != nil { |
| 879 |
t.Fatal("expected a self signature to be found ") |
| 880 |
} |
| 881 |
if len(selfSig.PreferredCipherSuites) == 0 { |
| 882 |
t.Fatal("didn't find a preferred mode in self signature") |
| 883 |
} |
| 884 |
cipher := selfSig.PreferredCipherSuites[0][0] |
| 885 |
if cipher != uint8(cfg.Cipher()) { |
| 886 |
t.Fatalf("Expected preferred cipher to be %d, got %d", |
| 887 |
uint8(cfg.Cipher()), |
| 888 |
selfSig.PreferredCipherSuites[0][0]) |
| 889 |
} |
| 890 |
mode := selfSig.PreferredCipherSuites[0][1] |
| 891 |
if mode != uint8(cfg.AEAD().DefaultMode) { |
| 892 |
t.Fatalf("Expected preferred mode to be %d, got %d", |
| 893 |
uint8(cfg.AEAD().DefaultMode), |
| 894 |
selfSig.PreferredCipherSuites[0][1]) |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
func TestNewEntityPublicSerialization(t *testing.T) { |
| 901 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 902 |
if err != nil { |
| 903 |
t.Fatal(err) |
| 904 |
} |
| 905 |
serializedEntity := bytes.NewBuffer(nil) |
| 906 |
err = entity.Serialize(serializedEntity) |
| 907 |
if err != nil { |
| 908 |
t.Fatal(err) |
| 909 |
} |
| 910 |
|
| 911 |
_, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 912 |
if err != nil { |
| 913 |
t.Fatal(err) |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
func TestNewEntityPrivateSerialization(t *testing.T) { |
| 918 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 919 |
if err != nil { |
| 920 |
t.Fatal(err) |
| 921 |
} |
| 922 |
serializedEntity := bytes.NewBuffer(nil) |
| 923 |
err = entity.SerializePrivateWithoutSigning(serializedEntity, nil) |
| 924 |
if err != nil { |
| 925 |
t.Fatal(err) |
| 926 |
} |
| 927 |
|
| 928 |
_, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 929 |
if err != nil { |
| 930 |
t.Fatal(err) |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
func TestNotationPacket(t *testing.T) { |
| 935 |
keySerializeConfig := &packet.Config{ |
| 936 |
NonDeterministicSignaturesViaNotation: packet.BoolPointer(false), |
| 937 |
} |
| 938 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(keyWithNotation)) |
| 939 |
if err != nil { |
| 940 |
t.Fatal(err) |
| 941 |
} |
| 942 |
|
| 943 |
assertNotationPackets(t, keys) |
| 944 |
|
| 945 |
serializedEntity := bytes.NewBuffer(nil) |
| 946 |
err = keys[0].SerializePrivate(serializedEntity, keySerializeConfig) |
| 947 |
if err != nil { |
| 948 |
t.Fatal(err) |
| 949 |
} |
| 950 |
|
| 951 |
keys, err = ReadKeyRing(serializedEntity) |
| 952 |
if err != nil { |
| 953 |
t.Fatal(err) |
| 954 |
} |
| 955 |
|
| 956 |
assertNotationPackets(t, keys) |
| 957 |
} |
| 958 |
|
| 959 |
func assertNotationPackets(t *testing.T, keys EntityList) { |
| 960 |
if len(keys) != 1 { |
| 961 |
t.Errorf("Failed to accept key, %d", len(keys)) |
| 962 |
} |
| 963 |
|
| 964 |
identity := keys[0].Identities["Test <[email protected]>"] |
| 965 |
|
| 966 |
if numSigs, numExpected := len(identity.SelfCertifications), 1; numSigs != numExpected { |
| 967 |
t.Fatalf("got %d signatures, expected %d", numSigs, numExpected) |
| 968 |
} |
| 969 |
|
| 970 |
notations := identity.SelfCertifications[0].Packet.Notations |
| 971 |
if numNotations, numExpected := len(notations), 2; numNotations != numExpected { |
| 972 |
t.Fatalf("got %d Notation Data subpackets, expected %d", numNotations, numExpected) |
| 973 |
} |
| 974 |
|
| 975 |
if notations[0].IsHumanReadable != true { |
| 976 |
t.Fatalf("got false, expected true") |
| 977 |
} |
| 978 |
|
| 979 |
if notations[0].Name != "[email protected]" { |
| 980 |
t.Fatalf("got %s, expected [email protected]", notations[0].Name) |
| 981 |
} |
| 982 |
|
| 983 |
if string(notations[0].Value) != "test" { |
| 984 |
t.Fatalf("got %s, expected \"test\"", string(notations[0].Value)) |
| 985 |
} |
| 986 |
|
| 987 |
if notations[1].IsHumanReadable != false { |
| 988 |
t.Fatalf("got true, expected false") |
| 989 |
} |
| 990 |
|
| 991 |
if notations[1].Name != "[email protected]" { |
| 992 |
t.Fatalf("got %s, expected [email protected]", notations[1].Name) |
| 993 |
} |
| 994 |
|
| 995 |
if !bytes.Equal(notations[1].Value, []byte{0, 1, 2, 3}) { |
| 996 |
t.Fatalf("got %s, expected {0, 1, 2, 3}", string(notations[1].Value)) |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
func TestEntityPrivateSerialization(t *testing.T) { |
| 1001 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKeyBlock)) |
| 1002 |
if err != nil { |
| 1003 |
t.Fatal(err) |
| 1004 |
} |
| 1005 |
|
| 1006 |
for _, entity := range keys { |
| 1007 |
serializedEntity := bytes.NewBuffer(nil) |
| 1008 |
err = entity.SerializePrivateWithoutSigning(serializedEntity, nil) |
| 1009 |
if err != nil { |
| 1010 |
t.Fatal(err) |
| 1011 |
} |
| 1012 |
|
| 1013 |
_, err := ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1014 |
if err != nil { |
| 1015 |
t.Fatal(err) |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
func TestAddUserId(t *testing.T) { |
| 1021 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1022 |
if err != nil { |
| 1023 |
t.Fatal(err) |
| 1024 |
} |
| 1025 |
|
| 1026 |
err = entity.AddUserId("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1027 |
if err != nil { |
| 1028 |
t.Fatal(err) |
| 1029 |
} |
| 1030 |
|
| 1031 |
err = entity.AddUserId("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1032 |
if err != nil { |
| 1033 |
t.Fatal(err) |
| 1034 |
} |
| 1035 |
|
| 1036 |
ignore_err := entity.AddUserId("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1037 |
if ignore_err == nil { |
| 1038 |
t.Fatal(err) |
| 1039 |
} |
| 1040 |
|
| 1041 |
if len(entity.Identities) != 3 { |
| 1042 |
t.Fatalf("Expected 3 id, got %d", len(entity.Identities)) |
| 1043 |
} |
| 1044 |
|
| 1045 |
for _, sk := range entity.Identities { |
| 1046 |
var zeroTime time.Time |
| 1047 |
selfSig, err := sk.LatestValidSelfCertification(zeroTime, &packet.Config{}) |
| 1048 |
if err != nil { |
| 1049 |
t.Fatal("expected a self signature to be found") |
| 1050 |
} |
| 1051 |
err = entity.PrimaryKey.VerifyUserIdSignature(sk.UserId.Id, entity.PrimaryKey, selfSig) |
| 1052 |
if err != nil { |
| 1053 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
serializedEntity := bytes.NewBuffer(nil) |
| 1058 |
if err := entity.SerializePrivate(serializedEntity, nil); err != nil { |
| 1059 |
t.Fatal(err) |
| 1060 |
} |
| 1061 |
|
| 1062 |
_, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1063 |
if err != nil { |
| 1064 |
t.Fatal(err) |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
func TestAddSubkey(t *testing.T) { |
| 1069 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1070 |
if err != nil { |
| 1071 |
t.Fatal(err) |
| 1072 |
} |
| 1073 |
|
| 1074 |
err = entity.AddSigningSubkey(nil) |
| 1075 |
if err != nil { |
| 1076 |
t.Fatal(err) |
| 1077 |
} |
| 1078 |
|
| 1079 |
err = entity.AddEncryptionSubkey(nil) |
| 1080 |
if err != nil { |
| 1081 |
t.Fatal(err) |
| 1082 |
} |
| 1083 |
|
| 1084 |
if len(entity.Subkeys) != 3 { |
| 1085 |
t.Fatalf("Expected 3 subkeys, got %d", len(entity.Subkeys)) |
| 1086 |
} |
| 1087 |
|
| 1088 |
for _, sk := range entity.Subkeys { |
| 1089 |
var zeroTime time.Time |
| 1090 |
var config *packet.Config |
| 1091 |
selfSig, err := sk.LatestValidBindingSignature(zeroTime, config) |
| 1092 |
if err != nil { |
| 1093 |
t.Fatal("expected a self signature to be found") |
| 1094 |
} |
| 1095 |
err = entity.PrimaryKey.VerifyKeySignature(sk.PublicKey, selfSig) |
| 1096 |
if err != nil { |
| 1097 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
serializedEntity := bytes.NewBuffer(nil) |
| 1102 |
if err := entity.SerializePrivate(serializedEntity, nil); err != nil { |
| 1103 |
t.Fatal(err) |
| 1104 |
} |
| 1105 |
|
| 1106 |
_, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1107 |
if err != nil { |
| 1108 |
t.Fatal(err) |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
func TestAddSubkeySerialized(t *testing.T) { |
| 1113 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1114 |
if err != nil { |
| 1115 |
t.Fatal(err) |
| 1116 |
} |
| 1117 |
|
| 1118 |
err = entity.AddSigningSubkey(nil) |
| 1119 |
if err != nil { |
| 1120 |
t.Fatal(err) |
| 1121 |
} |
| 1122 |
|
| 1123 |
err = entity.AddEncryptionSubkey(nil) |
| 1124 |
if err != nil { |
| 1125 |
t.Fatal(err) |
| 1126 |
} |
| 1127 |
|
| 1128 |
serializedEntity := bytes.NewBuffer(nil) |
| 1129 |
if err := entity.SerializePrivateWithoutSigning(serializedEntity, nil); err != nil { |
| 1130 |
t.Fatal(err) |
| 1131 |
} |
| 1132 |
|
| 1133 |
entity, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1134 |
if err != nil { |
| 1135 |
t.Fatal(err) |
| 1136 |
} |
| 1137 |
|
| 1138 |
if len(entity.Subkeys) != 3 { |
| 1139 |
t.Fatalf("Expected 3 subkeys, got %d", len(entity.Subkeys)) |
| 1140 |
} |
| 1141 |
|
| 1142 |
for _, sk := range entity.Subkeys { |
| 1143 |
var zeroTime time.Time |
| 1144 |
var config *packet.Config |
| 1145 |
selfSig, err := sk.LatestValidBindingSignature(zeroTime, config) |
| 1146 |
if err != nil { |
| 1147 |
t.Fatal("expected a self signature to be found") |
| 1148 |
} |
| 1149 |
err = entity.PrimaryKey.VerifyKeySignature(sk.PublicKey, selfSig) |
| 1150 |
if err != nil { |
| 1151 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
func TestAddSubkeyWithConfig(t *testing.T) { |
| 1157 |
c := &packet.Config{ |
| 1158 |
DefaultHash: crypto.SHA512, |
| 1159 |
Algorithm: packet.PubKeyAlgoEdDSA, |
| 1160 |
} |
| 1161 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1162 |
if err != nil { |
| 1163 |
t.Fatal(err) |
| 1164 |
} |
| 1165 |
|
| 1166 |
err = entity.AddSigningSubkey(c) |
| 1167 |
if err != nil { |
| 1168 |
t.Fatal(err) |
| 1169 |
} |
| 1170 |
|
| 1171 |
err = entity.AddEncryptionSubkey(c) |
| 1172 |
if err != nil { |
| 1173 |
t.Fatal(err) |
| 1174 |
} |
| 1175 |
|
| 1176 |
if len(entity.Subkeys) != 3 { |
| 1177 |
t.Fatalf("Expected 3 subkeys, got %d", len(entity.Subkeys)) |
| 1178 |
} |
| 1179 |
|
| 1180 |
if entity.Subkeys[1].PublicKey.PubKeyAlgo != packet.PubKeyAlgoEdDSA { |
| 1181 |
t.Fatalf("Expected subkey algorithm: %v, got: %v", packet.PubKeyAlgoEdDSA, |
| 1182 |
entity.Subkeys[1].PublicKey.PubKeyAlgo) |
| 1183 |
} |
| 1184 |
|
| 1185 |
if entity.Subkeys[2].PublicKey.PubKeyAlgo != packet.PubKeyAlgoECDH { |
| 1186 |
t.Fatalf("Expected subkey algorithm: %v, got: %v", packet.PubKeyAlgoECDH, |
| 1187 |
entity.Subkeys[2].PublicKey.PubKeyAlgo) |
| 1188 |
} |
| 1189 |
|
| 1190 |
var zeroTime time.Time |
| 1191 |
var config *packet.Config |
| 1192 |
selfSig1, err := entity.Subkeys[1].LatestValidBindingSignature(zeroTime, config) |
| 1193 |
if err != nil { |
| 1194 |
t.Fatal("expected a self signature to be found") |
| 1195 |
} |
| 1196 |
if selfSig1.Hash != c.DefaultHash { |
| 1197 |
t.Fatalf("Expected subkey hash method: %v, got: %v", c.DefaultHash, |
| 1198 |
selfSig1.Hash) |
| 1199 |
} |
| 1200 |
if selfSig1.EmbeddedSignature.Hash != c.DefaultHash { |
| 1201 |
t.Fatalf("Expected subkey hash method: %v, got: %v", c.DefaultHash, |
| 1202 |
selfSig1.EmbeddedSignature.Hash) |
| 1203 |
} |
| 1204 |
err = entity.PrimaryKey.VerifyKeySignature(entity.Subkeys[1].PublicKey, selfSig1) |
| 1205 |
if err != nil { |
| 1206 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1207 |
} |
| 1208 |
|
| 1209 |
selfSig2, err := entity.Subkeys[2].LatestValidBindingSignature(zeroTime, config) |
| 1210 |
if err != nil { |
| 1211 |
t.Fatal("expected a self signature to be found") |
| 1212 |
} |
| 1213 |
if selfSig2.Hash != c.DefaultHash { |
| 1214 |
t.Fatalf("Expected subkey hash method: %v, got: %v", c.DefaultHash, |
| 1215 |
selfSig2.Hash) |
| 1216 |
} |
| 1217 |
err = entity.PrimaryKey.VerifyKeySignature(entity.Subkeys[2].PublicKey, selfSig2) |
| 1218 |
if err != nil { |
| 1219 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1220 |
} |
| 1221 |
|
| 1222 |
serializedEntity := bytes.NewBuffer(nil) |
| 1223 |
if err := entity.SerializePrivate(serializedEntity, nil); err != nil { |
| 1224 |
t.Fatal(err) |
| 1225 |
} |
| 1226 |
|
| 1227 |
_, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1228 |
if err != nil { |
| 1229 |
t.Fatal(err) |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
func TestAddSubkeyWithConfigSerialized(t *testing.T) { |
| 1234 |
c := &packet.Config{ |
| 1235 |
DefaultHash: crypto.SHA512, |
| 1236 |
Algorithm: packet.PubKeyAlgoEdDSA, |
| 1237 |
} |
| 1238 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1239 |
if err != nil { |
| 1240 |
t.Fatal(err) |
| 1241 |
} |
| 1242 |
|
| 1243 |
err = entity.AddSigningSubkey(c) |
| 1244 |
if err != nil { |
| 1245 |
t.Fatal(err) |
| 1246 |
} |
| 1247 |
|
| 1248 |
err = entity.AddEncryptionSubkey(c) |
| 1249 |
if err != nil { |
| 1250 |
t.Fatal(err) |
| 1251 |
} |
| 1252 |
|
| 1253 |
serializedEntity := bytes.NewBuffer(nil) |
| 1254 |
if err := entity.SerializePrivateWithoutSigning(serializedEntity, nil); err != nil { |
| 1255 |
t.Fatal(err) |
| 1256 |
} |
| 1257 |
|
| 1258 |
entity, err = ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1259 |
if err != nil { |
| 1260 |
t.Fatal(err) |
| 1261 |
} |
| 1262 |
|
| 1263 |
if len(entity.Subkeys) != 3 { |
| 1264 |
t.Fatalf("Expected 3 subkeys, got %d", len(entity.Subkeys)) |
| 1265 |
} |
| 1266 |
|
| 1267 |
var zeroTime time.Time |
| 1268 |
var config *packet.Config |
| 1269 |
selfSig1, err := entity.Subkeys[1].LatestValidBindingSignature(zeroTime, config) |
| 1270 |
if err != nil { |
| 1271 |
t.Fatal("expected a self signature to be found") |
| 1272 |
} |
| 1273 |
if entity.Subkeys[1].PublicKey.PubKeyAlgo != packet.PubKeyAlgoEdDSA { |
| 1274 |
t.Fatalf("Expected subkey algorithm: %v, got: %v", packet.PubKeyAlgoEdDSA, |
| 1275 |
entity.Subkeys[1].PublicKey.PubKeyAlgo) |
| 1276 |
} |
| 1277 |
|
| 1278 |
if entity.Subkeys[2].PublicKey.PubKeyAlgo != packet.PubKeyAlgoECDH { |
| 1279 |
t.Fatalf("Expected subkey algorithm: %v, got: %v", packet.PubKeyAlgoECDH, |
| 1280 |
entity.Subkeys[2].PublicKey.PubKeyAlgo) |
| 1281 |
} |
| 1282 |
|
| 1283 |
if selfSig1.Hash != c.DefaultHash { |
| 1284 |
t.Fatalf("Expected subkey hash method: %v, got: %v", c.DefaultHash, |
| 1285 |
selfSig1.Hash) |
| 1286 |
} |
| 1287 |
|
| 1288 |
if selfSig1.EmbeddedSignature.Hash != c.DefaultHash { |
| 1289 |
t.Fatalf("Expected subkey hash method: %v, got: %v", c.DefaultHash, |
| 1290 |
selfSig1.EmbeddedSignature.Hash) |
| 1291 |
} |
| 1292 |
|
| 1293 |
selfSig2, err := entity.Subkeys[2].LatestValidBindingSignature(zeroTime, config) |
| 1294 |
if err != nil { |
| 1295 |
t.Fatal("expected a self signature to be found") |
| 1296 |
} |
| 1297 |
if selfSig2.Hash != c.DefaultHash { |
| 1298 |
t.Fatalf("Expected subkey hash method: %v, got: %v", c.DefaultHash, |
| 1299 |
selfSig2.Hash) |
| 1300 |
} |
| 1301 |
err = entity.PrimaryKey.VerifyKeySignature(entity.Subkeys[1].PublicKey, selfSig1) |
| 1302 |
if err != nil { |
| 1303 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1304 |
} |
| 1305 |
err = entity.PrimaryKey.VerifyKeySignature(entity.Subkeys[2].PublicKey, selfSig2) |
| 1306 |
if err != nil { |
| 1307 |
t.Errorf("Invalid subkey signature: %v", err) |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
func TestRevokeKey(t *testing.T) { |
| 1312 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1313 |
if err != nil { |
| 1314 |
t.Fatal(err) |
| 1315 |
} |
| 1316 |
|
| 1317 |
err = entity.Revoke(packet.NoReason, "Key revocation", nil) |
| 1318 |
if err != nil { |
| 1319 |
t.Fatal(err) |
| 1320 |
} |
| 1321 |
|
| 1322 |
if len(entity.Revocations) == 0 { |
| 1323 |
t.Fatal("Revocation signature missing from entity") |
| 1324 |
} |
| 1325 |
|
| 1326 |
for _, r := range entity.Revocations { |
| 1327 |
err = entity.PrimaryKey.VerifyRevocationSignature(r.Packet) |
| 1328 |
if err != nil { |
| 1329 |
t.Errorf("Invalid revocation: %v", err) |
| 1330 |
} |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
func TestRevokeKeyWithConfig(t *testing.T) { |
| 1335 |
c := &packet.Config{ |
| 1336 |
DefaultHash: crypto.SHA512, |
| 1337 |
} |
| 1338 |
|
| 1339 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", &packet.Config{ |
| 1340 |
Algorithm: packet.PubKeyAlgoEdDSA, |
| 1341 |
}) |
| 1342 |
if err != nil { |
| 1343 |
t.Fatal(err) |
| 1344 |
} |
| 1345 |
|
| 1346 |
err = entity.Revoke(packet.NoReason, "Key revocation", c) |
| 1347 |
if err != nil { |
| 1348 |
t.Fatal(err) |
| 1349 |
} |
| 1350 |
|
| 1351 |
if len(entity.Revocations) == 0 { |
| 1352 |
t.Fatal("Revocation signature missing from entity") |
| 1353 |
} |
| 1354 |
|
| 1355 |
if entity.Revocations[0].Packet.Hash != c.DefaultHash { |
| 1356 |
t.Fatalf("Expected signature hash method: %v, got: %v", c.DefaultHash, |
| 1357 |
entity.Revocations[0].Packet.Hash) |
| 1358 |
} |
| 1359 |
|
| 1360 |
for _, r := range entity.Revocations { |
| 1361 |
err = entity.PrimaryKey.VerifyRevocationSignature(r.Packet) |
| 1362 |
if err != nil { |
| 1363 |
t.Errorf("Invalid revocation: %v", err) |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
func TestRevokeSubkey(t *testing.T) { |
| 1369 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1370 |
if err != nil { |
| 1371 |
t.Fatal(err) |
| 1372 |
} |
| 1373 |
|
| 1374 |
err = entity.Subkeys[0].Revoke(packet.NoReason, "Key revocation", nil) |
| 1375 |
if err != nil { |
| 1376 |
t.Fatal(err) |
| 1377 |
} |
| 1378 |
|
| 1379 |
if len(entity.Subkeys[0].Revocations) != 1 { |
| 1380 |
t.Fatalf("Expected 1 subkey revocation signature, got %v", len(entity.Subkeys[0].Revocations)) |
| 1381 |
} |
| 1382 |
|
| 1383 |
revSig := entity.Subkeys[0].Revocations[0] |
| 1384 |
|
| 1385 |
err = entity.PrimaryKey.VerifySubkeyRevocationSignature(revSig.Packet, entity.Subkeys[0].PublicKey) |
| 1386 |
if err != nil { |
| 1387 |
t.Fatal(err) |
| 1388 |
} |
| 1389 |
|
| 1390 |
if revSig.Packet.RevocationReason == nil { |
| 1391 |
t.Fatal("Revocation reason was not set") |
| 1392 |
} |
| 1393 |
if revSig.Packet.RevocationReasonText == "" { |
| 1394 |
t.Fatal("Revocation reason text was not set") |
| 1395 |
} |
| 1396 |
|
| 1397 |
serializedEntity := bytes.NewBuffer(nil) |
| 1398 |
if err := entity.SerializePrivate(serializedEntity, nil); err != nil { |
| 1399 |
t.Fatal(err) |
| 1400 |
} |
| 1401 |
|
| 1402 |
// Make sure revocation reason subpackets are not lost during serialization. |
| 1403 |
newEntity, err := ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes()))) |
| 1404 |
if err != nil { |
| 1405 |
t.Fatal(err) |
| 1406 |
} |
| 1407 |
|
| 1408 |
if newEntity.Subkeys[0].Revocations[0].Packet.RevocationReason == nil { |
| 1409 |
t.Fatal("Revocation reason lost after serialization of entity") |
| 1410 |
} |
| 1411 |
if newEntity.Subkeys[0].Revocations[0].Packet.RevocationReasonText == "" { |
| 1412 |
t.Fatal("Revocation reason text lost after serialization of entity") |
| 1413 |
} |
| 1414 |
} |
| 1415 |
|
| 1416 |
func TestRevokeSubkeyWithInvalidSignature(t *testing.T) { |
| 1417 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1418 |
if err != nil { |
| 1419 |
t.Fatal(err) |
| 1420 |
} |
| 1421 |
|
| 1422 |
sk := entity.Subkeys[0] |
| 1423 |
sk.Bindings[0].Packet = &packet.Signature{Version: 4} |
| 1424 |
|
| 1425 |
err = sk.Revoke(packet.NoReason, "Key revocation", nil) |
| 1426 |
if err == nil { |
| 1427 |
t.Fatal("Entity was able to revoke a subkey with invalid signature") |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
func TestRevokeSubkeyWithConfig(t *testing.T) { |
| 1432 |
c := &packet.Config{ |
| 1433 |
DefaultHash: crypto.SHA512, |
| 1434 |
} |
| 1435 |
|
| 1436 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1437 |
if err != nil { |
| 1438 |
t.Fatal(err) |
| 1439 |
} |
| 1440 |
|
| 1441 |
sk := entity.Subkeys[0] |
| 1442 |
err = sk.Revoke(packet.NoReason, "Key revocation", c) |
| 1443 |
if err != nil { |
| 1444 |
t.Fatal(err) |
| 1445 |
} |
| 1446 |
|
| 1447 |
if len(sk.Revocations) != 1 { |
| 1448 |
t.Fatalf("Expected 1 subkey revocation signature, got %v", len(sk.Revocations)) |
| 1449 |
} |
| 1450 |
|
| 1451 |
revSig := sk.Revocations[0].Packet |
| 1452 |
|
| 1453 |
if revSig.Hash != c.DefaultHash { |
| 1454 |
t.Fatalf("Expected signature hash method: %v, got: %v", c.DefaultHash, revSig.Hash) |
| 1455 |
} |
| 1456 |
|
| 1457 |
err = entity.PrimaryKey.VerifySubkeyRevocationSignature(revSig, sk.PublicKey) |
| 1458 |
if err != nil { |
| 1459 |
t.Fatal(err) |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
func TestEncryptAndDecryptPrivateKeys(t *testing.T) { |
| 1464 |
s2kModesToTest := []s2k.Mode{s2k.IteratedSaltedS2K, s2k.Argon2S2K} |
| 1465 |
|
| 1466 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 1467 |
if err != nil { |
| 1468 |
t.Fatal(err) |
| 1469 |
} |
| 1470 |
|
| 1471 |
err = entity.AddSigningSubkey(nil) |
| 1472 |
if err != nil { |
| 1473 |
t.Fatal(err) |
| 1474 |
} |
| 1475 |
|
| 1476 |
err = entity.AddEncryptionSubkey(nil) |
| 1477 |
if err != nil { |
| 1478 |
t.Fatal(err) |
| 1479 |
} |
| 1480 |
for _, mode := range s2kModesToTest { |
| 1481 |
t.Run(fmt.Sprintf("S2KMode %d", mode), func(t *testing.T) { |
| 1482 |
passphrase := []byte("password") |
| 1483 |
config := &packet.Config{ |
| 1484 |
S2KConfig: &s2k.Config{ |
| 1485 |
S2KMode: mode, |
| 1486 |
}, |
| 1487 |
} |
| 1488 |
if mode == s2k.Argon2S2K { |
| 1489 |
config.AEADConfig = &packet.AEADConfig{} |
| 1490 |
} |
| 1491 |
err = entity.EncryptPrivateKeys(passphrase, config) |
| 1492 |
if err != nil { |
| 1493 |
t.Fatal(err) |
| 1494 |
} |
| 1495 |
|
| 1496 |
if !entity.PrivateKey.Encrypted { |
| 1497 |
t.Fatal("Expected encrypted private key") |
| 1498 |
} |
| 1499 |
for _, subkey := range entity.Subkeys { |
| 1500 |
if !subkey.PrivateKey.Encrypted { |
| 1501 |
t.Fatal("Expected encrypted private key") |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
err = entity.DecryptPrivateKeys(passphrase) |
| 1506 |
if err != nil { |
| 1507 |
t.Fatal(err) |
| 1508 |
} |
| 1509 |
|
| 1510 |
if entity.PrivateKey.Encrypted { |
| 1511 |
t.Fatal("Expected plaintext private key") |
| 1512 |
} |
| 1513 |
for _, subkey := range entity.Subkeys { |
| 1514 |
if subkey.PrivateKey.Encrypted { |
| 1515 |
t.Fatal("Expected plaintext private key") |
| 1516 |
} |
| 1517 |
} |
| 1518 |
}) |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
func TestKeyValidateOnDecrypt(t *testing.T) { |
| 1523 |
randomPassword := make([]byte, 128) |
| 1524 |
_, err := rand.Read(randomPassword) |
| 1525 |
if err != nil { |
| 1526 |
t.Fatal(err) |
| 1527 |
} |
| 1528 |
|
| 1529 |
t.Run("RSA", func(t *testing.T) { |
| 1530 |
t.Run("Hardcoded:2048 bits", func(t *testing.T) { |
| 1531 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(rsa2048PrivateKey)) |
| 1532 |
if err != nil { |
| 1533 |
t.Fatal("Unable to parse hardcoded key: ", err) |
| 1534 |
} |
| 1535 |
|
| 1536 |
if err := keys[0].PrivateKey.Decrypt([]byte("password")); err != nil { |
| 1537 |
t.Fatal("Unable to decrypt hardcoded key: ", err) |
| 1538 |
} |
| 1539 |
|
| 1540 |
testKeyValidateRsaOnDecrypt(t, keys[0], randomPassword) |
| 1541 |
}) |
| 1542 |
|
| 1543 |
for _, bits := range []int{2048, 3072, 4096} { |
| 1544 |
t.Run("Generated:"+strconv.Itoa(bits)+" bits", func(t *testing.T) { |
| 1545 |
key := testGenerateRSA(t, bits) |
| 1546 |
testKeyValidateRsaOnDecrypt(t, key, randomPassword) |
| 1547 |
}) |
| 1548 |
} |
| 1549 |
}) |
| 1550 |
|
| 1551 |
t.Run("ECDSA", func(t *testing.T) { |
| 1552 |
t.Run("Hardcoded:NIST P-256", func(t *testing.T) { |
| 1553 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(ecdsaPrivateKey)) |
| 1554 |
if err != nil { |
| 1555 |
t.Fatal("Unable to parse hardcoded key: ", err) |
| 1556 |
} |
| 1557 |
|
| 1558 |
if err := keys[0].PrivateKey.Decrypt([]byte("password")); err != nil { |
| 1559 |
t.Fatal("Unable to decrypt hardcoded key: ", err) |
| 1560 |
} |
| 1561 |
|
| 1562 |
if err := keys[0].Subkeys[0].PrivateKey.Decrypt([]byte("password")); err != nil { |
| 1563 |
t.Fatal("Unable to decrypt hardcoded subkey: ", err) |
| 1564 |
} |
| 1565 |
|
| 1566 |
testKeyValidateEcdsaOnDecrypt(t, keys[0], randomPassword) |
| 1567 |
}) |
| 1568 |
|
| 1569 |
ecdsaCurves := map[string]packet.Curve{ |
| 1570 |
"NIST P-256": packet.CurveNistP256, |
| 1571 |
"NIST P-384": packet.CurveNistP384, |
| 1572 |
"NIST P-521": packet.CurveNistP521, |
| 1573 |
"Brainpool P-256": packet.CurveBrainpoolP256, |
| 1574 |
"Brainpool P-384": packet.CurveBrainpoolP384, |
| 1575 |
"Brainpool P-512": packet.CurveBrainpoolP512, |
| 1576 |
"SecP256k1": packet.CurveSecP256k1, |
| 1577 |
} |
| 1578 |
|
| 1579 |
for name, curveType := range ecdsaCurves { |
| 1580 |
t.Run("Generated:"+name, func(t *testing.T) { |
| 1581 |
key := testGenerateEC(t, packet.PubKeyAlgoECDSA, curveType) |
| 1582 |
testKeyValidateEcdsaOnDecrypt(t, key, randomPassword) |
| 1583 |
}) |
| 1584 |
} |
| 1585 |
}) |
| 1586 |
|
| 1587 |
t.Run("EdDSA", func(t *testing.T) { |
| 1588 |
eddsaHardcoded := map[string]string{ |
| 1589 |
"Curve25519": curve25519PrivateKey, |
| 1590 |
"Curve448": curve448PrivateKey, |
| 1591 |
} |
| 1592 |
|
| 1593 |
for name, skData := range eddsaHardcoded { |
| 1594 |
t.Run("Hardcoded:"+name, func(t *testing.T) { |
| 1595 |
keys, err := ReadArmoredKeyRing(bytes.NewBufferString(skData)) |
| 1596 |
if err != nil { |
| 1597 |
t.Fatal("Unable to parse hardcoded key: ", err) |
| 1598 |
} |
| 1599 |
|
| 1600 |
testKeyValidateEddsaOnDecrypt(t, keys[0], randomPassword) |
| 1601 |
}) |
| 1602 |
} |
| 1603 |
|
| 1604 |
eddsaCurves := map[string]packet.Curve{ |
| 1605 |
"Curve25519": packet.Curve25519, |
| 1606 |
"Curve448": packet.Curve448, |
| 1607 |
} |
| 1608 |
|
| 1609 |
for name, curveType := range eddsaCurves { |
| 1610 |
t.Run("Generated:"+name, func(t *testing.T) { |
| 1611 |
key := testGenerateEC(t, packet.PubKeyAlgoEdDSA, curveType) |
| 1612 |
testKeyValidateEddsaOnDecrypt(t, key, randomPassword) |
| 1613 |
}) |
| 1614 |
} |
| 1615 |
}) |
| 1616 |
|
| 1617 |
t.Run("DSA With El Gamal Subkey", func(t *testing.T) { |
| 1618 |
testKeyValidateDsaElGamalOnDecrypt(t, randomPassword) |
| 1619 |
}) |
| 1620 |
} |
| 1621 |
|
| 1622 |
func testGenerateRSA(t *testing.T, bits int) *Entity { |
| 1623 |
config := &packet.Config{Algorithm: packet.PubKeyAlgoRSA, RSABits: bits} |
| 1624 |
rsaEntity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", config) |
| 1625 |
if err != nil { |
| 1626 |
t.Fatal(err) |
| 1627 |
} |
| 1628 |
|
| 1629 |
return rsaEntity |
| 1630 |
} |
| 1631 |
|
| 1632 |
func testKeyValidateRsaOnDecrypt(t *testing.T, rsaEntity *Entity, password []byte) { |
| 1633 |
var err error |
| 1634 |
rsaPrimaryKey := rsaEntity.PrivateKey |
| 1635 |
if err = rsaPrimaryKey.Encrypt(password); err != nil { |
| 1636 |
t.Fatal(err) |
| 1637 |
} |
| 1638 |
if err = rsaPrimaryKey.Decrypt(password); err != nil { |
| 1639 |
t.Fatal("Valid RSA key was marked as invalid: ", err) |
| 1640 |
} |
| 1641 |
|
| 1642 |
if err = rsaPrimaryKey.Encrypt(password); err != nil { |
| 1643 |
t.Fatal(err) |
| 1644 |
} |
| 1645 |
|
| 1646 |
// Corrupt public modulo n in primary key |
| 1647 |
n := rsaPrimaryKey.PublicKey.PublicKey.(*rsa.PublicKey).N |
| 1648 |
rsaPrimaryKey.PublicKey.PublicKey.(*rsa.PublicKey).N = new(big.Int).Add(n, big.NewInt(2)) |
| 1649 |
err = rsaPrimaryKey.Decrypt(password) |
| 1650 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1651 |
t.Fatal("Failed to detect invalid RSA key") |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
func testGenerateEC(t *testing.T, algorithm packet.PublicKeyAlgorithm, curve packet.Curve) *Entity { |
| 1656 |
config := &packet.Config{Algorithm: algorithm, Curve: curve} |
| 1657 |
rsaEntity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", config) |
| 1658 |
if err != nil { |
| 1659 |
t.Fatal(err) |
| 1660 |
} |
| 1661 |
|
| 1662 |
return rsaEntity |
| 1663 |
} |
| 1664 |
|
| 1665 |
func testKeyValidateEcdsaOnDecrypt(t *testing.T, ecdsaKey *Entity, password []byte) { |
| 1666 |
var err error |
| 1667 |
ecdsaPrimaryKey := ecdsaKey.PrivateKey |
| 1668 |
|
| 1669 |
if err = ecdsaPrimaryKey.Encrypt(password); err != nil { |
| 1670 |
t.Fatal(err) |
| 1671 |
} |
| 1672 |
|
| 1673 |
if err := ecdsaPrimaryKey.Decrypt(password); err != nil { |
| 1674 |
t.Fatal("Valid ECDSA key was marked as invalid: ", err) |
| 1675 |
} |
| 1676 |
|
| 1677 |
if err = ecdsaPrimaryKey.Encrypt(password); err != nil { |
| 1678 |
t.Fatal(err) |
| 1679 |
} |
| 1680 |
|
| 1681 |
// Corrupt public X in primary key |
| 1682 |
X := ecdsaPrimaryKey.PublicKey.PublicKey.(*ecdsa.PublicKey).X |
| 1683 |
ecdsaPrimaryKey.PublicKey.PublicKey.(*ecdsa.PublicKey).X = new(big.Int).Add(X, big.NewInt(1)) |
| 1684 |
err = ecdsaPrimaryKey.Decrypt(password) |
| 1685 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1686 |
t.Fatal("Failed to detect invalid ECDSA key") |
| 1687 |
} |
| 1688 |
|
| 1689 |
// ECDH |
| 1690 |
ecdsaSubkey := ecdsaKey.Subkeys[0].PrivateKey |
| 1691 |
if err = ecdsaSubkey.Encrypt(password); err != nil { |
| 1692 |
t.Fatal(err) |
| 1693 |
} |
| 1694 |
|
| 1695 |
if err := ecdsaSubkey.Decrypt(password); err != nil { |
| 1696 |
t.Fatal("Valid ECDH key was marked as invalid: ", err) |
| 1697 |
} |
| 1698 |
|
| 1699 |
if err = ecdsaSubkey.Encrypt(password); err != nil { |
| 1700 |
t.Fatal(err) |
| 1701 |
} |
| 1702 |
|
| 1703 |
// Corrupt public X in subkey |
| 1704 |
ecdsaSubkey.PublicKey.PublicKey.(*ecdh.PublicKey).Point[5] ^= 1 |
| 1705 |
|
| 1706 |
err = ecdsaSubkey.Decrypt(password) |
| 1707 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1708 |
t.Fatal("Failed to detect invalid ECDH key") |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
func testKeyValidateEddsaOnDecrypt(t *testing.T, eddsaEntity *Entity, password []byte) { |
| 1713 |
var err error |
| 1714 |
|
| 1715 |
eddsaPrimaryKey := eddsaEntity.PrivateKey // already encrypted |
| 1716 |
if err = eddsaPrimaryKey.Encrypt(password); err != nil { |
| 1717 |
t.Fatal(err) |
| 1718 |
} |
| 1719 |
|
| 1720 |
if err := eddsaPrimaryKey.Decrypt(password); err != nil { |
| 1721 |
t.Fatal("Valid EdDSA key was marked as invalid: ", err) |
| 1722 |
} |
| 1723 |
|
| 1724 |
if err = eddsaPrimaryKey.Encrypt(password); err != nil { |
| 1725 |
t.Fatal(err) |
| 1726 |
} |
| 1727 |
|
| 1728 |
pubKey := *eddsaPrimaryKey.PublicKey.PublicKey.(*eddsa.PublicKey) |
| 1729 |
pubKey.X[10] ^= 1 |
| 1730 |
err = eddsaPrimaryKey.Decrypt(password) |
| 1731 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1732 |
t.Fatal("Failed to detect invalid EdDSA key") |
| 1733 |
} |
| 1734 |
|
| 1735 |
// ECDH |
| 1736 |
ecdhSubkey := eddsaEntity.Subkeys[len(eddsaEntity.Subkeys)-1].PrivateKey |
| 1737 |
if err = ecdhSubkey.Encrypt(password); err != nil { |
| 1738 |
t.Fatal(err) |
| 1739 |
} |
| 1740 |
|
| 1741 |
if err := ecdhSubkey.Decrypt(password); err != nil { |
| 1742 |
t.Fatal("Valid ECDH key was marked as invalid: ", err) |
| 1743 |
} |
| 1744 |
|
| 1745 |
if err = ecdhSubkey.Encrypt(password); err != nil { |
| 1746 |
t.Fatal(err) |
| 1747 |
} |
| 1748 |
|
| 1749 |
// Corrupt public X in subkey |
| 1750 |
ecdhSubkey.PublicKey.PublicKey.(*ecdh.PublicKey).Point[5] ^= 1 |
| 1751 |
err = ecdhSubkey.Decrypt(password) |
| 1752 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1753 |
t.Fatal("Failed to detect invalid ECDH key") |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
// ...the legacy bits |
| 1758 |
func testKeyValidateDsaElGamalOnDecrypt(t *testing.T, randomPassword []byte) { |
| 1759 |
var err error |
| 1760 |
|
| 1761 |
dsaKeys, err := ReadArmoredKeyRing(bytes.NewBufferString(dsaPrivateKeyWithElGamalSubkey)) |
| 1762 |
if err != nil { |
| 1763 |
t.Fatal(err) |
| 1764 |
} |
| 1765 |
dsaPrimaryKey := dsaKeys[0].PrivateKey // already encrypted |
| 1766 |
if err := dsaPrimaryKey.Decrypt([]byte("password")); err != nil { |
| 1767 |
t.Fatal("Valid DSA key was marked as invalid: ", err) |
| 1768 |
} |
| 1769 |
|
| 1770 |
if err = dsaPrimaryKey.Encrypt(randomPassword); err != nil { |
| 1771 |
t.Fatal(err) |
| 1772 |
} |
| 1773 |
// corrupt DSA generator |
| 1774 |
G := dsaPrimaryKey.PublicKey.PublicKey.(*dsa.PublicKey).G |
| 1775 |
dsaPrimaryKey.PublicKey.PublicKey.(*dsa.PublicKey).G = new(big.Int).Add(G, big.NewInt(1)) |
| 1776 |
err = dsaPrimaryKey.Decrypt(randomPassword) |
| 1777 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1778 |
t.Fatal("Failed to detect invalid DSA key") |
| 1779 |
} |
| 1780 |
|
| 1781 |
// ElGamal |
| 1782 |
elGamalSubkey := dsaKeys[0].Subkeys[0].PrivateKey // already encrypted |
| 1783 |
if err := elGamalSubkey.Decrypt([]byte("password")); err != nil { |
| 1784 |
t.Fatal("Valid ElGamal key was marked as invalid: ", err) |
| 1785 |
} |
| 1786 |
|
| 1787 |
if err = elGamalSubkey.Encrypt(randomPassword); err != nil { |
| 1788 |
t.Fatal(err) |
| 1789 |
} |
| 1790 |
|
| 1791 |
// corrupt ElGamal generator |
| 1792 |
G = elGamalSubkey.PublicKey.PublicKey.(*elgamal.PublicKey).G |
| 1793 |
elGamalSubkey.PublicKey.PublicKey.(*elgamal.PublicKey).G = new(big.Int).Add(G, big.NewInt(1)) |
| 1794 |
err = elGamalSubkey.Decrypt(randomPassword) |
| 1795 |
if _, ok := err.(errors.KeyInvalidError); !ok { |
| 1796 |
t.Fatal("Failed to detect invalid ElGamal key") |
| 1797 |
} |
| 1798 |
} |
| 1799 |
|
| 1800 |
var foreignKeysv4 = []string{ |
| 1801 |
v4Key25519, |
| 1802 |
} |
| 1803 |
|
| 1804 |
func TestReadPrivateForeignV4Key(t *testing.T) { |
| 1805 |
for _, str := range foreignKeysv4 { |
| 1806 |
kring, err := ReadArmoredKeyRing(strings.NewReader(str)) |
| 1807 |
if err != nil { |
| 1808 |
t.Fatal(err) |
| 1809 |
} |
| 1810 |
checkV4Key(t, kring[0]) |
| 1811 |
} |
| 1812 |
} |
| 1813 |
|
| 1814 |
func checkV4Key(t *testing.T, ent *Entity) { |
| 1815 |
key := ent.PrimaryKey |
| 1816 |
if key.Version != 4 { |
| 1817 |
t.Errorf("wrong key version %d", key.Version) |
| 1818 |
} |
| 1819 |
if len(key.Fingerprint) != 20 { |
| 1820 |
t.Errorf("Wrong fingerprint length: %d", len(key.Fingerprint)) |
| 1821 |
} |
| 1822 |
signatures := ent.Revocations |
| 1823 |
for _, id := range ent.Identities { |
| 1824 |
signatures = append(signatures, id.SelfCertifications...) |
| 1825 |
} |
| 1826 |
for _, sig := range signatures { |
| 1827 |
if sig == nil { |
| 1828 |
continue |
| 1829 |
} |
| 1830 |
if sig.Packet.Version != 4 { |
| 1831 |
t.Errorf("wrong signature version %d", sig.Packet.Version) |
| 1832 |
} |
| 1833 |
fgptLen := len(sig.Packet.IssuerFingerprint) |
| 1834 |
if fgptLen != 20 { |
| 1835 |
t.Errorf("Wrong fingerprint length in signature: %d", fgptLen) |
| 1836 |
} |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
// Should not panic (generated with go-fuzz) |
| 1841 |
func TestCorruptKeys(t *testing.T) { |
| 1842 |
data := `-----BEGIN PGP PUBLIC KEY BLOCK00000 |
| 1843 |
|
| 1844 |
mQ00BF00000BCAD0000000000000000000000000000000000000000000000000 |
| 1845 |
0000000000000000000000000000000000000000000000000000000000000000 |
| 1846 |
0000000000000000000000000000000000000000000000000000000000000000 |
| 1847 |
0000000000000000000000000000000000000000000000000000000000000000 |
| 1848 |
0000000000000000000000000000000000000000000000000000000000000000 |
| 1849 |
000000000000000000000000000000000000ABE000G0Dn000000000000000000iQ00BB0BAgAGBCG00000` |
| 1850 |
ReadArmoredKeyRing(strings.NewReader(data)) |
| 1851 |
} |
| 1852 |
|
| 1853 |
func TestMultiIdentity(t *testing.T) { |
| 1854 |
data := `-----BEGIN PGP PUBLIC KEY BLOCK----- |
| 1855 |
|
| 1856 |
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 |
| 1906 |
zaXZE2aAMQ== |
| 1907 |
=Ty4h |
| 1908 |
-----END PGP PUBLIC KEY BLOCK-----` |
| 1909 |
key, err := ReadArmoredKeyRing(strings.NewReader(data)) |
| 1910 |
if err != nil { |
| 1911 |
t.Fatal(err) |
| 1912 |
} |
| 1913 |
var config *packet.Config |
| 1914 |
sig, _ := key[0].PrimaryIdentity(config.Now(), &packet.Config{}) |
| 1915 |
if sig.IsPrimaryId == nil || !*sig.IsPrimaryId { |
| 1916 |
t.Fatal("expected primary identity to be selected") |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
func TestParseKeyWithUnsupportedSubkey(t *testing.T) { |
| 1921 |
data := `-----BEGIN PGP PUBLIC KEY BLOCK----- |
| 1922 |
|
| 1923 |
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 |
| 1963 |
ruxTPhR2zcmefHKGU7cCC/uo |
| 1964 |
=KLLX |
| 1965 |
-----END PGP PUBLIC KEY BLOCK-----` |
| 1966 |
_, err := ReadArmoredKeyRing(strings.NewReader(data)) |
| 1967 |
if err != nil { |
| 1968 |
t.Fatal(err) |
| 1969 |
} |
| 1970 |
} |
| 1971 |
|
| 1972 |
func TestParseUnsupportedSubkey(t *testing.T) { |
| 1973 |
data := `-----BEGIN PGP PUBLIC KEY BLOCK----- |
| 1974 |
|
| 1975 |
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 |
| 2015 |
NciH07RTRuMS/aRhRg4OB8PQROmTnZ+iZS0= |
| 2016 |
=7DF0 |
| 2017 |
-----END PGP PUBLIC KEY BLOCK----- |
| 2018 |
` |
| 2019 |
_, err := ReadArmoredKeyRing(strings.NewReader(data)) |
| 2020 |
if err != nil { |
| 2021 |
t.Fatal(err) |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
func TestAllowAllKeyFlagsWhenMissing(t *testing.T) { |
| 2026 |
// Make a master key. |
| 2027 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 2028 |
if err != nil { |
| 2029 |
t.Fatal(err) |
| 2030 |
} |
| 2031 |
|
| 2032 |
config := &packet.Config{} |
| 2033 |
|
| 2034 |
primarySelfSignature, err := entity.VerifyPrimaryKey(time.Now(), config) |
| 2035 |
if err != nil { |
| 2036 |
t.Fatal(err) |
| 2037 |
} |
| 2038 |
|
| 2039 |
if !entity.PrimaryKey.PubKeyAlgo.CanEncrypt() || |
| 2040 |
!entity.PrimaryKey.PubKeyAlgo.CanSign() { |
| 2041 |
t.Fatal("PubKeyAlgo must be valid for signature and encryption") |
| 2042 |
} |
| 2043 |
|
| 2044 |
/// Flags valid, but not set. |
| 2045 |
primarySelfSignature.FlagsValid = true |
| 2046 |
primarySelfSignature.FlagSign = false |
| 2047 |
primarySelfSignature.FlagCertify = false |
| 2048 |
primarySelfSignature.FlagEncryptCommunications = false |
| 2049 |
|
| 2050 |
if isValidSigningKey(primarySelfSignature, entity.PrimaryKey.PubKeyAlgo, config) { |
| 2051 |
t.Error("isValidSigningKey must be false") |
| 2052 |
} |
| 2053 |
|
| 2054 |
if isValidEncryptionKey(primarySelfSignature, entity.PrimaryKey.PubKeyAlgo, config) { |
| 2055 |
t.Error("isValidEncryptionKey must be false") |
| 2056 |
} |
| 2057 |
|
| 2058 |
if isValidCertificationKey(primarySelfSignature, entity.PrimaryKey.PubKeyAlgo, config) { |
| 2059 |
t.Error("isValidCertificationKey must be false") |
| 2060 |
} |
| 2061 |
|
| 2062 |
/// Flags not valid, but InsecureAllowAllKeyFlagsWhenMissing set. |
| 2063 |
primarySelfSignature.FlagsValid = false |
| 2064 |
config = &packet.Config{InsecureAllowAllKeyFlagsWhenMissing: true} |
| 2065 |
|
| 2066 |
if !isValidSigningKey(primarySelfSignature, entity.PrimaryKey.PubKeyAlgo, config) { |
| 2067 |
t.Error("isValidSigningKey must be true when InsecureAllowAllKeyFlagsWhenMissing is true") |
| 2068 |
} |
| 2069 |
|
| 2070 |
if !isValidEncryptionKey(primarySelfSignature, entity.PrimaryKey.PubKeyAlgo, config) { |
| 2071 |
t.Error("isValidEncryptionKey must be true when InsecureAllowAllKeyFlagsWhenMissing is true") |
| 2072 |
} |
| 2073 |
|
| 2074 |
if !isValidCertificationKey(primarySelfSignature, entity.PrimaryKey.PubKeyAlgo, config) { |
| 2075 |
t.Error("isValidCertificationKey must be true when InsecureAllowAllKeyFlagsWhenMissing is true") |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
func TestEncryptionKeyError(t *testing.T) { |
| 2080 |
// Make a master key. |
| 2081 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", nil) |
| 2082 |
if err != nil { |
| 2083 |
t.Fatal(err) |
| 2084 |
} |
| 2085 |
|
| 2086 |
_, err = entity.EncryptionKeyWithError(time.Unix(1405544146, 0), nil) |
| 2087 |
if err == nil { |
| 2088 |
t.Fatal("should fail") |
| 2089 |
} |
| 2090 |
if !strings.Contains(err.Error(), "no valid self signature found") { |
| 2091 |
t.Fatal("wrong error") |
| 2092 |
} |
| 2093 |
|
| 2094 |
entity.Subkeys[0].PublicKey.Version = 20 |
| 2095 |
_, err = entity.EncryptionKeyWithError(time.Now(), nil) |
| 2096 |
if err == nil { |
| 2097 |
t.Fatal("should fail") |
| 2098 |
} |
| 2099 |
if !strings.Contains(err.Error(), "no valid binding signature found for subkey") { |
| 2100 |
t.Fatal("wrong error") |
| 2101 |
} |
| 2102 |
|
| 2103 |
entity.Subkeys = nil |
| 2104 |
_, err = entity.EncryptionKeyWithError(time.Now(), nil) |
| 2105 |
if err == nil { |
| 2106 |
t.Fatal("should fail") |
| 2107 |
} |
| 2108 |
if !strings.Contains(err.Error(), "no encryption-capable key found (no key flags or invalid algorithm)") { |
| 2109 |
t.Fatal("wrong error") |
| 2110 |
} |
| 2111 |
} |
| 2112 |
|
| 2113 |
func TestGenerateRSAKeyPLessThanQ(t *testing.T) { |
| 2114 |
// RFC 9580 section 5.5.5.1 requires p < q for RSA keys. |
| 2115 |
checkKey := func(t *testing.T, key *rsa.PrivateKey, err error) { |
| 2116 |
t.Helper() |
| 2117 |
if err != nil { |
| 2118 |
t.Fatal(err) |
| 2119 |
} |
| 2120 |
// Primes[1] is serialized as p and Primes[0] as q. |
| 2121 |
if key.Primes[1].Cmp(key.Primes[0]) >= 0 { |
| 2122 |
t.Fatal("expected p < q") |
| 2123 |
} |
| 2124 |
if err := key.Validate(); err != nil { |
| 2125 |
t.Fatal(err) |
| 2126 |
} |
| 2127 |
} |
| 2128 |
|
| 2129 |
t.Run("enforceRSAPrimeOrder", func(t *testing.T) { |
| 2130 |
key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 2131 |
if err != nil { |
| 2132 |
t.Fatal(err) |
| 2133 |
} |
| 2134 |
// Force p > q so that the swap is always exercised. |
| 2135 |
if key.Primes[0].Cmp(key.Primes[1]) > 0 { |
| 2136 |
key.Primes[0], key.Primes[1] = key.Primes[1], key.Primes[0] |
| 2137 |
key.Precomputed = rsa.PrecomputedValues{} |
| 2138 |
key.Precompute() |
| 2139 |
} |
| 2140 |
enforceRSAPrimeOrder(key) |
| 2141 |
checkKey(t, key, nil) |
| 2142 |
}) |
| 2143 |
|
| 2144 |
t.Run("generateRSAKey", func(t *testing.T) { |
| 2145 |
key, err := generateRSAKey(rand.Reader, 2048) |
| 2146 |
checkKey(t, key, err) |
| 2147 |
}) |
| 2148 |
|
| 2149 |
t.Run("generateRSAKeyWithPrimes", func(t *testing.T) { |
| 2150 |
key, err := generateRSAKeyWithPrimes(rand.Reader, 2, 2048, nil) |
| 2151 |
checkKey(t, key, err) |
| 2152 |
}) |
| 2153 |
} |