You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 lines
4.7 KiB

  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/rsa"
  8. "encoding/hex"
  9. "fmt"
  10. "math/big"
  11. "testing"
  12. )
  13. func bigFromBase10(s string) *big.Int {
  14. b, ok := new(big.Int).SetString(s, 10)
  15. if !ok {
  16. panic("bigFromBase10 failed")
  17. }
  18. return b
  19. }
  20. var encryptedKeyPub = rsa.PublicKey{
  21. E: 65537,
  22. N: bigFromBase10("115804063926007623305902631768113868327816898845124614648849934718568541074358183759250136204762053879858102352159854352727097033322663029387610959884180306668628526686121021235757016368038585212410610742029286439607686208110250133174279811431933746643015923132833417396844716207301518956640020862630546868823"),
  23. }
  24. var encryptedKeyRSAPriv = &rsa.PrivateKey{
  25. PublicKey: encryptedKeyPub,
  26. D: bigFromBase10("32355588668219869544751561565313228297765464314098552250409557267371233892496951383426602439009993875125222579159850054973310859166139474359774543943714622292329487391199285040721944491839695981199720170366763547754915493640685849961780092241140181198779299712578774460837139360803883139311171713302987058393"),
  27. }
  28. var encryptedKeyPriv = &PrivateKey{
  29. PublicKey: PublicKey{
  30. PubKeyAlgo: PubKeyAlgoRSA,
  31. },
  32. PrivateKey: encryptedKeyRSAPriv,
  33. }
  34. func TestDecryptingEncryptedKey(t *testing.T) {
  35. for i, encryptedKeyHex := range []string{
  36. "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8",
  37. // MPI can be shorter than the length of the key.
  38. "c18b032a67d68660df41c70103f8e520c52ae9807183c669ce26e772e482dc5d8cf60e6f59316e145be14d2e5221ee69550db1d5618a8cb002a719f1f0b9345bde21536d410ec90ba86cac37748dec7933eb7f9873873b2d61d3321d1cd44535014f6df58f7bc0c7afb5edc38e1a974428997d2f747f9a173bea9ca53079b409517d332df62d805564cffc9be6",
  39. } {
  40. const expectedKeyHex = "d930363f7e0308c333b9618617ea728963d8df993665ae7be1092d4926fd864b"
  41. p, err := Read(readerFromHex(encryptedKeyHex))
  42. if err != nil {
  43. t.Errorf("#%d: error from Read: %s", i, err)
  44. return
  45. }
  46. ek, ok := p.(*EncryptedKey)
  47. if !ok {
  48. t.Errorf("#%d: didn't parse an EncryptedKey, got %#v", i, p)
  49. return
  50. }
  51. if ek.KeyId != 0x2a67d68660df41c7 || ek.Algo != PubKeyAlgoRSA {
  52. t.Errorf("#%d: unexpected EncryptedKey contents: %#v", i, ek)
  53. return
  54. }
  55. err = ek.Decrypt(encryptedKeyPriv, nil)
  56. if err != nil {
  57. t.Errorf("#%d: error from Decrypt: %s", i, err)
  58. return
  59. }
  60. if ek.CipherFunc != CipherAES256 {
  61. t.Errorf("#%d: unexpected EncryptedKey contents: %#v", i, ek)
  62. return
  63. }
  64. keyHex := fmt.Sprintf("%x", ek.Key)
  65. if keyHex != expectedKeyHex {
  66. t.Errorf("#%d: bad key, got %s want %s", i, keyHex, expectedKeyHex)
  67. }
  68. }
  69. }
  70. func TestEncryptingEncryptedKey(t *testing.T) {
  71. key := []byte{1, 2, 3, 4}
  72. const expectedKeyHex = "01020304"
  73. const keyId = 42
  74. pub := &PublicKey{
  75. PublicKey: &encryptedKeyPub,
  76. KeyId: keyId,
  77. PubKeyAlgo: PubKeyAlgoRSAEncryptOnly,
  78. }
  79. buf := new(bytes.Buffer)
  80. err := SerializeEncryptedKey(buf, pub, CipherAES128, key, nil)
  81. if err != nil {
  82. t.Errorf("error writing encrypted key packet: %s", err)
  83. }
  84. p, err := Read(buf)
  85. if err != nil {
  86. t.Errorf("error from Read: %s", err)
  87. return
  88. }
  89. ek, ok := p.(*EncryptedKey)
  90. if !ok {
  91. t.Errorf("didn't parse an EncryptedKey, got %#v", p)
  92. return
  93. }
  94. if ek.KeyId != keyId || ek.Algo != PubKeyAlgoRSAEncryptOnly {
  95. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  96. return
  97. }
  98. err = ek.Decrypt(encryptedKeyPriv, nil)
  99. if err != nil {
  100. t.Errorf("error from Decrypt: %s", err)
  101. return
  102. }
  103. if ek.CipherFunc != CipherAES128 {
  104. t.Errorf("unexpected EncryptedKey contents: %#v", ek)
  105. return
  106. }
  107. keyHex := fmt.Sprintf("%x", ek.Key)
  108. if keyHex != expectedKeyHex {
  109. t.Errorf("bad key, got %s want %s", keyHex, expectedKeyHex)
  110. }
  111. }
  112. func TestSerializingEncryptedKey(t *testing.T) {
  113. const encryptedKeyHex = "c18c032a67d68660df41c70104005789d0de26b6a50c985a02a13131ca829c413a35d0e6fa8d6842599252162808ac7439c72151c8c6183e76923fe3299301414d0c25a2f06a2257db3839e7df0ec964773f6e4c4ac7ff3b48c444237166dd46ba8ff443a5410dc670cb486672fdbe7c9dfafb75b4fea83af3a204fe2a7dfa86bd20122b4f3d2646cbeecb8f7be8"
  114. p, err := Read(readerFromHex(encryptedKeyHex))
  115. if err != nil {
  116. t.Fatalf("error from Read: %s", err)
  117. }
  118. ek, ok := p.(*EncryptedKey)
  119. if !ok {
  120. t.Fatalf("didn't parse an EncryptedKey, got %#v", p)
  121. }
  122. var buf bytes.Buffer
  123. ek.Serialize(&buf)
  124. if bufHex := hex.EncodeToString(buf.Bytes()); bufHex != encryptedKeyHex {
  125. t.Fatalf("serialization of encrypted key differed from original. Original was %s, but reserialized as %s", encryptedKeyHex, bufHex)
  126. }
  127. }