diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-01-21 18:19:36 +0100 |
---|---|---|
committer | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-01-21 18:19:36 +0100 |
commit | 17f05b763d70f350bad482df9378c571c2ebddf6 (patch) | |
tree | b388db6cdf2906fab3dd5a1dc2f79fbfe3f903ea /src/main/java/org/uic/barcode/dynamicFrame/api | |
parent | extended api for dynamic content (diff) | |
download | UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.tar UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.tar.gz UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.tar.bz2 UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.tar.lz UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.tar.xz UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.tar.zst UIC-barcode-17f05b763d70f350bad482df9378c571c2ebddf6.zip |
Diffstat (limited to 'src/main/java/org/uic/barcode/dynamicFrame/api')
8 files changed, 1371 insertions, 0 deletions
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/IData.java b/src/main/java/org/uic/barcode/dynamicFrame/api/IData.java new file mode 100644 index 0000000..51f9c7b --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/IData.java @@ -0,0 +1,39 @@ +package org.uic.barcode.dynamicFrame.api;
+
+/**
+ * The Class DataType.
+ */
+public interface IData {
+
+
+
+
+ /**
+ * Gets the data format.
+ *
+ * @return the data format
+ */
+ public String getFormat();
+
+ /**
+ * Sets the data format.
+ *
+ * @param dataFormat the new data format
+ */
+ public void setFormat(String format);
+ /**
+ * Gets the data.
+ *
+ * @return the data
+ */
+ public byte[] getData();
+
+ /**
+ * Sets the data.
+ *
+ * @param data the new data
+ */
+ public void setData(byte[] data);
+
+
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java new file mode 100644 index 0000000..f357c4d --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/IDynamicFrame.java @@ -0,0 +1,202 @@ +package org.uic.barcode.dynamicFrame.api;
+
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import org.uic.barcode.dynamicContent.api.IUicDynamicContent;
+import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1;
+import org.uic.barcode.ticket.EncodingFormatException;
+
+
+/**
+ * The DynamicHeader for bar codes
+ *
+ */
+public interface IDynamicFrame{
+
+
+
+ /**
+ * Gets the format.
+ *
+ * @return the format
+ */
+ public String getFormat();
+
+
+ /**
+ * Sets the format.
+ *
+ * @param format the new format
+ */
+ public void setFormat(String format);
+
+ /**
+ * Gets the level 2 signed data.
+ *
+ * @return the level 2 signed data
+ */
+ public ILevel2Data getLevel2Data();
+
+ /**
+ * Sets the level 2 signed data.
+ *
+ * @param level2SignedData the new level 2 signed data
+ */
+ public void setLevel2Data(ILevel2Data level2Data);
+
+
+ /**
+ * Gets the level 2 signature.
+ *
+ * @return the level 2 signature
+ */
+ public byte[] getLevel2Signature();
+
+
+ /**
+ * Sets the level 2 signature.
+ *
+ * @param level2Signature the new level 2 signature
+ */
+ 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
+ */
+ public void decode(byte[] bytes);
+
+
+
+ /**
+ * Verify the level 2 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel2() throws EncodingFormatException;
+
+ /**
+ * Verify the level 2 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param prov the prov
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel2(Provider prov) throws EncodingFormatException;
+
+ /**
+ * Verify the level 1 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param key the key
+ * @param prov the prov
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel1(PublicKey key, Provider prov) throws EncodingFormatException;
+
+
+ /**
+ * Verify the level 1 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param key the key
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel1(PublicKey key) throws EncodingFormatException;
+
+ /**
+ * Sign level 2 data without a specific security provider.
+ *
+ * @param key the key
+ * @throws Exception the exception
+ */
+ public void signLevel2(PrivateKey key) throws Exception;
+
+
+ /**
+ * Sign level 2 data.
+ *
+ * @param key the key
+ * @param prov the security Provider
+ * @throws Exception the exception
+ */
+ public void signLevel2(PrivateKey key, Provider prov) throws Exception;
+
+
+ /**
+ * Adds the dynamic content and encodes it. (API level)
+ *
+ * @param content the dynamic content
+ * @throws EncodingFormatException the encoding format exception
+ */
+ public void addDynamicContent(IUicDynamicContent content) throws EncodingFormatException;
+
+
+ /**
+ * Adds the level 2 dynamic data. (ASN level)
+ *
+ * @param dynamicData the dynamic data
+ */
+ public void addLevel2DynamicData(UicDynamicContentDataFDC1 dynamicData);
+
+ /**
+ * Gets the dynamic content.
+ *
+ * @return the dynamic content
+ */
+ public IUicDynamicContent getDynamicContent();
+
+
+ /**
+ * Sign the contained data block.
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param key the key
+ * @return
+ * @return the byte[]
+ * @throws Exception
+ */
+ public void signLevel1(PrivateKey key) throws Exception;
+
+ /**
+ * Sign the contained data block.
+ *
+ * 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
+ * @return the byte[]
+ * @throws Exception
+ */
+ public void signLevel1(PrivateKey key, Provider prov) throws Exception;
+
+
+
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java new file mode 100644 index 0000000..206d613 --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel1Data.java @@ -0,0 +1,164 @@ +package org.uic.barcode.dynamicFrame.api;
+
+import java.util.Collection;
+import java.util.Date;
+
+/**
+ * The Class SignedDataType.
+ */
+public interface ILevel1Data {
+
+
+
+ /**
+ * Sets the security provider
+ *
+ * @param securityProviderNum the new security provider
+ */
+ public void setSecurityProvider(String securityProvider);
+
+
+ /**
+ * Gets the security provider
+ *
+ * @return the security provider
+ */
+ public String getSecurityProvider();
+
+
+
+
+
+
+ /**
+ * Gets the key id.
+ *
+ * @return the key id
+ */
+ public Long getKeyId();
+
+
+ /**
+ * Sets the key id.
+ *
+ * @param keyId the new key id
+ */
+ public void setKeyId(Long keyId);
+
+ /**
+ * Gets the data.
+ *
+ * @return the data
+ */
+ public Collection<IData> getData();
+
+
+ /**
+ * Sets the data.
+ *
+ * @param data the new data
+ */
+ public void setData(Collection<IData> data);
+
+ /**
+ * Adds data.
+ *
+ * @param data the new data
+ */
+ public void addData(IData data);
+
+ /**
+ * Gets the level 2 key alg.
+ *
+ * @return the level 2 key alg
+ */
+ public String getLevel2KeyAlg();
+
+
+ /**
+ * Sets the level 2 key alg.
+ *
+ * @param level2KeyAlg the new level 2 key alg
+ */
+ public void setLevel2KeyAlg(String level2KeyAlg);
+
+
+ /**
+ * Gets the level 1 signing alg.
+ *
+ * @return the level 1 signing alg
+ */
+ public String getLevel1SigningAlg();
+
+
+ /**
+ * Sets the level 1 signing alg.
+ *
+ * @param level1SigningAlg the new level 1 signing alg
+ */
+ public void setLevel1SigningAlg(String level1SigningAlg);
+
+
+ /**
+ * Gets the level 2 signing alg.
+ *
+ * @return the level 2 signing alg
+ */
+ public String getLevel2SigningAlg();
+
+
+ /**
+ * Sets the level 2 signing alg.
+ *
+ * @param level2SigningAlg the new level 2 signing alg
+ */
+ public void setLevel2SigningAlg(String level2SigningAlg);
+
+
+ /**
+ * Gets the level 2 public key.
+ *
+ * @return the level 2 public key
+ */
+ public byte[] getLevel2publicKey();
+
+
+ /**
+ * Sets the level 2 public key.
+ *
+ * @param level2publicKey the new level 2 public key
+ */
+ public void setLevel2publicKey(byte[] level2publicKey);
+
+
+
+ /**
+ * Gets the level 1 key alg.
+ *
+ * @return the level 1 key alg
+ */
+ public String getLevel1KeyAlg();
+
+ /**
+ * Sets the level 1 key alg.
+ *
+ * @param level1KeyAlg the new level 1 key alg
+ */
+ public void setLevel1KeyAlg(String level1KeyAlg);
+
+
+ /**
+ * Sets the end of validity date. The validity date has to be provided in UTC.
+ *
+ * @param date the new end of validity date
+ */
+ public void setEndOfBarcodeValidity(Date date);
+
+
+ /**
+ * Gets the end of validity date.
+ *
+ * @return the end of validity date
+ */
+ public Date getEndOfBarcodeValidity();
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel2Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel2Data.java new file mode 100644 index 0000000..cc28422 --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/ILevel2Data.java @@ -0,0 +1,25 @@ +package org.uic.barcode.dynamicFrame.api;
+
+/**
+ * The Level 2 data.
+ */
+
+public interface ILevel2Data {
+
+
+
+ public ILevel1Data getLevel1Data();
+
+ public void setLevel1Data(ILevel1Data level1Data);
+
+ public byte[] getLevel1Signature();
+
+ public byte[] getLevel1SignatureBytes();
+
+ public void setLevel1Signature(byte[] level1Signature);
+
+ public IData getLevel2Data();
+
+ public void setLevel2Data(IData level2Data);
+
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleData.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleData.java new file mode 100644 index 0000000..cb762de --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleData.java @@ -0,0 +1,59 @@ +package org.uic.barcode.dynamicFrame.api;
+
+import org.uic.barcode.asn1.datatypes.CharacterRestriction;
+import org.uic.barcode.asn1.datatypes.HasExtensionMarker;
+import org.uic.barcode.asn1.datatypes.RestrictedString;
+import org.uic.barcode.asn1.datatypes.Sequence;
+import org.uic.barcode.asn1.datatypesimpl.OctetString;
+import org.uic.barcode.asn1.uper.UperEncoder;
+
+/**
+ * The Class DataType.
+ */
+public class SimpleData implements IData{
+
+
+ /** The data format.
+ *
+ * -- FCB1 FCB version 1
+ * -- FCB2 FCB version 2
+ * -- RICS company code + ...
+ **/
+ public String format;
+
+ /** The data. */
+ public byte[] data;
+
+ /**
+ * Gets the data format.
+ *
+ * @return the data format
+ */
+ public String getFormat() {
+ return format;
+ }
+
+ /**
+ * Sets the data format.
+ *
+ * @param dataFormat the new data format
+ */
+ public void setFormat(String format) {
+ this.format = format;
+ }
+
+ /**
+ * Gets the data.
+ *
+ * @return the data
+ */
+ public byte[] getData() {
+ return data;
+ }
+
+ @Override
+ public void setData(byte[] data) {
+ this.data = data;
+ }
+
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java new file mode 100644 index 0000000..4c5c879 --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java @@ -0,0 +1,553 @@ +package org.uic.barcode.dynamicFrame.api;
+
+import java.security.InvalidKeyException;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.X509EncodedKeySpec;
+import java.util.Date;
+
+import org.uic.barcode.dynamicContent.api.DynamicContentCoder;
+import org.uic.barcode.dynamicContent.api.IUicDynamicContent;
+import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1;
+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;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+
+
+
+/**
+ * The DynamicHeader for bar codes
+ *
+ */
+public class SimpleDynamicFrame implements IDynamicFrame {
+
+ /**
+ * Instantiates a new dynamic frame.
+ */
+ public SimpleDynamicFrame() {}
+
+ public SimpleDynamicFrame(String format) {
+ this.format = format;
+ }
+
+ /** The format. */
+ public String format = Constants.DYNAMIC_BARCODE_FORMAT_DEFAULT;
+
+ /** The level 2 signed data. */
+ /*level 2 data*/
+ public ILevel2Data level2Data;
+
+
+ /** The signature of level 2 data. */
+ public byte[] level2Signature;
+
+ public Date endOfValidity = null;
+
+ /**
+ * Gets the format.
+ *
+ * @return the format
+ */
+ public String getFormat() {
+ return format;
+ }
+
+ /**
+ * Sets the format.
+ *
+ * @param format the new format
+ */
+ public void setFormat(String format) {
+ this.format = format;
+ }
+
+ /**
+ * Gets the level 2 signed data.
+ *
+ * @return the level 2 signed data
+ */
+ public ILevel2Data getLevel2Data() {
+ return level2Data;
+ }
+
+ /**
+ * Sets the level 2 signed data.
+ *
+ * @param level2SignedData the new level 2 signed data
+ */
+ public void setLevel2Data(ILevel2Data level2SignedData) {
+ this.level2Data = level2SignedData;
+ }
+
+ /**
+ * Gets the level 2 signature.
+ *
+ * @return the level 2 signature
+ */
+ public byte[] getLevel2Signature() {
+ return level2Signature;
+ }
+
+ /**
+ * Sets the level 2 signature.
+ *
+ * @param level2Signature the new level 2 signature
+ */
+ public void setLevel2Signature(byte[] level2Signature) {
+ this.level2Signature = level2Signature;
+ }
+
+ /**
+ * Encode.
+ *
+ * Encode the header as ASN.1 PER UNALIGNED byte array
+ *
+ * @return the byte[]
+ * @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_1.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_1.equals(format)) {
+
+ return DynamicFrameCoderV2.encode(level1Data);
+
+ }
+
+ return null;
+ }
+
+ private byte[] encode(ILevel2Data level2SignedData2) throws EncodingFormatException {
+
+ if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) {
+
+ return DynamicFrameCoderV1.encode(level2SignedData2);
+
+ } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) {
+
+ return DynamicFrameCoderV2.encode(level2SignedData2);
+
+ }
+
+ return null;
+ }
+
+ /**
+ * Decode.
+ *
+ * Decode the header from an ASN.1 PER UNALIGNED encoded byte array
+ *
+ * @param bytes the bytes
+ * @return the dynamic header
+ */
+ public void decode(byte[] bytes) {
+
+ String format = getFormat(bytes);
+
+ if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(format)) {
+
+ DynamicFrameCoderV1.decode(this,bytes);
+
+ } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(format)) {
+
+ DynamicFrameCoderV2.decode(this,bytes);
+
+ }
+
+
+
+ }
+
+
+
+
+ /**
+ * Checks if is static header.
+ *
+ * @param data the data
+ * @return true, if is static header
+ */
+ private static String getFormat(byte[] data) {
+ byte[] start = "U1".getBytes();
+ if (start[0] != data[0] || start[1]!= start[1]) {
+ return Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1;
+ }
+ start = "U2".getBytes();
+ if (start[0] != data[0] || start[1]!= start[1]) {
+ return Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2;
+ }
+ return null;
+ }
+
+ /**
+ * Verify the level 2 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel2() throws EncodingFormatException {
+
+ return validateLevel2(null);
+
+ }
+
+ /**
+ * Verify the level 2 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param prov the prov
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel2(Provider prov) throws EncodingFormatException {
+
+
+ String level2KeyAlg = this.getLevel2Data().getLevel1Data().getLevel2KeyAlg();
+
+
+ if (level2KeyAlg == null || level2KeyAlg.length() == 0) {
+ return Constants.LEVEL2_VALIDATION_NO_KEY;
+ }
+
+ if (level2Signature == null || level2Signature.length == 0) {
+ return Constants.LEVEL2_VALIDATION_NO_SIGNATURE;
+ }
+
+ String keyAlgName = null;
+ try {
+ keyAlgName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, level2KeyAlg);
+ } catch (Exception e1) {
+ return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED;
+ }
+ if (keyAlgName == null || keyAlgName.length() == 0) {
+ return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED;
+ }
+
+ PublicKey key = null;
+ try {
+ byte[] keyBytes = this.getLevel2Data().getLevel1Data().getLevel2publicKey();
+ X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
+ key = KeyFactory.getInstance(keyAlgName).generatePublic(keySpec);
+ } catch (InvalidKeySpecException e1) {
+ return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED;
+ } catch (NoSuchAlgorithmException e1) {
+ return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED;
+ }
+
+ //find the algorithm name for the signature OID
+ String level2SigAlg = this.getLevel2Data().getLevel1Data().getLevel2SigningAlg();
+
+ String sigAlgName = null;
+ try {
+ sigAlgName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_SIGNATURE_ALG,level2SigAlg);
+ } catch (Exception e1) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+ if (sigAlgName == null) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+
+ Signature sig;
+ try {
+ if (prov == null) {
+ sig = Signature.getInstance(sigAlgName);
+ } else {
+ sig = Signature.getInstance(sigAlgName, prov);
+ }
+ } catch (NoSuchAlgorithmException e) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+ try {
+ sig.initVerify(key);
+ } catch (InvalidKeyException e) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+
+ try {
+ byte[] data = encode(level2Data);
+ sig.update(data);
+ } catch (SignatureException e) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ } catch (IllegalArgumentException e) {
+ return Constants.LEVEL2_VALIDATION_ENCODING_ERROR;
+ } catch (UnsupportedOperationException e) {
+ return Constants.LEVEL2_VALIDATION_ENCODING_ERROR;
+ }
+
+ byte[] signature = level2Signature;
+ try {
+ if (sig.verify(signature)){
+ return Constants.LEVEL2_VALIDATION_OK;
+ } else {
+ return Constants.LEVEL2_VALIDATION_FRAUD;
+ }
+ } catch (SignatureException e) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+ }
+
+ /**
+ * Verify the level 1 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param key the key
+ * @param prov the prov
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel1(PublicKey key, Provider prov) throws EncodingFormatException {
+
+ if (level2Data == null) {
+ return Constants.LEVEL1_VALIDATION_NO_SIGNATURE;
+ }
+
+
+ if (level2Data == null ||
+ level2Data.getLevel1Signature().length == 0) {
+ return Constants.LEVEL1_VALIDATION_NO_SIGNATURE;
+ }
+
+ byte[] signature = this.getLevel2Data().getLevel1Signature();
+
+ //find the algorithm name for the signature OID
+ String algo = null;
+ try {
+ algo = AlgorithmNameResolver.getSignatureAlgorithmName(getLevel2Data().getLevel1Data().getLevel1SigningAlg());
+ } catch (Exception e1) {
+ return Constants.LEVEL1_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+ if (algo == null) {
+ return Constants.LEVEL1_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+
+ Signature sig;
+ try {
+ if (prov != null) {
+ sig = Signature.getInstance(algo, prov);
+ } else {
+ sig = Signature.getInstance(algo);
+
+ }
+ } catch (NoSuchAlgorithmException e) {
+ return Constants.LEVEL1_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+ try {
+ sig.initVerify(key);
+ } catch (InvalidKeyException e) {
+ return Constants.LEVEL1_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+
+ try {
+ sig.update(encode(level2Data.getLevel1Data()));
+ } catch (SignatureException e) {
+ return Constants.LEVEL1_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ } catch (IllegalArgumentException e) {
+ return Constants.LEVEL1_VALIDATION_ENCODING_ERROR;
+ } catch (UnsupportedOperationException e) {
+ return Constants.LEVEL1_VALIDATION_ENCODING_ERROR;
+ }
+
+
+ try {
+ if (sig.verify(signature)){
+ return Constants.LEVEL2_VALIDATION_OK;
+ } else {
+ return Constants.LEVEL2_VALIDATION_FRAUD;
+ }
+ } catch (SignatureException e) {
+ return Constants.LEVEL2_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
+ }
+ }
+
+
+
+
+
+ /**
+ * Verify the level 1 signature
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param key the key
+ * @return the int
+ * @throws EncodingFormatException
+ */
+ public int validateLevel1(PublicKey key) throws EncodingFormatException {
+
+ return validateLevel1(key, null);
+
+ }
+
+ /**
+ * Sign level 2 data without a specific security provider.
+ *
+ * @param key the key
+ * @throws Exception the exception
+ */
+ public void signLevel2(PrivateKey key) throws Exception {
+
+ //find the algorithm name for the signature OID
+ String algo = AlgorithmNameResolver.getSignatureAlgorithmName(level2Data.getLevel1Data().getLevel2SigningAlg());
+ Signature sig = Signature.getInstance(algo);
+ sig.initSign(key);
+ byte[] data = encode(level2Data);
+ sig.update(data);
+ level2Signature = sig.sign();
+
+ }
+
+ /**
+ * Sign level 2 data.
+ *
+ * @param key the key
+ * @param prov the security Provider
+ * @throws Exception the exception
+ */
+ public void signLevel2(PrivateKey key, Provider prov) throws Exception {
+
+ //find the algorithm name for the signature OID
+ String algo = AlgorithmNameResolver.getSignatureAlgorithmName(this.getLevel2Data().getLevel1Data().getLevel2SigningAlg());
+ Signature sig = Signature.getInstance(algo,prov);
+ sig.initSign(key);
+ byte[] data = encode(level2Data);
+ sig.update(data);
+ level2Signature = sig.sign();
+
+ }
+
+
+ /**
+ * Adds the dynamic content and encodes it. (API level)
+ *
+ * @param content the dynamic content
+ * @throws EncodingFormatException the encoding format exception
+ */
+ public void addDynamicContent(IUicDynamicContent content) throws EncodingFormatException {
+
+
+ level2Data.setLevel2Data(new SimpleData());
+
+ level2Data.getLevel2Data().setFormat(DynamicContentCoder.dynamicContentDataFDC1);
+
+ level2Data.getLevel2Data().setData(DynamicContentCoder.encode(content, DynamicContentCoder.dynamicContentDataFDC1));
+
+ }
+
+ /**
+ * Adds the level 2 dynamic data. (ASN level)
+ *
+ * @param dynamicData the dynamic data
+ */
+ public void addLevel2DynamicData(UicDynamicContentDataFDC1 dynamicData) {
+ this.getLevel2Data().setLevel2Data(dynamicData.getApiDataType());
+ }
+
+ /**
+ * Gets the dynamic content.
+ *
+ * @return the dynamic content
+ */
+ public IUicDynamicContent getDynamicContent() {
+
+ if (this.getLevel2Data() == null ||
+ this.getLevel2Data().getLevel2Data() == null){
+ return null;
+ }
+
+ return DynamicContentCoder.decode(level2Data.getLevel2Data().getData());
+
+ }
+
+
+ /**
+ * Sign the contained data block.
+ *
+ * Note: an appropriate security provider (e.g. BC) must be registered before
+ *
+ * @param key the key
+ * @return
+ * @return the byte[]
+ * @throws Exception
+ */
+ public void signLevel1(PrivateKey key) throws Exception {
+
+ if (level2Data == null) return;
+
+ ILevel1Data level1Data = level2Data.getLevel1Data();
+
+ if (level1Data == null) return;
+
+ //find the algorithm name for the signature OID
+ String algo = AlgorithmNameResolver.getSignatureAlgorithmName(level1Data.getLevel1SigningAlg());
+ Signature sig = Signature.getInstance(algo);
+ sig.initSign(key);
+ byte[] data = encode(level1Data);
+ sig.update(data);
+ level2Data.setLevel1Signature(sig.sign());
+ }
+
+ /**
+ * Sign the contained data block.
+ *
+ * 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
+ * @return the byte[]
+ * @throws Exception
+ */
+ @Override
+ public void signLevel1(PrivateKey key, Provider prov) throws Exception {
+
+ if (level2Data == null) return;
+
+ ILevel1Data level1Data = level2Data.getLevel1Data();
+
+ if (level1Data == null) return;
+
+ //find the algorithm name for the signature OID
+ String algo = AlgorithmNameResolver.getSignatureAlgorithmName(level1Data.getLevel1SigningAlg());
+ Signature sig = Signature.getInstance(algo, prov);
+ sig.initSign(key);
+
+ byte[] data = encode(level1Data);
+ sig.update(data);
+ level2Data.setLevel1Signature(sig.sign());
+ }
+
+
+
+
+
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java new file mode 100644 index 0000000..241cf6d --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel1Data.java @@ -0,0 +1,254 @@ +package org.uic.barcode.dynamicFrame.api;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import org.uic.barcode.asn1.uper.UperEncoder;
+
+/**
+ * The Class SignedDataType.
+ */
+
+public class SimpleLevel1Data implements ILevel1Data {
+
+ /** The security provider */
+ public String securityProvider;
+
+
+ /** The key id. */
+ public Long keyId;
+
+
+ /** The data. */
+ public Collection<IData> dataList;
+
+ /**
+ * The key generator algorithms
+ * Object Identifier of the Algorithm
+ * Number notation:
+ *
+ * e.g.:
+ * -- DSA SHA224 2.16.840.1.101.3.4.3.1
+ * -- DSA SHA256 2.16.840.1.101.3.4.3.2
+ * -- ECC 256 1.2.840.10045.3.1.7
+ *
+ *
+ */
+ public String level1KeyAlg;
+
+ /** The level 2 key alg. */
+ public String level2KeyAlg;
+
+ /**
+ * The signing algorithm
+ * Object Identifier of the Algorithms
+ * Number notation:
+ *
+ * e.g.:
+ * -- DSA SHA224 2.16.840.1.101.3.4.3.1
+ * -- DSA SHA256 2.16.840.1.101.3.4.3.2
+ * -- ECC 256 1.2.840.10045.3.1.7
+ *
+ *
+ */
+ public String level1SigningAlg;
+
+ /** The level 2 signing alg. */
+ public String level2SigningAlg;
+
+
+ /** The level 2 public key. */
+ public byte[] level2publicKey;
+
+
+ public Date endOfBarcodeValidity = null;
+
+
+
+
+
+ /**
+ * Gets the security provider .
+ *
+ * @return the security provider
+ */
+ public String getSecurityProvider() {
+ return securityProvider;
+ }
+
+ /**
+ * Sets the security provider.
+ *
+ * in case the security provider code is encoded in IA5 this will return null
+ *
+ * @param securityProviderNum the new security provider
+ */
+ public void setSecurityProvider(String securityProvider) {
+ this.securityProvider = securityProvider;
+ }
+
+
+
+
+
+ /**
+ * Gets the key id.
+ *
+ * @return the key id
+ */
+ public Long getKeyId() {
+ return keyId;
+ }
+
+ /**
+ * Sets the key id.
+ *
+ * @param keyId the new key id
+ */
+ public void setKeyId(Long keyId) {
+ this.keyId = keyId;
+ }
+
+ /**
+ * Gets the data.
+ *
+ * @return the data
+ */
+ public Collection<IData> getData() {
+ return dataList;
+ }
+
+ /**
+ * Sets the data.
+ *
+ * @param data the new data
+ */
+ public void setData(Collection<IData> data) {
+ this.dataList = data;
+ }
+
+ /**
+ * Gets the level 2 key alg.
+ *
+ * @return the level 2 key alg
+ */
+ public String getLevel2KeyAlg() {
+ return level2KeyAlg;
+ }
+
+ /**
+ * Sets the level 2 key alg.
+ *
+ * @param level2KeyAlg the new level 2 key alg
+ */
+ public void setLevel2KeyAlg(String level2KeyAlg) {
+ this.level2KeyAlg = level2KeyAlg;
+ }
+
+ /**
+ * Gets the level 1 signing alg.
+ *
+ * @return the level 1 signing alg
+ */
+ public String getLevel1SigningAlg() {
+ return level1SigningAlg;
+ }
+
+ /**
+ * Sets the level 1 signing alg.
+ *
+ * @param level1SigningAlg the new level 1 signing alg
+ */
+ public void setLevel1SigningAlg(String level1SigningAlg) {
+ this.level1SigningAlg = level1SigningAlg;
+ }
+
+ /**
+ * Gets the level 2 signing alg.
+ *
+ * @return the level 2 signing alg
+ */
+ public String getLevel2SigningAlg() {
+ return level2SigningAlg;
+ }
+
+ /**
+ * Sets the level 2 signing alg.
+ *
+ * @param level2SigningAlg the new level 2 signing alg
+ */
+ public void setLevel2SigningAlg(String level2SigningAlg) {
+ this.level2SigningAlg = level2SigningAlg;
+ }
+
+ /**
+ * Gets the level 2 public key.
+ *
+ * @return the level 2 public key
+ */
+ public byte[] getLevel2publicKey() {
+ return level2publicKey;
+ }
+
+ /**
+ * Sets the level 2 public key.
+ *
+ * @param level2publicKey the new level 2 public key
+ */
+ public void setLevel2publicKey(byte[] level2publicKey) {
+ this.level2publicKey = level2publicKey;
+ }
+
+
+
+ /**
+ * Gets the level 1 key alg.
+ *
+ * @return the level 1 key alg
+ */
+ public String getLevel1KeyAlg() {
+ return level1KeyAlg;
+ }
+
+ /**
+ * Sets the level 1 key alg.
+ *
+ * @param level1KeyAlg the new level 1 key alg
+ */
+ public void setLevel1KeyAlg(String level1KeyAlg) {
+ this.level1KeyAlg = level1KeyAlg;
+ }
+
+ /**
+ * Sets the end of validity date. The validity date has to be provided in UTC.
+ *
+ * @param date the new end of validity date
+ */
+ public void setEndOfBarcodeValidity(Date date){
+
+ endOfBarcodeValidity = date;
+
+
+ }
+
+ /**
+ * Gets the end of validity date.
+ *
+ * @return the end of validity date
+ */
+ public Date getEndOfBarcodeValidity() {
+
+ return endOfBarcodeValidity;
+ }
+
+ @Override
+ public void addData(IData data) {
+
+ if (dataList == null) {
+ dataList = new ArrayList<IData>();
+ }
+
+ dataList.add(data);
+
+ }
+}
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java new file mode 100644 index 0000000..395db4d --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleLevel2Data.java @@ -0,0 +1,75 @@ +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)
+ ILevel1Data level1Data;
+
+ /** The data. */
+ @FieldOrder(order = 1)
+ @Asn1Optional public byte[] level1Signature;
+
+ @FieldOrder(order = 2)
+ @Asn1Optional IData level2Data;
+
+
+ public ILevel1Data getLevel1Data() {
+ return level1Data;
+ }
+
+
+ public void setLevel1Data(ILevel1Data level1Data) {
+ this.level1Data = level1Data;
+ }
+
+
+ public byte[] getLevel1Signature() {
+ return level1Signature;
+ }
+
+ public byte[] getLevel1SignatureBytes() {
+ return level1Signature;
+ }
+
+
+ public void setLevel1Signature(byte[] level1Signature) {
+ this.level1Signature = level1Signature;
+ }
+
+
+ public IData getLevel2Data() {
+ return level2Data;
+ }
+
+
+ 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);
+ }
+
+
+
+
+}
|