Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick and probably nasty kludge to introduce increased entropy method… #3

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func main() {

loginError = WebAuthnError{Message: "Unable to login"}
registrationError = WebAuthnError{Message: "Error during registration"}
util.RandInit()

users = make(map[string]u.User)
registrations = make(map[string]u.User)
Expand Down Expand Up @@ -179,7 +178,7 @@ func main() {
}
webAuthns[rpOrigin] = webAuthn

var sessionStoreKey = util.RandStringBytesRmndr(32)
var sessionStoreKey = []byte(util.GenChallenge())
var sessionStore = sessions.NewCookieStore(sessionStoreKey)
// Sessions maintained for up to soft timeout limit
sessionStore.Options = &sessions.Options{
Expand Down Expand Up @@ -611,7 +610,7 @@ func checkOrigin(r *http.Request) (*webauthn.WebAuthn, *sessions.CookieStore, er
}
webAuthns[origin] = webAuthn

var sessionStoreKey = util.RandStringBytesRmndr(32)
var sessionStoreKey = []byte(util.GenChallenge())
var sessionStore = sessions.NewCookieStore(sessionStoreKey)
// Sessions maintained for up to soft timeout limit
sessionStore.Options = &sessions.Options{
Expand Down
26 changes: 8 additions & 18 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package util

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"regexp"

"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/webauthn"
"github.com/gorilla/sessions"

crypto_rand "crypto/rand"
"encoding/binary"
math_rand "math/rand"
)

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down Expand Up @@ -86,20 +84,12 @@ func SaveWebauthnSession(session *sessions.Session, key string, sessionData *web
return nil
}

func RandInit() {
var b [8]byte
_, err := crypto_rand.Read(b[:])
// Generate crytographically secure challenge
func GenChallenge() string {
//call on the import DUO method
challenge, err := protocol.CreateChallenge()
if err != nil {
panic("cannot seed math/rand package with cryptographically secure random number generator")
}
math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
}

// Generate a random string of alpha characters of length n
func RandStringBytesRmndr(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[math_rand.Int63()%int64(len(letterBytes))]
panic("Failed to generate cryographically secure challenge")
}
return b
return base64.RawURLEncoding.EncodeToString(challenge)
}