-
Notifications
You must be signed in to change notification settings - Fork 7
/
easytlv.c
410 lines (345 loc) · 11 KB
/
easytlv.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
/**
* _____ _____ _ _ _
* | ___| |_ _| | | | | |
* | |__ __ _ ___ _ _| | | | | | | |
* | __|/ _` / __| | | | | | | | | | |
* | |__| (_| \__ \ |_| | | | |___\ \_/ /
* \____/\__,_|___/\__, \_/ \_____/\___/
* __/ |
* |___/
*
* This library implements the ASN1 Tag-Length-Value encoding standard. For
* more information about this standard, see the ISO8825-1 Basic Encoding
* Rules: https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
*
* Note: This library does not implement indefinite length TLV objects, as
* described in section 8.1.3
*/
/**
* MIT License
*
* Copyright (c) 2020 John Boyd <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "easytlv.h"
#include <stdint.h>
// Debugging
#ifndef ETLV_DEBUG
#define ETLV_PRINTF(...)
#define ETLV_LOG_HEX(...)
#else
#include "easytlv_debug.h"
#endif
#define ETLV_LOG(...) ETLV_PRINTF("<etlv debug> " __VA_ARGS__)
#define ETLV_LOG_LINE() ETLV_PRINTF("\n\r")
#define GET_MSBYTE(i) ((i >> 24) & 0xFF)
// Compute the minimum number of bytes required to represent a number
static inline uint8_t min_size(uint32_t d)
{
uint8_t n = 0;
while (d) {
d >>= 8;
n++;
}
return n;
}
// Left-shift a number, until it has no leading zeros
static inline uint32_t trim_leading_zeros(uint32_t i)
{
while (i <= 0x00FFFFFF)
i <<= 8;
return i;
}
// Read the tag field from the source buffer and decode it
// Modifies src to point to next byte after tag bytes
// Returns tag, or negative error
static inline int decode_tag(uint32_t* tag, const uint8_t** src, int srcLen)
{
if (!tag || !src || !*src)
return ETLV_ERR_BADARG;
if (srcLen <= 0)
return ETLV_ERR_NODATA;
const uint8_t* s = *src;
const uint8_t* const BEGIN = s;
const uint8_t* const END = s + srcLen;
*src = 0; // Reset src to null
if ((*s & 0x1F) > 30) { // Is the tag extended?
// First subsequent octet must not be 0
if (srcLen < 2 || *(s+1) == 0)
return ETLV_ERR_INVAL;
*tag = *s++;
do {
// Detect overflow
if ((*tag << 8) < *tag)
return ETLV_ERR_OVERFLOW;
// Join each octet of the tag
*tag = (*tag << 8) + (*s & 0xFF);
} while (s < END && *s++ & 0x80); // Last octet will clear MSB
if (s > END)
return ETLV_ERR_MSGSIZE;
*src = s;
} else { // Tag is just one byte
*src = s+1;
*tag = *s;
}
return s-BEGIN;
}
// Encode the tag and Write it to the destination buffer
// Modifies dest to point to next byte after tag bytes
// Returns number of bytes written, or negative error
static inline int encode_tag(uint8_t** dest, int destLen, uint32_t tag)
{
if (!dest || !*dest)
return ETLV_ERR_BADARG;
if (destLen <= 0)
return ETLV_ERR_NOMEM;
uint8_t* d = *dest;
const uint8_t* const BEGIN = d;
const uint8_t* const END = d + destLen;
*dest = 0; // Reset dest to null
if (tag < 0 || tag > 0xFF) { // Extended tag
// Scan to first tag byte
tag = trim_leading_zeros(tag);
// Check first byte is valid extended byte
if ((GET_MSBYTE(tag) & 0x1F) < 31)
return ETLV_ERR_INVAL;
// Write tag bytes
while (tag > 0x00FFFFFF) {
// Check for enough memory
if (d >= END)
return ETLV_ERR_NOMEM;
// Copy next tag byte
*d++ = GET_MSBYTE(tag);
tag <<= 8;
}
} else { // Short tag
if ((tag & 0x1F) > 30)
return ETLV_ERR_INVAL; // Invalid short tag
// Check for end of buffer
if (d >= END)
return ETLV_ERR_NOMEM;
// Write tag byte
*d++ = tag & 0xFF;
}
*dest = d;
return d-BEGIN;
}
// Read the length field from the source buffer and decode it
// Modifies src to point to next byte after length bytes
// Returns number of bytes read or negative error
static inline int decode_length(uint32_t* length, const uint8_t** src, int srcLen)
{
if (!length || !src || !*src)
return ETLV_ERR_BADARG;
if (srcLen <= 0)
return ETLV_ERR_NODATA;
const uint8_t* s = *src;
const uint8_t* const BEGIN = s;
const uint8_t* const END = s + srcLen;
*src = (void*) 0; // Reset src to null
if (*s & 0x80) { // Is the length in long form?
// The first byte must not be 0xFF
if (*s == 0xFF)
return ETLV_ERR_INVAL;
const int N = *s++ & 0x7F; // Number of length bytes to use
if (N > 4 || (N > 3 && s[3] & 0x80))
return ETLV_ERR_OVERFLOW;
int len = 0;
for(int i = 0; i < N; i++)
len = (len << 8) + s[i];
*src = s+N;
*length = len;
} else { // Tag is just one byte
*src = s+1;
*length = *s;
}
if (s >= END)
return ETLV_ERR_MSGSIZE;
return s-BEGIN;
}
// Encode the length and Write it to the destination buffer
// Modifies dest to point to next byte after length bytes
// Returns number of bytes written, or negative error
static inline int encode_length(uint8_t** dest, int destLen, uint32_t length)
{
if (!dest || !*dest || length <= 0)
return ETLV_ERR_BADARG;
if (destLen <= 0)
return ETLV_ERR_NOMEM;
uint8_t* d = *dest;
const uint8_t* const BEGIN = d;
const uint8_t* const END = d + destLen;
*dest = 0; // Reset dest to null
if (length > 0x7F) { // Long form
// Determine how many length bytes
int n = min_size(length);
if (n > 0x7E) // This long cannot fit in TLV encoding
return ETLV_ERR_OVERFLOW;
if (d >= END)
return ETLV_ERR_NOMEM;
*d++ = (n | 0x80) & 0xFF;
// Write length bytes
length = trim_leading_zeros(length);
while (n-- > 0) {
if (d >= END)
return ETLV_ERR_NOMEM;
*d++ = GET_MSBYTE(length);
length <<= 8;
}
} else { // Short form
if (d >= END)
return ETLV_ERR_NOMEM;
*d++ = length & 0xFF;
}
*dest = d;
return d-BEGIN;
}
int etlv_parse(ETLVToken* t, int* nTok, const void* src, int srcLen)
{
if (!t || !nTok || *nTok < 0 || !src || srcLen < 0)
return ETLV_ERR_BADARG;
const uint8_t* s = src;
const uint8_t* const BEGIN = s;
const uint8_t* const END = s + srcLen;
ETLV_LOG("Parse input: ");
ETLV_LOG_HEX(src, srcLen);
ETLV_LOG_LINE();
int n = 0;
int err = 0;
while (s < END) {
// Check for memory
if (n >= *nTok) {
n = ETLV_ERR_NOMEM;
break;
}
// Decode the tag field
err = decode_tag(&t[n].tag, &s, END-s);
if (err < 0) {
n = err;
break;
}
ETLV_LOG("tag: %08X\n\r", t[n].tag);
// Decode the length field
err = decode_length(&t[n].len, &s, END-s);
if (err < 0) {
n = err;
break;
}
ETLV_LOG("len: %u\n\r", t[n].len);
// Save pointer to value field
t[n].val = s;
if (t[n].val == 0) {
n = ETLV_ERR_UNKNOWN;
break;
}
ETLV_LOG("val: ");
ETLV_LOG_HEX(t[n].val, t[n].len);
ETLV_LOG_LINE();
// Point to next object
s = t[n].val + t[n].len;
n++;
}
// Output the number of tokens found
if (n >= 0)
*nTok = n;
// Handle errors
if (n < 0) // Error during parse loop
return n;
if (s > END) // TLV data exceeds byte array provided
return ETLV_ERR_MSGSIZE;
// Return the total length of the TLV data
return s-BEGIN;
}
int etlv_serialize(void* dest, int* len, const ETLVToken* t, int nTok)
{
if (!dest || !len || *len < 0 || !t || nTok < 0)
return ETLV_ERR_BADARG;
uint8_t* d = dest;
const uint8_t* const BEGIN = d;
const uint8_t* const END = d + *len;
ETLV_LOG("Serializing %d tokens into %i byte buffer\n\r", nTok, *len);
*len = 0;
int err = 0;
for (int i = 0; i < nTok; i++) {
// Write the tag field
ETLV_LOG("tag: %08X\n\r", t[i].tag);
err = encode_tag(&d, END-d, t[i].tag);
if (err < 0)
return err;
if (*len + err < *len)
return ETLV_ERR_OVERFLOW;
// Write the length field
ETLV_LOG("len: %u\n\r", t[i].len);
err = encode_length(&d, END-d, t[i].len);
if (err < 0)
return err;
if (*len + err < *len)
return ETLV_ERR_OVERFLOW;
// Copy the value
ETLV_LOG("val: ");
ETLV_LOG_HEX(t[i].val, t[i].len);
ETLV_LOG_LINE();
const uint8_t* v = t[i].val;
const uint8_t* const VAL_END = v + t[i].len;
while (v < VAL_END) {
// Make sure not to overrun the buffer
if (d >= END)
return ETLV_ERR_NOMEM;
*d++ = *v++;
}
}
// Return total serialized length
return *len = d-BEGIN;
}
int etlv_find(ETLVToken* t, uint32_t tag, const void* src, int srcLen)
{
if (!t || !src || srcLen < 0)
return ETLV_ERR_BADARG;
const uint8_t* s = src;
const uint8_t* const BEGIN = s;
const uint8_t* const END = s + srcLen;
int err;
uint32_t found;
uint32_t len;
int offset = 0;
while (s < END) {
offset = s-BEGIN;
err = decode_tag(&found, &s, END-s);
if (err < 0)
break;
ETLV_LOG("Found tag: 0x%08X\n\r", found);
err = decode_length(&len, &s, END-s);
if (err < 0)
break;
if (found == tag)
break;
s += len;
}
if (err < 0)
return err;
if (s >= END)
return ETLV_ERR_NOENT;
// Output a token for the found tag
t->tag = found;
t->len = len;
t->val = s;
// Return the byte offset of the found token
return offset;
}