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_handshake_context.go
178 lines (164 loc) · 5.64 KB
/
server_handshake_context.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package dtls
import (
"bytes"
"errors"
"fmt"
"log"
)
type serverHandshake struct {
baseHandshakeContext
}
func (sh *serverHandshake) beginHandshake() {
sh.currentFlight = 1
}
func (sh *serverHandshake) continueHandshake(message *handshake) (complete bool, err error) {
sh.receiveMessage(message)
if sh.currentFlight == 1 && sh.isFlightOneComplete() {
err := sh.sendFlightTwo()
if err == nil {
sh.currentFlight = 3
} else {
log.Printf("Error while sending flight two: %s", err)
}
return false, err
}
if sh.currentFlight == 3 {
log.Printf("We're in flight 3, state is\n%+v", sh.baseHandshakeContext)
if sh.clientKeyExchange != nil && sh.masterSecret == nil {
log.Printf("Handling client key exchange")
err := sh.handleKeyExchange()
if err != nil {
log.Printf("Error while handling client key exchange")
}
return false, err
}
complete, err := sh.isFlightThreeComplete()
if complete && err == nil {
sh.sendFlightFour()
}
return complete, err
}
return false, nil
}
func (sh *serverHandshake) isFlightOneComplete() bool {
return sh.clientHello != nil
}
func (sh *serverHandshake) prepareFlightTwo() error {
clientHello, err := readHandshakeClientHello(sh.clientHello.Fragment)
if err != nil {
return errors.New(fmt.Sprintf("Failed to read client hello: %s", err))
}
cipherSuite := findCommonCipherSuite(clientHello.CipherSuites, cipherSuites)
if cipherSuite == nil {
return errors.New("Client does not support any cipher suites we support")
}
sh.cipherSuite = *cipherSuite
sh.keyAgreement = cipherSuite.KeyAgreement()
compressionMethod, ok := findCommonCompressionMethod(clientHello.CompressionMethods)
if !ok {
return errors.New("Client does not support any compression methods we support")
}
sh.clientRandom = clientHello.Random
sh.serverRandom = newRandom()
srvHello := handshakeServerHello{
ServerVersion: sh.Conn.version,
Random: sh.serverRandom,
CipherSuite: cipherSuite,
CompressionMethod: compressionMethod,
}
sh.serverHello = sh.buildNextHandshakeMessage(serverHello, srvHello.Bytes())
srvKeyExchange, err := sh.keyAgreement.generateServerKeyExchange()
if err != nil {
return err
}
sh.serverKeyExchange = sh.buildNextHandshakeMessage(serverKeyExchange, srvKeyExchange)
sh.serverHelloDone = sh.buildNextHandshakeMessage(serverHelloDone, []byte{})
return nil
}
func (sh *serverHandshake) sendFlightTwo() error {
if err := sh.prepareFlightTwo(); err != nil {
return err
}
sh.sendHandshakeMessage(sh.serverHello)
sh.sendHandshakeMessage(sh.serverKeyExchange)
sh.sendHandshakeMessage(sh.serverHelloDone)
return nil
}
func findCommonCipherSuite(client, server []*cipherSuite) *cipherSuite {
log.Printf("Client supports ciphersuites: %+v", client)
log.Printf("We support: %+v", server)
for _, suiteA := range client {
for _, suiteB := range server {
if suiteA.id == suiteB.id {
return suiteA
}
}
}
return nil
}
func findCommonCompressionMethod(methods []compressionMethod) (compressionMethod, bool) {
for _, method := range methods {
if method == compressionNone {
return compressionNone, true
}
}
return 255, false
}
func (sh *serverHandshake) handleKeyExchange() error {
clientKeyExchange, err := readClientKeyExchange(sh.clientKeyExchange.Fragment)
if err != nil {
log.Printf("Error while reading client key exchange: %v", err)
return err
}
preMasterSecret, err := sh.keyAgreement.processClientKeyExchange(clientKeyExchange)
log.Printf("Premaster secret is %x", preMasterSecret)
if err != nil {
log.Printf("Error while processing client key exchange: %v", err)
return err
}
masterSecret, clientMAC, serverMAC, clientKey, serverKey :=
keysFromPreMasterSecret(sh.Conn.version, preMasterSecret, sh.clientRandom.Bytes(), sh.serverRandom.Bytes(),
sh.cipherSuite.macLen, sh.cipherSuite.keyLen)
sh.masterSecret = masterSecret
sh.Conn.pendingWriteState.Cipher = sh.cipherSuite.cipher(serverKey)
sh.Conn.pendingWriteState.Mac = sh.cipherSuite.mac(serverMAC)
sh.Conn.pendingReadState.Cipher = sh.cipherSuite.cipher(clientKey)
sh.Conn.pendingReadState.Mac = sh.cipherSuite.mac(clientMAC)
logMasterSecret(sh.clientRandom.Bytes(), masterSecret)
return nil
}
func (sh *serverHandshake) isFlightThreeComplete() (complete bool, err error) {
if sh.clientFinished == nil {
return false, nil
}
clientFinished, err := readHandshakeFinished(sh.clientFinished.Fragment)
if err != nil {
return true, err
}
sh.finishedHash = newFinishedHash()
sh.finishedHash.Write(sh.clientHello.Bytes())
sh.finishedHash.Write(sh.serverHello.Bytes())
sh.finishedHash.Write(sh.serverKeyExchange.Bytes())
sh.finishedHash.Write(sh.serverHelloDone.Bytes())
sh.finishedHash.Write(sh.clientKeyExchange.Bytes())
if sh.Conn.version == DTLS_10 && !bytes.Equal(clientFinished.VerifyData, sh.finishedHash.clientSum10(sh.masterSecret)) {
err = errors.New("Client sent incorrect verify data")
} else if sh.Conn.version == DTLS_12 && !bytes.Equal(clientFinished.VerifyData, sh.finishedHash.clientSum12(sh.masterSecret)) {
err = errors.New("Client sent incorrect verify data")
}
return true, err
}
func (sh *serverHandshake) prepareFlightFour() {
sh.finishedHash.Write(sh.clientFinished.Bytes())
if sh.Conn.version == DTLS_10 {
serverFinished := &handshakeFinished{VerifyData: sh.finishedHash.serverSum10(sh.masterSecret)}
} else if sh.Conn.version == DTLS_12 {
serverFinished := &handshakeFinished{VerifyData: sh.finishedHash.serverSum12(sh.masterSecret)}
}
sh.serverFinished = sh.buildNextHandshakeMessage(finished, serverFinished.Bytes())
}
func (sh *serverHandshake) sendFlightFour() {
sh.prepareFlightFour()
sh.Conn.sendChangeCipherSpec()
sh.sendHandshakeMessage(sh.serverFinished)
}