tls_quic.c
Go to the documentation of this file.
1 /**
2  * @file tls_quic.c
3  * @brief QUIC TLS related API
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  * @section Description
28  *
29  * The TLS protocol provides communications security over the Internet. The
30  * protocol allows client/server applications to communicate in a way that
31  * is designed to prevent eavesdropping, tampering, or message forgery
32  *
33  * @author Oryx Embedded SARL (www.oryx-embedded.com)
34  * @version 2.5.4
35  **/
36 
37 //Switch to the appropriate trace level
38 #define TRACE_LEVEL TLS_TRACE_LEVEL
39 
40 //Dependencies
41 #include "tls.h"
42 #include "tls_quic.h"
43 #include "debug.h"
44 
45 //Check TLS library configuration
46 #if (TLS_SUPPORT == ENABLED && TLS_QUIC_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief Register QUIC-specific callback functions
51  * @param[in] context Pointer to the TLS context
52  * @param[in] quicCallbacks QUIC callback functions
53  * @param[in] handle An opaque pointer passed to the callback functions
54  * @return Error code
55  **/
56 
58  const TlsQuicCallbacks *quicCallbacks, void *handle)
59 {
60  //Invalid TLS context?
61  if(context == NULL)
63 
64  //Save QUIC-specific callback functions
65  context->quicCallbacks = *quicCallbacks;
66  //This opaque pointer will be directly passed to the callback functions
67  context->quicHandle = handle;
68 
69  //Successful processing
70  return NO_ERROR;
71 }
72 
73 
74 /**
75  * @brief Set QUIC-specific handle
76  * @param[in] context Pointer to the TLS context
77  * @param[in] handle An opaque pointer passed to the callback functions
78  * @return Error code
79  **/
80 
81 error_t tlsSetQuicHandle(TlsContext *context, void *handle)
82 {
83  //Invalid TLS context?
84  if(context == NULL)
86 
87  //This opaque pointer will be directly passed to the callback functions
88  context->quicHandle = handle;
89 
90  //Successful processing
91  return NO_ERROR;
92 }
93 
94 
95 /**
96  * @brief Set local QUIC transport parameters
97  * @param[in] context Pointer to the TLS context
98  * @param[in] params Pointer to the QUIC transport parameters
99  * @param[in] length Length of the QUIC transport parameters, in bytes
100  * @return Error code
101  **/
102 
104  const uint8_t *params, size_t length)
105 {
106  //Check parameters
107  if(context == NULL || params == NULL)
109 
110  //Check whether the QUIC transport parameters have already been configured
111  if(context->localQuicTransportParams != NULL)
112  {
113  //Release memory
114  tlsFreeMem(context->localQuicTransportParams);
115  context->localQuicTransportParams = NULL;
116  context->localQuicTransportParamsLen = 0;
117  }
118 
119  //Valid QUIC transport parameters?
120  if(length > 0)
121  {
122  //Allocate a memory block to hold the QUIC transport parameters
123  context->localQuicTransportParams = tlsAllocMem(length);
124  //Failed to allocate memory?
125  if(context->localQuicTransportParams == NULL)
126  return ERROR_OUT_OF_MEMORY;
127 
128  //Save the QUIC transport parameters
129  osMemcpy(context->localQuicTransportParams, params, length);
130  context->localQuicTransportParamsLen = length;
131  }
132 
133  //Successful processing
134  return NO_ERROR;
135 }
136 
137 
138 /**
139  * @brief Get remote QUIC transport parameters
140  * @param[in] context Pointer to the TLS context
141  * @param[out] params Pointer to the QUIC transport parameters
142  * @param[out] length Length of the QUIC transport parameters, in bytes
143  * @return Error code
144  **/
145 
147  const uint8_t **params, size_t *length)
148 {
149  //Check parameters
150  if(context == NULL || params == NULL || length == NULL)
152 
153  //Return the QUIC transport parameters
154  *params = context->remoteQuicTransportParams;
155  *length = context->remoteQuicTransportParamsLen;
156 
157  //Successful processing
158  return NO_ERROR;
159 }
160 
161 
162 /**
163  * @brief Process incoming handshake data
164  * @param[in] context Pointer to the TLS context
165  * @param[in] level Encryption level
166  * @param[in] data Pointer to the handshake data
167  * @param[in] length Length of the handshake data, in bytes
168  * @return Error code
169  **/
170 
172  TlsEncryptionLevel level, const uint8_t *data, size_t length)
173 {
174  error_t error;
175 
176  //Initialize status code
177  error = NO_ERROR;
178 
179  //Empty receive buffer?
180  if(context->rxBufferLen == 0)
181  {
182  //Rewind to the beginning of the buffer
183  context->rxBufferPos = 0;
184  }
185 
186  //Check current TLS receiving encryption level
187  if(level == context->decryptionEngine.level)
188  {
189  //Check the length of the handshake data
190  if((context->rxBufferLen + length) <= context->rxBufferSize)
191  {
192  //QUIC CRYPTO frames only carry TLS handshake messages (refer to
193  //RFC 9001, section 4.1.3)
194  context->rxBufferType = TLS_TYPE_HANDSHAKE;
195 
196  //The content of CRYPTO frames might either be processed incrementally by
197  //TLS or buffered until complete messages or flights are available. TLS
198  //is responsible for buffering handshake bytes that have arrived in order
199  osMemcpy(context->rxBuffer + context->rxBufferLen, data, length);
200 
201  //Number of bytes available for reading
202  context->rxBufferLen += length;
203  }
204  else
205  {
206  //Report an error
207  error = ERROR_BUFFER_OVERFLOW;
208  }
209  }
210  else
211  {
212  //Report an error
213  error = ERROR_INVALID_LEVEL;
214  }
215 
216  //Return status code
217  return error;
218 }
219 
220 #endif
#define tlsAllocMem(size)
Definition: tls.h:888
@ ERROR_INVALID_LEVEL
Definition: error.h:209
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:143
error_t tlsSetLocalQuicTransportParams(TlsContext *context, const uint8_t *params, size_t length)
Set local QUIC transport parameters.
Definition: tls_quic.c:103
uint8_t data[]
Definition: ethernet.h:224
@ TLS_TYPE_HANDSHAKE
Definition: tls.h:1070
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
error_t tlsGetRemoteQuicTransportParams(TlsContext *context, const uint8_t **params, size_t *length)
Get remote QUIC transport parameters.
Definition: tls_quic.c:146
TlsEncryptionLevel
Encryption level.
Definition: tls.h:1579
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
error_t tlsRegisterQuicCallbacks(TlsContext *context, const TlsQuicCallbacks *quicCallbacks, void *handle)
Register QUIC-specific callback functions.
Definition: tls_quic.c:57
error_t tlsSetQuicHandle(TlsContext *context, void *handle)
Set QUIC-specific handle.
Definition: tls_quic.c:81
uint8_t length
Definition: tcp.h:375
TLS (Transport Layer Security)
QUIC callback functions.
Definition: tls.h:2159
QUIC TLS related API.
error_t tlsProcessQuicHandshakeMessage(TlsContext *context, TlsEncryptionLevel level, const uint8_t *data, size_t length)
Process incoming handshake data.
Definition: tls_quic.c:171
#define tlsFreeMem(p)
Definition: tls.h:893
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.