-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandshake_message_client_key_exchange.go
46 lines (38 loc) · 1.19 KB
/
handshake_message_client_key_exchange.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dtls
import (
"encoding/binary"
)
type handshakeMessageClientKeyExchange struct {
identityHint []byte
publicKey []byte
}
func (h handshakeMessageClientKeyExchange) handshakeType() handshakeType {
return handshakeTypeClientKeyExchange
}
func (h *handshakeMessageClientKeyExchange) Marshal() ([]byte, error) {
switch {
case (h.identityHint != nil && h.publicKey != nil) || (h.identityHint == nil && h.publicKey == nil):
return nil, errInvalidClientKeyExchange
case h.publicKey != nil:
return append([]byte{byte(len(h.publicKey))}, h.publicKey...), nil
default:
out := append([]byte{0x00, 0x00}, h.identityHint...)
binary.BigEndian.PutUint16(out, uint16(len(out)-2))
return out, nil
}
}
func (h *handshakeMessageClientKeyExchange) Unmarshal(data []byte) error {
if len(data) < 2 {
return errBufferTooSmall
}
// If parsed as PSK return early and only populate PSK Identity Hint
if pskLength := binary.BigEndian.Uint16(data); len(data) == int(pskLength+2) {
h.identityHint = append([]byte{}, data[2:]...)
return nil
}
if publicKeyLength := int(data[0]); len(data) != publicKeyLength+1 {
return errBufferTooSmall
}
h.publicKey = append([]byte{}, data[1:]...)
return nil
}