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 #define NX_SECURE_SOURCE_CODE
23 
24 #include "nx_secure_tls.h"
25 #include "nx_secure_x509.h"
26 
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_tls_local_certificate_add                PORTABLE C      */
32 /*                                                           6.2.1        */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function adds an initialized NX_SECURE_TLS_CERTIFICATE to a    */
40 /*    TLS session for use as a local identification certificate - the     */
41 /*    TLS Server certificate for TLS servers, and the Client certificate  */
42 /*    for TLS clients. The function may be called repeatedly to add       */
43 /*    multiple certificates to the internal linked-list.                  */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    tls_session                           Pointer to TLS Session        */
48 /*    certificate                           Pointer to certificate        */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                Completion status             */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    tx_mutex_get                          Get protection mutex          */
57 /*    tx_mutex_put                          Put protection mutex          */
58 /*    _nx_secure_x509_store_certificate_add Add the certificate to the    */
59 /*                                           local store                  */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*    _nx_secure_tls_server_certificate_add Add server certificate        */
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 /*                                            resulting in version 6.1    */
73 /*  04-02-2021     Timothy Stapko           Modified comment(s),          */
74 /*                                            updated X.509 return value, */
75 /*                                            resulting in version 6.1.6  */
76 /*  03-08-2023     Yanwu Cai                Modified comment(s),          */
77 /*                                            fixed compiler errors when  */
78 /*                                            x509 is disabled,           */
79 /*                                            resulting in version 6.2.1  */
80 /*                                                                        */
81 /**************************************************************************/
_nx_secure_tls_local_certificate_add(NX_SECURE_TLS_SESSION * tls_session,NX_SECURE_X509_CERT * certificate)82 UINT _nx_secure_tls_local_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
83                                           NX_SECURE_X509_CERT *certificate)
84 {
85 #ifndef NX_SECURE_DISABLE_X509
86 UINT status;
87 
88     /* Get the protection. */
89     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
90 
91     /* Assign the TLS Session metadata areas to the certificate for later use. */
92     certificate -> nx_secure_x509_public_cipher_metadata_area = tls_session -> nx_secure_public_cipher_metadata_area;
93     certificate -> nx_secure_x509_public_cipher_metadata_size = tls_session -> nx_secure_public_cipher_metadata_size;
94 
95     certificate -> nx_secure_x509_hash_metadata_area = tls_session -> nx_secure_hash_mac_metadata_area;
96     certificate -> nx_secure_x509_hash_metadata_size = tls_session -> nx_secure_hash_mac_metadata_size;
97 
98     /* Assign the cipher table from the parent TLS session. */
99     certificate -> nx_secure_x509_cipher_table = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table;
100     certificate -> nx_secure_x509_cipher_table_size = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table_size;
101 
102 
103     /* Add the certificate to the TLS session credentials X509 store. */
104     status = _nx_secure_x509_store_certificate_add(certificate, &tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store,
105                                                    NX_SECURE_X509_CERT_LOCATION_LOCAL);
106 
107     /* Release the protection. */
108     tx_mutex_put(&_nx_secure_tls_protection);
109 
110     /* Translate some X.509 return values into TLS return values. */
111     if (status == NX_SECURE_X509_CERT_ID_DUPLICATE)
112     {
113         return(NX_SECURE_TLS_CERT_ID_DUPLICATE);
114     }
115 
116     return(status);
117 #else
118     NX_PARAMETER_NOT_USED(tls_session);
119     NX_PARAMETER_NOT_USED(certificate);
120 
121     return(NX_NOT_SUPPORTED);
122 #endif
123 }
124 
125