From 7410ac59ba8e1994254a872104ea660b992cba9a Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 28 Jan 2022 17:06:47 +0100 Subject: new dynamic header version --- .../dynamicFrame/api/DynamicFrameCoder.java | 107 ++++++++++++ .../barcode/dynamicFrame/api/IDynamicFrame.java | 75 +++++---- .../uic/barcode/dynamicFrame/api/ILevel1Data.java | 60 +++++-- .../dynamicFrame/api/SimpleDynamicFrame.java | 183 ++++++--------------- .../barcode/dynamicFrame/api/SimpleLevel1Data.java | 11 ++ .../barcode/dynamicFrame/api/SimpleLevel2Data.java | 19 --- 6 files changed, 252 insertions(+), 203 deletions(-) create mode 100644 src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java (limited to 'src/main/java/org/uic/barcode/dynamicFrame/api') diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java b/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java new file mode 100644 index 0000000..53efb3e --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java @@ -0,0 +1,107 @@ +package org.uic.barcode.dynamicFrame.api; + +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.dynamicFrame.v1.DynamicFrameCoderV1; +import org.uic.barcode.dynamicFrame.v2.DynamicFrameCoderV2; +import org.uic.barcode.ticket.EncodingFormatException; + +public class DynamicFrameCoder { + + /** + * Encode. + * + * Encode the header as ASN.1 PER UNALIGNED byte array + * + * @return the byte[] + * @throws EncodingFormatException + */ + public static byte[] encode(IDynamicFrame frame) throws EncodingFormatException { + + if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(frame.getFormat())) { + + return DynamicFrameCoderV1.encode(frame); + + } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(frame.getFormat())) { + + return DynamicFrameCoderV2.encode(frame); + + } + + throw new EncodingFormatException("Frame version not supported for encoding"); + } + + + /** + * Decode. + * + * Decode the header from an ASN.1 PER UNALIGNED encoded byte array + * + * @param bytes the bytes + * @return the dynamic header + * @throws EncodingFormatException + */ + public static IDynamicFrame decode(byte[] bytes) throws EncodingFormatException { + + IDynamicFrame frame = new SimpleDynamicFrame(); + + try { + DynamicFrameCoderV1.decode(frame,bytes); + + if (frame.getFormat() != null && frame.getFormat().equals(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1)) { + return frame; + } + } catch(Exception e1) { + frame = null; + // failed, try next + } + + frame = new SimpleDynamicFrame(); + try { + DynamicFrameCoderV2.decode(frame,bytes); + + if (frame.getFormat() != null && frame.getFormat().equals(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2)) { + return frame; + } + } catch(Exception e1) { + throw new EncodingFormatException("Dynamic Header Version not supported"); + // failed + } + + throw new EncodingFormatException("Dynamic Header Version not supported"); + + } + + + public static byte[] encodeLevel1(IDynamicFrame frame) throws EncodingFormatException { + + if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(frame.getFormat())) { + + return DynamicFrameCoderV1.encodeLevel1(frame); + + } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(frame.getFormat())) { + + return DynamicFrameCoderV2.encodeLevel1(frame); + + } + + throw new EncodingFormatException("Frame version not supported for encoding"); + + } + + + public static byte[] encodeLevel2Data(IDynamicFrame frame) throws EncodingFormatException { + + if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(frame.getFormat())) { + + return DynamicFrameCoderV1.encodeLevel2Data(frame.getLevel2Data()); + + } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(frame.getFormat())) { + + return DynamicFrameCoderV2.encodeLevel2Data(frame.getLevel2Data()); + + } + + throw new EncodingFormatException("Dynamic Header Version not supported: " + frame.getFormat()); + } + +} diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java index d901a6e..c917b6a 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java @@ -8,9 +8,9 @@ import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; import org.uic.barcode.ticket.EncodingFormatException; + /** - * The DynamicHeader for bar codes - * + * The DynamicHeader for bar codes . */ public interface IDynamicFrame{ @@ -41,7 +41,7 @@ public interface IDynamicFrame{ /** * Sets the level 2 signed data. * - * @param level2SignedData the new level 2 signed data + * @param level2Data the new level 2 data */ public void setLevel2Data(ILevel2Data level2Data); @@ -61,29 +61,6 @@ public interface IDynamicFrame{ */ public void setLevel2Signature(byte[] level2Signature); - - /** - * Encode. - * - * Encode the header as ASN.1 PER UNALIGNED byte array - * - * @return the byte[] - * @throws EncodingFormatException - */ - public byte[] encode() throws EncodingFormatException; - - /** - * Decode. - * - * Decode the header from an ASN.1 PER UNALIGNED encoded byte array - * - * @param bytes the bytes - * @return the dynamic header - * @throws EncodingFormatException - */ - public void decode(byte[] bytes) throws EncodingFormatException; - - /** * Verify the level 2 signature @@ -92,7 +69,7 @@ public interface IDynamicFrame{ * * @param data the data content * @return the return error code - * @throws EncodingFormatException + * @throws EncodingFormatException the encoding format exception */ public int validateLevel2(byte[] data) throws EncodingFormatException; @@ -102,9 +79,9 @@ public interface IDynamicFrame{ * Note: an appropriate security provider (e.g. BC) must be registered before * * @param prov the registered security provider - * @param data the data content + * @param data the data content * @return the return error code - * @throws EncodingFormatException + * @throws EncodingFormatException the encoding format exception */ public int validateLevel2(Provider prov, byte[] data) throws EncodingFormatException; @@ -116,7 +93,7 @@ public interface IDynamicFrame{ * @param key the key * @param data the data content * @return the return error code - * @throws EncodingFormatException + * @throws EncodingFormatException the encoding format exception */ public int validateLevel1(PublicKey key, byte[] data) throws EncodingFormatException; @@ -126,10 +103,10 @@ public interface IDynamicFrame{ * Note: an appropriate security provider (e.g. BC) must be registered before * * @param key the key - * @param prov the registered security provider - * @param the data content + * @param prov the registered security provider + * @param data the data * @return the return error code - * @throws EncodingFormatException + * @throws EncodingFormatException the encoding format exception */ public int validateLevel1(PublicKey key, Provider prov, byte[] data) throws EncodingFormatException; @@ -183,9 +160,8 @@ public interface IDynamicFrame{ * Note: an appropriate security provider (e.g. BC) must be registered before * * @param key the key - * @return * @return the byte[] - * @throws Exception + * @throws Exception the exception */ public void signLevel1(PrivateKey key) throws Exception; @@ -195,13 +171,36 @@ public interface IDynamicFrame{ * Note: an appropriate security provider (e.g. BC) must be registered before * * @param key the key - * @param security provider - security provider that must be sued to create the signature - * @return + * @param prov the prov * @return the byte[] - * @throws Exception + * @throws Exception the exception */ public void signLevel1(PrivateKey key, Provider prov) throws Exception; + + + /** + * Gets the signature of the level 1 data. + * + * @return the level 1 signature + */ + public byte[] getLevel1Signature(); + + + /** + * Gets the level 1 data in binary as they are signed by the level 1 signature. + * + * @return the level 1 data binary + * @throws EncodingFormatException the encoding format exception + */ + public byte[] getLevel1DataBin() throws EncodingFormatException; + /** + * Gets the level 2 data in binary as they are signed by the level 1 signature. + * + * @return the level 2 data binary + * @throws EncodingFormatException the encoding format exception + */ + public byte[] getLevel2DataBin() throws EncodingFormatException; } diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java index 206d613..e23fc88 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java @@ -11,15 +11,15 @@ public interface ILevel1Data { /** - * Sets the security provider + * Sets the security provider . * - * @param securityProviderNum the new security provider + * @param securityProvider the new security provider */ public void setSecurityProvider(String securityProvider); /** - * Gets the security provider + * Gets the security provider. * * @return the security provider */ @@ -68,7 +68,7 @@ public interface ILevel1Data { public void addData(IData data); /** - * Gets the level 2 key alg. + * Gets the level 2 key algorithm OID. * * @return the level 2 key alg */ @@ -76,7 +76,7 @@ public interface ILevel1Data { /** - * Sets the level 2 key alg. + * Sets the level 2 key algorithm OID. * * @param level2KeyAlg the new level 2 key alg */ @@ -92,7 +92,7 @@ public interface ILevel1Data { /** - * Sets the level 1 signing alg. + * Sets the level 1 signing algorithm OID. * * @param level1SigningAlg the new level 1 signing alg */ @@ -100,7 +100,7 @@ public interface ILevel1Data { /** - * Gets the level 2 signing alg. + * Gets the level 2 signing algorithm OID. * * @return the level 2 signing alg */ @@ -108,7 +108,7 @@ public interface ILevel1Data { /** - * Sets the level 2 signing alg. + * Sets the level 2 signing algorithm OID. * * @param level2SigningAlg the new level 2 signing alg */ @@ -133,14 +133,14 @@ public interface ILevel1Data { /** - * Gets the level 1 key alg. + * Gets the level 1 key algorithm OID. * * @return the level 1 key alg */ public String getLevel1KeyAlg(); /** - * Sets the level 1 key alg. + * Sets the level 1 key algorithm OID. * * @param level1KeyAlg the new level 1 key alg */ @@ -149,6 +149,13 @@ public interface ILevel1Data { /** * Sets the end of validity date. The validity date has to be provided in UTC. + * + * -- end of the validity of the bar code, after this date and time the bar code needs to be regenerated + * -- by the provider of the ticket + * -- if end of validity is provided year day and time must be provided. + * -- year, day, time are in UTC + * -- the provider of the bar code should ensure that the endOfValidity given here does not exceed + * -- the validity of the key pair used on level 2. * * @param date the new end of validity date */ @@ -156,9 +163,40 @@ public interface ILevel1Data { /** - * Gets the end of validity date. + * Gets the end of validity date and time. + * + * -- end of the validity of the bar code, after this date and time the bar code needs to be regenerated + * -- by the provider of the ticket + * -- if end of validity is provided year day and time must be provided. + * -- year, day, time are in UTC + * -- the provider of the bar code should ensure that the endOfValidity given here does not exceed + * -- the validity of the key pair used on level 2. * * @return the end of validity date */ public Date getEndOfBarcodeValidity(); + + + /** + * Gets the validity duration of the bar code in seconds. + * + * -- validity duration in seconds of the bar code shown with reference to the time stamp dynamicContentTimeStamp + * -- in the dynamic data included in the level2Data + * + * @return the validity duration + */ + public Long getValidityDuration(); + + + /** + * Sets the validity validity duration of the bar code in seconds. + * + * -- validity duration in seconds of the bar code shown with reference to the time stamp dynamicContentTimeStamp + * -- in the dynamic data included in the level2Data + * + * @param validityDuration the new validity duration + */ + public void setValidityDuration(Long validityDuration); + + } diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java index 65b81d6..8d53f9a 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java @@ -12,7 +12,7 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; import java.util.Date; -import org.uic.barcode.asn1.uper.AsnUtils; +import org.uic.barcode.asn1.uper.UperEncoder; import org.uic.barcode.dynamicContent.api.DynamicContentCoder; import org.uic.barcode.dynamicContent.api.IUicDynamicContent; import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; @@ -40,7 +40,7 @@ public class SimpleDynamicFrame implements IDynamicFrame { } /** The format. */ - public String format = Constants.DYNAMIC_BARCODE_FORMAT_DEFAULT; + public String format = null; /** The level 2 signed data. */ /*level 2 data*/ @@ -115,136 +115,15 @@ public class SimpleDynamicFrame implements IDynamicFrame { * @throws EncodingFormatException */ public byte[] encode() throws EncodingFormatException { - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { - - return DynamicFrameCoderV1.encode(this); - - } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { - - return DynamicFrameCoderV2.encode(this); - - } - - return null; - } - - private byte[] encode(ILevel1Data level1Data) throws EncodingFormatException { - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { - - return DynamicFrameCoderV1.encode(level1Data); - - } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { - - return DynamicFrameCoderV2.encode(level1Data); - - } - throw new EncodingFormatException("Dynamic Header Version not supported: " + format); - } - - private byte[] getEncoded(String path, byte[] data) throws EncodingFormatException { - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { - - return DynamicFrameCoderV1.getEncoded(path, data); - - } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { - - return DynamicFrameCoderV2.getEncoded(path, data); - - } - throw new EncodingFormatException("Dynamic Header Version not supported: " + format); - } - - - private byte[] encode(ILevel2Data level2Data) throws EncodingFormatException { - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { - - return DynamicFrameCoderV1.encode(level2Data); - - } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { - - return DynamicFrameCoderV2.encode(level2Data); - - } - - throw new EncodingFormatException("Dynamic Header Version not supported: " + format); - } - - /** - * Decode. - * - * Decode the header from an ASN.1 PER UNALIGNED encoded byte array - * - * @param bytes the bytes - * @return the dynamic header - * @throws EncodingFormatException - */ - public void decode(byte[] bytes) throws EncodingFormatException { - - String format = getFormat(bytes); - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { - - DynamicFrameCoderV1.decode(this,bytes); - return; + return DynamicFrameCoder.encode(this); - } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { - - DynamicFrameCoderV2.decode(this,bytes); - return; - } - - throw new EncodingFormatException("Dynamic Header Version not supported"); + } - - /** - * Checks if is static header. - * - * @param data the data - * @return true, if is static header - */ - private static String getFormat(byte[] data) { - - if (data == null || data.length < 4) return null; - - byte[] startBits = new byte[4]; - startBits[0] = data[0]; - startBits[1] = data[1]; - startBits[2] = data[2]; - startBits[3] = data[3]; - - String start = AsnUtils.toBooleanString(startBits); - - /* - * bitshift: - * - * version 1: - * optional Level2Data 1 bit - * length of format: 8 bit - * - * version 2: - * extensionIndicator 1 bit - * optional Level2Data 1 bit - * length of format: 8 bit - */ - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1_BIN.equals(start.substring(9, 23))) { - return Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1; - } - - if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2_BIN.equals(start.substring(10, 24))) { - return Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2; - } - return null; - } /** * Verify the level 2 signature @@ -334,13 +213,7 @@ public class SimpleDynamicFrame implements IDynamicFrame { } try { - //TODO - //byte[] signedData = encode(level2Data); - //String s1 = AsnUtils.toBooleanString(signedData); - - byte[] signedData2 = getEncoded("Level2Data", data); - //String s2 = AsnUtils.toBooleanString(signedData); - + byte[] signedData2 = getLevel2DataBin(); sig.update(signedData2); } catch (SignatureException e) { return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED; @@ -416,7 +289,7 @@ public class SimpleDynamicFrame implements IDynamicFrame { try { - byte[] encodedData = getEncoded("Level1Data", data); + byte[] encodedData = getLevel1DataBin(); sig.update(encodedData); @@ -489,7 +362,7 @@ public class SimpleDynamicFrame implements IDynamicFrame { sig = Signature.getInstance(algo); } sig.initSign(key); - byte[] signedData = encode(level2Data); + byte[] signedData = DynamicFrameCoder.encodeLevel2Data(this); sig.update(signedData); level2Signature = sig.sign(); @@ -584,9 +457,49 @@ public class SimpleDynamicFrame implements IDynamicFrame { } sig.initSign(key); - byte[] data = encode(level1Data); + byte[] data = DynamicFrameCoder.encodeLevel1(this); sig.update(data); level2Data.setLevel1Signature(sig.sign()); } + + @Override + public byte[] getLevel1Signature() { + return getLevel2Data().getLevel1Signature(); + } + + @Override + public byte[] getLevel1DataBin() throws EncodingFormatException { + + if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { + + return DynamicFrameCoderV1.encode(getLevel2Data().getLevel1Data()); + + } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { + + return DynamicFrameCoderV2.encode(getLevel2Data().getLevel1Data()); + + } + + throw new EncodingFormatException("Dynamic Header Version not supported"); + + } + + + public byte[] getLevel2DataBin() throws EncodingFormatException { + + if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) { + + return DynamicFrameCoderV1.encodeLevel2Data(getLevel2Data()); + + } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) { + + return DynamicFrameCoderV2.encodeLevel2Data(getLevel2Data()); + + } + + throw new EncodingFormatException("Dynamic Header Version not supported"); + + } + } diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java index e9b1d4e..f42ff98 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java @@ -63,6 +63,7 @@ public class SimpleLevel1Data implements ILevel1Data { public Date endOfBarcodeValidity = null; + public Long validityDuration = null; @@ -250,4 +251,14 @@ public class SimpleLevel1Data implements ILevel1Data { dataList.add(data); } + + public Long getValidityDuration() { + return validityDuration; + } + + public void setValidityDuration(Long validityDuration) { + this.validityDuration = validityDuration; + } + + } diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java index 395db4d..17e71db 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java @@ -2,15 +2,10 @@ package org.uic.barcode.dynamicFrame.api; import org.uic.barcode.asn1.datatypes.Asn1Optional; import org.uic.barcode.asn1.datatypes.FieldOrder; -import org.uic.barcode.asn1.datatypes.HasExtensionMarker; -import org.uic.barcode.asn1.datatypes.Sequence; -import org.uic.barcode.asn1.uper.UperEncoder; /** * The Class DataType. */ -@Sequence -@HasExtensionMarker public class SimpleLevel2Data implements ILevel2Data { @FieldOrder(order = 0) @@ -56,20 +51,6 @@ public class SimpleLevel2Data implements ILevel2Data { public void setLevel2Data(IData level2Data) { this.level2Data = level2Data; } - - - /** - * Encode. - * - * Encode the header as ASN.1 PER UNALIGNED byte array - * - * @return the byte[] - */ - public byte[] encode() { - return UperEncoder.encode(this); - } - - } -- cgit v1.2.3