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_key_exchange.go
65 lines (56 loc) · 1.55 KB
/
server_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dtls
import (
"bytes"
"encoding/binary"
"fmt"
)
type serverDHParams struct {
G []byte
P []byte
PublicKey []byte
}
func readServerDHParams(buffer *bytes.Buffer) (sdhp serverDHParams, err error) {
pLength := readUint16(buffer)
sdhp.P = buffer.Next(int(pLength))
gLength := readUint16(buffer)
sdhp.G = buffer.Next(int(gLength))
pubKeyLength := readUint16(buffer)
sdhp.PublicKey = buffer.Next(int(pubKeyLength))
return
}
func (sdhp serverDHParams) String() string {
return fmt.Sprintf("ServerDHParams{ G: %x, P: %x, PublicKey: %x }", sdhp.G, sdhp.P, sdhp.PublicKey)
}
func (sdhp serverDHParams) Bytes() []byte {
buffer := &bytes.Buffer{}
t := make([]byte, 2)
binary.BigEndian.PutUint16(t, uint16(len(sdhp.P)))
buffer.Write(t)
buffer.Write(sdhp.P)
t = make([]byte, 2)
binary.BigEndian.PutUint16(t, uint16(len(sdhp.G)))
buffer.Write(t)
buffer.Write(sdhp.G)
t = make([]byte, 2)
binary.BigEndian.PutUint16(t, uint16(len(sdhp.PublicKey)))
buffer.Write(t)
buffer.Write(sdhp.PublicKey)
return buffer.Bytes()
}
type handshakeServerKeyExchange struct {
Params serverDHParams
}
func readHandshakeServerKeyExchange(byts []byte) (ske handshakeServerKeyExchange, err error) {
buffer := bytes.NewBuffer(byts)
ske.Params, err = readServerDHParams(buffer)
if err != nil {
panic("Unparsable server key exchange message")
}
return
}
func (ske handshakeServerKeyExchange) String() string {
return fmt.Sprintf("ServerKeyExchange{ Params: %s }", ske.Params)
}
func (ske handshakeServerKeyExchange) Bytes() []byte {
return ske.Params.Bytes()
}