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
package main
import (
"context"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/openbao/openbao/sdk/v2/framework"
"github.com/openbao/openbao/sdk/v2/logical"
"golang.org/x/crypto/ssh"
)
func pathBind(b *backend) *framework.Path {
return &framework.Path{
Pattern: "bind$",
Fields: map[string]*framework.FieldSchema{
"role": {Type: framework.TypeString, Description: "Name of the role to bind.", Required: true},
"bind_secret": {Type: framework.TypeString, Description: "The one-time bind secret.", Required: true},
"host_key": {Type: framework.TypeString, Description: "The ed25519 ssh host public key, in authorized_keys form.", Required: true},
"signature": {Type: framework.TypeString, Description: "Armored sshsig over the bind secret by the host key, namespace prove-sshd.", Required: true},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{Callback: b.bind},
},
HelpSynopsis: "Binds an ed25519 ssh host key to a role, once, with the role's bind secret.",
}
}
func pathChallenge(b *backend) *framework.Path {
return &framework.Path{
Pattern: "challenge$",
Fields: map[string]*framework.FieldSchema{
"role": {Type: framework.TypeString, Description: "Name of a bound role.", Required: true},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{Callback: b.challenge},
},
HelpSynopsis: "Issues a short-lived, single-use login challenge for a bound role.",
}
}
func pathLogin(b *backend) *framework.Path {
return &framework.Path{
Pattern: "login$",
Fields: map[string]*framework.FieldSchema{
"role": {Type: framework.TypeString, Description: "Name of a bound role.", Required: true},
"challenge": {Type: framework.TypeString, Description: "A challenge issued for the role.", Required: true},
"signature": {Type: framework.TypeString, Description: "Armored sshsig over the challenge by the bound host key, namespace prove-sshd.", Required: true},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{Callback: b.login},
},
HelpSynopsis: "Logs in with an sshsig over a challenge made by the role's bound host key.",
}
}
func requiredStrings(d *framework.FieldData, names ...string) ([]string, *logical.Response) {
out := make([]string, len(names))
for i, n := range names {
v := strings.TrimSpace(d.Get(n).(string))
if v == "" {
return nil, logical.ErrorResponse(fmt.Sprintf("%s is required", n))
}
out[i] = v
}
return out, nil
}
func refused(msg string) (*logical.Response, error) {
return logical.ErrorResponse(msg), logical.ErrPermissionDenied
}
func (b *backend) bind(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
in, bad := requiredStrings(d, "role", "bind_secret", "host_key", "signature")
if bad != nil {
return bad, nil
}
name, secret, hostKeyText, signature := in[0], in[1], in[2], in[3]
hostKey, err := parseEd25519AuthorizedKey(hostKeyText)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
b.lock.Lock()
defer b.lock.Unlock()
r, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if r == nil {
return refused("bind refused")
}
if r.HostKey != "" || r.BindSecretHash == "" {
return refused(fmt.Sprintf("bind refused: role %q is already bound", name))
}
sum := sha256.Sum256([]byte(secret))
want, err := hex.DecodeString(r.BindSecretHash)
if err != nil || subtle.ConstantTimeCompare(sum[:], want) != 1 {
return refused("bind refused")
}
signer, err := verifySSHSig(signature, proveNamespace, []byte(secret))
if err != nil {
return refused("bind refused: " + err.Error())
}
if !sameKey(signer, hostKey) {
return refused("bind refused: the signature is not by host_key")
}
r.HostKey = strings.TrimSpace(string(ssh.MarshalAuthorizedKey(hostKey)))
r.BindSecretHash = ""
r.BoundAt = time.Now().UTC()
r.Challenges = nil
if err := putRole(ctx, req.Storage, name, r); err != nil {
return nil, err
}
return &logical.Response{Data: map[string]interface{}{
"role": name,
"host_key": r.HostKey,
"host_key_fingerprint": ssh.FingerprintSHA256(hostKey),
"bound_at": r.BoundAt.Format(time.RFC3339),
}}, nil
}
func (b *backend) challenge(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
in, bad := requiredStrings(d, "role")
if bad != nil {
return bad, nil
}
name := in[0]
nonce := make([]byte, 32)
if _, err := rand.Read(nonce); err != nil {
return nil, err
}
value := base64.RawURLEncoding.EncodeToString(nonce)
b.lock.Lock()
defer b.lock.Unlock()
r, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if r == nil || r.HostKey == "" {
return refused("challenge refused")
}
now := time.Now().UTC()
pruneChallenges(r, now)
for len(r.Challenges) >= maxChallenges {
delete(r.Challenges, oldestChallenge(r))
}
if r.Challenges == nil {
r.Challenges = map[string]time.Time{}
}
expires := now.Add(r.ChallengeTTL)
r.Challenges[value] = expires
if err := putRole(ctx, req.Storage, name, r); err != nil {
return nil, err
}
return &logical.Response{Data: map[string]interface{}{
"challenge": value,
"expires_at": expires.Format(time.RFC3339),
"ttl": int64(r.ChallengeTTL.Seconds()),
}}, nil
}
func pruneChallenges(r *roleEntry, now time.Time) {
for c, exp := range r.Challenges {
if !now.Before(exp) {
delete(r.Challenges, c)
}
}
}
func oldestChallenge(r *roleEntry) string {
var oldest string
var at time.Time
for c, exp := range r.Challenges {
if oldest == "" || exp.Before(at) {
oldest, at = c, exp
}
}
return oldest
}
func (b *backend) login(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
in, bad := requiredStrings(d, "role", "challenge", "signature")
if bad != nil {
return bad, nil
}
name, challenge, signature := in[0], in[1], in[2]
b.lock.Lock()
defer b.lock.Unlock()
r, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if r == nil || r.HostKey == "" {
return refused("login refused")
}
now := time.Now().UTC()
expires, found := r.Challenges[challenge]
pruneChallenges(r, now)
delete(r.Challenges, challenge)
if err := putRole(ctx, req.Storage, name, r); err != nil {
return nil, err
}
if !found || !now.Before(expires) {
return refused("login refused: unknown or expired challenge")
}
bound, err := r.hostKey()
if err != nil {
return nil, err
}
signer, err := verifySSHSig(signature, proveNamespace, []byte(challenge))
if err != nil {
return refused("login refused: " + err.Error())
}
if !sameKey(signer, bound) {
return refused("login refused: the signature is not by the bound host key")
}
fingerprint := ssh.FingerprintSHA256(bound)
auth := &logical.Auth{
DisplayName: r.AliasName,
InternalData: map[string]interface{}{
"role": name,
"host_key_fingerprint": fingerprint,
},
Metadata: map[string]string{
"role": name,
"host_key_fingerprint": fingerprint,
},
Alias: &logical.Alias{
Name: r.AliasName,
Metadata: map[string]string{
"role": name,
},
},
}
if err := r.PopulateTokenAuth(auth, req); err != nil {
return nil, err
}
return &logical.Response{Auth: auth}, nil
}
func (b *backend) loginRenew(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
if req.Auth == nil {
return nil, errors.New("renew: no auth")
}
name, _ := req.Auth.InternalData["role"].(string)
fingerprint, _ := req.Auth.InternalData["host_key_fingerprint"].(string)
r, err := getRole(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if r == nil || fingerprint == "" || r.hostKeyFingerprint() != fingerprint {
return refused("renew refused: the role or its bound host key changed")
}
resp := &logical.Response{Auth: req.Auth}
resp.Auth.TTL = r.TokenTTL
resp.Auth.MaxTTL = r.TokenMaxTTL
resp.Auth.Period = r.TokenPeriod
return resp, nil
}