tls_handshake.c
Go to the documentation of this file.
1 /**
2  * @file tls_handshake.c
3  * @brief TLS handshake
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_client_fsm.h"
38 #include "tls_server_fsm.h"
39 #include "tls_common.h"
40 #include "tls_transcript_hash.h"
41 #include "tls_quic_misc.h"
42 #include "tls_record.h"
43 #include "tls_misc.h"
44 #include "tls13_server_misc.h"
45 #include "dtls_record.h"
46 #include "debug.h"
47 
48 //Check TLS library configuration
49 #if (TLS_SUPPORT == ENABLED)
50 
51 
52 /**
53  * @brief TLS handshake initialization
54  * @param[in] context Pointer to the TLS context
55  * @return Error code
56  **/
57 
59 {
60  //Allocate send buffer if necessary
61  if(context->txBuffer == NULL)
62  {
63  //Allocate TX buffer
64  context->txBuffer = tlsAllocMem(context->txBufferSize);
65 
66  //Failed to allocate memory?
67  if(context->txBuffer == NULL)
68  return ERROR_OUT_OF_MEMORY;
69 
70  //Clear TX buffer
71  osMemset(context->txBuffer, 0, context->txBufferSize);
72  }
73 
74  //Allocate receive buffer if necessary
75  if(context->rxBuffer == NULL)
76  {
77  //Allocate RX buffer
78  context->rxBuffer = tlsAllocMem(context->rxBufferSize);
79 
80  //Failed to allocate memory?
81  if(context->rxBuffer == NULL)
82  return ERROR_OUT_OF_MEMORY;
83 
84  //Clear RX buffer
85  osMemset(context->rxBuffer, 0, context->rxBufferSize);
86  }
87 
88 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
89  //Server mode?
90  if(context->entity == TLS_CONNECTION_END_SERVER)
91  {
92  //A server implementation may choose to reject the early data
93  context->earlyDataRejected = TRUE;
94  }
95 #endif
96 
97  //The client initiates the TLS handshake by sending a ClientHello message
98  //to the server
100 
101  //Successful processing
102  return NO_ERROR;
103 }
104 
105 
106 /**
107  * @brief Perform TLS handshake
108  *
109  * TLS handshake protocol is responsible for the authentication and key
110  * exchange necessary to establish a secure session
111  *
112  * @param[in] context Pointer to the TLS context
113  * @return Error code
114  **/
115 
117 {
118  error_t error;
119 
120 #if (TLS_CLIENT_SUPPORT == ENABLED)
121  //Client mode?
122  if(context->entity == TLS_CONNECTION_END_CLIENT)
123  {
124  //Perform TLS handshake with the remote server
125  error = tlsPerformClientHandshake(context);
126  }
127  else
128 #endif
129 #if (TLS_SERVER_SUPPORT == ENABLED)
130  //Server mode?
131  if(context->entity == TLS_CONNECTION_END_SERVER)
132  {
133  //Perform TLS handshake with the remote client
134  error = tlsPerformServerHandshake(context);
135  }
136  else
137 #endif
138  //Unsupported mode of operation?
139  {
140  //Report an error
141  error = ERROR_INVALID_PARAMETER;
142  }
143 
144  //Return status code
145  return error;
146 }
147 
148 
149 /**
150  * @brief Send handshake message
151  * @param[in] context Pointer to the TLS context
152  * @param[in] data Pointer to the handshake message
153  * @param[in] length Length of the handshake message
154  * @param[in] type Handshake message type
155  * @return Error code
156  **/
157 
159  size_t length, TlsMessageType type)
160 {
161  error_t error;
162 
163 #if (DTLS_SUPPORT == ENABLED)
164  //DTLS protocol?
165  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
166  {
168 
169  //Point to the handshake message header
170  message = (DtlsHandshake *) data;
171 
172  //Make room for the handshake message header
173  osMemmove(message->data, data, length);
174 
175  //Handshake message type
176  message->msgType = type;
177  //Number of bytes in the message
178  STORE24BE(length, message->length);
179  //Message sequence number
180  message->msgSeq = htons(context->txMsgSeq);
181  //Fragment offset
182  STORE24BE(0, message->fragOffset);
183  //Fragment length
184  STORE24BE(length, message->fragLength);
185 
186  //Whenever a new message is generated, the message sequence number is
187  //incremented by one
188  context->txMsgSeq++;
189 
190  //Total length of the handshake message
191  length += sizeof(DtlsHandshake);
192  }
193  else
194 #endif
195  //TLS protocol?
196  {
198 
199  //Point to the handshake message header
200  message = (TlsHandshake *) data;
201 
202  //Make room for the handshake message header
203  osMemmove(message->data, data, length);
204 
205  //Handshake message type
206  message->msgType = type;
207  //Number of bytes in the message
208  STORE24BE(length, message->length);
209 
210  //Total length of the handshake message
211  length += sizeof(TlsHandshake);
212  }
213 
214  //The HelloRequest message must not be included in the message hashes
215  //that are maintained throughout the handshake and used in the Finished
216  //messages and the CertificateVerify message
218  {
220  }
221 
222  //TLS protocol?
223  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM ||
224  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_EAP)
225  {
226  //Send handshake message
228  }
229 #if (DTLS_SUPPORT == ENABLED)
230  //DTLS protocol?
231  else if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
232  {
233  //Send handshake message
235  }
236 #endif
237 #if (TLS_QUIC_SUPPORT == ENABLED)
238  //QUIC protocol?
239  else if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_QUIC)
240  {
241  //TLS handshake messages are carried directly over the QUIC transport
242  //(refer to RFC 9001, section 3)
243  error = tlsSendQuicHandshakeMessage(context, data, length);
244  }
245 #endif
246  //Unknown protocol?
247  else
248  {
249  //Report an error
250  error = ERROR_INVALID_PROTOCOL;
251  }
252 
253  //Return status code
254  return error;
255 }
256 
257 
258 /**
259  * @brief Receive peer's message
260  * @param[in] context Pointer to the TLS context
261  * @return Error code
262  **/
263 
265 {
266  error_t error;
267  size_t length;
268  uint8_t *data;
269  TlsContentType contentType;
270 
271 #if (DTLS_SUPPORT == ENABLED)
272  //DTLS protocol?
273  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
274  {
275  //A message can be fragmented across several DTLS records
276  error = dtlsReadProtocolData(context, &data, &length, &contentType);
277  }
278  else
279 #endif
280  //TLS protocol?
281  {
282  //A message can be fragmented across several TLS records
283  error = tlsReadProtocolData(context, &data, &length, &contentType);
284  }
285 
286  //Check status code
287  if(!error)
288  {
289  //Advance data pointer
290  context->rxBufferPos += length;
291  //Number of bytes still pending in the receive buffer
292  context->rxBufferLen -= length;
293 
294  //Handshake message received?
295  if(contentType == TLS_TYPE_HANDSHAKE)
296  {
297  //Parse handshake message
298  error = tlsParseHandshakeMessage(context, data, length);
299  }
300  //ChangeCipherSpec message received?
301  else if(contentType == TLS_TYPE_CHANGE_CIPHER_SPEC)
302  {
303  //The ChangeCipherSpec message is sent by an endpoint to notify the
304  //peer that subsequent records will be protected under the newly
305  //negotiated CipherSpec and keys
307  length);
308  }
309  //Alert message received?
310  else if(contentType == TLS_TYPE_ALERT)
311  {
312  //Parse Alert message
313  error = tlsParseAlert(context, (TlsAlert *) data, length);
314  }
315 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
316  //Application data received?
317  else if(contentType == TLS_TYPE_APPLICATION_DATA)
318  {
319 #if (TLS_SERVER_SUPPORT == ENABLED)
320  //Server mode?
321  if(context->entity == TLS_CONNECTION_END_SERVER)
322  {
323  //Process early data
324  error = tls13ProcessEarlyData(context, data, length);
325  }
326  else
327 #endif
328  {
329  //The server cannot transmit application data before the handshake
330  //is completed
331  error = ERROR_UNEXPECTED_MESSAGE;
332  }
333  }
334 #endif
335  //Unexpected message received?
336  else
337  {
338  //Abort the handshake with an unexpected_message alert
339  error = ERROR_UNEXPECTED_MESSAGE;
340  }
341  }
342 
343  //Return status code
344  return error;
345 }
346 
347 
348 /**
349  * @brief Parse handshake message
350  * @param[in] context Pointer to the TLS context
351  * @param[in] message Pointer to the handshake message to parse
352  * @param[in] length Length of the handshake messaged
353  * @return Error code
354  **/
355 
357  size_t length)
358 {
359  error_t error;
360  uint8_t msgType;
361  size_t n;
362  const void *p;
363 
364 #if (DTLS_SUPPORT == ENABLED)
365  //DTLS protocol?
366  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
367  {
368  //Retrieve handshake message type
369  msgType = ((DtlsHandshake *) message)->msgType;
370  //Point to the handshake message
371  p = message + sizeof(DtlsHandshake);
372  //Calculate the length of the handshake message
373  n = length - sizeof(DtlsHandshake);
374  }
375  else
376 #endif
377  //TLS protocol?
378  {
379  //Retrieve handshake message type
380  msgType = ((TlsHandshake *) message)->msgType;
381  //Point to the handshake message
382  p = message + sizeof(TlsHandshake);
383  //Calculate the length of the handshake message
384  n = length - sizeof(TlsHandshake);
385  }
386 
387 #if (TLS_MAX_KEY_UPDATE_MESSAGES > 0)
388  //Reset the count of consecutive KeyUpdate messages
390  {
391  context->keyUpdateCount = 0;
392  }
393 #endif
394 
395 #if (TLS_CLIENT_SUPPORT == ENABLED)
396  //Client mode?
397  if(context->entity == TLS_CONNECTION_END_CLIENT)
398  {
399  //Parse server's handshake message
400  error = tlsParseServerHandshakeMessage(context, msgType, p, n);
401 
402  //Update the hash value with the incoming handshake message
404  }
405  else
406 #endif
407 #if (TLS_SERVER_SUPPORT == ENABLED)
408  //Server mode?
409  if(context->entity == TLS_CONNECTION_END_SERVER)
410  {
411  //Update the hash value with the incoming handshake message
413  {
415  }
416 
417  //Parse client's handshake message
418  error = tlsParseClientHandshakeMessage(context, msgType, p, n);
419 
420  //Update the hash value with the incoming handshake message
422  {
424  }
425  }
426  else
427 #endif
428  //Unsupported mode of operation?
429  {
430  //Report an error
431  error = ERROR_FAILURE;
432  }
433 
434  //Return status code
435  return error;
436 }
437 
438 #endif
#define tlsAllocMem(size)
Definition: tls.h:888
#define htons(value)
Definition: cpu_endian.h:413
TLS helper functions.
@ TLS_TRANSPORT_PROTOCOL_QUIC
Definition: tls.h:1001
TLS state machine (TLS server)
error_t tlsPerformHandshake(TlsContext *context)
Perform TLS handshake.
TLS handshake.
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
QUIC helper functions.
uint8_t p
Definition: ndp.h:300
uint8_t message[]
Definition: chap.h:154
error_t tlsPerformClientHandshake(TlsContext *context)
TLS client handshake.
#define TRUE
Definition: os_port.h:50
@ TLS_TYPE_CHANGE_CIPHER_SPEC
Definition: tls.h:1068
uint8_t data[]
Definition: ethernet.h:224
@ TLS_TYPE_HANDSHAKE
Definition: tls.h:1070
error_t tlsPerformServerHandshake(TlsContext *context)
TLS server handshake.
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:1000
TlsMessageType
Handshake message type.
Definition: tls.h:1083
error_t tlsParseChangeCipherSpec(TlsContext *context, const TlsChangeCipherSpec *message, size_t length)
Parse ChangeCipherSpec message.
Definition: tls_common.c:1360
uint8_t type
Definition: coap_common.h:176
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
error_t tlsReceiveHandshakeMessage(TlsContext *context)
Receive peer's message.
TlsChangeCipherSpec
Definition: tls.h:1980
error_t tlsSendHandshakeMessage(TlsContext *context, const void *data, size_t length, TlsMessageType type)
Send handshake message.
@ TLS_STATE_CLIENT_HELLO
Definition: tls.h:1539
TlsHandshake
Definition: tls.h:1872
error_t tlsParseAlert(TlsContext *context, const TlsAlert *message, size_t length)
Parse Alert message.
Definition: tls_common.c:1637
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
DTLS record protocol.
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:1013
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
TlsAlert
Definition: tls.h:1991
@ TLS_TYPE_APPLICATION_DATA
Definition: tls.h:1071
@ TLS_TYPE_ALERT
Definition: tls.h:1069
Handshake message processing (TLS client and server)
TLS record protocol.
error_t tlsReadProtocolData(TlsContext *context, uint8_t **data, size_t *length, TlsContentType *contentType)
Read protocol data.
Definition: tls_record.c:157
@ TLS_TRANSPORT_PROTOCOL_EAP
Definition: tls.h:1002
error_t dtlsWriteProtocolData(TlsContext *context, const uint8_t *data, size_t length, TlsContentType contentType)
Write protocol data.
Definition: dtls_record.c:58
uint8_t length
Definition: tcp.h:375
@ ERROR_INVALID_PROTOCOL
Definition: error.h:101
@ TLS_TYPE_CLIENT_KEY_EXCHANGE
Definition: tls.h:1099
Helper functions for TLS 1.3 server.
TLS state machine (TLS client)
void tlsUpdateTranscriptHash(TlsContext *context, const void *data, size_t length)
Update hash value with a handshake message.
Transcript hash calculation.
TlsContentType
Content type.
Definition: tls.h:1066
uint8_t msgType
error_t dtlsReadProtocolData(TlsContext *context, uint8_t **data, size_t *length, TlsContentType *contentType)
Read protocol data.
Definition: dtls_record.c:131
uint8_t n
error_t tlsParseHandshakeMessage(TlsContext *context, const uint8_t *message, size_t length)
Parse handshake message.
DtlsHandshake
Definition: dtls_misc.h:195
error_t tlsWriteProtocolData(TlsContext *context, const uint8_t *data, size_t length, TlsContentType contentType)
Write protocol data.
Definition: tls_record.c:54
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1012
TLS (Transport Layer Security)
#define STORE24BE(a, p)
Definition: cpu_endian.h:273
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:999
error_t tlsSendQuicHandshakeMessage(TlsContext *context, const uint8_t *message, size_t length)
Send handshake message.
error_t tlsParseServerHandshakeMessage(TlsContext *context, uint8_t msgType, const void *message, size_t length)
Parse server's handshake message.
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_TYPE_HELLO_REQUEST
Definition: tls.h:1084
#define osMemset(p, value, length)
Definition: os_port.h:138
@ TLS_TYPE_KEY_UPDATE
Definition: tls.h:1104
error_t tls13ProcessEarlyData(TlsContext *context, const uint8_t *data, size_t length)
Process early data.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define osMemmove(dest, src, length)
Definition: os_port.h:150
error_t tlsInitHandshake(TlsContext *context)
TLS handshake initialization.
Definition: tls_handshake.c:58