@@ -71,6 +71,8 @@ func ExtensionFromID(id uint16) TLSExtension {
7171 return & NPNExtension {}
7272 case utlsExtensionApplicationSettings :
7373 return & ApplicationSettingsExtension {}
74+ case utlsExtensionApplicationSettingsNew :
75+ return & ApplicationSettingsExtensionNew {}
7476 case fakeOldExtensionChannelID :
7577 return & FakeChannelIDExtension {true }
7678 case fakeExtensionChannelID :
@@ -684,39 +686,39 @@ func (e *ALPNExtension) Write(b []byte) (int, error) {
684686 return fullLen , nil
685687}
686688
687- // ApplicationSettingsExtension represents the TLS ALPS extension.
689+ // applicationSettingsExtension represents the TLS ALPS extension.
688690// At the time of this writing, this extension is currently a draft:
689691// https://datatracker.ietf.org/doc/html/draft-vvv-tls-alps-01
690- type ApplicationSettingsExtension struct {
691- SupportedProtocols [] string
692+ type applicationSettingsExtension struct {
693+ codePoint uint16
692694}
693695
694- func (e * ApplicationSettingsExtension ) writeToUConn (uc * UConn ) error {
696+ func (e * applicationSettingsExtension ) writeToUConn (uc * UConn ) error {
695697 return nil
696698}
697699
698- func (e * ApplicationSettingsExtension ) Len () int {
700+ func (e * applicationSettingsExtension ) Len (supportedProtocols [] string ) int {
699701 bLen := 2 + 2 + 2 // Type + Length + ALPS Extension length
700- for _ , s := range e . SupportedProtocols {
702+ for _ , s := range supportedProtocols {
701703 bLen += 1 + len (s ) // Supported ALPN Length + actual length of protocol
702704 }
703705 return bLen
704706}
705707
706- func (e * ApplicationSettingsExtension ) Read (b []byte ) (int , error ) {
707- if len (b ) < e .Len () {
708+ func (e * applicationSettingsExtension ) Read (b []byte , supportedProtocols [] string ) (int , error ) {
709+ if len (b ) < e .Len (supportedProtocols ) {
708710 return 0 , io .ErrShortBuffer
709711 }
710712
711713 // Read Type.
712- b [0 ] = byte (utlsExtensionApplicationSettings >> 8 ) // hex: 44 dec: 68
713- b [1 ] = byte (utlsExtensionApplicationSettings & 0xff ) // hex: 69 dec: 105
714+ b [0 ] = byte (e . codePoint >> 8 ) // hex: 44 dec: 68
715+ b [1 ] = byte (e . codePoint & 0xff ) // hex: 69 dec: 105
714716
715717 lengths := b [2 :] // get the remaining buffer without Type
716718 b = b [6 :] // set the buffer to the buffer without Type, Length and ALPS Extension Length (so only the Supported ALPN list remains)
717719
718720 stringsLength := 0
719- for _ , s := range e . SupportedProtocols {
721+ for _ , s := range supportedProtocols {
720722 l := len (s ) // Supported ALPN Length
721723 b [0 ] = byte (l ) // Supported ALPN Length in bytes hex: 02 dec: 2
722724 copy (b [1 :], s ) // copy the Supported ALPN as bytes to the buffer
@@ -730,92 +732,87 @@ func (e *ApplicationSettingsExtension) Read(b []byte) (int, error) {
730732 lengths [0 ] = byte (stringsLength >> 8 ) // Length hex:00 dec: 0
731733 lengths [1 ] = byte (stringsLength ) // Length hex: 05 dec: 5
732734
733- return e .Len (), io .EOF
734- }
735-
736- func (e * ApplicationSettingsExtension ) UnmarshalJSON (b []byte ) error {
737- var applicationSettingsSupport struct {
738- SupportedProtocols []string `json:"supported_protocols"`
739- }
740-
741- if err := json .Unmarshal (b , & applicationSettingsSupport ); err != nil {
742- return err
743- }
744-
745- e .SupportedProtocols = applicationSettingsSupport .SupportedProtocols
746- return nil
735+ return e .Len (supportedProtocols ), io .EOF
747736}
748737
749738// Write implementation copied from ALPNExtension.Write
750- func (e * ApplicationSettingsExtension ) Write (b []byte ) (int , error ) {
739+ func (e * applicationSettingsExtension ) Write (b []byte ) ([] string , int , error ) {
751740 fullLen := len (b )
752741 extData := cryptobyte .String (b )
753742 // https://datatracker.ietf.org/doc/html/draft-vvv-tls-alps-01
754743 var protoList cryptobyte.String
755744 if ! extData .ReadUint16LengthPrefixed (& protoList ) || protoList .Empty () {
756- return 0 , errors .New ("unable to read ALPN extension data" )
745+ return nil , 0 , errors .New ("unable to read ALPN extension data" )
757746 }
758747 alpnProtocols := []string {}
759748 for ! protoList .Empty () {
760749 var proto cryptobyte.String
761750 if ! protoList .ReadUint8LengthPrefixed (& proto ) || proto .Empty () {
762- return 0 , errors .New ("unable to read ALPN extension data" )
751+ return nil , 0 , errors .New ("unable to read ALPN extension data" )
763752 }
764753 alpnProtocols = append (alpnProtocols , string (proto ))
765754
766755 }
767- e .SupportedProtocols = alpnProtocols
768- return fullLen , nil
756+ return alpnProtocols , fullLen , nil
769757}
770758
771- // ApplicationSettingsExtensionNew represents the TLS ALPS codepoint extension introduced by Chrome 133.
772- // More information can be found here: https://chromestatus.com/feature/5149147365900288
773- // TODO: This probably should be implemented differently
774- type ApplicationSettingsExtensionNew struct {
775- * ApplicationSettingsExtension
759+ // ApplicationSettingsExtension embeds applicationSettingsExtension to implement the TLS ALPS extension on codepoint 17513
760+ type ApplicationSettingsExtension struct {
761+ applicationSettingsExtension
762+ SupportedProtocols []string
776763}
777764
778- func (e * ApplicationSettingsExtensionNew ) Len () int {
779- bLen := 2 + 2 + 2 // Type + Length + ALPS Extension length
780- for _ , s := range e .SupportedProtocols {
781- bLen += 1 + len (s ) // Supported ALPN Length + actual length of protocol
782- }
783- return bLen
765+ func (e * ApplicationSettingsExtension ) Len () int {
766+ return e .applicationSettingsExtension .Len (e .SupportedProtocols )
784767}
785768
786- func (e * ApplicationSettingsExtensionNew ) Read (b []byte ) (int , error ) {
787- if len (b ) < e .Len () {
788- return 0 , io .ErrShortBuffer
769+ func (e * ApplicationSettingsExtension ) Read (b []byte ) (int , error ) {
770+ e .applicationSettingsExtension .codePoint = utlsExtensionApplicationSettings
771+ return e .applicationSettingsExtension .Read (b , e .SupportedProtocols )
772+ }
773+
774+ func (e * ApplicationSettingsExtension ) UnmarshalJSON (b []byte ) error {
775+ var applicationSettingsSupport struct {
776+ SupportedProtocols []string `json:"supported_protocols"`
789777 }
790778
791- // Read Type.
792- b [ 0 ] = byte ( 17613 >> 8 ) // hex: 44 dec: 68
793- b [ 1 ] = byte ( 17613 & 0xff ) // hex: 69 dec: 105
779+ if err := json . Unmarshal ( b , & applicationSettingsSupport ); err != nil {
780+ return err
781+ }
794782
795- lengths := b [2 :] // get the remaining buffer without Type
796- b = b [6 :] // set the buffer to the buffer without Type, Length and ALPS Extension Length (so only the Supported ALPN list remains)
783+ e .SupportedProtocols = applicationSettingsSupport .SupportedProtocols
784+ return nil
785+ }
797786
798- stringsLength := 0
799- for _ , s := range e .SupportedProtocols {
800- l := len (s ) // Supported ALPN Length
801- b [0 ] = byte (l ) // Supported ALPN Length in bytes hex: 02 dec: 2
802- copy (b [1 :], s ) // copy the Supported ALPN as bytes to the buffer
803- b = b [1 + l :] // set the buffer to the buffer without the Supported ALPN Length and Supported ALPN (so we can continue to the next protocol in this loop)
804- stringsLength += 1 + l // Supported ALPN Length (the field itself) + Supported ALPN Length (the value)
805- }
787+ // Write implementation copied from ALPNExtension.Write
788+ func (e * ApplicationSettingsExtension ) Write (b []byte ) (int , error ) {
789+ var (
790+ fullLen int
791+ err error
792+ )
793+ e .SupportedProtocols , fullLen , err = e .applicationSettingsExtension .Write (b )
794+ return fullLen , err
795+ }
806796
807- lengths [2 ] = byte (stringsLength >> 8 ) // ALPS Extension Length hex: 00 dec: 0
808- lengths [3 ] = byte (stringsLength ) // ALPS Extension Length hex: 03 dec: 3
809- stringsLength += 2 // plus ALPS Extension Length field length
810- lengths [0 ] = byte (stringsLength >> 8 ) // Length hex:00 dec: 0
811- lengths [1 ] = byte (stringsLength ) // Length hex: 05 dec: 5
797+ // ApplicationSettingsExtensionNew embeds applicationSettingsExtension to implement the TLS ALPS extension on codepoint 17613
798+ // More information can be found here: https://chromestatus.com/feature/5149147365900288
799+ type ApplicationSettingsExtensionNew struct {
800+ applicationSettingsExtension
801+ SupportedProtocols []string
802+ }
812803
813- return e .Len (), io .EOF
804+ func (e * ApplicationSettingsExtensionNew ) Len () int {
805+ return e .applicationSettingsExtension .Len (e .SupportedProtocols )
806+ }
807+
808+ func (e * ApplicationSettingsExtensionNew ) Read (b []byte ) (int , error ) {
809+ e .applicationSettingsExtension .codePoint = utlsExtensionApplicationSettingsNew
810+ return e .applicationSettingsExtension .Read (b , e .SupportedProtocols )
814811}
815812
816813func (e * ApplicationSettingsExtensionNew ) UnmarshalJSON (b []byte ) error {
817814 var applicationSettingsSupport struct {
818- SupportedProtocols []string `json:"supported_protocols_new "`
815+ SupportedProtocols []string `json:"supported_protocols "`
819816 }
820817
821818 if err := json .Unmarshal (b , & applicationSettingsSupport ); err != nil {
@@ -828,24 +825,12 @@ func (e *ApplicationSettingsExtensionNew) UnmarshalJSON(b []byte) error {
828825
829826// Write implementation copied from ALPNExtension.Write
830827func (e * ApplicationSettingsExtensionNew ) Write (b []byte ) (int , error ) {
831- fullLen := len (b )
832- extData := cryptobyte .String (b )
833- // https://datatracker.ietf.org/doc/html/draft-vvv-tls-alps-01
834- var protoList cryptobyte.String
835- if ! extData .ReadUint16LengthPrefixed (& protoList ) || protoList .Empty () {
836- return 0 , errors .New ("unable to read ALPN extension data" )
837- }
838- alpnProtocols := []string {}
839- for ! protoList .Empty () {
840- var proto cryptobyte.String
841- if ! protoList .ReadUint8LengthPrefixed (& proto ) || proto .Empty () {
842- return 0 , errors .New ("unable to read ALPN extension data" )
843- }
844- alpnProtocols = append (alpnProtocols , string (proto ))
845-
846- }
847- e .SupportedProtocols = alpnProtocols
848- return fullLen , nil
828+ var (
829+ fullLen int
830+ err error
831+ )
832+ e .SupportedProtocols , fullLen , err = e .applicationSettingsExtension .Write (b )
833+ return fullLen , err
849834}
850835
851836// SCTExtension implements signed_certificate_timestamp (18)
0 commit comments