@@ -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.
1216type 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.
2428func (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