From 3439ff611093d54368826b59515659f04c5788e3 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:20:23 +0100 Subject: bug fixes on dynamic frame - ExtensionMarker removed - default on format removed - keyId starting from 0 --- src/main/java/org/uic/barcode/dynamicFrame/DataType.java | 2 -- src/main/java/org/uic/barcode/dynamicFrame/DynamicFrame.java | 6 +++--- src/main/java/org/uic/barcode/dynamicFrame/Level1DataType.java | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main/java/org/uic/barcode/dynamicFrame/DataType.java b/src/main/java/org/uic/barcode/dynamicFrame/DataType.java index 2d6d984..2ea63ca 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/DataType.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/DataType.java @@ -1,6 +1,5 @@ package org.uic.barcode.dynamicFrame; -import org.uic.barcode.asn1.datatypes.Asn1Default; import org.uic.barcode.asn1.datatypes.CharacterRestriction; import org.uic.barcode.asn1.datatypes.RestrictedString; import org.uic.barcode.asn1.datatypes.Sequence; @@ -20,7 +19,6 @@ public class DataType { * -- FCB2 FCB version 2 * -- RICS company code + ... **/ - @Asn1Default("FCB1") @RestrictedString(CharacterRestriction.IA5String) public String format; diff --git a/src/main/java/org/uic/barcode/dynamicFrame/DynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/DynamicFrame.java index 6cc1eaa..c74215d 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/DynamicFrame.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/DynamicFrame.java @@ -14,7 +14,6 @@ import java.security.spec.X509EncodedKeySpec; import org.uic.barcode.asn1.datatypes.Asn1Optional; import org.uic.barcode.asn1.datatypes.CharacterRestriction; import org.uic.barcode.asn1.datatypes.FieldOrder; -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; @@ -29,7 +28,6 @@ import org.uic.barcode.utils.AlgorithmNameResolver; * Implementation of the Draft under discussion, not final. */ @Sequence -@HasExtensionMarker public class DynamicFrame extends Object{ public DynamicFrame() {} @@ -152,7 +150,9 @@ public class DynamicFrame extends Object{ byte[] keyBytes = this.getLevel2SignedData().getLevel1Data().level2publicKey.toByteArray(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); key = KeyFactory.getInstance(keyAlgName).generatePublic(keySpec); - } catch (InvalidKeySpecException | NoSuchAlgorithmException e1) { + } catch (InvalidKeySpecException e1) { + return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED; + } catch (NoSuchAlgorithmException e1) { return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED; } diff --git a/src/main/java/org/uic/barcode/dynamicFrame/Level1DataType.java b/src/main/java/org/uic/barcode/dynamicFrame/Level1DataType.java index aac0188..958cafc 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/Level1DataType.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/Level1DataType.java @@ -34,7 +34,7 @@ public class Level1DataType { /** The key id. */ @FieldOrder(order = 2) - @IntRange(minValue=1,maxValue=99999) + @IntRange(minValue=0,maxValue=99999) @Asn1Optional public Long keyId; -- cgit v1.2.3 From ecdc5505218d71ba365ff729f11a1b504a71c986 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 09:59:31 +0100 Subject: added fcb version 3 data type to the DOSIPAS encoding and decoding --- src/main/java/org/uic/barcode/Decoder.java | 2 + src/main/java/org/uic/barcode/Encoder.java | 8 +- .../org/uic/barcode/dynamicFrame/Constants.java | 1 + .../ticket/api/spec/uicBarcodeHeader0.1.asn | 151 --------------------- 4 files changed, 10 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/org/uic/barcode/ticket/api/spec/uicBarcodeHeader0.1.asn (limited to 'src') diff --git a/src/main/java/org/uic/barcode/Decoder.java b/src/main/java/org/uic/barcode/Decoder.java index 09ee839..9f102d3 100644 --- a/src/main/java/org/uic/barcode/Decoder.java +++ b/src/main/java/org/uic/barcode/Decoder.java @@ -172,6 +172,8 @@ public class Decoder { uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getByteData(), 1); } else if (level1Content.getFormat().equals("FCB2")) { uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getByteData(), 2); + } else if (level1Content.getFormat().equals("FCB3")) { + uicTicket = uicTicketCoder.decodeFromAsn(level1Content.getByteData(), 3); } } diff --git a/src/main/java/org/uic/barcode/Encoder.java b/src/main/java/org/uic/barcode/Encoder.java index e906881..9463548 100644 --- a/src/main/java/org/uic/barcode/Encoder.java +++ b/src/main/java/org/uic/barcode/Encoder.java @@ -100,7 +100,13 @@ public class Encoder { DataType ticketData = new DataType(); UicRailTicketCoder uicTicketCoder = new UicRailTicketCoder(); - ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_1); + if (fcbVersion == 1) { + ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_1); + } else if (fcbVersion == 2) { + ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_2); + } else if (fcbVersion == 3) { + ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_3); + } ticketData.setData(new OctetString(uicTicketCoder.encode(ticket, fcbVersion))); dynamicFrame.getLevel2SignedData().getLevel1Data().getData().add(ticketData); diff --git a/src/main/java/org/uic/barcode/dynamicFrame/Constants.java b/src/main/java/org/uic/barcode/dynamicFrame/Constants.java index 774475a..8f47986 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/Constants.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/Constants.java @@ -16,6 +16,7 @@ public class Constants { public static String DATA_TYPE_FCB_VERSION_1 = "FCB1"; public static String DATA_TYPE_FCB_VERSION_2 = "FCB2"; + public static String DATA_TYPE_FCB_VERSION_3 = "FCB3"; public static String DYNAMIC_BARCODE_FORMAT_DEFAULT = "U1"; diff --git a/src/main/java/org/uic/barcode/ticket/api/spec/uicBarcodeHeader0.1.asn b/src/main/java/org/uic/barcode/ticket/api/spec/uicBarcodeHeader0.1.asn deleted file mode 100644 index 2999d18..0000000 --- a/src/main/java/org/uic/barcode/ticket/api/spec/uicBarcodeHeader0.1.asn +++ /dev/null @@ -1,151 +0,0 @@ --- Creator: ASN.1 Editor (http://asneditor.sourceforge.net) --- Author: ClemensGantert --- Created: Tue Aug 11 11:40:28 CEST 2015 -ASN-Module DEFINITIONS AUTOMATIC TAGS ::= BEGIN - --- imports and exports --- EXPORTS ALL; - - --- ############################################################################################## --- # --- # UIC barcode header - first draft --- # --- ############################################################################################## - - --- ############################################################################################## --- # --- # Naming and encoding conventions --- # --- # Elements included as String and as Numeric values: --- # Some elements are included in different formats to reduce the data size. --- # These elements must be included only once. --- # These elements are named with the same name and appendix --- # Num (numeric values) --- # IA5 (String values according to ASN IA5String (7Bit)) --- # --- # RICS codes must be used to encode companies (issuer, product owner, ...) where available --- # other codes are possible based on bilateral agreements --- # the format is kept more flexible to cover upcoming extensions of the RICS code by ERA --- # --- # Stations can be coded using the UIC and upcoming ERA code lists. Proprietary codes are --- # possible based on bilateral agreements. Format: 1..9999999 or alphanumeric without --- # special character (IA5String) --- # --- # --- # ! INTEGERS must not exceed the value of 9,223,372,036,854,775,807 (64 bit) even in case --- # ! they are unrestricted!!! --- # ! --- # ! Some elements like ReferenceNum or cardIdNum are defined as an unrestricted integer. --- # ! Unlike other numerical values the cardIdNum and referenceNum can be larger than a usual 32 bit Integer --- # ! Some ASN.1 implementation tools are limited to 32 bit integers which is too small. --- # ! Please ensure to use a tool capable of dealing with larger numbers. --- # --- # BOOLEAN is always non optional --- # --- # Encoding of time: --- # time is encoded as the number of minutes of the day 0 = 00:00, 1440 = 24:00, --- # time data elements end with "time" in their name --- # --- # Encoding of date: --- # ......................................................................................................... --- # The issuing date is given in UTC, but some other date values are given in local time where the exact time zone is not known. --- # --- # --- # --- # ASN.1 Extensions: --- # --- # The specification makes use of extension (",..."). --- # These extesions might be defined in future versions of the UIC specification --- # Implementations must support the extension feature of ASN.1, at least they must be able to ignore extensions while decoding the data --- # ASN.1 extensions will be defined by UIC. It is not allowed to define bilateral extensions. --- # --- # Bilateral Extensions: --- # Bilateral extensions can be included in the data element "ExtensionData". --- # --- # --- # --- ######################################################################################### - - --- ############################################################################################ - - --- type assignments - - -- ######################################################################################### - -- the basic entry point of the data structure - -- the data include: - -- -issuer informations - -- -the details of the transport document - -- -informations required for the control process - -- -informations on the travelers independent from the transport document - -- -proprietary extensions - -- - -- ########################################################################################## - UicBarcodeHeader ::= SEQUENCE { - -- format type - format IA5String, - -- "UIC" = UIC ticket - - version Integer (1..16), - - -- provider of the signature (RICS code) - securityProviderNum INTEGER (1..32000) OPTIONAL, - securityProviderIA5 IA5String OPTIONAL, - - - staticData SEQUENCE OF DataType, - staticSignature SignatureType OPTIONAL, - - - - -- additional dynamic data i.e. phone number, IMEI, timestamp , .... --> To be defined separately - dynamicDataFormat IA5String OPTIONAL, - dynamicData OCTET STRING OPTIONAL, - dynamicPublicKey OCTET STRING OPTIONAL, - dynamicSignature SignatureType OPTIONAL - - -- proprietary data defined bilaterally - extension SEQUENCE OF ExtensionData OPTIONAL - ,... - - } - - DataType ::= SEQUENCE { - staticDataFormat IA5String DEFAULT "FCB1", - -- FCB1 FCB version 1 - -- 1080XYZ - staticData OCTET STRING - } - - - - SignatureType ::= SEQUENCE { - signingAlg IA5String, - keyId IA5String (SIZE(1..5)), - signature OCTET STRING, - } - - - - -- ########################################################################################### - -- generic non standard extension element - -- the generic non - standard element contains: - -- - an extension id to distinguish different extensions - -- - the extension data as binary data - -- proprietary extensions are by definition proprietary. This standard provides - -- the means to identify these extensions - -- within the data and to skip these data. - -- the evaluation of these data and the unique identification of the extensions - -- via the extension id is in the - -- responsibility of the railways which use these extensions. - -- ########################################################################################### - ExtensionData ::= SEQUENCE { - extensionId IA5String, - extensionData OCTET STRING - } - - -END \ No newline at end of file -- cgit v1.2.3 From cd76118fb6737eb241cab60207262617e12d237d Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:16:40 +0100 Subject: test added --- src/main/java/org/uic/barcode/Encoder.java | 2 +- .../org/uic/barcode/test/DynamicFrameFCB3Test.java | 175 +++++++++++++++++++++ 2 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java (limited to 'src') diff --git a/src/main/java/org/uic/barcode/Encoder.java b/src/main/java/org/uic/barcode/Encoder.java index 9463548..f0f8d14 100644 --- a/src/main/java/org/uic/barcode/Encoder.java +++ b/src/main/java/org/uic/barcode/Encoder.java @@ -100,7 +100,7 @@ public class Encoder { DataType ticketData = new DataType(); UicRailTicketCoder uicTicketCoder = new UicRailTicketCoder(); - if (fcbVersion == 1) { + if (fcbVersion == 1 || fcbVersion == 13) { ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_1); } else if (fcbVersion == 2) { ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_2); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java new file mode 100644 index 0000000..2ac7353 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java @@ -0,0 +1,175 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFCB3Test { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPair = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateECKeys(Constants.KG_EC, elipticCurve); + //keyPair = generateECDSAKeys("ECDSA", "B-571"); + } catch (Exception e) { + assert(false); + } + + assert(keyPair != null); + + } + + + @Test public void testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + + } + + @Test public void testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); + + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + + + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 47133b358a65723b1b131f66d2e340a12685f9e6 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:23:55 +0100 Subject: renamed --- .../org/uic/barcode/test/DynamicFrameFCB3Test.java | 175 --------------------- .../org/uic/barcode/test/DynamicFrameFcb3Test.java | 175 +++++++++++++++++++++ 2 files changed, 175 insertions(+), 175 deletions(-) delete mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java (limited to 'src') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java deleted file mode 100644 index 2ac7353..0000000 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFCB3Test.java +++ /dev/null @@ -1,175 +0,0 @@ -package org.uic.barcode.test; - -import java.io.IOException; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.SecureRandom; -import java.security.Security; -import java.security.SignatureException; -import java.util.zip.DataFormatException; - -import org.bouncycastle.jce.ECNamedCurveTable; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.jce.spec.ECParameterSpec; -import org.junit.Before; -import org.junit.Test; -import org.uic.barcode.Decoder; -import org.uic.barcode.Encoder; -import org.uic.barcode.dynamicFrame.Constants; -import org.uic.barcode.test.utils.SimpleUICTestTicket; -import org.uic.barcode.ticket.EncodingFormatException; -import org.uic.barcode.ticket.api.spec.IUicRailTicket; - -public class DynamicFrameFCB3Test { - - public String signatureAlgorithmOID = null; - public String elipticCurve = null; - public String keyPairAlgorithmOID = null; - - public KeyPair keyPair = null; - - public IUicRailTicket testFCBticket = null; - - - @Before public void initialize() { - - signatureAlgorithmOID = Constants.ECDSA_SHA256; - keyPairAlgorithmOID = Constants.KG_EC_256; - elipticCurve = "secp256k1"; - - testFCBticket = SimpleUICTestTicket.getUicTestTicket(); - - Security.addProvider(new BouncyCastleProvider()); - - try { - keyPair = generateECKeys(Constants.KG_EC, elipticCurve); - //keyPair = generateECDSAKeys("ECDSA", "B-571"); - } catch (Exception e) { - assert(false); - } - - assert(keyPair != null); - - } - - - @Test public void testDynamicHeaderBarcodeEncodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } catch (IOException | EncodingFormatException e1) { - assert(false); - } - - assert(enc != null); - - try { - enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); - } catch (Exception e) { - assert(false); - } - - - byte[] encoded = null; - try { - encoded = enc.encode(); - } catch (Exception e) { - assert(false); - } - - assert(encoded != null); - - - - } - - @Test public void testDynamicHeaderBarcodeDecodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } catch (IOException | EncodingFormatException e1) { - assert(false); - } - - assert(enc != null); - - try { - enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); - } catch (Exception e) { - assert(false); - } - - - byte[] encoded = null; - try { - encoded = enc.encode(); - } catch (Exception e) { - assert(false); - } - - assert(encoded != null); - - Decoder dec = null; - try { - dec = new Decoder(encoded); - } catch (IOException e) { - assert(false); - } catch (EncodingFormatException e) { - assert(false); - } catch (DataFormatException e) { - assert(false); - } - assert(dec != null); - - int signatureCheck = 0; - try { - signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); - } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException - | UnsupportedOperationException | IOException | EncodingFormatException e) { - assert(false); - } - - assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - - assert(dec.getDynamicHeader().getFormat().equals("U1")); - - assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); - - - SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); - - - - - } - - public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ - - String keyAlgorithmName = "ECDSA"; - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - -} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java new file mode 100644 index 0000000..cb252a2 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java @@ -0,0 +1,175 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcb3Test { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPair = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateECKeys(Constants.KG_EC, elipticCurve); + //keyPair = generateECDSAKeys("ECDSA", "B-571"); + } catch (Exception e) { + assert(false); + } + + assert(keyPair != null); + + } + + + @Test public void testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + + } + + @Test public void testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); + + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + + + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 900acb165c53e9801112c3f1b8f57ff4d9ad531d Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 13:28:56 +0100 Subject: additional tests for DOSIPAS --- .../org/uic/barcode/ticket/UicRailTicketCoder.java | 2 +- .../test/DynamicFrameDynamicContentTest.java | 208 +++++++++++++++++++++ .../org/uic/barcode/test/DynamicFrameFcb3Test.java | 175 ----------------- .../barcode/test/DynamicFrameFcbVersion1Test.java | 174 +++++++++++++++++ .../barcode/test/DynamicFrameFcbVersion3Test.java | 174 +++++++++++++++++ 5 files changed, 557 insertions(+), 176 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java delete mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java (limited to 'src') diff --git a/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java b/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java index e9d2eb4..0586ed4 100644 --- a/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java +++ b/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java @@ -33,7 +33,7 @@ public class UicRailTicketCoder { public byte[] encode (IUicRailTicket uicRailTicket, int version) throws IOException, EncodingFormatException{ - if (version == 13) { + if (version == 13 || version == 1) { Api2OpenAsnEncoder uicEncoder = new Api2OpenAsnEncoder(); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java new file mode 100644 index 0000000..93ecdfb --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -0,0 +1,208 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameDynamicContentTest { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPairLevel1 = null; + public KeyPair keyPairLevel2 = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + } catch (Exception e) { + assert(false); + } + + assert(keyPairLevel1 != null); + + assert(keyPairLevel2 != null); + + } + + + @Test public void testDynamicContentEncoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID, keyPairLevel2.getPublic()); + + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + try { + UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); + dcd.setChallengeString("CHALLENGE"); + dcd.setAppId("MyApp"); + enc.setDynamicContentDataUIC1(dcd); + enc.signLevel2(keyPairLevel2.getPrivate()); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + + } + + @Test public void testDynamicContentDecoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID, keyPairLevel2.getPublic()); + + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + try { + UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); + dcd.setChallengeString("CHALLENGE"); + dcd.setAppId("MyApp"); + enc.setDynamicContentDataUIC1(dcd); + enc.signLevel2(keyPairLevel2.getPrivate()); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + int level2check = 0; + try { + level2check = dec.validateLevel2(); + } catch (Exception e) { + assert(false); + } + + assert(level2check == Constants.LEVEL2_VALIDATION_OK); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getChallengeString().equals("CHALLENGE")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getAppId().equals("MyApp")); + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java deleted file mode 100644 index cb252a2..0000000 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java +++ /dev/null @@ -1,175 +0,0 @@ -package org.uic.barcode.test; - -import java.io.IOException; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.SecureRandom; -import java.security.Security; -import java.security.SignatureException; -import java.util.zip.DataFormatException; - -import org.bouncycastle.jce.ECNamedCurveTable; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.jce.spec.ECParameterSpec; -import org.junit.Before; -import org.junit.Test; -import org.uic.barcode.Decoder; -import org.uic.barcode.Encoder; -import org.uic.barcode.dynamicFrame.Constants; -import org.uic.barcode.test.utils.SimpleUICTestTicket; -import org.uic.barcode.ticket.EncodingFormatException; -import org.uic.barcode.ticket.api.spec.IUicRailTicket; - -public class DynamicFrameFcb3Test { - - public String signatureAlgorithmOID = null; - public String elipticCurve = null; - public String keyPairAlgorithmOID = null; - - public KeyPair keyPair = null; - - public IUicRailTicket testFCBticket = null; - - - @Before public void initialize() { - - signatureAlgorithmOID = Constants.ECDSA_SHA256; - keyPairAlgorithmOID = Constants.KG_EC_256; - elipticCurve = "secp256k1"; - - testFCBticket = SimpleUICTestTicket.getUicTestTicket(); - - Security.addProvider(new BouncyCastleProvider()); - - try { - keyPair = generateECKeys(Constants.KG_EC, elipticCurve); - //keyPair = generateECDSAKeys("ECDSA", "B-571"); - } catch (Exception e) { - assert(false); - } - - assert(keyPair != null); - - } - - - @Test public void testDynamicHeaderBarcodeEncodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } catch (IOException | EncodingFormatException e1) { - assert(false); - } - - assert(enc != null); - - try { - enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); - } catch (Exception e) { - assert(false); - } - - - byte[] encoded = null; - try { - encoded = enc.encode(); - } catch (Exception e) { - assert(false); - } - - assert(encoded != null); - - - - } - - @Test public void testDynamicHeaderBarcodeDecodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } catch (IOException | EncodingFormatException e1) { - assert(false); - } - - assert(enc != null); - - try { - enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); - } catch (Exception e) { - assert(false); - } - - - byte[] encoded = null; - try { - encoded = enc.encode(); - } catch (Exception e) { - assert(false); - } - - assert(encoded != null); - - Decoder dec = null; - try { - dec = new Decoder(encoded); - } catch (IOException e) { - assert(false); - } catch (EncodingFormatException e) { - assert(false); - } catch (DataFormatException e) { - assert(false); - } - assert(dec != null); - - int signatureCheck = 0; - try { - signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); - } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException - | UnsupportedOperationException | IOException | EncodingFormatException e) { - assert(false); - } - - assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - - assert(dec.getDynamicHeader().getFormat().equals("U1")); - - assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); - - - SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); - - - - - } - - public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ - - String keyAlgorithmName = "ECDSA"; - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - -} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java new file mode 100644 index 0000000..63ba68e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java @@ -0,0 +1,174 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcbVersion1Test { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPair = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateECKeys(Constants.KG_EC, elipticCurve); + //keyPair = generateECDSAKeys("ECDSA", "B-571"); + } catch (Exception e) { + assert(false); + } + + assert(keyPair != null); + + } + + + @Test public void testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + } + + @Test public void testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB1")); + + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + + + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java new file mode 100644 index 0000000..4f0897e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java @@ -0,0 +1,174 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcbVersion3Test { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPair = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateECKeys(Constants.KG_EC, elipticCurve); + //keyPair = generateECDSAKeys("ECDSA", "B-571"); + } catch (Exception e) { + assert(false); + } + + assert(keyPair != null); + + } + + + @Test public void testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + } + + @Test public void testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB3")); + + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + + + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 12ca8d77e0a0d63ce03886d38c079c686122253a Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 15:39:05 +0100 Subject: dynamic content time stamp fixed and test added --- .../uic/barcode/dynamicContent/fdc1/TimeStamp.java | 63 ++++++++++------------ .../fdc1/UicDynamicContentDataFDC1.java | 4 +- .../test/DynamicFrameDynamicContentTest.java | 38 ++++++++++++- 3 files changed, 67 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java b/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java index 32cce65..ecbb226 100644 --- a/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java +++ b/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java @@ -1,16 +1,17 @@ package org.uic.barcode.dynamicContent.fdc1; import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.time.temporal.ChronoField; -import java.util.Calendar; import java.util.Date; -import java.util.TimeZone; import org.uic.barcode.asn1.datatypes.FieldOrder; import org.uic.barcode.asn1.datatypes.IntRange; import org.uic.barcode.asn1.datatypes.Sequence; -// TODO: Auto-generated Javadoc + /** * The Class TimeStamp. */ @@ -20,16 +21,10 @@ public class TimeStamp { /* -- Moment of generation of the dynamic content, expressed in UTC : - -- * dynamicContentDay is the number of days from issuing date - -- (UicRailTicketData.issuingDetail.issuingYear and issuingDay) - -- The range 0..1070 allows a validity equal to that of the validFrom (700) plus - -- validUntil (370) elements of the different transport documents of UicRailTicketData. + -- * dynamicContentDay is the number of day in the year -- * dynamicContentTime is the number of seconds of the day -- (from 0 = 0:00:00 to 86399 = 23:59:59) - -- These two elements shall be either both present, either both absent - dynamicContentDay INTEGER (0..366), - * - */ + */ @FieldOrder(order = 0) @IntRange(minValue=1, maxValue=366) public Long day; @@ -46,7 +41,7 @@ public class TimeStamp { * Instantiates a new time stamp and sets the time-stamp to now. */ public TimeStamp() { - Instant now = Instant.now(); + ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); day = new Long(now.get(ChronoField.DAY_OF_YEAR)); secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY)); } @@ -55,7 +50,7 @@ public class TimeStamp { * Sets the the time-stamp to now. */ public void setNow() { - Instant now = Instant.now(); + ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); day = new Long(now.get(ChronoField.DAY_OF_YEAR)); secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY)); } @@ -102,25 +97,26 @@ public class TimeStamp { * @return the date and time of content creation in UTC */ public Date getTimeAsDate() { - - Calendar cal = Calendar.getInstance(); - int dayOfYear = cal.get(Calendar.DAY_OF_YEAR); - + + ZonedDateTime now = Instant.now().atZone(ZoneOffset.UTC); + int dayOfYear = now.getDayOfYear(); + if (dayOfYear - day.intValue() > 250) { - cal.add(Calendar.YEAR, 1); + now = now.plusDays(1); } if (day.intValue() - dayOfYear > 250) { - cal.add(Calendar.YEAR, -1); + now = now.minusDays(1); } - - cal.setTimeZone(TimeZone.getTimeZone("UTC")); - cal.set(Calendar.SECOND,0); - cal.set(Calendar.HOUR,0); - cal.set(Calendar.MINUTE,0); - cal.set(Calendar.DAY_OF_YEAR, day.intValue()); - cal.add(Calendar.SECOND, secondOfDay.intValue()); + + now = now.withDayOfYear(1); + now = now.withSecond(0); + now = now.withHour(0); + now = now.withMinute(0); + now = now.withDayOfYear(dayOfYear); + now = now.plusSeconds(secondOfDay); - return cal.getTime(); + return Date.from(now.toInstant()); + } /** @@ -129,15 +125,14 @@ public class TimeStamp { * @param dateUTC the current date and time in UTC */ public void setDateTime(Date dateUTC) { - - Calendar cal = Calendar.getInstance(); - cal.setTime(dateUTC); - day = Long.valueOf(cal.get(Calendar.DAY_OF_YEAR)); + ZonedDateTime date = dateUTC.toInstant().atZone(ZoneOffset.UTC); + + day = (long) date.getDayOfYear(); - secondOfDay = (long) cal.get(Calendar.SECOND); - secondOfDay = secondOfDay + 60 * (long) cal.get(Calendar.MINUTE); - secondOfDay = secondOfDay + 60 * 60 * (long) cal.get(Calendar.HOUR_OF_DAY); + secondOfDay = (long) date.getSecond(); + secondOfDay = secondOfDay + 60 * (long) date.getMinute(); + secondOfDay = secondOfDay + 60 * 60 * (long) date.getHour(); } diff --git a/src/main/java/org/uic/barcode/dynamicContent/fdc1/UicDynamicContentDataFDC1.java b/src/main/java/org/uic/barcode/dynamicContent/fdc1/UicDynamicContentDataFDC1.java index ae352d1..c658448 100644 --- a/src/main/java/org/uic/barcode/dynamicContent/fdc1/UicDynamicContentDataFDC1.java +++ b/src/main/java/org/uic/barcode/dynamicContent/fdc1/UicDynamicContentDataFDC1.java @@ -161,13 +161,13 @@ public class UicDynamicContentDataFDC1 { return null; } - public void setPassIdHash(byte[] phoneIdHash) { + public void setPassIdHash(byte[] passIdHash) { if (extensions == null) { extensions = new SequenceOfExtension(); }; ExtensionData ed = new ExtensionData(); ed.setExtensionId("pass"); - ed.setExtensionData(phoneIdHash); + ed.setExtensionData(passIdHash); extensions.add(ed); } diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index 93ecdfb..53b6af8 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -10,6 +10,11 @@ import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; +import java.util.Arrays; +import java.util.Date; import java.util.zip.DataFormatException; import org.bouncycastle.jce.ECNamedCurveTable; @@ -19,6 +24,7 @@ import org.junit.Before; import org.junit.Test; import org.uic.barcode.Decoder; import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicContent.fdc1.TimeStamp; import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; import org.uic.barcode.dynamicFrame.Constants; import org.uic.barcode.test.utils.SimpleUICTestTicket; @@ -34,6 +40,11 @@ public class DynamicFrameDynamicContentTest { public KeyPair keyPairLevel1 = null; public KeyPair keyPairLevel2 = null; + public byte[] passIdHash = "PassId".getBytes(); + public byte[] phoneIdHash = "myPhone".getBytes(); + + ZonedDateTime originalTimeStamp = ZonedDateTime.now(ZoneId.of("UTC")); + public IUicRailTicket testFCBticket = null; @@ -87,6 +98,12 @@ public class DynamicFrameDynamicContentTest { UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); dcd.setChallengeString("CHALLENGE"); dcd.setAppId("MyApp"); + dcd.setPhoneIdHash(phoneIdHash); + dcd.setPassIdHash(passIdHash); + TimeStamp ts = new TimeStamp(); + ts.setDateTime(Date.from(originalTimeStamp.toInstant())); + dcd.setTimeStamp(ts); + enc.setDynamicContentDataUIC1(dcd); enc.signLevel2(keyPairLevel2.getPrivate()); } catch (Exception e) { @@ -133,6 +150,12 @@ public class DynamicFrameDynamicContentTest { UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); dcd.setChallengeString("CHALLENGE"); dcd.setAppId("MyApp"); + dcd.setPhoneIdHash(phoneIdHash); + dcd.setPassIdHash(passIdHash); + TimeStamp ts = new TimeStamp(); + ts.setDateTime(Date.from(originalTimeStamp.toInstant())); + dcd.setTimeStamp(ts); + enc.setDynamicContentDataUIC1(dcd); enc.signLevel2(keyPairLevel2.getPrivate()); } catch (Exception e) { @@ -182,10 +205,21 @@ public class DynamicFrameDynamicContentTest { assert(level2check == Constants.LEVEL2_VALIDATION_OK); - assert(dec.getDynamicHeader().getDynamicDataFDC1().getChallengeString().equals("CHALLENGE")); + UicDynamicContentDataFDC1 dynamicData = dec.getDynamicHeader().getDynamicDataFDC1(); + + assert(dynamicData.getChallengeString().equals("CHALLENGE")); + + assert(dynamicData.getAppId().equals("MyApp")); + + assert(Arrays.equals(dynamicData.getPassIdHash(),passIdHash)); - assert(dec.getDynamicHeader().getDynamicDataFDC1().getAppId().equals("MyApp")); + assert(Arrays.equals(dynamicData.getPhoneIdHash(),phoneIdHash)); + Date timeStamp = dynamicData.getTimeStamp().getTimeAsDate(); + ZonedDateTime retrievedTimeStamp = timeStamp.toInstant().atZone(ZoneId.of("UTC")); + long diff = ChronoUnit.SECONDS.between(originalTimeStamp, retrievedTimeStamp); + + assert(diff == 0); } public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ -- cgit v1.2.3 From b51f825080e3535b259c673af7053ad43fc1e674 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 16:44:15 +0100 Subject: unused code removed test on security with designated provider --- .../org/uic/barcode/asn1/uper/UperEncoder.java | 11 -- ...cFrameDoubleSignatureProviderSelectionTest.java | 169 +++++++++++++++++++++ 2 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java (limited to 'src') diff --git a/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java b/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java index a5ef5c5..5956054 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java +++ b/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java @@ -691,17 +691,6 @@ public final class UperEncoder { return sb.toString(); } - public static byte[] bytesFromBinaryString(String s) { - int len = s.length(); - byte[] result = new byte[(len + Byte.SIZE - 1) / Byte.SIZE]; - char c; - for (int i = 0; i < len; i++) - if ((c = s.charAt(i)) == '1') result[i / Byte.SIZE] = (byte) (result[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE))); - else if (c != '0') - throw new IllegalArgumentException(); - return result; - } - private static BitBuffer bitBufferFromBinaryString(String s) { ByteBitBuffer result = ByteBitBuffer.allocate(s.length()); for (int i = 0; i < s.length(); i++) { diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java new file mode 100644 index 0000000..84f7795 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java @@ -0,0 +1,169 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.Arrays; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.dynamicFrame.DataType; +import org.uic.barcode.test.utils.Level2TestDataFactory; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameDoubleSignatureProviderSelectionTest { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPairLevel1 = null; + public KeyPair keyPairLevel2 = null; + + public IUicRailTicket testFCBticket = null; + + public Provider prov = null; + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + prov = new BouncyCastleProvider(); + + Security.addProvider(prov); + + try { + keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + } catch (Exception e) { + assert(false); + } + + assert(keyPairLevel1 != null); + + assert(keyPairLevel2 != null); + + } + + + + @Test public void testDynamicHeaderBarcodeDecoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID); + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic()); + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1",prov); + } catch (Exception e) { + assert(false); + } + + assert(enc != null); + + + DataType level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); + try { + enc.setLevel2Data(level2Data); + enc.signLevel2(keyPairLevel2.getPrivate(),prov); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(), null, prov); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + signatureCheck = 0; + try { + signatureCheck = dec.validateLevel2(prov); + } catch (IllegalArgumentException | UnsupportedOperationException e) { + assert(false); + } + assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK); + + DataType level2DataDec = dec.getLevel2Data(); + + assert(level2Data.getFormat().equals(level2DataDec.getFormat())); + assert(Arrays.equals(level2Data.getData().toByteArray(),level2DataDec.getData().toByteArray())); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3 From 2949fbd789fc3c35d340d17cb96e26a7a2ef7d02 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Wed, 24 Nov 2021 11:54:10 +0100 Subject: extended tests --- .../test/DynamicFrameDynamicContentTest.java | 16 ++ .../barcode/test/StaticFrameBarcodeTestFCB3.java | 193 +++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java (limited to 'src') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index 53b6af8..f3e4f54 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -24,6 +24,8 @@ import org.junit.Before; import org.junit.Test; import org.uic.barcode.Decoder; import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateSystemType; +import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateType; import org.uic.barcode.dynamicContent.fdc1.TimeStamp; import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; import org.uic.barcode.dynamicFrame.Constants; @@ -104,6 +106,11 @@ public class DynamicFrameDynamicContentTest { ts.setDateTime(Date.from(originalTimeStamp.toInstant())); dcd.setTimeStamp(ts); + GeoCoordinateType geo = new GeoCoordinateType(); + geo.setLatitude(123456L); + geo.setLongitude(823456L); + dcd.setGeoCoordinate(geo); + enc.setDynamicContentDataUIC1(dcd); enc.signLevel2(keyPairLevel2.getPrivate()); } catch (Exception e) { @@ -152,6 +159,12 @@ public class DynamicFrameDynamicContentTest { dcd.setAppId("MyApp"); dcd.setPhoneIdHash(phoneIdHash); dcd.setPassIdHash(passIdHash); + + GeoCoordinateType geo = new GeoCoordinateType(); + geo.setLatitude(123456L); + geo.setLongitude(823456L); + dcd.setGeoCoordinate(geo); + TimeStamp ts = new TimeStamp(); ts.setDateTime(Date.from(originalTimeStamp.toInstant())); dcd.setTimeStamp(ts); @@ -215,6 +228,9 @@ public class DynamicFrameDynamicContentTest { assert(Arrays.equals(dynamicData.getPhoneIdHash(),phoneIdHash)); + assert(dynamicData.getGeoCoordinate().getLatitude() == 123456L); + assert(dynamicData.getGeoCoordinate().getLongitude() == 823456L); + Date timeStamp = dynamicData.getTimeStamp().getTimeAsDate(); ZonedDateTime retrievedTimeStamp = timeStamp.toInstant().atZone(ZoneId.of("UTC")); long diff = ChronoUnit.SECONDS.between(originalTimeStamp, retrievedTimeStamp); diff --git a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java new file mode 100644 index 0000000..41ce365 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java @@ -0,0 +1,193 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.staticFrame.ticketLayoutBarcode.TicketLayout; +import org.uic.barcode.test.utils.SimpleTestTicketLayout; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +/** + * The Class StaticFrameBarcodeTest. + */ +public class StaticFrameBarcodeTestFCB3 { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA224; + + public int keySize = 2048; + + /** The key pair. */ + public KeyPair keyPair = null; + + + public IUicRailTicket testFCBticket = null; + + public TicketLayout testLayout = null; + + + /** + * Initialize. + * + * set the signature algorithm + * generate a key pair + * set the test content + * for ticket and layout + */ + @Before public void initialize() { + + algorithmOID = Constants.DSA_SHA224; + keySize = 2048; + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + testLayout = SimpleTestTicketLayout.getSimpleTestTicketLayout(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + } + + + /** + * Test dynamic header barcode encoding. + */ + @Test public void testStaticHeaderBarcodeEncoding() { + + IUicRailTicket ticket = testFCBticket; + + TicketLayout layout = testLayout; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setStaticHeaderParams("123456789012", "de"); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + } + + /** + * Test dynamic header barcode decoding. + */ + @Test public void testStaticHeaderBarcodeDecoding() { + + + IUicRailTicket ticket = testFCBticket; + + TicketLayout layout = testLayout; + + + Encoder enc = null; + + try { + enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + enc.setStaticHeaderParams("123456789012", "de"); + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + SimpleTestTicketLayout.compare(layout, dec.getLayout()); + + } + + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + +} -- cgit v1.2.3 From 3062dbc6b6df9c7ef824eeea8d31ad5702506518 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Wed, 24 Nov 2021 12:40:11 +0100 Subject: more tests --- .../barcode/asn1/test/UperEncodeBitStringTest.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java (limited to 'src') diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java new file mode 100644 index 0000000..574a9cf --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeBitStringTest.java @@ -0,0 +1,81 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.Asn1VarSizeBitstring; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeBitStringTest { + + /** + * Example from the Standard on UPER. +
+ World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value BIT STRING OPTIONAL, + } + END + + + rec1value TestRecord ::= { + value '001'B + } ++ + + + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Optional() Asn1VarSizeBitstring value; + + public TestRecord() { + this(false,false,true); + } + + public TestRecord(Boolean value1,Boolean value2,Boolean value3 ) { + List
+ World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value1 PrintableString ( FROM ("ACGT") ) OPTIONAL, + value2 PrintableString ( FROM ("ACGT") ) OPTIONAL + } + END + + rec1value TestRecord ::= { + value1 "ACGT", + value2 "ACTGCATCGA" + } ++ */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(value = CharacterRestriction.VisibleString, alphabet = GenAlphabet.class) + @Asn1Optional() String value1; + + @FieldOrder(order = 1) + @RestrictedString(value = CharacterRestriction.VisibleString, alphabet = GenAlphabet.class) + @Asn1Optional() String value2; + + public TestRecord() { + } + + public TestRecord(String v1, String v2) { + this.value1 = v1; + this.value2 = v2; + } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + + + TestRecord record = new TestRecord("ACGT", "ACTGCATCGA"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C106C2879360",hex); + + } + + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord("ACGT", "ACTGCATCGA"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C106C2879360",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1,record.value1); + assertEquals(result.value2,record.value2); + } + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeVarBitStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeVarBitStringTest.java new file mode 100644 index 0000000..d6212e2 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeVarBitStringTest.java @@ -0,0 +1,81 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.Asn1VarSizeBitstring; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeVarBitStringTest { + + /** + * Example from the Standard on UPER. +
+ World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value BIT STRING OPTIONAL, + } + END + + + rec1value TestRecord ::= { + value '001'B + } ++ + + + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Optional() Asn1VarSizeBitstring value; + + public TestRecord() { + this(false,false,true); + } + + public TestRecord(Boolean value1,Boolean value2,Boolean value3 ) { + List