-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
register.go
378 lines (336 loc) · 13.7 KB
/
register.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package webauthn
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/go-webauthn/webauthn/protocol"
wan "github.com/go-webauthn/webauthn/webauthn"
"github.com/google/uuid"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/types"
wantypes "github.com/gravitational/teleport/lib/auth/webauthntypes"
)
// RegistrationIdentity represents the subset of Identity methods used by
// RegistrationFlow.
type RegistrationIdentity interface {
UpsertWebauthnLocalAuth(ctx context.Context, user string, wla *types.WebauthnLocalAuth) error
GetWebauthnLocalAuth(ctx context.Context, user string) (*types.WebauthnLocalAuth, error)
GetTeleportUserByWebauthnID(ctx context.Context, webID []byte) (string, error)
GetMFADevices(ctx context.Context, user string, withSecrets bool) ([]*types.MFADevice, error)
UpsertMFADevice(ctx context.Context, user string, d *types.MFADevice) error
UpsertWebauthnSessionData(ctx context.Context, user, sessionID string, sd *wantypes.SessionData) error
GetWebauthnSessionData(ctx context.Context, user, sessionID string) (*wantypes.SessionData, error)
DeleteWebauthnSessionData(ctx context.Context, user, sessionID string) error
}
// WithInMemorySessionData returns a RegistrationIdentity implementation that
// keeps SessionData in memory.
func WithInMemorySessionData(identity RegistrationIdentity) RegistrationIdentity {
return &inMemoryIdentity{
RegistrationIdentity: identity,
sessionData: make(map[string]*wantypes.SessionData),
}
}
type inMemoryIdentity struct {
RegistrationIdentity
// mu guards the fields below it.
// We don't foresee concurrent use for inMemoryIdentity, but it's easy enough
// to play it safe.
mu sync.RWMutex
sessionData map[string]*wantypes.SessionData
}
func (identity *inMemoryIdentity) UpsertWebauthnSessionData(ctx context.Context, user, sessionID string, sd *wantypes.SessionData) error {
identity.mu.Lock()
defer identity.mu.Unlock()
identity.sessionData[sessionDataKey(user, sessionID)] = sd
return nil
}
func (identity *inMemoryIdentity) GetWebauthnSessionData(ctx context.Context, user, sessionID string) (*wantypes.SessionData, error) {
identity.mu.RLock()
defer identity.mu.RUnlock()
sd, ok := identity.sessionData[sessionDataKey(user, sessionID)]
if !ok {
return nil, trace.NotFound("session data for user %v not found ", user)
}
// The only known caller of GetWebauthnSessionData is the webauthn package
// itself, so we trust it to not modify the SessionData we are handing back.
return sd, nil
}
func (identity *inMemoryIdentity) DeleteWebauthnSessionData(ctx context.Context, user, sessionID string) error {
key := sessionDataKey(user, sessionID)
identity.mu.Lock()
defer identity.mu.Unlock()
if _, ok := identity.sessionData[key]; !ok {
return trace.NotFound("session data for user %v not found ", user)
}
delete(identity.sessionData, key)
return nil
}
func sessionDataKey(user, sessionID string) string {
return fmt.Sprintf("%v/%v", user, sessionID)
}
// RegistrationFlow represents the WebAuthn registration ceremony.
//
// Registration consists of:
//
// 1. Client requests a CredentialCreation (containing a challenge and various
// settings that may constrain allowed authenticators).
// 2. Server runs Begin(), generates a credential creation.
// 3. Client validates the credential creation, performs a user presence test
// (usually by asking the user to touch a secure token), and replies with a
// CredentialCreationResponse (containing the signed challenge and
// information about the credential and authenticator)
// 4. Server runs Finish()
// 5. If all server-side checks are successful, then registration is complete
// and the authenticator may now be used to login.
type RegistrationFlow struct {
Webauthn *types.Webauthn
Identity RegistrationIdentity
}
// Begin is the first step of the registration ceremony.
// The CredentialCreation created is relayed back to the client, who in turn
// performs a user presence check and signs the challenge contained within it.
// If passwordless is set, then registration asks the authenticator for a
// resident key.
// As a side effect Begin may assign (and record in storage) a WebAuthn ID for
// the user.
func (f *RegistrationFlow) Begin(ctx context.Context, user string, passwordless bool) (*wantypes.CredentialCreation, error) {
if user == "" {
return nil, trace.BadParameter("user required")
}
// Exclude known devices from the ceremony.
devices, err := f.Identity.GetMFADevices(ctx, user, false /* withSecrets */)
if err != nil {
return nil, trace.Wrap(err)
}
var exclusions []protocol.CredentialDescriptor
for _, dev := range devices {
// Skip existing U2F devices, letting users "upgrade" their registration is
// good for us.
if dev.GetU2F() != nil {
continue
}
// Let authenticator "upgrades" from non-resident (MFA) to resident
// (passwordless) happen, but prevent "downgrades" from resident to
// non-resident.
//
// Modern passkey implementations will "disobey" our MFA registrations and
// actually create passkeys, silently replacing the old passkey with the new
// "MFA" key, which can make Teleport confused (for example, by letting the
// "MFA" key be deleted because Teleport thinks the passkey still exists).
if webDev := dev.GetWebauthn(); webDev != nil && !webDev.ResidentKey && passwordless {
continue
}
cred, ok := deviceToCredential(dev, true /* idOnly */)
if !ok {
continue
}
exclusions = append(exclusions, protocol.CredentialDescriptor{
Type: protocol.PublicKeyCredentialType,
CredentialID: cred.ID,
})
}
webID, err := upsertOrGetWebID(ctx, user, f.Identity)
if err != nil {
return nil, trace.Wrap(err)
}
u := newWebUser(user, webID, true /* credentialIDOnly */, nil /* devices */)
web, err := newWebAuthn(webAuthnParams{
cfg: f.Webauthn,
rpID: f.Webauthn.RPID,
requireResidentKey: passwordless,
requireUserVerification: passwordless,
})
if err != nil {
return nil, trace.Wrap(err)
}
cc, sessionData, err := web.BeginRegistration(u, wan.WithExclusions(exclusions))
if err != nil {
return nil, trace.Wrap(err)
}
// TODO(codingllama): Send U2F App ID back in creation requests too. Useful to
// detect duplicate devices.
sd, err := wantypes.SessionDataFromProtocol(sessionData)
if err != nil {
return nil, trace.Wrap(err)
}
if err := f.Identity.UpsertWebauthnSessionData(ctx, user, scopeSession, sd); err != nil {
return nil, trace.Wrap(err)
}
return wantypes.CredentialCreationFromProtocol(cc), nil
}
func upsertOrGetWebID(ctx context.Context, user string, identity RegistrationIdentity) ([]byte, error) {
wla, err := identity.GetWebauthnLocalAuth(ctx, user)
switch {
case trace.IsNotFound(err): // first-time user, create a new ID
webID := []byte(uuid.New().String())
err := identity.UpsertWebauthnLocalAuth(ctx, user, &types.WebauthnLocalAuth{
UserID: webID,
})
return webID[:], trace.Wrap(err)
case err != nil:
return nil, trace.Wrap(err)
}
// Attempt to fix the webID->user index, if necessary.
// This applies to legacy (Teleport 8.x) registrations and to eventual bad
// writes.
indexedUser, err := identity.GetTeleportUserByWebauthnID(ctx, wla.UserID)
if err != nil && !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
if indexedUser != user {
// Re-write wla to force an index update.
if err := identity.UpsertWebauthnLocalAuth(ctx, user, wla); err != nil {
return nil, trace.Wrap(err)
}
}
return wla.UserID, nil
}
// RegisterResponse represents fields needed to finish registering a new
// WebAuthn device.
type RegisterResponse struct {
// User is the device owner.
User string
// DeviceName is the name for the new device.
DeviceName string
// CreationResponse is the response from the new device.
CreationResponse *wantypes.CredentialCreationResponse
// Passwordless is true if this is expected to be a passwordless registration.
// Callers may make certain concessions when processing passwordless
// registration (such as skipping password validation), this flag reflects that.
// The data stored in the Begin SessionData must match the passwordless flag,
// otherwise the registration is denied.
Passwordless bool
}
// Finish is the second and last step of the registration ceremony.
// If successful, it returns the created MFADevice. Finish has the side effect
// or writing the device to storage (using its Identity interface).
func (f *RegistrationFlow) Finish(ctx context.Context, req RegisterResponse) (*types.MFADevice, error) {
switch {
case req.User == "":
return nil, trace.BadParameter("user required")
case req.DeviceName == "":
return nil, trace.BadParameter("device name required")
case req.CreationResponse == nil:
return nil, trace.BadParameter("credential creation response required")
}
parsedResp, err := parseCredentialCreationResponse(req.CreationResponse)
if err != nil {
return nil, trace.Wrap(err)
}
origin := parsedResp.Response.CollectedClientData.Origin
if err := validateOrigin(origin, f.Webauthn.RPID); err != nil {
log.WithError(err).Debugf("WebAuthn: origin validation failed")
return nil, trace.Wrap(err)
}
// TODO(codingllama): Verify that the public key matches the allowed
// credential params? It doesn't look like duo-labs/webauthn does that.
wla, err := f.Identity.GetWebauthnLocalAuth(ctx, req.User)
if err != nil {
return nil, trace.Wrap(err)
}
u := newWebUser(req.User, wla.UserID, true /* credentialIDOnly */, nil /* devices */)
sd, err := f.Identity.GetWebauthnSessionData(ctx, req.User, scopeSession)
if err != nil {
return nil, trace.Wrap(err)
}
sessionData := wantypes.SessionDataToProtocol(sd)
// Activate passwordless switches (resident key, user verification) if we
// required verification in the begin step.
passwordless := sessionData.UserVerification == protocol.VerificationRequired
if req.Passwordless && !passwordless {
return nil, trace.BadParameter("passwordless registration failed, requested CredentialCreation was for an MFA registration")
}
web, err := newWebAuthn(webAuthnParams{
cfg: f.Webauthn,
rpID: f.Webauthn.RPID,
origin: origin,
requireResidentKey: passwordless,
requireUserVerification: passwordless,
})
if err != nil {
return nil, trace.Wrap(err)
}
credential, err := web.CreateCredential(u, *sessionData, parsedResp)
if err != nil {
// Use a more friendly message for certain verification errors.
protocolErr := &protocol.Error{}
if errors.As(err, &protocolErr) &&
protocolErr.Type == protocol.ErrVerification.Type &&
passwordless &&
!parsedResp.Response.AttestationObject.AuthData.Flags.UserVerified() {
log.WithError(err).Debug("WebAuthn: Replacing verification error with PIN message")
return nil, trace.BadParameter("authenticator doesn't support passwordless, setting up a PIN may fix this")
}
return nil, trace.Wrap(err)
}
// Finally, check against attestation settings, if any.
// This runs after web.CreateCredential so we can take advantage of the
// attestation format validation it performs.
if err := verifyAttestation(f.Webauthn, parsedResp.Response.AttestationObject); err != nil {
return nil, trace.Wrap(err)
}
newDevice := types.NewMFADevice(req.DeviceName, uuid.NewString() /* id */, time.Now() /* addedAt */)
newDevice.Device = &types.MFADevice_Webauthn{
Webauthn: &types.WebauthnDevice{
CredentialId: credential.ID,
PublicKeyCbor: credential.PublicKey,
AttestationType: credential.AttestationType,
Aaguid: credential.Authenticator.AAGUID,
SignatureCounter: credential.Authenticator.SignCount,
AttestationObject: req.CreationResponse.AttestationResponse.AttestationObject,
ResidentKey: req.Passwordless,
CredentialRpId: f.Webauthn.RPID,
},
}
// We delegate a few checks to identity, including:
// * The validity of the created MFADevice
// * Uniqueness validation of the deviceName
// * Uniqueness validation of the Webauthn credential ID.
if err := f.Identity.UpsertMFADevice(ctx, req.User, newDevice); err != nil {
return nil, trace.Wrap(err)
}
// Registration complete, remove the registration challenge we just used.
if err := f.Identity.DeleteWebauthnSessionData(ctx, req.User, scopeSession); err != nil {
log.Warnf("WebAuthn: failed to delete registration SessionData for user %v", req.User)
}
return newDevice, nil
}
func parseCredentialCreationResponse(resp *wantypes.CredentialCreationResponse) (*protocol.ParsedCredentialCreationData, error) {
// Remove extensions before marshaling, duo-labs/webauthn isn't expecting it.
exts := resp.Extensions
resp.Extensions = nil
defer func() {
resp.Extensions = exts
}()
// This is a roundabout way of getting resp validated, but unfortunately the
// APIs don't provide a better method (and it seems better than duplicating
// library code here).
body, err := json.Marshal(resp)
if err != nil {
return nil, trace.Wrap(err)
}
parsedResp, err := protocol.ParseCredentialCreationResponseBody(bytes.NewReader(body))
return parsedResp, trace.Wrap(err)
}