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 /**    Transport Layer Security (TLS)                                     */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 #include "nx_secure_tls.h"
26 
27 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    _nx_secure_tls_find_curve_method                    PORTABLE C      */
34 /*                                                           6.2.0        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    Timothy Stapko, Microsoft Corporation                               */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function finds the curve method for the specified named curve  */
42 /*    ID.                                                                 */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    tls_session                           TLS control block             */
47 /*    named_curve                           Named curve ID                */
48 /*    curve_method                          Pointer to hold the curve     */
49 /*                                            method                      */
50 /*    curve_priority                        Pointer to return value for   */
51 /*                                            priority value              */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _nx_secure_tls_generate_premaster_secret                            */
64 /*                                          Generate Pre-Master Secret    */
65 /*    _nx_secure_tls_process_certificate_verify                           */
66 /*                                          Process CertificateVerify     */
67 /*    _nx_secure_tls_proc_clienthello_sec_sa_extension                    */
68 /*                                          Process supported groups      */
69 /*                                            extensions in ClientHello   */
70 /*    _nx_secure_tls_process_client_key_exchange                          */
71 /*                                          Process ClientKeyExchange     */
72 /*    _nx_secure_tls_process_server_key_exchange                          */
73 /*                                          Process ServerKeyExchange     */
74 /*    _nx_secure_tls_send_certificate_verify                              */
75 /*                                          Send CertificateVerify        */
76 /*    _nx_secure_tls_send_server_key_exchange                             */
77 /*                                          Send ServerKeyExchange        */
78 /*                                                                        */
79 /*  RELEASE HISTORY                                                       */
80 /*                                                                        */
81 /*    DATE              NAME                      DESCRIPTION             */
82 /*                                                                        */
83 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
84 /*  09-30-2020     Timothy Stapko           Modified comment(s), added    */
85 /*                                            curve priority return value,*/
86 /*                                            resulting in version 6.1    */
87 /*  04-02-2021     Timothy Stapko           Modified comment(s), added    */
88 /*                                            ECC curve table in X509,    */
89 /*                                            resulting in version 6.1.6  */
90 /*  10-31-2022     Yanwu Cai                Modified comment(s),          */
91 /*                                            updated parameters list,    */
92 /*                                            resulting in version 6.2.0  */
93 /*                                                                        */
94 /**************************************************************************/
_nx_secure_tls_find_curve_method(NX_SECURE_TLS_ECC * tls_ecc,USHORT named_curve,const NX_CRYPTO_METHOD ** curve_method,UINT * curve_priority)95 UINT _nx_secure_tls_find_curve_method(NX_SECURE_TLS_ECC *tls_ecc, USHORT named_curve,
96                                       const NX_CRYPTO_METHOD **curve_method, UINT *curve_priority)
97 {
98 USHORT i;
99 
100     *curve_method = NX_NULL;
101 
102     /* Find out the curve method for the named curve. */
103     for (i = 0; i < tls_ecc -> nx_secure_tls_ecc_supported_groups_count; i++)
104     {
105         if (named_curve == tls_ecc -> nx_secure_tls_ecc_supported_groups[i])
106         {
107             *curve_method = tls_ecc -> nx_secure_tls_ecc_curves[i];
108 
109             /* The index in the supported list is the curve priority: lower value == higher priority. */
110             if(curve_priority != NX_NULL)
111             {
112                 *curve_priority = i;
113             }
114             break;
115         }
116     }
117 
118     if (*curve_method == NX_NULL)
119     {
120         return(NX_CRYTPO_MISSING_ECC_CURVE);
121     }
122 
123     return(NX_SUCCESS);
124 }
125 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
126