-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls_handshake.c
421 lines (368 loc) · 11.1 KB
/
tls_handshake.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* @file tls_handshake.c
* @brief TLS handshake
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneSSL Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.4.4
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL TLS_TRACE_LEVEL
//Dependencies
#include "tls.h"
#include "tls_handshake.h"
#include "tls_client_fsm.h"
#include "tls_server_fsm.h"
#include "tls_common.h"
#include "tls_transcript_hash.h"
#include "tls_record.h"
#include "tls_misc.h"
#include "tls13_server_misc.h"
#include "dtls_record.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED)
/**
* @brief TLS handshake initialization
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsInitHandshake(TlsContext *context)
{
//Allocate send buffer if necessary
if(context->txBuffer == NULL)
{
//Allocate TX buffer
context->txBuffer = tlsAllocMem(context->txBufferSize);
//Failed to allocate memory?
if(context->txBuffer == NULL)
return ERROR_OUT_OF_MEMORY;
//Clear TX buffer
osMemset(context->txBuffer, 0, context->txBufferSize);
}
//Allocate receive buffer if necessary
if(context->rxBuffer == NULL)
{
//Allocate RX buffer
context->rxBuffer = tlsAllocMem(context->rxBufferSize);
//Failed to allocate memory?
if(context->rxBuffer == NULL)
return ERROR_OUT_OF_MEMORY;
//Clear RX buffer
osMemset(context->rxBuffer, 0, context->rxBufferSize);
}
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Server mode?
if(context->entity == TLS_CONNECTION_END_SERVER)
{
//A server implementation may choose to reject the early data
context->earlyDataRejected = TRUE;
}
#endif
//The client initiates the TLS handshake by sending a ClientHello message
//to the server
tlsChangeState(context, TLS_STATE_CLIENT_HELLO);
//Successful processing
return NO_ERROR;
}
/**
* @brief Perform TLS handshake
*
* TLS handshake protocol is responsible for the authentication and key
* exchange necessary to establish a secure session
*
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsPerformHandshake(TlsContext *context)
{
error_t error;
#if (TLS_CLIENT_SUPPORT == ENABLED)
//Client mode?
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//Perform TLS handshake with the remote server
error = tlsPerformClientHandshake(context);
}
else
#endif
#if (TLS_SERVER_SUPPORT == ENABLED)
//Server mode?
if(context->entity == TLS_CONNECTION_END_SERVER)
{
//Perform TLS handshake with the remote client
error = tlsPerformServerHandshake(context);
}
else
#endif
//Unsupported mode of operation?
{
//Report an error
error = ERROR_INVALID_PARAMETER;
}
//Return status code
return error;
}
/**
* @brief Send handshake message
* @param[in] context Pointer to the TLS context
* @param[in] data Pointer to the handshake message
* @param[in] length Length of the handshake message
* @param[in] type Handshake message type
* @return Error code
**/
error_t tlsSendHandshakeMessage(TlsContext *context, const void *data,
size_t length, TlsMessageType type)
{
error_t error;
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
DtlsHandshake *message;
//Point to the handshake message header
message = (DtlsHandshake *) data;
//Make room for the handshake message header
osMemmove(message->data, data, length);
//Handshake message type
message->msgType = type;
//Number of bytes in the message
STORE24BE(length, message->length);
//Message sequence number
message->msgSeq = htons(context->txMsgSeq);
//Fragment offset
STORE24BE(0, message->fragOffset);
//Fragment length
STORE24BE(length, message->fragLength);
//Whenever a new message is generated, the message sequence number is
//incremented by one
context->txMsgSeq++;
//Total length of the handshake message
length += sizeof(DtlsHandshake);
}
else
#endif
//TLS protocol?
{
TlsHandshake *message;
//Point to the handshake message header
message = (TlsHandshake *) data;
//Make room for the handshake message header
osMemmove(message->data, data, length);
//Handshake message type
message->msgType = type;
//Number of bytes in the message
STORE24BE(length, message->length);
//Total length of the handshake message
length += sizeof(TlsHandshake);
}
//The HelloRequest message must not be included in the message hashes
//that are maintained throughout the handshake and used in the Finished
//messages and the CertificateVerify message
if(type != TLS_TYPE_HELLO_REQUEST)
{
tlsUpdateTranscriptHash(context, data, length);
}
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//Send handshake message
error = dtlsWriteProtocolData(context, data, length, TLS_TYPE_HANDSHAKE);
}
else
#endif
//TLS protocol?
{
//Send handshake message
error = tlsWriteProtocolData(context, data, length, TLS_TYPE_HANDSHAKE);
}
//Return status code
return error;
}
/**
* @brief Receive peer's message
* @param[in] context Pointer to the TLS context
* @return Error code
**/
error_t tlsReceiveHandshakeMessage(TlsContext *context)
{
error_t error;
size_t length;
uint8_t *data;
TlsContentType contentType;
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//A message can be fragmented across several DTLS records
error = dtlsReadProtocolData(context, &data, &length, &contentType);
}
else
#endif
//TLS protocol?
{
//A message can be fragmented across several TLS records
error = tlsReadProtocolData(context, &data, &length, &contentType);
}
//Check status code
if(!error)
{
//Advance data pointer
context->rxBufferPos += length;
//Number of bytes still pending in the receive buffer
context->rxBufferLen -= length;
//Handshake message received?
if(contentType == TLS_TYPE_HANDSHAKE)
{
//Parse handshake message
error = tlsParseHandshakeMessage(context, data, length);
}
//ChangeCipherSpec message received?
else if(contentType == TLS_TYPE_CHANGE_CIPHER_SPEC)
{
//The ChangeCipherSpec message is sent by an endpoint to notify the
//peer that subsequent records will be protected under the newly
//negotiated CipherSpec and keys
error = tlsParseChangeCipherSpec(context, (TlsChangeCipherSpec *) data,
length);
}
//Alert message received?
else if(contentType == TLS_TYPE_ALERT)
{
//Parse Alert message
error = tlsParseAlert(context, (TlsAlert *) data, length);
}
#if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//Application data received?
else if(contentType == TLS_TYPE_APPLICATION_DATA)
{
#if (TLS_SERVER_SUPPORT == ENABLED)
//Server mode?
if(context->entity == TLS_CONNECTION_END_SERVER)
{
//Process early data
error = tls13ProcessEarlyData(context, data, length);
}
else
#endif
{
//The server cannot transmit application data before the handshake
//is completed
error = ERROR_UNEXPECTED_MESSAGE;
}
}
#endif
//Unexpected message received?
else
{
//Abort the handshake with an unexpected_message alert
error = ERROR_UNEXPECTED_MESSAGE;
}
}
//Return status code
return error;
}
/**
* @brief Parse handshake message
* @param[in] context Pointer to the TLS context
* @param[in] message Pointer to the handshake message to parse
* @param[in] length Length of the handshake messaged
* @return Error code
**/
error_t tlsParseHandshakeMessage(TlsContext *context, const uint8_t *message,
size_t length)
{
error_t error;
uint8_t msgType;
size_t n;
const void *p;
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//Retrieve handshake message type
msgType = ((DtlsHandshake *) message)->msgType;
//Point to the handshake message
p = message + sizeof(DtlsHandshake);
//Calculate the length of the handshake message
n = length - sizeof(DtlsHandshake);
}
else
#endif
//TLS protocol?
{
//Retrieve handshake message type
msgType = ((TlsHandshake *) message)->msgType;
//Point to the handshake message
p = message + sizeof(TlsHandshake);
//Calculate the length of the handshake message
n = length - sizeof(TlsHandshake);
}
#if (TLS_MAX_KEY_UPDATE_MESSAGES > 0)
//Reset the count of consecutive KeyUpdate messages
if(msgType != TLS_TYPE_KEY_UPDATE)
{
context->keyUpdateCount = 0;
}
#endif
#if (TLS_CLIENT_SUPPORT == ENABLED)
//Client mode?
if(context->entity == TLS_CONNECTION_END_CLIENT)
{
//Parse server's handshake message
error = tlsParseServerHandshakeMessage(context, msgType, p, n);
//Update the hash value with the incoming handshake message
tlsUpdateTranscriptHash(context, message, length);
}
else
#endif
#if (TLS_SERVER_SUPPORT == ENABLED)
//Server mode?
if(context->entity == TLS_CONNECTION_END_SERVER)
{
//Update the hash value with the incoming handshake message
if(msgType == TLS_TYPE_CLIENT_KEY_EXCHANGE)
{
tlsUpdateTranscriptHash(context, message, length);
}
//Parse client's handshake message
error = tlsParseClientHandshakeMessage(context, msgType, p, n);
//Update the hash value with the incoming handshake message
if(msgType != TLS_TYPE_CLIENT_KEY_EXCHANGE)
{
tlsUpdateTranscriptHash(context, message, length);
}
}
else
#endif
//Unsupported mode of operation?
{
//Report an error
error = ERROR_FAILURE;
}
//Return status code
return error;
}
#endif