1
10
13
14
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
114
115
116
117
118
119
132
133
134
135
136
137
138
139
140
141
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
186
187
188
200
201
202
203
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
263
264
265
279
280
281
282
283
284
285
292
293
297
298
302
303
304
305
306
307
309
310
311
312
316
317
318
319
320
321
328
329
330
331
332
333
334
335
336
337
343
344
348
349
354
355
356
357
358
359
360
361
362
363
370
371
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
415
416
417
421
422
423
424
425
426
427
428
429
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_x509.h"
#ifndef NX_SECURE_X509_DISABLE_CRL
static UCHAR generated_hash[64];
static UCHAR decrypted_signature[512];
...
UINT _nx_secure_x509_crl_verify(NX_SECURE_X509_CERT *certificate, NX_SECURE_X509_CRL *crl,
NX_SECURE_X509_CERTIFICATE_STORE *store,
NX_SECURE_X509_CERT *issuer_certificate)
{
UINT status;
UINT oid_length;
const UCHAR *oid;
UINT decrypted_hash_length;
const UCHAR *decrypted_hash;
const UCHAR *crl_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_CRYPTO_PARAMETER_NOT_USED(store);
#ifndef NX_SECURE_X509_DISABLE_KEY_USAGE_CHECK
status = _nx_secure_x509_key_usage_extension_parse(issuer_certificate, &key_usage_bitfield);
if(NX_SECURE_X509_SUCCESS == status)
{
if(!(key_usage_bitfield & NX_SECURE_X509_KEY_USAGE_CRL_SIGN))
{
return(NX_SECURE_X509_KEY_USAGE_ERROR);
}if (!(key_usage_bitfield & NX_SECURE_X509_KEY_USAGE_CRL_SIGN)) { ... }
}if (NX_SECURE_X509_SUCCESS == status) { ... }
/* ... */#endif
crl_verify_data = crl -> nx_secure_x509_crl_verify_data;
verify_data_length = crl -> nx_secure_x509_crl_verify_data_length;
signature_data = crl -> nx_secure_x509_crl_signature_data;
signature_length = crl -> nx_secure_x509_crl_signature_data_length;
status = _nx_secure_x509_find_certificate_methods(certificate, crl -> nx_secure_x509_crl_signature_algorithm, &crypto_methods);
if (status != NX_SECURE_X509_SUCCESS)
{
return(status);
}if (status != NX_SECURE_X509_SUCCESS) { ... }
hash_method = crypto_methods -> nx_secure_x509_hash_method;
public_cipher_method = crypto_methods -> nx_secure_x509_public_cipher_method;
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) { ... }
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 *)crl_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
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (hash_method -> nx_crypto_cleanup) { ... }
hash_length = (hash_method -> nx_crypto_ICV_size_in_bits >> 3);
/* ... */
if (public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_KEY_EXCHANGE_RSA ||
public_cipher_method -> nx_crypto_algorithm == NX_CRYPTO_DIGITAL_SIGNATURE_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
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)
{
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
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
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
return(status);
}if (status != NX_CRYPTO_SUCCESS) { ... }
}if (public_cipher_method -> nx_crypto_cleanup) { ... }
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)
{
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
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_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
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)
{
if (issuer_certificate -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC)
{
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash));
#endif
return(NX_SECURE_X509_WRONG_SIGNATURE_METHOD);
}if (issuer_certificate -> nx_secure_x509_public_algorithm != NX_SECURE_TLS_X509_TYPE_EC) { ... }
ec_pubkey = &issuer_certificate -> nx_secure_x509_public_key.ec_public_key;
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)
{
NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash));
}if (status != NX_SECURE_X509_SUCCESS || curve_method == NX_CRYPTO_NULL) { ... }
/* ... */#endif
if(status != NX_SECURE_X509_SUCCESS)
{
return(status);
}if (status != NX_SECURE_X509_SUCCESS) { ... }
if (curve_method == NX_CRYPTO_NULL)
{
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
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
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
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
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
else
{
return(NX_SECURE_X509_UNSUPPORTED_PUBLIC_CIPHER);
}else { ... }
#ifdef NX_SECURE_KEY_CLEAR
NX_SECURE_MEMSET(generated_hash, 0, sizeof(generated_hash));
#endif
return(NX_SECURE_X509_CRL_SIGNATURE_CHECK_FAILED);
}{ ... }
#endif... /* ... */