@@ -0,0 +1,333 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package gpgkey |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "errors" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/ProtonMail/go-crypto/openpgp" |
| 13 | + "github.com/ProtonMail/go-crypto/openpgp/armor" |
| 14 | + "github.com/ProtonMail/go-crypto/openpgp/packet" |
| 15 | +) |
| 16 | + |
| 17 | +// ─── fixture helpers ──────────────────────────────────────────────── |
| 18 | +// |
| 19 | +// We synthesize all test fixtures in-memory via ProtonMail/go-crypto |
| 20 | +// rather than shipping committed .asc files. Tests then exercise the |
| 21 | +// real codec end-to-end (serialize → parse → assert) without depending |
| 22 | +// on a system gpg binary. |
| 23 | + |
| 24 | +// newEd25519 returns a freshly-generated ed25519 entity with a single |
| 25 | +// UID. ProtonMail's nil-config default is RSA-2048; we have to ask |
| 26 | +// for EdDSA explicitly. |
| 27 | +func newEd25519(t *testing.T, email string) *openpgp.Entity { |
| 28 | + t.Helper() |
| 29 | + e, err := openpgp.NewEntity("shithub-test", "", email, &packet.Config{ |
| 30 | + Algorithm: packet.PubKeyAlgoEdDSA, |
| 31 | + }) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("NewEntity ed25519: %v", err) |
| 34 | + } |
| 35 | + return e |
| 36 | +} |
| 37 | + |
| 38 | +// newRSA returns an RSA entity at the requested bit size. |
| 39 | +func newRSA(t *testing.T, email string, bits int) *openpgp.Entity { |
| 40 | + t.Helper() |
| 41 | + e, err := openpgp.NewEntity("shithub-test", "", email, &packet.Config{ |
| 42 | + Algorithm: packet.PubKeyAlgoRSA, |
| 43 | + RSABits: bits, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("NewEntity rsa%d: %v", bits, err) |
| 47 | + } |
| 48 | + return e |
| 49 | +} |
| 50 | + |
| 51 | +// armoredPublic serializes an entity's public-key block as ASCII armor. |
| 52 | +func armoredPublic(t *testing.T, e *openpgp.Entity) string { |
| 53 | + t.Helper() |
| 54 | + var buf bytes.Buffer |
| 55 | + w, err := armor.Encode(&buf, "PGP PUBLIC KEY BLOCK", nil) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("armor.Encode: %v", err) |
| 58 | + } |
| 59 | + if err := e.Serialize(w); err != nil { |
| 60 | + t.Fatalf("entity.Serialize: %v", err) |
| 61 | + } |
| 62 | + if err := w.Close(); err != nil { |
| 63 | + t.Fatalf("armor close: %v", err) |
| 64 | + } |
| 65 | + return buf.String() |
| 66 | +} |
| 67 | + |
| 68 | +// armoredPrivate serializes the SECRET key block — the "user uploaded |
| 69 | +// their private key by mistake" fixture. |
| 70 | +func armoredPrivate(t *testing.T, e *openpgp.Entity) string { |
| 71 | + t.Helper() |
| 72 | + var buf bytes.Buffer |
| 73 | + w, err := armor.Encode(&buf, "PGP PRIVATE KEY BLOCK", nil) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("armor.Encode private: %v", err) |
| 76 | + } |
| 77 | + if err := e.SerializePrivate(w, nil); err != nil { |
| 78 | + t.Fatalf("entity.SerializePrivate: %v", err) |
| 79 | + } |
| 80 | + if err := w.Close(); err != nil { |
| 81 | + t.Fatalf("armor close: %v", err) |
| 82 | + } |
| 83 | + return buf.String() |
| 84 | +} |
| 85 | + |
| 86 | +// armoredDetachedSig returns an armored detached signature over a |
| 87 | +// small payload — the "user uploaded a signature by mistake" fixture. |
| 88 | +func armoredDetachedSig(t *testing.T, e *openpgp.Entity) string { |
| 89 | + t.Helper() |
| 90 | + var buf bytes.Buffer |
| 91 | + w, err := armor.Encode(&buf, "PGP SIGNATURE", nil) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("armor.Encode sig: %v", err) |
| 94 | + } |
| 95 | + if err := openpgp.DetachSign(w, e, strings.NewReader("hello"), nil); err != nil { |
| 96 | + t.Fatalf("DetachSign: %v", err) |
| 97 | + } |
| 98 | + if err := w.Close(); err != nil { |
| 99 | + t.Fatalf("armor close: %v", err) |
| 100 | + } |
| 101 | + return buf.String() |
| 102 | +} |
| 103 | + |
| 104 | +// ─── happy-path tests ─────────────────────────────────────────────── |
| 105 | + |
| 106 | +func TestParse_Ed25519(t *testing.T) { |
| 107 | + e := newEd25519(t, "alice@shithub.test") |
| 108 | + armored := armoredPublic(t, e) |
| 109 | + |
| 110 | + got, err := Parse("My laptop", armored) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("Parse: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if got.Name != "My laptop" { |
| 116 | + t.Errorf("Name: got %q, want %q", got.Name, "My laptop") |
| 117 | + } |
| 118 | + if len(got.Fingerprint) != 40 { |
| 119 | + t.Errorf("Fingerprint length: got %d, want 40", len(got.Fingerprint)) |
| 120 | + } |
| 121 | + if !isHex(got.Fingerprint) { |
| 122 | + t.Errorf("Fingerprint not hex: %q", got.Fingerprint) |
| 123 | + } |
| 124 | + if len(got.KeyID) != 16 || !isHex(got.KeyID) { |
| 125 | + t.Errorf("KeyID malformed: %q", got.KeyID) |
| 126 | + } |
| 127 | + if !strings.HasSuffix(got.Fingerprint, got.KeyID) { |
| 128 | + t.Errorf("KeyID should be lower 16 of fingerprint: fp=%s key_id=%s", got.Fingerprint, got.KeyID) |
| 129 | + } |
| 130 | + if got.PrimaryAlgo != "ed25519" { |
| 131 | + t.Errorf("PrimaryAlgo: got %q, want ed25519", got.PrimaryAlgo) |
| 132 | + } |
| 133 | + if !got.CanSign { |
| 134 | + t.Error("expected CanSign=true for default ed25519 primary") |
| 135 | + } |
| 136 | + if !got.CanCertify { |
| 137 | + t.Error("expected CanCertify=true for default ed25519 primary") |
| 138 | + } |
| 139 | + if got.ExpiresAt != nil { |
| 140 | + t.Errorf("expected ExpiresAt=nil for default no-expiry key; got %v", got.ExpiresAt) |
| 141 | + } |
| 142 | + if len(got.UIDs) != 1 || got.UIDs[0] != "alice@shithub.test" { |
| 143 | + t.Errorf("UIDs: got %v, want [alice@shithub.test]", got.UIDs) |
| 144 | + } |
| 145 | + // Default openpgp.NewEntity creates one encryption subkey. |
| 146 | + if len(got.Subkeys) < 1 { |
| 147 | + t.Errorf("expected at least one subkey; got %d", len(got.Subkeys)) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestParse_RSA4096(t *testing.T) { |
| 152 | + e := newRSA(t, "bob@shithub.test", 4096) |
| 153 | + armored := armoredPublic(t, e) |
| 154 | + got, err := Parse("", armored) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("Parse: %v", err) |
| 157 | + } |
| 158 | + if got.PrimaryAlgo != "rsa4096" { |
| 159 | + t.Errorf("PrimaryAlgo: got %q, want rsa4096", got.PrimaryAlgo) |
| 160 | + } |
| 161 | + if !got.CanSign || !got.CanCertify { |
| 162 | + t.Errorf("expected sign+certify on RSA primary; got sign=%t certify=%t", got.CanSign, got.CanCertify) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func TestParse_EncryptOnly_Accepted(t *testing.T) { |
| 167 | + // Build an entity, then strip its primary's sign+certify flags so |
| 168 | + // it's encrypt-only. Re-issue the self-signature so the modified |
| 169 | + // flags persist through Serialize. |
| 170 | + e := newRSA(t, "encryptonly@shithub.test", 2048) |
| 171 | + for _, id := range e.Identities { |
| 172 | + id.SelfSignature.FlagSign = false |
| 173 | + id.SelfSignature.FlagCertify = false |
| 174 | + id.SelfSignature.FlagEncryptCommunications = true |
| 175 | + id.SelfSignature.FlagEncryptStorage = true |
| 176 | + // Re-sign with the modified flags. |
| 177 | + if err := id.SelfSignature.SignUserId(id.UserId.Id, e.PrimaryKey, e.PrivateKey, nil); err != nil { |
| 178 | + t.Fatalf("re-sign identity: %v", err) |
| 179 | + } |
| 180 | + } |
| 181 | + armored := armoredPublic(t, e) |
| 182 | + |
| 183 | + got, err := Parse("encryption only key", armored) |
| 184 | + if err != nil { |
| 185 | + t.Fatalf("Parse should accept encryption-only keys (gh parity); got: %v", err) |
| 186 | + } |
| 187 | + if got.CanSign { |
| 188 | + t.Error("CanSign: got true, want false on encryption-only primary") |
| 189 | + } |
| 190 | + if !got.CanEncryptComms && !got.CanEncryptStorage { |
| 191 | + t.Error("expected at least one encrypt-* flag true") |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func TestParse_MultiSubkey(t *testing.T) { |
| 196 | + e := newEd25519(t, "multi@shithub.test") |
| 197 | + // Add an extra signing subkey. ProtonMail/go-crypto's AddSigningSubkey |
| 198 | + // requires a Config to specify the algorithm. |
| 199 | + if err := e.AddSigningSubkey(nil); err != nil { |
| 200 | + t.Fatalf("AddSigningSubkey: %v", err) |
| 201 | + } |
| 202 | + armored := armoredPublic(t, e) |
| 203 | + got, err := Parse("", armored) |
| 204 | + if err != nil { |
| 205 | + t.Fatalf("Parse: %v", err) |
| 206 | + } |
| 207 | + if len(got.Subkeys) < 2 { |
| 208 | + t.Errorf("expected >=2 subkeys (one encryption from default, one we added); got %d", len(got.Subkeys)) |
| 209 | + } |
| 210 | + // At least one subkey should have can_sign. |
| 211 | + anySigning := false |
| 212 | + for _, sk := range got.Subkeys { |
| 213 | + if sk.CanSign { |
| 214 | + anySigning = true |
| 215 | + break |
| 216 | + } |
| 217 | + } |
| 218 | + if !anySigning { |
| 219 | + t.Error("expected at least one signing subkey") |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +// ─── rejection tests ──────────────────────────────────────────────── |
| 224 | + |
| 225 | +func TestParse_PrivateKeyBlock(t *testing.T) { |
| 226 | + e := newEd25519(t, "private@shithub.test") |
| 227 | + armored := armoredPrivate(t, e) |
| 228 | + _, err := Parse("", armored) |
| 229 | + if !errors.Is(err, ErrPrivateKeyBlock) { |
| 230 | + t.Errorf("err: got %v, want ErrPrivateKeyBlock", err) |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +func TestParse_SignatureBlock(t *testing.T) { |
| 235 | + e := newEd25519(t, "sig@shithub.test") |
| 236 | + armored := armoredDetachedSig(t, e) |
| 237 | + _, err := Parse("", armored) |
| 238 | + if !errors.Is(err, ErrSignatureBlock) { |
| 239 | + t.Errorf("err: got %v, want ErrSignatureBlock", err) |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +func TestParse_Expired(t *testing.T) { |
| 244 | + // Create an entity with a backdated creation time + a short lifetime |
| 245 | + // so the key is already expired by `time.Now()`. |
| 246 | + past := time.Now().Add(-48 * time.Hour) |
| 247 | + cfg := &packet.Config{ |
| 248 | + Time: func() time.Time { return past }, |
| 249 | + } |
| 250 | + e, err := openpgp.NewEntity("shithub-expired", "", "expired@shithub.test", cfg) |
| 251 | + if err != nil { |
| 252 | + t.Fatalf("NewEntity: %v", err) |
| 253 | + } |
| 254 | + // 1-hour lifetime from "past" → expired ~47 hours ago. |
| 255 | + oneHour := uint32(3600) |
| 256 | + for _, id := range e.Identities { |
| 257 | + id.SelfSignature.KeyLifetimeSecs = &oneHour |
| 258 | + if err := id.SelfSignature.SignUserId(id.UserId.Id, e.PrimaryKey, e.PrivateKey, cfg); err != nil { |
| 259 | + t.Fatalf("re-sign for expiry: %v", err) |
| 260 | + } |
| 261 | + } |
| 262 | + armored := armoredPublic(t, e) |
| 263 | + _, err = Parse("", armored) |
| 264 | + if !errors.Is(err, ErrExpired) { |
| 265 | + t.Errorf("err: got %v, want ErrExpired", err) |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +func TestParse_RSATooShort(t *testing.T) { |
| 270 | + e := newRSA(t, "short@shithub.test", 1024) |
| 271 | + armored := armoredPublic(t, e) |
| 272 | + _, err := Parse("", armored) |
| 273 | + if !errors.Is(err, ErrRSATooShort) { |
| 274 | + t.Errorf("err: got %v, want ErrRSATooShort", err) |
| 275 | + } |
| 276 | +} |
| 277 | + |
| 278 | +func TestParse_Garbage(t *testing.T) { |
| 279 | + _, err := Parse("", "not a key at all, just garbage") |
| 280 | + if !errors.Is(err, ErrUnparseable) { |
| 281 | + t.Errorf("err: got %v, want ErrUnparseable", err) |
| 282 | + } |
| 283 | +} |
| 284 | + |
| 285 | +func TestParse_Empty(t *testing.T) { |
| 286 | + _, err := Parse("", "") |
| 287 | + if !errors.Is(err, ErrUnparseable) { |
| 288 | + t.Errorf("err: got %v, want ErrUnparseable", err) |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +func TestParse_LeadingWhitespaceTolerated(t *testing.T) { |
| 293 | + e := newEd25519(t, "ws@shithub.test") |
| 294 | + armored := "\n\n \t" + armoredPublic(t, e) |
| 295 | + if _, err := Parse("", armored); err != nil { |
| 296 | + t.Errorf("Parse should trim leading whitespace; got %v", err) |
| 297 | + } |
| 298 | +} |
| 299 | + |
| 300 | +// ─── name-validation tests ────────────────────────────────────────── |
| 301 | + |
| 302 | +func TestParse_NameTooLong(t *testing.T) { |
| 303 | + e := newEd25519(t, "n@shithub.test") |
| 304 | + armored := armoredPublic(t, e) |
| 305 | + long := strings.Repeat("x", 81) |
| 306 | + _, err := Parse(long, armored) |
| 307 | + if !errors.Is(err, ErrNameTooLong) { |
| 308 | + t.Errorf("err: got %v, want ErrNameTooLong", err) |
| 309 | + } |
| 310 | +} |
| 311 | + |
| 312 | +func TestParse_NameControlChars(t *testing.T) { |
| 313 | + e := newEd25519(t, "n@shithub.test") |
| 314 | + armored := armoredPublic(t, e) |
| 315 | + _, err := Parse("bad\x00name", armored) |
| 316 | + if !errors.Is(err, ErrNameControl) { |
| 317 | + t.Errorf("err: got %v, want ErrNameControl", err) |
| 318 | + } |
| 319 | +} |
| 320 | + |
| 321 | +// ─── helpers ──────────────────────────────────────────────────────── |
| 322 | + |
| 323 | +func isHex(s string) bool { |
| 324 | + for _, r := range s { |
| 325 | + switch { |
| 326 | + case r >= '0' && r <= '9': |
| 327 | + case r >= 'a' && r <= 'f': |
| 328 | + default: |
| 329 | + return false |
| 330 | + } |
| 331 | + } |
| 332 | + return true |
| 333 | +} |