-
Notifications
You must be signed in to change notification settings - Fork 8
/
ssh_utils.go
229 lines (198 loc) · 4.84 KB
/
ssh_utils.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"crypto/dsa"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"math/big"
"strings"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
// FingerprintSHA256 returns the user presentation of the key's
// fingerprint as unpadded base64 encoded sha256 hash.
// This format was introduced from OpenSSH 6.8.
// https://www.openssh.com/txt/release-6.8
// https://tools.ietf.org/html/rfc4648#section-3.2 (unpadded base64 encoding)
func fingerprintSHA256(pubKey *agent.Key) string {
sha256sum := sha256.Sum256(pubKey.Blob)
hash := base64.RawStdEncoding.EncodeToString(sha256sum[:])
return "SHA256:" + hash
}
func getPubKey(addedKey *agent.AddedKey) (*agent.Key, error) {
return getPubKeyRaw(addedKey.PrivateKey, addedKey.Comment)
}
func getPubKeyRaw(key interface{}, comment string) (*agent.Key, error) {
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, err
}
pub := signer.PublicKey()
pubKey := &agent.Key{
Format: pub.Type(),
Blob: pub.Marshal(),
Comment: comment,
}
return pubKey, nil
}
func parseKeyBlob(blob []byte) (addedKey *agent.AddedKey, err error) {
var record struct {
Type string `sshtype:"17|25"`
Rest []byte `ssh:"rest"`
}
if err = ssh.Unmarshal(blob, &record); err != nil {
return nil, err
}
switch record.Type {
case ssh.KeyAlgoRSA:
addedKey, err = parseRSAKey(blob)
case ssh.KeyAlgoDSA:
addedKey, err = parseDSAKey(blob)
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
addedKey, err = parseECDSAKey(blob)
case ssh.KeyAlgoED25519:
addedKey, err = parseEd25519Key(blob)
case ssh.CertAlgoRSAv01:
addedKey, err = parseRSACert(blob)
case ssh.CertAlgoDSAv01:
addedKey, err = parseDSACert(blob)
case ssh.CertAlgoECDSA256v01, ssh.CertAlgoECDSA384v01, ssh.CertAlgoECDSA521v01:
addedKey, err = parseECDSACert(blob)
case ssh.CertAlgoED25519v01:
addedKey, err = parseEd25519Cert(blob)
default:
return nil, fmt.Errorf("agent: not implemented: %q", record.Type)
}
return
}
func (mk McKey) ToBlob() (blob []byte) {
switch k := mk.PrivateKey.(type) {
case *rsa.PrivateKey:
if len(k.Primes) != 2 {
log.Printf("agent: unsupported RSA key with %d primes\n", len(k.Primes))
return
}
k.Precompute()
blob = ssh.Marshal(rsaKeyMsg{
Type: ssh.KeyAlgoRSA,
N: k.N,
E: big.NewInt(int64(k.E)),
D: k.D,
Iqmp: k.Precomputed.Qinv,
P: k.Primes[0],
Q: k.Primes[1],
Comments: mk.Comment,
})
case *dsa.PrivateKey:
blob = ssh.Marshal(dsaKeyMsg{
Type: ssh.KeyAlgoDSA,
P: k.P,
Q: k.Q,
G: k.G,
Y: k.Y,
X: k.X,
Comments: mk.Comment,
})
case *ecdsa.PrivateKey:
nistID := fmt.Sprintf("nistp%d", k.Params().BitSize)
blob = ssh.Marshal(ecdsaKeyMsg{
Type: "ecdsa-sha2-" + nistID,
Curve: nistID,
KeyBytes: elliptic.Marshal(k.Curve, k.X, k.Y),
D: k.D,
Comments: mk.Comment,
})
case *ed25519.PrivateKey:
blob = ssh.Marshal(ed25519KeyMsg{
Type: ssh.KeyAlgoED25519,
Pub: []byte(*k)[32:],
Priv: []byte(*k),
Comments: mk.Comment,
})
}
return blob
}
func ToMcKey(addedKey *agent.AddedKey) *McKey {
mk := AddedKeyToMcKey(addedKey)
return &mk
}
func AddedKeyToMcKey(addedKey *agent.AddedKey) McKey {
mck := McKey{
PrivateKey: addedKey.PrivateKey,
Comment: addedKey.Comment,
}
return mck
}
func readOpensshComment(data []byte) string {
magic := append([]byte("openssh-key-v1"), 0)
remaining := data[len(magic):]
var w struct {
CipherName string
KdfName string
KdfOpts string
NumKeys uint32
PubKey []byte
PrivKeyBlock []byte
}
if err := ssh.Unmarshal(remaining, &w); err != nil {
return ""
}
pk1 := struct {
Check1 uint32
Check2 uint32
Keytype string
Rest []byte `ssh:"rest"`
}{}
if err := ssh.Unmarshal(w.PrivKeyBlock, &pk1); err != nil {
return ""
}
switch pk1.Keytype {
case ssh.KeyAlgoRSA:
// https://github.com/openssh/openssh-portable/blob/master/sshkey.c#L2760-L2773
key := struct {
N *big.Int
E *big.Int
D *big.Int
Iqmp *big.Int
P *big.Int
Q *big.Int
Comment string
Pad []byte `ssh:"rest"`
}{}
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil {
return ""
}
return key.Comment
case ssh.KeyAlgoED25519:
key := struct {
Pub []byte
Priv []byte
Comment string
Pad []byte `ssh:"rest"`
}{}
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil {
return ""
}
return key.Comment
default:
return ""
}
return ""
}
func readPubComment(fname string) string {
fileData, err := ioutil.ReadFile(fname)
if err != nil {
return ""
}
tokens := strings.Split(string(fileData), " ")
if len(tokens) < 3 {
return ""
}
return tokens[2]
}