Debian Codegraph @

openpgp/keys_v6_test.go

raw
1 package openpgp
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/rand"
7 "strings"
8 "testing"
9 "time"
10
11 "github.com/ProtonMail/go-crypto/openpgp/errors"
12 "github.com/ProtonMail/go-crypto/openpgp/mldsa_eddsa"
13 "github.com/ProtonMail/go-crypto/openpgp/mlkem_ecdh"
14
15 "github.com/ProtonMail/go-crypto/openpgp/packet"
16 )
17
18 var foreignKeysV6 = []string{
19 v6PrivKey,
20 v6ArgonSealedPrivKey,
21 }
22
23 func TestReadPrivateForeignV6Key(t *testing.T) {
24 for _, str := range foreignKeysV6 {
25 kring, err := ReadArmoredKeyRing(strings.NewReader(str))
26 if err != nil {
27 t.Fatal(err)
28 }
29 checkV6Key(t, kring[0])
30 }
31 }
32
33 func TestReadPrivateForeignV6KeyAndDecrypt(t *testing.T) {
34 password := []byte("correct horse battery staple")
35 kring, err := ReadArmoredKeyRing(strings.NewReader(v6ArgonSealedPrivKey))
36 if err != nil {
37 t.Fatal(err)
38 }
39 key := kring[0]
40 if key.PrivateKey != nil && key.PrivateKey.Encrypted {
41 if err := key.PrivateKey.Decrypt(password); err != nil {
42 t.Fatal(err)
43 }
44 }
45 for _, sub := range key.Subkeys {
46 if sub.PrivateKey != nil && sub.PrivateKey.Encrypted {
47 if err := key.PrivateKey.Decrypt(password); err != nil {
48 t.Fatal(err)
49 }
50 }
51 }
52 checkV6Key(t, kring[0])
53 }
54
55 func TestReadPrivateEncryptedV6Key(t *testing.T) {
56 c := &packet.Config{V6Keys: true}
57 e, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c)
58 if err != nil {
59 t.Fatal(err)
60 }
61 password := []byte("test v6 key # password")
62 // Encrypt private key
63 if err = e.PrivateKey.Encrypt(password); err != nil {
64 t.Fatal(err)
65 }
66 // Encrypt subkeys
67 for _, sub := range e.Subkeys {
68 if err = sub.PrivateKey.Encrypt(password); err != nil {
69 t.Fatal(err)
70 }
71 }
72 // Serialize, Read
73 serializedEntity := bytes.NewBuffer(nil)
74 err = e.SerializePrivateWithoutSigning(serializedEntity, nil)
75 if err != nil {
76 t.Fatal(err)
77 }
78 el, err := ReadKeyRing(serializedEntity)
79 if err != nil {
80 t.Fatal(err)
81 }
82
83 // Decrypt
84 if el[0].PrivateKey == nil {
85 t.Fatal("No private key found")
86 }
87 if err = el[0].PrivateKey.Decrypt(password); err != nil {
88 t.Error(err)
89 }
90
91 // Decrypt subkeys
92 for _, sub := range e.Subkeys {
93 if err = sub.PrivateKey.Decrypt(password); err != nil {
94 t.Error(err)
95 }
96 }
97
98 checkV6Key(t, el[0])
99 }
100
101 func TestNewEntitySerializeV6Key(t *testing.T) {
102 c := &packet.Config{V6Keys: true}
103 e, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c)
104 if err != nil {
105 t.Fatal(err)
106 }
107 checkSerializeReadv6(t, e)
108 }
109
110 func TestNewEntityV6Key(t *testing.T) {
111 c := &packet.Config{
112 V6Keys: true,
113 }
114 e, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c)
115 if err != nil {
116 t.Fatal(err)
117 }
118 checkV6Key(t, e)
119 }
120
121 func checkV6Key(t *testing.T, ent *Entity) {
122 key := ent.PrimaryKey
123 if key.Version != 6 {
124 t.Errorf("wrong key version %d", key.Version)
125 }
126 if len(key.Fingerprint) != 32 {
127 t.Errorf("Wrong fingerprint length: %d", len(key.Fingerprint))
128 }
129 signatures := ent.Revocations
130 for _, id := range ent.Identities {
131 signatures = append(signatures, id.SelfSignature)
132 signatures = append(signatures, id.Signatures...)
133 }
134 for _, sig := range signatures {
135 if sig == nil {
136 continue
137 }
138 if sig.Version != 6 {
139 t.Errorf("wrong signature version %d", sig.Version)
140 }
141 fgptLen := len(sig.IssuerFingerprint)
142 if fgptLen != 32 {
143 t.Errorf("Wrong fingerprint length in signature: %d", fgptLen)
144 }
145 }
146 }
147
148 func checkSerializeReadv6(t *testing.T, e *Entity) {
149 // Entity serialize
150 serializedEntity := bytes.NewBuffer(nil)
151 err := e.Serialize(serializedEntity)
152 if err != nil {
153 t.Fatal(err)
154 }
155 el, err := ReadKeyRing(serializedEntity)
156 if err != nil {
157 t.Fatal(err)
158 }
159 checkV6Key(t, el[0])
160
161 // Without signing
162 serializedEntity = bytes.NewBuffer(nil)
163 err = e.SerializePrivateWithoutSigning(serializedEntity, nil)
164 if err != nil {
165 t.Fatal(err)
166 }
167 el, err = ReadKeyRing(serializedEntity)
168 if err != nil {
169 t.Fatal(err)
170 }
171 checkV6Key(t, el[0])
172
173 // Private
174 serializedEntity = bytes.NewBuffer(nil)
175 err = e.SerializePrivate(serializedEntity, nil)
176 if err != nil {
177 t.Fatal(err)
178 }
179 el, err = ReadKeyRing(serializedEntity)
180 if err != nil {
181 t.Fatal(err)
182 }
183 checkV6Key(t, el[0])
184 }
185
186 func TestNewEntityWithDefaultHashv6(t *testing.T) {
187 for _, hash := range hashes[:5] {
188 c := &packet.Config{
189 V6Keys: true,
190 DefaultHash: hash,
191 }
192 entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", c)
193 if hash == crypto.SHA1 {
194 if err == nil {
195 t.Fatal("should fail on SHA1 key creation")
196 }
197 continue
198 }
199 prefs := entity.SelfSignature.PreferredHash
200 if prefs == nil {
201 t.Fatal(err)
202 }
203 }
204 }
205
206 func TestGeneratePqKey(t *testing.T) {
207 randomPassword := make([]byte, 128)
208 _, err := rand.Read(randomPassword)
209 if err != nil {
210 t.Fatal(err)
211 }
212
213 asymmAlgos := map[string]packet.PublicKeyAlgorithm{
214 "ML-DSA65_Ed25519": packet.PubKeyAlgoMldsa65Ed25519,
215 "ML-DSA87_Ed448": packet.PubKeyAlgoMldsa87Ed448,
216 }
217
218 for name, algo := range asymmAlgos {
219 t.Run(name, func(t *testing.T) {
220 config := &packet.Config{
221 DefaultHash: crypto.SHA512,
222 Algorithm: algo,
223 V6Keys: true,
224 DefaultCipher: packet.CipherAES256,
225 AEADConfig: &packet.AEADConfig{
226 DefaultMode: packet.AEADModeOCB,
227 },
228 Time: func() time.Time {
229 parsed, _ := time.Parse("2006-01-02", "2013-07-01")
230 return parsed
231 },
232 }
233
234 entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", config)
235 if err != nil {
236 t.Fatal(err)
237 }
238
239 serializedEntity := bytes.NewBuffer(nil)
240 err = entity.SerializePrivate(serializedEntity, nil)
241 if err != nil {
242 t.Fatalf("Failed to serialize entity: %s", err)
243 }
244
245 read, err := ReadEntity(packet.NewReader(bytes.NewBuffer(serializedEntity.Bytes())))
246 if err != nil {
247 t.Fatalf("Failed to parse entity: %s", err)
248 }
249
250 if read.PrimaryKey.PubKeyAlgo != algo {
251 t.Fatalf("Expected subkey algorithm: %v, got: %v", algo, read.PrimaryKey.PubKeyAlgo)
252 }
253
254 if err = read.PrivateKey.Encrypt(randomPassword); err != nil {
255 t.Fatal(err)
256 }
257
258 if err := read.PrivateKey.Decrypt(randomPassword); err != nil {
259 t.Fatal("Valid ML-DSA key was marked as invalid: ", err)
260 }
261
262 if err = read.PrivateKey.Encrypt(randomPassword); err != nil {
263 t.Fatal(err)
264 }
265
266 if pk, ok := read.PrivateKey.PublicKey.PublicKey.(*mldsa_eddsa.PublicKey); ok {
267 bin, err := pk.PublicMldsa.MarshalBinary()
268 if err != nil {
269 t.Fatal(err)
270 }
271 bin[5] ^= 1
272 if pk.PublicMldsa, err = pk.Mldsa.UnmarshalBinaryPublicKey(bin); err != nil {
273 t.Fatal(err)
274 }
275 }
276
277 err = read.PrivateKey.Decrypt(randomPassword)
278 if _, ok := err.(errors.KeyInvalidError); !ok {
279 t.Fatal("Failed to detect invalid ML-DSA key")
280 }
281
282 testMlkemSubkey(t, read.Subkeys[0], randomPassword)
283 })
284 }
285 }
286
287 func testMlkemSubkey(t *testing.T, subkey Subkey, randomPassword []byte) {
288 var err error
289 if err = subkey.PrivateKey.Encrypt(randomPassword); err != nil {
290 t.Fatal(err)
291 }
292
293 if err = subkey.PrivateKey.Decrypt(randomPassword); err != nil {
294 t.Fatal("Valid ML-KEM key was marked as invalid: ", err)
295 }
296
297 if err = subkey.PrivateKey.Encrypt(randomPassword); err != nil {
298 t.Fatal(err)
299 }
300
301 // Corrupt public ML-KEM in primary key
302 if pk, ok := subkey.PublicKey.PublicKey.(*mlkem_ecdh.PublicKey); ok {
303 bin, _ := pk.PublicMlkem.MarshalBinary()
304 bin[5] ^= 1
305 if pk.PublicMlkem, err = pk.Mlkem.UnmarshalBinaryPublicKey(bin); err != nil {
306 t.Fatal("unable to corrupt key")
307 }
308 } else {
309 t.Fatal("Invalid subkey")
310 }
311
312 err = subkey.PrivateKey.Decrypt(randomPassword)
313 if _, ok := err.(errors.KeyInvalidError); !ok {
314 t.Fatal("Failed to detect invalid ML-KEM key")
315 }
316 }
317
318 func TestAddV6MlkemSubkey(t *testing.T) {
319 eddsaConfig := &packet.Config{
320 DefaultHash: crypto.SHA512,
321 Algorithm: packet.PubKeyAlgoEd25519,
322 V6Keys: true,
323 DefaultCipher: packet.CipherAES256,
324 AEADConfig: &packet.AEADConfig{
325 DefaultMode: packet.AEADModeOCB,
326 },
327 Time: func() time.Time {
328 parsed, _ := time.Parse("2006-01-02", "2013-07-01")
329 return parsed
330 },
331 }
332
333 entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", eddsaConfig)
334 if err != nil {
335 t.Fatal(err)
336 }
337
338 testAddMlkemSubkey(t, entity, true)
339 }