-
Notifications
You must be signed in to change notification settings - Fork 24
/
ssh.go
207 lines (164 loc) · 4.75 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package ssh
import (
"crypto"
"crypto/rand"
"encoding/pem"
"encoding/binary"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)
const (
sshMagic = "openssh-key-v1\x00"
)
// 配置
type Opts struct {
Cipher Cipher
KDFOpts KDFOpts
}
// 默认配置
var DefaultOpts = Opts{
Cipher: AES256CTR,
KDFOpts: BcryptOpts{
SaltSize: 16,
Rounds: 16,
},
}
type openSSHPrivateKey struct {
CipherName string
KdfName string
KdfOpts string
NumKeys uint32
PubKey []byte
PrivKeyBlock []byte
}
type openSSHPrivateKeyBlock struct {
Check1 uint32
Check2 uint32
Keytype string
Rest []byte `ssh:"rest"`
}
// 解析
func ParseOpenSSHPrivateKey(key []byte) (crypto.PrivateKey, string, error) {
return ParseOpenSSHPrivateKeyWithPassword(key, nil)
}
// 解析带密码
func ParseOpenSSHPrivateKeyWithPassword(key []byte, password []byte) (crypto.PrivateKey, string, error) {
if len(key) < len(sshMagic) || string(key[:len(sshMagic)]) != sshMagic {
return nil, "", errors.New("invalid openssh private key format")
}
remaining := key[len(sshMagic):]
var w openSSHPrivateKey
if err := ssh.Unmarshal(remaining, &w); err != nil {
return nil, "", err
}
if w.KdfName != "none" || w.CipherName != "none" {
newCipher, err := ParseCipher(w.CipherName)
if err != nil {
return nil, "", err
}
newKdf, err := ParsePbkdf(w.KdfName)
if err != nil {
return nil, "", err
}
size := newCipher.KeySize() + newCipher.BlockSize()
k, err := newKdf.DeriveKey(password, w.KdfOpts, size)
if err != nil {
return nil, "", errors.Wrap(err, "error deriving password")
}
dst, err := newCipher.Decrypt(k, w.PrivKeyBlock)
if err != nil {
return nil, "", err
}
w.PrivKeyBlock = dst
}
var pk1 openSSHPrivateKeyBlock
if err := ssh.Unmarshal(w.PrivKeyBlock, &pk1); err != nil {
if w.KdfName != "none" || w.CipherName != "none" {
return nil, "", errors.New("incorrect passphrase supplied")
}
return nil, "", err
}
if pk1.Check1 != pk1.Check2 {
if w.KdfName != "none" || w.CipherName != "none" {
return nil, "", errors.New("incorrect passphrase supplied")
}
return nil, "", errors.New("error decoding key: check mismatch")
}
newKey, err := ParseKeytype(pk1.Keytype)
if err != nil {
return nil, "", err
}
parsedKey, comment, err := newKey.Parse(pk1.Rest)
if err != nil {
return nil, "", err
}
return parsedKey, comment, nil
}
// 编码
func MarshalOpenSSHPrivateKey(key crypto.PrivateKey, comment string) (*pem.Block, error) {
return MarshalOpenSSHPrivateKeyWithPassword(key, comment, nil)
}
// 编码
func MarshalOpenSSHPrivateKeyWithPassword(key crypto.PrivateKey, comment string, password []byte, opts ...Opts) (*pem.Block, error) {
var check uint32
if err := binary.Read(rand.Reader, binary.BigEndian, &check); err != nil {
return nil, errors.Wrap(err, "error generating random check ")
}
w := openSSHPrivateKey{
NumKeys: 1,
}
pk1 := openSSHPrivateKeyBlock{
Check1: check,
Check2: check,
}
opt := DefaultOpts
if len(opts) > 0 {
opt = opts[0]
}
if password == nil {
w.CipherName = "none"
w.KdfName = "none"
} else {
if opt.Cipher == nil {
return nil, errors.New("error opt Cipher is nil")
}
if opt.KDFOpts == nil {
return nil, errors.New("error opt KDFOpts is nil")
}
w.CipherName = opt.Cipher.Name()
w.KdfName = opt.KDFOpts.Name()
}
parsedKey, err := ParseKeytype(GetStructName(key))
if err != nil {
return nil, err
}
keyType, pubKey, rest, err := parsedKey.Marshal(key, comment)
if err != nil {
return nil, err
}
w.PubKey = pubKey
pk1.Keytype = keyType
pk1.Rest = rest
w.PrivKeyBlock = ssh.Marshal(pk1)
if password != nil {
newCipher := opt.Cipher
newKdf := opt.KDFOpts
size := newCipher.KeySize() + newCipher.BlockSize()
k, kdfOpts, err := newKdf.DeriveKey(password, size)
if err != nil {
return nil, errors.Wrap(err, "error deriving decryption key")
}
w.KdfOpts = kdfOpts
dst, err := newCipher.Encrypt(k, w.PrivKeyBlock)
if err != nil {
return nil, err
}
w.PrivKeyBlock = dst
}
b := ssh.Marshal(w)
block := &pem.Block{
Type: "OPENSSH PRIVATE KEY",
Bytes: append([]byte(sshMagic), b...),
}
return block, nil
}