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
/
server_hello.go
72 lines (67 loc) · 1.96 KB
/
server_hello.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
package dtls
import (
"bytes"
"errors"
"fmt"
)
type handshakeServerHello struct {
ServerVersion protocolVersion
Random random
SessionID []byte
CipherSuite *cipherSuite
CompressionMethod compressionMethod
Extensions []extension
}
func (sh handshakeServerHello) String() string {
return fmt.Sprintf("ServerHello{ ServerVersion: %s, Random: %s, SessionID: %x, CipherSuite: %s, CompressionMethod: %s }", sh.ServerVersion, sh.Random, sh.SessionID, sh.CipherSuite, sh.CompressionMethod)
}
func (sh handshakeServerHello) Bytes() []byte {
buffer := &bytes.Buffer{}
buffer.Write(sh.ServerVersion.Bytes())
buffer.Write(sh.Random.Bytes())
buffer.WriteByte(byte(len(sh.SessionID)))
buffer.Write(sh.SessionID)
buffer.Write(sh.CipherSuite.Bytes())
buffer.Write(sh.CompressionMethod.Bytes())
for _, extension := range sh.Extensions {
buffer.Write(extension.Bytes())
}
return buffer.Bytes()
}
func readHandshakeServerHello(byts []byte) (hsh handshakeServerHello, err error) {
buffer := bytes.NewBuffer(byts)
if buffer.Len() < 35 {
return hsh, errors.New("Buffer does not contain all bytes of server hello")
}
if hsh.ServerVersion, err = readProtocolVersion(buffer); err != nil {
return
}
if hsh.Random, err = readRandom(buffer); err != nil {
return
}
sessionIdLength, err := buffer.ReadByte()
if err != nil {
return
}
if buffer.Len() < int(sessionIdLength) {
return hsh, InvalidHandshakeError
}
hsh.SessionID = buffer.Next(int(sessionIdLength))
if hsh.CipherSuite, err = readCipherSuite(buffer); err != nil {
return
}
if hsh.CompressionMethod, err = readCompressionMethod(buffer); err != nil {
return
}
if buffer.Len() > 2 {
// TODO: does this field exist if there are no extensions?
numExtensions := readUint16(buffer)
hsh.Extensions = make([]extension, int(numExtensions))
for i := 0; i < int(numExtensions); i++ {
if hsh.Extensions[i], err = readExtension(buffer); err != nil {
return
}
}
}
return
}