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
90
91
92
93
94
98
99
100
105
106
107
112
113
114
115
116
117
118
119
120
121
122
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
199
200
201
202
203
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
226
227
228
229
230
234
240
246
247
248
252
253
254
255
256
257
258
259
260
261
262
263
272
276
277
278
279
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_x509.h"
static UINT _nx_secure_x509_asn1_time_to_unix_convert(const UCHAR *asn1_time, USHORT asn1_length,
USHORT format, ULONG *unix_time);
...
...
UINT _nx_secure_x509_expiration_check(NX_SECURE_X509_CERT *certificate, ULONG current_time)
{
ULONG not_before;
ULONG not_after;
UINT status;
status = _nx_secure_x509_asn1_time_to_unix_convert(certificate -> nx_secure_x509_not_before, certificate -> nx_secure_x509_not_before_length,
certificate -> nx_secure_x509_validity_format, ¬_before);
if (status != NX_SECURE_X509_SUCCESS)
{
return(status);
}if (status != NX_SECURE_X509_SUCCESS) { ... }
status = _nx_secure_x509_asn1_time_to_unix_convert(certificate -> nx_secure_x509_not_after, certificate -> nx_secure_x509_not_after_length,
certificate -> nx_secure_x509_validity_format, ¬_after);
if (status != NX_SECURE_X509_SUCCESS)
{
return(status);
}if (status != NX_SECURE_X509_SUCCESS) { ... }
if (current_time > not_after)
{
return(NX_SECURE_X509_CERTIFICATE_EXPIRED);
}if (current_time > not_after) { ... }
if (current_time < not_before)
{
return(NX_SECURE_X509_CERTIFICATE_NOT_YET_VALID);
}if (current_time < not_before) { ... }
return(NX_SECURE_X509_SUCCESS);
}{ ... }
#define date_2_chars_to_int(buffer, index) (LONG)(((buffer[index] - '0') * 10) + (buffer[index + 1] - '0'))
/* ... */
static const UINT days_before_month[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
...
...
static UINT _nx_secure_x509_asn1_time_to_unix_convert(const UCHAR *asn1_time, USHORT asn1_length,
USHORT format, ULONG *unix_time)
{
LONG year, month, day, hour, minute, second;
UINT index;
NX_CRYPTO_PARAMETER_NOT_USED(asn1_length);
index = 0;
if (format == NX_SECURE_ASN_TAG_UTC_TIME)
{
year = date_2_chars_to_int(asn1_time, 0);
month = date_2_chars_to_int(asn1_time, 2);
day = date_2_chars_to_int(asn1_time, 4) - 1;
hour = date_2_chars_to_int(asn1_time, 6);
minute = date_2_chars_to_int(asn1_time, 8);
second = 0;
index = 10;
if (asn1_time[index] != 'Z' && asn1_time[index] != '+' && asn1_time[index] != '-')
{
second = date_2_chars_to_int(asn1_time, index);
index += 2;
}if (asn1_time[index] != 'Z' && asn1_time[index] != '+' && asn1_time[index] != '-') { ... }
if (asn1_time[index] != 'Z')
{
/* ... */
if (asn1_time[index] == '+')
{
index++;
hour -= date_2_chars_to_int(asn1_time, index);
index += 2;
minute -= date_2_chars_to_int(asn1_time, index);
index += 2;
}if (asn1_time[index] == '+') { ... }
else if (asn1_time[index] == '-')
{
index++;
hour += date_2_chars_to_int(asn1_time, index);
index += 2;
minute += date_2_chars_to_int(asn1_time, index);
index += 2;
}else if (asn1_time[index] == '-') { ... }
else
{
return(NX_SECURE_X509_INVALID_DATE_FORMAT);
}else { ... }
}if (asn1_time[index] != 'Z') { ... }
/* ... */
if (year >= 70)
{
year -= 70;
day += ((year + 2) / 4);
}if (year >= 70) { ... }
else
{
year += 30;
day += ((year - 2) / 4) + 1;
}else { ... }
if (((year + 2) % 4 == 0) && (month < 3))
{
day -= 1;
}if (((year + 2) % 4 == 0) && (month < 3)) { ... }
day += year * 365;
day += (LONG)days_before_month[month - 1];
hour += day * 24;
minute += hour * 60;
second += minute * 60;
*unix_time = (ULONG)second;
}if (format == NX_SECURE_ASN_TAG_UTC_TIME) { ... }
else if (format == NX_SECURE_ASN_TAG_GENERALIZED_TIME)
{
/* ... */
return(NX_SECURE_X509_INVALID_DATE_FORMAT);
}else if (format == NX_SECURE_ASN_TAG_GENERALIZED_TIME) { ... }
else
{
return(NX_SECURE_X509_INVALID_DATE_FORMAT);
}else { ... }
return(NX_SECURE_X509_SUCCESS);
}{ ... }