-
Notifications
You must be signed in to change notification settings - Fork 66
/
agent.go
188 lines (166 loc) · 3.9 KB
/
agent.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
package sshagent
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"github.com/buptczq/WinCryptSSHAgent/capi"
"github.com/buptczq/WinCryptSSHAgent/utils"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"sync"
)
type sshKey struct {
cert *capi.Certificate
signer ssh.Signer
comment string
}
type CAPIAgent struct {
mu sync.Mutex
keys []*sshKey
}
func (s *CAPIAgent) close() (err error) {
for _, key := range s.keys {
err = key.cert.Free()
}
s.keys = nil
return
}
func (s *CAPIAgent) Close() (err error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.close()
}
func (s *CAPIAgent) loadCerts() (err error) {
certs, err := capi.LoadUserCerts()
if err != nil {
return
}
s.keys = make([]*sshKey, 0, len(certs))
for _, cert := range certs {
if !FilterCertificateEKU(cert) {
cert.Free()
continue
}
pub, err := ssh.NewPublicKey(cert.PublicKey)
if err != nil {
cert.Free()
continue
}
key := &sshKey{
cert: cert,
comment: cert.Subject.CommonName,
}
switch pub.Type() {
case ssh.KeyAlgoRSA:
key.signer = &rsaSigner{
pub: pub,
cert: cert,
}
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
key.signer = &ecdsaSigner{
pub: pub,
cert: cert,
}
default:
cert.Free()
continue
}
s.keys = append(s.keys, key)
if keyWithCert, err := loadSSHCertificate(key); err == nil {
s.keys = append(s.keys, keyWithCert)
}
}
return
}
func (s *CAPIAgent) List() (keys []*agent.Key, err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.keys != nil {
s.close()
}
err = s.loadCerts()
if err != nil {
return
}
var ids []*agent.Key
for _, k := range s.keys {
pub := k.signer.PublicKey()
ids = append(ids, &agent.Key{
Format: pub.Type(),
Blob: pub.Marshal(),
Comment: k.comment})
}
return ids, nil
}
func (s *CAPIAgent) Sign(key ssh.PublicKey, data []byte) (*ssh.Signature, error) {
return s.SignWithFlags(key, data, 0)
}
func (s *CAPIAgent) signed(comment string) {
utils.Notify(
"Authenticated",
"Authentication Success by Certificate <"+comment+">",
)
}
func (s *CAPIAgent) SignWithFlags(key ssh.PublicKey, data []byte, flags agent.SignatureFlags) (*ssh.Signature, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.keys == nil {
if err := s.loadCerts(); err != nil {
return nil, err
}
}
wanted := key.Marshal()
for _, k := range s.keys {
if bytes.Equal(k.signer.PublicKey().Marshal(), wanted) {
if flags == 0 {
sign, err := k.signer.Sign(rand.Reader, data)
if err == nil {
s.signed(k.comment)
}
return sign, err
} else {
if algorithmSigner, ok := k.signer.(ssh.AlgorithmSigner); !ok {
return nil, fmt.Errorf("agent: signature does not support non-default signature algorithm: %T", k.signer)
} else {
var algorithm string
switch flags {
case agent.SignatureFlagRsaSha256:
algorithm = ssh.SigAlgoRSASHA2256
case agent.SignatureFlagRsaSha512:
algorithm = ssh.SigAlgoRSASHA2512
default:
return nil, fmt.Errorf("agent: unsupported signature flags: %d", flags)
}
sign, err := algorithmSigner.SignWithAlgorithm(rand.Reader, data, algorithm)
if err == nil {
s.signed(k.comment)
}
return sign, err
}
}
}
}
return nil, errors.New("not found")
}
func (*CAPIAgent) Add(key agent.AddedKey) error {
return fmt.Errorf("implement me")
}
func (*CAPIAgent) Remove(key ssh.PublicKey) error {
return fmt.Errorf("implement me")
}
func (*CAPIAgent) RemoveAll() error {
return fmt.Errorf("implement me")
}
func (*CAPIAgent) Lock(passphrase []byte) error {
return fmt.Errorf("implement me")
}
func (*CAPIAgent) Unlock(passphrase []byte) error {
return fmt.Errorf("implement me")
}
func (*CAPIAgent) Signers() ([]ssh.Signer, error) {
return nil, fmt.Errorf("implement me")
}
func (s *CAPIAgent) Extension(extensionType string, contents []byte) ([]byte, error) {
return nil, agent.ErrExtensionUnsupported
}