From 1b24e351f45455a5838b33b4d6d5900ee4709ea8 Mon Sep 17 00:00:00 2001 From: "yakup.turgut" Date: Fri, 4 Oct 2019 17:41:29 +0300 Subject: [PATCH] Add ber tag factory to parser to allow custom tag creation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This may help to cope with data that was designed errornously such as GlobalPlatform public key data which has 'B0' and 'F0' tags which are not valid. Explanation from document [1] - Table 3-8: GlobalPlatform acknowledges as an error that values 'B0' and 'F0' in the table above are not valid ASN.1 tag values. This mistake was introduced in previous versions of this specification. Although this may be a problem for standard tools used to edit or display certificates and ASN.1 structures in general, it’s been decided, for the moment at least, to keep such values to preserve backward compatibility with existing implementations already coping with such errors. [1] https://globalplatform.org/wp-content/uploads/2019/07/GPC_2.3_A_ConfidentialCardContentMgmt_v1.2_PublicRelease-replacement.pdf --- .../com/payneteasy/tlv/BerTagFactory.java | 5 +++++ .../java/com/payneteasy/tlv/BerTlvParser.java | 20 +++++++++++++++---- .../payneteasy/tlv/DefaultBerTagFactory.java | 10 ++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/payneteasy/tlv/BerTagFactory.java create mode 100644 src/main/java/com/payneteasy/tlv/DefaultBerTagFactory.java diff --git a/src/main/java/com/payneteasy/tlv/BerTagFactory.java b/src/main/java/com/payneteasy/tlv/BerTagFactory.java new file mode 100644 index 0000000..41abbb1 --- /dev/null +++ b/src/main/java/com/payneteasy/tlv/BerTagFactory.java @@ -0,0 +1,5 @@ +package com.payneteasy.tlv; + +public interface BerTagFactory { + BerTag createTag(byte[] aBuf, int aOffset, int aLength); +} diff --git a/src/main/java/com/payneteasy/tlv/BerTlvParser.java b/src/main/java/com/payneteasy/tlv/BerTlvParser.java index 40b5227..57c614b 100644 --- a/src/main/java/com/payneteasy/tlv/BerTlvParser.java +++ b/src/main/java/com/payneteasy/tlv/BerTlvParser.java @@ -9,16 +9,28 @@ */ public class BerTlvParser { + private static final BerTagFactory DEFAULT_TAG_FACTORY = new DefaultBerTagFactory(); + + private final BerTagFactory tagFactory; private final IBerTlvLogger log; public BerTlvParser() { - this(EMPTY_LOGGER); + this(DEFAULT_TAG_FACTORY, EMPTY_LOGGER); } public BerTlvParser(IBerTlvLogger aLogger) { - log = aLogger; + this(DEFAULT_TAG_FACTORY, aLogger); } - + + public BerTlvParser(BerTagFactory aTagFactory) { + this(aTagFactory, EMPTY_LOGGER); + } + + public BerTlvParser(BerTagFactory aTagFactory, IBerTlvLogger aLogger) { + tagFactory = aTagFactory; + log = aLogger; + } + public BerTlv parseConstructed(byte[] aBuf) { return parseConstructed(aBuf, 0, aBuf.length); } @@ -164,7 +176,7 @@ private BerTag createTag(String aLevelPadding, byte[] aBuf, int aOffset, int aLe if(log.isDebugEnabled()) { log.debug("{}Creating tag {}...", aLevelPadding, HexUtil.toFormattedHexString(aBuf, aOffset, aLength)); } - return new BerTag(aBuf, aOffset, aLength); + return tagFactory.createTag(aBuf, aOffset, aLength); } private int getTagBytesCount(byte[] aBuf, int aOffset) { diff --git a/src/main/java/com/payneteasy/tlv/DefaultBerTagFactory.java b/src/main/java/com/payneteasy/tlv/DefaultBerTagFactory.java new file mode 100644 index 0000000..929a413 --- /dev/null +++ b/src/main/java/com/payneteasy/tlv/DefaultBerTagFactory.java @@ -0,0 +1,10 @@ +package com.payneteasy.tlv; + +public class DefaultBerTagFactory implements BerTagFactory { + + @Override + public BerTag createTag(byte[] aBuf, int aOffset, int aLength) { + return new BerTag(aBuf, aOffset, aLength); + } + +}