-
Notifications
You must be signed in to change notification settings - Fork 87
/
ssh.go
150 lines (114 loc) · 3.66 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ssh
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"math"
"math/big"
"strings"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/ssh"
)
type KeyAlgorithm string
const (
RSAKeyAlgorithm = "rsa"
Ed25519KeyAlgorithm = "ed25519"
RSAKeyLengthBits = 2048
)
var KeyAlgorithms = []string{RSAKeyAlgorithm, Ed25519KeyAlgorithm}
func CreateSSHKeyPair(keyAlgorithm string) (string, string, error) {
switch keyAlgorithm {
case RSAKeyAlgorithm:
return createSSHRSAKeyPair()
case Ed25519KeyAlgorithm:
return createSSHEd25519KeyPair()
default:
return "", "", fmt.Errorf("unsupported key algorithm: %s", keyAlgorithm)
}
}
func createSSHRSAKeyPair() (string, string, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, RSAKeyLengthBits)
if err != nil {
return "", "", fmt.Errorf("failed to generate rsa key pair: %w", err)
}
// generate and write private key as PEM
var privKeyBuf strings.Builder
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
return "", "", fmt.Errorf("failed to encode private key: %w", err)
}
// generate and write public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return "", "", fmt.Errorf("failed to generate public key: %w", err)
}
var pubKeyBuf strings.Builder
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub))
return pubKeyBuf.String(), privKeyBuf.String(), nil
}
func createSSHEd25519KeyPair() (string, string, error) {
pubKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return "", "", fmt.Errorf("failed to generate ed25519 key pair: %w", err)
}
// generate and write private key as PEM
var privKeyBuf strings.Builder
ed25519PrivateKey, err := marshalED25519PrivateKey(privateKey)
if err != nil {
return "", "", err
}
privateKeyPEM := &pem.Block{Type: "OPENSSH PRIVATE KEY", Bytes: ed25519PrivateKey}
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
return "", "", fmt.Errorf("failed to encode private key: %w", err)
}
pub, _ := ssh.NewPublicKey(pubKey)
var pubKeyBuf strings.Builder
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub))
return pubKeyBuf.String(), privKeyBuf.String(), nil
}
// marshalED25519PrivateKey is taken from https://github.com/mikesmitty/edkey
func marshalED25519PrivateKey(key ed25519.PrivateKey) ([]byte, error) {
magic := append([]byte("openssh-key-v1"), 0)
message := ed25519message{}
pk1 := ed25519pk1{}
rnd, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
if err != nil {
return nil, fmt.Errorf("failed to generate random number: %w", err)
}
//nolint:gosec // it won't overflow, as the max value is set to math.MaxUint32
{
pk1.Check1 = uint32(rnd.Uint64())
pk1.Check2 = uint32(rnd.Uint64())
}
pk1.Keytype = ssh.KeyAlgoED25519
publicKey, ok := key.Public().(ed25519.PublicKey)
if !ok {
return nil, errors.New("failed to convert public key")
}
pubKey := []byte(publicKey)
pk1.Pub = pubKey
pk1.Priv = key
pk1.Comment = ""
bs := 8
blockLen := len(ssh.Marshal(pk1))
padLen := (bs - (blockLen % bs)) % bs
pk1.Pad = make([]byte, padLen)
for i := range padLen {
pk1.Pad[i] = byte(i + 1)
}
pubkeyFull := []byte{0x0, 0x0, 0x0, 0x0b}
pubkeyFull = append(pubkeyFull, []byte(ssh.KeyAlgoED25519)...)
pubkeyFull = append(pubkeyFull, []byte{0x0, 0x0, 0x0, 0x20}...)
pubkeyFull = append(pubkeyFull, pubKey...)
message.CipherName = "none"
message.KdfName = "none"
message.KdfOpts = ""
message.NumKeys = 1
message.PubKey = pubkeyFull
message.PrivKeyBlock = ssh.Marshal(pk1)
magic = append(magic, ssh.Marshal(message)...)
return magic, nil
}