package handshake
import (
"encoding/binary"
"github.com/pion/dtls/v3/internal/ciphersuite/types"
)
type MessageClientKeyExchange struct {
IdentityHint []byte
PublicKey []byte
KeyExchangeAlgorithm types .KeyExchangeAlgorithm
}
func (m MessageClientKeyExchange ) Type () Type {
return TypeClientKeyExchange
}
func (m *MessageClientKeyExchange ) Marshal () (out []byte , err error ) {
if m .IdentityHint == nil && m .PublicKey == nil {
return nil , errInvalidClientKeyExchange
}
if m .IdentityHint != nil {
out = append ([]byte {0x00 , 0x00 }, m .IdentityHint ...)
binary .BigEndian .PutUint16 (out , uint16 (len (out )-2 ))
}
if m .PublicKey != nil {
out = append (out , byte (len (m .PublicKey )))
out = append (out , m .PublicKey ...)
}
return out , nil
}
func (m *MessageClientKeyExchange ) Unmarshal (data []byte ) error {
switch {
case len (data ) < 2 :
return errBufferTooSmall
case m .KeyExchangeAlgorithm == types .KeyExchangeAlgorithmNone :
return errCipherSuiteUnset
}
offset := 0
if m .KeyExchangeAlgorithm .Has (types .KeyExchangeAlgorithmPsk ) {
pskLength := int (binary .BigEndian .Uint16 (data ))
if pskLength > len (data )-2 {
return errBufferTooSmall
}
m .IdentityHint = append ([]byte {}, data [2 :pskLength +2 ]...)
offset += pskLength + 2
}
if m .KeyExchangeAlgorithm .Has (types .KeyExchangeAlgorithmEcdhe ) {
publicKeyLength := int (data [offset ])
if publicKeyLength > len (data )-1 -offset {
return errBufferTooSmall
}
m .PublicKey = append ([]byte {}, data [offset +1 :]...)
}
return nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .