From c45fc85b78f3d8701346a231f07b7ffa984ca4ed Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 23 Apr 2021 13:41:58 +0200 Subject: update --- .../java/org/uic/barcode/test/utils/TestUtils.java | 35 ++++++ .../ticketTestDB/DecodeSparpreisTicketDBTest.java | 1 - .../ticketTestDB/EncodeSparpreisTicketDBTest.java | 121 +++++++++++++++++++++ .../SignatureValidationDBTicketTest.java | 2 +- 4 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/uic/barcode/ticketTestDB/EncodeSparpreisTicketDBTest.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/uic/barcode/test/utils/TestUtils.java b/src/test/java/org/uic/barcode/test/utils/TestUtils.java index 7311ae4..20a21c4 100644 --- a/src/test/java/org/uic/barcode/test/utils/TestUtils.java +++ b/src/test/java/org/uic/barcode/test/utils/TestUtils.java @@ -1,9 +1,19 @@ package org.uic.barcode.test.utils; +import java.security.InvalidAlgorithmParameterException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.uic.barcode.utils.AlgorithmNameResolver; + public class TestUtils { final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); @@ -35,4 +45,29 @@ public class TestUtils { } + /** + * Generate DSA keys. + * + * @return the key pair + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchProviderException the no such provider exception + * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception + */ + public static KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC"); + g.initialize(keySize, new SecureRandom()); + return g.generateKeyPair(); + } + + public static KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, "BC"); + + 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/ticketTestDB/DecodeSparpreisTicketDBTest.java b/src/test/java/org/uic/barcode/ticketTestDB/DecodeSparpreisTicketDBTest.java index 480ab39..9999cec 100644 --- a/src/test/java/org/uic/barcode/ticketTestDB/DecodeSparpreisTicketDBTest.java +++ b/src/test/java/org/uic/barcode/ticketTestDB/DecodeSparpreisTicketDBTest.java @@ -1,6 +1,5 @@ package org.uic.barcode.ticketTestDB; -import static org.junit.jupiter.api.Assertions.assertAll; import java.util.TimeZone; diff --git a/src/test/java/org/uic/barcode/ticketTestDB/EncodeSparpreisTicketDBTest.java b/src/test/java/org/uic/barcode/ticketTestDB/EncodeSparpreisTicketDBTest.java new file mode 100644 index 0000000..787abf5 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticketTestDB/EncodeSparpreisTicketDBTest.java @@ -0,0 +1,121 @@ +package org.uic.barcode.ticketTestDB; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.KeyPair; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Security; +import java.util.TimeZone; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.staticFrame.StaticFrame; +import org.uic.barcode.test.utils.TestUtils; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class EncodeSparpreisTicketDBTest { + + /** The algorithm OID. */ + public String algorithmOID = Constants.DSA_SHA1; + + public int keySize = 1024; + + /** The key pair. */ + public KeyPair keyPair = null; + + public String securityProvider = null; + + /** the test ticket **/ + public IUicRailTicket ticket = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + * @throws DataFormatException + * @throws EncodingFormatException + * @throws IOException + */ + @Before public void prepare() throws IOException, EncodingFormatException, DataFormatException { + defaulttimeZone = TimeZone.getDefault(); + //decode in local CET time zone + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + + algorithmOID = Constants.DSA_SHA1; + keySize = 1024; + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = TestUtils.generateDSAKeys(keySize); + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) { + e.printStackTrace(); + } + + assert(keyPair != null); + + //get original ticket + byte[] content = UperEncoder.bytesFromHexString(getEncodingV2Hex()); + // decode to get the ticket + Decoder decoder = new Decoder(content); + ticket = decoder.getUicTicket(); + StaticFrame frame = decoder.getStaticFrame(); + securityProvider = frame.getSecurityProvider(); + } + + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + @Test + public void testDecoder() throws Exception { + + Encoder encoder = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_CLASSIC, 1, 2); + encoder.signLevel1(securityProvider, keyPair.getPrivate(), algorithmOID, "1"); + + byte[] encoded = encoder.encode(); + + assert(encoded != null); + + //TODO check ticket in detail + + } + + private static String getEncodingV2Hex() { + + return "2355543032313038303030303032782e" + + "2fe184a1d85e89e9338b298ec61aeba2" + + "48ce722056ca940a967c8a1d39126e2c" + + "628c4fcea91ba35216a0a350f894de5e" + + "bd7b8909920fde947feede0e20c43031" + + "3939789c01bc0043ff555f464c455831" + + "333031383862b20086e10dc125ea2815" + + "110881051c844464d985668e23a00a80" + + "000e96c2e4e6e8cadc08aed2d8d90104" + + "44d7be0100221ce610ea559b64364c38" + + "a82361d1cb5e1e5d32a3d0979bd099c8" + + "426b0b7373432b4b6852932baba3634b" + + "733b2b715ab34b09d101e18981c181f1" + + "424221521291521292a17a3a920a1152" + + "5a095282314952b20a49529952826278" + + "083001a4c38ae5bb303ace7003800700" + + "14b00240400f53757065722053706172" + + "7072656973c41e4a03"; + } + + + +} \ No newline at end of file diff --git a/src/test/java/org/uic/barcode/ticketTestDB/SignatureValidationDBTicketTest.java b/src/test/java/org/uic/barcode/ticketTestDB/SignatureValidationDBTicketTest.java index 44f9c4a..9249f4f 100644 --- a/src/test/java/org/uic/barcode/ticketTestDB/SignatureValidationDBTicketTest.java +++ b/src/test/java/org/uic/barcode/ticketTestDB/SignatureValidationDBTicketTest.java @@ -58,7 +58,7 @@ public class SignatureValidationDBTicketTest { assert("00002".equals(keyVersion)); - String algorithmOID = Constants.DSA_SHA248; + String algorithmOID = Constants.DSA_SHA256; int result = decoder.validateLevel1(getPublicKey2(), algorithmOID); -- cgit v1.2.3