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 --- pom.xml | 4 +- .../uic/barcode/dynamicContent/fdc1/TimeStamp.java | 63 ++++++++++------------ .../fdc1/UicDynamicContentDataFDC1.java | 4 +- .../test/DynamicFrameDynamicContentTest.java | 38 ++++++++++++- 4 files changed, 69 insertions(+), 40 deletions(-) diff --git a/pom.xml b/pom.xml index 5a4313c..f82b70b 100644 --- a/pom.xml +++ b/pom.xml @@ -23,8 +23,8 @@ maven-compiler-plugin 2.3.2 - ${jdk.version} - ${jdk.version} + 1.8 + 1.8 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