1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** NetX Secure Component */
16 /** */
17 /** Transport Layer Security (TLS) */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define NX_SECURE_SOURCE_CODE
23
24 #include "nx_secure_tls.h"
25
26 /**************************************************************************/
27 /* */
28 /* FUNCTION RELEASE */
29 /* */
30 /* _nx_secure_tls_send_finished PORTABLE C */
31 /* 6.2.1 */
32 /* AUTHOR */
33 /* */
34 /* Timothy Stapko, Microsoft Corporation */
35 /* */
36 /* DESCRIPTION */
37 /* */
38 /* This function generates the Finished message to send to the remote */
39 /* host. The Finished message contains a hash of all handshake */
40 /* messages received up to this point which is used to verify that */
41 /* none of the messages have been corrupted. */
42 /* */
43 /* INPUT */
44 /* */
45 /* tls_session TLS control block */
46 /* send_packet Packet used to send message */
47 /* */
48 /* OUTPUT */
49 /* */
50 /* status Completion status */
51 /* */
52 /* CALLS */
53 /* */
54 /* _nx_secure_tls_finished_hash_generate Generate Finished hash */
55 /* */
56 /* CALLED BY */
57 /* */
58 /* _nx_secure_dtls_client_handshake DTLS client state machine */
59 /* _nx_secure_dtls_server_handshake DTLS server state machine */
60 /* _nx_secure_tls_client_handshake TLS client state machine */
61 /* _nx_secure_tls_server_handshake TLS server state machine */
62 /* */
63 /* RELEASE HISTORY */
64 /* */
65 /* DATE NAME DESCRIPTION */
66 /* */
67 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
68 /* 09-30-2020 Timothy Stapko Modified comment(s), */
69 /* verified memcpy use cases, */
70 /* fixed renegotiation bug, */
71 /* resulting in version 6.1 */
72 /* 03-08-2023 Yanwu Cai Modified comment(s), */
73 /* fixed compiler errors when */
74 /* x509 is disabled, */
75 /* resulting in version 6.2.1 */
76 /* */
77 /**************************************************************************/
_nx_secure_tls_send_finished(NX_SECURE_TLS_SESSION * tls_session,NX_PACKET * send_packet)78 UINT _nx_secure_tls_send_finished(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet)
79 {
80 UCHAR *finished_label;
81 UINT hash_size = 0;
82 UINT status;
83 UINT is_server;
84
85
86 is_server = (tls_session -> nx_secure_tls_socket_type == NX_SECURE_TLS_SESSION_TYPE_SERVER);
87
88 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
89 if(tls_session->nx_secure_tls_1_3)
90 {
91
92 /* Generate the TLS 1.3-specific finished data. */
93 status = _nx_secure_tls_1_3_finished_hash_generate(tls_session, is_server, &hash_size,
94 send_packet -> nx_packet_append_ptr,
95 ((ULONG)(send_packet -> nx_packet_data_end) -
96 (ULONG)(send_packet -> nx_packet_append_ptr)));
97 }
98 else
99 #endif /* (NX_SECURE_TLS_TLS_1_3_ENABLED) */
100 {
101 /* Select our label for generating the finished hash expansion. */
102 if (is_server)
103 {
104 finished_label = (UCHAR *)"server finished";
105 }
106 else
107 {
108 finished_label = (UCHAR *)"client finished";
109 }
110
111 if (NX_SECURE_TLS_FINISHED_HASH_SIZE > ((ULONG)(send_packet -> nx_packet_data_end) - (ULONG)(send_packet -> nx_packet_append_ptr)))
112 {
113
114 /* Packet buffer too small. */
115 return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
116 }
117
118 /* Finally, generate the verification data required by TLS - 12 bytes using the PRF and the data
119 we have collected. Place the result directly into the packet buffer. */
120 status = _nx_secure_tls_finished_hash_generate(tls_session, finished_label, send_packet -> nx_packet_append_ptr);
121
122 #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
123 /* If we are doing secure renegotiation as per RFC 5746, we need to save off the generated
124 verify data now. For TLS 1.0-1.2 this is 12 bytes. If SSLv3 is ever used, it will be 36 bytes. */
125 NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_local_verify_data, send_packet -> nx_packet_append_ptr, NX_SECURE_TLS_FINISHED_HASH_SIZE); /* Use case of memcpy is verified. lgtm[cpp/banned-api-usage-required-any] */
126 #endif
127
128 /* The finished verify data is always 12 bytes for TLS 1.2 and earlier. */
129 hash_size = NX_SECURE_TLS_FINISHED_HASH_SIZE;
130 }
131
132 /* Adjust the packet into which we just wrote the finished hash. */
133 send_packet -> nx_packet_append_ptr = send_packet -> nx_packet_append_ptr + hash_size;
134 send_packet -> nx_packet_length = send_packet -> nx_packet_length + hash_size;
135
136 if (status != NX_SUCCESS)
137 {
138 return(status);
139 }
140
141 #ifndef NX_SECURE_DISABLE_X509
142
143 /* Finished with the handshake - we can free certificates now. */
144 status = _nx_secure_tls_remote_certificate_free_all(tls_session);
145 #endif
146
147 return(status);
148 }
149
150