From 12b733b1062430ce4bad08c077148a7d235439f3 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Wed, 15 Dec 2021 16:36:47 +0100 Subject: - unit test for the dynamic content in DOSIPAS - api layoer for the dynamic content in DOSIPAS --- src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index f3e4f54..a5f91ca 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -24,7 +24,6 @@ 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; -- cgit v1.2.3 From fbbc6349283bc8958cad51534da9cb82c46210c9 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Wed, 15 Dec 2021 16:37:37 +0100 Subject: api layer for dynamic content --- .../barcode/test/AsnLevelDynamicContentTest.java | 112 +++++++++++++++++++ .../uic/barcode/test/DynamicContentCoderTest.java | 118 +++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/AsnLevelDynamicContentTest.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/AsnLevelDynamicContentTest.java b/src/test/java/org/uic/barcode/test/AsnLevelDynamicContentTest.java new file mode 100644 index 0000000..955066d --- /dev/null +++ b/src/test/java/org/uic/barcode/test/AsnLevelDynamicContentTest.java @@ -0,0 +1,112 @@ +package org.uic.barcode.test; + +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.dynamicContent.fdc1.ExtensionData; +import org.uic.barcode.dynamicContent.fdc1.TimeStamp; +import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; +import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateType; + +public class AsnLevelDynamicContentTest { + + UicDynamicContentDataFDC1 asn = null; + + @Before public void initialize() { + + + asn = new UicDynamicContentDataFDC1(); + + asn.setAppId("appID"); + + asn.setChallengeString("challenge string"); + + ExtensionData asnE1 = new ExtensionData(); + asnE1.setExtensionData(UperEncoder.bytesFromHexString("82DA")); + asnE1.setExtensionId("challenge_extension_id1"); + asn.setDynamicContentExtension(asnE1); + + + asn.getExtensions().add(asnE1); + + ExtensionData asnE2 = new ExtensionData(); + asnE2.setExtensionData(UperEncoder.bytesFromHexString("83DA")); + asnE2.setExtensionId("phone"); + asn.getExtensions().add(asnE2); + + ExtensionData asnE3 = new ExtensionData(); + asnE3.setExtensionData(UperEncoder.bytesFromHexString("84DA")); + asnE3.setExtensionId("pass"); + asn.getExtensions().add(asnE3); + + + + asn.setGeoCoordinate(new GeoCoordinateType()); + asn.getGeoCoordinate().setLongitude( 12345L); + asn.getGeoCoordinate().setLatitude( 56789L); + + TimeStamp t = new TimeStamp(); + t.setTime(100L); + t.setDay(20L); + asn.setTimeStamp(t); + + + + } + + @Test public void testDynamicContentEncoding() { + + byte[] encodedBytes = UperEncoder.encode(asn); + + String encoding = UperEncoder.hexStringFromBytes(encodedBytes); + + String expectedEncoding = "7C170F0E126204C00C800230390300DDD504017A20C6D0C2D8D8CADCCECA40E6E8E4D2DCCE2F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA05E1A37EECA0507B409C30F3E60509B42F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA"; + + assert(expectedEncoding.equals(encoding)); + + } + + @Test public void testDynamicContentDecoding() { + + String encoding = "7C170F0E126204C00C800230390300DDD504017A20C6D0C2D8D8CADCCECA40E6E8E4D2DCCE2F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA05E1A37EECA0507B409C30F3E60509B42F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA"; + + UicDynamicContentDataFDC1 asn = UperEncoder.decode(UperEncoder.bytesFromHexString(encoding), UicDynamicContentDataFDC1.class); + + + assert("appID".equals(asn.getAppId())); + + assert("challenge string".equals(asn.getChallengeString())); + + ExtensionData asnE1 = asn.getDynamicContentExtension(); + assert(UperEncoder.hexStringFromBytes(asnE1.getExtensionData()).equals("82DA")); + assert(asnE1.getExtensionId().equals("challenge_extension_id1")); + + + assert(asn.getExtensions().size() == 4); + + assert(UperEncoder.hexStringFromBytes(asn.getExtensions().get(0).getExtensionData()).equals("6368616C6C656E676520737472696E67")); + assert(asn.getExtensions().get(0).getExtensionId().equals("=")); + + assert(UperEncoder.hexStringFromBytes(asn.getExtensions().get(1).getExtensionData()).equals("82DA")); + assert(asn.getExtensions().get(1).getExtensionId().equals("challenge_extension_id1")); + + assert(UperEncoder.hexStringFromBytes(asn.getExtensions().get(2).getExtensionData()).equals("83DA")); + assert(asn.getExtensions().get(2).getExtensionId().equals("phone")); + + assert(UperEncoder.hexStringFromBytes(asn.getExtensions().get(3).getExtensionData()).equals("84DA")); + assert(asn.getExtensions().get(3).getExtensionId().equals("pass")); + + + assert(asn.getGeoCoordinate() != null); + assert(asn.getGeoCoordinate().getLongitude() == 12345L); + assert(asn.getGeoCoordinate().getLatitude() == 56789L); + + assert(asn.getTimeStamp() != null); + assert(asn.getTimeStamp().getDay() == 20L); + assert(asn.getTimeStamp().getTime() == 100L); + + } + + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java b/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java new file mode 100644 index 0000000..10963c5 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java @@ -0,0 +1,118 @@ +package org.uic.barcode.test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.dynamicContent.api.DynamicContentCoder; +import org.uic.barcode.dynamicContent.api.IUicDynamicContent; +import org.uic.barcode.dynamicContent.api.SimpleUicDynamicContent; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.impl.SimpleExtension; +import org.uic.barcode.ticket.api.impl.SimpleGeoCoordinate; +import org.uic.barcode.ticket.api.spec.IExtension; +import org.uic.barcode.ticket.api.spec.IGeoCoordinate; + +public class DynamicContentCoderTest { + + IUicDynamicContent content = null; + + @Before public void initialize() { + + content = new SimpleUicDynamicContent(); + + content.setAppId("appID"); + + content.setChallengeString("challenge string"); + + IExtension e = new SimpleExtension(); + e.setId("challenge_extension_id1"); + e.setBinarydata(UperEncoder.bytesFromHexString("82DA")); + content.setExtension(e); + + IExtension e1 = new SimpleExtension(); + e1.setId("challenge_extension_id1"); + e1.setBinarydata(UperEncoder.bytesFromHexString("82DA")); + content.addDynamicContentResponse(e1); + + content.setPhoneIdHash(UperEncoder.bytesFromHexString("83DA")); + + content.setPassIdHash(UperEncoder.bytesFromHexString("84DA")); + + IGeoCoordinate g = new SimpleGeoCoordinate(); + g.setLongitude( 12345L); + g.setLatitude( 56789L); + content.setGeoCoordinate(g); + + try { + content.setTimeStamp(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + } catch (ParseException e2) { + // + } + + } + + @Test public void testDynamicContentEncoding() { + + + byte[] encodedBytes = null; + try { + encodedBytes = DynamicContentCoder.encode(content, null); + } catch (EncodingFormatException e) { + assert(false); + } + + String encoding = UperEncoder.hexStringFromBytes(encodedBytes); + + String expectedEncoding = "7C170F0E12620F9437000230390300DDD504017A20C6D0C2D8D8CADCCECA40E6E8E4D2DCCE2F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA05E1A37EECA0507B409C30F3E60509B42F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA"; + + assert(expectedEncoding.equals(encoding)); + + } + + @Test public void testDynamicContentDecoding() { + + String encoding = "7C170F0E12620F9437000230390300DDD504017A20C6D0C2D8D8CADCCECA40E6E8E4D2DCCE2F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA05E1A37EECA0507B409C30F3E60509B42F8F461D9B32EECF96FE5F1D32EEE7A77EEBFA72310282DA"; + + IUicDynamicContent content = DynamicContentCoder.decode(UperEncoder.bytesFromHexString(encoding)); + + + assert("appID".equals(content.getAppId())); + + assert("challenge string".equals(content.getChallengeString())); + + IExtension e1 = content.getExtension(); + assert(UperEncoder.hexStringFromBytes(e1.getBinarydata()).equals("82DA")); + assert(e1.getId().equals("challenge_extension_id1")); + + + assert(content.getChallengeString().equals("challenge string")); + + byte[] ce = null; + for (IExtension e : content.getDynamicContentResponseList()) { + if (e.getId().equals("challenge_extension_id1")) { + ce = e.getBinarydata(); + } + } + assert(UperEncoder.hexStringFromBytes(ce).equals("82DA")); + + assert(UperEncoder.hexStringFromBytes(content.getPhoneIdHash()).equals("83DA")); + + assert(UperEncoder.hexStringFromBytes(content.getPassIdHash()).equals("84DA")); + + assert(content.getGeoCoordinate() != null); + assert(content.getGeoCoordinate().getLongitude() == 12345L); + assert(content.getGeoCoordinate().getLatitude() == 56789L); + + assert(content.getTimeStamp() != null); + + assert(content.getTimeStamp().toString().equals("Wed Dec 15 12:30:00 CET 2021" )); + + + } + + + +} -- cgit v1.2.3 From bdb54c653eda54b003e50460928cfd8bbc80bc44 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Thu, 16 Dec 2021 16:13:13 +0100 Subject: extended api for dynamic content additional tests --- .../test/DynamicFrameDynamicContentApiTest.java | 230 +++++++++++++++++++++ .../java/org/uic/barcode/test/TimeStampTest.java | 36 ++++ .../uic/barcode/test/utils/DynamicTestContent.java | 38 ++++ 3 files changed, 304 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java create mode 100644 src/test/java/org/uic/barcode/test/TimeStampTest.java create mode 100644 src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java new file mode 100644 index 0000000..5a70841 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java @@ -0,0 +1,230 @@ +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.time.temporal.ChronoUnit; +import java.util.Arrays; +import java.util.Date; +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.api.IUicDynamicContent; +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 DynamicFrameDynamicContentApiTest { + + 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; + + + @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 { + enc.setDynamicData(DynamicTestContent.createDynamicTestContent()); + 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 { + enc.setDynamicData(DynamicTestContent.createDynamicTestContent()); + 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); + + IUicDynamicContent dynamicData = dec.getDynamicContent(); + + assert(dynamicData.getChallengeString().equals("CHALLENGE")); + + assert(dynamicData.getAppId().equals("MyApp")); + + assert(Arrays.equals(dynamicData.getPassIdHash(),passIdHash)); + + assert(Arrays.equals(dynamicData.getPhoneIdHash(),phoneIdHash)); + + assert(dynamicData.getGeoCoordinate().getLatitude() == 123456L); + assert(dynamicData.getGeoCoordinate().getLongitude() == 23456L); + + Date timeStamp = dynamicData.getTimeStamp(); + ZonedDateTime retrievedTimeStamp = timeStamp.toInstant().atZone(ZoneId.of("UTC")); + ZonedDateTime originalTimeStamp = ZonedDateTime.now(ZoneId.of("UTC")); + long diff = ChronoUnit.SECONDS.between(originalTimeStamp, retrievedTimeStamp); + + assert(diff > -50 && diff < 50); + } + + 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/TimeStampTest.java b/src/test/java/org/uic/barcode/test/TimeStampTest.java new file mode 100644 index 0000000..b2e3ab1 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/TimeStampTest.java @@ -0,0 +1,36 @@ +package org.uic.barcode.test; + +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.Test; +import org.uic.barcode.dynamicContent.fdc1.TimeStamp; + +public class TimeStampTest { + + + + @Test public void testDateConversion() { + + + TimeStamp ts = new TimeStamp(); + + ts.setDay(10L); + ts.setTime(6000L); + + Date date = ts.getTimeAsDate(); + + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + cal.setTime(date); + long seconds = cal.get(Calendar.SECOND); + seconds = seconds + cal.get(Calendar.MINUTE) * 60; + seconds = seconds + cal.get(Calendar.HOUR_OF_DAY) * 60 * 60; + + assert(seconds == 6000L); + + + } + + +} diff --git a/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java b/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java new file mode 100644 index 0000000..a39b270 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/DynamicTestContent.java @@ -0,0 +1,38 @@ +package org.uic.barcode.test.utils; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; + +import org.uic.barcode.dynamicContent.api.IUicDynamicContent; +import org.uic.barcode.dynamicContent.api.SimpleUicDynamicContent; +import org.uic.barcode.ticket.api.impl.SimpleGeoCoordinate; +import org.uic.barcode.ticket.api.spec.IGeoCoordinate; + +public class DynamicTestContent { + + public static byte[] passIdHash = "PassId".getBytes(); + public static byte[] phoneIdHash = "myPhone".getBytes(); + + + public static IUicDynamicContent createDynamicTestContent() { + + IUicDynamicContent dc = new SimpleUicDynamicContent(); + dc.setChallengeString("CHALLENGE"); + dc.setAppId("MyApp"); + dc.setPhoneIdHash(phoneIdHash); + dc.setPassIdHash(passIdHash); + + ZonedDateTime timeStamp = ZonedDateTime.now(ZoneId.of("UTC")); + + dc.setTimeStamp(Date.from(timeStamp.toInstant())); + + IGeoCoordinate geo = new SimpleGeoCoordinate(); + geo.setLatitude(123456L); + geo.setLongitude(23456L); + dc.setGeoCoordinate(geo); + + return dc; + } + +} -- cgit v1.2.3 From 17f05b763d70f350bad482df9378c571c2ebddf6 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:19:36 +0100 Subject: new dynamic header version 2.0.0 --- .../uic/barcode/test/DynamicContentCoderTest.java | 2 +- ...cFrameDoubleSignatureProviderSelectionTest.java | 10 ++++----- .../test/DynamicFrameDoubleSignatureTest.java | 10 ++++----- .../test/DynamicFrameDynamicContentTest.java | 7 ++++--- .../barcode/test/DynamicFrameFcbVersion1Test.java | 8 +++++--- .../barcode/test/DynamicFrameFcbVersion3Test.java | 6 ++++-- .../uic/barcode/test/DynamicFrameLOwLevelTest.java | 2 +- .../barcode/test/utils/Level2TestDataFactory.java | 10 ++++----- .../test/utils/SimpleDynamicFrameTestBarcode.java | 24 +++++++++++++--------- 9 files changed, 44 insertions(+), 35 deletions(-) (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java b/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java index 10963c5..5355d5d 100644 --- a/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java @@ -108,7 +108,7 @@ public class DynamicContentCoderTest { assert(content.getTimeStamp() != null); - assert(content.getTimeStamp().toString().equals("Wed Dec 15 12:30:00 CET 2021" )); + assert(content.getTimeStamp().toString().contains("04 12:30:00 CET")); } diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java index 84f7795..0730e69 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureProviderSelectionTest.java @@ -22,7 +22,7 @@ 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.dynamicFrame.api.IData; import org.uic.barcode.test.utils.Level2TestDataFactory; import org.uic.barcode.test.utils.SimpleUICTestTicket; import org.uic.barcode.ticket.EncodingFormatException; @@ -93,7 +93,7 @@ public class DynamicFrameDoubleSignatureProviderSelectionTest { assert(enc != null); - DataType level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); + IData level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); try { enc.setLevel2Data(level2Data); enc.signLevel2(keyPairLevel2.getPrivate(),prov); @@ -135,15 +135,15 @@ public class DynamicFrameDoubleSignatureProviderSelectionTest { signatureCheck = 0; try { signatureCheck = dec.validateLevel2(prov); - } catch (IllegalArgumentException | UnsupportedOperationException e) { + } catch (Exception e) { assert(false); } assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK); - DataType level2DataDec = dec.getLevel2Data(); + IData level2DataDec = dec.getLevel2Data(); assert(level2Data.getFormat().equals(level2DataDec.getFormat())); - assert(Arrays.equals(level2Data.getData().toByteArray(),level2DataDec.getData().toByteArray())); + assert(Arrays.equals(level2Data.getData(),level2DataDec.getData())); SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java index a316cfc..787ebae 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java @@ -21,7 +21,7 @@ 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.dynamicFrame.api.IData; import org.uic.barcode.test.utils.Level2TestDataFactory; import org.uic.barcode.test.utils.SimpleUICTestTicket; import org.uic.barcode.ticket.EncodingFormatException; @@ -89,7 +89,7 @@ public class DynamicFrameDoubleSignatureTest { assert(enc != null); - DataType level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); + IData level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); try { enc.setLevel2Data(level2Data); enc.signLevel2(keyPairLevel2.getPrivate()); @@ -131,15 +131,15 @@ public class DynamicFrameDoubleSignatureTest { signatureCheck = 0; try { signatureCheck = dec.validateLevel2(); - } catch (IllegalArgumentException | UnsupportedOperationException e) { + } catch (Exception e) { assert(false); } assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK); - DataType level2DataDec = dec.getLevel2Data(); + IData level2DataDec = dec.getLevel2Data(); assert(level2Data.getFormat().equals(level2DataDec.getFormat())); - assert(Arrays.equals(level2Data.getData().toByteArray(),level2DataDec.getData().toByteArray())); + assert(Arrays.equals(level2Data.getData(),level2DataDec.getData())); SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java index a5f91ca..4cfca12 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -24,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.api.IUicDynamicContent; import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateType; import org.uic.barcode.dynamicContent.fdc1.TimeStamp; import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; @@ -217,7 +218,7 @@ public class DynamicFrameDynamicContentTest { assert(level2check == Constants.LEVEL2_VALIDATION_OK); - UicDynamicContentDataFDC1 dynamicData = dec.getDynamicHeader().getDynamicDataFDC1(); + IUicDynamicContent dynamicData = dec.getDynamicHeader().getDynamicContent(); assert(dynamicData.getChallengeString().equals("CHALLENGE")); @@ -228,9 +229,9 @@ public class DynamicFrameDynamicContentTest { assert(Arrays.equals(dynamicData.getPhoneIdHash(),phoneIdHash)); assert(dynamicData.getGeoCoordinate().getLatitude() == 123456L); - assert(dynamicData.getGeoCoordinate().getLongitude() == 823456L); + assert(dynamicData.getGeoCoordinate().getLongitude() == 103456L); - Date timeStamp = dynamicData.getTimeStamp().getTimeAsDate(); + Date timeStamp = dynamicData.getTimeStamp(); 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/DynamicFrameFcbVersion1Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java index 63ba68e..861fc85 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java @@ -20,6 +20,7 @@ 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.api.IData; import org.uic.barcode.test.utils.SimpleUICTestTicket; import org.uic.barcode.ticket.EncodingFormatException; import org.uic.barcode.ticket.api.spec.IUicRailTicket; @@ -144,9 +145,10 @@ public class DynamicFrameFcbVersion1Test { assert(dec.getDynamicHeader().getFormat().equals("U1")); - assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB1")); - - + for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + assert(data.getFormat().equals("FCB1") ); + } + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java index 4f0897e..fa055dc 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java @@ -20,6 +20,7 @@ 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.api.IData; import org.uic.barcode.test.utils.SimpleUICTestTicket; import org.uic.barcode.ticket.EncodingFormatException; import org.uic.barcode.ticket.api.spec.IUicRailTicket; @@ -144,8 +145,9 @@ public class DynamicFrameFcbVersion3Test { assert(dec.getDynamicHeader().getFormat().equals("U1")); - assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB3")); - + for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + assert(data.getFormat().equals("FCB3") ); + } SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java index 5dd9668..d354837 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java @@ -21,7 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.uic.barcode.asn1.uper.UperEncoder; import org.uic.barcode.dynamicFrame.Constants; -import org.uic.barcode.dynamicFrame.DynamicFrame; +import org.uic.barcode.dynamicFrame.v1.DynamicFrame; import org.uic.barcode.test.utils.SimpleDynamicFrameTestBarcode; public class DynamicFrameLOwLevelTest { diff --git a/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java b/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java index b1eb0f5..bdabdc6 100644 --- a/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java +++ b/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java @@ -1,17 +1,17 @@ package org.uic.barcode.test.utils; -import org.uic.barcode.asn1.datatypesimpl.OctetString; -import org.uic.barcode.dynamicFrame.DataType; +import org.uic.barcode.dynamicFrame.api.IData; +import org.uic.barcode.dynamicFrame.api.SimpleData; public class Level2TestDataFactory { - public static DataType getLevel2SimpleTestData() { + public static IData getLevel2SimpleTestData() { String testContent = "2020.10.01T12:12.20"; - DataType level2Data = new DataType(); + IData level2Data = new SimpleData(); level2Data.setFormat("TEST"); - level2Data.setData(new OctetString(testContent.getBytes())); + level2Data.setData(testContent.getBytes()); return level2Data; } diff --git a/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java b/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java index 0060fcd..f2ef0f3 100644 --- a/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java +++ b/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java @@ -1,28 +1,27 @@ package org.uic.barcode.test.utils; import java.security.KeyPair; +import java.security.Signature; +import org.uic.barcode.asn1.uper.UperEncoder; import org.uic.barcode.dynamicFrame.Constants; -import org.uic.barcode.dynamicFrame.DataType; -import org.uic.barcode.dynamicFrame.DynamicFrame; -import org.uic.barcode.dynamicFrame.Level1DataType; -import org.uic.barcode.dynamicFrame.Level2DataType; -import org.uic.barcode.dynamicFrame.SequenceOfDataType; +import org.uic.barcode.dynamicFrame.v1.DataType; +import org.uic.barcode.dynamicFrame.v1.DynamicFrame; +import org.uic.barcode.dynamicFrame.v1.Level1DataType; +import org.uic.barcode.dynamicFrame.v1.Level2DataType; +import org.uic.barcode.dynamicFrame.v1.SequenceOfDataType; import org.uic.barcode.ticket.EncodingFormatException; import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; import org.uic.barcode.ticket.api.test.testtickets.SimpleUicTestTicket; +import org.uic.barcode.utils.AlgorithmNameResolver; public class SimpleDynamicFrameTestBarcode { public static DynamicFrame getSimpleDynamicHeaderBarcode(String algorithm, KeyPair keyPair) { - - DynamicFrame barcode = null; - - try { barcode = new DynamicFrame(); barcode.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_DEFAULT); @@ -47,7 +46,12 @@ public class SimpleDynamicFrameTestBarcode { level1Data.getData().add(data); try { - level2Data.signLevel1(keyPair.getPrivate()); + String algo = AlgorithmNameResolver.getSignatureAlgorithmName(level1Data.getLevel1SigningAlg()); + Signature sig = Signature.getInstance(algo); + sig.initSign(keyPair.getPrivate()); + byte[] data2 = UperEncoder.encode(level1Data); + sig.update(data2); + level2Data.setLevel1Signature(sig.sign()); } catch (Exception e) { assert(false); } -- cgit v1.2.3 From 09f0f9449a10b713207126348105fafec4781bed Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Mon, 24 Jan 2022 16:51:04 +0100 Subject: signature validation changed to work with teh dynamic header version 2. --- .../org/uic/barcode/test/BinaryStringTest.java | 61 ++++++++++++++++++++++ .../test/DynamicFrameDynamicContentApiTest.java | 2 - 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/BinaryStringTest.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/BinaryStringTest.java b/src/test/java/org/uic/barcode/test/BinaryStringTest.java new file mode 100644 index 0000000..d5533fa --- /dev/null +++ b/src/test/java/org/uic/barcode/test/BinaryStringTest.java @@ -0,0 +1,61 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import org.junit.Test; +import org.uic.barcode.asn1.uper.AsnUtils; +import org.uic.barcode.ticket.EncodingFormatException; + +public class BinaryStringTest { + + + + + @Test public void testBinaryString() throws IOException, EncodingFormatException{ + + String bs1 = "01000000"; + String ms1 = "1000000001000000001000000001000000001000000001000000001000000001"; + String ms2 = "10000000010000000010000000010000"; + + + //String bs1 = "1011111100001000011011100000000000000001000000010000010010000000"; + + byte[] bytes = AsnUtils.fromBooleanString(bs1); + + String bs2 = AsnUtils.toBooleanString(bytes); + + + + byte[] mask = new byte[] { + (byte) 0b1000_0000, + 0b0100_0000, + 0b0010_0000, + 0b0001_0000, + 0b0000_1000, + 0b0000_0100, + 0b0000_0010, + 0b0000_0001, + }; + String bs3 = AsnUtils.toBooleanString(mask); + byte[] bytes2 = AsnUtils.fromBooleanString(bs3); + + + byte[] mask2 = new byte[] { + (byte) 0b1000_0000, + 0b0100_0000, + 0b0010_0000, + 0b0001_0000, + }; + String bs4 = AsnUtils.toBooleanString(mask2); + byte[] bytes3 = AsnUtils.fromBooleanString(bs4); + + + assert(bs4.equals(ms2)); + + assert(bs3.equals(ms1)); + + assert(bs1.equals(bs2)); + + } + + +} \ No newline at end of file diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java index 5a70841..4d34c1b 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentApiTest.java @@ -10,8 +10,6 @@ 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.time.temporal.ChronoUnit; -- cgit v1.2.3 From 7af6c4ca50322258bbd23214920c4c9122482966 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 25 Jan 2022 13:33:37 +0100 Subject: test for the new dynamic header version 2 --- .../test/DynamicFrameV2FcbVersion3Test.java | 176 +++++++++++++++++ .../test/DynamicFrameV2ValidityDateTest.java | 208 +++++++++++++++++++++ 2 files changed, 384 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java new file mode 100644 index 0000000..8014b78 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameV2FcbVersion3Test.java @@ -0,0 +1,176 @@ +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.dynamicFrame.api.IData; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameV2FcbVersion3Test { + + 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, 2, 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, 2, 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("U2")); + + for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + assert(data.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/DynamicFrameV2ValidityDateTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.java new file mode 100644 index 0000000..03536c4 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameV2ValidityDateTest.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.text.ParseException; +import java.text.SimpleDateFormat; +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.dynamicFrame.api.IData; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameV2ValidityDateTest { + + 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, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + TimeZone local = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + Date endDate = null; + try { + endDate = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ); + } catch (ParseException e1) { + assert(false); + } + TimeZone.setDefault(local); + + enc.getDynamicFrame().getLevel2Data().getLevel1Data().setEndOfBarcodeValidity(endDate); + + 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, 2, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + TimeZone local = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + Date endDate = null; + try { + endDate = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ); + } catch (ParseException e1) { + assert(false); + } + TimeZone.setDefault(local); + + enc.getDynamicFrame().getLevel2Data().getLevel1Data().setEndOfBarcodeValidity(endDate); + + 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("U2")); + + for (IData data : dec.getDynamicHeader().getLevel2Data().getLevel1Data().getData()) { + assert(data.getFormat().equals("FCB3") ); + } + + Date endDate2 = dec.getDynamicHeader().getLevel2Data().getLevel1Data().getEndOfBarcodeValidity(); + + + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + String date2 = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).format(endDate2); + TimeZone.setDefault(local); + + assert("2021.03.04-12:30".equals(date2)); + + } + + 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 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/barcode') 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 From 2f72a6419dd1e20acf6e77a9276a6055892e732a Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 1 Feb 2022 12:45:04 +0100 Subject: - missing fieldOrder added - validation of level1 when the signature algorithm is missing --- src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java | 2 +- src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java index 4095d93..199a32d 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java @@ -135,7 +135,7 @@ public class DynamicFrameFcbVersion3Test { int signatureCheck = 0; try { - signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + signatureCheck = dec.validateLevel1(keyPair.getPublic()); } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException | UnsupportedOperationException | IOException | EncodingFormatException e) { assert(false); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java index d354837..b8a851b 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java @@ -84,17 +84,13 @@ public class DynamicFrameLOwLevelTest { } - @Test public void testDynamicHeaderBarcodeDecoding() { + @Test public void testDynamicHeaderBarcodeDecodingV1() { DynamicFrame barcode1 = SimpleDynamicFrameTestBarcode.getSimpleDynamicHeaderBarcode(algorithmOID, keyPair); byte[] encoded = barcode1.encode(); DynamicFrame barcode = DynamicFrame.decode(encoded); - - int signatureCheck = barcode.validateLevel1(keyPair.getPublic()); - - assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); SimpleDynamicFrameTestBarcode.compareFrame(barcode1, barcode); -- cgit v1.2.3 From 9ff0dde49e818348df5ba9c7bca91d165227234b Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 1 Feb 2022 14:16:00 +0100 Subject: - smplified api for level 2 signature --- .../test/DynamicFrameV2SignatureInsert2Test.java | 270 +++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsert2Test.java (limited to 'src/test/java/org/uic/barcode') diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsert2Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsert2Test.java new file mode 100644 index 0000000..a7d5098 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameV2SignatureInsert2Test.java @@ -0,0 +1,270 @@ +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 DynamicFrameV2SignatureInsert2Test { + + 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 encoder with dynamic frame to continue + try { + enc = new Encoder(encoded, 2); + } catch (Exception 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; + + } + + +} -- cgit v1.2.3