/
ssh_agent.go
313 lines (259 loc) · 6.87 KB
/
ssh_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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/user"
"sync"
"time"
"github.com/inconshreveable/log15"
"github.com/psanford/lambda-ssh-ca/internal/browser"
"github.com/psanford/lambda-ssh-ca/internal/msgs"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
var (
serverURL = flag.String("url", "http://localhost:1234", "Server url")
socketPath = flag.String("l", "", "agent: path of the UNIX socket to listen on")
)
func main() {
flag.Parse()
if *socketPath == "" {
flag.Usage()
os.Exit(1)
}
handler := log15.StreamHandler(os.Stdout, log15.LogfmtFormat())
log15.Root().SetHandler(handler)
lgr := log15.New()
a := &Agent{}
os.Remove(*socketPath)
l, err := net.Listen("unix", *socketPath)
if err != nil {
log.Fatalln("Failed to listen on UNIX socket:", err)
}
log.Printf("listening on: %s", *socketPath)
for {
c, err := l.Accept()
if err != nil {
type temporary interface {
Temporary() bool
}
if err, ok := err.(temporary); ok && err.Temporary() {
delay := 1 * time.Second
lgr.Error("temp_accept_err", "err", err, "retry_in", delay)
time.Sleep(delay)
continue
}
lgr.Error("accept_error", "err", err)
os.Exit(1)
}
go a.serveConn(c)
}
}
func parseOpenSSHPublicCert(encoded []byte) (*ssh.Certificate, error) {
parts := bytes.SplitN(encoded, []byte(" "), 3)
if len(parts) < 2 {
return nil, errors.New("public key or certificate not in OpenSSH format")
}
decoded, err := base64.StdEncoding.DecodeString(string(parts[1]))
if err != nil {
return nil, err
}
key, err := ssh.ParsePublicKey(decoded)
if err != nil {
return nil, err
}
cert, ok := key.(*ssh.Certificate)
if !ok {
return nil, errors.New("value is not an ssh certificate")
}
return cert, nil
}
type Agent struct {
mu sync.Mutex
pubKey ssh.PublicKey
cert *ssh.Certificate
signer ssh.Signer
}
func (a *Agent) fetchCert() error {
a.mu.Lock()
defer a.mu.Unlock()
now := uint64(time.Now().Unix())
if a.cert != nil && now > a.cert.ValidAfter && now < a.cert.ValidBefore {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return fmt.Errorf("generate ssh key err: %w", err)
}
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return fmt.Errorf("new signer err: %w", err)
}
hostname, _ := os.Hostname()
username := os.Getenv("USER")
if user, err := user.Current(); err == nil {
username = user.Username
}
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
}
defer l.Close()
nonce := make([]byte, 32)
_, err = io.ReadFull(rand.Reader, nonce)
if err != nil {
panic(err)
}
nonceString := base64.URLEncoding.EncodeToString(nonce)
certResult := make(chan string)
go http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/"+nonceString {
w.WriteHeader(400)
fmt.Fprintln(w, "nope")
return
}
r.ParseForm()
cert := r.FormValue("cert")
select {
case certResult <- cert:
fmt.Fprintln(w, "cert loaded")
case <-ctx.Done():
fmt.Fprintln(w, "error cancelled")
}
}))
cbURL := "http://" + l.Addr().String() + "/" + nonceString
signReq := msgs.PleaseSignRequest{
PublicKey: signer.PublicKey().Marshal(),
Host: hostname,
Username: username,
CompleteCallbackURL: cbURL,
}
signReqBody, err := json.Marshal(signReq)
if err != nil {
panic(err)
}
signURL := *serverURL + "/please_sign"
resp, err := http.Post(signURL, "application/json", bytes.NewReader(signReqBody))
if err != nil {
return fmt.Errorf("please_sign request err: %w", err)
}
r := io.LimitReader(resp.Body, 1<<20)
body, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("read please_sign_resp err: %w", err)
}
var signResp msgs.PleaseSignResponse
err = json.Unmarshal(body, &signResp)
if err != nil {
return fmt.Errorf("unmarshal please_sign_resp err, body: %q, err: %w", body, err)
}
browser.Open(signResp.AuthorizeURL)
var certTxt string
select {
case certTxt = <-certResult:
case <-ctx.Done():
log.Printf("context timed out")
return ctx.Err()
}
pubKeyCert, err := parseOpenSSHPublicCert([]byte(certTxt))
if err != nil {
return fmt.Errorf("parse public key cert err: %w", err)
}
a.signer = signer
a.pubKey = signer.PublicKey()
a.cert = pubKeyCert
return nil
}
// List returns the identities known to the agent.
func (a *Agent) List() ([]*agent.Key, error) {
if a.pubKey == nil {
err := a.fetchCert()
if err != nil {
return nil, err
}
}
return []*agent.Key{
{
Format: a.pubKey.Type(),
Blob: a.pubKey.Marshal(),
Comment: fmt.Sprintf("webkey"),
},
{
Format: a.cert.Type(),
Blob: a.cert.Marshal(),
Comment: fmt.Sprintf("webcert"),
},
}, nil
}
func (a *Agent) serveConn(c net.Conn) {
lgr := log15.New()
if err := agent.ServeAgent(a, c); err != io.EOF {
lgr.Error("agent_conn_err", "err", err)
}
}
// Sign has the agent sign the data using a protocol 2 key as defined
// in [PROTOCOL.agent] section 2.6.2.
func (a *Agent) Sign(key ssh.PublicKey, data []byte) (*ssh.Signature, error) {
return a.SignWithFlags(key, data, 0)
}
func (a *Agent) SignWithFlags(key ssh.PublicKey, data []byte, flags agent.SignatureFlags) (*ssh.Signature, error) {
if a.pubKey == nil {
err := a.fetchCert()
if err != nil {
return nil, err
}
}
if !bytes.Equal(a.pubKey.Marshal(), key.Marshal()) {
return nil, fmt.Errorf("no private keys match the requested public key")
}
signer, err := ssh.NewCertSigner(a.cert, a.signer)
if err != nil {
return nil, fmt.Errorf("new cert signer err: %w", err)
}
lgr := log15.New()
lgr.Info("sign_req", "alg", key.Type(), "flags", flags, "signer", signer.PublicKey().Type())
return signer.Sign(rand.Reader, data)
}
var ErrOperationUnsupported = errors.New("operation unsupported")
// Add adds a private key to the agent.
func (a *Agent) Add(key agent.AddedKey) error {
return ErrOperationUnsupported
}
// Remove removes all identities with the given public key.
func (a *Agent) Remove(key ssh.PublicKey) error {
return ErrOperationUnsupported
}
// RemoveAll removes all identities.
func (a *Agent) RemoveAll() error {
return ErrOperationUnsupported
}
// Lock locks the agent. Sign and Remove will fail, and List will empty an empty list.
func (a *Agent) Lock(passphrase []byte) error {
return ErrOperationUnsupported
}
// Unlock undoes the effect of Lock
func (a *Agent) Unlock(passphrase []byte) error {
return ErrOperationUnsupported
}
// Signers returns signers for all the known keys.
func (a *Agent) Signers() ([]ssh.Signer, error) {
signer, err := ssh.NewSignerFromKey(a.signer)
if err != nil {
return nil, fmt.Errorf("get ssh signer err: %w", err)
}
return []ssh.Signer{signer}, nil
}