Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
#include "nx_secure_x509.h"
#include "nx_secure_dtls.h"
...
...
_nx_secure_tls_process_certificate_request(NX_SECURE_TLS_SESSION *, UCHAR *, UINT)
Files
loading (3/7)...
SourceVuSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_tls_process_certificate_request.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Secure Component */ /** */ /** Transport Layer Security (TLS) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_tls.h" #include "nx_secure_x509.h" #ifdef NX_SECURE_ENABLE_DTLS #include "nx_secure_dtls.h" #endif /* NX_SECURE_ENABLE_DTLS */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_tls_process_certificate_request PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function processes an incoming TLS Certificate request */ /* message, typically sent by a TLS Server to request a client */ /* certificate for authentication. */ /* */ /* INPUT */ /* */ /* tls_session TLS control block */ /* packet_buffer Pointer to message data */ /* message_length Length of message data (bytes)*/ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_secure_x509_local_device_certificate_get */ /* Get the local certificate */ /* */ /* CALLED BY */ /* */ /* _nx_secure_dtls_client_handshake DTLS client state machine */ /* _nx_secure_tls_client_handshake TLS client state machine */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_secure_tls_process_certificate_request(NX_SECURE_TLS_SESSION *tls_session, UCHAR *packet_buffer, UINT message_length) { UINT length; UINT cert_types_length; UCHAR cert_type; UINT i; NX_SECURE_X509_CERT *local_certificate = NX_NULL; UCHAR expected_cert_type = 0; UINT status; #if (NX_SECURE_TLS_TLS_1_2_ENABLED) UINT sign_algs_length; UINT sign_alg; UINT expected_sign_alg = 0;/* ... */ #endif #if (NX_SECURE_TLS_TLS_1_3_ENABLED) UINT extension_total_length; UINT extension_length; UINT extension_type;/* ... */ #endif /* Structure: * | 1 | <Cert types count> | 2 | <Sig algs length> | * | Cert types count | Cert types (1 byte each) | Sig Hash algorithms length | Algorithms (2 bytes each) | *//* ... */ /* Use our length as an index into the buffer. */ length = 0; /* Get the local certificate. */ if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { local_certificate = tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate; }if (tls_session -> nx_secure_tls_credentials.nx_secure_tls_active_certificate != NX_NULL) { ... } else { /* Get reference to local device certificate. NX_NULL is passed for name to get default entry. */ status = _nx_secure_x509_local_device_certificate_get(&tls_session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store, NX_NULL, &local_certificate); if (status != NX_SUCCESS) { local_certificate = NX_NULL; }if (status != NX_SUCCESS) { ... } }else { ... } if (local_certificate != NX_NULL) { #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session -> nx_secure_tls_1_3) { tls_session -> nx_secure_tls_signature_algorithm = 0; /* TLS1.3 uses RSASSA-PSS instead of RSASSA-PKCS. RSASSA-PSS is not supported now. */ if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { return(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_TYPE); }if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { ... } /* In TLS 1.3, the signing curve is constrained. */ switch (local_certificate -> nx_secure_x509_private_key.ec_private_key.nx_secure_ec_named_curve) { case NX_CRYPTO_EC_SECP256R1: expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256; break;case NX_CRYPTO_EC_SECP256R1: case NX_CRYPTO_EC_SECP384R1: expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA384; break;case NX_CRYPTO_EC_SECP384R1: case NX_CRYPTO_EC_SECP521R1: expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA512; break;case NX_CRYPTO_EC_SECP521R1: default: return(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_ALG);default }switch (local_certificate -> nx_secure_x509_private_key.ec_private_key.nx_secure_ec_named_curve) { ... } }if (tls_session -> nx_secure_tls_1_3) { ... } else #endif { if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { expected_cert_type = NX_SECURE_TLS_CERT_TYPE_RSA_SIGN; #if (NX_SECURE_TLS_TLS_1_2_ENABLED) expected_sign_alg = NX_SECURE_TLS_SIGNATURE_RSA_SHA256; #endif }if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_RSA) { ... } #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE else if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { expected_cert_type = NX_SECURE_TLS_CERT_TYPE_ECDSA_SIGN; #if (NX_SECURE_TLS_TLS_1_2_ENABLED) expected_sign_alg = NX_SECURE_TLS_SIGNATURE_ECDSA_SHA256; #endif }else if (local_certificate -> nx_secure_x509_public_algorithm == NX_SECURE_TLS_X509_TYPE_EC) { ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ }else { ... } }if (local_certificate != NX_NULL) { ... } #if (NX_SECURE_TLS_TLS_1_3_ENABLED) /* struct { opaque certificate_request_context<0..2^8-1>; Extension extensions<2..2^16-1>; } CertificateRequest; *//* ... */ if (tls_session -> nx_secure_tls_1_3) { /* Skip certificate request context. */ length = length + 1 + packet_buffer[length]; extension_total_length = (UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); length += 2; /* Make sure what we extracted makes sense. */ if ((length + extension_total_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((length + extension_total_length) > message_length) { ... } /* Find SignatureAlgorithms extension. */ /* Note: Other extensions will be processed in the future. */ while (length < message_length) { extension_type = (UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); length += 2; extension_length = (UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); length += 2; if (extension_type == NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS) { break; }if (extension_type == NX_SECURE_TLS_EXTENSION_SIGNATURE_ALGORITHMS) { ... } length += extension_length; }while (length < message_length) { ... } if (length >= message_length) { return(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_ALG); }if (length >= message_length) { ... } }if (tls_session -> nx_secure_tls_1_3) { ... } else #endif { /* Extract the count of certificate types from the incoming data. */ cert_types_length = packet_buffer[length]; length += 1; /* Make sure what we extracted makes sense. */ if (cert_types_length > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if (cert_types_length > message_length) { ... } cert_type = NX_SECURE_TLS_CERT_TYPE_NONE; for (i = 0; i < cert_types_length; ++i) { if (packet_buffer[length] == expected_cert_type) { /* We found a type we support. */ cert_type = packet_buffer[length]; }if (packet_buffer[length] == expected_cert_type) { ... } length += 1; }for (i = 0; i < cert_types_length; ++i) { ... } /* Make sure our certificate type is one we support. */ if (cert_type != expected_cert_type) { return(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_TYPE); }if (cert_type != expected_cert_type) { ... } }else { ... } #if (NX_SECURE_TLS_TLS_1_2_ENABLED) /* TLS 1.2 CertificateRequest contains a list of signature algorithms that is not included in earlier TLS versions. *//* ... */ #ifdef NX_SECURE_ENABLE_DTLS if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2 || tls_session -> nx_secure_tls_protocol_version == NX_SECURE_DTLS_VERSION_1_2)/* ... */ #else if (tls_session -> nx_secure_tls_protocol_version == NX_SECURE_TLS_VERSION_TLS_1_2) #endif /* NX_SECURE_ENABLE_DTLS */ { /* Extract the count of algorithms from the incoming data. */ sign_algs_length = (UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); length = length + 2; /* Make sure what we extracted makes sense. */ if ((length + sign_algs_length) > message_length) { return(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH); }if ((length + sign_algs_length) > message_length) { ... } /* Extract the signature algorithms. */ sign_alg = NX_SECURE_TLS_HASH_ALGORITHM_NONE; for (i = 0; i < sign_algs_length; i += 2) { /* Look for a type we support. */ if ((UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]) == expected_sign_alg) { sign_alg = (UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]); break; }if ((UINT)((packet_buffer[length] << 8) + packet_buffer[length + 1]) == expected_sign_alg) { ... } length = length + 2; }for (i = 0; i < sign_algs_length; i += 2) { ... } /* Make sure we are using the right signature algorithm! */ if (sign_alg != expected_sign_alg) { return(NX_SECURE_TLS_UNSUPPORTED_CERT_SIGN_ALG); }if (sign_alg != expected_sign_alg) { ... } #if (NX_SECURE_TLS_TLS_1_3_ENABLED) if (tls_session -> nx_secure_tls_1_3) { tls_session -> nx_secure_tls_signature_algorithm = sign_alg; }if (tls_session -> nx_secure_tls_1_3) { ... } /* ... */#endif ...}/* ... */ #endif /* The remainder of the message is the Certificate Authorities list. It can be used to select a certificate in a particular authorization chain. In general, if there is only one device certificate we will send that one and if the server doesn't like it the connection will be dropped. *//* ... */ #ifdef NX_SECURE_TLS_CLIENT_DISABLED /* If TLS Client is disabled and we have processed a CertificateRequest, something is wrong... */ tls_session -> nx_secure_tls_server_state = NX_SECURE_TLS_SERVER_STATE_ERROR; return(NX_SECURE_TLS_INVALID_STATE);/* ... */ #else /* Set our state to indicate we received a certificate request. */ tls_session -> nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_CERTIFICATE_REQUEST; return(NX_SUCCESS);/* ... */ #endif }{ ... }
Details