]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/emv/tlv.c
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
29 #define TLV_TAG_CLASS_MASK 0xc0
30 #define TLV_TAG_COMPLEX 0x20
31 #define TLV_TAG_VALUE_MASK 0x1f
32 #define TLV_TAG_VALUE_CONT 0x1f
33 #define TLV_TAG_INVALID 0
35 #define TLV_LEN_LONG 0x80
36 #define TLV_LEN_MASK 0x7f
37 #define TLV_LEN_INVALID (~0)
39 // http://radek.io/2012/11/10/magical-container_of-macro/
40 //#define container_of(ptr, type, member) ({
41 // const typeof( ((type *)0)->member ) *__mptr = (ptr);
42 // (type *)( (char *)__mptr - offsetof(type,member) );})
48 struct tlvdb
*children
;
57 static tlv_tag_t
tlv_parse_tag(const unsigned char **buf
, size_t *len
)
62 return TLV_TAG_INVALID
;
66 if ((tag
& TLV_TAG_VALUE_MASK
) != TLV_TAG_VALUE_CONT
)
70 return TLV_TAG_INVALID
;
80 static size_t tlv_parse_len(const unsigned char **buf
, size_t *len
)
85 return TLV_LEN_INVALID
;
91 if (!(l
& TLV_LEN_LONG
))
94 size_t ll
= l
&~ TLV_LEN_LONG
;
96 return TLV_LEN_INVALID
;
100 return TLV_LEN_INVALID
;
109 bool tlv_parse_tl(const unsigned char **buf
, size_t *len
, struct tlv
*tlv
)
113 tlv
->tag
= tlv_parse_tag(buf
, len
);
114 if (tlv
->tag
== TLV_TAG_INVALID
)
117 tlv
->len
= tlv_parse_len(buf
, len
);
118 if (tlv
->len
== TLV_LEN_INVALID
)
124 static struct tlvdb
*tlvdb_parse_children(struct tlvdb
*parent
);
126 static bool tlvdb_parse_one(struct tlvdb
*tlvdb
,
127 struct tlvdb
*parent
,
128 const unsigned char **tmp
,
131 tlvdb
->next
= tlvdb
->children
= NULL
;
132 tlvdb
->parent
= parent
;
134 tlvdb
->tag
.tag
= tlv_parse_tag(tmp
, left
);
135 if (tlvdb
->tag
.tag
== TLV_TAG_INVALID
)
138 tlvdb
->tag
.len
= tlv_parse_len(tmp
, left
);
139 if (tlvdb
->tag
.len
== TLV_LEN_INVALID
)
142 if (tlvdb
->tag
.len
> *left
)
145 tlvdb
->tag
.value
= *tmp
;
147 *tmp
+= tlvdb
->tag
.len
;
148 *left
-= tlvdb
->tag
.len
;
150 if (tlv_is_constructed(&tlvdb
->tag
) && (tlvdb
->tag
.len
!= 0)) {
151 tlvdb
->children
= tlvdb_parse_children(tlvdb
);
152 if (!tlvdb
->children
)
155 tlvdb
->children
= NULL
;
164 static struct tlvdb
*tlvdb_parse_children(struct tlvdb
*parent
)
166 const unsigned char *tmp
= parent
->tag
.value
;
167 size_t left
= parent
->tag
.len
;
168 struct tlvdb
*tlvdb
, *first
= NULL
, *prev
= NULL
;
171 tlvdb
= malloc(sizeof(*tlvdb
));
178 if (!tlvdb_parse_one(tlvdb
, parent
, &tmp
, &left
))
181 tlvdb
->parent
= parent
;
192 struct tlvdb
*tlvdb_parse(const unsigned char *buf
, size_t len
)
194 struct tlvdb_root
*root
;
195 const unsigned char *tmp
;
201 root
= malloc(sizeof(*root
) + len
);
203 memcpy(root
->buf
, buf
, len
);
208 if (!tlvdb_parse_one(&root
->db
, NULL
, &tmp
, &left
))
217 tlvdb_free(&root
->db
);
222 struct tlvdb
*tlvdb_parse_multi(const unsigned char *buf
, size_t len
)
224 struct tlvdb_root
*root
;
225 const unsigned char *tmp
;
231 root
= malloc(sizeof(*root
) + len
);
233 memcpy(root
->buf
, buf
, len
);
238 if (!tlvdb_parse_one(&root
->db
, NULL
, &tmp
, &left
))
242 struct tlvdb
*db
= malloc(sizeof(*db
));
243 if (!tlvdb_parse_one(db
, NULL
, &tmp
, &left
)) {
248 tlvdb_add(&root
->db
, db
);
254 tlvdb_free(&root
->db
);
259 struct tlvdb
*tlvdb_fixed(tlv_tag_t tag
, size_t len
, const unsigned char *value
)
261 struct tlvdb_root
*root
= malloc(sizeof(*root
) + len
);
264 memcpy(root
->buf
, value
, len
);
266 root
->db
.parent
= root
->db
.next
= root
->db
.children
= NULL
;
267 root
->db
.tag
.tag
= tag
;
268 root
->db
.tag
.len
= len
;
269 root
->db
.tag
.value
= root
->buf
;
274 struct tlvdb
*tlvdb_external(tlv_tag_t tag
, size_t len
, const unsigned char *value
)
276 struct tlvdb_root
*root
= malloc(sizeof(*root
));
280 root
->db
.parent
= root
->db
.next
= root
->db
.children
= NULL
;
281 root
->db
.tag
.tag
= tag
;
282 root
->db
.tag
.len
= len
;
283 root
->db
.tag
.value
= value
;
288 void tlvdb_free(struct tlvdb
*tlvdb
)
290 struct tlvdb
*next
= NULL
;
295 for (; tlvdb
; tlvdb
= next
) {
297 tlvdb_free(tlvdb
->children
);
302 struct tlvdb
*tlvdb_find_next(struct tlvdb
*tlvdb
, tlv_tag_t tag
) {
306 return tlvdb_find(tlvdb
->next
, tag
);
309 struct tlvdb
*tlvdb_find(struct tlvdb
*tlvdb
, tlv_tag_t tag
) {
313 for (; tlvdb
; tlvdb
= tlvdb
->next
) {
314 if (tlvdb
->tag
.tag
== tag
)
321 struct tlvdb
*tlvdb_find_full(struct tlvdb
*tlvdb
, tlv_tag_t tag
) {
325 for (; tlvdb
; tlvdb
= tlvdb
->next
) {
326 if (tlvdb
->tag
.tag
== tag
)
329 if (tlvdb
->children
) {
330 struct tlvdb
* ch
= tlvdb_find_full(tlvdb
->children
, tag
);
339 struct tlvdb
*tlvdb_find_path(struct tlvdb
*tlvdb
, tlv_tag_t tag
[]) {
341 struct tlvdb
*tnext
= tlvdb
;
343 while (tnext
&& tag
[i
]) {
344 tnext
= tlvdb_find(tnext
, tag
[i
]);
346 if (tag
[i
] && tnext
) {
347 tnext
= tnext
->children
;
354 void tlvdb_add(struct tlvdb
*tlvdb
, struct tlvdb
*other
)
356 while (tlvdb
->next
) {
363 void tlvdb_change_or_add_node(struct tlvdb
*tlvdb
, tlv_tag_t tag
, size_t len
, const unsigned char *value
)
365 struct tlvdb
*telm
= tlvdb_find_full(tlvdb
, tag
);
368 tlvdb_add(tlvdb
, tlvdb_fixed(tag
, len
, value
));
370 // the same tlv structure
371 if (telm
->tag
.tag
== tag
&& telm
->tag
.len
== len
&& !memcmp(telm
->tag
.value
, value
, len
))
374 // replace tlv element
375 struct tlvdb
*tnewelm
= tlvdb_fixed(tag
, len
, value
);
376 tnewelm
->next
= telm
->next
;
377 tnewelm
->parent
= telm
->parent
;
379 // if telm stayed first in children chain
380 if (telm
->parent
&& telm
->parent
->children
== telm
) {
381 telm
->parent
->children
= tnewelm
;
384 // if telm have previous element
387 struct tlvdb
*celm
= tlvdb
;
388 // elm in child list of node
389 if (telm
->parent
&& telm
->parent
->children
)
390 celm
= telm
->parent
->children
;
392 // find previous element
393 for (; celm
; celm
= celm
->next
) {
394 if (celm
->next
== telm
) {
395 celm
->next
= tnewelm
;
401 // free old element with childrens
409 void tlvdb_visit(const struct tlvdb
*tlvdb
, tlv_cb cb
, void *data
, int level
)
411 struct tlvdb
*next
= NULL
;
416 for (; tlvdb
; tlvdb
= next
) {
418 cb(data
, &tlvdb
->tag
, level
, (tlvdb
->children
== NULL
));
419 tlvdb_visit(tlvdb
->children
, cb
, data
, level
+ 1);
423 static const struct tlvdb
*tlvdb_next(const struct tlvdb
*tlvdb
)
426 return tlvdb
->children
;
432 tlvdb
= tlvdb
->parent
;
438 const struct tlv
*tlvdb_get(const struct tlvdb
*tlvdb
, tlv_tag_t tag
, const struct tlv
*prev
)
441 // tlvdb = tlvdb_next(container_of(prev, struct tlvdb, tag));
442 tlvdb
= tlvdb_next((struct tlvdb
*)prev
);
447 if (tlvdb
->tag
.tag
== tag
)
450 tlvdb
= tlvdb_next(tlvdb
);
456 const struct tlv
*tlvdb_get_inchild(const struct tlvdb
*tlvdb
, tlv_tag_t tag
, const struct tlv
*prev
) {
457 tlvdb
= tlvdb
->children
;
458 return tlvdb_get(tlvdb
, tag
, prev
);
461 unsigned char *tlv_encode(const struct tlv
*tlv
, size_t *len
)
463 size_t size
= tlv
->len
;
467 if (tlv
->tag
> 0x100)
485 if (tlv
->tag
> 0x100) {
486 data
[pos
++] = tlv
->tag
>> 8;
487 data
[pos
++] = tlv
->tag
& 0xff;
489 data
[pos
++] = tlv
->tag
;
491 if (tlv
->len
> 0x7f) {
493 data
[pos
++] = tlv
->len
;
495 data
[pos
++] = tlv
->len
;
497 memcpy(data
+ pos
, tlv
->value
, tlv
->len
);
504 bool tlv_is_constructed(const struct tlv
*tlv
)
506 return (tlv
->tag
< 0x100 ? tlv
->tag
: tlv
->tag
>> 8) & TLV_TAG_COMPLEX
;
509 bool tlv_equal(const struct tlv
*a
, const struct tlv
*b
)
517 return a
->tag
== b
->tag
&& a
->len
== b
->len
&& !memcmp(a
->value
, b
->value
, a
->len
);