1
10
13
14
20
21
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
145
146
147
151
156
157
158
159
160
161
167
168
169
173
174
175
176
177
178
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
207
208
209
210
211
212
213
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
240
241
245
246
247
248
249
250
251
252
253
254
255
256
...
...
...
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_x509.h"
...
...
UINT _nx_secure_x509_asn1_tlv_block_parse(const UCHAR *buffer, ULONG *buffer_length, USHORT *tlv_type,
USHORT *tlv_tag_class, ULONG *tlv_length,
const UCHAR **tlv_data, ULONG *header_length)
{
UINT current_index;
USHORT current_tag;
ULONG length;
ULONG length_bytes;
current_index = 0;
current_tag = buffer[current_index];
if (*buffer_length < 1)
{
return(NX_SECURE_X509_ASN1_LENGTH_TOO_LONG);
}if (*buffer_length < 1) { ... }
if ((current_tag & NX_SECURE_ASN_TAG_MULTIBYTE_MASK) == NX_SECURE_ASN_TAG_MULTIBYTE_MASK)
{
return(NX_SECURE_X509_MULTIBYTE_TAG_UNSUPPORTED);
}if ((current_tag & NX_SECURE_ASN_TAG_MULTIBYTE_MASK) == NX_SECURE_ASN_TAG_MULTIBYTE_MASK) { ... }
else
{
*header_length = 1;
*buffer_length = *buffer_length - 1;
}else { ... }
*tlv_tag_class = (USHORT)((current_tag & NX_SECURE_ASN_TAG_CLASS_MASK) >> 6);
if (*tlv_tag_class == NX_SECURE_ASN_TAG_CLASS_APPLICATION ||
*tlv_tag_class == NX_SECURE_ASN_TAG_CLASS_PRIVATE)
{
return(NX_SECURE_X509_INVALID_TAG_CLASS);
}if (*tlv_tag_class == NX_SECURE_ASN_TAG_CLASS_APPLICATION || *tlv_tag_class == NX_SECURE_ASN_TAG_CLASS_PRIVATE) { ... }
if (current_tag & NX_SECURE_ASN_TAG_CONSTRUCTED_MASK)
{
current_tag = current_tag & (USHORT)(~NX_SECURE_ASN_TAG_CONSTRUCTED_MASK);
}if (current_tag & NX_SECURE_ASN_TAG_CONSTRUCTED_MASK) { ... }
*tlv_type = current_tag & NX_SECURE_ASN_TAG_MASK;
current_index++;
if (*buffer_length < 1)
{
return(NX_SECURE_X509_ASN1_LENGTH_TOO_LONG);
}if (*buffer_length < 1) { ... }
if (current_tag == NX_SECURE_ASN_TAG_NULL)
{
*tlv_length = 1;
*tlv_data = &buffer[current_index];
return(NX_SECURE_X509_SUCCESS);
}if (current_tag == NX_SECURE_ASN_TAG_NULL) { ... }
length = buffer[current_index];
current_index++;
*header_length = *header_length + 1;
*buffer_length = *buffer_length - 1;
if (length & 0x80)
{
/* ... */
length_bytes = length & 0x7F;
length = 0;
if (length_bytes > 4 || length_bytes > *buffer_length)
{
return(NX_SECURE_X509_ASN1_LENGTH_TOO_LONG);
}if (length_bytes > 4 || length_bytes > *buffer_length) { ... }
*header_length = *header_length + length_bytes;
*buffer_length = *buffer_length - length_bytes;
while (length_bytes > 0)
{
length <<= 8;
length += buffer[current_index];
current_index++;
length_bytes--;
}while (length_bytes > 0) { ... }
}if (length & 0x80) { ... }
else
{
/* ... */
*tlv_length = length;
}else { ... }
if (length > *buffer_length)
{
return(NX_SECURE_X509_ASN1_LENGTH_TOO_LONG);
}if (length > *buffer_length) { ... }
*buffer_length = *buffer_length - length;
*tlv_length = length;
*tlv_data = &buffer[current_index];
return(NX_SECURE_X509_SUCCESS);
}{ ... }