github.com/pion/dtls/v2@v2.2.12/pkg/protocol/extension/supported_point_formats.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package extension 5 6 import ( 7 "encoding/binary" 8 9 "github.com/pion/dtls/v2/pkg/crypto/elliptic" 10 ) 11 12 const ( 13 supportedPointFormatsSize = 5 14 ) 15 16 // SupportedPointFormats allows a Client/Server to negotiate 17 // the EllipticCurvePointFormats 18 // 19 // https://tools.ietf.org/html/rfc4492#section-5.1.2 20 type SupportedPointFormats struct { 21 PointFormats []elliptic.CurvePointFormat 22 } 23 24 // TypeValue returns the extension TypeValue 25 func (s SupportedPointFormats) TypeValue() TypeValue { 26 return SupportedPointFormatsTypeValue 27 } 28 29 // Marshal encodes the extension 30 func (s *SupportedPointFormats) Marshal() ([]byte, error) { 31 out := make([]byte, supportedPointFormatsSize) 32 33 binary.BigEndian.PutUint16(out, uint16(s.TypeValue())) 34 binary.BigEndian.PutUint16(out[2:], uint16(1+(len(s.PointFormats)))) 35 out[4] = byte(len(s.PointFormats)) 36 37 for _, v := range s.PointFormats { 38 out = append(out, byte(v)) 39 } 40 return out, nil 41 } 42 43 // Unmarshal populates the extension from encoded data 44 func (s *SupportedPointFormats) Unmarshal(data []byte) error { 45 if len(data) <= supportedPointFormatsSize { 46 return errBufferTooSmall 47 } else if TypeValue(binary.BigEndian.Uint16(data)) != s.TypeValue() { 48 return errInvalidExtensionType 49 } 50 51 pointFormatCount := int(binary.BigEndian.Uint16(data[4:])) 52 if supportedGroupsHeaderSize+(pointFormatCount) > len(data) { 53 return errLengthMismatch 54 } 55 56 for i := 0; i < pointFormatCount; i++ { 57 p := elliptic.CurvePointFormat(data[supportedPointFormatsSize+i]) 58 switch p { 59 case elliptic.CurvePointFormatUncompressed: 60 s.PointFormats = append(s.PointFormats, p) 61 default: 62 } 63 } 64 return nil 65 }