diff --git a/internal/server/turn.go b/internal/server/turn.go index 69e2285b..ed190bd8 100644 --- a/internal/server/turn.go +++ b/internal/server/turn.go @@ -7,12 +7,15 @@ import ( "fmt" "net" + "github.com/pion/randutil" "github.com/pion/stun/v2" "github.com/pion/turn/v3/internal/allocation" "github.com/pion/turn/v3/internal/ipnet" "github.com/pion/turn/v3/internal/proto" ) +const runesAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + // See: https://tools.ietf.org/html/rfc5766#section-6.2 func handleAllocateRequest(r Request, m *stun.Message) error { r.Log.Debugf("Received AllocateRequest from %s", r.SrcAddr.String()) @@ -106,7 +109,10 @@ func handleAllocateRequest(r Request, m *stun.Message) error { return buildAndSendErr(r.Conn, r.SrcAddr, err, insufficientCapacityMsg...) } requestedPort = randomPort - reservationToken = randSeq(8) + reservationToken, err = randutil.GenerateCryptoRandomString(8, runesAlpha) + if err != nil { + return err + } } // 7. At any point, the server MAY choose to reject the request with a diff --git a/internal/server/util.go b/internal/server/util.go index 9df12c22..d11f5291 100644 --- a/internal/server/util.go +++ b/internal/server/util.go @@ -5,10 +5,11 @@ package server import ( "crypto/md5" //nolint:gosec,gci + "crypto/rand" "errors" "fmt" "io" - "math/rand" + "math/big" "net" "strconv" "time" @@ -22,22 +23,21 @@ const ( nonceLifetime = time.Hour // See: https://tools.ietf.org/html/rfc5766#section-4 ) -func randSeq(n int) string { - letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - b := make([]rune, n) - for i := range b { - b[i] = letters[rand.Intn(len(letters))] //nolint:gosec - } - return string(b) -} - func buildNonce() (string, error) { /* #nosec */ h := md5.New() if _, err := io.WriteString(h, strconv.FormatInt(time.Now().Unix(), 10)); err != nil { return "", fmt.Errorf("%w: %v", errFailedToGenerateNonce, err) //nolint:errorlint } - if _, err := io.WriteString(h, strconv.FormatInt(rand.Int63(), 10)); err != nil { //nolint:gosec + + maxInt63 := big.NewInt(1<<63 - 1) + maxInt63.Add(maxInt63, big.NewInt(1)) + randInt63, err := rand.Int(rand.Reader, maxInt63) + if err != nil { + return "", fmt.Errorf("%w: %v", errFailedToGenerateNonce, err) //nolint:errorlint + } + + if _, err := io.WriteString(h, randInt63.String()); err != nil { //nolint:gosec return "", fmt.Errorf("%w: %v", errFailedToGenerateNonce, err) //nolint:errorlint } return fmt.Sprintf("%x", h.Sum(nil)), nil