ssh.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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"
  17. "crypto/dsa"
  18. "crypto/ecdsa"
  19. "crypto/ed25519"
  20. "crypto/elliptic"
  21. "crypto/rand"
  22. "crypto/rsa"
  23. "crypto/x509"
  24. "encoding/asn1"
  25. "encoding/pem"
  26. "math/big"
  27. "golang.org/x/crypto/ssh"
  28. "yunion.io/x/pkg/errors"
  29. )
  30. func GenerateRSASSHKeypair() (string, string, error) {
  31. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  32. if err != nil {
  33. return "", "", errors.Wrapf(err, "generate rsa key")
  34. }
  35. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  36. privateStr := string(pem.EncodeToMemory(privateKeyPEM))
  37. pub, err := exportSshPublicKey(&privateKey.PublicKey)
  38. if err != nil {
  39. return "", "", errors.Wrapf(err, "export ssh public key")
  40. }
  41. publicStr := string(pub)
  42. return privateStr, publicStr, nil
  43. }
  44. func GenerateED25519SSHKeypair() (string, string, error) {
  45. publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
  46. if err != nil {
  47. return "", "", errors.Wrapf(err, "generate ed25519 key")
  48. }
  49. pemBlock, err := ssh.MarshalPrivateKey(crypto.PrivateKey(privateKey), "")
  50. if err != nil {
  51. return "", "", errors.Wrapf(err, "marshal pkix private key")
  52. }
  53. privateStr := string(pem.EncodeToMemory(pemBlock))
  54. pub, err := exportSshPublicKey(publicKey)
  55. if err != nil {
  56. return "", "", errors.Wrapf(err, "export ssh public key")
  57. }
  58. publicStr := string(pub)
  59. return privateStr, publicStr, nil
  60. }
  61. func GenerateECDSASHAP521SSHKeypair() (string, string, error) {
  62. privateKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
  63. if err != nil {
  64. return "", "", errors.Wrapf(err, "generate ecdsa key")
  65. }
  66. pemBlock, err := ssh.MarshalPrivateKey(crypto.PrivateKey(privateKey), "")
  67. if err != nil {
  68. return "", "", errors.Wrapf(err, "marshal pkix private key")
  69. }
  70. privateStr := string(pem.EncodeToMemory(pemBlock))
  71. pub, err := exportSshPublicKey(&privateKey.PublicKey)
  72. if err != nil {
  73. return "", "", errors.Wrapf(err, "export ssh public key")
  74. }
  75. publicStr := string(pub)
  76. return privateStr, publicStr, nil
  77. }
  78. func GenerateDSASSHKeypair() (string, string, error) {
  79. var privateKey dsa.PrivateKey
  80. params := &privateKey.Parameters
  81. err := dsa.GenerateParameters(params, rand.Reader, dsa.L1024N160)
  82. if err != nil {
  83. return "", "", errors.Wrapf(err, "generate dsa key")
  84. }
  85. err = dsa.GenerateKey(&privateKey, rand.Reader)
  86. if err != nil {
  87. return "", "", errors.Wrapf(err, "generate dsa key")
  88. }
  89. type DsaASN1 struct {
  90. Version int
  91. P *big.Int
  92. Q *big.Int
  93. G *big.Int
  94. Pub *big.Int
  95. Priv *big.Int
  96. }
  97. k := DsaASN1{}
  98. k.P = privateKey.P
  99. k.Q = privateKey.Q
  100. k.G = privateKey.G
  101. k.Pub = privateKey.Y
  102. k.Priv = privateKey.X
  103. privBytes, err := asn1.Marshal(k)
  104. if err != nil {
  105. return "", "", errors.Wrapf(err, "asn1 marshal")
  106. }
  107. privateKeyPEM := &pem.Block{Type: "DSA PRIVATE KEY", Bytes: privBytes}
  108. privateStr := string(pem.EncodeToMemory(privateKeyPEM))
  109. pub, err := exportSshPublicKey(&privateKey.PublicKey)
  110. if err != nil {
  111. return "", "", errors.Wrapf(err, "export ssh public key")
  112. }
  113. publicStr := string(pub)
  114. return privateStr, publicStr, nil
  115. }
  116. func GetPublicKeyScheme(pubkey ssh.PublicKey) string {
  117. switch pubkey.Type() {
  118. case ssh.KeyAlgoRSA:
  119. return "RSA"
  120. case ssh.KeyAlgoDSA:
  121. return "DSA"
  122. case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
  123. return "ECDSA"
  124. case ssh.KeyAlgoED25519:
  125. return "ED25519"
  126. }
  127. return "UNKNOWN"
  128. }