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_trusted_certificate_add                  PORTABLE C      */
31 /*                                                           6.2.0        */
32 /*  AUTHOR                                                                */
33 /*                                                                        */
34 /*    Yanwu Cai, 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 /*    store                                 Pointer to certificate store  */
50 /*    certificate                           Pointer to certificate        */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _nx_secure_x509_store_certificate_add Add certificate to trusted    */
59 /*                                           store                        */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  10-31-2022     Yanwu Cai                Initial Version 6.2.0         */
70 /*                                                                        */
71 /**************************************************************************/
_nx_secure_trusted_certificate_add(NX_SECURE_X509_CERTIFICATE_STORE * store,NX_SECURE_X509_CERT * certificate)72 UINT _nx_secure_trusted_certificate_add(NX_SECURE_X509_CERTIFICATE_STORE *store,
73                                         NX_SECURE_X509_CERT *certificate)
74 {
75 UINT status;
76 
77 
78     /* Add the certificate to the TLS session credentials X509 store. */
79     status = _nx_secure_x509_store_certificate_add(certificate, store,
80                                                    NX_SECURE_X509_CERT_LOCATION_TRUSTED);
81 
82     /* Translate some X.509 return values into TLS return values. */
83     if (status == NX_SECURE_X509_CERT_ID_DUPLICATE)
84     {
85         return(NX_SECURE_TLS_CERT_ID_DUPLICATE);
86     }
87 
88 
89 
90     return(status);
91 }
92 
93