From 900acb165c53e9801112c3f1b8f57ff4d9ad531d Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 23 Nov 2021 13:28:56 +0100 Subject: additional tests for DOSIPAS --- .../org/uic/barcode/ticket/UicRailTicketCoder.java | 2 +- .../test/DynamicFrameDynamicContentTest.java | 208 +++++++++++++++++++++ .../org/uic/barcode/test/DynamicFrameFcb3Test.java | 175 ----------------- .../barcode/test/DynamicFrameFcbVersion1Test.java | 174 +++++++++++++++++ .../barcode/test/DynamicFrameFcbVersion3Test.java | 174 +++++++++++++++++ 5 files changed, 557 insertions(+), 176 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java delete mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java diff --git a/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java b/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java index e9d2eb4..0586ed4 100644 --- a/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java +++ b/src/main/java/org/uic/barcode/ticket/UicRailTicketCoder.java @@ -33,7 +33,7 @@ public class UicRailTicketCoder { public byte[] encode (IUicRailTicket uicRailTicket, int version) throws IOException, EncodingFormatException{ - if (version == 13) { + if (version == 13 || version == 1) { Api2OpenAsnEncoder uicEncoder = new Api2OpenAsnEncoder(); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java new file mode 100644 index 0000000..93ecdfb --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDynamicContentTest.java @@ -0,0 +1,208 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameDynamicContentTest { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPairLevel1 = null; + public KeyPair keyPairLevel2 = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve); + } catch (Exception e) { + assert(false); + } + + assert(keyPairLevel1 != null); + + assert(keyPairLevel2 != null); + + } + + + @Test public void testDynamicContentEncoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID, keyPairLevel2.getPublic()); + + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + try { + UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); + dcd.setChallengeString("CHALLENGE"); + dcd.setAppId("MyApp"); + enc.setDynamicContentDataUIC1(dcd); + enc.signLevel2(keyPairLevel2.getPrivate()); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + + } + + @Test public void testDynamicContentDecoding() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID, keyPairLevel2.getPublic()); + + try { + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + try { + UicDynamicContentDataFDC1 dcd = new UicDynamicContentDataFDC1(); + dcd.setChallengeString("CHALLENGE"); + dcd.setAppId("MyApp"); + enc.setDynamicContentDataUIC1(dcd); + enc.signLevel2(keyPairLevel2.getPrivate()); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + int level2check = 0; + try { + level2check = dec.validateLevel2(); + } catch (Exception e) { + assert(false); + } + + assert(level2check == Constants.LEVEL2_VALIDATION_OK); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getChallengeString().equals("CHALLENGE")); + + assert(dec.getDynamicHeader().getDynamicDataFDC1().getAppId().equals("MyApp")); + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java deleted file mode 100644 index cb252a2..0000000 --- a/src/test/java/org/uic/barcode/test/DynamicFrameFcb3Test.java +++ /dev/null @@ -1,175 +0,0 @@ -package org.uic.barcode.test; - -import java.io.IOException; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.SecureRandom; -import java.security.Security; -import java.security.SignatureException; -import java.util.zip.DataFormatException; - -import org.bouncycastle.jce.ECNamedCurveTable; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.jce.spec.ECParameterSpec; -import org.junit.Before; -import org.junit.Test; -import org.uic.barcode.Decoder; -import org.uic.barcode.Encoder; -import org.uic.barcode.dynamicFrame.Constants; -import org.uic.barcode.test.utils.SimpleUICTestTicket; -import org.uic.barcode.ticket.EncodingFormatException; -import org.uic.barcode.ticket.api.spec.IUicRailTicket; - -public class DynamicFrameFcb3Test { - - public String signatureAlgorithmOID = null; - public String elipticCurve = null; - public String keyPairAlgorithmOID = null; - - public KeyPair keyPair = null; - - public IUicRailTicket testFCBticket = null; - - - @Before public void initialize() { - - signatureAlgorithmOID = Constants.ECDSA_SHA256; - keyPairAlgorithmOID = Constants.KG_EC_256; - elipticCurve = "secp256k1"; - - testFCBticket = SimpleUICTestTicket.getUicTestTicket(); - - Security.addProvider(new BouncyCastleProvider()); - - try { - keyPair = generateECKeys(Constants.KG_EC, elipticCurve); - //keyPair = generateECDSAKeys("ECDSA", "B-571"); - } catch (Exception e) { - assert(false); - } - - assert(keyPair != null); - - } - - - @Test public void testDynamicHeaderBarcodeEncodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } catch (IOException | EncodingFormatException e1) { - assert(false); - } - - assert(enc != null); - - try { - enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); - } catch (Exception e) { - assert(false); - } - - - byte[] encoded = null; - try { - encoded = enc.encode(); - } catch (Exception e) { - assert(false); - } - - assert(encoded != null); - - - - } - - @Test public void testDynamicHeaderBarcodeDecodingFCB3() { - - IUicRailTicket ticket = testFCBticket; - - Encoder enc = null; - - try { - enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); - } catch (IOException | EncodingFormatException e1) { - assert(false); - } - - assert(enc != null); - - try { - enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); - } catch (Exception e) { - assert(false); - } - - - byte[] encoded = null; - try { - encoded = enc.encode(); - } catch (Exception e) { - assert(false); - } - - assert(encoded != null); - - Decoder dec = null; - try { - dec = new Decoder(encoded); - } catch (IOException e) { - assert(false); - } catch (EncodingFormatException e) { - assert(false); - } catch (DataFormatException e) { - assert(false); - } - assert(dec != null); - - int signatureCheck = 0; - try { - signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); - } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException - | UnsupportedOperationException | IOException | EncodingFormatException e) { - assert(false); - } - - assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); - - assert(dec.getDynamicHeader().getFormat().equals("U1")); - - assert(dec.getDynamicHeader().getDynamicDataFDC1().getDataType().getFormat().equals("FCB3")); - - - SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); - - - - - } - - public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ - - String keyAlgorithmName = "ECDSA"; - ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); - KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); - g.initialize(ecSpec, new SecureRandom()); - return g.generateKeyPair(); - } - - -} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java new file mode 100644 index 0000000..63ba68e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion1Test.java @@ -0,0 +1,174 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcbVersion1Test { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPair = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateECKeys(Constants.KG_EC, elipticCurve); + //keyPair = generateECDSAKeys("ECDSA", "B-571"); + } catch (Exception e) { + assert(false); + } + + assert(keyPair != null); + + } + + + @Test public void testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + } + + @Test public void testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 1); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB1")); + + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + + + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java new file mode 100644 index 0000000..4f0897e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameFcbVersion3Test.java @@ -0,0 +1,174 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.jce.spec.ECParameterSpec; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.Decoder; +import org.uic.barcode.Encoder; +import org.uic.barcode.dynamicFrame.Constants; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; + +public class DynamicFrameFcbVersion3Test { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPair = null; + + public IUicRailTicket testFCBticket = null; + + + @Before public void initialize() { + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256k1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + Security.addProvider(new BouncyCastleProvider()); + + try { + keyPair = generateECKeys(Constants.KG_EC, elipticCurve); + //keyPair = generateECDSAKeys("ECDSA", "B-571"); + } catch (Exception e) { + assert(false); + } + + assert(keyPair != null); + + } + + + @Test public void testDynamicHeaderBarcodeEncodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + + } + + @Test public void testDynamicHeaderBarcodeDecodingFCB3() { + + IUicRailTicket ticket = testFCBticket; + + Encoder enc = null; + + try { + enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 3); + } catch (IOException | EncodingFormatException e1) { + assert(false); + } + + assert(enc != null); + + try { + enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + + byte[] encoded = null; + try { + encoded = enc.encode(); + } catch (Exception e) { + assert(false); + } + + assert(encoded != null); + + Decoder dec = null; + try { + dec = new Decoder(encoded); + } catch (IOException e) { + assert(false); + } catch (EncodingFormatException e) { + assert(false); + } catch (DataFormatException e) { + assert(false); + } + assert(dec != null); + + int signatureCheck = 0; + try { + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null); + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException + | UnsupportedOperationException | IOException | EncodingFormatException e) { + assert(false); + } + + assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK); + + assert(dec.getDynamicHeader().getFormat().equals("U1")); + + assert(dec.getDynamicHeader().getLevel2SignedData().getLevel1Data().getData().get(0).getFormat().equals("FCB3")); + + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + + + + } + + public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{ + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + String keyAlgorithmName = "ECDSA"; + ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve); + KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC"); + g.initialize(ecSpec, new SecureRandom()); + return g.generateKeyPair(); + } + + +} -- cgit v1.2.3