1515package seclib2
1616
1717import (
18+ "crypto"
1819 "crypto/dsa"
20+ "crypto/ecdsa"
21+ "crypto/ed25519"
22+ "crypto/elliptic"
1923 "crypto/rand"
2024 "crypto/rsa"
2125 "crypto/x509"
@@ -25,22 +29,65 @@ import (
2529
2630 "golang.org/x/crypto/ssh"
2731
28- "yunion.io/x/log "
32+ "yunion.io/x/pkg/errors "
2933)
3034
3135func GenerateRSASSHKeypair () (string , string , error ) {
3236 privateKey , err := rsa .GenerateKey (rand .Reader , 2048 )
3337 if err != nil {
34- log .Errorf ("generate rsa key error %s" , err )
35- return "" , "" , err
38+ return "" , "" , errors .Wrapf (err , "generate rsa key" )
3639 }
3740
3841 privateKeyPEM := & pem.Block {Type : "RSA PRIVATE KEY" , Bytes : x509 .MarshalPKCS1PrivateKey (privateKey )}
3942 privateStr := string (pem .EncodeToMemory (privateKeyPEM ))
4043
4144 pub , err := exportSshPublicKey (& privateKey .PublicKey )
4245 if err != nil {
43- return "" , "" , err
46+ return "" , "" , errors .Wrapf (err , "export ssh public key" )
47+ }
48+ publicStr := string (pub )
49+
50+ return privateStr , publicStr , nil
51+ }
52+
53+ func GenerateED25519SSHKeypair () (string , string , error ) {
54+ publicKey , privateKey , err := ed25519 .GenerateKey (rand .Reader )
55+ if err != nil {
56+ return "" , "" , errors .Wrapf (err , "generate ed25519 key" )
57+ }
58+
59+ pemBlock , err := ssh .MarshalPrivateKey (crypto .PrivateKey (privateKey ), "" )
60+ if err != nil {
61+ return "" , "" , errors .Wrapf (err , "marshal pkix private key" )
62+ }
63+
64+ privateStr := string (pem .EncodeToMemory (pemBlock ))
65+
66+ pub , err := exportSshPublicKey (publicKey )
67+ if err != nil {
68+ return "" , "" , errors .Wrapf (err , "export ssh public key" )
69+ }
70+ publicStr := string (pub )
71+
72+ return privateStr , publicStr , nil
73+ }
74+
75+ func GenerateECDSASHAP521SSHKeypair () (string , string , error ) {
76+ privateKey , err := ecdsa .GenerateKey (elliptic .P521 (), rand .Reader )
77+ if err != nil {
78+ return "" , "" , errors .Wrapf (err , "generate ecdsa key" )
79+ }
80+
81+ pemBlock , err := ssh .MarshalPrivateKey (crypto .PrivateKey (privateKey ), "" )
82+ if err != nil {
83+ return "" , "" , errors .Wrapf (err , "marshal pkix private key" )
84+ }
85+
86+ privateStr := string (pem .EncodeToMemory (pemBlock ))
87+
88+ pub , err := exportSshPublicKey (& privateKey .PublicKey )
89+ if err != nil {
90+ return "" , "" , errors .Wrapf (err , "export ssh public key" )
4491 }
4592 publicStr := string (pub )
4693
@@ -53,13 +100,11 @@ func GenerateDSASSHKeypair() (string, string, error) {
53100 params := & privateKey .Parameters
54101 err := dsa .GenerateParameters (params , rand .Reader , dsa .L1024N160 )
55102 if err != nil {
56- log .Errorf ("generateParameter error %s" , err )
57- return "" , "" , err
103+ return "" , "" , errors .Wrapf (err , "generate dsa key" )
58104 }
59105 err = dsa .GenerateKey (& privateKey , rand .Reader )
60106 if err != nil {
61- log .Errorf ("generate key error %s" , err )
62- return "" , "" , err
107+ return "" , "" , errors .Wrapf (err , "generate dsa key" )
63108 }
64109
65110 type DsaASN1 struct {
@@ -80,16 +125,15 @@ func GenerateDSASSHKeypair() (string, string, error) {
80125
81126 privBytes , err := asn1 .Marshal (k )
82127 if err != nil {
83- log .Errorf ("asn1 marshal error %s" , err )
84- return "" , "" , err
128+ return "" , "" , errors .Wrapf (err , "asn1 marshal" )
85129 }
86130
87131 privateKeyPEM := & pem.Block {Type : "DSA PRIVATE KEY" , Bytes : privBytes }
88132 privateStr := string (pem .EncodeToMemory (privateKeyPEM ))
89133
90134 pub , err := exportSshPublicKey (& privateKey .PublicKey )
91135 if err != nil {
92- return "" , "" , err
136+ return "" , "" , errors . Wrapf ( err , "export ssh public key" )
93137 }
94138 publicStr := string (pub )
95139
@@ -104,8 +148,8 @@ func GetPublicKeyScheme(pubkey ssh.PublicKey) string {
104148 return "DSA"
105149 case ssh .KeyAlgoECDSA256 , ssh .KeyAlgoECDSA384 , ssh .KeyAlgoECDSA521 :
106150 return "ECDSA"
107- // case ssh.KeyAlgoED25519:
108- // return "ED "
151+ case ssh .KeyAlgoED25519 :
152+ return "ED25519 "
109153 }
110154 return "UNKNOWN"
111155}
0 commit comments