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