Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_x509.h"
generated_hash
decrypted_signature
...
...
_nx_secure_x509_certificate_verify(NX_SECURE_X509_CERTIFICATE_STORE *, NX_SECURE_X509_CERT *, NX_SECURE_X509_CERT *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduonx_secure/src/nx_secure_x509_certificate_verify.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** X.509 Digital Certificates */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SECURE_SOURCE_CODE #include "nx_secure_x509.h" static UCHAR generated_hash[64]; /* We need to be able to hold the entire generated hash - SHA-512 = 64 bytes. */ static UCHAR decrypted_signature[512]; /* This needs to hold the entire decrypted data - RSA 2048-bit key = 256 bytes. */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_secure_x509_certificate_verify PORTABLE C */ /* 6.1.6 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function verifies a certificate by checking its signature */ /* against its issuer's public key. */ /* */ /* INPUT */ /* */ /* store Pointer to certificate store */ /* certificate Pointer to certificate */ /* issuer_certificate Pointer to issuer certificate */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* [nx_crypto_init] Crypto initialization */ /* [nx_crypto_operation] Crypto operation */ /* _nx_secure_x509_pkcs7_decode Decode the PKCS#7 signature */ /* _nx_secure_x509_find_certificate_methods */ /* Find certificate methods */ /* _nx_secure_x509_find_curve_method Find named curve used */ /* _nx_secure_x509_asn1_tlv_block_parse Parse ASN.1 block */ /* */ /* CALLED BY */ /* */ /* _nx_secure_x509_certificate_chain_verify */ /* Verify cert against stores */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), update */ /* ECC find curve method, */ /* add KeyUsage check, */ /* resulting in version 6.1 */ /* 04-02-2021 Timothy Stapko Modified comment(s), */ /* removed dependency on TLS, */ /* resulting in version 6.1.6 */ /* */... /**************************************************************************/ UINT _nx_secure_x509_certificate_verify(NX_SECURE_X509_CERTIFICATE_STORE *store, NX_SECURE_X509_CERT *certificate, NX_SECURE_X509_CERT *issuer_certificate) { UINT status; UINT oid_length; const UCHAR *oid; UINT decrypted_hash_length; const UCHAR *decrypted_hash; const UCHAR *certificate_verify_data; UINT verify_data_length; const UCHAR *signature_data; UINT signature_length; UINT compare_result; UINT hash_length; const NX_CRYPTO_METHOD *hash_method; const NX_CRYPTO_METHOD *public_cipher_method; NX_SECURE_X509_CRYPTO *crypto_methods; VOID *handler = NX_CRYPTO_NULL; #ifndef NX_SECURE_X509_DISABLE_KEY_USAGE_CHECK USHORT key_usage_bitfield = 0; #endif #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE NX_SECURE_EC_PUBLIC_KEY *ec_pubkey; const NX_CRYPTO_METHOD *curve_method;/* ... */ #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ NX_CRYPTO_PARAMETER_NOT_USED(store); #ifndef NX_SECURE_X509_DISABLE_KEY_USAGE_CHECK /* Before we do any crypto verification, we need to check the KeyUsage extension. */ status = _nx_secure_x509_key_usage_extension_parse(issuer_certificate, &key_usage_bitfield); /* If extension is not present, we don't need to verify per RFC 5280. */ if(NX_SECURE_X509_SUCCESS == status) { /* The issuer cert has a KeyUsage extension - check the KeyCertSign bit. */ if(!(key_usage_bitfield & NX_SECURE_X509_KEY_USAGE_KEY_CERT_SIGN)) { return(NX_SECURE_X509_KEY_USAGE_ERROR); }if (!(key_usage_bitfield & NX_SECURE_X509_KEY_USAGE_KEY_CERT_SIGN)) { ... } }if (NX_SECURE_X509_SUCCESS == status) { ... } /* ... */#endif /* Get working pointers to relevant data. */ certificate_verify_data = certificate -> nx_secure_x509_certificate_data; verify_data_length = certificate -> nx_secure_x509_certificate_data_length; signature_data = certificate -> nx_secure_x509_signature_data; signature_length = certificate -> nx_secure_x509_signature_data_length; /* Find certificate crypto methods for this certificate. */ status = _nx_secure_x509_find_certificate_methods(certificate, (USHORT)certificate -> nx_secure_x509_signature_algorithm, &crypto_methods); if (status != NX_SECURE_X509_SUCCESS) { return(status); }if (status != NX_SECURE_X509_SUCCESS) { ... } /* Assign local pointers for the crypto methods. */ hash_method = crypto_methods -> nx_secure_x509_hash_method; public_cipher_method = crypto_methods -> nx_secure_x509_public_cipher_method; NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); NX_SECURE_MEMSET(decrypted_signature, 0, sizeof(decrypted_signature)); if (hash_method -> nx_crypto_init) { status = hash_method -> nx_crypto_init((NX_CRYPTO_METHOD*)hash_method, NX_CRYPTO_NULL, 0, &handler, certificate -> nx_secure_x509_hash_metadata_area, certificate -> nx_secure_x509_hash_metadata_size); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_init) { ... } /* We need to generate a hash of this certificate in order to verify it against our trusted store. */ if (hash_method -> nx_crypto_operation != NX_CRYPTO_NULL) { status = hash_method -> nx_crypto_operation(NX_CRYPTO_VERIFY, handler, (NX_CRYPTO_METHOD*)hash_method, NX_CRYPTO_NULL, 0, (UCHAR *)certificate_verify_data, verify_data_length, NX_CRYPTO_NULL, generated_hash, sizeof(generated_hash), certificate -> nx_secure_x509_hash_metadata_area, certificate -> nx_secure_x509_hash_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); if(status != NX_CRYPTO_SUCCESS) { return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_operation != NX_CRYPTO_NULL) { ... } if (hash_method -> nx_crypto_cleanup) { status = hash_method -> nx_crypto_cleanup(certificate -> nx_secure_x509_hash_metadata_area); if(status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (hash_method -> nx_crypto_cleanup) { ... } hash_length = (hash_method -> nx_crypto_ICV_size_in_bits >> 3); /* Perform a public-key decryption operation on the extracted signature from the certificate. * In this case, the operation is doing a "reverse decryption", using the public key to decrypt, rather * than the private. This allows us to tie a trusted root certificate to a signature of a certificate * signed by that root CA's private key. when combined with a hash method, this is the basic digital * signature operation. *//* ... */ if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA || public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_RSA) { /* Make sure the public algorithm of the issuer certificate is RSA. */ if (issuer_certificate -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(NX_SECURE_X509_WRONG_SIGNATURE_METHOD); }if (issuer_certificate -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_RSA) { ... } if (public_cipher_method -> nx_crypto_init != NX_CRYPTO_NULL) { /* Initialize the crypto method with public key. */ status = public_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)public_cipher_method, (UCHAR *)issuer_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus, (NX_CRYPTO_KEY_SIZE)(issuer_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_modulus_length << 3), &handler, certificate -> nx_secure_x509_public_cipher_metadata_area, certificate -> nx_secure_x509_public_cipher_metadata_size); if(status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (public_cipher_method -> nx_crypto_init != NX_CRYPTO_NULL) { ... } if (public_cipher_method -> nx_crypto_operation != NX_CRYPTO_NULL) { status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_DECRYPT, handler, (NX_CRYPTO_METHOD*)public_cipher_method, (UCHAR *)issuer_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_exponent, (NX_CRYPTO_KEY_SIZE)(issuer_certificate -> nx_secure_x509_public_key.rsa_public_key.nx_secure_rsa_public_exponent_length << 3), (UCHAR *)signature_data, signature_length, NX_CRYPTO_NULL, decrypted_signature, sizeof(decrypted_signature), certificate -> nx_secure_x509_public_cipher_metadata_area, certificate -> nx_secure_x509_public_cipher_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); if(status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (public_cipher_method -> nx_crypto_operation != NX_CRYPTO_NULL) { ... } if (public_cipher_method -> nx_crypto_cleanup) { status = public_cipher_method -> nx_crypto_cleanup(certificate -> nx_secure_x509_public_cipher_metadata_area); if(status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); NX_SECURE_MEMSET(decrypted_signature, 0, sizeof(decrypted_signature));/* ... */ #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (public_cipher_method -> nx_crypto_cleanup) { ... } /* Decode the decrypted signature, which should be in PKCS#7 format. */ status = _nx_secure_x509_pkcs7_decode(decrypted_signature, signature_length, &oid, &oid_length, &decrypted_hash, &decrypted_hash_length); #ifdef NX_SECURE_KEY_CLEAR if(status != NX_SECURE_X509_SUCCESS || decrypted_hash_length != hash_length) { /* Clear secrets state on errors. */ NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); NX_SECURE_MEMSET(decrypted_signature, 0, sizeof(decrypted_signature)); }if (status != NX_SECURE_X509_SUCCESS || decrypted_hash_length != hash_length) { ... } /* ... */#endif /* NX_SECURE_KEY_CLEAR */ if (status != NX_SECURE_X509_SUCCESS) { return(status); }if (status != NX_SECURE_X509_SUCCESS) { ... } if (decrypted_hash_length != hash_length) { return(NX_SECURE_X509_WRONG_SIGNATURE_METHOD); }if (decrypted_hash_length != hash_length) { ... } /* Compare generated hash with decrypted hash. */ compare_result = (UINT)NX_SECURE_MEMCMP(generated_hash, decrypted_hash, decrypted_hash_length); #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); NX_SECURE_MEMSET(decrypted_signature, 0, sizeof(decrypted_signature));/* ... */ #endif /* NX_SECURE_KEY_CLEAR */ /* If the comparision worked, return success. */ if (compare_result == 0) { return(NX_SECURE_X509_SUCCESS); }if (compare_result == 0) { ... } }if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA || public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_RSA) { ... } #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE else if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { /* Make sure the public algorithm of the issuer certificate is EC. */ if (issuer_certificate -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { return(NX_SECURE_X509_WRONG_SIGNATURE_METHOD); }if (issuer_certificate -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { ... } /* Verify the ECDSA signature. */ ec_pubkey = &issuer_certificate -> nx_secure_x509_public_key.ec_public_key; /* Find out which named curve the remote certificate is using. */ status = _nx_secure_x509_find_curve_method((USHORT)(ec_pubkey -> nx_secure_ec_named_curve), &curve_method); #ifdef NX_SECURE_KEY_CLEAR if(status != NX_SECURE_X509_SUCCESS || curve_method == NX_CRYPTO_NULL) { /* Clear secrets on errors. */ NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); }if (status != NX_SECURE_X509_SUCCESS || curve_method == NX_CRYPTO_NULL) { ... } /* ... */#endif /* NX_SECURE_KEY_CLEAR */ if(status != NX_SECURE_X509_SUCCESS) { return(status); }if (status != NX_SECURE_X509_SUCCESS) { ... } if (curve_method == NX_CRYPTO_NULL) { /* The issuer certificate is using an unsupported curve. */ return(NX_SECURE_X509_UNSUPPORTED_PUBLIC_CIPHER); }if (curve_method == NX_CRYPTO_NULL) { ... } if (public_cipher_method -> nx_crypto_init != NX_CRYPTO_NULL) { status = public_cipher_method -> nx_crypto_init((NX_CRYPTO_METHOD*)public_cipher_method, (UCHAR *)ec_pubkey -> nx_secure_ec_public_key, (NX_CRYPTO_KEY_SIZE)(ec_pubkey -> nx_secure_ec_public_key_length << 3), &handler, certificate -> nx_secure_x509_public_cipher_metadata_area, certificate -> nx_secure_x509_public_cipher_metadata_size); if (status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } }if (public_cipher_method -> nx_crypto_init != NX_CRYPTO_NULL) { ... } if (public_cipher_method -> nx_crypto_operation == NX_CRYPTO_NULL) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(NX_SECURE_X509_MISSING_CRYPTO_ROUTINE); }if (public_cipher_method -> nx_crypto_operation == NX_CRYPTO_NULL) { ... } status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET, handler, (NX_CRYPTO_METHOD*)public_cipher_method, NX_CRYPTO_NULL, 0, (UCHAR *)curve_method, sizeof(NX_CRYPTO_METHOD *), NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, certificate -> nx_secure_x509_public_cipher_metadata_area, certificate -> nx_secure_x509_public_cipher_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); if (status != NX_CRYPTO_SUCCESS) { #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ return(status); }if (status != NX_CRYPTO_SUCCESS) { ... } status = public_cipher_method -> nx_crypto_operation(NX_CRYPTO_VERIFY, handler, (NX_CRYPTO_METHOD*)public_cipher_method, (UCHAR *)ec_pubkey -> nx_secure_ec_public_key, (NX_CRYPTO_KEY_SIZE)(ec_pubkey -> nx_secure_ec_public_key_length << 3), generated_hash, hash_method -> nx_crypto_ICV_size_in_bits >> 3, NX_CRYPTO_NULL, (UCHAR *)signature_data, signature_length, certificate -> nx_secure_x509_public_cipher_metadata_area, certificate -> nx_secure_x509_public_cipher_metadata_size, NX_CRYPTO_NULL, NX_CRYPTO_NULL); #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ if (status == NX_CRYPTO_SUCCESS) { return(NX_SECURE_X509_SUCCESS); }if (status == NX_CRYPTO_SUCCESS) { ... } }else if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA) { ... } /* ... */#endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */ else { return(NX_SECURE_X509_UNSUPPORTED_PUBLIC_CIPHER); }else { ... } #ifdef NX_SECURE_KEY_CLEAR NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash)); #endif /* NX_SECURE_KEY_CLEAR */ /* Comparison failed, return error. */ return(NX_SECURE_X509_CERTIFICATE_SIG_CHECK_FAILED); }{ ... }