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 #define NX_SECURE_SOURCE_CODE
22 
23 #include "nx_secure_tls.h"
24 #include "nx_secure_x509.h"
25 
26 /**************************************************************************/
27 /*                                                                        */
28 /*  FUNCTION                                               RELEASE        */
29 /*                                                                        */
30 /*    _nx_secure_tls_trusted_certificate_add              PORTABLE C      */
31 /*                                                           6.2.1        */
32 /*  AUTHOR                                                                */
33 /*                                                                        */
34 /*    Timothy Stapko, Microsoft Corporation                               */
35 /*                                                                        */
36 /*  DESCRIPTION                                                           */
37 /*                                                                        */
38 /*    This function adds an initialized NX_SECURE_TLS_CERTIFICATE to a    */
39 /*    TLS session for use as a trusted Root Certificate - the certificate */
40 /*    is used to verify incoming certificates from the remote host, by    */
41 /*    matching the incoming certificate's Issuer Common Name field with   */
42 /*    that of the certificates in the Trusted Store to find the trusted   */
43 /*    key used to verify that the incoming certificate is valid.          */
44 /*    The function may be called repeatedly to add multiple certificates  */
45 /*    to the internal linked-list.                                        */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    tls_session                           Pointer to TLS Session        */
50 /*    certificate                           Pointer to certificate        */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    tx_mutex_get                          Get protection mutex          */
59 /*    tx_mutex_put                          Put protection mutex          */
60 /*    [nx_secure_trusted_certificate_add]   Add certificate to trusted    */
61 /*                                            store                       */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
72 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  04-02-2021     Timothy Stapko           Modified comment(s),          */
75 /*                                            updated X.509 return value, */
76 /*                                            resulting in version 6.1.6  */
77 /*  10-31-2022     Yanwu Cai                Modified comment(s), added    */
78 /*                                            custom secret generation,   */
79 /*                                            resulting in version 6.2.0  */
80 /*  03-08-2023     Yanwu Cai                Modified comment(s),          */
81 /*                                            fixed compiler errors when  */
82 /*                                            x509 is disabled,           */
83 /*                                            resulting in version 6.2.1  */
84 /*                                                                        */
85 /**************************************************************************/
_nx_secure_tls_trusted_certificate_add(NX_SECURE_TLS_SESSION * tls_session,NX_SECURE_X509_CERT * certificate)86 UINT _nx_secure_tls_trusted_certificate_add(NX_SECURE_TLS_SESSION *tls_session,
87                                             NX_SECURE_X509_CERT *certificate)
88 {
89 #ifndef NX_SECURE_DISABLE_X509
90 UINT status;
91 
92 
93     /* Get the protection. */
94     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
95 
96 
97     /* Assign the TLS Session metadata areas to the certificate for later use. */
98     certificate -> nx_secure_x509_public_cipher_metadata_area = tls_session -> nx_secure_public_cipher_metadata_area;
99     certificate -> nx_secure_x509_public_cipher_metadata_size = tls_session -> nx_secure_public_cipher_metadata_size;
100 
101     certificate -> nx_secure_x509_hash_metadata_area = tls_session -> nx_secure_hash_mac_metadata_area;
102     certificate -> nx_secure_x509_hash_metadata_size = tls_session -> nx_secure_hash_mac_metadata_size;
103 
104     /* Assign the cipher table from the parent TLS session. */
105     certificate -> nx_secure_x509_cipher_table = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table;
106     certificate -> nx_secure_x509_cipher_table_size = tls_session -> nx_secure_tls_crypto_table -> nx_secure_tls_x509_cipher_table_size;
107 
108 
109 
110     /* Add the certificate to the TLS session credentials X509 store. */
111     status = tls_session -> nx_secure_trusted_certificate_add(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store, certificate);
112 
113     /* Release the protection. */
114     tx_mutex_put(&_nx_secure_tls_protection);
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