Skip to content

Commit

Permalink
fix: handle lower case digest algorith #24
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Oct 17, 2024
1 parent c180e3b commit 7bd3a58
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ func digestProxyAuthApply(req *sip.Request, res *sip.Response, opts digest.Optio
return fmt.Errorf("fail to parse challenge authHeader=%q: %w", authHeader.Value(), err)
}

// Fix lower case algorithm although not supported by rfc
chal.Algorithm = sip.ASCIIToUpper(chal.Algorithm)

// Reply with digest
cred, err := digest.Digest(chal, opts)
if err != nil {
Expand All @@ -448,6 +451,9 @@ func digestAuthApply(req *sip.Request, res *sip.Response, opts digest.Options) e
return fmt.Errorf("fail to parse chalenge wwwauth=%q: %w", wwwAuth.Value(), err)
}

// Fix lower case algorithm although not supported by rfc
chal.Algorithm = sip.ASCIIToUpper(chal.Algorithm)

// Reply with digest
cred, err := digest.Digest(chal, opts)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/emiago/sipgo/sip"
"github.com/icholy/digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -202,3 +203,17 @@ func TestClientRequestOptions(t *testing.T) {
conn, err := tp.ClientRequestConnection(context.TODO(), req)
}
*/

func TestDigestAuthLowerCase(t *testing.T) {
challenge := `Digest username="user", realm="asterisk", nonce="662d65a084b88c6d2a745a9de086fa91", uri="sip:+user@example.com", algorithm=sha-256, response="3681b63e5d9c3bb80e5350e2783d7b88"`
chal, err := digest.ParseChallenge(challenge)
require.NoError(t, err)
chal.Algorithm = sip.ASCIIToUpper(chal.Algorithm)

_, err = digest.Digest(chal, digest.Options{
Method: "INVITE",
Username: "user",
URI: "sip:+user@example.com",
})
require.NoError(t, err)
}
27 changes: 27 additions & 0 deletions sip/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ func ASCIIToLowerInPlace(s []byte) {
}
}

func ASCIIToUpper(s string) string {
// first check is ascii already up to avoid alloc
nonLowInd := -1
for i, c := range s {
if 'A' <= c && c <= 'Z' {
continue
}
nonLowInd = i
break
}
if nonLowInd < 0 {
return s
}

var b strings.Builder
b.Grow(len(s))
b.WriteString(s[:nonLowInd])
for i := nonLowInd; i < len(s); i++ {
c := s[i]
if 'a' <= c && c <= 'z' {
c -= 'a' - 'A'
}
b.WriteByte(c)
}
return b.String()
}

// HeaderToLower is fast ASCII lower string
func HeaderToLower(s string) string {
// Avoid allocations
Expand Down

0 comments on commit 7bd3a58

Please sign in to comment.