fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / emv / tlv.c
blob49a4038b5ed197a1b3ed88e82c71001fe1e0ee5f
1 /*
2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 2012, 2015 Dmitry Eremin-Solenikov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * https://github.com/lumag/emv-tools/blob/master/lib/tlv.c
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 #include "tlv.h"
24 #include <string.h>
25 #include <stdlib.h>
27 #define TLV_TAG_CLASS_MASK 0xc0
28 #define TLV_TAG_COMPLEX 0x20
29 #define TLV_TAG_VALUE_MASK 0x1f
30 #define TLV_TAG_VALUE_CONT 0x1f
31 #define TLV_TAG_INVALID 0
33 #define TLV_LEN_LONG 0x80
34 #define TLV_LEN_MASK 0x7f
35 #define TLV_LEN_INVALID (~0)
37 // http://radek.io/2012/11/10/magical-container_of-macro/
38 //#define container_of(ptr, type, member) ({
39 // const typeof( ((type *)0)->member ) *__mptr = (ptr);
40 // (type *)( (char *)__mptr - offsetof(type,member) );})
42 struct tlvdb {
43 struct tlv tag;
44 struct tlvdb *next;
45 struct tlvdb *parent;
46 struct tlvdb *children;
49 struct tlvdb_root {
50 struct tlvdb db;
51 size_t len;
52 unsigned char buf[0];
55 static tlv_tag_t tlv_parse_tag(const unsigned char **buf, size_t *len) {
56 tlv_tag_t tag;
58 if (*len == 0)
59 return TLV_TAG_INVALID;
60 tag = **buf;
61 --*len;
62 ++*buf;
63 if ((tag & TLV_TAG_VALUE_MASK) != TLV_TAG_VALUE_CONT)
64 return tag;
66 if (*len == 0)
67 return TLV_TAG_INVALID;
69 tag <<= 8;
70 tag |= **buf;
71 --*len;
72 ++*buf;
74 return tag;
77 static size_t tlv_parse_len(const unsigned char **buf, size_t *len) {
78 size_t l;
80 if (*len == 0)
81 return TLV_LEN_INVALID;
83 l = **buf;
84 --*len;
85 ++*buf;
87 if (!(l & TLV_LEN_LONG))
88 return l;
90 size_t ll = l & ~ TLV_LEN_LONG;
91 if (ll > 5)
92 return TLV_LEN_INVALID;
94 l = 0;
95 for (int i = 1; i <= ll; i++) {
96 l = (l << 8) + **buf;
97 --*len;
98 ++*buf;
101 return l;
104 bool tlv_parse_tl(const unsigned char **buf, size_t *len, struct tlv *tlv) {
105 tlv->value = 0;
107 tlv->tag = tlv_parse_tag(buf, len);
108 if (tlv->tag == TLV_TAG_INVALID)
109 return false;
111 tlv->len = tlv_parse_len(buf, len);
112 if (tlv->len == TLV_LEN_INVALID)
113 return false;
115 return true;
118 static struct tlvdb *tlvdb_parse_children(struct tlvdb *parent);
120 static bool tlvdb_parse_one(struct tlvdb *tlvdb,
121 struct tlvdb *parent,
122 const unsigned char **tmp,
123 size_t *left) {
124 tlvdb->next = tlvdb->children = NULL;
125 tlvdb->parent = parent;
127 tlvdb->tag.tag = tlv_parse_tag(tmp, left);
128 if (tlvdb->tag.tag == TLV_TAG_INVALID)
129 goto err;
131 tlvdb->tag.len = tlv_parse_len(tmp, left);
132 if (tlvdb->tag.len == TLV_LEN_INVALID)
133 goto err;
135 if (tlvdb->tag.len > *left)
136 goto err;
138 tlvdb->tag.value = *tmp;
140 *tmp += tlvdb->tag.len;
141 *left -= tlvdb->tag.len;
143 if (tlv_is_constructed(&tlvdb->tag) && (tlvdb->tag.len != 0)) {
144 tlvdb->children = tlvdb_parse_children(tlvdb);
145 if (!tlvdb->children)
146 goto err;
147 } else {
148 tlvdb->children = NULL;
151 return true;
153 err:
154 return false;
157 static struct tlvdb *tlvdb_parse_children(struct tlvdb *parent) {
158 const unsigned char *tmp = parent->tag.value;
159 size_t left = parent->tag.len;
160 struct tlvdb *tlvdb, *first = NULL, *prev = NULL;
162 while (left != 0) {
163 tlvdb = malloc(sizeof(*tlvdb));
164 if (prev)
165 prev->next = tlvdb;
166 else
167 first = tlvdb;
168 prev = tlvdb;
170 if (!tlvdb_parse_one(tlvdb, parent, &tmp, &left))
171 goto err;
173 tlvdb->parent = parent;
176 return first;
178 err:
179 tlvdb_free(first);
181 return NULL;
184 struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len) {
185 struct tlvdb_root *root;
186 const unsigned char *tmp;
187 size_t left;
189 if (!len || !buf)
190 return NULL;
192 root = malloc(sizeof(*root) + len);
193 root->len = len;
194 memcpy(root->buf, buf, len);
196 tmp = root->buf;
197 left = len;
199 if (!tlvdb_parse_one(&root->db, NULL, &tmp, &left))
200 goto err;
202 if (left)
203 goto err;
205 return &root->db;
207 err:
208 tlvdb_free(&root->db);
210 return NULL;
213 struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len) {
214 struct tlvdb_root *root;
215 const unsigned char *tmp;
216 size_t left;
218 if (!len || !buf)
219 return NULL;
221 root = malloc(sizeof(*root) + len);
222 root->len = len;
223 memcpy(root->buf, buf, len);
225 tmp = root->buf;
226 left = len;
228 if (!tlvdb_parse_one(&root->db, NULL, &tmp, &left))
229 goto err;
231 while (left != 0) {
232 struct tlvdb *db = malloc(sizeof(*db));
233 if (!tlvdb_parse_one(db, NULL, &tmp, &left)) {
234 free(db);
235 goto err;
238 tlvdb_add(&root->db, db);
241 return &root->db;
243 err:
244 tlvdb_free(&root->db);
246 return NULL;
249 struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value) {
250 struct tlvdb_root *root = malloc(sizeof(*root) + len);
252 root->len = len;
253 memcpy(root->buf, value, len);
255 root->db.parent = root->db.next = root->db.children = NULL;
256 root->db.tag.tag = tag;
257 root->db.tag.len = len;
258 root->db.tag.value = root->buf;
260 return &root->db;
263 struct tlvdb *tlvdb_external(tlv_tag_t tag, size_t len, const unsigned char *value) {
264 struct tlvdb_root *root = malloc(sizeof(*root));
266 root->len = 0;
268 root->db.parent = root->db.next = root->db.children = NULL;
269 root->db.tag.tag = tag;
270 root->db.tag.len = len;
271 root->db.tag.value = value;
273 return &root->db;
276 void tlvdb_free(struct tlvdb *tlvdb) {
277 struct tlvdb *next = NULL;
279 if (!tlvdb)
280 return;
282 for (; tlvdb; tlvdb = next) {
283 next = tlvdb->next;
284 tlvdb_free(tlvdb->children);
285 free(tlvdb);
289 struct tlvdb *tlvdb_find_next(struct tlvdb *tlvdb, tlv_tag_t tag) {
290 if (!tlvdb)
291 return NULL;
293 return tlvdb_find(tlvdb->next, tag);
296 struct tlvdb *tlvdb_find(struct tlvdb *tlvdb, tlv_tag_t tag) {
297 if (!tlvdb)
298 return NULL;
300 for (; tlvdb; tlvdb = tlvdb->next) {
301 if (tlvdb->tag.tag == tag)
302 return tlvdb;
305 return NULL;
308 struct tlvdb *tlvdb_find_full(struct tlvdb *tlvdb, tlv_tag_t tag) {
309 if (!tlvdb)
310 return NULL;
312 for (; tlvdb; tlvdb = tlvdb->next) {
313 if (tlvdb->tag.tag == tag)
314 return tlvdb;
316 if (tlvdb->children) {
317 struct tlvdb *ch = tlvdb_find_full(tlvdb->children, tag);
318 if (ch)
319 return ch;
323 return NULL;
326 struct tlvdb *tlvdb_find_path(struct tlvdb *tlvdb, tlv_tag_t tag[]) {
327 int i = 0;
328 struct tlvdb *tnext = tlvdb;
330 while (tnext && tag[i]) {
331 tnext = tlvdb_find(tnext, tag[i]);
332 i++;
333 if (tag[i] && tnext) {
334 tnext = tnext->children;
338 return tnext;
341 void tlvdb_add(struct tlvdb *tlvdb, struct tlvdb *other) {
342 if (tlvdb == other)
343 return;
345 while (tlvdb->next) {
346 if (tlvdb->next == other)
347 return;
349 tlvdb = tlvdb->next;
352 tlvdb->next = other;
355 void tlvdb_change_or_add_node_ex(struct tlvdb *tlvdb, tlv_tag_t tag, size_t len, const unsigned char *value, struct tlvdb **tlvdb_elm) {
356 struct tlvdb *telm = tlvdb_find_full(tlvdb, tag);
357 if (telm == NULL) {
358 // new tlv element
359 struct tlvdb *elm = tlvdb_fixed(tag, len, value);
360 tlvdb_add(tlvdb, elm);
361 if (tlvdb_elm)
362 *tlvdb_elm = elm;
363 } else {
364 // the same tlv structure
365 if (telm->tag.tag == tag && telm->tag.len == len && !memcmp(telm->tag.value, value, len))
366 return;
368 // replace tlv element
369 struct tlvdb *tnewelm = tlvdb_fixed(tag, len, value);
370 bool tnewelm_linked = false;
371 tnewelm->next = telm->next;
372 tnewelm->parent = telm->parent;
374 // if telm stayed first in children chain
375 if (telm->parent && telm->parent->children == telm) {
376 telm->parent->children = tnewelm;
377 tnewelm_linked = true;
380 // if telm have previous element
381 if (telm != tlvdb) {
382 // elm in root
383 struct tlvdb *celm = tlvdb;
384 // elm in child list of node
385 if (telm->parent && telm->parent->children)
386 celm = telm->parent->children;
388 // find previous element
389 for (; celm; celm = celm->next) {
390 if (celm->next == telm) {
391 celm->next = tnewelm;
392 tnewelm_linked = true;
393 break;
398 // free old element with childrens
399 telm->next = NULL;
400 tlvdb_free(telm);
402 if (tlvdb_elm) {
403 *tlvdb_elm = tnewelm;
404 tnewelm_linked = true;
406 if (! tnewelm_linked) {
407 tlvdb_free(tnewelm);
411 return;
414 void tlvdb_change_or_add_node(struct tlvdb *tlvdb, tlv_tag_t tag, size_t len, const unsigned char *value) {
415 tlvdb_change_or_add_node_ex(tlvdb, tag, len, value, NULL);
418 void tlvdb_visit(const struct tlvdb *tlvdb, tlv_cb cb, void *data, int level) {
419 struct tlvdb *next = NULL;
421 if (!tlvdb)
422 return;
424 for (; tlvdb; tlvdb = next) {
425 next = tlvdb->next;
426 cb(data, &tlvdb->tag, level, (tlvdb->children == NULL));
427 tlvdb_visit(tlvdb->children, cb, data, level + 1);
431 static const struct tlvdb *tlvdb_next(const struct tlvdb *tlvdb) {
432 if (tlvdb->children)
433 return tlvdb->children;
435 while (tlvdb) {
436 if (tlvdb->next)
437 return tlvdb->next;
439 tlvdb = tlvdb->parent;
442 return NULL;
445 const struct tlv *tlvdb_get(const struct tlvdb *tlvdb, tlv_tag_t tag, const struct tlv *prev) {
446 if (prev) {
447 // tlvdb = tlvdb_next(container_of(prev, struct tlvdb, tag));
448 tlvdb = tlvdb_next((struct tlvdb *)prev);
452 while (tlvdb) {
453 if (tlvdb->tag.tag == tag)
454 return &tlvdb->tag;
456 tlvdb = tlvdb_next(tlvdb);
459 return NULL;
462 const struct tlv *tlvdb_get_inchild(const struct tlvdb *tlvdb, tlv_tag_t tag, const struct tlv *prev) {
463 tlvdb = tlvdb->children;
464 return tlvdb_get(tlvdb, tag, prev);
467 const struct tlv *tlvdb_get_tlv(const struct tlvdb *tlvdb) {
468 if (tlvdb)
469 return &tlvdb->tag;
470 else
471 return NULL;
474 unsigned char *tlv_encode(const struct tlv *tlv, size_t *len) {
475 size_t size = tlv->len;
476 unsigned char *data;
477 size_t pos;
479 if (tlv->tag > 0x100)
480 size += 2;
481 else
482 size += 1;
484 if (tlv->len > 0x7f)
485 size += 2;
486 else
487 size += 1;
489 data = malloc(size);
490 if (!data) {
491 *len = 0;
492 return NULL;
495 pos = 0;
497 if (tlv->tag > 0x100) {
498 data[pos++] = tlv->tag >> 8;
499 data[pos++] = tlv->tag & 0xff;
500 } else
501 data[pos++] = tlv->tag;
503 if (tlv->len > 0x7f) {
504 data[pos++] = 0x81;
505 data[pos++] = tlv->len;
506 } else
507 data[pos++] = tlv->len;
509 memcpy(data + pos, tlv->value, tlv->len);
510 pos += tlv->len;
512 *len = pos;
513 return data;
516 bool tlv_is_constructed(const struct tlv *tlv) {
517 return (((tlv->tag < 0x100 ? tlv->tag : tlv->tag >> 8) & TLV_TAG_COMPLEX) == TLV_TAG_COMPLEX);
520 bool tlv_equal(const struct tlv *a, const struct tlv *b) {
521 if (!a && !b)
522 return true;
524 if (!a || !b)
525 return false;
527 return a->tag == b->tag && a->len == b->len && !memcmp(a->value, b->value, a->len);
530 struct tlvdb *tlvdb_elm_get_next(struct tlvdb *tlvdb) {
531 return tlvdb->next;
534 struct tlvdb *tlvdb_elm_get_children(struct tlvdb *tlvdb) {
535 return tlvdb->children;
538 struct tlvdb *tlvdb_elm_get_parent(struct tlvdb *tlvdb) {
539 return tlvdb->parent;
542 bool tlvdb_get_uint8(struct tlvdb *tlvRoot, tlv_tag_t tag, uint8_t *value) {
543 const struct tlv *tlvelm = tlvdb_get(tlvRoot, tag, NULL);
544 return tlv_get_uint8(tlvelm, value);
547 bool tlv_get_uint8(const struct tlv *etlv, uint8_t *value) {
548 *value = 0;
549 if (etlv) {
550 if (etlv->len == 0)
551 return true;
553 if (etlv->len == 1) {
554 *value = etlv->value[0];
555 return true;
558 return false;
561 bool tlv_get_int(const struct tlv *etlv, int *value) {
562 *value = 0;
563 if (etlv) {
564 if (etlv->len == 0)
565 return true;
567 if (etlv->len <= 4) {
568 for (int i = 0; i < etlv->len; i++) {
569 *value += etlv->value[i] * (1 << (i * 8));
571 return true;
574 return false;