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 /**    X.509 Digital Certificates                                         */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 #include "nx_secure_x509.h"
26 
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_x509_local_certificate_find              PORTABLE C      */
32 /*                                                           6.1.6        */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function searches a given certificate store for a specific     */
40 /*    certificate. This is decided by searching the "local" certificate   */
41 /*    list in the given store for a certificate based on a specific       */
42 /*    unique ID in case multiple certificates share the same name.        */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    store                                 Pointer to certificate store  */
47 /*    certificate                           Pointer to cert pointer       */
48 /*    cert_id                               Unique certificate identifier */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                Completion status             */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    _nx_secure_tls_server_certificate_find                              */
61 /*                                          Find server certificate       */
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 /*                                            removed dependency on TLS,  */
72 /*                                            resulting in version 6.1.6  */
73 /*                                                                        */
74 /**************************************************************************/
_nx_secure_x509_local_certificate_find(NX_SECURE_X509_CERTIFICATE_STORE * store,NX_SECURE_X509_CERT ** certificate,UINT cert_id)75 UINT _nx_secure_x509_local_certificate_find(NX_SECURE_X509_CERTIFICATE_STORE *store,
76                                             NX_SECURE_X509_CERT **certificate, UINT cert_id)
77 {
78 NX_SECURE_X509_CERT *list_head;
79 NX_SECURE_X509_CERT *current_cert;
80 
81     /* Get the first certificate in the local store. */
82     list_head = store -> nx_secure_x509_local_certificates;
83 
84     if (list_head == NX_CRYPTO_NULL)
85     {
86         /* No certificates in this store! */
87         return(NX_SECURE_X509_CERTIFICATE_NOT_FOUND);
88     }
89 
90     /* Walk the list until we find a certificate that has a matching ID. */
91     current_cert = list_head;
92 
93     while (current_cert != NX_CRYPTO_NULL)
94     {
95         if (current_cert -> nx_secure_x509_cert_identifier == cert_id)
96         {
97             /* We found a match, return it. */
98             if (certificate != NX_CRYPTO_NULL)
99             {
100                 /* If certificate is NULL, just return that one was found, but nothing to return. */
101                 *certificate = current_cert;
102             }
103 
104             /* We are OK to quit now, we found the certificate. */
105             return(NX_SECURE_X509_SUCCESS);
106         }
107 
108         /* Advance our current certificate pointer. */
109         current_cert = current_cert -> nx_secure_x509_next_certificate;
110     }
111 
112     /* No matching certificates in this store! */
113     return(NX_SECURE_X509_CERTIFICATE_NOT_FOUND);
114 }
115 
116