1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Secure Component                                                 */
17 /**                                                                       */
18 /**    Transport Layer Security (TLS)                                     */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  COMPONENT DEFINITION                                   RELEASE        */
27 /*                                                                        */
28 /*    nx_secure_tls.h                                     PORTABLE C      */
29 /*                                                           6.3.0        */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Timothy Stapko, Microsoft Corporation                               */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines all service prototypes and data structure         */
37 /*    definitions for TLS implementation.                                 */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
44 /*  09-30-2020     Timothy Stapko           Modified comment(s), and      */
45 /*                                            fixed race condition for    */
46 /*                                            multithread transmission,   */
47 /*                                            supported chained packet,   */
48 /*                                            priority ciphersuite and ECC*/
49 /*                                            curve logic, updated product*/
50 /*                                            constants, fixed compiler   */
51 /*                                            warning, fixed renegotiation*/
52 /*                                            bug, fixed certificate      */
53 /*                                            buffer allocation,          */
54 /*                                            resulting in version 6.1    */
55 /*  12-31-2020     Timothy Stapko           Modified comment(s),          */
56 /*                                            updated product constants,  */
57 /*                                            improved buffer length      */
58 /*                                            verification,               */
59 /*                                            resulting in version 6.1.3  */
60 /*  02-02-2021     Timothy Stapko           Modified comment(s), added    */
61 /*                                            support for fragmented TLS  */
62 /*                                            Handshake messages,         */
63 /*                                            resulting in version 6.1.4  */
64 /*  03-02-2021     Yuxin Zhou               Modified comment(s), and      */
65 /*                                            updated product constants,  */
66 /*                                            resulting in version 6.1.5  */
67 /*  04-02-2021     Yuxin Zhou               Modified comment(s), and      */
68 /*                                            updated product constants,  */
69 /*                                            resulting in version 6.1.6  */
70 /*  06-02-2021     Yuxin Zhou               Modified comment(s), and      */
71 /*                                            updated product constants,  */
72 /*                                            resulting in version 6.1.7  */
73 /*  08-02-2021     Timothy Stapko           Modified comment(s), added    */
74 /*                                            hash clone and cleanup,     */
75 /*                                            added state to cleanup      */
76 /*                                            session cipher,             */
77 /*                                            resulting in version 6.1.8  */
78 /*  10-15-2021     Timothy Stapko           Modified comment(s), added    */
79 /*                                            support to disable client   */
80 /*                                            initiated renegotiation,    */
81 /*                                            resulting in version 6.1.9  */
82 /*  01-31-2022     Yuxin Zhou               Modified comment(s), and      */
83 /*                                            updated product constants,  */
84 /*                                            resulting in version 6.1.10 */
85 /*  04-25-2022     Yuxin Zhou               Modified comment(s), and      */
86 /*                                            enabled AEAD for TLS 1.3,   */
87 /*                                            resulting in version 6.1.11 */
88 /*  07-29-2022     Yuxin Zhou               Modified comment(s), and      */
89 /*                                            updated product constants,  */
90 /*                                            fixed compiler errors when  */
91 /*                                            TX_SAFETY_CRITICAL is       */
92 /*                                            enabled, increased default  */
93 /*                                            pre-master sec size for PSK,*/
94 /*                                            updated alert message for   */
95 /*                                            downgrade protection,       */
96 /*                                            resulting in version 6.1.12 */
97 /*  10-31-2022     Yanwu Cai                Modified comment(s), and added*/
98 /*                                            custom secret generation,   */
99 /*                                            fixed renegotiation when    */
100 /*                                            receiving in non-block mode,*/
101 /*                                            added function to set packet*/
102 /*                                            pool,                       */
103 /*                                            resulting in version 6.2.0  */
104 /*  10-31-2022     Bo Chen                  Modified comment(s), and      */
105 /*                                            updated product constants,  */
106 /*                                            resulting in version 6.3.0  */
107 /*                                                                        */
108 /**************************************************************************/
109 
110 #ifndef SRC_NX_SECURE_TLS_H_
111 #define SRC_NX_SECURE_TLS_H_
112 
113 /* Determine if a C++ compiler is being used.  If so, ensure that standard
114    C is used to process the API information.  */
115 #ifdef __cplusplus
116 
117 /* Yes, C++ compiler is present.  Use standard C.  */
118 extern   "C" {
119 
120 #endif
121 
122 #ifdef ECLIPSE_TEST
123 #define __REV16(x) x
124 #endif
125 
126 /* Include the ThreadX and port-specific data type file.  */
127 #include "tx_port.h"
128 
129 #ifdef NX_SECURE_SOURCE_CODE
130 #ifndef TX_SAFETY_CRITICAL
131 #ifndef TX_DISABLE_ERROR_CHECKING
132 #define TX_DISABLE_ERROR_CHECKING
133 #endif
134 #endif
135 #ifndef NX_DISABLE_ERROR_CHECKING
136 #define NX_DISABLE_ERROR_CHECKING
137 #endif
138 #endif
139 
140 #include "nx_api.h"
141 #include "nx_secure_port.h"
142 #include "nx_crypto.h"
143 
144 /* Enable ECC by default. */
145 #ifndef NX_SECURE_DISABLE_ECC_CIPHERSUITE
146 #ifndef NX_SECURE_ENABLE_ECC_CIPHERSUITE
147 #define NX_SECURE_ENABLE_ECC_CIPHERSUITE
148 #endif
149 #else
150 #undef NX_SECURE_ENABLE_ECC_CIPHERSUITE
151 #endif
152 
153 #include "nx_secure_x509.h"
154 
155 #if defined(NX_SECURE_TLS_ENABLE_TLS_1_3) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE)
156 #ifndef NX_SECURE_ENABLE_AEAD_CIPHER
157 #define NX_SECURE_ENABLE_AEAD_CIPHER
158 #endif /* NX_SECURE_ENABLE_AEAD_CIPHER */
159 #endif
160 #ifndef NX_SECURE_AEAD_CIPHER_CHECK
161 #define NX_SECURE_AEAD_CIPHER_CHECK(a)                  NX_FALSE
162 #endif /* NX_SECURE_AEAD_CIPHER_CHECK */
163 
164 /* ID is used to determine if a TLS session has been initialized. */
165 #define NX_SECURE_TLS_ID                                ((ULONG)0x544c5320)
166 
167 #define AZURE_RTOS_NETX_SECURE
168 #define NETX_SECURE_MAJOR_VERSION                       6
169 #define NETX_SECURE_MINOR_VERSION                       3
170 #define NETX_SECURE_PATCH_VERSION                       0
171 
172 /* The following symbols are defined for backward compatibility reasons. */
173 #define EL_PRODUCT_NETX_SECURE
174 #define __PRODUCT_NETX_SECURE__
175 #define __NETX_SECURE_MAJOR_VERSION__                   NETX_SECURE_MAJOR_VERSION
176 #define __NETX_SECURE_MINOR_VERSION__                   NETX_SECURE_MINOR_VERSION
177 #define __NETX_SECURE_SERVICE_PACK_VERSION__            NETX_SECURE_PATCH_VERSION
178 #define NETX_SECURE_SERVICE_PACK_VERSION                NETX_SECURE_PATCH_VERSION
179 
180 /* Define memcpy, memset and memcmp functions used internal. */
181 #ifndef NX_SECURE_MEMCPY
182 #define NX_SECURE_MEMCPY                                memcpy
183 #endif /* NX_SECURE_MEMCPY */
184 
185 #ifndef NX_SECURE_MEMCMP
186 #define NX_SECURE_MEMCMP                                memcmp
187 #endif /* NX_SECURE_MEMCMP */
188 
189 #ifndef NX_SECURE_MEMSET
190 #define NX_SECURE_MEMSET                                memset
191 #endif /* NX_SECURE_MEMSET */
192 
193 #ifndef NX_SECURE_MEMMOVE
194 #define NX_SECURE_MEMMOVE                               memmove
195 #endif /* NX_SECURE_MEMMOVE */
196 
197 #ifndef NX_SECURE_HASH_METADATA_CLONE
198 #define NX_SECURE_HASH_METADATA_CLONE                   NX_SECURE_MEMCPY
199 #endif /* NX_SECURE_HASH_METADATA_CLONE */
200 
201 #ifndef NX_SECURE_HASH_CLONE_CLEANUP
202 #define NX_SECURE_HASH_CLONE_CLEANUP(x, y)
203 #endif /* NX_SECURE_HASH_CLONE_CLEANUP  */
204 
205 /* Map NX_SECURE_CALLER_CHECKING_EXTERNS to NX_CALLER_CHECKING_EXTERNS, which is defined
206    in nx_port.h.*/
207 
208 #define NX_SECURE_CALLER_CHECKING_EXTERNS               NX_CALLER_CHECKING_EXTERNS
209 
210 /* Configuration macros - define these to disable TLS client or server.
211    #define NX_SECURE_TLS_SERVER_DISABLED
212    #define NX_SECURE_TLS_CLIENT_DISABLED
213  */
214 
215 /* Configuration macro: allow self-signed certificates to be used to identify a remote host. */
216 /* #define NX_SECURE_ALLOW_SELF_SIGNED_CERTIFICATES */
217 
218 /* Configuration macro: disable secure session renegotiation extension (RFC 5746).
219    #define NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
220  */
221 /* Configuration macro: terminate the connection immediately upon failure to receive the
222    secure renegotiation extension during the initial handshake.
223    #define NX_SECURE_TLS_REQUIRE_RENEGOTIATION_EXT
224  */
225 
226 /* API return values.  */
227 
228 #define NX_SECURE_TLS_SUCCESS                           0x00        /* Function returned successfully. */
229 #define NX_SECURE_TLS_SESSION_UNINITIALIZED             0x101       /* TLS main loop called with uninitialized socket. */
230 #define NX_SECURE_TLS_UNRECOGNIZED_MESSAGE_TYPE         0x102       /* TLS record layer received an unrecognized message type. */
231 #define NX_SECURE_TLS_INVALID_STATE                     0x103       /* Internal error - state not recognized. */
232 #define NX_SECURE_TLS_INVALID_PACKET                    0x104       /* Internal error - received packet did not contain TLS data. */
233 #define NX_SECURE_TLS_UNKNOWN_CIPHERSUITE               0x105       /* The chosen ciphersuite is not supported - internal error for server,
234                                                                        for client it means the remote host sent a bad ciphersuite (error or attack). */
235 #define NX_SECURE_TLS_UNSUPPORTED_CIPHER                0x106       /* In doing an encryption or decryption, the chosen cipher is disabled or unavailable. */
236 #define NX_SECURE_TLS_HANDSHAKE_FAILURE                 0x107       /* Something in message processing during the handshake has failed. */
237 #define NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE           0x108       /* An incoming record had a MAC that did not match the one we generated. */
238 #define NX_SECURE_TLS_TCP_SEND_FAILED                   0x109       /* The outgoing TCP send of a record failed for some reason. */
239 #define NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH          0x10A       /* An incoming message had a length that was incorrect (usually a length other
240                                                                        than one in the header, as in certificate messages) */
241 #define NX_SECURE_TLS_BAD_CIPHERSPEC                    0x10B       /* An incoming ChangeCipherSpec message was incorrect. */
242 #define NX_SECURE_TLS_INVALID_SERVER_CERT               0x10C       /* An incoming server certificate did not parse correctly. */
243 #define NX_SECURE_TLS_UNSUPPORTED_PUBLIC_CIPHER         0x10D       /* A certificate provided by a server specified a public-key operation we do not support. */
244 #define NX_SECURE_TLS_NO_SUPPORTED_CIPHERS              0x10E       /* Received a ClientHello with no supported ciphersuites. */
245 #define NX_SECURE_TLS_UNKNOWN_TLS_VERSION               0x10F       /* An incoming record had a TLS version that isn't recognized. */
246 #define NX_SECURE_TLS_UNSUPPORTED_TLS_VERSION           0x110       /* An incoming record had a valid TLS version, but one that isn't supported. */
247 #define NX_SECURE_TLS_ALLOCATE_PACKET_FAILED            0x111       /* An internal packet allocation for a TLS message failed. */
248 #define NX_SECURE_TLS_INVALID_CERTIFICATE               0x112       /* An X509 certificate did not parse correctly. */
249 #define NX_SECURE_TLS_NO_CLOSE_RESPONSE                 0x113       /* During a TLS session close, did not receive a CloseNotify from the remote host. */
250 #define NX_SECURE_TLS_ALERT_RECEIVED                    0x114       /* The remote host sent an alert, indicating an error and closing the connection. */
251 #define NX_SECURE_TLS_FINISHED_HASH_FAILURE             0x115       /* The Finish message hash received does not match the local generated hash - handshake corruption. */
252 #define NX_SECURE_TLS_UNKNOWN_CERT_SIG_ALGORITHM        0x116       /* A certificate during verification had an unsupported signature algorithm. */
253 #define NX_SECURE_TLS_CERTIFICATE_SIG_CHECK_FAILED      0x117       /* A certificate signature verification check failed - certificate data did not match signature. */
254 #define NX_SECURE_TLS_BAD_COMPRESSION_METHOD            0x118       /* Received a Hello message with an unsupported compression method. */
255 #define NX_SECURE_TLS_CERTIFICATE_NOT_FOUND             0x119       /* In an operation on a certificate list, no matching certificate was found. */
256 #define NX_SECURE_TLS_INVALID_SELF_SIGNED_CERT          0x11A       /* The remote host sent a self-signed certificate and NX_SECURE_ALLOW_SELF_SIGNED_CERTIFICATES is not defined. */
257 #define NX_SECURE_TLS_ISSUER_CERTIFICATE_NOT_FOUND      0x11B       /* A remote certificate was received with an issuer not in the local trusted store. */
258 #define NX_SECURE_TLS_OUT_OF_ORDER_MESSAGE              0x11C       /* A DTLS message was received in the wrong order - a dropped datagram is the likely culprit. */
259 #define NX_SECURE_TLS_INVALID_REMOTE_HOST               0x11D       /* A packet was received from a remote host that we do not recognize. */
260 #define NX_SECURE_TLS_INVALID_EPOCH                     0x11E       /* A DTLS message was received and matched to a DTLS session but it had the wrong epoch and should be ignored. */
261 #define NX_SECURE_TLS_REPEAT_MESSAGE_RECEIVED           0x11F       /* A DTLS message was received with a sequence number we have already seen, ignore it. */
262 #define NX_SECURE_TLS_NEED_DTLS_SESSION                 0x120       /* A TLS session was used in a DTLS API that was not initialized for DTLS. */
263 #define NX_SECURE_TLS_NEED_TLS_SESSION                  0x121       /* A TLS session was used in a TLS API that was initialized for DTLS and not TLS. */
264 #define NX_SECURE_TLS_SEND_ADDRESS_MISMATCH             0x122       /* Caller attempted to send data over a DTLS session with an IP address or port that did not match the session. */
265 #define NX_SECURE_TLS_NO_FREE_DTLS_SESSIONS             0x123       /* A new connection tried to get a DTLS session from the cache, but there were none free. */
266 #define NX_SECURE_DTLS_SESSION_NOT_FOUND                0x124       /* The caller searched for a DTLS session, but the given IP address and port did not match any entries in the cache. */
267 #define NX_SECURE_TLS_NO_MORE_PSK_SPACE                 0x125       /* The caller attempted to add a PSK to a TLS session but there was no more space in the given session. */
268 #define NX_SECURE_TLS_NO_MATCHING_PSK                   0x126       /* A remote host provided a PSK identity hint that did not match any in our local store. */
269 #define NX_SECURE_TLS_CLOSE_NOTIFY_RECEIVED             0x127       /* A TLS session received a CloseNotify alert from the remote host indicating the session is complete. */
270 #define NX_SECURE_TLS_NO_AVAILABLE_SESSIONS             0x128       /* No TLS sessions in a TLS object are available to handle a connection. */
271 #define NX_SECURE_TLS_NO_CERT_SPACE_ALLOCATED           0x129       /* No certificate space was allocated for incoming remote certificates. */
272 #define NX_SECURE_TLS_PADDING_CHECK_FAILED              0x12A       /* Encryption padding in an incoming message was not correct. */
273 #define NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_TYPE        0x12B       /* In processing a CertificateVerifyRequest, no supported certificate type was provided by the remote server. */
274 #define NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_ALG         0x12C       /* In processing a CertificateVerifyRequest, no supported signature algorithm was provided by the remote server. */
275 #define NX_SECURE_TLS_INSUFFICIENT_CERT_SPACE           0x12D       /* Not enough certificate buffer space allocated for a certificate. */
276 #define NX_SECURE_TLS_PROTOCOL_VERSION_CHANGED          0x12E       /* The protocol version in an incoming TLS record did not match the version of the established session. */
277 #define NX_SECURE_TLS_NO_RENEGOTIATION_ERROR            0x12F       /* A HelloRequest message was received, but we are not re-negotiating. */
278 #define NX_SECURE_TLS_UNSUPPORTED_FEATURE               0x130       /* A feature that was disabled was encountered during a TLS session or handshake. */
279 #define NX_SECURE_TLS_CERTIFICATE_VERIFY_FAILURE        0x131       /* A CertificateVerify message from a remote Client failed to verify the Client certificate. */
280 #define NX_SECURE_TLS_EMPTY_REMOTE_CERTIFICATE_RECEIVED 0x132       /* The remote host sent an empty certificate message. */
281 #define NX_SECURE_TLS_RENEGOTIATION_EXTENSION_ERROR     0x133       /* An error occurred in processing or sending the Secure Renegotiation Indication Extension. */
282 #define NX_SECURE_TLS_RENEGOTIATION_SESSION_INACTIVE    0x134       /* A server attempted to re-establish a connection that was already closed. */
283 #define NX_SECURE_TLS_PACKET_BUFFER_TOO_SMALL           0x135       /* A TLS record was received which has a size that exceeds the allocated packet buffer space. */
284 #define NX_SECURE_TLS_EXTENSION_NOT_FOUND               0x136       /* A TLS extension parsing function did not find the intended extension in the hello extension data. */
285 #define NX_SECURE_TLS_SNI_EXTENSION_INVALID             0x137       /* Received a ClientHello containing invalid SNI extension data. */
286 #define NX_SECURE_TLS_CERT_ID_INVALID                   0x138       /* Tried to add a certificate with a numeric ID that was invalid (likely 0). */
287 #define NX_SECURE_TLS_CERT_ID_DUPLICATE                 0x139       /* Tried to add a certificate with a numeric ID that was already used - needs to be unique. */
288 #define NX_SECURE_TLS_RENEGOTIATION_FAILURE             0x13A       /* Attempted a renegotiation with a remote host that did not supply the SCSV or renegotiation extension. */
289 #define NX_SECURE_TLS_MISSING_CRYPTO_ROUTINE            0x13B       /* In attempting to perform a cryptographic operation, an entry in the ciphersuite table (or one of its function pointers) was NULL. */
290 #define NX_SECURE_TLS_EMPTY_EC_GROUP                    0x13C       /* ECC ciphersuite is set but no supported EC group. */
291 #define NX_SECURE_TLS_EMPTY_EC_POINT_FORMAT             0x13D       /* ECC ciphersuite is set but no supported EC point format. */
292 #define NX_SECURE_TLS_BAD_SERVERHELLO_KEYSHARE          0x13E       /* In a TLS 1.3 KeyShare extension from a remote server, the server provided something we didn't expect. */
293 #define NX_SECURE_TLS_INSUFFICIENT_METADATA_SPACE       0x13F       /* The application-supplied "metadata" for TLS cryptographic routines was too small. */
294 #define NX_SECURE_TLS_POST_HANDSHAKE_RECEIVED           0x140       /* Not an error, but an indication to continue processing until application data is received. */
295 #define NX_SECURE_TLS_BAD_CLIENTHELLO_KEYSHARE          0x141       /* In a TLS 1.3 KeyShare extension from a remote client, the client provided something we didn't expect. */
296 #define NX_SECURE_TLS_1_3_UNKNOWN_CIPHERSUITE           0x142       /* Received unknown ciphersuite when using TLS 1.3. */
297 #define NX_SECURE_TLS_INVALID_SESSION_TICKET            0x143       /* Received a NewSessionTicket message with improper or invalid parameters. */
298 #define NX_SECURE_TLS_MISSING_EXTENSION                 0x144       /* Specific extension is missed in the message. */
299 #define NX_SECURE_TLS_CERTIFICATE_REQUIRED              0x145       /* Server receive empty certificate. */
300 #define NX_SECURE_TLS_UNEXPECTED_CLIENTHELLO            0x146       /* TLS 1.3 Server receive ClientHello for renegotiation. */
301 #define NX_SECURE_TLS_INAPPROPRIATE_FALLBACK            0x147       /* Remote Client attempted an inappropriate TLS version downgrade. */
302 #define NX_SECURE_TLS_BAD_CLIENTHELLO_PSK_EXTENSION     0x148       /* In a TLS 1.3 PSK extension from a remote client, the client provided something we didn't expect. */
303 #define NX_SECURE_TLS_PSK_BINDER_MISMATCH               0x149       /* In a TLS 1.3 PSK extension from a remote client, the client provided a bad PSK binder value. */
304 #define NX_SECURE_TLS_CRYPTO_KEYS_TOO_LARGE             0x14A       /* In attempting to generate TLS session keys, the key buffer was too small - increase NX_SECURE_TLS_KEY_MATERIAL_SIZE. */
305 #define NX_SECURE_TLS_UNSUPPORTED_ECC_CURVE             0x14B       /* The remote host provided a certificate or chose a ciphersuite with an ECC curve that isn't supported. */
306 #define NX_SECURE_TLS_UNSUPPORTED_ECC_FORMAT            0x14C       /* Encountered a curve type or ECC format that is not supported. */
307 #define NX_SECURE_TLS_UNSUPPORTED_SIGNATURE_ALGORITHM   0x14D       /* An unsupported signature algorithm was encountered (used in key exchange or other non-certificate situations). */
308 #define NX_SECURE_TLS_SIGNATURE_VERIFICATION_ERROR      0x14E       /* A signature verification check failed (used in key exchange or other non-cert situations). */
309 #define NX_SECURE_TLS_UNEXPECTED_MESSAGE                0x14F       /* TLS received an unexpected message from the remote host. */
310 #define NX_SECURE_TLS_AEAD_DECRYPT_FAIL                 0x150       /* An incoming record did not pass integrity check with AEAD ciphers. */
311 #define NX_SECURE_TLS_RECORD_OVERFLOW                   0x151       /* Received a TLSCiphertext record that had a length too long. */
312 #define NX_SECURE_TLS_HANDSHAKE_FRAGMENT_RECEIVED       0x152       /* Received a fragmented handshake message - take appropriate action at a higher level of the state machine. */
313 #define NX_SECURE_TLS_TRANSMIT_LOCKED                   0x153       /* Another thread is transmitting. */
314 #define NX_SECURE_TLS_DOWNGRADE_DETECTED                0x154       /* Detected an inappropriate TLS version downgrade by TLS 1.3 client. */
315 
316 /* NX_CONTINUE is a symbol defined in NetX Duo 5.10.  For backward compatibility, this symbol is defined here */
317 #if ((__NETXDUO_MAJOR_VERSION__ == 5) && (__NETXDUO_MINOR_VERSION__ == 9))
318 #define NX_CONTINUE                                     0x55
319 #endif
320 
321 #ifdef NX_SECURE_TLS_SERVER_DISABLED
322 #ifdef NX_SECURE_TLS_CLIENT_DISABLED
323 #error "Must enable either TLS Client or TLS Server!"
324 #endif
325 #endif
326 
327 /* Define TLS and DTLS identity */
328 #define NX_SECURE_TLS                                   0
329 #define NX_SECURE_DTLS                                  1
330 
331 /* For proper handshake processing, we save off what type of socket we have.
332  * These values represent the possible types. */
333 #define NX_SECURE_TLS_SESSION_TYPE_NONE                 0 /* No socket type assigned yet. Should produce an error if the wrong function is called. */
334 #define NX_SECURE_TLS_SESSION_TYPE_CLIENT               1 /* Client TLS socket - assigned when nx_secure_tls_client_socket_connect is called. */
335 #define NX_SECURE_TLS_SESSION_TYPE_SERVER               2 /* Server TLS socket - assigned when nx_secure_tls_server_socket_listen is called. */
336 
337 /* TLS handshake states - Server. */
338 #define NX_SECURE_TLS_SERVER_STATE_IDLE                 0  /* TLS server is waiting for a ClientHello. */
339 #define NX_SECURE_TLS_SERVER_STATE_ERROR                1  /* TLS server encountered an internal (non-alert) error. */
340 #define NX_SECURE_TLS_SERVER_STATE_ALERT_SENT           2  /* TLS server encountered an issue and sent an alert to the remote client. */
341 #define NX_SECURE_TLS_SERVER_STATE_SEND_HELLO           3  /* A ClientHello has been received and we need to respond. */
342 #define NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_VERIFY    4  /* In DTLS, send a HelloVerifyRequest message back to the client. */
343 #define NX_SECURE_TLS_SERVER_STATE_HELLO_SENT           5  /* ServerHelloDone response has been sent to client. */
344 #define NX_SECURE_TLS_SERVER_STATE_CLIENT_CERTIFICATE   6  /* A Client Certificate message has been received. */
345 #define NX_SECURE_TLS_SERVER_STATE_KEY_EXCHANGE         7  /* Server needs to send a key exchange message. */
346 #define NX_SECURE_TLS_SERVER_STATE_CERTIFICATE_VERIFY   8  /* A Client CertificateVerify message has been received. */
347 #define NX_SECURE_TLS_SERVER_STATE_FINISH_HANDSHAKE     9  /* Server received a client Finished message and needs to respond. */
348 #define NX_SECURE_TLS_SERVER_STATE_HANDSHAKE_FINISHED   10 /* Server has completed the handshake. */
349 #define NX_SECURE_TLS_SERVER_STATE_HELLO_REQUEST        11 /* A HelloRequest has been sent. */
350 #define NX_SECURE_TLS_SERVER_STATE_SEND_HELLO_RETRY     12 /* A ClientHello has been received but key_share mismatch. We need to respond HelloRetryRequest. */
351 
352 /* TLS handshake states - Client. */
353 #define NX_SECURE_TLS_CLIENT_STATE_IDLE                 0  /* Client socket is not connected, waiting for connection request from application. */
354 #define NX_SECURE_TLS_CLIENT_STATE_ERROR                1  /* Client socket has encountered an error (separate from alerts). */
355 #define NX_SECURE_TLS_CLIENT_STATE_ALERT_SENT           2  /* TLS Client sent an alert to the remote server. */
356 #define NX_SECURE_TLS_CLIENT_STATE_HELLO_REQUEST        3  /* TLS server sent a hello request, we need to re-negotiate the session. */
357 #define NX_SECURE_TLS_CLIENT_STATE_HELLO_VERIFY         4  /* A HelloVerifyRequest was received - need to re-send ClientHello (DTLS). */
358 #define NX_SECURE_TLS_CLIENT_STATE_SERVERHELLO          5  /* A ServerHello has been received. */
359 #define NX_SECURE_TLS_CLIENT_STATE_SERVER_CERTIFICATE   6  /* A Server Certificate message has been received. */
360 #define NX_SECURE_TLS_CLIENT_STATE_SERVER_KEY_EXCHANGE  7  /* A ServerKeyExchange message has been received. */
361 #define NX_SECURE_TLS_CLIENT_STATE_CERTIFICATE_REQUEST  8  /* A Server CertificateRequest message has been received. */
362 #define NX_SECURE_TLS_CLIENT_STATE_SERVERHELLO_DONE     9  /* A ServerHelloDone message has been received. */
363 #define NX_SECURE_TLS_CLIENT_STATE_HANDSHAKE_FINISHED   10 /* Client has received a Finished message to end the handshake. */
364 #define NX_SECURE_TLS_CLIENT_STATE_RENEGOTIATING        11 /* Client is renegotiating a handshake. Only used to kick off a renegotiation. */
365 #define NX_SECURE_TLS_CLIENT_STATE_ENCRYPTED_EXTENSIONS 12 /* Client received and processed an encrypted extensions handshake message. */
366 #define NX_SECURE_TLS_CLIENT_STATE_HELLO_RETRY          13 /* A HelloRetryRequest has been received. We need to resend ClientHello. */
367 
368 #define NX_SECURE_TLS_HANDSHAKE_NO_FRAGMENT             0  /* There is no fragmented handshake message. */
369 #define NX_SECURE_TLS_HANDSHAKE_RECEIVED_FRAGMENT       1  /* Received a fragmented handshake message. */
370 
371 /* TLS Alert message numbers from RFC 5246. */
372 #define NX_SECURE_TLS_ALERT_CLOSE_NOTIFY                0
373 #define NX_SECURE_TLS_ALERT_UNEXPECTED_MESSAGE          10
374 #define NX_SECURE_TLS_ALERT_BAD_RECORD_MAC              20
375 #define NX_SECURE_TLS_ALERT_DECRYPTION_FAILED_RESERVED  21
376 #define NX_SECURE_TLS_ALERT_RECORD_OVERFLOW             22
377 #define NX_SECURE_TLS_ALERT_DECOMPRESSION_FAILURE       30
378 #define NX_SECURE_TLS_ALERT_HANDSHAKE_FAILURE           40
379 #define NX_SECURE_TLS_ALERT_NO_CERTIFICATE_RESERVED     41
380 #define NX_SECURE_TLS_ALERT_BAD_CERTIFICATE             42
381 #define NX_SECURE_TLS_ALERT_UNSUPPORTED_CERTIFICATE     43
382 #define NX_SECURE_TLS_ALERT_CERTIFICATE_REVOKED         44
383 #define NX_SECURE_TLS_ALERT_CERTIFICATE_EXPIRED         45
384 #define NX_SECURE_TLS_ALERT_CERTIFICATE_UNKNOWN         46
385 #define NX_SECURE_TLS_ALERT_ILLEGAL_PARAMETER           47
386 #define NX_SECURE_TLS_ALERT_UNKNOWN_CA                  48
387 #define NX_SECURE_TLS_ALERT_ACCESS_DENIED               49
388 #define NX_SECURE_TLS_ALERT_DECODE_ERROR                50
389 #define NX_SECURE_TLS_ALERT_DECRYPT_ERROR               51
390 #define NX_SECURE_TLS_ALERT_EXPORT_RESTRICTION_RESERVED 60
391 #define NX_SECURE_TLS_ALERT_PROTOCOL_VERSION            70
392 #define NX_SECURE_TLS_ALERT_INSUFFICIENT_SECURITY       71
393 #define NX_SECURE_TLS_ALERT_INTERNAL_ERROR              80
394 #define NX_SECURE_TLS_ALERT_INAPPROPRIATE_FALLBACK      86
395 #define NX_SECURE_TLS_ALERT_USER_CANCELED               90
396 #define NX_SECURE_TLS_ALERT_NO_RENEGOTIATION            100
397 #define NX_SECURE_TLS_ALERT_MISSING_EXTENSION           109
398 #define NX_SECURE_TLS_ALERT_UNSUPPORTED_EXTENSION       110
399 #define NX_SECURE_TLS_ALERT_UNKNOWN_PSK_IDENTITY        115
400 #define NX_SECURE_TLS_ALERT_CERTIFICATE_REQUIRED        116
401 
402 /* TLS alert levels. */
403 #define NX_SECURE_TLS_ALERT_LEVEL_WARNING               0x1
404 #define NX_SECURE_TLS_ALERT_LEVEL_FATAL                 0x2
405 
406 
407 /* TLS protocol versions - TLS version 1.2 has protocol version 3.3 (for legacy reasons). */
408 #define NX_SECURE_TLS_VERSION_MAJOR_3                   0x3
409 #define NX_SECURE_SSL_VERSION_MINOR_3_0                 0x0
410 #define NX_SECURE_TLS_VERSION_MINOR_1_0                 0x1
411 #define NX_SECURE_TLS_VERSION_MINOR_1_1                 0x2
412 #define NX_SECURE_TLS_VERSION_MINOR_1_2                 0x3
413 #define NX_SECURE_TLS_VERSION_MINOR_1_3                 0x4
414 
415 #define NX_SECURE_TLS_VERSION_SSL_3_0                   ((NX_SECURE_TLS_VERSION_MAJOR_3 << 8) | NX_SECURE_SSL_VERSION_MINOR_3_0)
416 #define NX_SECURE_TLS_VERSION_TLS_1_0                   ((NX_SECURE_TLS_VERSION_MAJOR_3 << 8) | NX_SECURE_TLS_VERSION_MINOR_1_0)
417 #define NX_SECURE_TLS_VERSION_TLS_1_1                   ((NX_SECURE_TLS_VERSION_MAJOR_3 << 8) | NX_SECURE_TLS_VERSION_MINOR_1_1)
418 #define NX_SECURE_TLS_VERSION_TLS_1_2                   ((NX_SECURE_TLS_VERSION_MAJOR_3 << 8) | NX_SECURE_TLS_VERSION_MINOR_1_2)
419 #define NX_SECURE_TLS_VERSION_TLS_1_3                   ((NX_SECURE_TLS_VERSION_MAJOR_3 << 8) | NX_SECURE_TLS_VERSION_MINOR_1_3)
420 
421 
422 /* The number of TLS versions actually recognized by the NetX Secure TLS stack. */
423 #define NX_SECURE_TLS_NUM_VERSIONS                      (4)
424 
425 /* Configuration macros for supported TLS versions. */
426 #ifdef NX_SECURE_TLS_ENABLE_SSL_3_0
427 #define NX_SECURE_TLS_SSL_3_0_ENABLED                   (1) /* SSLv3 supported. */
428 #else
429 #define NX_SECURE_TLS_SSL_3_0_ENABLED                   (0) /* SSLv3 not currently supported. */
430 #endif
431 
432 #ifdef NX_SECURE_TLS_ENABLE_TLS_1_0
433 #define NX_SECURE_TLS_TLS_1_0_ENABLED                   (1) /* TLS 1.0 supported. */
434 #else
435 #define NX_SECURE_TLS_TLS_1_0_ENABLED                   (0) /* TLS 1.0 not currently supported. */
436 #endif
437 
438 #ifdef NX_SECURE_TLS_ENABLE_TLS_1_1
439 #define NX_SECURE_TLS_TLS_1_1_ENABLED                   (1) /* TLS 1.1 supported. */
440 #else
441 #define NX_SECURE_TLS_TLS_1_1_ENABLED                   (0) /* TLS 1.1 not currently supported. */
442 #endif
443 
444 #ifndef NX_SECURE_TLS_TLS_1_2_ENABLED
445 #define NX_SECURE_TLS_TLS_1_2_ENABLED                   (1)
446 #endif
447 
448 #ifdef NX_SECURE_TLS_ENABLE_TLS_1_3
449 #define NX_SECURE_TLS_TLS_1_3_ENABLED                   (1)
450 #endif
451 
452 #ifndef NX_SECURE_TLS_TLS_1_3_ENABLED
453 #define NX_SECURE_TLS_TLS_1_3_ENABLED                   (0)
454 #endif
455 
456 
457 /* Define a structure to keep track of which versions of TLS are enabled and supported. */
458 typedef struct NX_SECURE_TLS_VERSIONS_STRUCT
459 {
460     /* The protocol version in network byte-order format for use in TLS messages. */
461     USHORT nx_secure_tls_protocol_version;
462 
463     /* Flag indicating that the associated TLS protocol version is supported/enabled. */
464     USHORT nx_secure_tls_is_supported;
465 } NX_SECURE_TLS_VERSIONS;
466 
467 /* Define a structure to keep track of supported versions for TLS and DTLS */
468 typedef struct NX_SECURE_VERSIONS_LIST_STRUCT
469 {
470     const NX_SECURE_TLS_VERSIONS *nx_secure_versions_list;
471     UINT                          nx_secure_versions_list_count;
472 } NX_SECURE_VERSIONS_LIST;
473 
474 
475 /* Disambiguation label/id for ciphersuites table. */
476 #define NX_SECURE_APPLICATION_NONE                         0
477 #define NX_SECURE_APPLICATION_TLS                          1
478 #define NX_SECURE_APPLICATION_X509                         2
479 
480 /* Bitfields for TLS versions. */
481 #define NX_SECURE_TLS_BITFIELD_VERSION_1_0                 0x00000001
482 #define NX_SECURE_TLS_BITFIELD_VERSION_1_1                 0x00000002
483 #define NX_SECURE_TLS_BITFIELD_VERSION_1_2                 0x00000004
484 #define NX_SECURE_TLS_BITFIELD_VERSION_1_3                 0x00000008
485 #define NX_SECURE_DTLS_BITFIELD_VERSION_1_0                0x00000010
486 #define NX_SECURE_DTLS_BITFIELD_VERSION_1_2                0x00000020
487 #define NX_SECURE_DTLS_BITFIELD_VERSION_1_3                0x00000040
488 #define NX_SECURE_X509_BITFIELD_VERSION_3                  0x00000080
489 
490 /* Composite bitfields. */
491 #define NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3            (NX_SECURE_TLS_BITFIELD_VERSION_1_0 | NX_SECURE_TLS_BITFIELD_VERSION_1_1 | NX_SECURE_TLS_BITFIELD_VERSION_1_2)
492 #define NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3           (NX_SECURE_DTLS_BITFIELD_VERSION_1_0 | NX_SECURE_DTLS_BITFIELD_VERSION_1_2)
493 #define NX_SECURE_TLS_BITFIELD_VERSIONS_ALL                (NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_TLS_BITFIELD_VERSION_1_3 | NX_SECURE_DTLS_BITFIELD_VERSION_1_3)
494 
495 /* TLS ciphersuite definitions. */
496 #define TLS_NULL_WITH_NULL_NULL                            0x0000
497 #define TLS_RSA_WITH_NULL_MD5                              0x0001
498 #define TLS_RSA_WITH_NULL_SHA                              0x0002
499 #define TLS_RSA_WITH_AES_128_CBC_SHA                       0x002F
500 #define TLS_DH_DSS_WITH_AES_128_CBC_SHA                    0x0030
501 #define TLS_DH_RSA_WITH_AES_128_CBC_SHA                    0x0031
502 #define TLS_DHE_DSS_WITH_AES_128_CBC_SHA                   0x0032
503 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA                   0x0033
504 #define TLS_DH_anon_WITH_AES_128_CBC_SHA                   0x0034
505 #define TLS_RSA_WITH_AES_256_CBC_SHA                       0x0035
506 #define TLS_DH_DSS_WITH_AES_256_CBC_SHA                    0x0036
507 #define TLS_DH_RSA_WITH_AES_256_CBC_SHA                    0x0037
508 #define TLS_DHE_DSS_WITH_AES_256_CBC_SHA                   0x0038
509 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA                   0x0039
510 #define TLS_DH_anon_WITH_AES_256_CBC_SHA                   0x003A
511 #define TLS_RSA_WITH_AES_128_CBC_SHA256                    0x003C
512 #define TLS_RSA_WITH_AES_256_CBC_SHA256                    0x003D
513 #define TLS_PSK_WITH_AES_128_CBC_SHA                       0x008C
514 #define TLS_PSK_WITH_AES_256_CBC_SHA                       0x008D
515 #define TLS_RSA_WITH_AES_128_GCM_SHA256                    0x009C
516 #define TLS_RSA_WITH_AES_256_GCM_SHA384                    0x009D
517 #define TLS_PSK_WITH_AES_128_CBC_SHA256                    0x00AE
518 #define TLS_PSK_WITH_AES_128_CCM_8                         0xC0A8
519 
520 /* EC Ciphersuites. */
521 #define TLS_ECDH_ECDSA_WITH_NULL_SHA                       0xC001
522 #define TLS_ECDH_ECDSA_WITH_RC4_128_SHA                    0xC002
523 #define TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA               0xC003
524 #define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA                0xC004
525 #define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA                0xC005
526 #define TLS_ECDHE_ECDSA_WITH_NULL_SHA                      0xC006
527 #define TLS_ECDHE_ECDSA_WITH_RC4_128_SHA                   0xC007
528 #define TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA              0xC008
529 #define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA               0xC009
530 #define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA               0xC00A
531 #define TLS_ECDH_RSA_WITH_NULL_SHA                         0xC00B
532 #define TLS_ECDH_RSA_WITH_RC4_128_SHA                      0xC00C
533 #define TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA                 0xC00D
534 #define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA                  0xC00E
535 #define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA                  0xC00F
536 #define TLS_ECDHE_RSA_WITH_NULL_SHA                        0xC010
537 #define TLS_ECDHE_RSA_WITH_RC4_128_SHA                     0xC011
538 #define TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA                0xC012
539 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA                 0xC013
540 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA                 0xC014
541 #define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256            0xC023
542 #define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384            0xC024
543 #define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256             0xC025
544 #define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384             0xC026
545 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256              0xC027
546 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384              0xC028
547 #define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256               0xC029
548 #define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384               0xC02A
549 #define TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256            0xC02B
550 #define TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384            0xC02C
551 #define TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256             0xC02D
552 #define TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384             0xC02E
553 #define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256              0xC02F
554 #define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384              0xC030
555 #define TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256               0xC031
556 #define TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384               0xC032
557 
558 #define TLS_AES_128_GCM_SHA256                             0x1301
559 #define TLS_AES_256_GCM_SHA384                             0x1302
560 #define TLS_AES_128_CCM_SHA256                             0x1304
561 #define TLS_AES_128_CCM_8_SHA256                           0x1305
562 
563 #define TLS_EMPTY_RENEGOTIATION_INFO_SCSV                  0x00FF /* Secure renegotiation extension Signalling Ciphersuite Value (SCSV). */
564 #define TLS_FALLBACK_NOTIFY_SCSV                           0x5600 /* TLS version fallback notification SCSV. */
565 
566 /* Cipher and hash methods for look up in determining what methods to use for a particular ciphersuite. */
567 #define TLS_CIPHER_NULL                                    0x00
568 #define TLS_CIPHER_AES                                     0x01
569 #define TLS_CIPHER_RC4                                     0x02
570 #define TLS_ECJPAKE_WITH_AES_128_CCM_8                     0xC0FF
571 
572 /* Define the key exchange algorithm for backward compatibility. */
573 #define TLS_CIPHER_RSA                                     NX_CRYPTO_KEY_EXCHANGE_RSA
574 #define TLS_PUBLIC_AUTH_PSK                                NX_CRYPTO_KEY_EXCHANGE_PSK
575 #define TLS_PUBLIC_AUTH_ECJPAKE                            NX_CRYPTO_KEY_EXCHANGE_ECJPAKE
576 #define TLS_PRF_1                                          NX_CRYPTO_PRF_HMAC_SHA1 /* TLSv1.0/1.1 PRF */
577 #define TLS_PRF_SHA_256                                    NX_CRYPTO_PRF_HMAC_SHA2_256 /* TLS PRF, SHA-256 based for TLSv1.2. */
578 #define TLS_HASH_SHA_1                                     NX_CRYPTO_HASH_SHA1
579 #define TLS_HASH_SHA_256                                   NX_CRYPTO_HASH_SHA256
580 #define TLS_HASH_SHA_384                                   NX_CRYPTO_HASH_SHA384
581 #define TLS_HASH_SHA_512                                   NX_CRYPTO_HASH_SHA512
582 
583 /* Client Certificate Types for Certificate Request messages.
584    Values taken directly from RFC 5246, section 7.4.4. */
585 #define NX_SECURE_TLS_CERT_TYPE_NONE                       0
586 #define NX_SECURE_TLS_CERT_TYPE_RSA_SIGN                   1
587 #define NX_SECURE_TLS_CERT_TYPE_DSS_SIGN                   2
588 #define NX_SECURE_TLS_CERT_TYPE_RSA_FIXED_DH               3
589 #define NX_SECURE_TLS_CERT_TYPE_DSS_FIXED_DH               4
590 #define NX_SECURE_TLS_CERT_TYPE_RSA_EPHEMERAL_DH           5
591 #define NX_SECURE_TLS_CERT_TYPE_DSS_EPHEMERAL_DH           6
592 #define NX_SECURE_TLS_CERT_TYPE_FORTEZZA_DMS               20
593 /* Values taken directly from RFC 4492, section 5.5. */
594 #define NX_SECURE_TLS_CERT_TYPE_ECDSA_SIGN                 64
595 #define NX_SECURE_TLS_CERT_TYPE_RSA_FIXED_ECDH             65
596 #define NX_SECURE_TLS_CERT_TYPE_ECDSA_FIXED_ECDH           66
597 
598 
599 /* Algorithm identifiers for signature methods used in CertificateVerify messages and
600    in the "signature_algorithms" extension, from RFC 5246, section 7.4.1.4.1.*/
601 #define NX_SECURE_TLS_HASH_ALGORITHM_NONE                  0
602 #define NX_SECURE_TLS_HASH_ALGORITHM_MD5                   1
603 #define NX_SECURE_TLS_HASH_ALGORITHM_SHA1                  2
604 #define NX_SECURE_TLS_HASH_ALGORITHM_SHA224                3
605 #define NX_SECURE_TLS_HASH_ALGORITHM_SHA256                4
606 #define NX_SECURE_TLS_HASH_ALGORITHM_SHA384                5
607 #define NX_SECURE_TLS_HASH_ALGORITHM_SHA512                6
608 
609 /* Signature algorithms paired with the hash algorithms above. */
610 #define NX_SECURE_TLS_SIGNATURE_ALGORITHM_ANONYMOUS        0
611 #define NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA              1
612 #define NX_SECURE_TLS_SIGNATURE_ALGORITHM_DSA              2
613 #define NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA            3
614 
615 /* Packed algorithm values (as seen over the wire). */
616 #define NX_SECURE_TLS_SIGNATURE_RSA_MD5                    (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_MD5 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA)
617 #define NX_SECURE_TLS_SIGNATURE_RSA_SHA1                   (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA1 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA)
618 #define NX_SECURE_TLS_SIGNATURE_RSA_SHA256                 (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA256 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA)
619 #define NX_SECURE_TLS_SIGNATURE_RSA_SHA384                 (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA384 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA)
620 #define NX_SECURE_TLS_SIGNATURE_RSA_SHA512                 (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA512 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_RSA)
621 #define NX_SECURE_TLS_SIGNATURE_ECDSA_SHA1                 (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA1 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA)
622 #define NX_SECURE_TLS_SIGNATURE_ECDSA_SHA224               (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA224 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA)
623 #define NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256               (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA256 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA)
624 #define NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384               (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA384 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA)
625 #define NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512               (((UINT)NX_SECURE_TLS_HASH_ALGORITHM_SHA512 << 8) + (UINT)NX_SECURE_TLS_SIGNATURE_ALGORITHM_ECDSA)
626 
627 
628 /* Session key generation and assignment constants. */
629 #define NX_SECURE_TLS_KEY_SET_LOCAL                        0
630 #define NX_SECURE_TLS_KEY_SET_REMOTE                       1
631 
632 /* TLS extension definitions from RFC 5246, 5746, 6066, and others. */
633 #define NX_SECURE_TLS_EXTENSION_SERVER_NAME_INDICATION     (0x0000)
634 #define NX_SECURE_TLS_EXTENSION_MAX_FRAGMENT_LENGTH        (0x0001)
635 #define NX_SECURE_TLS_EXTENSION_CLIENT_CERTIFICATE_URL     (0x0002)
636 #define NX_SECURE_TLS_EXTENSION_TRUSTED_CA_INDICATION      (0x0003)
637 #define NX_SECURE_TLS_EXTENSION_TRUNCATED_HMAC             (0x0004)
638 #define NX_SECURE_TLS_EXTENSION_CERTIFICATE_STATUS_REQUEST (0x0005)
639 #define NX_SECURE_TLS_EXTENSION_EC_GROUPS                  (0x000A)
640 #define NX_SECURE_TLS_EXTENSION_EC_POINT_FORMATS           (0x000B)
641 #define NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS       (0x000D)
642 #define NX_SECURE_TLS_EXTENSION_PRE_SHARED_KEY             (0x0029)
643 #define NX_SECURE_TLS_EXTENSION_EARLY_DATA                 (0x002A)
644 #define NX_SECURE_TLS_EXTENSION_SUPPORTED_VERSIONS         (0x002B)
645 #define NX_SECURE_TLS_EXTENSION_COOKIE                     (0x002C)
646 #define NX_SECURE_TLS_EXTENSION_PSK_KEY_EXCHANGE_MODES     (0x002D)
647 #define NX_SECURE_TLS_EXTENSION_CERTIFICATE_AUTHORITIES    (0x002F)
648 #define NX_SECURE_TLS_EXTENSION_OID_FILTERS                (0x0030)
649 #define NX_SECURE_TLS_EXTENSION_POST_HANDSHAKE_AUTH        (0x0031)
650 #define NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS_CERT  (0x0032)
651 #define NX_SECURE_TLS_EXTENSION_KEY_SHARE                  (0x0033)
652 #define NX_SECURE_TLS_EXTENSION_ECJPAKE_KEY_KP_PAIR        (0x0100)
653 #define NX_SECURE_TLS_EXTENSION_SECURE_RENEGOTIATION       (0xFF01)
654 
655 /* Extension-specific values. */
656 #define NX_SECURE_TLS_SNI_NAME_TYPE_DNS                    (0x0)
657 
658 /* Define the maximum number of structures allocated for TLS ClientHello and ServerHello extension data. */
659 #define NX_SECURE_TLS_HELLO_EXTENSIONS_MAX                 (10)
660 
661 /* Some constants for use in defining buffers for crypto and hash operations. */
662 #define NX_SECURE_TLS_RANDOM_SIZE                          (32)  /* Size of the server and client random values, in bytes. */
663 #define NX_SECURE_TLS_MAX_HASH_SIZE                        (32)  /* This is the largest size a single hash/MAC for ANY session *might* be, in bytes. */
664 #define NX_SECURE_TLS_1_3_MAX_TRANSCRIPT_HASHES            (5)   /* This is the number of transcript hashes we need to save for TLS 1.3 key generation. */
665 
666 /* The following #defines are indicies into the transcript hash array used to store
667    the various transcript hashes for TLS 1.3 key generation. */
668 #define NX_SECURE_TLS_TRANSCRIPT_IDX_CLIENTHELLO           (0)
669 #define NX_SECURE_TLS_TRANSCRIPT_IDX_SERVERHELLO           (1)
670 #define NX_SECURE_TLS_TRANSCRIPT_IDX_CERTIFICATE           (2)
671 #define NX_SECURE_TLS_TRANSCRIPT_IDX_CLIENT_FINISHED       (3)
672 #define NX_SECURE_TLS_TRANSCRIPT_IDX_SERVER_FINISHED       (4)
673 
674 #define NX_SECURE_TLS_RSA_PREMASTER_SIZE                   (48)  /* The size of RSA encrypted pre-master secret. */
675 #define NX_SECURE_TLS_EC_PREMASTER_SIZE                    (68)  /* The size of pre-master secret for EC. */
676 #ifndef NX_SECURE_TLS_MASTER_SIZE
677 #define NX_SECURE_TLS_MASTER_SIZE                          (48)  /* The master secret is also 48 bytes. */
678 #endif
679 #define NX_SECURE_TLS_MAX_KEY_SIZE                         (32)  /* Maximum size of a session key in bytes. */
680 #define NX_SECURE_TLS_MAX_IV_SIZE                          (16)  /* Maximum size of a session initialization vector in bytes. */
681 #define NX_SECURE_TLS_SESSION_ID_SIZE                      (256) /* Maximum size of a session ID value used for renegotiation in bytes. */
682 #define NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE                 (2)   /* Size of sequence numbers for TLS records in 32-bit words. */
683 #define NX_SECURE_TLS_RECORD_HEADER_SIZE                   (5)   /* Size of the TLS record header in bytes. */
684 #define NX_SECURE_TLS_HANDSHAKE_HEADER_SIZE                (4)   /* Size of the TLS handshake record header in bytes. */
685 #define NX_SECURE_TLS_FINISHED_HASH_SIZE                   (12)  /* Size of the TLS handshake Finished hash in bytes. If SSLv3 is added, the hash size will need to
686                                                                     be revisited because it is different. */
687 #define NX_SECURE_TLS_MAX_CIPHER_BLOCK_SIZE                (128) /* Size of the largest block used by session ciphers (in block mode). */
688 
689 #define NX_SECURE_TLS_MAX_SESSION_TICKET_AGE               (604800) /* Maximum lifetime of a NewSessionTicket (in milliseconds). */
690 
691 #define NX_SECURE_TLS_MAX_CIPHERTEXT_LENGTH                (18432) /* Maximum TLSCiphertext record length. */
692 #define NX_SECURE_TLS_MAX_CIPHERTEXT_LENGTH_1_3            (16640) /* Maximum TLSCiphertext record length of TLS 1.3. */
693 #define NX_SECURE_TLS_MAX_PLAINTEXT_LENGTH                 (16384) /* Maximum TLSPlaintext record length. */
694 
695 /* The minimum size for the TLS message buffer is determined by a number of factors, but primarily
696  * the expected size of the TLS handshake Certificate message (sent by the TLS server) that may
697  * contain multiple certificates of 1-2KB each. The upper limit is determined by the length field
698  * in the TLS header (16 bit), and is 64KB.
699  */
700 #ifndef NX_SECURE_TLS_MINIMUM_MESSAGE_BUFFER_SIZE
701 #define NX_SECURE_TLS_MINIMUM_MESSAGE_BUFFER_SIZE          (4000)
702 #endif
703 
704 /* Define a minimum reasonable size for a TLS X509 certificate. This is used in checking for
705  * errors in allocating certificate space. The size is determined by assuming a 512-bit RSA
706  * key, MD5 hash, and a rough estimate of other data. It is theoretically possible for a real
707  * certificate to be smaller, but in that case, bypass the error checking by re-defining this
708  * macro.
709  *    Approximately: 64(RSA) + 16(MD5) + 176(ASN.1 + text data, common name, etc)
710  */
711 #ifndef NX_SECURE_TLS_MINIMUM_CERTIFICATE_SIZE
712 #define NX_SECURE_TLS_MINIMUM_CERTIFICATE_SIZE               (256)
713 #endif
714 
715 /* We store the key material in a single contiguous block in the TLS control block, using offsets to
716  * get the actual key values. We need to size the key material according to the maximum amount of
717  * key material needed by any of the supported ciphersuites, times 2 because there are separate keys for
718  * client and server. */
719 #ifndef NX_SECURE_TLS_KEY_MATERIAL_SIZE
720 #define NX_SECURE_TLS_KEY_MATERIAL_SIZE                    (2 * (NX_SECURE_TLS_MAX_HASH_SIZE + NX_SECURE_TLS_MAX_KEY_SIZE + NX_SECURE_TLS_MAX_IV_SIZE))
721 #endif
722 
723 /* PSK-specific defines. If PSK is disabled, don't bring PSK types into the build. */
724 #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || (NX_SECURE_TLS_TLS_1_3_ENABLED)
725 
726 
727 #ifndef NX_SECURE_TLS_MAX_PSK_SIZE
728 /* The maximum PSK size for TLS 1.3 must be greater than or equal to the largest possible hash output for PSK session resumption keys. */
729 #define NX_SECURE_TLS_MAX_PSK_SIZE                         (64)
730 #endif /* NX_SECURE_TLS_MAX_PSK_SIZE */
731 
732 #ifndef NX_SECURE_TLS_MAX_PSK_KEYS
733 #define NX_SECURE_TLS_MAX_PSK_KEYS                         (5)
734 #endif /* NX_SECURE_TLS_MAX_PSK_KEYS */
735 
736 #ifndef NX_SECURE_TLS_MAX_PSK_ID_SIZE
737 #define NX_SECURE_TLS_MAX_PSK_ID_SIZE                      (20)
738 #endif /* NX_SECURE_TLS_MAX_PSK_ID_SIZE */
739 
740 #ifndef NX_SECURE_TLS_MAX_PSK_NONCE_SIZE
741 #define NX_SECURE_TLS_MAX_PSK_NONCE_SIZE                   (255)
742 #endif
743 
744 /* The pre-master secret size should be at least (2 * NX_SECURE_TLS_MAX_PSK_SIZE + 4) bytes for PSK cipher suites. */
745 #define NX_SECURE_TLS_MIN_PREMASTER_SIZE_PSK               (2 * NX_SECURE_TLS_MAX_PSK_SIZE + 4)
746 
747 /* This structure holds the data for Pre-Shared Keys (PSKs) for use with
748    the TLS PSK ciphersuites. The actual keys are generated from this data
749    as part of the TLS handshake, but the user must provide this seed and
750    an "identity" to match a remote host to a known key. */
751 typedef struct NX_SECURE_TLS_PSK_STORE_STRUCT
752 {
753     /* This holds the actual key data for the PSK. */
754     UCHAR nx_secure_tls_psk_data[NX_SECURE_TLS_MAX_PSK_SIZE];
755     UINT  nx_secure_tls_psk_data_size;
756 
757     /* This holds the identity information for the key in this PSK entry. */
758     UCHAR nx_secure_tls_psk_id[NX_SECURE_TLS_MAX_PSK_ID_SIZE];
759     UINT  nx_secure_tls_psk_id_size;
760 
761     /* This holds the "identity hint" sent to a TLS server during the handshake.
762        The hint tells the server how to choose a PSK/identity pair. */
763     UCHAR nx_secure_tls_psk_id_hint[NX_SECURE_TLS_MAX_PSK_ID_SIZE];
764     UINT  nx_secure_tls_psk_id_hint_size;
765 
766 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
767     /* TLS 1.3 session ticket data. */
768     UINT  nx_secure_tls_psk_ticket_lifetime;
769     UCHAR nx_secure_tls_psk_ticket_nonce[NX_SECURE_TLS_MAX_PSK_NONCE_SIZE];
770     UCHAR  nx_secure_tls_psk_ticket_nonce_size;
771 
772     /* PSK early secret. */
773     UCHAR nx_secure_tls_psk_early_secret[NX_SECURE_TLS_MAX_PSK_SIZE];
774     UINT  nx_secure_tls_psk_early_secret_size;
775 
776     /* PSK binder key. */
777     UCHAR nx_secure_tls_psk_binder_key[NX_SECURE_TLS_MAX_PSK_SIZE];
778     UINT  nx_secure_tls_psk_binder_key_size;
779 
780     /* PSK finished binder key. */
781     UCHAR nx_secure_tls_psk_finished_key[NX_SECURE_TLS_MAX_PSK_SIZE];
782     UINT  nx_secure_tls_psk_finished_key_size;
783 
784     /* PSK binder value. */
785     UCHAR nx_secure_tls_psk_binder[NX_SECURE_TLS_MAX_PSK_SIZE];
786     UINT  nx_secure_tls_psk_binder_size;
787 
788 
789 
790     /* The PSK is associated with a hash routine to generate the binder.
791      * If the PSK is for session resumption, the hash is that of the original
792      * handshake's chosen ciphersuite. For user-defined PSKs, the hash is
793      * either chosen by the user or defaults to SHA-256. RFC 8446, Section 4.2.11.
794      */
795     const struct NX_SECURE_TLS_CIPHERSUITE_INFO_STRUCT *nx_secure_tls_psk_binder_ciphersuite;
796 
797 #endif
798 } NX_SECURE_TLS_PSK_STORE;
799 #endif /* defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) */
800 
801 
802 #ifndef NX_SECURE_TLS_PREMASTER_SIZE
803 
804 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
805 #define NX_SECURE_TLS_MIN_PREMASTER_SIZE                   NX_SECURE_TLS_EC_PREMASTER_SIZE  /* The pre-master secret should be at least 66 bytes for ECDH/ECDHE with secp521r1. */
806 #else
807 #define NX_SECURE_TLS_MIN_PREMASTER_SIZE                   NX_SECURE_TLS_RSA_PREMASTER_SIZE /* The pre-master secret should be at least 48 bytes. */
808 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
809 
810 #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE)
811 
812 #if NX_SECURE_TLS_MIN_PREMASTER_SIZE_PSK > NX_SECURE_TLS_MIN_PREMASTER_SIZE
813 #define NX_SECURE_TLS_PREMASTER_SIZE                       NX_SECURE_TLS_MIN_PREMASTER_SIZE_PSK /* The pre-master secret should be at least NX_SECURE_TLS_MIN_PREMASTER_SIZE_PSK bytes for PSK cipher suites. */
814 #else
815 #define NX_SECURE_TLS_PREMASTER_SIZE                       NX_SECURE_TLS_MIN_PREMASTER_SIZE
816 #endif
817 
818 #else
819 #define NX_SECURE_TLS_PREMASTER_SIZE                       NX_SECURE_TLS_MIN_PREMASTER_SIZE
820 #endif /* defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || (NX_SECURE_TLS_TLS_1_3_ENABLED) */
821 
822 #endif
823 
824 /* TLS Ciphersuite lookup table. Contains all pertinent information for ciphersuites used in TLS operations.
825  * The lookup is based on the first field, which will contain the defined TLS value for the ciphersuite. */
826 typedef struct NX_SECURE_TLS_CIPHERSUITE_INFO_STRUCT
827 {
828     /* The value of the ciphersuite "on the wire" as defined by the TLS spec. */
829     USHORT nx_secure_tls_ciphersuite;
830 
831     /* The Public Key operation in this suite - RSA or DH. */
832     const NX_CRYPTO_METHOD *nx_secure_tls_public_cipher;
833 
834     /* The Public Authentication method used for signing data. */
835     const NX_CRYPTO_METHOD *nx_secure_tls_public_auth;
836 
837     /* NOTE: The Public Key size is determined by the public keys used and cannot be determined at compile time. */
838 
839     /* The session cipher being used - AES, RC4, etc. */
840     const NX_CRYPTO_METHOD *nx_secure_tls_session_cipher;
841 
842     /* The size of the initialization vectors needed for the session cipher. N/A for all session ciphers (enter "NONE"). */
843     USHORT nx_secure_tls_iv_size;
844 
845     /* The key size for the session cipher. */
846     UCHAR nx_secure_tls_session_key_size;
847 
848     /* The hash being used - MD5, SHA-1, SHA-256, etc. */
849     const NX_CRYPTO_METHOD *nx_secure_tls_hash;
850 
851     /* The size of the hash being used. This is for convenience as the size is determined
852        by the hash, e.g. SHA-1 is 20 bytes, MD5 is 16 bytes. */
853     USHORT nx_secure_tls_hash_size;
854 
855     /* The TLS PRF being used - for TLSv1.0 and TLSv1.1 this is a single function. For TLSv1.2,
856        the PRF is determined by the ciphersuite. */
857     const NX_CRYPTO_METHOD *nx_secure_tls_prf;
858 
859 } NX_SECURE_TLS_CIPHERSUITE_INFO;
860 
861 
862 typedef USHORT NX_SECURE_TLS_STATE;
863 typedef USHORT NX_SECURE_TLS_SERVER_STATE;
864 typedef USHORT NX_SECURE_TLS_CLIENT_STATE;
865 
866 
867 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
868 /* EC handshake information. */
869 typedef struct NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA_STRUCT
870 {
871     /* Named curve used. */
872     UINT nx_secure_tls_ecdhe_named_curve;
873 
874     /* Signature Algorithm for ECDHE. */
875     USHORT nx_secure_tls_ecdhe_signature_algorithm;
876 
877     /* Length of the private key. */
878     USHORT nx_secure_tls_ecdhe_private_key_length;
879 
880     /* Private key for ECDHE. */
881     UCHAR nx_secure_tls_ecdhe_private_key[NX_SECURE_TLS_EC_PREMASTER_SIZE];
882 
883     /* Length of the public key. */
884     USHORT nx_secure_tls_ecdhe_public_key_length;
885 
886     /* Public key for ECDHE. */
887     UCHAR nx_secure_tls_ecdhe_public_key[4 * NX_SECURE_TLS_EC_PREMASTER_SIZE];
888 
889 } NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA;
890 
891 /* ECC information. */
892 typedef struct NX_SECURE_TLS_ECC_STRUCT
893 {
894     /* Supported named curves. */
895     const USHORT *nx_secure_tls_ecc_supported_groups;
896 
897     /* Number of supported named curves. */
898     USHORT  nx_secure_tls_ecc_supported_groups_count;
899 
900     /* Corresponding crypto methods for the supported named curve. */
901     const NX_CRYPTO_METHOD **nx_secure_tls_ecc_curves;
902 } NX_SECURE_TLS_ECC;
903 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
904 
905 
906 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
907 typedef struct NX_SECURE_TLS_KEY_SECRETS_STRUCT
908 {
909     /* TLS 1.3 has many secrets and keys to be generated. This structure contains them. */
910 
911     UCHAR tls_early_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
912     UINT  tls_early_secret_len;
913     UCHAR tls_binder_key[NX_SECURE_TLS_MAX_HASH_SIZE];
914     UINT  tls_binder_key_len;
915     UCHAR tls_client_early_traffic_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
916     UINT  tls_client_early_traffic_secret_len;
917     UCHAR tls_early_exporter_master_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
918     UINT  tls_early_exporter_master_secret_len;
919     UCHAR tls_handshake_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
920     UINT  tls_handshake_secret_len;
921     UCHAR tls_client_handshake_traffic_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
922     UINT  tls_client_handshake_traffic_secret_len;
923     UCHAR tls_server_handshake_traffic_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
924     UINT  tls_server_handshake_traffic_secret_len;
925     UCHAR tls_master_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
926     UINT  tls_master_secret_len;
927     UCHAR tls_client_application_traffic_secret_0[NX_SECURE_TLS_MAX_HASH_SIZE];
928     UINT  tls_client_application_traffic_secret_0_len;
929     UCHAR tls_server_application_traffic_secret_0[NX_SECURE_TLS_MAX_HASH_SIZE];
930     UINT  tls_server_application_traffic_secret_0_len;
931     UCHAR tls_exporter_master_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
932     UINT  tls_exporter_master_secret_len;
933     UCHAR tls_resumption_master_secret[NX_SECURE_TLS_MAX_HASH_SIZE];
934     UINT  tls_resumption_master_secret_len;
935 
936     UCHAR tls_server_finished_key[NX_SECURE_TLS_MAX_HASH_SIZE];
937     UINT  tls_server_finished_key_len;
938     UCHAR tls_client_finished_key[NX_SECURE_TLS_MAX_HASH_SIZE];
939     UINT  tls_client_finished_key_len;
940 } NX_SECURE_TLS_KEY_SECRETS;
941 
942 
943 #endif
944 
945 
946 typedef struct NX_SECURE_TLS_KEY_MATERIAL_STRUCT
947 {
948     /* Client random bytes - generated during the handshake. */
949     UCHAR nx_secure_tls_client_random[NX_SECURE_TLS_RANDOM_SIZE];
950 
951     /* Server random bytes - generated during the handshake. */
952     UCHAR nx_secure_tls_server_random[NX_SECURE_TLS_RANDOM_SIZE];
953 
954     /* The pre-master-secret length is dependent upon the public key
955        algorithm chosen - the RSA pre-master-secret is 48 bytes.
956        THIS MUST BE DELETED FROM MEMORY ONCE KEYS ARE GENERATED. */
957     UCHAR nx_secure_tls_pre_master_secret[NX_SECURE_TLS_PREMASTER_SIZE];
958     UINT  nx_secure_tls_pre_master_secret_size;
959 
960     /* The master secret is always 48 bytes in length, regardless of the
961        length of the pre-master-secret. */
962     UCHAR nx_secure_tls_master_secret[NX_SECURE_TLS_MASTER_SIZE];
963 
964     /* We store generate the session key material into this buffer,
965        thus needing no copying of data (using the pointers to actual data below). */
966     UCHAR nx_secure_tls_key_material_data[NX_SECURE_TLS_KEY_MATERIAL_SIZE];
967 
968     /* During a session renegotiation, there will be the current set of session keys
969      * in use, and a new set of keys that will be generated during the renegotiation
970      * handshake. However, there is a period of time where the local and remote keys
971      * are out of sync (after a ChangeCipherSpec is sent/received but before the second
972      * CCS message is sent) so we need to keep a separate buffer for new keys until
973      * we are fully using the new keys.
974      */
975     UCHAR nx_secure_tls_new_key_material_data[NX_SECURE_TLS_KEY_MATERIAL_SIZE];
976 
977     /* Storage space for public ECC key data for curves supported (mostly for client).
978      * For TLS 1.3 we have to generate public keys before sending the ClientHello - one
979      * key for each curve we support!
980      */
981 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
982     /* TLS 1.3 ECDHE key data. */
983     NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA nx_secure_tls_ecc_key_data[10];
984 
985     /* Selected ECDHE key data index. */
986     UINT nx_secure_tls_ecc_key_data_selected;
987 
988     /* TLS 1.3 key secrets. */
989     NX_SECURE_TLS_KEY_SECRETS nx_secure_tls_key_secrets;
990 
991     /* Store each transcript hash as it is generated. */
992     UCHAR nx_secure_tls_transcript_hashes[NX_SECURE_TLS_1_3_MAX_TRANSCRIPT_HASHES][NX_SECURE_TLS_MAX_HASH_SIZE];
993 
994 #endif
995 
996     /* Pointer to buffer where we can store handshake messages to hash once we know
997        the hash routine we are using. */
998     UCHAR nx_secure_tls_handshake_cache[500];
999     UINT  nx_secure_tls_handshake_cache_length;
1000 
1001     /* The TLS protocol requires a "secret" used in the hash of each message,
1002        and one secret each for client and server. */
1003     UCHAR *nx_secure_tls_client_write_mac_secret;
1004     UCHAR *nx_secure_tls_server_write_mac_secret;
1005 
1006     /* The actual TLS Session keys used to encrypt session data (e.g. using AES.).
1007        There is one key for each direction, so the client encrypts with the "client_write"
1008        key and the server decrypts incoming data using the same key. */
1009     UCHAR *nx_secure_tls_client_write_key;
1010     UCHAR *nx_secure_tls_server_write_key;
1011 
1012     /* Some algorithms used in the TLS session require initialization vectors. */
1013     UCHAR *nx_secure_tls_client_iv;
1014     UCHAR *nx_secure_tls_server_iv;
1015 
1016 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
1017     /* Any time we are switching keys (going from one encrypted context to another) we need to switch
1018      * the keys for client and server independently. These pointers allow us to refer to the next
1019      * keys to be used without having to access the key material directly.  */
1020     UCHAR *nx_secure_tls_client_next_write_key;
1021     UCHAR *nx_secure_tls_server_next_write_key;
1022 
1023     /* Some algorithms used in the TLS session require initialization vectors. */
1024     UCHAR *nx_secure_tls_client_next_iv;
1025     UCHAR *nx_secure_tls_server_next_iv;
1026 #endif
1027 
1028 } NX_SECURE_TLS_KEY_MATERIAL;
1029 
1030 /* This structure contains the metadata for the TLS handshake hash - the state
1031  * of the hash must persist through the entire handshake process so it is stored
1032  * separately from the rest of the crypto metadata.
1033  */
1034 typedef struct NX_SECURE_TLS_HANDSHAKE_HASH_STRUCT
1035 {
1036     /* Handshake verification hash context - we need MD5 and SHA-1 for TLS 1.0 and 1.1. */
1037     CHAR *nx_secure_tls_handshake_hash_md5_metadata;
1038     ULONG nx_secure_tls_handshake_hash_md5_metadata_size;
1039     VOID *nx_secure_tls_handshake_hash_md5_handler;
1040 
1041     /* SHA-1 handshake hash context. */
1042     CHAR *nx_secure_tls_handshake_hash_sha1_metadata;
1043     ULONG nx_secure_tls_handshake_hash_sha1_metadata_size;
1044     VOID *nx_secure_tls_handshake_hash_sha1_handler;
1045 
1046     /* SHA-256 handshake hash context. */
1047     CHAR *nx_secure_tls_handshake_hash_sha256_metadata;
1048     ULONG nx_secure_tls_handshake_hash_sha256_metadata_size;
1049     VOID *nx_secure_tls_handshake_hash_sha256_handler;
1050 
1051     /* Scratch metadata space for copying one of the above states when
1052        generating the final hash. */
1053     CHAR *nx_secure_tls_handshake_hash_scratch;
1054     ULONG nx_secure_tls_handshake_hash_scratch_size;
1055 } NX_SECURE_TLS_HANDSHAKE_HASH;
1056 
1057 
1058 /* Top-level structure that contains all the relevant cryptographic method
1059    information for all TLS versions. */
1060 typedef struct NX_SECURE_TLS_CRYPTO_STRUCT
1061 {
1062     /* Table that maps ciphersuites to crypto methods. */
1063     NX_SECURE_TLS_CIPHERSUITE_INFO *nx_secure_tls_ciphersuite_lookup_table;
1064     USHORT                          nx_secure_tls_ciphersuite_lookup_table_size;
1065 
1066     /* Table that maps X.509 cipher identifiers to crypto methods. */
1067 #ifndef NX_SECURE_DISABLE_X509
1068     NX_SECURE_X509_CRYPTO *nx_secure_tls_x509_cipher_table;
1069     USHORT                 nx_secure_tls_x509_cipher_table_size;
1070 #endif
1071 
1072     /* Specific routines needed for specific TLS versions. */
1073 #if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
1074     const NX_CRYPTO_METHOD *nx_secure_tls_handshake_hash_md5_method;
1075     const NX_CRYPTO_METHOD *nx_secure_tls_handshake_hash_sha1_method;
1076     const NX_CRYPTO_METHOD *nx_secure_tls_prf_1_method;
1077 #endif
1078 
1079 #if (NX_SECURE_TLS_TLS_1_2_ENABLED)
1080     const NX_CRYPTO_METHOD *nx_secure_tls_handshake_hash_sha256_method;
1081     const NX_CRYPTO_METHOD *nx_secure_tls_prf_sha256_method;
1082 #endif
1083 
1084 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
1085     const NX_CRYPTO_METHOD *nx_secure_tls_hkdf_method;
1086     const NX_CRYPTO_METHOD *nx_secure_tls_hmac_method;
1087     const NX_CRYPTO_METHOD *nx_secure_tls_ecdhe_method;
1088 #endif
1089 
1090 } NX_SECURE_TLS_CRYPTO;
1091 
1092 
1093 typedef struct NX_SECURE_TLS_CREDENTIALS_STRUCT
1094 {
1095     /* X509 certificates are enabled by default. Disable them using this macro. */
1096 #ifndef NX_SECURE_DISABLE_X509
1097     /* X509 Certificate store. */
1098     NX_SECURE_X509_CERTIFICATE_STORE nx_secure_tls_certificate_store;
1099 
1100     /* Pointer to the active local certificate, if non-NULL it overrides the store
1101        when sending out a certificate. */
1102     NX_SECURE_X509_CERT *nx_secure_tls_active_certificate;
1103 
1104 #endif
1105 
1106 #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || (NX_SECURE_TLS_TLS_1_3_ENABLED)
1107 
1108     /* Server identity value (received from remote host). */
1109     UCHAR nx_secure_tls_remote_psk_id[NX_SECURE_TLS_MAX_PSK_ID_SIZE];
1110     UINT  nx_secure_tls_remote_psk_id_size;
1111 
1112     /* Client PSK for use with a specific server. */
1113     NX_SECURE_TLS_PSK_STORE nx_secure_tls_client_psk;
1114 #endif
1115 
1116 #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) || (NX_SECURE_TLS_TLS_1_3_ENABLED)
1117     /* Store for PSK ciphersuite keys. Used for TLS servers and PSK. */
1118     NX_SECURE_TLS_PSK_STORE nx_secure_tls_psk_store[NX_SECURE_TLS_MAX_PSK_KEYS];
1119 
1120     /* Current count/index into PSK store. */
1121     UINT nx_secure_tls_psk_count;
1122 #endif /* defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) */
1123 
1124 } NX_SECURE_TLS_CREDENTIALS;
1125 
1126 /* This structure encapsulates a single extension and its associated data. The
1127    structure is used to pass opaque data in and out of the TLS stack. Helper
1128    functions are used to extract/fill extension-specific data. */
1129 typedef struct NX_SECURE_TLS_HELLO_EXTENSION_STRUCT
1130 {
1131 
1132     /* Identifier for the extension - used to identify the data in the buffer. */
1133     USHORT nx_secure_tls_extension_id;
1134 
1135     /* Length of data in the buffer. */
1136     USHORT nx_secure_tls_extension_data_length;
1137 
1138     /* Data for the extensions. Pointer to a buffer containing the data which
1139        is formatted according to the particular extension. */
1140     const UCHAR *nx_secure_tls_extension_data;
1141 } NX_SECURE_TLS_HELLO_EXTENSION;
1142 
1143 
1144 /* Definition of the top-level TLS session control block used by the application. */
1145 typedef struct NX_SECURE_TLS_SESSION_STRUCT
1146 {
1147     /* Identifier to determine if TLS session has been properly initialized. */
1148     ULONG nx_secure_tls_id;
1149 
1150     /* Underlying TCP socket. */
1151     NX_TCP_SOCKET *nx_secure_tls_tcp_socket;
1152 
1153     /* Queue the incoming packets for one record. */
1154     NX_PACKET *nx_secure_record_queue_header;
1155     NX_PACKET *nx_secure_record_decrypted_packet;
1156 
1157     /* Packet pool used by TLS stack to allocate outgoing packets used in TLS handshake. */
1158     NX_PACKET_POOL *nx_secure_tls_packet_pool;
1159 
1160     /* Packet/message buffer for re-assembling TLS messages. */
1161     UCHAR *nx_secure_tls_packet_buffer;
1162     ULONG  nx_secure_tls_packet_buffer_size;
1163     ULONG  nx_secure_tls_packet_buffer_original_size;
1164 
1165     /* The number of bytes copied into packet/message buffer. */
1166     ULONG  nx_secure_tls_packet_buffer_bytes_copied;
1167 
1168     /* The exepected number of bytes for an incoming handshake record. */
1169     ULONG  nx_secure_tls_handshake_record_expected_length;
1170 
1171     /* Whether a handshake message is fragmented across several records. */
1172     USHORT nx_secure_tls_handshake_record_fragment_state;
1173 
1174     /* The offset of current record to be processed. */
1175     ULONG  nx_secure_tls_record_offset;
1176 
1177     /* The prcessed number of bytes in current tls record. */
1178     ULONG  nx_secure_tls_bytes_processed;
1179 
1180     /* What type of socket is this? Client or server? */
1181     UINT nx_secure_tls_socket_type;
1182 
1183     /* Protocol version used for the current session. Actual version depends on
1184      * user preference and the remote host. */
1185     USHORT nx_secure_tls_protocol_version;
1186 
1187     /* TLS 1.3 doesn't use the protocol version - it's fixed to TLS 1.2 (0x0303) so
1188        we distinguish a TLS 1.3 session from others using the flag below. */
1189     USHORT nx_secure_tls_supported_versions;
1190 
1191 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
1192     UCHAR nx_secure_tls_1_3;
1193     UCHAR nx_secure_tls_1_3_supported;
1194 #endif
1195 
1196     /* This field overrides the version returned by _nx_secure_tls_newest_supported_version. */
1197     USHORT nx_secure_tls_protocol_version_override;
1198 
1199     /* The highest supported protocol version obtained through negotiation. */
1200     USHORT nx_secure_tls_negotiated_highest_protocol_version;
1201 
1202     /* State of local and remote encryption - post ChangeCipherSpec. */
1203     UCHAR nx_secure_tls_remote_session_active;
1204     UCHAR nx_secure_tls_local_session_active;
1205 
1206     /* State of whether the client and server session cipher is initialized. */
1207     UCHAR nx_secure_tls_session_cipher_client_initialized;
1208     UCHAR nx_secure_tls_session_cipher_server_initialized;
1209 
1210     /* Chosen ciphersuite. */
1211     const NX_SECURE_TLS_CIPHERSUITE_INFO *nx_secure_tls_session_ciphersuite;
1212 
1213     /* Chosen ciphersuite table, passed in during the session create call. */
1214     NX_SECURE_TLS_CRYPTO *nx_secure_tls_crypto_table;
1215 
1216     /* Key material (master secret, session keys, etc.) is stored here. */
1217     NX_SECURE_TLS_KEY_MATERIAL nx_secure_tls_key_material;
1218 
1219     /* Session ID length. */
1220     UCHAR nx_secure_tls_session_id_length;
1221 
1222     /* Session ID used for session re-negotiation. */
1223     UCHAR nx_secure_tls_session_id[NX_SECURE_TLS_SESSION_ID_SIZE];
1224 
1225 #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
1226     /* This flag indicates whether the remote host supports secure renegotiation
1227        as indicated in the initial Hello messages (SCSV or the renegotiation
1228        extension were provided). */
1229     USHORT nx_secure_tls_secure_renegotiation;
1230 
1231     /* This flag indicates whether the renegotiation_info extension is present and
1232        the data in the extension is verified during secure renegotiation. */
1233     USHORT nx_secure_tls_secure_renegotiation_verified;
1234 
1235     /* This flag indicates that a server instance has requested a renegotiation
1236        so we can differentiate between client initiated and server initiated. */
1237     USHORT nx_secure_tls_server_renegotiation_requested;
1238 
1239     /* The verify data is named "remote" and "local" since it can be used by
1240        both TLS Client and TLS Server instances. */
1241     UCHAR nx_secure_tls_remote_verify_data[NX_SECURE_TLS_FINISHED_HASH_SIZE];
1242     UCHAR nx_secure_tls_local_verify_data[NX_SECURE_TLS_FINISHED_HASH_SIZE];
1243 #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */
1244 
1245 
1246     /* Sequence number for the current TLS session - local host. */
1247     ULONG nx_secure_tls_local_sequence_number[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE];
1248 
1249     /* Sequence number for the current TLS session - remote host. For verification of incoming records. */
1250     ULONG nx_secure_tls_remote_sequence_number[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE];
1251 
1252     /* Pointer to TLS credentials for this session - stores PSKs, certificates, and other identification. */
1253     NX_SECURE_TLS_CREDENTIALS nx_secure_tls_credentials;
1254 
1255     /* Handshake hash (for the Finished message) must be maintained for all handshake messages. The
1256      * TLS version determines the actual hash being used, so all hash context data is encapsulated in
1257      * the handshake hash structure. */
1258     NX_SECURE_TLS_HANDSHAKE_HASH nx_secure_tls_handshake_hash;
1259 
1260     /* If our TLS server wishes to verify the client certificate, the application
1261        will set this to true (non-zero). */
1262     USHORT nx_secure_tls_verify_client_certificate;
1263 
1264     /* This flag will be set to true when TLS has received credentials (e.g. certificate, PSK)
1265        from the remote host. If it is still false when we get to the end of the handshake,
1266        we have not received credentials from the remote host and should fail the handshake. */
1267     USHORT nx_secure_tls_received_remote_credentials;
1268 
1269     /* This mutex used for TLS session while transmitting packets. */
1270     TX_MUTEX nx_secure_tls_session_transmit_mutex;
1271 
1272 #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
1273     /* If we receive a hello message from the remote server during a session,
1274        we have a re-negotiation handshake we need to process. */
1275     USHORT nx_secure_tls_renegotiation_handshake;
1276 
1277     /* Flag to enable/disable session renegotiation at application's choosing. */
1278     USHORT nx_secure_tls_renegotation_enabled;
1279 
1280     /* Flag to indicate that the local host initiated the renegotiation. */
1281     USHORT nx_secure_tls_local_initiated_renegotiation;
1282 #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */
1283 
1284 #ifndef NX_SECURE_TLS_SERVER_DISABLED
1285     /* The state of the server handshake if this is a server socket. */
1286     NX_SECURE_TLS_SERVER_STATE nx_secure_tls_server_state;
1287 #endif
1288 
1289 #ifndef NX_SECURE_TLS_CLIENT_DISABLED
1290     /* The state of the client handshake if this is a client socket. */
1291     NX_SECURE_TLS_CLIENT_STATE nx_secure_tls_client_state;
1292 
1293     /* If the remote TLS Server requests a certificate, save that state here so we can send the cert. */
1294     USHORT nx_secure_tls_client_certificate_requested;
1295 #endif
1296 
1297     /* Define the link between other TLS structures created by the application.  */
1298     struct NX_SECURE_TLS_SESSION_STRUCT
1299         *nx_secure_tls_created_previous,
1300         *nx_secure_tls_created_next;
1301 
1302     /* Define the public cipher metadata area. */
1303     VOID *nx_secure_public_cipher_metadata_area;
1304 
1305     /* Define the public cipher metadata size. */
1306     ULONG nx_secure_public_cipher_metadata_size;
1307 
1308     /* Define the public authentication handler. */
1309     VOID *nx_secure_public_auth_handler;
1310 
1311     /* Define the public authentication metadata area. */
1312     VOID *nx_secure_public_auth_metadata_area;
1313 
1314     /* Define the public authentication metadata size. */
1315     ULONG nx_secure_public_auth_metadata_size;
1316 
1317     /* Define the session cipher handler for client. */
1318     VOID *nx_secure_session_cipher_handler_client;
1319 
1320     /* Define the session cipher handler for server. */
1321     VOID *nx_secure_session_cipher_handler_server;
1322 
1323     /* Define the session cipher metadata area for client. */
1324     VOID *nx_secure_session_cipher_metadata_area_client;
1325 
1326     /* Define the crypto metadata area for server. */
1327     VOID *nx_secure_session_cipher_metadata_area_server;
1328 
1329     /* Define the crypto metadata size. */
1330     ULONG nx_secure_session_cipher_metadata_size;
1331 
1332     /* Define the hash Message Authentication Code (MAC) handler. */
1333     VOID *nx_secure_hash_mac_handler;
1334 
1335     /* Define the hash Message Authentication Code (MAC) metadata area. */
1336     VOID *nx_secure_hash_mac_metadata_area;
1337 
1338     /* Define the hash Message Authentication Code (MAC) metadata size. */
1339     ULONG nx_secure_hash_mac_metadata_size;
1340 
1341     /* Define the TLS PRF metadata area. */
1342     VOID *nx_secure_tls_prf_metadata_area;
1343 
1344     /* Define the TLS PRF metadata size. */
1345     ULONG nx_secure_tls_prf_metadata_size;
1346 
1347     /* Function (set by user) to call when TLS needs the current time. */
1348     ULONG (*nx_secure_tls_session_time_function)(void);
1349 
1350     /* Function (set by application) to call when TLS has a certificate from the
1351        remote host that has passed basic validation but requires additional checks
1352        by the application before being accepted. */
1353     ULONG (*nx_secure_tls_session_certificate_callback)(struct NX_SECURE_TLS_SESSION_STRUCT *session, NX_SECURE_X509_CERT *certificate);
1354 
1355 #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION
1356     /* Function (set by user) to call when TLS receives a re-negotiation request from the remote host. */
1357     ULONG (*nx_secure_tls_session_renegotiation_callback)(struct NX_SECURE_TLS_SESSION_STRUCT *session);
1358 #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */
1359 
1360     /* Function (set by user) to call when a TLS Client receives a ServerHello message containing extensions
1361        that require specific actions. */
1362     ULONG (*nx_secure_tls_session_client_callback)(struct NX_SECURE_TLS_SESSION_STRUCT *tls_session, NX_SECURE_TLS_HELLO_EXTENSION *extensions, UINT num_extensions);
1363 
1364     /* Function (set by user) to call when a TLS Server receives a ClientHello message containing extensions
1365        that require specific actions. */
1366     ULONG (*nx_secure_tls_session_server_callback)(struct NX_SECURE_TLS_SESSION_STRUCT *tls_session, NX_SECURE_TLS_HELLO_EXTENSION *extensions, UINT num_extensions);
1367 
1368 #ifndef NX_SECURE_TLS_SNI_EXTENSION_DISABLED
1369     /* Server Name Indication (SNI) extension. For TLS Client, this is a single DNS name.
1370        For TLS Server, this is unused. */
1371     NX_SECURE_X509_DNS_NAME *nx_secure_tls_sni_extension_server_name;
1372 #endif
1373 
1374     /* These are used to store off the alert value and level when an alert is recevied. */
1375     UINT nx_secure_tls_received_alert_level;
1376     UINT nx_secure_tls_received_alert_value;
1377 
1378 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
1379     /* Supported EC groups information for this session. */
1380     NX_SECURE_TLS_ECC nx_secure_tls_ecc;
1381 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
1382 
1383 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
1384     UCHAR *nx_secure_tls_cookie;
1385     UINT   nx_secure_tls_cookie_length;
1386 
1387     UINT nx_secure_tls_signature_algorithm;
1388 #endif
1389 
1390     /* Functions that can be replaced to implement custom key generation. */
1391     UINT (*nx_secure_generate_premaster_secret)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1392                                                 NX_SECURE_TLS_CREDENTIALS *tls_credentials, UINT session_type, USHORT *received_remote_credentials,
1393                                                 VOID *public_cipher_metadata, ULONG public_cipher_metadata_size, VOID *tls_ecc_curves);
1394     UINT (*nx_secure_generate_master_secret)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version,
1395                                              const NX_CRYPTO_METHOD *session_prf_method, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1396                                              UCHAR *pre_master_sec, UINT pre_master_sec_size, UCHAR *master_sec,
1397                                              VOID *prf_metadata, ULONG prf_metadata_size);
1398     UINT (*nx_secure_generate_session_keys)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version,
1399                                             const NX_CRYPTO_METHOD *session_prf_method, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1400                                             UCHAR *master_sec, VOID *prf_metadata, ULONG prf_metadata_size);
1401     UINT (*nx_secure_session_keys_set)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1402                                        UINT key_material_data_size, UINT is_client, UCHAR *session_cipher_initialized,
1403                                        VOID *session_cipher_metadata, VOID **session_cipher_handler, ULONG session_cipher_metadata_size);
1404 #ifndef NX_SECURE_TLS_CLIENT_DISABLED
1405     UINT(*nx_secure_process_server_key_exchange)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, NX_SECURE_TLS_CRYPTO *tls_crypto_table,
1406                                                  USHORT protocol_version, UCHAR *packet_buffer, UINT message_length,
1407                                                  NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1408                                                  NX_SECURE_TLS_HANDSHAKE_HASH *tls_handshake_hash,
1409                                                  VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1410                                                  VOID *public_auth_metadata, ULONG public_auth_metadata_size, VOID *tls_ecc_curves);
1411     UINT(*nx_secure_generate_client_key_exchange)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite,
1412                                                   NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1413                                                   UCHAR *data_buffer, ULONG buffer_length, ULONG *output_size,
1414                                                   VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1415                                                   VOID *public_auth_metadata, ULONG public_auth_metadata_size);
1416 #endif
1417 #ifndef NX_SECURE_TLS_SERVER_DISABLED
1418     UINT(*nx_secure_process_client_key_exchange)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version,
1419                                                  UCHAR *packet_buffer, UINT message_length, USHORT *received_remote_credentials,
1420                                                  NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1421                                                  VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1422                                                  VOID *public_auth_metadata, ULONG public_auth_metadata_size, VOID *tls_ecc_curves);
1423     UINT(*nx_secure_generate_server_key_exchange)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version, UCHAR tls_1_3,
1424                                                   NX_SECURE_TLS_CRYPTO *tls_crypto_table, NX_SECURE_TLS_HANDSHAKE_HASH *tls_handshake_hash,
1425                                                   NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1426                                                   UCHAR *data_buffer, ULONG buffer_length, ULONG *output_size,
1427                                                   VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1428                                                   VOID *public_auth_metadata, ULONG public_auth_metadata_size, VOID *tls_ecc_curves);
1429 #endif
1430     UINT (*nx_secure_verify_mac)(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, UCHAR *mac_secret, ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
1431                                  UCHAR *header_data, USHORT header_length, NX_PACKET *packet_ptr, ULONG offset, UINT *length,
1432                                  VOID *hash_mac_metadata, ULONG hash_mac_metadata_size);
1433     UINT (*nx_secure_remote_certificate_verify)(NX_SECURE_X509_CERTIFICATE_STORE *store,
1434                                                 NX_SECURE_X509_CERT *certificate, ULONG current_time);
1435     UINT (*nx_secure_trusted_certificate_add)(NX_SECURE_X509_CERTIFICATE_STORE *store,
1436                                               NX_SECURE_X509_CERT *certificate);
1437 } NX_SECURE_TLS_SESSION;
1438 
1439 /* TLS record types. */
1440 #define NX_SECURE_TLS_CHANGE_CIPHER_SPEC   20
1441 #define NX_SECURE_TLS_ALERT                21
1442 #define NX_SECURE_TLS_HANDSHAKE            22
1443 #define NX_SECURE_TLS_APPLICATION_DATA     23
1444 
1445 /* TLS handshake message values. */
1446 #define NX_SECURE_TLS_HELLO_REQUEST        0
1447 #define NX_SECURE_TLS_CLIENT_HELLO         1
1448 #define NX_SECURE_TLS_SERVER_HELLO         2
1449 #define NX_SECURE_TLS_HELLO_VERIFY_REQUEST 3
1450 #define NX_SECURE_TLS_NEW_SESSION_TICKET   4
1451 #define NX_SECURE_TLS_END_OF_EARLY_DATA    5
1452 #define NX_SECURE_TLS_ENCRYPTED_EXTENSIONS 8
1453 #define NX_SECURE_TLS_CERTIFICATE_MSG      11
1454 #define NX_SECURE_TLS_SERVER_KEY_EXCHANGE  12
1455 #define NX_SECURE_TLS_CERTIFICATE_REQUEST  13
1456 #define NX_SECURE_TLS_SERVER_HELLO_DONE    14
1457 #define NX_SECURE_TLS_CERTIFICATE_VERIFY   15
1458 #define NX_SECURE_TLS_CLIENT_KEY_EXCHANGE  16
1459 #define NX_SECURE_TLS_FINISHED             20
1460 #define NX_SECURE_TLS_CERTIFICATE_URL      21
1461 #define NX_SECURE_TLS_CERTIFICATE_STATUS   22
1462 #define NX_SECURE_TLS_KEY_UPDATE           24
1463 #define NX_SECURE_TLS_INVALID_MESSAGE      100
1464 #define NX_SECURE_TLS_MESSAGE_HASH         254
1465 
1466 
1467 /* Declare internal functions. */
1468 
1469 #ifdef NX_SECURE_KEY_CLEAR
1470 #define nx_secure_tls_packet_release _nx_secure_tls_packet_release
1471 #else
1472 #define nx_secure_tls_packet_release nx_packet_release
1473 #endif /* NX_SECURE_KEY_CLEAR */
1474 
1475 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
1476 UINT _nx_secure_tls_1_3_crypto_init(NX_SECURE_TLS_SESSION *tls_session);
1477 UINT _nx_secure_tls_1_3_client_handshake(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1478                                          UINT data_length, ULONG wait_option);
1479 UINT _nx_secure_tls_1_3_server_handshake(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1480                                          UINT data_length, ULONG wait_option);
1481 UINT _nx_secure_tls_1_3_generate_handshake_keys(NX_SECURE_TLS_SESSION *tls_session);
1482 UINT _nx_secure_tls_1_3_generate_session_keys(NX_SECURE_TLS_SESSION *tls_session);
1483 UINT _nx_secure_tls_1_3_session_psk_generate(NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_PSK_STORE *ticket_psk, UCHAR *nonce, UINT nonce_len);
1484 UINT _nx_secure_tls_psk_binder_generate(NX_SECURE_TLS_SESSION *tls_session, NX_SECURE_TLS_PSK_STORE *psk_entry);
1485 UINT _nx_secure_tls_1_3_session_keys_set(NX_SECURE_TLS_SESSION *tls_session, USHORT key_set);
1486 UINT _nx_secure_tls_1_3_transcript_hash_save(NX_SECURE_TLS_SESSION *tls_session, UINT hash_index, UINT need_copy);
1487 UINT _nx_secure_tls_1_3_finished_hash_generate(NX_SECURE_TLS_SESSION *tls_session,
1488                                                UINT is_server, UINT *hash_size, UCHAR *finished_hash,
1489                                                ULONG available_size);
1490 UINT _nx_secure_tls_1_3_generate_psk_secret(NX_SECURE_TLS_SESSION *tls_session,
1491                                             NX_SECURE_TLS_PSK_STORE *psk_entry,
1492                                             const NX_CRYPTO_METHOD *hash_method);
1493 UINT _nx_secure_tls_send_newsessionticket(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet);
1494 UINT _nx_secure_tls_process_newsessionticket(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1495                                              UINT message_length);
1496 UINT _nx_secure_tls_process_encrypted_extensions(NX_SECURE_TLS_SESSION *tls_session,
1497                                                  UCHAR *packet_buffer, UINT message_length);
1498 UINT _nx_secure_tls_send_encrypted_extensions(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet);
1499 #endif
1500 
1501 VOID _nx_secure_tls_get_signature_algorithm_id(UINT signature_algorithm, USHORT *signature_algorithm_id);
1502 UINT _nx_secure_tls_allocate_handshake_packet(NX_SECURE_TLS_SESSION *tls_session,
1503                                               NX_PACKET_POOL *packet_pool,
1504                                               NX_PACKET **send_packet, ULONG wait_option);
1505 UINT _nx_secure_tls_check_protocol_version(NX_SECURE_TLS_SESSION *tls_session,
1506                                            USHORT protocol_version, UINT id);
1507 UINT _nx_secure_tls_ciphersuite_lookup(NX_SECURE_TLS_SESSION *tls_session, UINT ciphersuite,
1508                                        const NX_SECURE_TLS_CIPHERSUITE_INFO **info, USHORT *ciphersuite_priority);
1509 UINT _nx_secure_tls_client_handshake(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1510                                      UINT data_length, ULONG wait_option);
1511 UINT _nx_secure_tls_finished_hash_generate(NX_SECURE_TLS_SESSION *tls_session,
1512                                            UCHAR *finished_label, UCHAR *finished_hash);
1513 UINT _nx_secure_tls_generate_keys(NX_SECURE_TLS_SESSION *tls_session);
1514 UINT _nx_secure_tls_generate_premaster_secret(NX_SECURE_TLS_SESSION *tls_session, UINT id);
1515 UINT _nx_secure_tls_handshake_hash_init(NX_SECURE_TLS_SESSION *tls_session);
1516 UINT _nx_secure_tls_handshake_hash_update(NX_SECURE_TLS_SESSION *tls_session, UCHAR *data,
1517                                           UINT length);
1518 UINT _nx_secure_tls_handshake_process(NX_SECURE_TLS_SESSION *tls_session, UINT wait_option);
1519 UINT _nx_secure_tls_hash_record(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite,
1520                                 ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
1521                                 UCHAR *header, UINT header_length, NX_PACKET *packet_ptr,
1522                                 ULONG offset, UINT length, UCHAR *record_hash, UINT *hash_length,
1523                                 UCHAR *mac_secret, VOID *metadata, ULONG metadata_size);
1524 UINT _nx_secure_tls_key_material_init(NX_SECURE_TLS_KEY_MATERIAL *key_material);
1525 VOID _nx_secure_tls_map_error_to_alert(UINT error_number, UINT *alert_number,
1526                                        UINT *alert_level);
1527 VOID _nx_secure_tls_newest_supported_version(NX_SECURE_TLS_SESSION *session_ptr,
1528                                              USHORT *protocol_version, UINT id);
1529 VOID _nx_secure_tls_highest_supported_version_negotiate(NX_SECURE_TLS_SESSION *session_ptr,
1530                                                         USHORT *protocol_version, UINT id);
1531 UINT _nx_secure_tls_packet_release(NX_PACKET *packet_ptr);
1532 VOID _nx_secure_tls_protocol_version_get(NX_SECURE_TLS_SESSION *session_ptr,
1533                                          USHORT *protocol_version, UINT id);
1534 UINT _nx_secure_tls_process_certificate_request(NX_SECURE_TLS_SESSION *tls_session,
1535                                                 UCHAR *packet_buffer, UINT message_length);
1536 UINT _nx_secure_tls_process_certificate_verify(NX_SECURE_TLS_SESSION *tls_session,
1537                                                UCHAR *packet_buffer, UINT message_length);
1538 UINT _nx_secure_tls_process_changecipherspec(NX_SECURE_TLS_SESSION *tls_session,
1539                                              UCHAR *packet_buffer, UINT message_length);
1540 UINT _nx_secure_tls_process_client_key_exchange(NX_SECURE_TLS_SESSION *tls_session,
1541                                                 UCHAR *packet_buffer, UINT message_length, UINT id);
1542 UINT _nx_secure_tls_process_clienthello(NX_SECURE_TLS_SESSION *tls_session,
1543                                         UCHAR *packet_buffer, UINT message_length);
1544 UINT _nx_secure_tls_process_clienthello_extensions(NX_SECURE_TLS_SESSION *tls_session,
1545                                                    UCHAR *packet_buffer, UINT message_length,
1546                                                    NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1547                                                    UINT *num_extensions, UCHAR *client_hello_buffer, UINT client_hello_length);
1548 
1549 UINT _nx_secure_tls_process_finished(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1550                                      UINT message_length);
1551 UINT _nx_secure_tls_process_header(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *packet_ptr,
1552                                    ULONG record_offset, USHORT *message_type, UINT *length,
1553                                    UCHAR *header_data, USHORT *header_length);
1554 UINT _nx_secure_tls_process_handshake_header(UCHAR *packet_buffer, USHORT *message_type,
1555                                              UINT *header_size, UINT *message_length);
1556 UINT _nx_secure_tls_process_record(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *packet_ptr,
1557                                    ULONG *bytes_processed, ULONG wait_option);
1558 UINT _nx_secure_tls_process_remote_certificate(NX_SECURE_TLS_SESSION *tls_session,
1559                                                UCHAR *packet_buffer,
1560                                                UINT message_length,
1561                                                UINT data_length);
1562 UINT _nx_secure_tls_process_server_key_exchange(NX_SECURE_TLS_SESSION *tls_session,
1563                                                 UCHAR *packet_buffer, UINT message_length);
1564 UINT _nx_secure_tls_process_serverhello(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1565                                         UINT message_length);
1566 UINT _nx_secure_tls_process_serverhello_extensions(NX_SECURE_TLS_SESSION *tls_session,
1567                                                    UCHAR *packet_buffer, UINT message_length,
1568                                                    NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1569                                                    UINT *num_extensions);
1570 UINT _nx_secure_tls_record_hash_calculate(NX_SECURE_TLS_SESSION *tls_session, UCHAR *record_hash,
1571                                           UINT *hash_length);
1572 UINT _nx_secure_tls_record_hash_initialize(NX_SECURE_TLS_SESSION *tls_session,
1573                                            ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
1574                                            UCHAR *header, UINT header_length, UINT *hash_length,
1575                                            UCHAR *mac_secret);
1576 UINT _nx_secure_tls_record_hash_update(NX_SECURE_TLS_SESSION *tls_session, UCHAR *data,
1577                                        UINT length);
1578 UINT _nx_secure_tls_record_payload_decrypt(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *encrypted_packet,
1579                                            UINT offset, UINT message_length, NX_PACKET **decrypted_packet,
1580                                            ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
1581                                            UCHAR record_type, UINT wait_option);
1582 UINT _nx_secure_tls_record_payload_encrypt(NX_SECURE_TLS_SESSION *tls_session,
1583                                            NX_PACKET *send_packet,
1584                                            ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
1585                                            UCHAR record_type);
1586 UINT _nx_secure_tls_remote_certificate_free(NX_SECURE_TLS_SESSION *tls_session,
1587                                             NX_SECURE_X509_DISTINGUISHED_NAME *name);
1588 UINT _nx_secure_tls_remote_certificate_verify(NX_SECURE_TLS_SESSION *tls_session);
1589 VOID _nx_secure_tls_send_alert(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet,
1590                                UCHAR alert_number, UCHAR alert_level);
1591 UINT _nx_secure_tls_send_certificate(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet,
1592                                      ULONG wait_option);
1593 UINT _nx_secure_tls_send_certificate_request(NX_SECURE_TLS_SESSION *tls_session,
1594                                              NX_PACKET *send_packet);
1595 UINT _nx_secure_tls_send_changecipherspec(NX_SECURE_TLS_SESSION *tls_session,
1596                                           NX_PACKET *send_packet);
1597 UINT _nx_secure_tls_send_clienthello(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet);
1598 UINT _nx_secure_tls_send_clienthello_extensions(NX_SECURE_TLS_SESSION *tls_session,
1599                                                 UCHAR *packet_buffer, ULONG *packet_offset,
1600                                                 ULONG *extensions_length, ULONG available_size);
1601 UINT _nx_secure_tls_send_client_key_exchange(NX_SECURE_TLS_SESSION *tls_session,
1602                                              NX_PACKET *send_packet);
1603 UINT _nx_secure_tls_send_finished(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet);
1604 UINT _nx_secure_tls_send_handshake_record(NX_SECURE_TLS_SESSION *tls_session,
1605                                           NX_PACKET *send_packet, UCHAR handshake_type,
1606                                           ULONG wait_option);
1607 UINT _nx_secure_tls_send_hellorequest(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet);
1608 UINT _nx_secure_tls_send_certificate_verify(NX_SECURE_TLS_SESSION *tls_session,
1609                                             NX_PACKET *send_packet);
1610 UINT _nx_secure_tls_send_record(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet,
1611                                 UCHAR record_type, ULONG wait_option);
1612 UINT _nx_secure_tls_send_server_key_exchange(NX_SECURE_TLS_SESSION *tls_session,
1613                                              NX_PACKET *send_packet);
1614 UINT _nx_secure_tls_send_serverhello(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *send_packet);
1615 UINT _nx_secure_tls_send_serverhello_extensions(NX_SECURE_TLS_SESSION *tls_session,
1616                                                 UCHAR *packet_buffer, ULONG *packet_offset,
1617                                                 ULONG available_size);
1618 UINT _nx_secure_tls_server_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1619                                            NX_SECURE_X509_CERT *certificate, UINT cert_id);
1620 UINT _nx_secure_tls_server_certificate_find(NX_SECURE_TLS_SESSION *tls_session,
1621                                             NX_SECURE_X509_CERT **certificate, UINT cert_id);
1622 UINT _nx_secure_tls_server_certificate_remove(NX_SECURE_TLS_SESSION *tls_session, UINT cert_id);
1623 UINT _nx_secure_tls_server_handshake(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer,
1624                                      UINT data_length, ULONG wait_option);
1625 UINT _nx_secure_tls_session_iv_size_get(NX_SECURE_TLS_SESSION *tls_session, USHORT *iv_size);
1626 UINT _nx_secure_tls_session_keys_set(NX_SECURE_TLS_SESSION *tls_session, USHORT key_set);
1627 UINT _nx_secure_tls_session_receive_records(NX_SECURE_TLS_SESSION *tls_session,
1628                                             NX_PACKET **packet_ptr_ptr, ULONG wait_option);
1629 UINT _nx_secure_tls_verify_mac(NX_SECURE_TLS_SESSION *tls_session, UCHAR *header_data,
1630                                USHORT header_length, NX_PACKET *packet_ptr, ULONG offset, UINT *length);
1631 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
1632 UINT _nx_secure_tls_ecc_generate_keys(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version, UCHAR tls_1_3,
1633                                       NX_SECURE_TLS_CRYPTO *tls_crypto_table, NX_SECURE_TLS_HANDSHAKE_HASH *tls_handshake_hash,
1634                                       NX_SECURE_TLS_ECC *tls_ecc_curves, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1635                                       NX_SECURE_TLS_CREDENTIALS *tls_credentials, UINT ecc_named_curve, USHORT sign_key,
1636                                       UCHAR *public_key, UINT *public_key_size, NX_SECURE_TLS_ECDHE_HANDSHAKE_DATA *ecc_data,
1637                                       VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1638                                       VOID *public_auth_metadata, ULONG public_auth_metadata_size);
1639 UINT _nx_secure_tls_find_curve_method(NX_SECURE_TLS_ECC *tls_ecc, USHORT named_curve,
1640                                       const NX_CRYPTO_METHOD **curve_method, UINT *curve_priority);
1641 UINT _nx_secure_tls_proc_clienthello_sec_sa_extension(NX_SECURE_TLS_SESSION *tls_session,
1642                                                       NX_SECURE_TLS_HELLO_EXTENSION *exts,
1643                                                       UINT num_extensions,
1644                                                       UINT *selected_curve, USHORT cert_curve,
1645                                                       UINT *cert_curve_supported,
1646                                                       USHORT *ecdhe_signature_algorithm,
1647                                                       NX_SECURE_X509_CERT *cert);
1648 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
1649 
1650 
1651 /* Actual API functions .*/
1652 UINT nx_secure_module_hash_compute(NX_CRYPTO_METHOD *hmac_ptr,
1653                                    UINT start_address,
1654                                    UINT end_address,
1655                                    UCHAR *key, UINT key_length,
1656                                    VOID *metadata, UINT metadata_size,
1657                                    UCHAR *output_buffer, UINT output_buffer_size, UINT *actual_size);
1658 UINT _nx_secure_tls_active_certificate_set(NX_SECURE_TLS_SESSION *tls_session,
1659                                            NX_SECURE_X509_CERT *certificate);
1660 VOID _nx_secure_tls_initialize(VOID);
1661 UINT _nx_secure_tls_shutdown(VOID);
1662 
1663 UINT _nx_secure_tls_local_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1664                                           NX_SECURE_X509_CERT *certificate);
1665 UINT _nx_secure_tls_local_certificate_find(NX_SECURE_TLS_SESSION *tls_session,
1666                                            NX_SECURE_X509_CERT **certificate, UCHAR *common_name, UINT name_length);
1667 UINT _nx_secure_tls_local_certificate_remove(NX_SECURE_TLS_SESSION *tls_session,
1668                                              UCHAR *common_name, UINT common_name_length);
1669 UINT _nx_secure_tls_metadata_size_calculate(const NX_SECURE_TLS_CRYPTO *crypto_table,
1670                                             ULONG *metadata_size);
1671 UINT _nx_secure_tls_remote_certificate_allocate(NX_SECURE_TLS_SESSION *tls_session,
1672                                                 NX_SECURE_X509_CERT *certificate,
1673                                                 UCHAR *raw_certificate_buffer, UINT buffer_size);
1674 UINT _nx_secure_tls_remote_certificate_buffer_allocate(NX_SECURE_TLS_SESSION *tls_session,
1675                                                     UINT certs_number, VOID *certificate_buffer, ULONG buffer_size);
1676 UINT _nx_secure_tls_remote_certificate_free_all(NX_SECURE_TLS_SESSION *tls_session);
1677 UINT _nx_secure_tls_server_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1678                                            NX_SECURE_X509_CERT *certificate, UINT cert_id);
1679 UINT _nx_secure_tls_server_certificate_find(NX_SECURE_TLS_SESSION *tls_session,
1680                                             NX_SECURE_X509_CERT **certificate, UINT cert_id);
1681 UINT _nx_secure_tls_server_certificate_remove(NX_SECURE_TLS_SESSION *tls_session, UINT cert_id);
1682 UINT _nx_secure_tls_session_alert_value_get(NX_SECURE_TLS_SESSION *tls_session,
1683                                             UINT *alert_level, UINT *alert_value);
1684 UINT _nx_secure_tls_session_certificate_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1685                                                      ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *session,
1686                                                                        NX_SECURE_X509_CERT *certificate));
1687 UINT _nx_secure_tls_session_client_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1688                                                 ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *tls_session,
1689                                                                   NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1690                                                                   UINT num_extensions));
1691 UINT _nx_secure_tls_session_client_verify_disable(NX_SECURE_TLS_SESSION *tls_session);
1692 UINT _nx_secure_tls_session_client_verify_enable(NX_SECURE_TLS_SESSION *tls_session);
1693 UINT _nx_secure_tls_session_x509_client_verify_configure(NX_SECURE_TLS_SESSION *tls_session, UINT certs_number,
1694                                                            VOID *certificate_buffer, ULONG buffer_size);
1695 UINT _nx_secure_tls_session_create(NX_SECURE_TLS_SESSION *session_ptr,
1696                                    const NX_SECURE_TLS_CRYPTO *cipher_table,
1697                                    VOID *metadata_area,
1698                                    ULONG metadata_size);
1699 
1700 UINT _nx_secure_tls_session_create_ext(NX_SECURE_TLS_SESSION *tls_session,
1701                                    const NX_CRYPTO_METHOD **crypto_array, UINT crypto_array_size,
1702                                    const NX_CRYPTO_CIPHERSUITE **cipher_map, UINT cipher_map_size,
1703                                    VOID *metadata_buffer,
1704                                    ULONG metadata_size);
1705 
1706 UINT _nx_secure_tls_session_delete(NX_SECURE_TLS_SESSION *tls_session);
1707 UINT _nx_secure_tls_session_end(NX_SECURE_TLS_SESSION *tls_session, UINT wait_option);
1708 UINT _nx_secure_tls_session_packet_buffer_set(NX_SECURE_TLS_SESSION *session_ptr,
1709                                               UCHAR *buffer_ptr, ULONG buffer_size);
1710 UINT _nx_secure_tls_session_packet_pool_set(NX_SECURE_TLS_SESSION *tls_session,
1711                                             NX_PACKET_POOL *packet_pool);
1712 UINT _nx_secure_tls_session_protocol_version_override(NX_SECURE_TLS_SESSION *tls_session,
1713                                                       USHORT protocol_version);
1714 UINT _nx_secure_tls_session_receive(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET **packet_ptr_ptr,
1715                                     ULONG wait_option);
1716 UINT _nx_secure_tls_session_renegotiate(NX_SECURE_TLS_SESSION *tls_session,
1717                                         UINT wait_option);
1718 UINT _nx_secure_tls_session_renegotiate_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1719                                                      ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *session));
1720 UINT _nx_secure_tls_session_reset(NX_SECURE_TLS_SESSION *tls_session);
1721 UINT _nx_secure_tls_session_send(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *packet_ptr,
1722                                  ULONG wait_option);
1723 UINT _nx_secure_tls_session_server_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1724                                                 ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *tls_session,
1725                                                                   NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1726                                                                   UINT num_extensions));
1727 UINT _nx_secure_tls_session_sni_extension_parse(NX_SECURE_TLS_SESSION *tls_session,
1728                                                 NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1729                                                 UINT num_extensions, NX_SECURE_X509_DNS_NAME *dns_name);
1730 UINT _nx_secure_tls_session_sni_extension_set(NX_SECURE_TLS_SESSION *tls_session,
1731                                               NX_SECURE_X509_DNS_NAME *dns_name);
1732 UINT _nx_secure_tls_session_start(NX_SECURE_TLS_SESSION *tls_session, NX_TCP_SOCKET *tcp_socket,
1733                                   UINT wait_option);
1734 UINT _nx_secure_tls_session_time_function_set(NX_SECURE_TLS_SESSION *tls_session,
1735                                               ULONG (*time_func_ptr)(void));
1736 UINT _nx_secure_tls_trusted_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1737                                             NX_SECURE_X509_CERT *certificate);
1738 UINT _nx_secure_tls_trusted_certificate_remove(NX_SECURE_TLS_SESSION *tls_session,
1739                                                UCHAR *common_name, UINT common_name_length);
1740 UINT _nx_secure_tls_packet_allocate(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET_POOL *pool_ptr,
1741                                     NX_PACKET **packet_ptr, ULONG wait_option);
1742 #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE)
1743 UINT _nx_secure_tls_psk_add(NX_SECURE_TLS_SESSION *tls_session, UCHAR *pre_shared_key, UINT psk_length,
1744                             UCHAR *psk_identity, UINT identity_length, UCHAR *hint, UINT hint_length);
1745 UINT _nx_secure_tls_psk_find(NX_SECURE_TLS_CREDENTIALS *tls_credentials, UCHAR **psk_data, UINT *psk_length,
1746                              UCHAR *psk_identity_hint, UINT identity_length, UINT *psk_store_index);
1747 UINT _nx_secure_tls_client_psk_set(NX_SECURE_TLS_SESSION *tls_session, UCHAR *pre_shared_key, UINT psk_length,
1748                                    UCHAR *psk_identity, UINT identity_length, UCHAR *hint, UINT hint_length);
1749 #endif
1750 #if (NX_SECURE_TLS_TLS_1_3_ENABLED) && defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES)
1751 UINT _nx_secure_tls_psk_identity_find(NX_SECURE_TLS_SESSION *tls_session, UCHAR **psk_data, UINT *psk_length,
1752                                       UCHAR *psk_identity, UINT identity_length, UINT *psk_store_index);
1753 #endif
1754 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
1755 UINT _nx_secure_tls_ecc_initialize(NX_SECURE_TLS_SESSION *tls_session,
1756                                    const USHORT *supported_groups, USHORT supported_group_count,
1757                                    const NX_CRYPTO_METHOD **curves);
1758 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
1759 
1760 /* Functions for error checking .*/
1761 UINT _nxe_secure_tls_active_certificate_set(NX_SECURE_TLS_SESSION *tls_session,
1762                                             NX_SECURE_X509_CERT *certificate);
1763 UINT _nxe_secure_tls_local_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1764                                            NX_SECURE_X509_CERT *certificate);
1765 UINT _nxe_secure_tls_local_certificate_find(NX_SECURE_TLS_SESSION *tls_session,
1766                                             NX_SECURE_X509_CERT **certificate, UCHAR *common_name,
1767                                             UINT name_length);
1768 UINT _nxe_secure_tls_local_certificate_remove(NX_SECURE_TLS_SESSION *tls_session,
1769                                               UCHAR *common_name, UINT common_name_length);
1770 UINT _nxe_secure_tls_metadata_size_calculate(const NX_SECURE_TLS_CRYPTO *crypto_table,
1771                                              ULONG *metadata_size);
1772 UINT _nxe_secure_tls_remote_certificate_allocate(NX_SECURE_TLS_SESSION *tls_session,
1773                                                  NX_SECURE_X509_CERT *certificate,
1774                                                  UCHAR *raw_certificate_buffer, UINT buffer_size);
1775 UINT _nxe_secure_tls_remote_certificate_buffer_allocate(NX_SECURE_TLS_SESSION *tls_session,
1776                                                     UINT certs_number, VOID *certificate_buffer, ULONG buffer_size);
1777 UINT _nxe_secure_tls_remote_certificate_free_all(NX_SECURE_TLS_SESSION *tls_session);
1778 UINT _nxe_secure_tls_server_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1779                                             NX_SECURE_X509_CERT *certificate, UINT cert_id);
1780 UINT _nxe_secure_tls_server_certificate_find(NX_SECURE_TLS_SESSION *tls_session,
1781                                              NX_SECURE_X509_CERT **certificate, UINT cert_id);
1782 UINT _nxe_secure_tls_server_certificate_remove(NX_SECURE_TLS_SESSION *tls_session, UINT cert_id);
1783 UINT  _nxe_secure_tls_session_alert_value_get(NX_SECURE_TLS_SESSION *tls_session,
1784                                                         UINT *alert_level, UINT *alert_value);
1785 UINT _nxe_secure_tls_session_certificate_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1786                                                       ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *session,
1787                                                                         NX_SECURE_X509_CERT *certificate));
1788 UINT _nxe_secure_tls_session_client_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1789                                                  ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *tls_session,
1790                                                                    NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1791                                                                    UINT num_extensions));
1792 UINT _nxe_secure_tls_session_client_verify_disable(NX_SECURE_TLS_SESSION *tls_session);
1793 UINT _nxe_secure_tls_session_client_verify_enable(NX_SECURE_TLS_SESSION *tls_session);
1794 UINT _nxe_secure_tls_session_x509_client_verify_configure(NX_SECURE_TLS_SESSION *tls_session, UINT certs_number,
1795                                                            VOID *certificate_buffer, ULONG buffer_size);
1796 UINT _nxe_secure_tls_session_create(NX_SECURE_TLS_SESSION *session_ptr,
1797                                     const NX_SECURE_TLS_CRYPTO *cipher_table,
1798                                     VOID *metadata_area,
1799                                     ULONG metadata_size);
1800 UINT _nxe_secure_tls_session_delete(NX_SECURE_TLS_SESSION *tls_session);
1801 UINT _nxe_secure_tls_session_end(NX_SECURE_TLS_SESSION *tls_session, UINT wait_option);
1802 UINT _nxe_secure_tls_session_packet_buffer_set(NX_SECURE_TLS_SESSION *session_ptr,
1803                                                UCHAR *buffer_ptr, ULONG buffer_size);
1804 UINT _nxe_secure_tls_session_packet_pool_set(NX_SECURE_TLS_SESSION *tls_session,
1805                                              NX_PACKET_POOL *packet_pool);
1806 UINT _nxe_secure_tls_session_protocol_version_override(NX_SECURE_TLS_SESSION *tls_session,
1807                                                        USHORT protocol_version);
1808 UINT _nxe_secure_tls_session_receive(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET **packet_ptr_ptr,
1809                                      ULONG wait_option);
1810 UINT _nxe_secure_tls_session_renegotiate(NX_SECURE_TLS_SESSION *tls_session,
1811                                          UINT wait_option);
1812 UINT _nxe_secure_tls_session_renegotiate_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1813                                                       ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *session));
1814 UINT _nxe_secure_tls_session_reset(NX_SECURE_TLS_SESSION *tls_session);
1815 UINT _nxe_secure_tls_session_send(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET *packet_ptr,
1816                                   ULONG wait_option);
1817 UINT _nxe_secure_tls_session_server_callback_set(NX_SECURE_TLS_SESSION *tls_session,
1818                                                  ULONG (*func_ptr)(NX_SECURE_TLS_SESSION *tls_session,
1819                                                                    NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1820                                                                    UINT num_extensions));
1821 UINT _nxe_secure_tls_session_sni_extension_parse(NX_SECURE_TLS_SESSION *tls_session,
1822                                                  NX_SECURE_TLS_HELLO_EXTENSION *extensions,
1823                                                  UINT num_extensions, NX_SECURE_X509_DNS_NAME *dns_name);
1824 UINT _nxe_secure_tls_session_sni_extension_set(NX_SECURE_TLS_SESSION *tls_session,
1825                                                NX_SECURE_X509_DNS_NAME *dns_name);
1826 UINT _nxe_secure_tls_session_start(NX_SECURE_TLS_SESSION *tls_session, NX_TCP_SOCKET *tcp_socket,
1827                                    UINT wait_option);
1828 UINT _nxe_secure_tls_session_time_function_set(NX_SECURE_TLS_SESSION *tls_session,
1829                                                ULONG (*time_func_ptr)(void));
1830 UINT _nxe_secure_tls_trusted_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
1831                                              NX_SECURE_X509_CERT *certificate);
1832 UINT _nxe_secure_tls_trusted_certificate_remove(NX_SECURE_TLS_SESSION *tls_session,
1833                                                 UCHAR *common_name, UINT common_name_length);
1834 UINT _nxe_secure_tls_packet_allocate(NX_SECURE_TLS_SESSION *tls_session, NX_PACKET_POOL *pool_ptr,
1835                                      NX_PACKET **packet_ptr, ULONG wait_option);
1836 #if defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) || defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE)
1837 UINT _nxe_secure_tls_psk_add(NX_SECURE_TLS_SESSION *tls_session, UCHAR *pre_shared_key, UINT psk_length,
1838                              UCHAR *psk_identity, UINT identity_length, UCHAR *hint, UINT hint_length);
1839 UINT _nxe_secure_tls_psk_find(NX_SECURE_TLS_SESSION *tls_session, UCHAR **psk_data, UINT *psk_length,
1840                               UCHAR *psk_identity, UINT identity_length);
1841 UINT _nxe_secure_tls_client_psk_set(NX_SECURE_TLS_SESSION *tls_session, UCHAR *pre_shared_key, UINT psk_length,
1842                                     UCHAR *psk_identity, UINT identity_length, UCHAR *hint, UINT hint_length);
1843 #endif
1844 
1845 UINT _nx_secure_process_server_key_exchange(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, NX_SECURE_TLS_CRYPTO *tls_crypto_table,
1846                                             USHORT protocol_version, UCHAR *packet_buffer, UINT message_length,
1847                                             NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1848                                             NX_SECURE_TLS_HANDSHAKE_HASH *tls_handshake_hash,
1849                                             VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1850                                             VOID *public_auth_metadata, ULONG public_auth_metadata_size, VOID *tls_ecc_curves);
1851 UINT _nx_secure_process_client_key_exchange(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version,
1852                                             UCHAR *packet_buffer, UINT message_length, USHORT *received_remote_credentials,
1853                                             NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1854                                             VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1855                                             VOID *public_auth_metadata, ULONG public_auth_metadata_size, VOID *tls_ecc_curves);
1856 UINT _nx_secure_generate_premaster_secret(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1857                                           NX_SECURE_TLS_CREDENTIALS *tls_credentials, UINT session_type, USHORT *received_remote_credentials,
1858                                           VOID *public_cipher_metadata, ULONG public_cipher_metadata_size, VOID *tls_ecc_curves);
1859 UINT _nx_secure_generate_client_key_exchange(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite,
1860                                              NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1861                                              UCHAR *data_buffer, ULONG buffer_length, ULONG *output_size,
1862                                              VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1863                                              VOID *public_auth_metadata, ULONG public_auth_metadata_size);
1864 UINT _nx_secure_generate_server_key_exchange(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version, UCHAR tls_1_3,
1865                                              NX_SECURE_TLS_CRYPTO *tls_crypto_table, NX_SECURE_TLS_HANDSHAKE_HASH *tls_handshake_hash,
1866                                              NX_SECURE_TLS_KEY_MATERIAL *tls_key_material, NX_SECURE_TLS_CREDENTIALS *tls_credentials,
1867                                              UCHAR *data_buffer, ULONG buffer_length, ULONG *output_size,
1868                                              VOID *public_cipher_metadata, ULONG public_cipher_metadata_size,
1869                                              VOID *public_auth_metadata, ULONG public_auth_metadata_size, VOID *tls_ecc_curves);
1870 UINT _nx_secure_generate_master_secret(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version,
1871                                        const NX_CRYPTO_METHOD *session_prf_method, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1872                                        UCHAR *pre_master_sec, UINT pre_master_sec_size, UCHAR *master_sec,
1873                                        VOID *prf_metadata, ULONG prf_metadata_size);
1874 UINT _nx_secure_generate_session_keys(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, USHORT protocol_version,
1875                                       const NX_CRYPTO_METHOD *session_prf_method, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1876                                       UCHAR *master_sec, VOID *prf_metadata, ULONG prf_metadata_size);
1877 UINT _nx_secure_session_keys_set(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, NX_SECURE_TLS_KEY_MATERIAL *tls_key_material,
1878                                  UINT key_material_data_size, UINT is_client, UCHAR *session_cipher_initialized,
1879                                  VOID *session_cipher_metadata, VOID **session_cipher_handler, ULONG session_cipher_metadata_size);
1880 UINT _nx_secure_verify_mac(const NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite, UCHAR *mac_secret, ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
1881                            UCHAR *header_data, USHORT header_length, NX_PACKET *packet_ptr, ULONG offset, UINT *length,
1882                            VOID *hash_mac_metadata, ULONG hash_mac_metadata_size);
1883 UINT _nx_secure_remote_certificate_verify(NX_SECURE_X509_CERTIFICATE_STORE *store,
1884                                           NX_SECURE_X509_CERT *certificate, ULONG current_time);
1885 UINT _nx_secure_trusted_certificate_add(NX_SECURE_X509_CERTIFICATE_STORE *store,
1886                                         NX_SECURE_X509_CERT *certificate);
1887 #ifdef NX_SECURE_CUSTOM_SECRET_GENERATION
1888 UINT nx_secure_custom_secret_generation_init(NX_SECURE_TLS_SESSION *tls_session);
1889 #endif
1890 
1891 /* TLS component data declarations follow.  */
1892 
1893 /* Determine if the initialization function of this component is including
1894    this file.  If so, make the data definitions really happen.  Otherwise,
1895    make them extern so other functions in the component can access them.  */
1896 
1897 #ifdef NX_SECURE_TLS_INIT
1898 #define TLS_DECLARE
1899 #else
1900 #define TLS_DECLARE extern
1901 #endif
1902 
1903 
1904 /* Define the head pointer of the created TLS list.  */
1905 TLS_DECLARE  NX_SECURE_TLS_SESSION *_nx_secure_tls_created_ptr;
1906 TLS_DECLARE  ULONG    _nx_secure_tls_created_count;
1907 TLS_DECLARE  TX_MUTEX _nx_secure_tls_protection;
1908 
1909 #ifdef __cplusplus
1910 }
1911 #endif
1912 
1913 #endif /* SRC_NX_SECURE_TLS_H_ */
1914 
1915