Debian Codegraph @

state-machine.go

raw symbols SCIP
1 package mint
2
3 import (
4 "crypto/x509"
5 "time"
6 )
7
8 // Marker interface for actions that an implementation should take based on
9 // state transitions.
10 type HandshakeAction interface{}
11
12 type QueueHandshakeMessage struct {
13 Message *HandshakeMessage
14 }
15
16 type SendQueuedHandshake struct{}
17
18 type SendEarlyData struct{}
19
20 type RekeyIn struct {
21 epoch Epoch
22 KeySet KeySet
23 }
24
25 type RekeyOut struct {
26 epoch Epoch
27 KeySet KeySet
28 }
29
30 type ResetOut struct {
31 seq uint64
32 }
33
34 type StorePSK struct {
35 PSK PreSharedKey
36 }
37
38 type HandshakeState interface {
39 Next(handshakeMessageReader) (HandshakeState, []HandshakeAction, Alert)
40 State() State
41 }
42
43 type AppExtensionHandler interface {
44 Send(hs HandshakeType, el *ExtensionList) error
45 Receive(hs HandshakeType, el *ExtensionList) error
46 }
47
48 // ConnectionOptions objects represent per-connection settings for a client
49 // initiating a connection
50 type ConnectionOptions struct {
51 ServerName string
52 NextProtos []string
53 }
54
55 // ConnectionParameters objects represent the parameters negotiated for a
56 // connection.
57 type ConnectionParameters struct {
58 UsingPSK bool
59 UsingDH bool
60 ClientSendingEarlyData bool
61 UsingEarlyData bool
62 RejectedEarlyData bool
63 UsingClientAuth bool
64
65 CipherSuite CipherSuite
66 ServerName string
67 NextProto string
68 }
69
70 // Working state for the handshake.
71 type HandshakeContext struct {
72 timeoutMS uint32
73 timers *timerSet
74 recvdRecords []uint64
75 sentFragments []*SentHandshakeFragment
76 hIn, hOut *HandshakeLayer
77 waitingNextFlight bool
78 earlyData []byte
79 }
80
81 func (hc *HandshakeContext) SetVersion(version uint16) {
82 if hc.hIn.conn != nil {
83 hc.hIn.conn.SetVersion(version)
84 }
85 if hc.hOut.conn != nil {
86 hc.hOut.conn.SetVersion(version)
87 }
88 }
89
90 // stateConnected is symmetric between client and server
91 type stateConnected struct {
92 Params ConnectionParameters
93 hsCtx *HandshakeContext
94 isClient bool
95 cryptoParams CipherSuiteParams
96 resumptionSecret []byte
97 clientTrafficSecret []byte
98 serverTrafficSecret []byte
99 exporterSecret []byte
100 peerCertificates []*x509.Certificate
101 verifiedChains [][]*x509.Certificate
102 }
103
104 var _ HandshakeState = &stateConnected{}
105
106 func (state stateConnected) State() State {
107 if state.isClient {
108 return StateClientConnected
109 }
110 return StateServerConnected
111 }
112
113 func (state *stateConnected) KeyUpdate(request KeyUpdateRequest) ([]HandshakeAction, Alert) {
114 var trafficKeys KeySet
115 if state.isClient {
116 state.clientTrafficSecret = HkdfExpandLabel(state.cryptoParams.Hash, state.clientTrafficSecret,
117 labelClientApplicationTrafficSecret, []byte{}, state.cryptoParams.Hash.Size())
118 trafficKeys = makeTrafficKeys(state.cryptoParams, state.clientTrafficSecret)
119 } else {
120 state.serverTrafficSecret = HkdfExpandLabel(state.cryptoParams.Hash, state.serverTrafficSecret,
121 labelServerApplicationTrafficSecret, []byte{}, state.cryptoParams.Hash.Size())
122 trafficKeys = makeTrafficKeys(state.cryptoParams, state.serverTrafficSecret)
123 }
124
125 kum, err := state.hsCtx.hOut.HandshakeMessageFromBody(&KeyUpdateBody{KeyUpdateRequest: request})
126 if err != nil {
127 logf(logTypeHandshake, "[StateConnected] Error marshaling key update message: %v", err)
128 return nil, AlertInternalError
129 }
130
131 toSend := []HandshakeAction{
132 QueueHandshakeMessage{kum},
133 SendQueuedHandshake{},
134 RekeyOut{epoch: EpochUpdate, KeySet: trafficKeys},
135 }
136 return toSend, AlertNoAlert
137 }
138
139 func (state *stateConnected) NewSessionTicket(length int, lifetime, earlyDataLifetime uint32) ([]HandshakeAction, Alert) {
140 tkt, err := NewSessionTicket(length, lifetime)
141 if err != nil {
142 logf(logTypeHandshake, "[StateConnected] Error generating NewSessionTicket: %v", err)
143 return nil, AlertInternalError
144 }
145
146 err = tkt.Extensions.Add(&TicketEarlyDataInfoExtension{earlyDataLifetime})
147 if err != nil {
148 logf(logTypeHandshake, "[StateConnected] Error adding extension to NewSessionTicket: %v", err)
149 return nil, AlertInternalError
150 }
151
152 resumptionKey := HkdfExpandLabel(state.cryptoParams.Hash, state.resumptionSecret,
153 labelResumption, tkt.TicketNonce, state.cryptoParams.Hash.Size())
154
155 newPSK := PreSharedKey{
156 CipherSuite: state.cryptoParams.Suite,
157 IsResumption: true,
158 Identity: tkt.Ticket,
159 Key: resumptionKey,
160 NextProto: state.Params.NextProto,
161 ReceivedAt: time.Now(),
162 ExpiresAt: time.Now().Add(time.Duration(tkt.TicketLifetime) * time.Second),
163 TicketAgeAdd: tkt.TicketAgeAdd,
164 }
165
166 tktm, err := state.hsCtx.hOut.HandshakeMessageFromBody(tkt)
167 if err != nil {
168 logf(logTypeHandshake, "[StateConnected] Error marshaling NewSessionTicket: %v", err)
169 return nil, AlertInternalError
170 }
171
172 toSend := []HandshakeAction{
173 StorePSK{newPSK},
174 QueueHandshakeMessage{tktm},
175 SendQueuedHandshake{},
176 }
177 return toSend, AlertNoAlert
178 }
179
180 // Next does nothing for this state.
181 func (state stateConnected) Next(hr handshakeMessageReader) (HandshakeState, []HandshakeAction, Alert) {
182 return state, nil, AlertNoAlert
183 }
184
185 func (state stateConnected) ProcessMessage(hm *HandshakeMessage) (HandshakeState, []HandshakeAction, Alert) {
186 if hm == nil {
187 logf(logTypeHandshake, "[StateConnected] Unexpected message")
188 return nil, nil, AlertUnexpectedMessage
189 }
190
191 bodyGeneric, err := hm.ToBody()
192 if err != nil {
193 logf(logTypeHandshake, "[StateConnected] Error decoding message: %v", err)
194 return nil, nil, AlertDecodeError
195 }
196
197 switch body := bodyGeneric.(type) {
198 case *KeyUpdateBody:
199 var trafficKeys KeySet
200 if !state.isClient {
201 state.clientTrafficSecret = HkdfExpandLabel(state.cryptoParams.Hash, state.clientTrafficSecret,
202 labelClientApplicationTrafficSecret, []byte{}, state.cryptoParams.Hash.Size())
203 trafficKeys = makeTrafficKeys(state.cryptoParams, state.clientTrafficSecret)
204 } else {
205 state.serverTrafficSecret = HkdfExpandLabel(state.cryptoParams.Hash, state.serverTrafficSecret,
206 labelServerApplicationTrafficSecret, []byte{}, state.cryptoParams.Hash.Size())
207 trafficKeys = makeTrafficKeys(state.cryptoParams, state.serverTrafficSecret)
208 }
209
210 toSend := []HandshakeAction{RekeyIn{epoch: EpochUpdate, KeySet: trafficKeys}}
211
212 // If requested, roll outbound keys and send a KeyUpdate
213 if body.KeyUpdateRequest == KeyUpdateRequested {
214 logf(logTypeHandshake, "Received key update, update requested", body.KeyUpdateRequest)
215 moreToSend, alert := state.KeyUpdate(KeyUpdateNotRequested)
216 if alert != AlertNoAlert {
217 return nil, nil, alert
218 }
219 toSend = append(toSend, moreToSend...)
220 }
221 return state, toSend, AlertNoAlert
222 case *NewSessionTicketBody:
223 // XXX: Allow NewSessionTicket in both directions?
224 if !state.isClient {
225 return nil, nil, AlertUnexpectedMessage
226 }
227
228 resumptionKey := HkdfExpandLabel(state.cryptoParams.Hash, state.resumptionSecret,
229 labelResumption, body.TicketNonce, state.cryptoParams.Hash.Size())
230 psk := PreSharedKey{
231 CipherSuite: state.cryptoParams.Suite,
232 IsResumption: true,
233 Identity: body.Ticket,
234 Key: resumptionKey,
235 NextProto: state.Params.NextProto,
236 ReceivedAt: time.Now(),
237 ExpiresAt: time.Now().Add(time.Duration(body.TicketLifetime) * time.Second),
238 TicketAgeAdd: body.TicketAgeAdd,
239 }
240
241 toSend := []HandshakeAction{StorePSK{psk}}
242 return state, toSend, AlertNoAlert
243 }
244
245 logf(logTypeHandshake, "[StateConnected] Unexpected message type %v", hm.msgType)
246 return nil, nil, AlertUnexpectedMessage
247 }