Coder Social home page Coder Social logo

ber-tlv's Introduction

maven Build Status CircleCI

BER-TLV parser and builder

BerTlv is a java library for parsing and building BER TLV encoded data.

Features

  • supported types: amount, date, time, text, BCD, bytes
  • thread safe (provides immutable container BerTlv)
  • production ready (uses in several projects)
  • lightweight (no external dependencies)

Setup with dependency managers

Maven

<dependency>
  <groupId>com.payneteasy</groupId>
  <artifactId>ber-tlv</artifactId>
  <version>1.0-11</version>
</dependency>

Gradle

implementation 'com.payneteasy:ber-tlv:1.0-11'

How to parse

byte[] bytes = HexUtil.parseHex("50045649534157131000023100000033D44122011003400000481F");

BerTlvParser parser = new BerTlvParser(LOG);
BerTlvs tlvs = parser.parse(bytes, 0, bytes.length);
  
BerTlvLogger.log("    ", tlvs, LOG);

How to build

byte[] bytes =  new BerTlvBuilder()
                .addHex(new BerTag(0x50), "56495341")
                .addHex(new BerTag(0x57), "1000023100000033D44122011003400000481F")
                .buildArray();

License

The BerTlv framework is licensed under the Apache License 2.0

ber-tlv's People

Contributors

evsinev avatar kullanici0606 avatar ligi avatar patrickfav avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ber-tlv's Issues

Semantic versioning

I suggest to use semantic versioning MAJOR.MINOR.PATCH for package version
Currently Gradle 5.6.4 in Android studio processes dependency version wrong:
'com.payneteasy:ber-tlv:1.0-9' is newer than 'com.payneteasy:ber-tlv:1.0-10'

Add module definition for Java 9+

To get rid of:

[WARNING] ******************************************************************************************************************************************
[WARNING] * Required filename-based automodules detected: [ber-tlv-1.0-11.jar]. Please don't publish this project to a public artifact repository! *
[WARNING] ******************************************************************************************************************************************

Custom buffer size ignored when adding BerTlv to BerTlvBuilder

The following code will lead to an error, if myBerTlv has a value that has over 5120 bytes:

BerTlvBuilder berTlvBuilder = new BerTlvBuilder(myBerTlv.getTag(),
                                                        new byte[1000000],
                                                        0,
                                                        1000000);
byte[] bytes = berTlvBuilder.addBerTlv(myBerTlv);

The exception I got was:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at com.payneteasy.tlv.BerTlvBuilder.addBytes(BerTlvBuilder.java:178)
	at com.payneteasy.tlv.BerTlvBuilder.addBytes(BerTlvBuilder.java:162)
	at com.payneteasy.tlv.BerTlvBuilder.addBerTlv(BerTlvBuilder.java:196)
	at com.payneteasy.tlv.BerTlvBuilder.from(BerTlvBuilder.java:45)
	at com.payneteasy.tlv.BerTlvBuilder.addBerTlv(BerTlvBuilder.java:194)
	at com.payneteasy.tlv.BerTlvBuilder.from(BerTlvBuilder.java:45)
	at com.payneteasy.tlv.BerTlvBuilder.addBerTlv(BerTlvBuilder.java:194)
	at com.payneteasy.tlv.BerTlvBuilder.from(BerTlvBuilder.java:45)
	at com.payneteasy.tlv.BerTlvBuilder.addBerTlv(BerTlvBuilder.java:194)
	at com.payneteasy.tlv.BerTlvBuilder.from(BerTlvBuilder.java:45)
	at com.payneteasy.tlv.BerTlvBuilder.addBerTlv(BerTlvBuilder.java:194)

When using the addBerTlv method, the code eventually calls public BerTlvBuilder(BerTag aTemplate), a method that overwrites the custom buffer with the default settings.

unable to parse encrypted data using parser

We are building like below:

byte[] bytes = new BerTlvBuilder()
.addHex(new BerTag(0x50), "CX89D")
.addHex(new BerTag(0x57), "This is content to generate QR code")
.buildArray();

But unable to parse as mentioned in the document: like below

byte[] bytes = HexUtil.parseHex("");
BerTlvParser parser = new BerTlvParser(LOG);
BerTlvs tlvs = parser.parse(bytes, 0, bytes.length);

Can you please guide me how to parse ? my encrypted string, in above builder I ll get bytes array not encrypted string, please guide me how to use it.

Regards
Vinod

why parse function only parsed 100 tlv?

for(int i=0; i<100; i++) {
ParseResult result = parseWithResult(0, aBuf, offset, aLen-offset);
tlvs.add(result.tlv);

if(result.offset>=aOffset+aLen) {
    break;
}

offset = result.offset;

}

i think that code is better

int curOffset;
do {
ParseResult result = parseWithResult(0, aBuf, offset, aLen-offset);
tlvs.add(result.tlv);

  if(result.offset>=aOffset+aLen) {
      break;
  }
  curOffset = result.offset;

  offset = result.offset;

} while (curOffset<aOffset+aLen);

BerTlvBuilder - addHex looses data if size not even

BerTlvBuilder.addHex - when adding Stiring of odd length, will just process the even number of characters.
In my view either it should throw an exception/runtime exception or process all characters correctly.

The data is then skipped in HexUtil.parseHex where the size of destination buffer is integer division that doesn't work for numbers that are not power of 2.

Limite depth on BerTlv find

Current BerTlv find is searching for the TAG recursively and sometimes ends up returns incorrect values.

Example:

If I have the following data and want to find the BerTLV 0x80 under 0x62, it will return the 0x80 inside 0xA5.

62 19
82 02 4121
83 02 2F05
A5 03 800161
8A 01 05
8B 03 2F0605
80 02 000A

One option would be to create a findOnChildBerTlv, to limit the search to a 1 level depth.
Adding a new parameter maxDepth would be another solution.

FF tag

Dear Sirs,

I just trying to parse Visa CDET cards from https://play.google.com/store/apps/details?id=com.visa.app.cdet app. App's card 09 is marked as Card with '00' and 'FF' Padding.
When READ RECORD command performed:
00b2011400
Response from the card is:
700C5F340101FFFFFF9F570208409000
ber-tlv parser parses that string in the following manner:
BerTlvs{tlvs=[BerTlv{theTag=+ 70, theValue=null, theList=[BerTlv{theTag=- 5F34, theValue=[1], theList=null}, BerTlv{theTag=+ FFFFFF9F57, theValue=null, theList=[]}]}]}
But according Annex B of EMV 4.3 Book 3 value where bytes from 5 to 1 all = 1 mean "see subsequent bytes" ie all FF's should be ignored.
Could you fix this please?
Thanks in advance,
Igor

Last tag from a hex string is sometimes missing after parsing

Hi

While I was using this librairy, i've encountered a bug that prevented the parsing of the following Hex String:

BF01820114DF0105A0000000049F2201EFDF020101DF030101DF0481F8A191CB87473F29349B5D60A88B3EAEE0973AA6F1A082F358D849FDDFF9C091F899EDA9792CAF09EF28F5D22404B88A2293EEBBC1949C43BEA4D60CFD879A1539544E09E0F09F60F065B2BF2A13ECC705F3D468B9D33AE77AD9D3F19CA40F23DCF5EB7C04DC8F69EBA565B1EBCB4686CD274785530FF6F6E9EE43AA43FDB02CE00DAEC15C7B8FD6A9B394BABA419D3F6DC85E16569BE8E76989688EFEA2DF22FF7D35C043338DEAA982A02B866DE5328519EBBCD6F03CDD686673847F84DB651AB86C28CF1462562C577B853564A290C8556D818531268D25CC98A4CC6A0BDFFFDA2DCCA3A94C998559E307FDDF915006D9A987B07DDAEB3BDF050103

The node "DF05" with the length of 1 and value of "03" was not found by the API (It's at the end of the String). After some debugging I've come up with a fix that I zipped and attached to this bug entry. Not being familiar with Git Hub, i've not made a push request... sorry

Have a good day!
BerTlvParser.zip

In BerTlvBuilder "aLength" is never used

is it intentional?

    public BerTlvBuilder(BerTag aTemplate, byte[] aBuffer, int aOffset, int aLength) {
        theTemplate  = aTemplate;
        theBuffer = aBuffer;
        thePos = aOffset;
        theBufferOffset = aOffset;
    }

For 61 Tag

Sir, it's works fine. But I want parse 61 tag what should i do in this case? When i pass 61 tag it will through error.

Name the "magic numbers"

BerTlvBuilder contains a lot of "magic numbers" (0x80, 0x81, 0x100 and so on). What are they? Could you make them constant?

    private void fillLength(byte[] aBuffer, int aOffset, int aLength) {

        if(aLength < 0x80) {
            aBuffer[aOffset] = (byte) aLength;

        } else if (aLength <0x100) {
            aBuffer[aOffset] = (byte) 0x81;
            aBuffer[aOffset+1] = (byte) aLength;

        } else if( aLength < 0x10000) {

            aBuffer[aOffset]   = (byte) 0x82;
            aBuffer[aOffset+1] = (byte) (aLength / 0x100);
            aBuffer[aOffset+2] = (byte) (aLength % 0x100);

        } else if( aLength < 0x1000000 ) {
            aBuffer[aOffset]   = (byte) 0x83;
            aBuffer[aOffset+1] = (byte) (aLength / 0x10000);
            aBuffer[aOffset+2] = (byte) (aLength / 0x100);
            aBuffer[aOffset+3] = (byte) (aLength % 0x100);
        } else {
            throw new IllegalStateException("length ["+aLength+"] out of range (0x1000000)");
        }
    }

Time for a new release?

One year has passed from -8. Version that would not bring in junit dependency (b3eacba) for non-test requirements and other minor improvements would be cool!

add `try{} catch(){}` in BerTlvParser class

Some tags on the card are not in standard format or have some problem, but the rest is fine. If you do not catch this exception, all information is lost when the parcel is made.

add try{} catch(){} in BerTlvParser class at line 84

int lengthBytesCount;
int valueLength;
try {
	lengthBytesCount = getLengthBytesCount(aBuf, aOffset + tagBytesCount);
	valueLength = getDataLength(aBuf, aOffset + tagBytesCount);
}catch (Exception e){
	lengthBytesCount = 0;
	valueLength = 0;
}

0 length Ber TLV tag got drop when using BerTlvBuilder

Hi, thanks for your great library. I've some issue with building nested BerTLV tag. The following is working as expected :

BerTlv tlv = new BerTlv(new BerTag(0x01),new byte[]{0x06});
BerTlv some = new BerTlvBuilder()
       .addBytes(new BerTag(0xE3), new BerTlvBuilder().addBerTlv(tlv).buildArray())
       .buildTlv();

which gives :

010106
E303010106

But when using 0 length BerTLV tag, the following is not working

BerTlv tlv = new BerTlv(new BerTag(0x01),new byte[]{});
BerTlv some = new BerTlvBuilder()
       .addBytes(new BerTag(0xE3), new BerTlvBuilder().addBerTlv(tlv).buildArray())
       .buildTlv();

I get :

0100
E300

But I'm expecting :

0100
E3020100

Thanks for your help

Tag FFEX

HI,
I'm trying to generate tag FF Ex ( X=1, 2,3.... like -FFE1 /FFE2/FFE3),
when I sent to the builder this tag, the Ex is recognized as constructed tag. and builds it in the wrong way.
Ex ( X=1, 2,3....) is known as a constructed tag? why does the builder recognize it as one?

p.s
the tag FF 12 does not generate this problem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.