-
Notifications
You must be signed in to change notification settings - Fork 8
/
cie_BerReader.cpp
391 lines (325 loc) · 15.3 KB
/
cie_BerReader.cpp
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
/**************************************************************************/
/*!
@file cie_BerReader.cpp
@author Developers Italia
@license BSD (see License)
Reads and parses a BER encoded binary content
@section HISTORY
v1.0 - Reading of fragments and binary values
*/
/**************************************************************************/
#include "cie_BerReader.h"
#define PN532DEBUGPRINT Serial
//Please refer to https://en.wikipedia.org/wiki/X.690#Identifier_octets
cie_BerReader::cie_BerReader (cie_PN532 *cie) :
_cie(cie),
_currentOffset(0)
{
}
/**************************************************************************/
/*!
@brief Reads the triples hierarchy in the BER encoded file
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param cieBerTripleCallbackFunc A callback function that will be invoked each time a triple has been found
@param maxDepth How deep down in the hierarchy we should look for triples
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::readTriples(const cie_EFPath filePath, cieBerTripleCallbackFunc callback, word *length, const byte maxDepth) {
resetCursor();
if (maxDepth < 1) {
PN532DEBUGPRINT.println(F("Warning: you choose a maxDepth of 0 which won't read any triple"));
return true;
}
byte currentDepth = 1;
byte triplesCount = 0;
cie_BerTriple *tripleStack = new cie_BerTriple[maxDepth];
bool willEncapsulate = false;
byte oid_subjectKeyIdentifier[] = {0x67, 0x81, 0x08, 0x01, 0x01, 0x01}; //2.5.29.14
byte oid_keyUsage[] = {0x55, 0x1D, 0x0F}; //2.5.29.15
byte oid_authorityKeyIdentifier[] = {0x55, 0x1D, 0x23}; //2.5.29.35
byte oid_rsaEncryption[] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01}; //1.2.840.113549.1.1.1
byte oid_mRTDSignatureData[] = {0x67, 0x81, 0x08, 0x01, 0x01, 0x01}; //2.23.136.1.1.1
bool result = true;
*length = 0;
do {
word tripleLength;
if (!readTriple(filePath, &tripleStack[currentDepth-1], &tripleLength)) {
result = false;
break;
}
if (*length == 0) {
*length = tripleLength;
}
tripleStack[currentDepth-1].depth = currentDepth;
byte isBinaryString = tripleStack[currentDepth-1].type == 0x03 || tripleStack[currentDepth-1].type == 0x04;
bool isObjectIdentifier = tripleStack[currentDepth-1].type == 0x06;
//Some OCTET STRINGS and BIT STRING might be encapsulating other ASN.1 triples
//Find them by their preceding Object Identifier
if (isObjectIdentifier)
{
byte oid[tripleStack[currentDepth-1].contentLength];
_cie->readBinaryContent(filePath, oid, tripleStack[currentDepth-1].contentOffset, tripleStack[currentDepth-1].contentLength);
if (areEqual(oid, tripleStack[currentDepth-1].contentLength, oid_subjectKeyIdentifier, sizeof(oid_subjectKeyIdentifier)) ||
areEqual(oid, tripleStack[currentDepth-1].contentLength, oid_keyUsage, sizeof(oid_keyUsage)) ||
areEqual(oid, tripleStack[currentDepth-1].contentLength, oid_authorityKeyIdentifier, sizeof(oid_authorityKeyIdentifier)) ||
areEqual(oid, tripleStack[currentDepth-1].contentLength, oid_rsaEncryption, sizeof(oid_rsaEncryption)) ||
(areEqual(oid, tripleStack[currentDepth-1].contentLength, oid_mRTDSignatureData, sizeof(oid_mRTDSignatureData)) && (currentDepth+1<=maxDepth))) {
willEncapsulate = true;
} else {
willEncapsulate = false;
}
} else if (isBinaryString && willEncapsulate) {
willEncapsulate = false;
tripleStack[currentDepth-1].encoding = 0x01;
}
if (*callback != NULL) {
if (!(*callback)(&tripleStack[currentDepth-1])) {
//Navigation interrupted by callback, we stop here.
result = true;
break;
}
}
triplesCount += 1;
if (tripleStack[currentDepth-1].contentLength > BER_READER_MAX_LENGTH) {
PN532DEBUGPRINT.print(F("Sorry, we don't support content lengths greater than "));
PN532DEBUGPRINT.println(BER_READER_MAX_LENGTH);
result = false;
break;
}
if (tripleStack[currentDepth-1].contentOffset > BER_READER_MAX_OFFSET) {
PN532DEBUGPRINT.print(F("Sorry, we don't support content offsets greater than "));
PN532DEBUGPRINT.println(BER_READER_MAX_OFFSET);
result = false;
break;
}
if (triplesCount > BER_READER_MAX_COUNT) {
PN532DEBUGPRINT.print(F("Sorry, we don't support as many triples as "));
PN532DEBUGPRINT.println(BER_READER_MAX_COUNT);
result = false;
break;
}
word contentLength = tripleStack[currentDepth-1].contentLength;
word myOffset = tripleStack[currentDepth-1].offset;
//check if we've finished reading a parent (or granparent) triple and go up levels accordingly
if (currentDepth > 1) {
//we can go up only if we're not at the root level
for (byte i = currentDepth-1; i>0; i--) {
bool isConstructed = tripleStack[i].encoding == 0x01;
bool isLastInParent = _currentOffset + contentLength >= tripleStack[i-1].contentOffset + tripleStack[i-1].contentLength;
bool canGoDown = currentDepth + 1 <= maxDepth;
bool stillToRead = _currentOffset < (tripleStack[i].contentOffset + tripleStack[i].contentLength);
if (isLastInParent && !(isConstructed && canGoDown && stillToRead)) {
currentDepth-=1;
_currentOffset = tripleStack[i].contentOffset + tripleStack[i].contentLength;
} else {
break;
}
}
}
//Let's see if we should go down one level
bool isConstructed = tripleStack[currentDepth-1].encoding == 0x01;
bool isAtBeginning = _currentOffset == tripleStack[currentDepth-1].contentOffset;
bool hasNonZeroLength = tripleStack[currentDepth-1].contentLength > 0;
bool canGoDown = currentDepth + 1 <= maxDepth;
if (isConstructed && isAtBeginning && hasNonZeroLength && canGoDown) {
currentDepth += 1;
} else if (!isConstructed || !canGoDown) {
_currentOffset = tripleStack[currentDepth-1].contentOffset + tripleStack[currentDepth-1].contentLength;
}
} while(_currentOffset < *length);
delete [] tripleStack;
return result;
}
/**************************************************************************/
/*!
@brief Extracts informations from a single triple
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param triple The pointer to the triple object
@param length The pointer to the outer length (tag octets + length octets + content length)
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::readTriple(const cie_EFPath filePath, cie_BerTriple *triple, word *length) {
byte tagOctets, lengthOctets;
if (!detectTag(filePath, &triple->classification, &triple->encoding, &triple->type, &tagOctets) ||
!detectLength(filePath, &triple->contentOffset, &triple->contentLength, &lengthOctets)) {
return false;
}
triple->offset = triple->contentOffset - tagOctets - lengthOctets;
*length = tagOctets + lengthOctets + triple->contentLength;
return true;
}
/**************************************************************************/
/*!
@brief Reads the binary content of a triple into the buffer
@param triple The triple to be read
@param buffer The pointer to the buffer where the binary content will be written to
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::readTripleValue(const cie_BerTriple triple, byte *buffer) {
resetCursor();
//TODO leggi valore
return true;
}
/**************************************************************************/
/*!
@brief Detects the content length in a triple
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param contentOffset The pointer to binary content offset
@param contentLength The pointer to binary content length
@param lengthOctets The pointer to the number of length octets found in this triple)
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::detectLength(const cie_EFPath filePath, word *contentOffset, word *contentLength, byte *lengthOctets) {
if (!readOctet(filePath, lengthOctets)) {
PN532DEBUGPRINT.println(F("Couldn't detect length of a BER encoded file"));
return false;
}
if (*lengthOctets == 0b10000000)
{
//Indefinite mode, we don't currently support this as it's sloooooow
PN532DEBUGPRINT.println(F("Indefinite length for BER encoded file not supported"));
return false;
}
else if ((*lengthOctets & 0b10000000) == 0b10000000)
{
//Definite, long
//In the initial octet, bit 8 is 1, and bits 1–7 (excluding the values 0 and 127) encode the number of octets that follow.
*lengthOctets &= 0b1111111;
if (*lengthOctets == 0 || *lengthOctets == 127) {
PN532DEBUGPRINT.println(F("Invalid value for a BER encoded file length"));
return false;
}
*contentLength = 0;
byte *buffer = new byte[*lengthOctets];
readOctets(filePath, buffer, *lengthOctets);
//The following octets encode, as big-endian, the length (which may be 0) as a number of octets.
for (byte i = 0; i < *lengthOctets; i++)
{
*contentLength <<= 8;
*contentLength |= buffer[i];
}
//Don't forget to add the first length octet
*lengthOctets+=1;
delete [] buffer;
} else {
//Definite, short
*contentLength = *lengthOctets;
*lengthOctets = 1;
}
*contentOffset = _currentOffset;
return true;
}
/**************************************************************************/
/*!
@brief Detects the tag type, class and content encoding
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param classification The pointer to the detected class value
@param encoding The pointer to the detected binary content encoding (either primitive or constructed)
@param type The pointer to the detected type value
@param tagOctets The pointer to the number of tag octets found in this triple
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::detectTag (const cie_EFPath filePath, byte *classification, byte *encoding, unsigned int *type, byte *tagOctets) {
byte tag = 0x00;
*tagOctets = 0;
while (tag == 0x00) { //End of content can't be a tag
if (!readOctet(filePath, &tag)) {
PN532DEBUGPRINT.println(F("Couldn't detect a tag in the BER content"));
return false;
}
*tagOctets += 1;
}
//bit 6 encodes whether the type is primitive or constructed
*encoding = (byte)((tag >> 5) & 0b1);
//bit 7–8 encode the class of the type
*classification = (byte)((tag >> 6) & 0b11);
//bits 1–5 encode the tag number.
*type = (unsigned int) (tag & 0b11111);
//Where the identifier is not universal, its tag number may be too large for the 5-bit tag field, so it is encoded in further octets.
//bits 1–5 are 1
if (*type == 0b11111)
{
*type = 0x00;
//The tag number is encoded in the following octets, where bit 8 of each is 1 if there are more octets
bool moreOctets = false;
do
{
*tagOctets += 1;
if (!readOctet(filePath, &tag)) {
PN532DEBUGPRINT.println(F("Couldn't detect a tag in the BER content"));
return false;
}
//bits 1–7 encode the tag number. The tag number bits combined, big-endian, encode the tag number
*type <<= 7;
*type |= (byte)(tag & 0b1111111);
//bit 8 of each is 1 if there are more octets
moreOctets = ((tag & 0b10000000) == 0b10000000);
} while (moreOctets);
return true;
}
return true;
}
/**************************************************************************/
/*!
@brief Reads bytes from the file
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param buffer The pointer to the buffer which will contain the octets
@param offset The offset from which we should read
@param length The number of bytes to read
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::readOctets(const cie_EFPath filePath, byte *buffer, const word offset, const word length) {
if (!_cie->readBinaryContent(filePath, buffer, offset, length)) {
return false;
}
_currentOffset = offset+length;
return true;
}
/**************************************************************************/
/*!
@brief Reads bytes from the file
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param buffer The pointer to the buffer which will contain the octets
@param length The number of bytes to read
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::readOctets(const cie_EFPath filePath, byte *buffer, const word length) {
return readOctets(filePath, buffer, _currentOffset, length);
}
/**************************************************************************/
/*!
@brief Reads bytes from the file
@param filePath a structure indicating the parent Dedicated File (either ROOT_MF or CIE_DF), the selection mode (either SELECT_BY_EFID or SELECT_BY_SFI) and the file identifier (either a sfi or an efid)
@param octet The pointer to the byte read
@returns A value indicating whether the operation succeeded or not
*/
/**************************************************************************/
bool cie_BerReader::readOctet(const cie_EFPath filePath, byte *octet) {
return readOctets(filePath, octet, _currentOffset, 1);
}
bool cie_BerReader::areEqual(byte *buffer1, byte length1, byte *buffer2, byte length2) {
if (length1 != length2) {
return false;
}
for (byte i = 0; i < length1; i++) {
if (buffer1[i] != buffer2[i]) {
return false;
}
}
return true;
}
/**************************************************************************/
/*!
@brief Resets the cursor to the binary content
*/
/**************************************************************************/
void cie_BerReader::resetCursor() {
_currentOffset = 0;
}