summaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorCGantert345 <57003061+CGantert345@users.noreply.github.com>2023-03-09 16:30:19 +0100
committerCGantert345 <57003061+CGantert345@users.noreply.github.com>2023-03-09 16:30:19 +0100
commit37ddd3d075d0664de44f05639712869596bfb9cd (patch)
tree5d22d5578e550a0b3435c78fe133545026dffabe /src/main/java
parenterror messages for wrong header content (diff)
downloadUIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.tar
UIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.tar.gz
UIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.tar.bz2
UIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.tar.lz
UIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.tar.xz
UIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.tar.zst
UIC-barcode-37ddd3d075d0664de44f05639712869596bfb9cd.zip
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/uic/barcode/Decoder.java151
-rw-r--r--src/main/java/org/uic/barcode/Encoder.java24
-rw-r--r--src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java32
3 files changed, 162 insertions, 45 deletions
diff --git a/src/main/java/org/uic/barcode/Decoder.java b/src/main/java/org/uic/barcode/Decoder.java
index 9f5ea82..1b3fb0c 100644
--- a/src/main/java/org/uic/barcode/Decoder.java
+++ b/src/main/java/org/uic/barcode/Decoder.java
@@ -15,6 +15,7 @@ import org.uic.barcode.dynamicFrame.api.IData;
import org.uic.barcode.dynamicFrame.api.IDynamicFrame;
import org.uic.barcode.dynamicFrame.api.ILevel1Data;
import org.uic.barcode.dynamicFrame.api.ILevel2Data;
+import org.uic.barcode.ssbFrame.SsbFrame;
import org.uic.barcode.staticFrame.StaticFrame;
import org.uic.barcode.staticFrame.UFLEXDataRecord;
import org.uic.barcode.staticFrame.UTLAYDataRecord;
@@ -39,6 +40,9 @@ public class Decoder {
/** The static frame. */
private StaticFrame staticFrame = null;
+ /** The ssb frame. */
+ private SsbFrame ssbFrame = null;
+
/** The uic ticket coder. */
private UicRailTicketCoder uicTicketCoder = null;
@@ -80,7 +84,7 @@ public class Decoder {
* @throws EncodingFormatException the encoding format exception
*/
public int validateLevel1(PublicKey key) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
- if (dynamicFrame != null) {
+ if (dynamicFrame != null && dynamicFrame != null) {
return dynamicFrame.validateLevel1(key) ;
} else {
if (staticFrame != null) {
@@ -106,15 +110,22 @@ public class Decoder {
* @throws EncodingFormatException the encoding format exception
*/
public int validateLevel1(PublicKey key, String signingAlg) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
- if (dynamicFrame != null) {
+ if (dynamicFrame != null ) {
return dynamicFrame.validateLevel1(key, signingAlg) ;
- } else {
+ } else if (staticFrame != null) {
if (staticFrame.verifyByAlgorithmOid(key,signingAlg)) {
return Constants.LEVEL1_VALIDATION_OK;
} else {
return Constants.LEVEL1_VALIDATION_FRAUD;
}
+ } else if (ssbFrame!= null) {
+ if (ssbFrame.verifyByAlgorithmOid(key,signingAlg, null)) {
+ return Constants.LEVEL1_VALIDATION_OK;
+ } else {
+ return Constants.LEVEL1_VALIDATION_FRAUD;
+ }
}
+ return Constants.LEVEL1_VALIDATION_NO_SIGNATURE;
}
/**
@@ -133,15 +144,25 @@ public class Decoder {
* @throws EncodingFormatException the encoding format exception
*/
public int validateLevel1(PublicKey key, String signingAlg, Provider provider) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
- if (!isStaticHeader(data)) {
+ if (!isStaticHeader(data) && dynamicFrame != null) {
return dynamicFrame.validateLevel1(key, provider) ;
- } else {
+ } else if (isSsbFrame(data) && ssbFrame != null) {
+
+ if (ssbFrame.verifyByAlgorithmOid(key,signingAlg, provider)) {
+ return Constants.LEVEL1_VALIDATION_OK;
+ } else {
+ return Constants.LEVEL1_VALIDATION_FRAUD;
+ }
+
+ } else if (staticFrame != null) {
+
if (staticFrame.verifyByAlgorithmOid(key,signingAlg, provider)) {
return Constants.LEVEL1_VALIDATION_OK;
} else {
return Constants.LEVEL1_VALIDATION_FRAUD;
}
}
+ return Constants.LEVEL1_VALIDATION_NO_SIGNATURE;
}
/**
@@ -151,7 +172,7 @@ public class Decoder {
* @throws EncodingFormatException
*/
public int validateLevel2() throws EncodingFormatException {
- if (!isStaticHeader(data)) {
+ if (!isStaticHeader(data) && dynamicFrame != null) {
return dynamicFrame.validateLevel2() ;
} else {
return Constants.LEVEL2_VALIDATION_NO_SIGNATURE;
@@ -164,7 +185,7 @@ public class Decoder {
* @return the return code indicating errors
*/
public int validateLevel2(Provider prov) throws EncodingFormatException {
- if (!isStaticHeader(data)) {
+ if (!isStaticHeader(data) && dynamicFrame != null) {
return dynamicFrame.validateLevel2(prov) ;
} else {
return Constants.LEVEL2_VALIDATION_NO_SIGNATURE;
@@ -183,45 +204,74 @@ public class Decoder {
public void decode(byte[] data) throws IOException, EncodingFormatException, DataFormatException {
if (!isStaticHeader(data)) {
-
- dynamicFrame = DynamicFrameCoder.decode(data);
-
- ILevel2Data level2 = dynamicFrame.getLevel2Data();
-
- ILevel1Data level1 = level2.getLevel1Data();
-
- for (IData level1Content : level1.getData()) {
-
- uicTicketCoder = new UicRailTicketCoder();
- if (level1Content.getFormat().equals("FCB1")) {
- uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getData(), 1);
- } else if (level1Content.getFormat().equals("FCB2")) {
- uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getData(), 2);
- } else if (level1Content.getFormat().equals("FCB3")) {
- uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getData(), 3);
+ try {
+ decodeDynamicFrame(data);
+ } catch (Exception e) {
+ dynamicFrame = null;
+ if (isSsbFrame(data)) {
+ decodeSsbFrame(data);
+ } else {
+ throw e;
}
}
-
+
} else if (isStaticHeader(data)){
-
- staticFrame = new StaticFrame();
-
- staticFrame.decode(data);
-
- UFLEXDataRecord flex = staticFrame.getuFlex();
-
- if (flex != null) {
- uicTicket = flex.getTicket();
+ try {
+ decodeStaticFrame(data);
+ } catch (Exception e) {
+ staticFrame = null;
+ throw e;
}
-
- UTLAYDataRecord tlay = staticFrame.getuTlay();
+ }
+ }
+
+ private void decodeDynamicFrame(byte[] data) throws EncodingFormatException, IOException {
+
+ dynamicFrame = DynamicFrameCoder.decode(data);
+
+ ILevel2Data level2 = dynamicFrame.getLevel2Data();
+
+ ILevel1Data level1 = level2.getLevel1Data();
+
+ for (IData level1Content : level1.getData()) {
- if (tlay != null) {
- layout = tlay.getLayout();
+ uicTicketCoder = new UicRailTicketCoder();
+ if (level1Content.getFormat().equals("FCB1")) {
+ uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getData(), 1);
+ } else if (level1Content.getFormat().equals("FCB2")) {
+ uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getData(), 2);
+ } else if (level1Content.getFormat().equals("FCB3")) {
+ uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getData(), 3);
}
}
}
+
+ private void decodeStaticFrame(byte[] data) throws EncodingFormatException, DataFormatException, IOException {
+
+ staticFrame = new StaticFrame();
+
+ staticFrame.decode(data);
+
+ UFLEXDataRecord flex = staticFrame.getuFlex();
+
+ if (flex != null) {
+ uicTicket = flex.getTicket();
+ }
+
+ UTLAYDataRecord tlay = staticFrame.getuTlay();
+
+ if (tlay != null) {
+ layout = tlay.getLayout();
+ }
+ }
+ private void decodeSsbFrame(byte[] data) throws EncodingFormatException, DataFormatException, IOException {
+
+ ssbFrame = new SsbFrame();
+
+ ssbFrame.decode(data);
+
+ }
/**
* Checks if is static header.
@@ -236,6 +286,19 @@ public class Decoder {
}
/**
+ * Checks if is ssb frame.
+ *
+ * @param data the data
+ * @return true, if is static header
+ */
+ private boolean isSsbFrame(byte[] data) {
+ if (data.length == 144) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
* Gets the uic ticket.
*
* @return the uic ticket
@@ -301,14 +364,14 @@ public class Decoder {
}
public IData getLevel2Data() {
- if (!isStaticHeader(data) && dynamicFrame.getLevel2Data() != null) {
+ if (!isStaticHeader(data) && dynamicFrame != null && dynamicFrame.getLevel2Data() != null) {
return dynamicFrame.getLevel2Data().getLevel2Data();
}
return null;
}
public byte[] getEncodedLevel1Data() throws IOException, EncodingFormatException {
- if (!isStaticHeader(data)) {
+ if (!isStaticHeader(data) && dynamicFrame != null) {
return dynamicFrame.getLevel1DataBin();
} else if (staticFrame != null) {
return staticFrame.getDataForSignature();
@@ -341,5 +404,15 @@ public class Decoder {
}
}
+
+ public SsbFrame getSsbFrame() {
+ return ssbFrame;
+ }
+
+ public void setSsbFrame(SsbFrame ssbFrame) {
+ this.ssbFrame = ssbFrame;
+ }
+
+
}
diff --git a/src/main/java/org/uic/barcode/Encoder.java b/src/main/java/org/uic/barcode/Encoder.java
index b01ca14..cf2d4d2 100644
--- a/src/main/java/org/uic/barcode/Encoder.java
+++ b/src/main/java/org/uic/barcode/Encoder.java
@@ -19,6 +19,7 @@ import org.uic.barcode.dynamicFrame.api.SimpleLevel1Data;
import org.uic.barcode.dynamicFrame.api.SimpleLevel2Data;
import org.uic.barcode.dynamicFrame.v1.DynamicFrameCoderV1;
import org.uic.barcode.dynamicFrame.v2.DynamicFrameCoderV2;
+import org.uic.barcode.ssbFrame.SsbFrame;
import org.uic.barcode.staticFrame.StaticFrame;
import org.uic.barcode.staticFrame.UFLEXDataRecord;
import org.uic.barcode.staticFrame.UHEADDataRecord;
@@ -45,6 +46,9 @@ public class Encoder {
/** The static frame. */
private StaticFrame staticFrame = null;
+ /** The ssb frame. */
+ private SsbFrame ssbFrame = null;
+
/** The UIC bar code type classic. */
public static String UIC_BARCODE_TYPE_CLASSIC = "UIC_CLASSIC";
@@ -52,6 +56,9 @@ public class Encoder {
/** The UIC bar code type DOSIPAS. */
public static String UIC_BARCODE_TYPE_DOSIPAS = "UIC_DOSIPAS";
+ /** The UIC bar code type SSB. */
+ public static String UIC_BARCODE_TYPE_SSB = "UIC_SSB";
+
/**
* Instantiates a new encoder.
*
@@ -119,6 +126,11 @@ public class Encoder {
dynamicFrame.getLevel2Data().getLevel1Data().addData(ticketData);
}
+
+ } else if (barcodeType == UIC_BARCODE_TYPE_SSB) {
+
+ ssbFrame = new SsbFrame();
+
}
}
@@ -369,6 +381,8 @@ public class Encoder {
staticFrame.getHeaderRecord().setIssuer(securityProvider);
}
staticFrame.signByAlgorithmOID(key,signingAlg);
+ } else if (ssbFrame != null) {
+ ssbFrame.signLevel1(key, null, keyId, signingAlg);
}
}
@@ -395,6 +409,8 @@ public class Encoder {
staticFrame.getHeaderRecord().setIssuer(securityProvider);
}
staticFrame.signByAlgorithmOID(key,signingAlg,prov);
+ } else if (ssbFrame != null) {
+ ssbFrame.signLevel1(key, prov, keyId, signingAlg);
}
}
@@ -461,6 +477,14 @@ public class Encoder {
}
}
+ public SsbFrame getSsbFrame() {
+ return ssbFrame;
+ }
+
+ public void setSsbFrame(SsbFrame ssbFrame) {
+ this.ssbFrame = ssbFrame;
+ }
+
diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java b/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java
index b496b17..81b5eb4 100644
--- a/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java
+++ b/src/main/java/org/uic/barcode/ssbFrame/SsbFrame.java
@@ -7,6 +7,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
+import java.security.Security;
import java.security.Signature;
import java.security.SignatureException;
import java.security.Provider.Service;
@@ -198,16 +199,18 @@ public class SsbFrame {
this.passData = passData;
}
- public void signLevel1(PrivateKey key, Provider prov, String algorithmOid) throws Exception {
+ public void signLevel1(PrivateKey key, Provider prov, String keyId, String algorithmOid) throws Exception {
+ this.header.setKeyId(Integer.parseInt(keyId));
+
byte[] data = getDataForSignature();
if (prov == null) {
//check for a provider supporting the key
prov = SecurityUtils.findPrivateKeyProvider(key);
}
-
+
//find the algorithm name for the signature OID
String algo = AlgorithmNameResolver.getSignatureAlgorithmName(algorithmOid, prov);
Signature sig = null;
@@ -261,10 +264,25 @@ public class SsbFrame {
public boolean verifyByAlgorithmOid(PublicKey key, String signingAlg, Provider prov) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
//find the algorithm name for the signature OID
String algo = null;
- Service service = prov.getService("Signature",signingAlg);
- if (service != null) {
- algo = service.getAlgorithm();
- }
+
+ if (prov != null) {
+ Service service = prov.getService("Signature",signingAlg);
+ if (service != null) {
+ algo = service.getAlgorithm();
+ }
+ } else {
+ Provider[] provs = Security.getProviders();
+ for (Provider p : provs) {
+ if (algo == null) {
+ Service service = p.getService("Signature",signingAlg);
+ if (service != null) {
+ algo = service.getAlgorithm();
+ }
+ }
+ }
+
+ }
+
if (algo == null) {
throw new NoSuchAlgorithmException("No service for algorithm found: " + signingAlg);
}
@@ -279,4 +297,6 @@ public class SsbFrame {
return sig.verify(signature);
}
+
+
}