github.com/pion/dtls/v2@v2.2.12/pkg/protocol/extension/use_master_secret.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package extension 5 6 import "encoding/binary" 7 8 const ( 9 useExtendedMasterSecretHeaderSize = 4 10 ) 11 12 // UseExtendedMasterSecret defines a TLS extension that contextually binds the 13 // master secret to a log of the full handshake that computes it, thus 14 // preventing MITM attacks. 15 type UseExtendedMasterSecret struct { 16 Supported bool 17 } 18 19 // TypeValue returns the extension TypeValue 20 func (u UseExtendedMasterSecret) TypeValue() TypeValue { 21 return UseExtendedMasterSecretTypeValue 22 } 23 24 // Marshal encodes the extension 25 func (u *UseExtendedMasterSecret) Marshal() ([]byte, error) { 26 if !u.Supported { 27 return []byte{}, nil 28 } 29 30 out := make([]byte, useExtendedMasterSecretHeaderSize) 31 32 binary.BigEndian.PutUint16(out, uint16(u.TypeValue())) 33 binary.BigEndian.PutUint16(out[2:], uint16(0)) // length 34 return out, nil 35 } 36 37 // Unmarshal populates the extension from encoded data 38 func (u *UseExtendedMasterSecret) Unmarshal(data []byte) error { 39 if len(data) < useExtendedMasterSecretHeaderSize { 40 return errBufferTooSmall 41 } else if TypeValue(binary.BigEndian.Uint16(data)) != u.TypeValue() { 42 return errInvalidExtensionType 43 } 44 45 u.Supported = true 46 47 return nil 48 }