encrypted_key_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. package packet
  5. import (
  6. "bytes"
  7. "crypto"
  8. "crypto/rsa"
  9. "encoding/hex"
  10. "fmt"
  11. "io"
  12. "math/big"
  13. "testing"
  14. )
  15. func bigFromBase10(s string) *big.Int {
  16. b, ok := new(big.Int).SetString(s, 10)
  17. if !ok {
  18. panic("bigFromBase10 failed")
  19. }
  20. return b
  21. }
  22. var encryptedKeyPub = rsa.PublicKey{
  23. E: 65537,
  24. N: bigFromBase10("115804063926007623305902631768113868327816898845124614648849934718568541074358183759250136204762053879858102352159854352727097033322663029387610959884180306668628526686121021235757016368038585212410610742029286439607686208110250133174279811431933746643015923132833417396844716207301518956640020862630546868823"),
  25. }
  26. var encryptedKeyRSAPriv = &rsa.PrivateKey{
  27. PublicKey: encryptedKeyPub,
  28. D: bigFromBase10("32355588668219869544751561565313228297765464314098552250409557267371233892496951383426602439009993875125222579159850054973310859166139474359774543943714622292329487391199285040721944491839695981199720170366763547754915493640685849961780092241140181198779299712578774460837139360803883139311171713302987058393"),
  29. }
  30. var encryptedKeyPriv = &PrivateKey{
  31. PublicKey: PublicKey{
  32. PubKeyAlgo: PubKeyAlgoRSA,
  33. },
  34. PrivateKey: encryptedKeyRSAPriv,
  35. }
  36. func TestDecryptingEncryptedKey(t *testing.T) {
  37. for i, encryptedKeyHex := range []string{
  38. "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8",
  39. // MPI can be shorter than the length of the key.
  40. "c18b032a67d68660df41c70103f8e520c52ae9807183c669ce26e772e482dc5d8cf60e6f59316e145be14d2e5221ee69550db1d5618a8cb002a719f1f0b9345bde21536d410ec90ba86cac37748dec7933eb7f9873873b2d61d3321d1cd44535014f6df58f7bc0c7afb5edc38e1a974428997d2f747f9a173bea9ca53079b409517d332df62d805564cffc9be6",
  41. } {
  42. const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b"
  43. p, err := Read(readerFromHex(encryptedKeyHex))
  44. if err != nil {
  45. t.Errorf("#%d: error from Read: %s", i, err)
  46. return
  47. }
  48. ek, ok := p.(*EncryptedKey)
  49. if !ok {
  50. t.Errorf("#%d: didn't parse an EncryptedKey, got %#v", i, p)
  51. return
  52. }
  53. if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA {
  54. t.Errorf("#%d: unexpected EncryptedKey contents: %#v", i, ek)
  55. return
  56. }
  57. err = ek.Decrypt(encryptedKeyPriv, nil)
  58. if err != nil {
  59. t.Errorf("#%d: error from Decrypt: %s", i, err)
  60. return
  61. }
  62. if ek.CipherFunc != CipherAES256 {
  63. t.Errorf("#%d: unexpected EncryptedKey contents: %#v", i, ek)
  64. return
  65. }
  66. keyHex := fmt.Sprintf("%x", ek.Key)
  67. if keyHex != expectedKeyHex {
  68. t.Errorf("#%d: bad key, got %s want %s", i, keyHex, expectedKeyHex)
  69. }
  70. }
  71. }
  72. type rsaDecrypter struct {
  73. rsaPrivateKey *rsa.PrivateKey
  74. decryptCount int
  75. }
  76. func (r *rsaDecrypter) Public() crypto.PublicKey {
  77. return &r.rsaPrivateKey.PublicKey
  78. }
  79. func (r *rsaDecrypter) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) {
  80. r.decryptCount++
  81. return r.rsaPrivateKey.Decrypt(rand, msg, opts)
  82. }
  83. func TestRSADecrypter(t *testing.T) {
  84. const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8"
  85. const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b"
  86. p, err := Read(readerFromHex(encryptedKeyHex))
  87. if err != nil {
  88. t.Errorf("error from Read: %s", err)
  89. return
  90. }
  91. ek, ok := p.(*EncryptedKey)
  92. if !ok {
  93. t.Errorf("didn't parse an EncryptedKey, got %#v", p)
  94. return
  95. }
  96. if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA {
  97. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  98. return
  99. }
  100. customDecrypter := &rsaDecrypter{
  101. rsaPrivateKey: encryptedKeyRSAPriv,
  102. }
  103. customKeyPriv := &PrivateKey{
  104. PublicKey: PublicKey{
  105. PubKeyAlgo: PubKeyAlgoRSA,
  106. },
  107. PrivateKey: customDecrypter,
  108. }
  109. err = ek.Decrypt(customKeyPriv, nil)
  110. if err != nil {
  111. t.Errorf("error from Decrypt: %s", err)
  112. return
  113. }
  114. if ek.CipherFunc != CipherAES256 {
  115. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  116. return
  117. }
  118. keyHex := fmt.Sprintf("%x", ek.Key)
  119. if keyHex != expectedKeyHex {
  120. t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex)
  121. }
  122. if customDecrypter.decryptCount != 1 {
  123. t.Errorf("Expected customDecrypter.Decrypt() to be called 1 time, but was called %d times", customDecrypter.decryptCount)
  124. }
  125. }
  126. func TestEncryptingEncryptedKey(t *testing.T) {
  127. key := []byte{1, 2, 3, 4}
  128. const expectedKeyHex = "01020304"
  129. const keyId = 42
  130. pub := &PublicKey{
  131. PublicKey: &encryptedKeyPub,
  132. KeyId: keyId,
  133. PubKeyAlgo: PubKeyAlgoRSAEncryptOnly,
  134. }
  135. buf := new(bytes.Buffer)
  136. err := SerializeEncryptedKey(buf, pub, CipherAES128, key, nil)
  137. if err != nil {
  138. t.Errorf("error writing encrypted key packet: %s", err)
  139. }
  140. p, err := Read(buf)
  141. if err != nil {
  142. t.Errorf("error from Read: %s", err)
  143. return
  144. }
  145. ek, ok := p.(*EncryptedKey)
  146. if !ok {
  147. t.Errorf("didn't parse an EncryptedKey, got %#v", p)
  148. return
  149. }
  150. if ek.KeyId != keyId || ek.Algo != PubKeyAlgoRSAEncryptOnly {
  151. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  152. return
  153. }
  154. err = ek.Decrypt(encryptedKeyPriv, nil)
  155. if err != nil {
  156. t.Errorf("error from Decrypt: %s", err)
  157. return
  158. }
  159. if ek.CipherFunc != CipherAES128 {
  160. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  161. return
  162. }
  163. keyHex := fmt.Sprintf("%x", ek.Key)
  164. if keyHex != expectedKeyHex {
  165. t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex)
  166. }
  167. }
  168. func TestSerializingEncryptedKey(t *testing.T) {
  169. const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8"
  170. p, err := Read(readerFromHex(encryptedKeyHex))
  171. if err != nil {
  172. t.Fatalf("error from Read: %s", err)
  173. }
  174. ek, ok := p.(*EncryptedKey)
  175. if !ok {
  176. t.Fatalf("didn't parse an EncryptedKey, got %#v", p)
  177. }
  178. var buf bytes.Buffer
  179. ek.Serialize(&buf)
  180. if bufHex := hex.EncodeToString(buf.Bytes()); bufHex != encryptedKeyHex {
  181. t.Fatalf("serialization of encrypted key differed from original. Original was %s, but reserialized as %s", encryptedKeyHex, bufHex)
  182. }
  183. }