-
Notifications
You must be signed in to change notification settings - Fork 14
/
2fa_totp.go
564 lines (524 loc) · 18 KB
/
2fa_totp.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"html/template"
"image/png"
"math"
"net/http"
"regexp"
"strconv"
"time"
"github.com/Cloud-Foundations/keymaster/lib/instrumentedwriter"
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto"
"github.com/pquerna/otp/totp"
)
const labelRSA = "totp:rsa:"
const totpGeneratNewPath = "/totp/GenerateNew/"
const totpValidateNewPath = "/totp/ValidateNew/"
func (state *RuntimeState) encryptWithPublicKeys(clearTextMessage []byte) ([][]byte, error) {
var cipherTexts [][]byte
for _, key := range state.KeymasterPublicKeys {
logger.Debugf(3, "encryptWithPublicKeys: On internal loop with type %T", key)
// TODO: do Handle ECC keys
rsaPubKey, ok := key.(*rsa.PublicKey)
if ok {
label := []byte(labelRSA)
rng := rand.Reader
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rng, rsaPubKey, clearTextMessage, label)
if err != nil {
logger.Printf("Error from encryption: %s\n", err)
return nil, err
}
cipherTexts = append(cipherTexts, ciphertext)
continue
}
}
if len(cipherTexts) < 1 {
return nil, errors.New("cannot encrypt with any key")
}
return cipherTexts, nil
}
func (state *RuntimeState) decryptWithPublicKeys(cipherTexts [][]byte) ([]byte, error) {
logger.Debugf(5, "signer type=%T", state.Signer)
for _, cipherText := range cipherTexts {
rsaPrivateKey, ok := state.Signer.(*rsa.PrivateKey)
if ok {
label := []byte(labelRSA)
rng := rand.Reader
plaintext, err := rsa.DecryptOAEP(sha256.New(), rng, rsaPrivateKey, cipherText, label)
if err != nil {
logger.Printf("Error from decryption: %s\n", err)
continue
}
return plaintext, nil
}
}
return nil, errors.New("Cannot decrypt Message")
}
func (state *RuntimeState) GenerateNewTOTP(w http.ResponseWriter, r *http.Request) {
if state.sendFailureToClientIfLocked(w, r) {
return
}
// TODO: think if we are going to allow admins to register these tokens
authData, err := state.checkAuth(w, r, state.getRequiredWebUIAuthLevel())
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
// TODO: check if TOTP is even enabled.
// TODO: check for method, we should only allow POST requests
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.Printf("DB is being cached and requesting registration aborting it")
state.writeFailureResponse(w, r, http.StatusServiceUnavailable, "DB in cached state, cannot create new TOTP now")
return
}
logger.Debugf(2, "%v", profile)
key, err := totp.Generate(totp.GenerateOpts{
Issuer: state.HostIdentity,
AccountName: authData.Username,
})
if err != nil {
logger.Printf("generating new key error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
encryptedKeys, err := state.encryptWithPublicKeys([]byte(key.Secret()))
if err != nil {
logger.Printf("Encrypting key error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
profile.PendingTOTPSecret = &encryptedKeys
err = state.SaveUserProfile(authData.Username, profile)
if err != nil {
logger.Printf("Saving profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
logger.Debugf(3, "Generate TOTP: profile=%+v", profile)
// Convert TOTP key into a PNG
var buf bytes.Buffer
img, err := key.Image(200, 200)
if err != nil {
panic(err)
}
png.Encode(&buf, img)
base64Image := base64.StdEncoding.EncodeToString(buf.Bytes())
logger.Debugf(10, "base64image=%s", base64Image)
// We need custom CSP policy to allow embedded images
w.Header().Set("Content-Security-Policy", "default-src 'self' ;img-src 'self' data: ;style-src 'self' fonts.googleapis.com 'unsafe-inline'; font-src fonts.gstatic.com fonts.googleapis.com")
displayData := newTOTPPageTemplateData{
AuthUsername: authData.Username,
SessionExpires: authData.expires(),
Title: "New TOTP Generation", //TODO: maybe include username?
TOTPSecret: key.Secret(),
TOTPBase64Image: template.HTML("<img src=\"data:image/png;base64," + base64Image + "\" alt=\"beastie.png\" scale=\"0\" />"),
}
returnAcceptType := getPreferredAcceptType(r)
switch returnAcceptType {
case "text/html":
err = state.htmlTemplate.ExecuteTemplate(w, "newTOTPage", displayData)
if err != nil {
logger.Printf("Failed to execute %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
default:
json.NewEncoder(w).Encode(displayData)
}
return
}
func (state *RuntimeState) validateNewTOTP(w http.ResponseWriter, r *http.Request) {
authUser, _, otpValue, err := state.commonTOTPPostHandler(w, r, state.getRequiredWebUIAuthLevel())
if err != nil {
logger.Printf("Error in common Handler")
return
}
OTPString := fmt.Sprintf("%06d", otpValue)
profile, _, fromCache, err := state.LoadUserProfile(authUser)
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
}
if profile.PendingTOTPSecret == nil {
logger.Printf("No pending Secrets")
state.writeFailureResponse(w, r, http.StatusBadRequest, "No pending Secrets")
return
}
// TODO: The encrypted value MUST have also an expiration
encryptedKeys := profile.PendingTOTPSecret
clearTextKey, err := state.decryptWithPublicKeys(*encryptedKeys)
if err != nil {
logger.Printf("Decrypting secret error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
valid := totp.Validate(OTPString, string(clearTextKey))
if !valid {
//render try again vailidate page, with an error message
logger.Printf("Invalid Entry")
w.WriteHeader(http.StatusBadRequest)
displayData := newTOTPPageTemplateData{
AuthUsername: authUser,
Title: "Validate New TOTP Generation", //TODO: maybe include username?
ErrorMessage: "Invalid TOTP value, please try again",
}
returnAcceptType := getPreferredAcceptType(r)
switch returnAcceptType {
case "text/html":
err = state.htmlTemplate.ExecuteTemplate(w, "newTOTPage", displayData)
if err != nil {
logger.Printf("Failed to execute %v", err)
//http.Error(w, "error", http.StatusInternalServerError)
return
}
default:
json.NewEncoder(w).Encode(displayData)
}
return
}
// TODO: check if same secret already there
newTOTPAuthData := totpAuthData{
CreatedAt: time.Now(),
EncryptedSecret: *profile.PendingTOTPSecret,
ValidatorAddr: r.RemoteAddr,
Enabled: true,
}
newIndex := newTOTPAuthData.CreatedAt.Unix()
profile.TOTPAuthData[newIndex] = &newTOTPAuthData
profile.PendingTOTPSecret = nil
profile.UserHasRegistered2ndFactor = true
err = state.SaveUserProfile(authUser, profile)
if err != nil {
logger.Printf("Saving profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
//redirect to profile page?
http.Redirect(w, r, profilePath, 302)
}
const totpTokenManagementPath = "/api/v0/manageTOTPToken"
func (state *RuntimeState) totpTokenManagerHandler(w http.ResponseWriter,
r *http.Request) {
// User must be logged in
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, state.getRequiredWebUIAuthLevel())
if err != nil {
logger.Debugf(1, "%v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
// TODO: ensure is a valid method (POST)
if r.Method != "POST" {
logger.Printf("Wanted Post got='%s'", r.Method)
state.writeFailureResponse(w, r, http.StatusMethodNotAllowed, "")
return
}
err = r.ParseForm()
if err != nil {
logger.Println(err)
state.writeFailureResponse(w, r, http.StatusBadRequest,
"Error parsing form")
return
}
logger.Debugf(3, "Form: %+v", r.Form)
assumedUser := r.Form.Get("username")
// Have admin rights = Must be admin + authenticated with U2F
hasAdminRights := state.IsAdminUserAndU2F(authData.Username,
authData.AuthType)
// Check params
if !hasAdminRights && assumedUser != authData.Username {
logger.Printf("bad username authUser=%s requested=%s",
authData.Username, r.Form.Get("username"))
state.writeFailureResponse(w, r, http.StatusUnauthorized, "")
return
}
tokenIndex, err := strconv.ParseInt(r.Form.Get("index"), 10, 64)
if err != nil {
logger.Printf("tokenindex is not a number")
state.writeFailureResponse(w, r, http.StatusBadRequest,
"tokenindex is not a number")
return
}
//Do a redirect
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
}
// Todo: check for negative values
_, ok := profile.TOTPAuthData[tokenIndex]
if !ok {
logger.Printf("bad index number")
state.writeFailureResponse(w, r, http.StatusBadRequest,
"bad index Value")
return
}
actionName := r.Form.Get("action")
switch actionName {
case "Update":
tokenName := r.Form.Get("name")
if m, _ := regexp.MatchString("^[-/.a-zA-Z0-9_ ]+$", tokenName); !m {
logger.Printf("%s", tokenName)
state.writeFailureResponse(w, r, http.StatusBadRequest,
"invalidtokenName")
return
}
profile.TOTPAuthData[tokenIndex].Name = tokenName
case "Disable":
profile.TOTPAuthData[tokenIndex].Enabled = false
case "Enable":
profile.TOTPAuthData[tokenIndex].Enabled = true
case "Delete":
delete(profile.TOTPAuthData, tokenIndex)
default:
state.writeFailureResponse(w, r, http.StatusBadRequest,
"Invalid Operation")
return
}
err = state.SaveUserProfile(assumedUser, profile)
if err != nil {
logger.Printf("Saving profile error: %v", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
// Success!
returnAcceptType := getPreferredAcceptType(r)
switch returnAcceptType {
case "text/html":
http.Redirect(w, r, profileURI(authData.Username, assumedUser), 302)
default:
w.WriteHeader(200)
fmt.Fprintf(w, "Success!")
}
return
}
// TODO: these consts need to be eventually turned into config settings
const minSecsBetweenTOTPValidations = 2
const numHoursForLocalTOTPRateLimitReset = 24
const numFailedTOTPChecksForTimeoutIncrease = 5
// This function is the one actually validating the TOTP values, returns err non nil
// if there is a problem with the internal state. Returns true if the previous OTP success
// for this user is NOT on this period AND one of the otp values matches the one of the user's
// registered keys.
func (state *RuntimeState) validateUserTOTP(username string, OTPValue int, t time.Time) (bool, error) {
//Do a redirect
profile, _, fromCache, err := state.LoadUserProfile(username)
if err != nil {
logger.Printf("validateUserTOTP: loading profile error: %v", err)
return false, err
}
state.totpLocalTateLimitMutex.Lock()
userRateLimit := state.totpLocalRateLimit[username]
if userRateLimit.lastCheckTime.
Add(time.Second * time.Duration(minSecsBetweenTOTPValidations)).
After(time.Now()) {
state.totpLocalTateLimitMutex.Unlock()
return false, err
}
userRateLimit.lastCheckTime = time.Now()
state.totpLocalRateLimit[username] = userRateLimit
state.totpLocalTateLimitMutex.Unlock()
if userRateLimit.lockoutExpirationTime.After(time.Now()) {
return false, err
}
if userRateLimit.lastFailTime.
Add(time.Duration(numHoursForLocalTOTPRateLimitReset) * time.Hour).Before(time.Now()) {
userRateLimit.failCount = 0
userRateLimit.lockoutExpirationTime = time.Now()
}
if fromCache {
//TODO we what do do on disconnected? I think we should allow it to proceed, but
// enable a blacklist so that ip addresses/users have a limit of say 5/min?
logger.Printf("DB is being cached and requesting registration aborting it")
//http.Error(w, "db backend is offline for writes", http.StatusServiceUnavailable)
//return
}
//Check if value is on blacklist for that user?
// Check if there is a value successfully accepted for that counter value
const defaultPeriod = 30
counter := int64(math.Floor(float64(t.Unix()) / float64(defaultPeriod)))
if profile.LastSuccessfullTOTPCounter == counter {
logger.Printf("validateUserTOTP: already done TOTP within time period")
return false, nil
}
OTPString := fmt.Sprintf("%06d", OTPValue)
//Now iterate
for _, deviceInfo := range profile.TOTPAuthData {
if !deviceInfo.Enabled {
continue
}
clearTextKey, err := state.decryptWithPublicKeys(deviceInfo.EncryptedSecret)
if err != nil {
logger.Printf("Decrypting secret error: %v", err)
return false, err
}
valid := totp.Validate(OTPString, string(clearTextKey))
if !valid {
continue
}
if !fromCache {
profile.LastSuccessfullTOTPCounter = counter
err = state.SaveUserProfile(username, profile)
if err != nil {
logger.Printf("Saving profile error: %v", err)
return false, err
}
}
userRateLimit.failCount = 0
userRateLimit.lockoutExpirationTime = time.Now()
state.totpLocalTateLimitMutex.Lock()
state.totpLocalRateLimit[username] = userRateLimit
state.totpLocalTateLimitMutex.Unlock()
return true, nil
}
userRateLimit.failCount++
//every 5th bad try, make it wait an extra hour
if userRateLimit.failCount%numFailedTOTPChecksForTimeoutIncrease == 0 {
userRateLimit.lockoutExpirationTime.Add(time.Duration(3600) * time.Second)
}
userRateLimit.lastFailTime = time.Now()
state.totpLocalTateLimitMutex.Lock()
state.totpLocalRateLimit[username] = userRateLimit
state.totpLocalTateLimitMutex.Unlock()
return false, nil
}
func (state *RuntimeState) commonTOTPPostHandler(w http.ResponseWriter, r *http.Request, requiredAuthLevel int) (string, int, int, error) {
// User must be logged in
if state.sendFailureToClientIfLocked(w, r) {
return "", 0, 0, errors.New("server still sealed")
}
// TODO(camilo_viecco1): reorder checks so that simple checks are done before checking user creds
authData, err := state.checkAuth(w, r, requiredAuthLevel)
if err != nil {
logger.Debugf(1, "%v", err)
http.Error(w, "error", http.StatusInternalServerError)
return "", 0, 0, err
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
// TODO: ensure is a valid method (POST)
if r.Method != "POST" {
logger.Printf("Wanted Post got='%s'", r.Method)
state.writeFailureResponse(w, r, http.StatusMethodNotAllowed, "")
return "", 0, 0, errors.New("Invalid Method requeted")
}
err = r.ParseForm()
if err != nil {
logger.Println(err)
state.writeFailureResponse(w, r, http.StatusBadRequest, "Error parsing form")
return "", 0, 0, err
}
logger.Debugf(3, "Form: %+v", r.Form)
var OTPString string
if val, ok := r.Form["OTP"]; ok {
if len(val) > 1 {
state.writeFailureResponse(w, r, http.StatusBadRequest, "Just one OTP Value allowed")
logger.Printf("Login with multiple OTP Values")
return "", 0, 0, errors.New("multiple OTP values")
}
OTPString = val[0]
}
otpValue, err := strconv.Atoi(OTPString)
if err != nil {
logger.Println(err)
state.writeFailureResponse(w, r, http.StatusBadRequest, "Error parsing OTP value")
return "", 0, 0, err
}
return authData.Username, authData.AuthType, otpValue, nil
}
const totpVerifyHandlerPath = "/api/v0/VerifyTOTP"
func (state *RuntimeState) verifyTOTPHandler(w http.ResponseWriter, r *http.Request) {
authUser, _, otpValue, err := state.commonTOTPPostHandler(w, r, state.getRequiredWebUIAuthLevel())
if err != nil {
logger.Printf("Error in common Handler")
return
}
valid, err := state.validateUserTOTP(authUser, otpValue, time.Now())
if err != nil {
logger.Printf("Error validating UserTOTP. Err: %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "")
return
}
// TODO change these for real continuation Messages.
if !valid {
//Send to contine Page of Error?
state.writeFailureResponse(w, r, http.StatusOK, "Verification Failed")
return
}
http.Redirect(w, r, profilePath, 302)
}
const totpAuthPath = "/api/v0/TOTPAuth"
func (state *RuntimeState) TOTPAuthHandler(w http.ResponseWriter, r *http.Request) {
logger.Debugf(1, "Top of TOTPAuthHandler")
authUser, currentAuthLevel, otpValue, err := state.commonTOTPPostHandler(w, r, AuthTypeAny)
if err != nil {
logger.Printf("Error in common Handler err:%s", err)
return
}
logger.Debugf(1, "TOTPAuthHandler, After commonPostHandler, currentAuthLevel=%x", currentAuthLevel)
state.internalTOTPAuthHandler(w, r, authUser, currentAuthLevel, otpValue)
return
}
func (state *RuntimeState) internalTOTPAuthHandler(w http.ResponseWriter, r *http.Request, authUser string, currentAuthLevel int, otpValue int) {
valid, err := state.validateUserTOTP(authUser, otpValue, time.Now())
if err != nil {
logger.Printf("Error validating TOTP %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating OTP token")
return
}
if !valid {
logger.Printf("Invalid OTP value login for %s", authUser)
// TODO if client is html then do a redirect back to vipLoginPage
state.writeFailureResponse(w, r, http.StatusUnauthorized, "")
return
}
_, err = state.updateAuthCookieAuthlevel(w, r, currentAuthLevel|AuthTypeTOTP)
if err != nil {
logger.Printf("Auth Cookie NOT found ? %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating OTP token")
return
}
returnAcceptType := getPreferredAcceptType(r)
switch returnAcceptType {
case "text/html":
loginDestination := getLoginDestination(r)
http.Redirect(w, r, loginDestination, 302)
default:
loginResponse := proto.LoginResponse{Message: "success"}
w.WriteHeader(200)
json.NewEncoder(w).Encode(loginResponse)
}
return
}