tls_server_fsm.c
Go to the documentation of this file.
1 /**
2  * @file tls_server_fsm.c
3  * @brief TLS state machine (TLS server)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSL Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls/tls.h"
36 #include "tls/tls_handshake.h"
37 #include "tls/tls_server.h"
38 #include "tls/tls_server_fsm.h"
39 #include "tls/tls_common.h"
40 #include "tls/tls_cache.h"
41 #include "tls/tls_record.h"
42 #include "tls/tls_misc.h"
43 #include "tls13/tls13_server.h"
44 #include "tls13/tls13_common.h"
46 #include "debug.h"
47 
48 //Check TLS library configuration
49 #if (TLS_SUPPORT == ENABLED && TLS_SERVER_SUPPORT == ENABLED)
50 
51 
52 /**
53  * @brief TLS server handshake
54  * @param[in] context Pointer to the TLS context
55  * @return Error code
56  **/
57 
59 {
60  error_t error;
61 
62  //Initialize status code
63  error = NO_ERROR;
64 
65  //Wait for the handshake to complete
66  while(!error)
67  {
68  //TLS protocol?
69  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM)
70  {
71  //Check current state
72  if(context->state != TLS_STATE_INIT &&
73  context->state != TLS_STATE_CLOSED)
74  {
75  //Flush send buffer
76  error = tlsWriteProtocolData(context, NULL, 0, TLS_TYPE_NONE);
77  //Any error to report?
78  if(error)
79  break;
80  }
81  }
82 
83  //Check whether the handshake is complete
84  if(context->state == TLS_STATE_APPLICATION_DATA ||
85  context->state == TLS_STATE_NEW_SESSION_TICKET_ACK ||
86  context->state == TLS_STATE_KEY_UPDATE_ACK)
87  {
88  //At this is point, the handshake is complete and the server starts
89  //to exchange application-layer data
90  break;
91  }
92 
93  //The TLS handshake is implemented as a state machine representing the
94  //current location in the protocol
95  switch(context->state)
96  {
97  //Initial state?
98  case TLS_STATE_INIT:
99  //TLS handshake initialization
100  error = tlsInitHandshake(context);
101  break;
102 
103  //Sending ServerHello message?
106  //The server will send this message in response to a ClientHello
107  //message when it was able to find an acceptable set of algorithms
108  error = tlsSendServerHello(context);
109  break;
110 
111  //Sending Certificate message?
113  //The server must send a Certificate message whenever the agreed-
114  //upon key exchange method uses certificates for authentication. This
115  //message will always immediately follow the ServerHello message
116  error = tlsSendCertificate(context);
117  break;
118 
119  //Sending Certificate message?
121  //A non-anonymous server can optionally request a certificate from the
122  //client, if appropriate for the selected cipher suite. This message,
123  //if sent, will immediately follow the ServerKeyExchange message
124  error = tlsSendCertificateRequest(context);
125  break;
126 
127  //Sending NewSessionTicket message?
129  //The NewSessionTicket message is sent by the server during the TLS
130  //handshake before the ChangeCipherSpec message
131  error = tlsSendNewSessionTicket(context);
132  break;
133 
134  //Sending ChangeCipherSpec message?
137  //The ChangeCipherSpec message is sent by the server and to notify the
138  //client that subsequent records will be protected under the newly
139  //negotiated CipherSpec and keys
140  error = tlsSendChangeCipherSpec(context);
141  break;
142 
143  //Sending Finished message?
145  //A Finished message is always sent immediately after a ChangeCipherSpec
146  //message to verify that the key exchange and authentication processes
147  //were successful
148  error = tlsSendFinished(context);
149  break;
150 
151 #if (DTLS_SUPPORT == ENABLED)
152  //Sending HelloVerifyRequest message?
154  //When the client sends its ClientHello message to the server, the
155  //server may respond with a HelloVerifyRequest message
156  error = dtlsSendHelloVerifyRequest(context);
157  break;
158 #endif
159 
160 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
161  //Sending ServerKeyExchange message?
163  //The ServerKeyExchange message is sent by the server only when the
164  //server Certificate message (if sent) does not contain enough data
165  //to allow the client to exchange a premaster secret
166  error = tlsSendServerKeyExchange(context);
167  break;
168 
169  //Sending ServerHelloDone message?
171  //The ServerHelloDone message is sent by the server to indicate the
172  //end of the ServerHello and associated messages
173  error = tlsSendServerHelloDone(context);
174  break;
175 #endif
176 
177 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
178  //Sending HelloRetryRequest message?
180  //The server sends a HelloRetryRequest message if the ClientHello
181  //message does not contain sufficient information to proceed with
182  //the handshake
183  error = tls13SendHelloRetryRequest(context);
184  break;
185 
186  //Handshake traffic key generation?
188  //Compute handshake traffic keys
189  error = tls13GenerateHandshakeTrafficKeys(context);
190  break;
191 
192  //Sending EncryptedExtensions message?
194  //The server sends the EncryptedExtensions message immediately after
195  //the ServerHello message. The EncryptedExtensions message contains
196  //extensions that can be protected
197  error = tls13SendEncryptedExtensions(context);
198  break;
199 
200  //Sending CertificateVerify message?
202  //Servers must send this message when authenticating via a
203  //certificate. When sent, this message must appear immediately
204  //after the Certificate message
205  error = tlsSendCertificateVerify(context);
206  break;
207 
208  //Server application traffic key generation?
210  //Compute server application traffic keys
211  error = tls13GenerateServerAppTrafficKeys(context);
212  break;
213 
214  //Client application traffic key generation?
216  //Compute client application traffic keys
217  error = tls13GenerateClientAppTrafficKeys(context);
218  break;
219 
220  //Sending NewSessionTicket message?
222  //At any time after the server has received the client Finished
223  //message, it may send a NewSessionTicket message
224  error = tls13SendNewSessionTicket(context);
225  break;
226 
227  //Sending KeyUpdate message?
229  //The KeyUpdate handshake message is used to indicate that the sender
230  //is updating its sending cryptographic keys
231  error = tls13SendKeyUpdate(context);
232  break;
233 #endif
234 
235 #if (DTLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
236  //Sending ACK message for client's final flight?
237  case TLS_STATE_FINAL_ACK:
238  //When a handshake flight is sent without any expected response, as is
239  //the case with the client's final flight, the flight must be
240  //acknowledged with an ACK message
241  error = dtls13SendAck(context);
242  break;
243 #endif
244 
245  //Waiting for a message from the client?
253  //Receive client's message
254  error = tlsReceiveHandshakeMessage(context);
255  break;
256 
257  //Sending Alert message?
258  case TLS_STATE_CLOSING:
259  //Mark the TLS connection as closed
261  break;
262 
263  //TLS connection closed?
264  case TLS_STATE_CLOSED:
265  //Debug message
266  TRACE_WARNING("TLS handshake failure!\r\n");
267  //Report an error
268  error = ERROR_HANDSHAKE_FAILED;
269  break;
270 
271  //Invalid state?
272  default:
273  //Report an error
274  error = ERROR_UNEXPECTED_STATE;
275  break;
276  }
277 
278 #if (TLS_RTOS_SUPPORT == DISABLED)
279  //Force TLS to operate in non-blocking mode
280  if(error == NO_ERROR &&
281  context->state != TLS_STATE_APPLICATION_DATA &&
282  context->state != TLS_STATE_NEW_SESSION_TICKET_ACK &&
283  context->state != TLS_STATE_KEY_UPDATE_ACK)
284  {
285  error = ERROR_WOULD_BLOCK;
286  break;
287  }
288 #endif
289  }
290 
291  //Successful TLS handshake?
292  if(!error)
293  {
294 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
295  //Version of TLS prior to TLS 1.3?
296  if(context->version <= TLS_VERSION_1_2)
297  {
298 #if (TLS_TICKET_SUPPORT == ENABLED)
299  //Any ticket presented by the client?
300  if(context->sessionTicketExtReceived)
301  {
302  //If a ticket is presented by the client, the server must not
303  //attempt to use the Session ID in the ClientHello for stateful
304  //session resumption
305  }
306  else
307 #endif
308  {
309  //Save current session in the session cache for further reuse
310  tlsSaveToCache(context);
311  }
312  }
313 #endif
314  }
315  else
316  {
317  //Send an alert message to the client, if applicable
318  tlsProcessError(context, error);
319  }
320 
321  //Return status code
322  return error;
323 }
324 
325 
326 /**
327  * @brief Parse client's handshake message
328  * @param[in] context Pointer to the TLS context
329  * @param[in] msgType Handshake message type
330  * @param[in] message Pointer to the handshake message to parse
331  * @param[in] length Length of the handshake messaged
332  * @return Error code
333  **/
334 
336  const void *message, size_t length)
337 {
338  error_t error;
339 
340  //Check handshake message type
341  switch(msgType)
342  {
343  //ClientHello message received?
345  //When a client first connects to a server, it is required to send the
346  //ClientHello as its first message
347  error = tlsParseClientHello(context, message, length);
348  break;
349 
350  //Certificate message received?
352  //This is the first message the client can send after receiving a
353  //ServerHelloDone message. This message is only sent if the server
354  //requests a certificate
355  error = tlsParseCertificate(context, message, length);
356  break;
357 
358  //CertificateVerify message received?
360  //This message is used to provide explicit verification of a client
361  //certificate. This message is only sent following a client certificate
362  //that has signing capability. When sent, it must immediately follow
363  //the clientKeyExchange message
364  error = tlsParseCertificateVerify(context, message, length);
365  break;
366 
367  //Finished message received?
368  case TLS_TYPE_FINISHED:
369  //A Finished message is always sent immediately after a ChangeCipherSpec
370  //message to verify that the key exchange and authentication processes
371  //were successful
372  error = tlsParseFinished(context, message, length);
373  break;
374 
375 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
376  //ClientKeyExchange message received?
378  //This message must immediately follow the client certificate message, if
379  //it is sent. Otherwise, it must be the first message sent by the client
380  //after it receives the ServerHelloDone message
381  error = tlsParseClientKeyExchange(context, message, length);
382  break;
383 #endif
384 
385 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
386  //KeyUpdate message received?
387  case TLS_TYPE_KEY_UPDATE:
388  //The KeyUpdate handshake message is used to indicate that the client is
389  //updating its sending cryptographic keys. This message can be sent by
390  //the client after it has sent a Finished message
391  error = tls13ParseKeyUpdate(context, message, length);
392  break;
393 #endif
394 
395  //Invalid handshake message received?
396  default:
397  //Report an error
398  error = ERROR_UNEXPECTED_MESSAGE;
399  break;
400  }
401 
402  //Return status code
403  return error;
404 }
405 
406 #endif
TLS helper functions.
error_t tlsSaveToCache(TlsContext *context)
Save current session in cache.
Definition: tls_cache.c:164
TLS state machine (TLS server)
@ TLS_STATE_HELLO_RETRY_REQUEST
Definition: tls.h:1563
error_t tlsSendNewSessionTicket(TlsContext *context)
Send NewSessionTicket message.
Definition: tls_server.c:405
Handshake message processing (TLS 1.3 server)
@ ERROR_WOULD_BLOCK
Definition: error.h:96
error_t tls13GenerateHandshakeTrafficKeys(TlsContext *context)
Compute handshake traffic keys.
error_t tlsSendChangeCipherSpec(TlsContext *context)
Send ChangeCipherSpec message.
Definition: tls_common.c:273
TLS handshake.
@ TLS_STATE_SERVER_KEY_EXCHANGE
Definition: tls.h:1570
error_t tls13SendEncryptedExtensions(TlsContext *context)
Send EncryptedExtensions message.
Definition: tls13_server.c:152
error_t tls13SendHelloRetryRequest(TlsContext *context)
Send HelloRetryRequest message.
Definition: tls13_server.c:67
error_t tlsSendCertificate(TlsContext *context)
Send Certificate message.
Definition: tls_common.c:66
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
error_t tlsSendCertificateRequest(TlsContext *context)
Send CertificateRequest message.
Definition: tls_server.c:273
uint8_t message[]
Definition: chap.h:154
@ TLS_STATE_CERTIFICATE_REQUEST
Definition: tls.h:1572
error_t tlsPerformServerHandshake(TlsContext *context)
TLS server handshake.
Session cache management.
Handshake message processing (TLS 1.3 client and server)
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1589
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ TLS_STATE_SERVER_APP_TRAFFIC_KEYS
Definition: tls.h:1587
error_t tlsReceiveHandshakeMessage(TlsContext *context)
Receive peer's message.
error_t tlsParseClientHello(TlsContext *context, const TlsClientHello *message, size_t length)
Parse ClientHello message.
Definition: tls_server.c:1173
error_t tls13GenerateClientAppTrafficKeys(TlsContext *context)
Compute client application traffic keys.
@ TLS_STATE_CLIENT_HELLO
Definition: tls.h:1559
error_t tlsSendServerHelloDone(TlsContext *context)
Send ServerHelloDone message.
Definition: tls_server.c:352
error_t tlsParseClientKeyExchange(TlsContext *context, const TlsClientKeyExchange *message, size_t length)
Parse ClientKeyExchange message.
Definition: tls_server.c:1665
@ TLS_TYPE_CERTIFICATE
Definition: tls.h:1111
@ TLS_STATE_SERVER_HELLO
Definition: tls.h:1564
@ TLS_STATE_HELLO_VERIFY_REQUEST
Definition: tls.h:1562
@ TLS_STATE_KEY_UPDATE
Definition: tls.h:1592
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
error_t tlsSendServerKeyExchange(TlsContext *context)
Send ServerKeyExchange message.
Definition: tls_server.c:190
#define TLS_VERSION_1_2
Definition: tls.h:97
@ TLS_TYPE_CLIENT_HELLO
Definition: tls.h:1102
@ TLS_STATE_SERVER_FINISHED
Definition: tls.h:1585
Handshake message processing (TLS client and server)
TLS record protocol.
@ TLS_STATE_CLIENT_CERTIFICATE_VERIFY
Definition: tls.h:1576
@ TLS_TYPE_CERTIFICATE_VERIFY
Definition: tls.h:1115
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC
Definition: tls.h:1583
error_t tls13GenerateServerAppTrafficKeys(TlsContext *context)
Compute server application traffic keys.
error_t tls13SendNewSessionTicket(TlsContext *context)
Send NewSessionTicket message.
Definition: tls13_server.c:211
uint8_t length
Definition: tcp.h:375
@ TLS_STATE_CLIENT_APP_TRAFFIC_KEYS
Definition: tls.h:1580
@ TLS_TYPE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1116
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1581
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC
Definition: tls.h:1577
@ TLS_STATE_NEW_SESSION_TICKET_2
Definition: tls.h:1582
error_t tlsParseCertificate(TlsContext *context, const TlsCertificate *message, size_t length)
Parse Certificate message.
Definition: tls_common.c:1039
@ TLS_STATE_HANDSHAKE_TRAFFIC_KEYS
Definition: tls.h:1567
#define TRACE_WARNING(...)
Definition: debug.h:93
@ TLS_TYPE_NONE
Definition: tls.h:1084
@ TLS_STATE_CLIENT_HELLO_2
Definition: tls.h:1560
error_t tls13ParseKeyUpdate(TlsContext *context, const Tls13KeyUpdate *message, size_t length)
Parse KeyUpdate message.
Definition: tls13_common.c:185
@ TLS_STATE_CLOSING
Definition: tls.h:1594
@ TLS_STATE_SERVER_CERTIFICATE_VERIFY
Definition: tls.h:1571
uint8_t msgType
@ TLS_STATE_SERVER_CERTIFICATE
Definition: tls.h:1569
@ ERROR_UNEXPECTED_STATE
Definition: error.h:99
@ TLS_STATE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1575
error_t tlsParseFinished(TlsContext *context, const TlsFinished *message, size_t length)
Parse Finished message.
Definition: tls_common.c:1464
@ TLS_TYPE_FINISHED
Definition: tls.h:1117
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1574
@ TLS_STATE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1568
@ TLS_STATE_INIT
Definition: tls.h:1558
Handshake message processing (TLS server)
error_t tlsWriteProtocolData(TlsContext *context, const uint8_t *data, size_t length, TlsContentType contentType)
Write protocol data.
Definition: tls_record.c:54
error_t tlsSendFinished(TlsContext *context)
Send Finished message.
Definition: tls_common.c:387
TLS (Transport Layer Security)
@ TLS_STATE_FINAL_ACK
Definition: tls.h:1588
error_t tlsParseCertificateVerify(TlsContext *context, const TlsCertificateVerify *message, size_t length)
Parse CertificateVerify message.
Definition: tls_common.c:1248
error_t dtlsSendHelloVerifyRequest(TlsContext *context)
Send HelloVerifyRequest message.
Definition: dtls_misc.c:281
error_t dtls13SendAck(TlsContext *context)
Send ACK message.
Definition: dtls13_misc.c:117
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:1016
TLS 1.3 key schedule.
void tlsProcessError(TlsContext *context, error_t errorCode)
Translate an error code to an alert message.
Definition: tls_misc.c:74
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC_2
Definition: tls.h:1584
error_t tlsParseClientHandshakeMessage(TlsContext *context, uint8_t msgType, const void *message, size_t length)
Parse client's handshake message.
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
@ TLS_STATE_SERVER_HELLO_DONE
Definition: tls.h:1573
@ TLS_STATE_SERVER_HELLO_2
Definition: tls.h:1565
@ TLS_STATE_CLIENT_FINISHED
Definition: tls.h:1579
@ TLS_STATE_KEY_UPDATE_ACK
Definition: tls.h:1593
@ TLS_TYPE_KEY_UPDATE
Definition: tls.h:1121
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ TLS_STATE_NEW_SESSION_TICKET_ACK
Definition: tls.h:1591
error_t tlsSendCertificateVerify(TlsContext *context)
Send CertificateVerify message.
Definition: tls_common.c:192
error_t tlsSendServerHello(TlsContext *context)
Send ServerHello message.
Definition: tls_server.c:80
error_t tlsInitHandshake(TlsContext *context)
TLS handshake initialization.
Definition: tls_handshake.c:58
error_t tls13SendKeyUpdate(TlsContext *context)
Send KeyUpdate message.
Definition: tls13_common.c:57
@ TLS_STATE_CLOSED
Definition: tls.h:1595