| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package packet |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/elliptic" |
| 10 |
"encoding/hex" |
| 11 |
"math/big" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/ProtonMail/go-crypto/openpgp/ecdsa" |
| 16 |
"github.com/ProtonMail/go-crypto/openpgp/internal/ecc" |
| 17 |
) |
| 18 |
|
| 19 |
var pubKeyTests = []struct { |
| 20 |
hexData string |
| 21 |
hexFingerprint string |
| 22 |
creationTime time.Time |
| 23 |
pubKeyAlgo PublicKeyAlgorithm |
| 24 |
keyId uint64 |
| 25 |
keyIdString string |
| 26 |
keyIdShort string |
| 27 |
}{ |
| 28 |
{rsaPkDataHex, rsaFingerprintHex, time.Unix(0x4d3c5c10, 0), PubKeyAlgoRSA, 0xa34d7e18c20c31bb, "A34D7E18C20C31BB", "C20C31BB"}, |
| 29 |
{dsaPkDataHex, dsaFingerprintHex, time.Unix(0x4d432f89, 0), PubKeyAlgoDSA, 0x8e8fbe54062f19ed, "8E8FBE54062F19ED", "062F19ED"}, |
| 30 |
{ecdsaPkDataHex, ecdsaFingerprintHex, time.Unix(0x5071c294, 0), PubKeyAlgoECDSA, 0x43fe956c542ca00b, "43FE956C542CA00B", "542CA00B"}, |
| 31 |
{ecdhPkDataHex, ecdhFingerprintHex, time.Unix(0x4d530592, 0), PubKeyAlgoECDH, 0xaa8B938f9a201946, "AA8B938F9A201946", "9A201946"}, |
| 32 |
{eddsaPkDataHex, eddsaFingerprintHex, time.Unix(0x056e2132b, 0), PubKeyAlgoEdDSA, 0x907e75e1dd99ad8, "0907E75E1DD99AD8", "1DD99AD8"}, |
| 33 |
} |
| 34 |
|
| 35 |
func TestPublicKeyRead(t *testing.T) { |
| 36 |
for i, test := range pubKeyTests { |
| 37 |
packet, err := Read(readerFromHex(test.hexData)) |
| 38 |
if err != nil { |
| 39 |
t.Errorf("#%d: Read error: %s", i, err) |
| 40 |
continue |
| 41 |
} |
| 42 |
pk, ok := packet.(*PublicKey) |
| 43 |
if !ok { |
| 44 |
t.Errorf("#%d: failed to parse, got: %#v", i, packet) |
| 45 |
continue |
| 46 |
} |
| 47 |
if pk.PubKeyAlgo != test.pubKeyAlgo { |
| 48 |
t.Errorf("#%d: bad public key algorithm got:%x want:%x", i, pk.PubKeyAlgo, test.pubKeyAlgo) |
| 49 |
} |
| 50 |
if !pk.CreationTime.Equal(test.creationTime) { |
| 51 |
t.Errorf("#%d: bad creation time got:%v want:%v", i, pk.CreationTime, test.creationTime) |
| 52 |
} |
| 53 |
expectedFingerprint, _ := hex.DecodeString(test.hexFingerprint) |
| 54 |
if !bytes.Equal(expectedFingerprint, pk.Fingerprint) { |
| 55 |
t.Errorf("#%d: bad fingerprint got:%x want:%x", i, pk.Fingerprint, expectedFingerprint) |
| 56 |
} |
| 57 |
if pk.KeyId != test.keyId { |
| 58 |
t.Errorf("#%d: bad keyid got:%x want:%x", i, pk.KeyId, test.keyId) |
| 59 |
} |
| 60 |
if g, e := pk.KeyIdString(), test.keyIdString; g != e { |
| 61 |
t.Errorf("#%d: bad KeyIdString got:%q want:%q", i, g, e) |
| 62 |
} |
| 63 |
if g, e := pk.KeyIdShortString(), test.keyIdShort; g != e { |
| 64 |
t.Errorf("#%d: bad KeyIdShortString got:%q want:%q", i, g, e) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestPublicKeySerialize(t *testing.T) { |
| 70 |
for i, test := range pubKeyTests { |
| 71 |
packet, err := Read(readerFromHex(test.hexData)) |
| 72 |
if err != nil { |
| 73 |
t.Errorf("#%d: Read error: %s", i, err) |
| 74 |
continue |
| 75 |
} |
| 76 |
pk, ok := packet.(*PublicKey) |
| 77 |
if !ok { |
| 78 |
t.Errorf("#%d: failed to parse, got: %#v", i, packet) |
| 79 |
continue |
| 80 |
} |
| 81 |
serializeBuf := bytes.NewBuffer(nil) |
| 82 |
err = pk.Serialize(serializeBuf) |
| 83 |
if err != nil { |
| 84 |
t.Errorf("#%d: failed to serialize: %s", i, err) |
| 85 |
continue |
| 86 |
} |
| 87 |
|
| 88 |
packet, err = Read(serializeBuf) |
| 89 |
if err != nil { |
| 90 |
t.Errorf("#%d: Read error (from serialized data): %s", i, err) |
| 91 |
continue |
| 92 |
} |
| 93 |
_, ok = packet.(*PublicKey) |
| 94 |
if !ok { |
| 95 |
t.Errorf("#%d: failed to parse serialized data, got: %#v", i, packet) |
| 96 |
continue |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestEcc384Serialize(t *testing.T) { |
| 102 |
r := readerFromHex(ecc384PubHex) |
| 103 |
var w bytes.Buffer |
| 104 |
for i := 0; i < 2; i++ { |
| 105 |
// Public key |
| 106 |
p, err := Read(r) |
| 107 |
if err != nil { |
| 108 |
t.Error(err) |
| 109 |
} |
| 110 |
pubkey := p.(*PublicKey) |
| 111 |
if !bytes.Equal(pubkey.oid.Bytes(), []byte{0x2b, 0x81, 0x04, 0x00, 0x22}) { |
| 112 |
t.Errorf("Unexpected pubkey OID: %x", pubkey.oid.Bytes()) |
| 113 |
} |
| 114 |
if !bytes.Equal(pubkey.p.Bytes()[:5], []byte{0x04, 0xf6, 0xb8, 0xc5, 0xac}) { |
| 115 |
t.Errorf("Unexpected pubkey P[:5]: %x", pubkey.p.Bytes()) |
| 116 |
} |
| 117 |
if pubkey.KeyId != 0x098033880F54719F { |
| 118 |
t.Errorf("Unexpected pubkey ID: %x", pubkey.KeyId) |
| 119 |
} |
| 120 |
err = pubkey.Serialize(&w) |
| 121 |
if err != nil { |
| 122 |
t.Error(err) |
| 123 |
} |
| 124 |
// User ID |
| 125 |
p, err = Read(r) |
| 126 |
if err != nil { |
| 127 |
t.Error(err) |
| 128 |
} |
| 129 |
uid := p.(*UserId) |
| 130 |
if uid.Id != "ec_dsa_dh_384 <[email protected]>" { |
| 131 |
t.Error("Unexpected UID:", uid.Id) |
| 132 |
} |
| 133 |
err = uid.Serialize(&w) |
| 134 |
if err != nil { |
| 135 |
t.Error(err) |
| 136 |
} |
| 137 |
// User ID Sig |
| 138 |
p, err = Read(r) |
| 139 |
if err != nil { |
| 140 |
t.Error(err) |
| 141 |
} |
| 142 |
uidSig := p.(*Signature) |
| 143 |
err = pubkey.VerifyUserIdSignature(uid.Id, pubkey, uidSig) |
| 144 |
if err != nil { |
| 145 |
t.Error(err, ": UID") |
| 146 |
} |
| 147 |
err = uidSig.Serialize(&w) |
| 148 |
if err != nil { |
| 149 |
t.Error(err) |
| 150 |
} |
| 151 |
// Subkey |
| 152 |
p, err = Read(r) |
| 153 |
if err != nil { |
| 154 |
t.Error(err) |
| 155 |
} |
| 156 |
subkey := p.(*PublicKey) |
| 157 |
if !bytes.Equal(subkey.oid.Bytes(), []byte{0x2b, 0x81, 0x04, 0x00, 0x22}) { |
| 158 |
t.Errorf("Unexpected subkey OID: %x", subkey.oid.Bytes()) |
| 159 |
} |
| 160 |
if !bytes.Equal(subkey.p.Bytes()[:5], []byte{0x04, 0x2f, 0xaa, 0x84, 0x02}) { |
| 161 |
t.Errorf("Unexpected subkey P[:5]: %x", subkey.p.Bytes()) |
| 162 |
} |
| 163 |
if !bytes.Equal(subkey.kdf.Bytes(), []byte{0x01, 0x09, 0x09}) { |
| 164 |
t.Errorf("Unexpected subkey KDF: %x", subkey.kdf.Bytes()) |
| 165 |
} |
| 166 |
if subkey.KeyId != 0xAA8B938F9A201946 { |
| 167 |
t.Errorf("Unexpected subkey ID: %x", subkey.KeyId) |
| 168 |
} |
| 169 |
err = subkey.Serialize(&w) |
| 170 |
if err != nil { |
| 171 |
t.Error(err) |
| 172 |
} |
| 173 |
// Subkey Sig |
| 174 |
p, err = Read(r) |
| 175 |
if err != nil { |
| 176 |
t.Error(err) |
| 177 |
} |
| 178 |
subkeySig := p.(*Signature) |
| 179 |
err = pubkey.VerifyKeySignature(subkey, subkeySig) |
| 180 |
if err != nil { |
| 181 |
t.Error(err) |
| 182 |
} |
| 183 |
err = subkeySig.Serialize(&w) |
| 184 |
if err != nil { |
| 185 |
t.Error(err) |
| 186 |
} |
| 187 |
// Now read back what we've written again |
| 188 |
r = bytes.NewBuffer(w.Bytes()) |
| 189 |
w.Reset() |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func TestP256KeyID(t *testing.T) { |
| 194 |
// Confirm that key IDs are correctly calculated for ECC keys. |
| 195 |
ecdsaPub := ecdsa.NewPublicKey(ecc.NewGenericCurve(elliptic.P256())) |
| 196 |
ecdsaPub.X = fromHex("81fbbc20eea9e8d1c3ceabb0a8185925b113d1ac42cd5c78403bd83da19235c6") |
| 197 |
ecdsaPub.Y = fromHex("5ed6db13d91db34507d0129bf88981878d29adbf8fcd1720afdb767bb3fcaaff") |
| 198 |
|
| 199 |
pub := NewECDSAPublicKey(time.Unix(1297309478, 0), ecdsaPub) |
| 200 |
|
| 201 |
const want = uint64(0xd01055fbcadd268e) |
| 202 |
if pub.KeyId != want { |
| 203 |
t.Errorf("want key ID: %x, got %x", want, pub.KeyId) |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func fromHex(hex string) *big.Int { |
| 208 |
n, ok := new(big.Int).SetString(hex, 16) |
| 209 |
if !ok { |
| 210 |
panic("bad hex number: " + hex) |
| 211 |
} |
| 212 |
return n |
| 213 |
} |
| 214 |
|
| 215 |
func TestECDHPublicKeyShortKDFHash(t *testing.T) { |
| 216 |
oid := []byte{0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01} // Curve25519Legacy |
| 217 |
body := []byte{4, 0, 0, 0, 0, byte(PubKeyAlgoECDH)} |
| 218 |
body = append(body, oid...) |
| 219 |
body = append(body, 0x01, 0x07, 0x40, 9) // MPI: 0x40-prefixed point |
| 220 |
body = append(body, make([]byte, 31)...) |
| 221 |
body = append(body, 0x03, 0x01, 0x0b, 0x09) // KDF: SHA224, AES256 |
| 222 |
|
| 223 |
if err := new(PublicKey).parse(bytes.NewReader(body)); err == nil { |
| 224 |
t.Fatal("expected error for ECDH key whose KDF hash is shorter than the KDF cipher key") |
| 225 |
} |
| 226 |
} |