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