1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Secure Component                                                 */
16 /**                                                                       */
17 /**    Transport Layer Security (TLS)                                     */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SECURE_SOURCE_CODE
23 
24 #include "nx_secure_tls.h"
25 
26 /**************************************************************************/
27 /*                                                                        */
28 /*  FUNCTION                                               RELEASE        */
29 /*                                                                        */
30 /*    _nx_secure_tls_client_psk_set                       PORTABLE C      */
31 /*                                                           6.1          */
32 /*  AUTHOR                                                                */
33 /*                                                                        */
34 /*    Timothy Stapko, Microsoft Corporation                               */
35 /*                                                                        */
36 /*  DESCRIPTION                                                           */
37 /*                                                                        */
38 /*    This function sets the pre-shared key (PSK) for a TLS Client in a   */
39 /*    TLS session control block for use with a remote server that is      */
40 /*    using a PSK ciphersuite. The PSK is found using an "identity hint"  */
41 /*    that should match a field in the PSK structure in the TLS session.  */
42 /*                                                                        */
43 /*  INPUT                                                                 */
44 /*                                                                        */
45 /*    tls_session                           Pointer to TLS Session        */
46 /*    pre_shared_key                        The Preshared Key             */
47 /*    psk_length                            Length of preshared key       */
48 /*    psk_identity                          Identity string               */
49 /*    identity_length                       Length of the identity string */
50 /*    hint                                  Hint string                   */
51 /*    hint_length                           Length of the hint string     */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    tx_mutex_get                          Get protection mutex          */
60 /*    tx_mutex_get                          Put protection mutex          */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
71 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
72 /*                                            verified memcpy use cases,  */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
76 #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
_nx_secure_tls_client_psk_set(NX_SECURE_TLS_SESSION * tls_session,UCHAR * pre_shared_key,UINT psk_length,UCHAR * psk_identity,UINT identity_length,UCHAR * hint,UINT hint_length)77 UINT _nx_secure_tls_client_psk_set(NX_SECURE_TLS_SESSION *tls_session, UCHAR *pre_shared_key, UINT psk_length,
78                                    UCHAR *psk_identity, UINT identity_length, UCHAR *hint, UINT hint_length)
79 {
80 UINT status;
81 
82     /* Get the protection. */
83     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
84 
85     /* Make sure the PSK will fit. */
86     if (psk_length <= NX_SECURE_TLS_MAX_PSK_SIZE &&
87         identity_length <= NX_SECURE_TLS_MAX_PSK_ID_SIZE &&
88         hint_length <= NX_SECURE_TLS_MAX_PSK_ID_SIZE)
89     {
90         /* Save off the PSK and its length. */
91         NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data, pre_shared_key, psk_length); /* Use case of memcpy is verified. */
92         tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_data_size = psk_length;
93 
94         /* Save off the identity and its length. */
95         NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_id, psk_identity, identity_length); /* Use case of memcpy is verified. */
96         tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_id_size = identity_length;
97 
98         /* Save off the hint and its length. */
99         NX_SECURE_MEMCPY(tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_id_hint, hint, hint_length); /* Use case of memcpy is verified. */
100         tls_session -> nx_secure_tls_credentials.nx_secure_tls_client_psk.nx_secure_tls_psk_id_hint_size = hint_length;
101 
102         status = NX_SUCCESS;
103     }
104     else
105     {
106         /* Can't add any more PSKs. */
107         status = NX_SECURE_TLS_NO_MORE_PSK_SPACE;
108     }
109 
110     /* Release the protection. */
111     tx_mutex_put(&_nx_secure_tls_protection);
112 
113     return(status);
114 }
115 #endif
116 
117