-
Notifications
You must be signed in to change notification settings - Fork 15
/
2fa_webauthn.go
369 lines (321 loc) · 11.7 KB
/
2fa_webauthn.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/tstranex/u2f"
"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/webauthn"
"github.com/Cloud-Foundations/keymaster/lib/instrumentedwriter"
"github.com/Cloud-Foundations/keymaster/proto/eventmon"
)
// from: https://github.com/duo-labs/webauthn.io/blob/3f03b482d21476f6b9fb82b2bf1458ff61a61d41/server/response.go#L15
func webauthnJsonResponse(w http.ResponseWriter, d interface{}, c int) {
dj, err := json.Marshal(d)
if err != nil {
http.Error(w, "Error creating JSON response", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(c)
logger.Debugf(3, "webauth json response=%s", dj)
fmt.Fprintf(w, "%s", dj)
}
const webAutnRegististerRequestPath = "/webauthn/RegisterRequest/"
// RegisterRequest?
func (state *RuntimeState) webauthnBeginRegistration(w http.ResponseWriter, r *http.Request) {
logger.Debugf(3, "top of webauthnBeginRegistration")
if state.sendFailureToClientIfLocked(w, r) {
return
}
// /u2f/RegisterRequest/<assumed user>
// pieces[0] == "" pieces[1] = "u2f" pieces[2] == "RegisterRequest"
pieces := strings.Split(r.URL.Path, "/")
var assumedUser string
if len(pieces) >= 4 {
assumedUser = pieces[3]
} else {
logger.Debugf(1, "webauthnBeginRegistration: bad number of pieces")
http.Error(w, "error", http.StatusBadRequest)
return
}
// TODO(camilo_viecco1): reorder checks so that simple checks are done before checking user creds
authData, err := state.checkAuth(w, r, state.getRequiredWebUIAuthLevel())
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
// Check that they can change other users
if !state.IsAdminUserAndU2F(authData.Username, authData.AuthType) &&
authData.Username != assumedUser {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
profile, _, fromCache, err := state.LoadUserProfile(assumedUser)
if err != nil {
logger.Printf("webauthnBeginRegistration: loading profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
if fromCache {
logger.Printf("DB is being cached and requesting registration aborting it")
http.Error(w, "db backend is offline for writes", http.StatusServiceUnavailable)
return
}
profile.FixupCredential(assumedUser, assumedUser)
logger.Debugf(2, "webauthnBeginRegistration profile=%+v", profile)
logger.Debugf(2, "webauthnBeginRegistration: About to begin BeginRegistration")
options, sessionData, err := state.webAuthn.BeginRegistration(profile)
if err != nil {
state.logger.Printf("webauthnBeginRegistration: begin login failed %s", err)
// TODO: we should not be sending ALL the errors to clients
webauthnJsonResponse(w, err.Error(), http.StatusInternalServerError)
return
}
profile.WebauthnSessionData = sessionData
err = state.SaveUserProfile(assumedUser, profile)
if err != nil {
logger.Printf("Saving profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
webauthnJsonResponse(w, options, http.StatusOK)
}
const webAutnRegististerFinishPath = "/webauthn/RegisterFinish/"
func (state *RuntimeState) webauthnFinishRegistration(w http.ResponseWriter, r *http.Request) {
logger.Debugf(3, "top of webauthnFinishRegistration")
if state.sendFailureToClientIfLocked(w, r) {
return
}
// TODO: better pattern matching
// /u2f/RegisterRequest/<assumed user>
// pieces[0] == "" pieces[1] = "u2f" pieces[2] == "RegisterRequest"
pieces := strings.Split(r.URL.Path, "/")
var assumedUser string
if len(pieces) >= 4 {
assumedUser = pieces[3]
} else {
http.Error(w, "error", http.StatusBadRequest)
return
}
// TODO(camilo_viecco1): reorder checks so that simple checks are done before checking user creds
authData, err := state.checkAuth(w, r, state.getRequiredWebUIAuthLevel())
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
// Check that they can change other users
if !state.IsAdminUserAndU2F(authData.Username, authData.AuthType) &&
authData.Username != assumedUser {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
profile, _, fromCache, err := state.LoadUserProfile(assumedUser)
if err != nil {
logger.Printf("loading profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
if fromCache {
logger.Printf("DB is being cached and requesting registration aborting it")
http.Error(w, "db backend is offline for writes", http.StatusServiceUnavailable)
return
}
// load the session data
credential, err := state.webAuthn.FinishRegistration(profile, *profile.WebauthnSessionData, r)
if err != nil {
state.logger.Println(err)
webauthnJsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
logger.Debugf(2, "new credential=%+v\n", *credential)
err = profile.AddWebAuthnCredential(*credential)
if err != nil {
logger.Printf("Saving adding credential error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
err = state.SaveUserProfile(assumedUser, profile)
if err != nil {
logger.Printf("Saving profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
webauthnJsonResponse(w, "Registration Success", http.StatusOK)
}
const webAuthnAuthBeginPath = "/webauthn/AuthBegin/"
func (state *RuntimeState) webauthnAuthLogin(w http.ResponseWriter, r *http.Request) {
logger.Debugf(3, "top of webauthnAuthBegin")
if state.sendFailureToClientIfLocked(w, r) {
return
}
// TODO(camilo_viecco1): reorder checks so that simple checks are done before checking user creds
authData, err := state.checkAuth(w, r, AuthTypeAny)
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
profile, _, fromCache, err := state.LoadUserProfile(authData.Username)
if err != nil {
logger.Printf("loading profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
if fromCache {
logger.Debugf(1, "DB is being cached and requesting authentication, proceeding with cached values")
}
// TODO: there is an extension to ensure it is an actual secirity key... need to add this to the call.
extensions := protocol.AuthenticationExtensions{"appid": u2fAppID}
options, sessionData, err := state.webAuthn.BeginLogin(profile,
webauthn.WithAssertionExtensions(extensions))
if err != nil {
logger.Printf("webauthnAuthBegin: %s", err)
webauthnJsonResponse(w, err.Error(), http.StatusInternalServerError)
return
}
c, err := u2f.NewChallenge(u2fAppID, u2fTrustedFacets)
if err != nil {
logger.Printf("u2f.NewChallenge error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
c.Challenge, err = base64.RawURLEncoding.DecodeString(sessionData.Challenge)
if err != nil {
logger.Printf("webauthnAuthBegin base64 error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
var localAuth localUserData
localAuth.U2fAuthChallenge = c
localAuth.WebAuthnChallenge = sessionData
localAuth.ExpiresAt = time.Now().Add(maxAgeU2FVerifySeconds * time.Second)
state.Mutex.Lock()
state.localAuthData[authData.Username] = localAuth
state.Mutex.Unlock()
webauthnJsonResponse(w, options, http.StatusOK)
logger.Debugf(3, "end of webauthnAuthBegin")
}
const webAuthnAuthFinishPath = "/webauthn/AuthFinish/"
func (state *RuntimeState) webauthnAuthFinish(w http.ResponseWriter, r *http.Request) {
logger.Debugf(3, "top of webauthnAuthFinish")
if state.sendFailureToClientIfLocked(w, r) {
return
}
// TODO(camilo_viecco1): reorder checks so that simple checks are done before checking user creds
authData, err := state.checkAuth(w, r, AuthTypeAny)
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
profile, ok, _, err := state.LoadUserProfile(authData.Username)
if err != nil {
logger.Printf("loading profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
if !ok {
http.Error(w, "No regstered data", http.StatusBadRequest)
return
}
state.Mutex.Lock()
localAuth, ok := state.localAuthData[authData.Username]
state.Mutex.Unlock()
if !ok {
http.Error(w, "challenge missing", http.StatusBadRequest)
return
}
parsedResponse, err := protocol.ParseCredentialRequestResponse(r)
if err != nil {
logger.Printf("Error parsing Response err =%s", err)
http.Error(w, "", http.StatusBadRequest)
return
}
userCredentials := profile.WebAuthnCredentials()
var loginCredential webauthn.Credential
var credentialFound bool
var credentialIndex int64
for _, cred := range userCredentials {
if cred.AttestationType != "fido-u2f" {
continue
}
if bytes.Equal(cred.ID, parsedResponse.RawID) {
loginCredential = cred
credentialFound = true
for i, u2fReg := range profile.U2fAuthData {
if !u2fReg.Enabled {
continue
}
if bytes.Equal(u2fReg.Registration.KeyHandle, parsedResponse.RawID) {
credentialIndex = i
}
}
break
}
credentialFound = false
}
verifiedAuth := authData.AuthType
if !credentialFound {
// DO STD webaautn verification
_, err = state.webAuthn.ValidateLogin(profile, *localAuth.WebAuthnChallenge, parsedResponse) // iFinishLogin(profile, *localAuth.WebAuthnChallenge, r)
if err != nil {
logger.Printf("webauthnAuthFinish: auth failure err=%s", err)
webauthnJsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
// TODO also update the profile with latest counter
verifiedAuth = AuthTypeFIDO2
} else {
// NOTE: somehow the extensions for grabbing the appID are failing
// So we "unroll" the important pieces of webAuthn.ValidateLogin here, with
// explicit changes for our appID
// Notice that if we where strict we would iterate over all the alloowed values.
session := *localAuth.WebAuthnChallenge
shouldVerifyUser := session.UserVerification == protocol.VerificationRequired
rpID := state.webAuthn.Config.RPID
rpOrigin := state.webAuthn.Config.RPOrigin
appID := u2fAppID
// Handle steps 4 through 16
validError := parsedResponse.Verify(session.Challenge, rpID, rpOrigin, appID, shouldVerifyUser, loginCredential.PublicKey)
if validError != nil {
logger.Printf("failed to verify webauthn parsedResponse")
state.writeFailureResponse(w, r, http.StatusUnauthorized, "Credential Not Found")
return
}
//loginCredential.Authenticator.UpdateCounter(parsedResponse.Response.AuthenticatorData.Counter)
u2fReg, ok := profile.U2fAuthData[credentialIndex]
if ok {
u2fReg.Counter = parsedResponse.Response.AuthenticatorData.Counter
profile.U2fAuthData[credentialIndex] = u2fReg
go state.SaveUserProfile(authData.Username, profile)
}
verifiedAuth = AuthTypeU2F
logger.Debugf(3, "success (LOCAL)")
}
logger.Debugf(1, "webauthnAuthFinish: auth success")
// TODO: disinguish better between the two protocols or just use one
//metricLogAuthOperation(getClientType(r), proto.AuthTypeU2F, true)
state.Mutex.Lock()
delete(state.localAuthData, authData.Username)
state.Mutex.Unlock()
//TODO: distinguish here u2f vs webauthn
eventNotifier.PublishAuthEvent(eventmon.AuthTypeU2F, authData.Username)
_, isXHR := r.Header["X-Requested-With"]
if isXHR {
eventNotifier.PublishWebLoginEvent(authData.Username)
}
_, err = state.updateAuthCookieAuthlevel(w, r,
authData.AuthType|verifiedAuth|AuthTypeU2F)
if err != nil {
logger.Printf("Auth Cookie NOT found ? %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure updating vip token")
return
}
webauthnJsonResponse(w, "Login Success", http.StatusOK)
}