summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_tls_1_3_finished_hash_generate.c
blob: 94e102e868d34ff502e69beb92eb7359a03bc95e (plain)
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
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    Transport Layer Security (TLS)                                     */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/
#define NX_SECURE_SOURCE_CODE

#include "nx_secure_tls.h"
#ifdef NX_SECURE_ENABLE_DTLS
#include "nx_secure_dtls.h"
#endif /* NX_SECURE_ENABLE_DTLS */

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_tls_1_3_finished_hash_generate           PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function generates the Finished message hash using the hash    */
/*    data collected in the TLS session control block during the          */
/*    handshake. TLS 1.3 uses a different construction for the finished   */
/*    hash separate from the earlier versions.                            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    tls_session                           TLS control block             */
/*    is_server                             Non-zero for server hash      */
/*    hash_size                             Return the size of the hash   */
/*    finished_hash                         Pointer to hash output buffer */
/*    available_size                        Available size of buffer      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    [nx_crypto_init]                      Initialize crypto             */
/*    [nx_crypto_operation]                 Crypto operation              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_tls_send_finished          Send Finished message         */
/*    _nx_secure_tls_process_finished       Process Finished message      */
/*                                                                        */
/**************************************************************************/
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
UINT _nx_secure_tls_1_3_finished_hash_generate(NX_SECURE_TLS_SESSION *tls_session,
                                               UINT is_server, UINT *hash_size, UCHAR *finished_hash,
                                               ULONG available_size)
{
UINT                                  status;
const NX_CRYPTO_METHOD               *hmac_method = NX_NULL;
const NX_CRYPTO_METHOD               *hash_method = NX_NULL;
CHAR                                 *metadata = NX_NULL;
UINT                                  metadata_size;
VOID                                 *handler = NX_NULL;
UCHAR                                *finished_key = NX_NULL;
UINT                                  finished_key_len;
//UINT                                  hash_index;
UCHAR                                *transcript_hash;
NX_SECURE_TLS_KEY_SECRETS *secrets;

    /* We need to extract the Finished hash and verify that the handshake to this point
       has not been corrupted or tampered with. */

    /* From the RFC 8446 (TLS 1.3):
        finished_key = HKDF-Expand-Label(BaseKey, "finished", "", Hash.length)

        Structure of this message:

          struct {
              opaque verify_data[Hash.length];
          } Finished;

       The verify_data value is computed as follows:

          verify_data =
              HMAC(finished_key,
                   Transcript-Hash(Handshake Context,
                                   Certificate*, CertificateVerify*))
    */

    if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL)
    {
        return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
    }

    if (available_size < tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size)
    {

        /* Packet buffer too small. */
        return(NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL);
    }

    /* Get our generated secrets. */
    secrets = &tls_session->nx_secure_tls_key_material.nx_secure_tls_key_secrets;


    /* Get our HMAC and hash methods. */
    hash_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash;
    hmac_method = tls_session -> nx_secure_tls_crypto_table->nx_secure_tls_hmac_method;
    *hash_size = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size;

    /* Get our Finished Hash metadata space. */
    metadata = tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch;
    metadata_size = tls_session -> nx_secure_tls_handshake_hash.nx_secure_tls_handshake_hash_scratch_size;    

    /* Now select the finished key (generated in _nx_secure_tls_1_3_generate_keys) based on the current request. */
    if(is_server)
    {
        finished_key = secrets->tls_server_finished_key;
        finished_key_len = secrets->tls_server_finished_key_len;
    }
    else
    {
        /* Client. */
        finished_key = secrets->tls_client_finished_key;
        finished_key_len = secrets->tls_client_finished_key_len;
    }

    /* Get the transcript hash context up to now - Server Finished received for Client, all messages before Finished sent for Server. */
    transcript_hash = tls_session->nx_secure_tls_key_material.nx_secure_tls_transcript_hashes[NX_SECURE_TLS_TRANSCRIPT_IDX_SERVER_FINISHED];

    /* Initialize HMAC. */
    if (hash_method -> nx_crypto_init)
    {
        status = hmac_method -> nx_crypto_init((NX_CRYPTO_METHOD*)hmac_method,
                                                finished_key,
                                                (finished_key_len << 3),
                                                handler,
                                                metadata,
                                                metadata_size);

        if (status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }
    }

    /* Initialize HMAC with our hash method. */
    status = hmac_method->nx_crypto_operation(NX_CRYPTO_HMAC_SET_HASH, NX_NULL, (NX_CRYPTO_METHOD*)hash_method, NX_NULL, 0, NX_NULL, 0,
                                              NX_NULL, NX_NULL, 0, metadata, metadata_size, NX_NULL, NX_NULL);

    if (status != NX_CRYPTO_SUCCESS)
    {
        return(status);
    }

    if (hmac_method -> nx_crypto_operation != NX_NULL)
    {
        status = hmac_method -> nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE,
                                                 handler,
                                                 (NX_CRYPTO_METHOD*)hmac_method,
                                                 finished_key,
                                                 (NX_CRYPTO_KEY_SIZE)(finished_key_len << 3),
                                                 NX_NULL,
                                                 0,
                                                 NX_NULL,
                                                 NX_NULL,
                                                 0,
                                                 metadata,
                                                 metadata_size,
                                                 NX_NULL,
                                                 NX_NULL);

        if (status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }
    }

    /* Update the HMAC with the transcript hash context. */
    if (hmac_method -> nx_crypto_operation != NX_NULL)
    {
        status = hmac_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
                                                    handler,
                                                    (NX_CRYPTO_METHOD*)hmac_method,
                                                    NX_NULL, /* Key. */
                                                    0,
                                                    transcript_hash, /* Input. */
                                                    *hash_size,
                                                    NX_NULL,
                                                    NX_NULL, /* output */
                                                    0,
                                                    metadata,
                                                    metadata_size,
                                                    NX_NULL,
                                                    NX_NULL);

        if (status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }
    }

    /* Finally, calculate the Finished hash data. */
    if (hmac_method -> nx_crypto_operation != NX_NULL)
    {
        status = hmac_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
                                                    handler,
                                                    (NX_CRYPTO_METHOD*)hmac_method,
                                                    NX_NULL,
                                                    0,
                                                    NX_NULL,
                                                    0,
                                                    NX_NULL,
                                                    finished_hash,
                                                    *hash_size,
                                                    metadata,
                                                    metadata_size,
                                                    NX_NULL,
                                                    NX_NULL);

        if (status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }
    }
    else
    {
        return(NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE);
    }

    return(NX_SUCCESS);
}
#endif