6

I intend to parse BER-TLV format from smart card response to interpret the data.

it similar like JACCAL, but in Objective-C or C++

Can anyone give reference any open source project or any reference to do this?

HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • I did not see any BER parsing/encoding in JACCAL you pointed to. ISO 7816-4 requires an extremely limited subset of BER parsing/encoding (and, funny enough, no ASN.1 definitions) so you might want to create a few helper classes and methods instead. – Maarten Bodewes Nov 21 '12 at 21:07
  • Botan seems to have a small BER parsing facility. – Maarten Bodewes Nov 21 '12 at 21:15
  • 1
    I recently parsed BER TLV responses from smart card by referring to the tokend project. I ended up writing my own functions but relied heavily on tokend project for the length parsing. A thing to note is that length can occupy 1,2,3 or 4 bytes. I don't have access to my code right now. I will try posting it later tonight, – Himanshu Page Nov 28 '12 at 16:37
  • thank you! I could create my own function if i can get reference on how to do. please do post. – HelmiB Nov 29 '12 at 02:21
  • Hi, i want to use the first library https://github.com/chrisridd/asn1-dump/ to do BER-TLV decoding but i cannot find an example how to use it. I would appreciate any help, thank you – Sarah Feb 13 '14 at 15:41

2 Answers2

4

Here is a project decode the ASN.1 BER format. https://github.com/chrisridd/asn1-dump/

main logic located in this file: https://github.com/chrisridd/asn1-dump/blob/master/berd.m

And if have enough time, it's not hard to write you own decoder after you read the standard: http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf http://luca.ntop.org/Teaching/Appunti/asn1.html

decode flow is like this: read in Tag, Length, Value sequence.

  • From Tag you will get

    • Data Class, usually be Universal(predefined type in standard like "Boolean","Sequence"...) and context-specific (to distinguish different field with same type).
    • Primitive (like boolean and integer) or Constructed (usually is sequence). because constructed type can contains Primitive or Constructed type. maybe need recursive decoding.
    • Tag Number, determine data types (boolean? integer? bitstring?)
  • Length:

    • determine length of the contents to decode into (maybe need recursively decoding).
    • length has two form (short and long). you'd better support both.
  • Value: the real value to read in current TLV level. if this is a constructed data(like sequence), the Value will contains internal level of TLV.

In the end of standard (http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf) there is an image shows multiple level TLV, maybe can better help you to understand BER.

After you read the standards, the best way is: 1) find some GUI viewer to look some BER Certificate file, to get familar with it. google "ASN.1 viewer" to find. 2) start to look the code https://github.com/chrisridd/asn1-dump/blob/master/berd.m

whunmr
  • 2,435
  • 2
  • 22
  • 35
  • Hi, i want to use the first library https://github.com/chrisridd/asn1-dump/ to do BER-TLV decoding but i cannot find an example how to use it. I would appreciate any help, thank you – Sarah Feb 13 '14 at 15:40
  • Hi @Sarah, download the code in the github, and open in xcode, and build it, and run. you will got an application "ASN.1 dump" running. Using the File->open to open the *.cer file in http://www.swview.org/sites/swview.org/files/x.509-sample-keys-and-certificates.zip and see the showed winodw for expanded asn.1 structures. Because *.cer files is in BER format. – whunmr Feb 14 '14 at 12:58
  • @Sarah, contact me if you need more help on skype (id: whunmr) – whunmr Feb 14 '14 at 13:04
1

How about tlve? http://tlve.sourceforge.net/

Apple's Tokend seems useful too: http://www.opensource.apple.com/source/Tokend/Tokend-36720/PIV/TLV.cpp

jayacard on sourceforge is another project that dealt with this, now seems abandoned, but source is here: http://www.codeforge.com/read/7149/tlv.c__html

Alex I
  • 19,689
  • 9
  • 86
  • 158