ssh_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package seclib2
  15. import (
  16. "crypto/x509"
  17. "encoding/pem"
  18. "fmt"
  19. "testing"
  20. "golang.org/x/crypto/ssh"
  21. )
  22. func TestGenerateRSASSHKeypair(t *testing.T) {
  23. priv, pub, _ := GenerateRSASSHKeypair()
  24. t.Logf("%s", priv)
  25. t.Logf("%s", pub)
  26. }
  27. func TestGenerateDSASSHKeypair(t *testing.T) {
  28. priv, pub, _ := GenerateDSASSHKeypair()
  29. t.Logf("%s", priv)
  30. t.Logf("%s", pub)
  31. }
  32. func TestGenerateECDSASHAP521SSHKeypair(t *testing.T) {
  33. priv, pub, _ := GenerateECDSASHAP521SSHKeypair()
  34. t.Logf("%s", priv)
  35. t.Logf("%s", pub)
  36. }
  37. func TestGenerateED25519SSHKeypair(t *testing.T) {
  38. priv, pub, _ := GenerateED25519SSHKeypair()
  39. t.Logf("%s", priv)
  40. t.Logf("%s", pub)
  41. }
  42. func getPublicKeyPem(privateKey string) ([]byte, error) {
  43. block, _ := pem.Decode([]byte(privateKey))
  44. if block == nil {
  45. return nil, fmt.Errorf("invalid private key")
  46. }
  47. priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  48. if err != nil {
  49. return nil, err
  50. }
  51. derPkix, err := x509.MarshalPKIXPublicKey(&priv.PublicKey)
  52. if err != nil {
  53. return nil, err
  54. }
  55. block = &pem.Block{Type: "PUBLIC KEY", Bytes: derPkix}
  56. return pem.EncodeToMemory(block), nil
  57. }
  58. func getRSAPublicKeySsh(privateKey string) ([]byte, error) {
  59. block, _ := pem.Decode([]byte(privateKey))
  60. if block == nil {
  61. return nil, fmt.Errorf("invalid private key")
  62. }
  63. priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return exportSshPublicKey(&priv.PublicKey)
  68. }
  69. func getDSAPublicKeySsh(privateKey string) ([]byte, error) {
  70. block, _ := pem.Decode([]byte(privateKey))
  71. if block == nil {
  72. return nil, fmt.Errorf("invalid private key")
  73. }
  74. priv, err := ssh.ParseDSAPrivateKey(block.Bytes)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return exportSshPublicKey(&priv.PublicKey)
  79. }
  80. func TestRsaDecryptEncrypt(t *testing.T) {
  81. privateKey, publicKey, err := GenerateRSASSHKeypair()
  82. if err != nil {
  83. t.Errorf("fail to generate keypair %s", err)
  84. return
  85. }
  86. /* publicKey2, err := getPublicKeyPem(privateKey)
  87. if err != nil {
  88. t.Errorf("fail to get public key in pem format %s", err)
  89. return
  90. } */
  91. pub3, err := getRSAPublicKeySsh(privateKey)
  92. if err != nil {
  93. t.Errorf("fail to get public key in ssh format %s", err)
  94. return
  95. }
  96. if publicKey != string(pub3) {
  97. t.Errorf("public key mismatch! %s != %s", publicKey, pub3)
  98. return
  99. }
  100. t.Logf("%s", string(pub3))
  101. // t.Logf("%s", string(publicKey2))
  102. secret := "this is a secret string!!!"
  103. code, err := EncryptBase64(publicKey, secret)
  104. if err != nil {
  105. t.Errorf("rsa encrypt error %s", err)
  106. return
  107. }
  108. t.Logf("%s", code)
  109. secret2, err := DecryptBase64(privateKey, code)
  110. if err != nil {
  111. t.Errorf("rsa decrypt error %s", err)
  112. return
  113. }
  114. if secret != secret2 {
  115. t.Errorf("rsa decrypt/encrypt error! %s != %s", secret2, secret)
  116. return
  117. }
  118. }
  119. func TestDsaDecryptEncrypt(t *testing.T) {
  120. privateKey, publicKey, err := GenerateDSASSHKeypair()
  121. if err != nil {
  122. t.Errorf("fail to generate keypair %s", err)
  123. return
  124. }
  125. /* publicKey2, err := getPublicKeyPem(privateKey)
  126. if err != nil {
  127. t.Errorf("fail to get public key in pem format %s", err)
  128. return
  129. } */
  130. pub3, err := getDSAPublicKeySsh(privateKey)
  131. if err != nil {
  132. t.Errorf("fail to get public key in ssh format %s", err)
  133. return
  134. }
  135. if publicKey != string(pub3) {
  136. t.Errorf("public key mismatch! %s != %s", publicKey, pub3)
  137. return
  138. }
  139. t.Logf("%s", string(pub3))
  140. // t.Logf("%s", string(publicKey2))
  141. secret := "this is a secret string!!!"
  142. code, err := EncryptBase64(publicKey, secret)
  143. if err != nil {
  144. t.Errorf("dsa encrypt error %s", err)
  145. return
  146. }
  147. t.Logf("%s", code)
  148. secret2, err := DecryptBase64(privateKey, code)
  149. if err != nil {
  150. t.Errorf("rsa decrypt error %s", err)
  151. return
  152. }
  153. if secret != secret2 {
  154. t.Errorf("rsa decrypt/encrypt error! %s != %s", secret2, secret)
  155. return
  156. }
  157. }