This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handshake.go
144 lines (132 loc) · 3.38 KB
/
handshake.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package dtls
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
)
type handshakeType byte
const (
helloRequest handshakeType = 0
clientHello = 1
serverHello = 2
helloVerifyRequest = 3
certificate = 11
serverKeyExchange = 12
certificateRequest = 13
serverHelloDone = 14
certificateVerify = 15
clientKeyExchange = 16
finished = 20
)
func (ht handshakeType) Bytes() []byte {
return []byte{byte(ht)}
}
func (ht handshakeType) String() string {
switch ht {
case helloRequest:
return "HelloRequest"
case clientHello:
return "ClientHello"
case serverHello:
return "ServerHello"
case helloVerifyRequest:
return "HelloVerifyRequest"
case certificate:
return "Certificate"
case serverKeyExchange:
return "ServerKeyExchange"
case certificateRequest:
return "CertificateRequest"
case serverHelloDone:
return "ServerHelloDone"
case certificateVerify:
return "CertificateVerify"
case clientKeyExchange:
return "ClientKeyExchange"
case finished:
return "Finished"
default:
return "xxx"
}
}
var InvalidHandshakeType = errors.New("Invalid handshake type")
func readHandshakeType(buffer *bytes.Buffer) (handshakeType, error) {
b, err := buffer.ReadByte()
if err != nil {
return 255, err
}
switch b {
case 0:
return helloRequest, nil
case 1:
return clientHello, nil
case 2:
return serverHello, nil
case 3:
return helloVerifyRequest, nil
case 11:
return certificate, nil
case 12:
return serverKeyExchange, nil
case 13:
return certificateRequest, nil
case 14:
return serverHelloDone, nil
case 15:
return certificateVerify, nil
case 16:
return clientKeyExchange, nil
case 20:
return finished, nil
default:
return 0, InvalidHandshakeType
}
}
type handshake struct {
MsgType handshakeType
Length uint32
MessageSeq uint16
FragmentOffset uint32
FragmentLength uint32
Fragment []byte
}
var InvalidHandshakeError = errors.New("Invalid handshake")
func readHandshake(buffer *bytes.Buffer) (h handshake, err error) {
if buffer.Len() < 12 {
return h, errors.New("Buffer does not contain enough bytes to read handshake header")
}
if h.MsgType, err = readHandshakeType(buffer); err != nil {
return
}
h.Length = readUint24(buffer)
h.MessageSeq = readUint16(buffer)
h.FragmentOffset = readUint24(buffer)
h.FragmentLength = readUint24(buffer)
if buffer.Len() < int(h.FragmentLength) {
return h, errors.New("Buffer does not contain all bytes of fragment")
}
h.Fragment = buffer.Next(int(h.FragmentLength))
return
}
func (h handshake) Bytes() []byte {
buffer := bytes.Buffer{}
buffer.Write(h.MsgType.Bytes())
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, h.Length)
buffer.Write(b[1:])
b = make([]byte, 2)
binary.BigEndian.PutUint16(b, h.MessageSeq)
buffer.Write(b)
b = make([]byte, 4)
binary.BigEndian.PutUint32(b, h.FragmentOffset)
buffer.Write(b[1:])
b = make([]byte, 4)
binary.BigEndian.PutUint32(b, h.FragmentLength)
buffer.Write(b[1:])
buffer.Write(h.Fragment)
return buffer.Bytes()
}
func (h handshake) String() string {
return fmt.Sprintf("Handshake{ Type: %s, Length: %d, MessageSeq: %d, FragmentOffset: %d, FragmentLength: %d, Fragment: %x }", h.MsgType, h.Length, h.MessageSeq, h.FragmentOffset, h.FragmentLength, h.Fragment)
}