Skip to content

Commit 3121b47

Browse files
committed
Small fixes
1 parent 0b2733f commit 3121b47

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

pkg/protocol/extension/cookie.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"golang.org/x/crypto/cryptobyte"
1010
)
1111

12+
const maxCookieSize = 0xffff - 2
13+
14+
// CookieExt implements the cookie extension in DTLS 1.3.
15+
// See RFC 8446 section 4.2.2. Cookie.
1216
type CookieExt struct {
1317
Cookie []byte
1418
}
@@ -18,13 +22,13 @@ func (c CookieExt) TypeValue() TypeValue {
1822
return CookieTypeValue
1923
}
2024

21-
var errCoookieExtFormat = errors.New("invalid cookie format")
25+
var errCookieExtFormat = errors.New("invalid cookie format")
2226

2327
// Marshal encodes the extension.
2428
func (c *CookieExt) Marshal() ([]byte, error) {
2529
cookieLength := len(c.Cookie)
26-
if cookieLength == 0 || cookieLength > 0xfffd {
27-
return nil, errCoookieExtFormat
30+
if cookieLength == 0 || cookieLength > maxCookieSize {
31+
return nil, errCookieExtFormat
2832
}
2933
var b cryptobyte.Builder
3034
b.AddUint16(uint16(c.TypeValue()))
@@ -52,16 +56,11 @@ func (c *CookieExt) Unmarshal(data []byte) error { //nolint:cyclop
5256
}
5357

5458
var cookie cryptobyte.String
55-
if !extData.ReadUint16LengthPrefixed(&cookie) {
56-
return errCoookieExtFormat
57-
}
58-
59-
cookieLength := len(cookie)
60-
if cookieLength == 0 || cookieLength > 0xfffd {
61-
return errCoookieExtFormat
59+
if !extData.ReadUint16LengthPrefixed(&cookie) || cookie.Empty() || len(cookie) > maxCookieSize {
60+
return errCookieExtFormat
6261
}
6362

64-
c.Cookie = cookie
63+
c.Cookie = append([]byte(nil), cookie...)
6564

6665
return nil
6766
}

0 commit comments

Comments
 (0)