From 7410ac59ba8e1994254a872104ea660b992cba9a Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 28 Jan 2022 17:06:47 +0100 Subject: new dynamic header version --- .../asn1/test/UperEncodeFieldOrderTest.java | 2 - .../asn1/test/UperEncodeIntegerExtensionTest.java | 2 - .../asn1/test/UperEncodeSequenceOfStringTest.java | 2 - .../test/DynamicFrameDynamicContentTest.java | 2 +- .../barcode/test/DynamicFrameFcbVersion1Test.java | 4 +- .../barcode/test/DynamicFrameFcbVersion3Test.java | 4 +- .../test/DynamicFrameV2FcbVersion3Test.java | 4 +- .../test/DynamicFrameV2SignatureInsertTest.java | 289 +++++++++++++++++++++ .../test/DynamicFrameV2ValidityDateTest.java | 10 +- .../uic/barcode/test/utils/DynamicTestContent.java | 2 + 10 files changed, 305 insertions(+), 16 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsertTest.java (limited to 'src/test/java/org/uic') diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java index 5a23f24..d86b4cd 100644 --- a/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java @@ -2,8 +2,6 @@ package org.uic.barcode.asn1.test; import static org.junit.Assert.assertEquals; -import java.util.logging.Level; - import org.junit.Test; import org.uic.barcode.asn1.datatypes.Asn1Optional; import org.uic.barcode.asn1.datatypes.CharacterRestriction; diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java index d0acd20..acec756 100644 --- a/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java @@ -2,8 +2,6 @@ package org.uic.barcode.asn1.test; import static org.junit.Assert.assertEquals; -import java.util.logging.Level; - import org.junit.Test; import org.uic.barcode.asn1.datatypes.Asn1BigInteger; import org.uic.barcode.asn1.datatypes.FieldOrder; diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java index f8eccc6..f3bff6d 100644 --- a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java @@ -5,9 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; import org.junit.Test; -import org.uic.barcode.asn1.datatypes.CharacterRestriction; import org.uic.barcode.asn1.datatypes.FieldOrder; -import org.uic.barcode.asn1.datatypes.RestrictedString; import org.uic.barcode.asn1.datatypes.Sequence; import org.uic.barcode.asn1.datatypesimpl.SequenceOfStringIA5; import org.uic.barcode.asn1.uper.UperEncoder; diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index 4cfca12..d26ab77 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -218,7 +218,7 @@ public class DynamicFrameDynamicContentTest { assert(level2check == Constants.LEVEL2_VALIDATION_OK); - IUicDynamicContent dynamicData = dec.getDynamicHeader().getDynamicContent(); + IUicDynamicContent dynamicData = dec.getDynamicFrame().getDynamicContent(); assert(dynamicData.getChallengeString().equals("CHALLENGE")); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java index 861fc85..ea95a88 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java @@ -143,9 +143,9 @@ public class DynamicFrameFcbVersion1Test { assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - assert(dec.getDynamicHeader().getFormat().equals("U1")); + assert(dec.getDynamicFrame().getFormat().equals("U1")); - for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + for (IData data : dec.getDynamicFrame().getLevel2Data().getLevel1Data().getData()) { assert(data.getFormat().equals("FCB1") ); } diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java index fa055dc..4095d93 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java @@ -143,9 +143,9 @@ public class DynamicFrameFcbVersion3Test { assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - assert(dec.getDynamicHeader().getFormat().equals("U1")); + assert(dec.getDynamicFrame().getFormat().equals("U1")); - for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + for (IData data : dec.getDynamicFrame().getLevel2Data().getLevel1Data().getData()) { assert(data.getFormat().equals("FCB3") ); } diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java index 8014b78..17bccd6 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java @@ -143,9 +143,9 @@ public class DynamicFrameV2FcbVersion3Test { assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - assert(dec.getDynamicHeader().getFormat().equals("U2")); + assert(dec.getDynamicFrame().getFormat().equals("U2")); - for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + for (IData data : dec.getDynamicFrame().getLevel2Data().getLevel1Data().getData()) { assert(data.getFormat().equals("FCB3") ); } diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsertTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsertTest.java new file mode 100644 index 0000000..8f97574 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsertTest.java @@ -0,0 +1,289 @@ +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.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.TimeZone; +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.DynamicTestContent; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameV2SignatureInsertTest { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPairLevel1 = null; + public KeyPair keyPairLevel2 = null; + + public byte[] passIdHash = "PassId".getBytes(); + public byte[] phoneIdHash = "myPhone".getBytes(); + + public IUicRailTicket testFCBticket = null; + + ZonedDateTime originalTimeStamp = ZonedDateTime.now(ZoneId.of("UTC")); + + @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 testDynamicHeaderBarcodeDecoding() { + + //--------------------------------------------------------------------------- + //create barcode data + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + assert(enc != null); + + //complete level 1 data + enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID); + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic()); + enc.getDynamicFrame().getLevel2Data().getLevel1Data().setEndOfBarcodeValidity(getUtcDate("2021.03.04-12:30")); + enc.getDynamicFrame().getLevel2Data().getLevel1Data().setValidityDuration(100L); + + + //sign level 1 data + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + // encode + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + assert(encoded != null); + + + + //---------------------------------------------------------------------------------------------- + //decode and check level 1 + 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); + + String keyId = null; + try { + keyId = dec.getLevel1KeyId(); + } catch (EncodingFormatException e3) { + assert(false); + } + assert(keyId != 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); + + + + + + //-------------------------------------------------------------------------------------------------- + // get encoded level 1 data + // add level 2 data and signature + + + byte[] signatureLevel1Data = enc.getDynamicFrame().getLevel2Data().getLevel1Signature(); + byte[] encodedLevel1Data = null; + try { + encodedLevel1Data = enc.getEncodedLevel1Data(); + } catch (IOException e2) { + assert(false); + } catch (EncodingFormatException e2) { + assert(false); + } + + + + //dynamic barcode creation + + //------------------------------------------------------------------------------- + //add the signed level 1 data for encoding of level 2 + try { + enc = new Encoder(encodedLevel1Data,signatureLevel1Data , 2); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + + //set dynamic content + try { + enc.setDynamicData(DynamicTestContent.createDynamicTestContent()); + } catch (EncodingFormatException e1) { + assert(false); + } + //----------- + // sign level 2 + try { + enc.signLevel2(keyPairLevel2.getPrivate()); + } catch (Exception e) { + assert(false); + } + + //------------------------ + //encode complete bar code + encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + assert(encoded != null); + + //---------------------------------------------------------------------------------------------------- + //decode full bar code + + 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); + + //--------------------------------------------------------------------------------------------------- + //check level 1 signature + + signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + //-------------------------------------------------------------------------------------------------------- + //check level 2 signature + + signatureCheck = 0; + try { + signatureCheck = dec.validateLevel2(); + } catch (Exception e) { + assert(false); + } + assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK); + + + } + + 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(); + + } + + public Date getUtcDate(String s) { + + TimeZone local = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + Date date = null; + try { + date = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse(s); + } catch (ParseException e1) { + assert(false); + } + TimeZone.setDefault(local); + + return date; + + } + + public String formatUTC(Date date) { + + TimeZone local = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + String dateS = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).format(date); + TimeZone.setDefault(local); + return dateS; + + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.java index 03536c4..9bbd736 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.java @@ -87,6 +87,7 @@ public class DynamicFrameV2ValidityDateTest { TimeZone.setDefault(local); enc.getDynamicFrame().getLevel2Data().getLevel1Data().setEndOfBarcodeValidity(endDate); + enc.getDynamicFrame().getLevel2Data().getLevel1Data().setValidityDuration(100L); try { enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); @@ -132,6 +133,7 @@ public class DynamicFrameV2ValidityDateTest { TimeZone.setDefault(local); enc.getDynamicFrame().getLevel2Data().getLevel1Data().setEndOfBarcodeValidity(endDate); + enc.getDynamicFrame().getLevel2Data().getLevel1Data().setValidityDuration(100L); try { enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); @@ -171,13 +173,13 @@ public class DynamicFrameV2ValidityDateTest { assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - assert(dec.getDynamicHeader().getFormat().equals("U2")); + assert(dec.getDynamicFrame().getFormat().equals("U2")); - for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + for (IData data : dec.getDynamicFrame().getLevel2Data().getLevel1Data().getData()) { assert(data.getFormat().equals("FCB3") ); } - Date endDate2 = dec.getDynamicHeader().getLevel2Data().getLevel1Data().getEndOfBarcodeValidity(); + Date endDate2 = dec.getDynamicFrame().getLevel2Data().getLevel1Data().getEndOfBarcodeValidity(); TimeZone.setDefault(TimeZone.getTimeZone("UTC")); @@ -186,6 +188,8 @@ public class DynamicFrameV2ValidityDateTest { assert("2021.03.04-12:30".equals(date2)); + assert(100L == dec.getDynamicFrame().getLevel2Data().getLevel1Data().getValidityDuration()); + } public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ diff --git a/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java b/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java index a39b270..9380372 100644 --- a/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java +++ b/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java @@ -35,4 +35,6 @@ public class DynamicTestContent { return dc; } + + } -- cgit v1.2.3