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 #include "nx_secure_x509.h"
26 
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_tls_trusted_certificate_remove           PORTABLE C      */
32 /*                                                           6.2.1        */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function removes an NX_SECURE_TLS_CERTIFICATE instance from    */
40 /*    the trusted certificates store, keyed on the Common Name field.     */
41 /*                                                                        */
42 /*  INPUT                                                                 */
43 /*                                                                        */
44 /*    tls_session                           Pointer to TLS Session        */
45 /*    common_name                           String to match "CN" field    */
46 /*    common_name_length                    Length of name                */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    status                                Completion status             */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    tx_mutex_get                          Get protection mutex          */
55 /*    tx_mutex_put                          Put protection mutex          */
56 /*    _nx_secure_x509_store_certificate_remove                            */
57 /*                                          Remove certificate from store */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application Code                                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
68 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*  04-02-2021     Timothy Stapko           Modified comment(s),          */
71 /*                                            updated X.509 return value, */
72 /*                                            resulting in version 6.1.6  */
73 /*  03-08-2023     Yanwu Cai                Modified comment(s),          */
74 /*                                            fixed compiler errors when  */
75 /*                                            x509 is disabled,           */
76 /*                                            resulting in version 6.2.1  */
77 /*                                                                        */
78 /**************************************************************************/
_nx_secure_tls_trusted_certificate_remove(NX_SECURE_TLS_SESSION * tls_session,UCHAR * common_name,UINT common_name_length)79 UINT _nx_secure_tls_trusted_certificate_remove(NX_SECURE_TLS_SESSION *tls_session,
80                                                UCHAR *common_name, UINT common_name_length)
81 {
82 #ifndef NX_SECURE_DISABLE_X509
83 UINT                              status;
84 NX_SECURE_X509_DISTINGUISHED_NAME name;
85 
86 
87     /* Get the protection. */
88     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
89 
90     /* Set up the distinguished name with the passed-in common name. */
91     name.nx_secure_x509_common_name_length = (USHORT)common_name_length;
92     name.nx_secure_x509_common_name = common_name;
93 
94     /* Remove the certificate from the local certificates list in our TLS control block. */
95     status = _nx_secure_x509_store_certificate_remove(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store,
96                                                       &name, NX_SECURE_X509_CERT_LOCATION_TRUSTED, 0);
97 
98 
99     /* Release the protection. */
100     tx_mutex_put(&_nx_secure_tls_protection);
101 
102     /* Translate some X.509 return values into TLS return values. */
103     if (status == NX_SECURE_X509_CERTIFICATE_NOT_FOUND)
104     {
105         return(NX_SECURE_TLS_CERTIFICATE_NOT_FOUND);
106     }
107 
108     return(status);
109 #else
110     NX_PARAMETER_NOT_USED(tls_session);
111     NX_PARAMETER_NOT_USED(common_name);
112     NX_PARAMETER_NOT_USED(common_name_length);
113 
114     return(NX_NOT_SUPPORTED);
115 #endif
116 }
117 
118