| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 11 |
) |
| 12 |
|
| 13 |
var foreignKeysV6 = []string{ |
| 14 |
v6PrivKey, |
| 15 |
v6ArgonSealedPrivKey, |
| 16 |
} |
| 17 |
|
| 18 |
func TestReadPrivateForeignV6Key(t *testing.T) { |
| 19 |
for _, str := range foreignKeysV6 { |
| 20 |
kring, err := ReadArmoredKeyRing(strings.NewReader(str)) |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
checkV6Key(t, kring[0]) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func TestReadPrivateForeignV6KeyAndDecrypt(t *testing.T) { |
| 29 |
password := []byte("correct horse battery staple") |
| 30 |
kring, err := ReadArmoredKeyRing(strings.NewReader(v6ArgonSealedPrivKey)) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
key := kring[0] |
| 35 |
if key.PrivateKey != nil && key.PrivateKey.Encrypted { |
| 36 |
if err := key.PrivateKey.Decrypt(password); err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
} |
| 40 |
for _, sub := range key.Subkeys { |
| 41 |
if sub.PrivateKey != nil && sub.PrivateKey.Encrypted { |
| 42 |
if err := key.PrivateKey.Decrypt(password); err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
checkV6Key(t, kring[0]) |
| 48 |
} |
| 49 |
|
| 50 |
func TestReadPrivateEncryptedV6Key(t *testing.T) { |
| 51 |
c := &packet.Config{V6Keys: true} |
| 52 |
e, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c) |
| 53 |
if err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
password := []byte("test v6 key # password") |
| 57 |
// Encrypt private key |
| 58 |
if err = e.PrivateKey.Encrypt(password); err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
// Encrypt subkeys |
| 62 |
for _, sub := range e.Subkeys { |
| 63 |
if err = sub.PrivateKey.Encrypt(password); err != nil { |
| 64 |
t.Fatal(err) |
| 65 |
} |
| 66 |
} |
| 67 |
// Serialize, Read |
| 68 |
serializedEntity := bytes.NewBuffer(nil) |
| 69 |
err = e.SerializePrivateWithoutSigning(serializedEntity, nil) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
el, err := ReadKeyRing(serializedEntity) |
| 74 |
if err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
|
| 78 |
// Decrypt |
| 79 |
if el[0].PrivateKey == nil { |
| 80 |
t.Fatal("No private key found") |
| 81 |
} |
| 82 |
if err = el[0].PrivateKey.Decrypt(password); err != nil { |
| 83 |
t.Error(err) |
| 84 |
} |
| 85 |
|
| 86 |
// Decrypt subkeys |
| 87 |
for _, sub := range e.Subkeys { |
| 88 |
if err = sub.PrivateKey.Decrypt(password); err != nil { |
| 89 |
t.Error(err) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
checkV6Key(t, el[0]) |
| 94 |
} |
| 95 |
|
| 96 |
func TestNewEntitySerializeV6Key(t *testing.T) { |
| 97 |
c := &packet.Config{V6Keys: true} |
| 98 |
e, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c) |
| 99 |
if err != nil { |
| 100 |
t.Fatal(err) |
| 101 |
} |
| 102 |
checkSerializeReadv6(t, e) |
| 103 |
} |
| 104 |
|
| 105 |
func TestNewEntityV6Key(t *testing.T) { |
| 106 |
c := &packet.Config{ |
| 107 |
V6Keys: true, |
| 108 |
} |
| 109 |
e, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c) |
| 110 |
if err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
checkV6Key(t, e) |
| 114 |
} |
| 115 |
|
| 116 |
func checkV6Key(t *testing.T, ent *Entity) { |
| 117 |
key := ent.PrimaryKey |
| 118 |
if key.Version != 6 { |
| 119 |
t.Errorf("wrong key version %d", key.Version) |
| 120 |
} |
| 121 |
if len(key.Fingerprint) != 32 { |
| 122 |
t.Errorf("Wrong fingerprint length: %d", len(key.Fingerprint)) |
| 123 |
} |
| 124 |
signatures := ent.Revocations |
| 125 |
for _, id := range ent.Identities { |
| 126 |
signatures = append(signatures, id.SelfCertifications...) |
| 127 |
} |
| 128 |
for _, sig := range signatures { |
| 129 |
if sig.Packet == nil { |
| 130 |
continue |
| 131 |
} |
| 132 |
if sig.Packet.Version != 6 { |
| 133 |
t.Errorf("wrong signature version %d", sig.Packet.Version) |
| 134 |
} |
| 135 |
fgptLen := len(sig.Packet.IssuerFingerprint) |
| 136 |
if fgptLen != 32 { |
| 137 |
t.Errorf("Wrong fingerprint length in signature: %d", fgptLen) |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func checkSerializeReadv6(t *testing.T, e *Entity) { |
| 143 |
// Entity serialize |
| 144 |
serializedEntity := bytes.NewBuffer(nil) |
| 145 |
err := e.Serialize(serializedEntity) |
| 146 |
if err != nil { |
| 147 |
t.Fatal(err) |
| 148 |
} |
| 149 |
el, err := ReadKeyRing(serializedEntity) |
| 150 |
if err != nil { |
| 151 |
t.Fatal(err) |
| 152 |
} |
| 153 |
checkV6Key(t, el[0]) |
| 154 |
|
| 155 |
// Without signing |
| 156 |
serializedEntity = bytes.NewBuffer(nil) |
| 157 |
err = e.SerializePrivateWithoutSigning(serializedEntity, nil) |
| 158 |
if err != nil { |
| 159 |
t.Fatal(err) |
| 160 |
} |
| 161 |
el, err = ReadKeyRing(serializedEntity) |
| 162 |
if err != nil { |
| 163 |
t.Fatal(err) |
| 164 |
} |
| 165 |
checkV6Key(t, el[0]) |
| 166 |
|
| 167 |
// Private |
| 168 |
serializedEntity = bytes.NewBuffer(nil) |
| 169 |
err = e.SerializePrivate(serializedEntity, nil) |
| 170 |
if err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
el, err = ReadKeyRing(serializedEntity) |
| 174 |
if err != nil { |
| 175 |
t.Fatal(err) |
| 176 |
} |
| 177 |
checkV6Key(t, el[0]) |
| 178 |
} |
| 179 |
|
| 180 |
func TestNewEntityWithDefaultHashV6(t *testing.T) { |
| 181 |
for _, hash := range hashes[:5] { |
| 182 |
c := &packet.Config{ |
| 183 |
V6Keys: true, |
| 184 |
DefaultHash: hash, |
| 185 |
} |
| 186 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", c) |
| 187 |
if hash == crypto.SHA1 { |
| 188 |
if err == nil { |
| 189 |
t.Fatal("should fail on SHA1 key creation") |
| 190 |
} |
| 191 |
continue |
| 192 |
} |
| 193 |
var zeroTime time.Time |
| 194 |
selfSig, err := entity.PrimarySelfSignature(zeroTime, c) |
| 195 |
if err != nil { |
| 196 |
t.Fatal("self-signature should be found") |
| 197 |
} |
| 198 |
prefs := selfSig.PreferredHash |
| 199 |
if prefs == nil { |
| 200 |
t.Fatal(err) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestKeyGenerationHighSecurityLevel(t *testing.T) { |
| 206 |
c := &packet.Config{ |
| 207 |
V6Keys: true, |
| 208 |
Algorithm: packet.PubKeyAlgoEd448, |
| 209 |
DefaultHash: crypto.SHA256, |
| 210 |
} |
| 211 |
entity, err := NewEntity("Golang Gopher", "Test Key", "[email protected]", c) |
| 212 |
if err != nil { |
| 213 |
t.Fatal(err) |
| 214 |
} |
| 215 |
selfSig, err := entity.PrimarySelfSignature(time.Time{}, c) |
| 216 |
if err != nil { |
| 217 |
t.Fatal(err) |
| 218 |
} |
| 219 |
if !(selfSig.PreferredHash[0] == hashToHashId(crypto.SHA512)) { |
| 220 |
t.Fatal("sha 512 should be the preferred option") |
| 221 |
} |
| 222 |
if selfSig.Hash != crypto.SHA512 { |
| 223 |
t.Fatal("sha 512 should be used in self signatures") |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
func TestKeyGenLegacyECC(t *testing.T) { |
| 229 |
c := &packet.Config{ |
| 230 |
V6Keys: true, |
| 231 |
Algorithm: packet.PubKeyAlgoEdDSA, |
| 232 |
} |
| 233 |
_, err := NewEntity("V6 Key Owner", "V6 Key", "[email protected]", c) |
| 234 |
if err == nil { |
| 235 |
t.Fatal("should not be able to generate v6 keys with legacy OIDs") |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestReadLegacyECC(t *testing.T) { |
| 240 |
_, err := ReadArmoredKeyRing(strings.NewReader(LegacyECCKeyV6)) |
| 241 |
if err == nil { |
| 242 |
t.Fatal("should not be able to read v6 legacy ECC key") |
| 243 |
} |
| 244 |
} |