| 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 |
"encoding/hex" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"math/big" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"crypto" |
| 17 |
"crypto/rsa" |
| 18 |
|
| 19 |
"github.com/ProtonMail/go-crypto/openpgp/x25519" |
| 20 |
"github.com/ProtonMail/go-crypto/openpgp/x448" |
| 21 |
) |
| 22 |
|
| 23 |
func bigFromBase10(s string) *big.Int { |
| 24 |
b, ok := new(big.Int).SetString(s, 10) |
| 25 |
if !ok { |
| 26 |
panic("bigFromBase10 failed") |
| 27 |
} |
| 28 |
return b |
| 29 |
} |
| 30 |
|
| 31 |
var encryptedKeyPub = rsa.PublicKey{ |
| 32 |
E: 65537, |
| 33 |
N: bigFromBase10("115804063926007623305902631768113868327816898845124614648849934718568541074358183759250136204762053879858102352159854352727097033322663029387610959884180306668628526686121021235757016368038585212410610742029286439607686208110250133174279811431933746643015923132833417396844716207301518956640020862630546868823"), |
| 34 |
} |
| 35 |
|
| 36 |
var encryptedKeyRSAPriv = &rsa.PrivateKey{ |
| 37 |
PublicKey: encryptedKeyPub, |
| 38 |
D: bigFromBase10("32355588668219869544751561565313228297765464314098552250409557267371233892496951383426602439009993875125222579159850054973310859166139474359774543943714622292329487391199285040721944491839695981199720170366763547754915493640685849961780092241140181198779299712578774460837139360803883139311171713302987058393"), |
| 39 |
} |
| 40 |
|
| 41 |
var encryptedKeyPriv = &PrivateKey{ |
| 42 |
PublicKey: PublicKey{ |
| 43 |
PubKeyAlgo: PubKeyAlgoRSA, |
| 44 |
KeyId: 0x2a67d68660df41c7, |
| 45 |
}, |
| 46 |
PrivateKey: encryptedKeyRSAPriv, |
| 47 |
} |
| 48 |
|
| 49 |
func TestDecryptingEncryptedKey(t *testing.T) { |
| 50 |
for i, encryptedKeyHex := range []string{ |
| 51 |
"c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8", |
| 52 |
// MPI can be shorter than the length of the key. |
| 53 |
"c18b032a67d68660df41c70103f8e520c52ae9807183c669ce26e772e482dc5d8cf60e6f59316e145be14d2e5221ee69550db1d5618a8cb002a719f1f0b9345bde21536d410ec90ba86cac37748dec7933eb7f9873873b2d61d3321d1cd44535014f6df58f7bc0c7afb5edc38e1a974428997d2f747f9a173bea9ca53079b409517d332df62d805564cffc9be6", |
| 54 |
} { |
| 55 |
const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b" |
| 56 |
|
| 57 |
p, err := Read(readerFromHex(encryptedKeyHex)) |
| 58 |
if err != nil { |
| 59 |
t.Errorf("#%d: error from Read: %s", i, err) |
| 60 |
return |
| 61 |
} |
| 62 |
ek, ok := p.(*EncryptedKey) |
| 63 |
if !ok { |
| 64 |
t.Errorf("#%d: didn't parse an EncryptedKey, got %#v", i, p) |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA { |
| 69 |
t.Errorf("#%d: unexpected EncryptedKey contents: %#v", i, ek) |
| 70 |
return |
| 71 |
} |
| 72 |
|
| 73 |
err = ek.Decrypt(encryptedKeyPriv, nil) |
| 74 |
if err != nil { |
| 75 |
t.Errorf("#%d: error from Decrypt: %s", i, err) |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
if ek.CipherFunc != CipherAES256 { |
| 80 |
t.Errorf("#%d: unexpected EncryptedKey contents: %#v", i, ek) |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
keyHex := fmt.Sprintf("%x", ek.Key) |
| 85 |
if keyHex != expectedKeyHex { |
| 86 |
t.Errorf("#%d: bad key, got %s want %s", i, keyHex, expectedKeyHex) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
type rsaDecrypter struct { |
| 92 |
rsaPrivateKey *rsa.PrivateKey |
| 93 |
decryptCount int |
| 94 |
} |
| 95 |
|
| 96 |
func (r *rsaDecrypter) Public() crypto.PublicKey { |
| 97 |
return &r.rsaPrivateKey.PublicKey |
| 98 |
} |
| 99 |
|
| 100 |
func (r *rsaDecrypter) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) { |
| 101 |
r.decryptCount++ |
| 102 |
return r.rsaPrivateKey.Decrypt(rand, msg, opts) |
| 103 |
} |
| 104 |
|
| 105 |
func TestRSADecrypter(t *testing.T) { |
| 106 |
const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8" |
| 107 |
|
| 108 |
const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b" |
| 109 |
|
| 110 |
p, err := Read(readerFromHex(encryptedKeyHex)) |
| 111 |
if err != nil { |
| 112 |
t.Errorf("error from Read: %s", err) |
| 113 |
return |
| 114 |
} |
| 115 |
ek, ok := p.(*EncryptedKey) |
| 116 |
if !ok { |
| 117 |
t.Errorf("didn't parse an EncryptedKey, got %#v", p) |
| 118 |
return |
| 119 |
} |
| 120 |
|
| 121 |
if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA { |
| 122 |
t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 123 |
return |
| 124 |
} |
| 125 |
|
| 126 |
customDecrypter := &rsaDecrypter{ |
| 127 |
rsaPrivateKey: encryptedKeyRSAPriv, |
| 128 |
} |
| 129 |
|
| 130 |
customKeyPriv := &PrivateKey{ |
| 131 |
PublicKey: PublicKey{ |
| 132 |
KeyId: ek.KeyId, |
| 133 |
PubKeyAlgo: PubKeyAlgoRSA, |
| 134 |
}, |
| 135 |
PrivateKey: customDecrypter, |
| 136 |
} |
| 137 |
|
| 138 |
err = ek.Decrypt(customKeyPriv, nil) |
| 139 |
if err != nil { |
| 140 |
t.Errorf("error from Decrypt: %s", err) |
| 141 |
return |
| 142 |
} |
| 143 |
|
| 144 |
if ek.CipherFunc != CipherAES256 { |
| 145 |
t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
keyHex := fmt.Sprintf("%x", ek.Key) |
| 150 |
if keyHex != expectedKeyHex { |
| 151 |
t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex) |
| 152 |
} |
| 153 |
|
| 154 |
if customDecrypter.decryptCount != 1 { |
| 155 |
t.Errorf("Expected customDecrypter.Decrypt() to be called 1 time, but was called %d times", customDecrypter.decryptCount) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestEncryptingEncryptedKey(t *testing.T) { |
| 160 |
key := []byte{1, 2, 3, 4} |
| 161 |
const expectedKeyHex = "01020304" |
| 162 |
const keyId = 0x2a67d68660df41c7 |
| 163 |
|
| 164 |
pub := &PublicKey{ |
| 165 |
PublicKey: &encryptedKeyPub, |
| 166 |
KeyId: keyId, |
| 167 |
PubKeyAlgo: PubKeyAlgoRSA, |
| 168 |
} |
| 169 |
|
| 170 |
buf := new(bytes.Buffer) |
| 171 |
err := SerializeEncryptedKeyAEAD(buf, pub, CipherAES128, false, key, nil) |
| 172 |
if err != nil { |
| 173 |
t.Errorf("error writing encrypted key packet: %s", err) |
| 174 |
} |
| 175 |
|
| 176 |
p, err := Read(buf) |
| 177 |
if err != nil { |
| 178 |
t.Errorf("error from Read: %s", err) |
| 179 |
return |
| 180 |
} |
| 181 |
ek, ok := p.(*EncryptedKey) |
| 182 |
if !ok { |
| 183 |
t.Errorf("didn't parse an EncryptedKey, got %#v", p) |
| 184 |
return |
| 185 |
} |
| 186 |
|
| 187 |
if ek.KeyId != keyId || ek.Algo != PubKeyAlgoRSA { |
| 188 |
t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 189 |
return |
| 190 |
} |
| 191 |
|
| 192 |
err = ek.Decrypt(encryptedKeyPriv, nil) |
| 193 |
if err != nil { |
| 194 |
t.Errorf("error from Decrypt: %s", err) |
| 195 |
return |
| 196 |
} |
| 197 |
|
| 198 |
if ek.CipherFunc != CipherAES128 { |
| 199 |
t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 200 |
return |
| 201 |
} |
| 202 |
|
| 203 |
keyHex := fmt.Sprintf("%x", ek.Key) |
| 204 |
if keyHex != expectedKeyHex { |
| 205 |
t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func TestEncryptingEncryptedKeyV6(t *testing.T) { |
| 210 |
key := []byte{1, 2, 3, 4} |
| 211 |
config := &Config{ |
| 212 |
AEADConfig: &AEADConfig{}, |
| 213 |
} |
| 214 |
rsaKey, _ := rsa.GenerateKey(config.Random(), 2048) |
| 215 |
rsaWrappedKey := NewRSAPrivateKey(time.Now(), rsaKey) |
| 216 |
rsaWrappedKey.UpgradeToV6() |
| 217 |
rsaWrappedKeyPub := &rsaWrappedKey.PublicKey |
| 218 |
|
| 219 |
buf := new(bytes.Buffer) |
| 220 |
err := SerializeEncryptedKeyAEAD(buf, rsaWrappedKeyPub, CipherAES128, true, key, config) |
| 221 |
|
| 222 |
if err != nil { |
| 223 |
t.Errorf("error writing encrypted key packet: %s", err) |
| 224 |
} |
| 225 |
|
| 226 |
p, err := Read(buf) |
| 227 |
if err != nil { |
| 228 |
t.Errorf("error from Read: %s", err) |
| 229 |
return |
| 230 |
} |
| 231 |
ek, ok := p.(*EncryptedKey) |
| 232 |
if !ok { |
| 233 |
t.Errorf("didn't parse an EncryptedKey, got %#v", p) |
| 234 |
return |
| 235 |
} |
| 236 |
|
| 237 |
if !bytes.Equal(ek.KeyFingerprint, rsaWrappedKey.Fingerprint) || |
| 238 |
ek.Algo != PubKeyAlgoRSA || |
| 239 |
ek.KeyVersion != rsaWrappedKey.Version { |
| 240 |
t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 241 |
return |
| 242 |
} |
| 243 |
|
| 244 |
err = ek.Decrypt(rsaWrappedKey, nil) |
| 245 |
if err != nil { |
| 246 |
t.Errorf("error from Decrypt: %s", err) |
| 247 |
return |
| 248 |
} |
| 249 |
|
| 250 |
keyHex := fmt.Sprintf("%x", ek.Key) |
| 251 |
expectedKeyHex := fmt.Sprintf("%x", key) |
| 252 |
if keyHex != expectedKeyHex { |
| 253 |
t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
func TestEncryptingEncryptedKeyXAlgorithms(t *testing.T) { |
| 258 |
key := []byte{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4} |
| 259 |
config := &Config{ |
| 260 |
AEADConfig: &AEADConfig{}, |
| 261 |
} |
| 262 |
x25519Gen := func() (*PrivateKey, PublicKeyAlgorithm) { |
| 263 |
x25519Key, _ := x25519.GenerateKey(config.Random()) |
| 264 |
x25519WrappedKey := NewX25519PrivateKey(time.Now(), x25519Key) |
| 265 |
x25519WrappedKey.UpgradeToV6() |
| 266 |
return x25519WrappedKey, PubKeyAlgoX25519 |
| 267 |
} |
| 268 |
x448Gen := func() (*PrivateKey, PublicKeyAlgorithm) { |
| 269 |
x448Key, _ := x448.GenerateKey(config.Random()) |
| 270 |
x448WrappedKey := NewX448PrivateKey(time.Now(), x448Key) |
| 271 |
x448WrappedKey.UpgradeToV6() |
| 272 |
return x448WrappedKey, PubKeyAlgoX448 |
| 273 |
} |
| 274 |
testCaseFunc := []func() (*PrivateKey, PublicKeyAlgorithm){x25519Gen, x448Gen} |
| 275 |
|
| 276 |
for _, genFunc := range testCaseFunc { |
| 277 |
wrappedKey, pubType := genFunc() |
| 278 |
wrappedKeyPub := &wrappedKey.PublicKey |
| 279 |
|
| 280 |
buf := new(bytes.Buffer) |
| 281 |
err := SerializeEncryptedKeyAEAD(buf, wrappedKeyPub, CipherAES128, true, key, config) |
| 282 |
|
| 283 |
if err != nil { |
| 284 |
t.Errorf("error writing encrypted key packet: %s", err) |
| 285 |
} |
| 286 |
|
| 287 |
p, err := Read(buf) |
| 288 |
if err != nil { |
| 289 |
t.Errorf("error from Read: %s", err) |
| 290 |
return |
| 291 |
} |
| 292 |
ek, ok := p.(*EncryptedKey) |
| 293 |
if !ok { |
| 294 |
t.Errorf("didn't parse an EncryptedKey, got %#v", p) |
| 295 |
return |
| 296 |
} |
| 297 |
|
| 298 |
if !bytes.Equal(ek.KeyFingerprint, wrappedKey.Fingerprint) || |
| 299 |
ek.Algo != pubType || |
| 300 |
ek.KeyVersion != wrappedKey.Version { |
| 301 |
t.Errorf("unexpected EncryptedKey contents: %#v", ek) |
| 302 |
return |
| 303 |
} |
| 304 |
|
| 305 |
err = ek.Decrypt(wrappedKey, nil) |
| 306 |
if err != nil { |
| 307 |
t.Errorf("error from Decrypt: %s", err) |
| 308 |
return |
| 309 |
} |
| 310 |
|
| 311 |
keyHex := fmt.Sprintf("%x", ek.Key) |
| 312 |
expectedKeyHex := fmt.Sprintf("%x", key) |
| 313 |
if keyHex != expectedKeyHex { |
| 314 |
t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex) |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
func TestSerializingEncryptedKey(t *testing.T) { |
| 320 |
const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8" |
| 321 |
|
| 322 |
p, err := Read(readerFromHex(encryptedKeyHex)) |
| 323 |
if err != nil { |
| 324 |
t.Fatalf("error from Read: %s", err) |
| 325 |
} |
| 326 |
ek, ok := p.(*EncryptedKey) |
| 327 |
if !ok { |
| 328 |
t.Fatalf("didn't parse an EncryptedKey, got %#v", p) |
| 329 |
} |
| 330 |
|
| 331 |
var buf bytes.Buffer |
| 332 |
err = ek.Serialize(&buf) |
| 333 |
if err != nil { |
| 334 |
panic(err) |
| 335 |
} |
| 336 |
|
| 337 |
if bufHex := hex.EncodeToString(buf.Bytes()); bufHex != encryptedKeyHex { |
| 338 |
t.Fatalf("serialization of encrypted key differed from original. Original was %s, but reserialized as %s", encryptedKeyHex, bufHex) |
| 339 |
} |
| 340 |
} |