4
4
package server
5
5
6
6
import (
7
- "crypto/md5" //nolint:gosec,gci
8
7
"errors"
9
8
"fmt"
10
- "io"
11
9
"math/rand"
12
10
"net"
13
- "strconv"
14
11
"time"
15
12
16
13
"github.com/pion/stun"
@@ -19,7 +16,6 @@ import (
19
16
20
17
const (
21
18
maximumAllocationLifetime = time .Hour // See: https://tools.ietf.org/html/rfc5766#section-6.2 defines 3600 seconds recommendation
22
- nonceLifetime = time .Hour // See: https://tools.ietf.org/html/rfc5766#section-4
23
19
)
24
20
25
21
func randSeq (n int ) string {
@@ -31,18 +27,6 @@ func randSeq(n int) string {
31
27
return string (b )
32
28
}
33
29
34
- func buildNonce () (string , error ) {
35
- /* #nosec */
36
- h := md5 .New ()
37
- if _ , err := io .WriteString (h , strconv .FormatInt (time .Now ().Unix (), 10 )); err != nil {
38
- return "" , fmt .Errorf ("%w: %v" , errFailedToGenerateNonce , err ) //nolint:errorlint
39
- }
40
- if _ , err := io .WriteString (h , strconv .FormatInt (rand .Int63 (), 10 )); err != nil { //nolint:gosec
41
- return "" , fmt .Errorf ("%w: %v" , errFailedToGenerateNonce , err ) //nolint:errorlint
42
- }
43
- return fmt .Sprintf ("%x" , h .Sum (nil )), nil
44
- }
45
-
46
30
func buildAndSend (conn net.PacketConn , dst net.Addr , attrs ... stun.Setter ) error {
47
31
msg , err := stun .Build (attrs ... )
48
32
if err != nil {
@@ -70,16 +54,11 @@ func buildMsg(transactionID [stun.TransactionIDSize]byte, msgType stun.MessageTy
70
54
71
55
func authenticateRequest (r Request , m * stun.Message , callingMethod stun.Method ) (stun.MessageIntegrity , bool , error ) {
72
56
respondWithNonce := func (responseCode stun.ErrorCode ) (stun.MessageIntegrity , bool , error ) {
73
- nonce , err := buildNonce ()
57
+ nonce , err := r . NonceHash . Generate ()
74
58
if err != nil {
75
59
return nil , false , err
76
60
}
77
61
78
- // Nonce has already been taken
79
- if _ , keyCollision := r .Nonces .LoadOrStore (nonce , time .Now ()); keyCollision {
80
- return nil , false , errDuplicatedNonce
81
- }
82
-
83
62
return nil , false , buildAndSend (r .Conn , r .SrcAddr , buildMsg (m .TransactionID ,
84
63
stun .NewType (callingMethod , stun .ClassErrorResponse ),
85
64
& stun.ErrorCodeAttribute {Code : responseCode },
@@ -101,15 +80,8 @@ func authenticateRequest(r Request, m *stun.Message, callingMethod stun.Method)
101
80
return nil , false , buildAndSendErr (r .Conn , r .SrcAddr , err , badRequestMsg ... )
102
81
}
103
82
104
- // Assert Nonce exists and is not expired
105
- nonceCreationTime , nonceFound := r .Nonces .Load (string (* nonceAttr ))
106
- if ! nonceFound {
107
- r .Nonces .Delete (nonceAttr )
108
- return respondWithNonce (stun .CodeStaleNonce )
109
- }
110
-
111
- if timeValue , ok := nonceCreationTime .(time.Time ); ! ok || time .Since (timeValue ) >= nonceLifetime {
112
- r .Nonces .Delete (nonceAttr )
83
+ // Assert Nonce is signed and is not expired
84
+ if err := r .NonceHash .Validate (nonceAttr .String ()); err != nil {
113
85
return respondWithNonce (stun .CodeStaleNonce )
114
86
}
115
87
0 commit comments