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-2025 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.5.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls.h"
36 #include "tls_handshake.h"
37 #include "tls_server.h"
38 #include "tls_server_fsm.h"
39 #include "tls_common.h"
40 #include "tls_cache.h"
41 #include "tls_record.h"
42 #include "tls_misc.h"
43 #include "tls13_server.h"
44 #include "tls13_common.h"
45 #include "tls13_key_material.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  {
86  //At this is point, the handshake is complete and the server starts
87  //to exchange application-layer data
88  break;
89  }
90 
91  //The TLS handshake is implemented as a state machine representing the
92  //current location in the protocol
93  switch(context->state)
94  {
95  //Initial state?
96  case TLS_STATE_INIT:
97  //TLS handshake initialization
98  error = tlsInitHandshake(context);
99  break;
100 
101  //Sending ServerHello message?
104  //The server will send this message in response to a ClientHello
105  //message when it was able to find an acceptable set of algorithms
106  error = tlsSendServerHello(context);
107  break;
108 
109  //Sending Certificate message?
111  //The server must send a Certificate message whenever the agreed-
112  //upon key exchange method uses certificates for authentication. This
113  //message will always immediately follow the ServerHello message
114  error = tlsSendCertificate(context);
115  break;
116 
117  //Sending Certificate message?
119  //A non-anonymous server can optionally request a certificate from the
120  //client, if appropriate for the selected cipher suite. This message,
121  //if sent, will immediately follow the ServerKeyExchange message
122  error = tlsSendCertificateRequest(context);
123  break;
124 
125  //Sending NewSessionTicket message?
127 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
128  //TLS 1.3 currently selected?
129  if(context->version == TLS_VERSION_1_3)
130  {
131  //At any time after the server has received the client Finished
132  //message, it may send a NewSessionTicket message
133  error = tls13SendNewSessionTicket(context);
134  }
135  else
136 #endif
137  {
138  //The NewSessionTicket message is sent by the server during the TLS
139  //handshake before the ChangeCipherSpec message
140  error = tlsSendNewSessionTicket(context);
141  }
142 
143  break;
144 
145  //Sending ChangeCipherSpec message?
148  //The ChangeCipherSpec message is sent by the server and to notify the
149  //client that subsequent records will be protected under the newly
150  //negotiated CipherSpec and keys
151  error = tlsSendChangeCipherSpec(context);
152  break;
153 
154  //Sending Finished message?
156  //A Finished message is always sent immediately after a ChangeCipherSpec
157  //message to verify that the key exchange and authentication processes
158  //were successful
159  error = tlsSendFinished(context);
160  break;
161 
162 #if (DTLS_SUPPORT == ENABLED)
163  //Sending HelloVerifyRequest message?
165  //When the client sends its ClientHello message to the server, the
166  //server may respond with a HelloVerifyRequest message
167  error = dtlsSendHelloVerifyRequest(context);
168  break;
169 #endif
170 
171 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
172  //Sending ServerKeyExchange message?
174  //The ServerKeyExchange message is sent by the server only when the
175  //server Certificate message (if sent) does not contain enough data
176  //to allow the client to exchange a premaster secret
177  error = tlsSendServerKeyExchange(context);
178  break;
179 
180  //Sending ServerHelloDone message?
182  //The ServerHelloDone message is sent by the server to indicate the
183  //end of the ServerHello and associated messages
184  error = tlsSendServerHelloDone(context);
185  break;
186 #endif
187 
188 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
189  //Sending HelloRetryRequest message?
191  //The server sends a HelloRetryRequest message if the ClientHello
192  //message does not contain sufficient information to proceed with
193  //the handshake
194  error = tls13SendHelloRetryRequest(context);
195  break;
196 
197  //Handshake traffic key generation?
199  //Compute handshake traffic keys
200  error = tls13GenerateHandshakeTrafficKeys(context);
201  break;
202 
203  //Sending EncryptedExtensions message?
205  //The server sends the EncryptedExtensions message immediately after
206  //the ServerHello message. The EncryptedExtensions message contains
207  //extensions that can be protected
208  error = tls13SendEncryptedExtensions(context);
209  break;
210 
211  //Sending CertificateVerify message?
213  //Servers must send this message when authenticating via a
214  //certificate. When sent, this message must appear immediately
215  //after the Certificate message
216  error = tlsSendCertificateVerify(context);
217  break;
218 
219  //Server application traffic key generation?
221  //Compute server application traffic keys
222  error = tls13GenerateServerAppTrafficKeys(context);
223  break;
224 
225  //Client application traffic key generation?
227  //Compute client application traffic keys
228  error = tls13GenerateClientAppTrafficKeys(context);
229  break;
230 
231  //Sending KeyUpdate message?
233  //The KeyUpdate handshake message is used to indicate that the sender
234  //is updating its sending cryptographic keys
235  error = tls13SendKeyUpdate(context);
236  break;
237 #endif
238 
239  //Waiting for a message from the client?
247  //Receive client's message
248  error = tlsReceiveHandshakeMessage(context);
249  break;
250 
251  //Sending Alert message?
252  case TLS_STATE_CLOSING:
253  //Mark the TLS connection as closed
255  break;
256 
257  //TLS connection closed?
258  case TLS_STATE_CLOSED:
259  //Debug message
260  TRACE_WARNING("TLS handshake failure!\r\n");
261  //Report an error
262  error = ERROR_HANDSHAKE_FAILED;
263  break;
264 
265  //Invalid state?
266  default:
267  //Report an error
268  error = ERROR_UNEXPECTED_STATE;
269  break;
270  }
271 
272 #if (TLS_RTOS_SUPPORT == DISABLED)
273  //Force TLS to operate in non-blocking mode
274  if(error == NO_ERROR && context->state < TLS_STATE_APPLICATION_DATA)
275  {
276  error = ERROR_WOULD_BLOCK;
277  break;
278  }
279 #endif
280  }
281 
282  //Successful TLS handshake?
283  if(!error)
284  {
285 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
286  //Version of TLS prior to TLS 1.3?
287  if(context->version <= TLS_VERSION_1_2)
288  {
289 #if (TLS_TICKET_SUPPORT == ENABLED)
290  //Any ticket presented by the client?
291  if(context->sessionTicketExtReceived)
292  {
293  //If a ticket is presented by the client, the server must not
294  //attempt to use the Session ID in the ClientHello for stateful
295  //session resumption
296  }
297  else
298 #endif
299  {
300  //Save current session in the session cache for further reuse
301  tlsSaveToCache(context);
302  }
303  }
304 #endif
305  }
306  else
307  {
308  //Send an alert message to the client, if applicable
309  tlsProcessError(context, error);
310  }
311 
312  //Return status code
313  return error;
314 }
315 
316 
317 /**
318  * @brief Parse client's handshake message
319  * @param[in] context Pointer to the TLS context
320  * @param[in] msgType Handshake message type
321  * @param[in] message Pointer to the handshake message to parse
322  * @param[in] length Length of the handshake messaged
323  * @return Error code
324  **/
325 
327  const void *message, size_t length)
328 {
329  error_t error;
330 
331  //Check handshake message type
332  switch(msgType)
333  {
334  //ClientHello message received?
336  //When a client first connects to a server, it is required to send the
337  //ClientHello as its first message
338  error = tlsParseClientHello(context, message, length);
339  break;
340 
341  //Certificate message received?
343  //This is the first message the client can send after receiving a
344  //ServerHelloDone message. This message is only sent if the server
345  //requests a certificate
346  error = tlsParseCertificate(context, message, length);
347  break;
348 
349  //CertificateVerify message received?
351  //This message is used to provide explicit verification of a client
352  //certificate. This message is only sent following a client certificate
353  //that has signing capability. When sent, it must immediately follow
354  //the clientKeyExchange message
355  error = tlsParseCertificateVerify(context, message, length);
356  break;
357 
358  //Finished message received?
359  case TLS_TYPE_FINISHED:
360  //A Finished message is always sent immediately after a ChangeCipherSpec
361  //message to verify that the key exchange and authentication processes
362  //were successful
363  error = tlsParseFinished(context, message, length);
364  break;
365 
366 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
367  //ClientKeyExchange message received?
369  //This message must immediately follow the client certificate message, if
370  //it is sent. Otherwise, it must be the first message sent by the client
371  //after it receives the ServerHelloDone message
372  error = tlsParseClientKeyExchange(context, message, length);
373  break;
374 #endif
375 
376 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
377  //KeyUpdate message received?
378  case TLS_TYPE_KEY_UPDATE:
379  //The KeyUpdate handshake message is used to indicate that the client is
380  //updating its sending cryptographic keys. This message can be sent by
381  //the client after it has sent a Finished message
382  error = tls13ParseKeyUpdate(context, message, length);
383  break;
384 #endif
385 
386  //Invalid handshake message received?
387  default:
388  //Report an error
389  error = ERROR_UNEXPECTED_MESSAGE;
390  break;
391  }
392 
393  //Return status code
394  return error;
395 }
396 
397 #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:1543
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:1550
error_t tls13SendEncryptedExtensions(TlsContext *context)
Send EncryptedExtensions message.
Definition: tls13_server.c:138
error_t tls13SendHelloRetryRequest(TlsContext *context)
Send HelloRetryRequest message.
Definition: tls13_server.c:66
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:1552
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:1568
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ TLS_STATE_SERVER_APP_TRAFFIC_KEYS
Definition: tls.h:1565
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:1163
error_t tls13GenerateClientAppTrafficKeys(TlsContext *context)
Compute client application traffic keys.
@ TLS_STATE_CLIENT_HELLO
Definition: tls.h:1539
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:1649
@ TLS_TYPE_CERTIFICATE
Definition: tls.h:1094
@ TLS_STATE_SERVER_HELLO
Definition: tls.h:1544
@ TLS_STATE_HELLO_VERIFY_REQUEST
Definition: tls.h:1542
@ TLS_STATE_KEY_UPDATE
Definition: tls.h:1567
#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:96
@ TLS_TYPE_CLIENT_HELLO
Definition: tls.h:1085
@ TLS_STATE_SERVER_FINISHED
Definition: tls.h:1563
#define TLS_VERSION_1_3
Definition: tls.h:97
Handshake message processing (TLS client and server)
TLS record protocol.
@ TLS_STATE_CLIENT_CERTIFICATE_VERIFY
Definition: tls.h:1556
@ TLS_TYPE_CERTIFICATE_VERIFY
Definition: tls.h:1098
@ TLS_STATE_SERVER_CHANGE_CIPHER_SPEC
Definition: tls.h:1561
error_t tls13GenerateServerAppTrafficKeys(TlsContext *context)
Compute server application traffic keys.
error_t tls13SendNewSessionTicket(TlsContext *context)
Send NewSessionTicket message.
Definition: tls13_server.c:197
uint8_t length
Definition: tcp.h:375
@ TLS_STATE_CLIENT_APP_TRAFFIC_KEYS
Definition: tls.h:1560
@ TLS_TYPE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1099
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1566
@ TLS_STATE_CLIENT_CHANGE_CIPHER_SPEC
Definition: tls.h:1557
error_t tlsParseCertificate(TlsContext *context, const TlsCertificate *message, size_t length)
Parse Certificate message.
Definition: tls_common.c:1054
@ TLS_STATE_HANDSHAKE_TRAFFIC_KEYS
Definition: tls.h:1547
#define TRACE_WARNING(...)
Definition: debug.h:93
@ TLS_TYPE_NONE
Definition: tls.h:1067
@ TLS_STATE_CLIENT_HELLO_2
Definition: tls.h:1540
error_t tls13ParseKeyUpdate(TlsContext *context, const Tls13KeyUpdate *message, size_t length)
Parse KeyUpdate message.
Definition: tls13_common.c:178
@ TLS_STATE_CLOSING
Definition: tls.h:1569
@ TLS_STATE_SERVER_CERTIFICATE_VERIFY
Definition: tls.h:1551
uint8_t msgType
@ TLS_STATE_SERVER_CERTIFICATE
Definition: tls.h:1549
@ ERROR_UNEXPECTED_STATE
Definition: error.h:99
@ TLS_STATE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1555
error_t tlsParseFinished(TlsContext *context, const TlsFinished *message, size_t length)
Parse Finished message.
Definition: tls_common.c:1491
@ TLS_TYPE_FINISHED
Definition: tls.h:1100
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1554
@ TLS_STATE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1548
@ TLS_STATE_INIT
Definition: tls.h:1538
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:402
TLS (Transport Layer Security)
error_t tlsParseCertificateVerify(TlsContext *context, const TlsCertificateVerify *message, size_t length)
Parse CertificateVerify message.
Definition: tls_common.c:1263
error_t dtlsSendHelloVerifyRequest(TlsContext *context)
Send HelloVerifyRequest message.
Definition: dtls_misc.c:247
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:999
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:1562
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:1553
@ TLS_STATE_SERVER_HELLO_2
Definition: tls.h:1545
@ TLS_STATE_CLIENT_FINISHED
Definition: tls.h:1559
@ TLS_TYPE_KEY_UPDATE
Definition: tls.h:1104
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
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:1570