From b6dc3080f4cc86e26a76dec7c0100dfb04d56664 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 21 May 2024 15:25:16 +0200 Subject: tests changed to the functions including a dedicated provider fixed the validation function in case the algorithmOid is missing --- src/main/java/org/uic/barcode/Decoder.java | 2 ++ src/main/java/org/uic/barcode/staticFrame/StaticFrame.java | 5 +++++ .../java/org/uic/barcode/test/DynamicContentCoderTest.java | 7 +++++++ .../test/DynamicFrameDoubleSignatureBCelipticTest2.java | 8 ++++---- .../StaticFrameBarcodeSignatureAlgorithmDetectionTest.java | 4 ++-- .../org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java | 12 ++++++++---- .../uic/barcode/ticket/api/test/SecurityProviderTestV1.java | 5 ++++- 7 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/uic/barcode/Decoder.java b/src/main/java/org/uic/barcode/Decoder.java index 0878d8d..d9f20ff 100644 --- a/src/main/java/org/uic/barcode/Decoder.java +++ b/src/main/java/org/uic/barcode/Decoder.java @@ -23,6 +23,7 @@ import org.uic.barcode.staticFrame.ticketLayoutBarcode.TicketLayout; import org.uic.barcode.ticket.EncodingFormatException; import org.uic.barcode.ticket.UicRailTicketCoder; import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.utils.SecurityUtils; /** @@ -165,6 +166,7 @@ public class Decoder { } else if (staticFrame != null) { + // guess the signature algorithm based on the signature size if (staticFrame.verifyByAlgorithmOid(key,signingAlg, provider)) { return Constants.LEVEL1_VALIDATION_OK; } else { diff --git a/src/main/java/org/uic/barcode/staticFrame/StaticFrame.java b/src/main/java/org/uic/barcode/staticFrame/StaticFrame.java index bf97acb..a8d7651 100644 --- a/src/main/java/org/uic/barcode/staticFrame/StaticFrame.java +++ b/src/main/java/org/uic/barcode/staticFrame/StaticFrame.java @@ -706,6 +706,11 @@ public class StaticFrame { * @throws IOException */ public boolean verifyByAlgorithmOid(PublicKey key, String signingAlg, Provider prov) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException { + + if ((signingAlg == null || signingAlg.length() < 1) && this.getSignature() != null) { + signingAlg = SecurityUtils.getDsaAlgorithm(this.getSignature()); + } + //find the algorithm name for the signature OID String algo = null; Service service = prov.getService("Signature",signingAlg); diff --git a/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java b/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java index c6a693f..4982536 100644 --- a/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java +++ b/src/test/java/org/uic/barcode/test/DynamicContentCoderTest.java @@ -81,6 +81,13 @@ public class DynamicContentCoderTest { IUicDynamicContent content = DynamicContentCoder.decode(UperEncoder.bytesFromHexString(encoding)); + try { + content.setTimeStamp(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + } catch (ParseException e2) { + // + } + + assert("appID".equals(content.getAppId())); diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureBCelipticTest2.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureBCelipticTest2.java index f5d0729..7d7f550 100644 --- a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureBCelipticTest2.java +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureBCelipticTest2.java @@ -86,7 +86,7 @@ public class DynamicFrameDoubleSignatureBCelipticTest2 { try { enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID); enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic()); - enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1",provider); } catch (Exception e) { assert(false); } @@ -97,7 +97,7 @@ public class DynamicFrameDoubleSignatureBCelipticTest2 { IData level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); try { enc.setLevel2Data(level2Data); - enc.signLevel2(keyPairLevel2.getPrivate()); + enc.signLevel2(keyPairLevel2.getPrivate(),provider); } catch (Exception e) { assert(false); } @@ -126,7 +126,7 @@ public class DynamicFrameDoubleSignatureBCelipticTest2 { int signatureCheck = 0; try { - signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(), null); + signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(), null,provider); } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException | UnsupportedOperationException | IOException | EncodingFormatException e) { assert(false); @@ -135,7 +135,7 @@ public class DynamicFrameDoubleSignatureBCelipticTest2 { signatureCheck = 0; try { - signatureCheck = dec.validateLevel2(); + signatureCheck = dec.validateLevel2(provider); } catch (Exception e) { assert(false); } diff --git a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeSignatureAlgorithmDetectionTest.java b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeSignatureAlgorithmDetectionTest.java index be3db95..d6c2b9c 100644 --- a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeSignatureAlgorithmDetectionTest.java +++ b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeSignatureAlgorithmDetectionTest.java @@ -132,7 +132,7 @@ public class StaticFrameBarcodeSignatureAlgorithmDetectionTest { int signatureCheck = 0; try { - signatureCheck = dec.validateLevel1(keyPair.getPublic()); + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null,prov); } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException | UnsupportedOperationException | IOException | EncodingFormatException e) { assert(false); @@ -298,7 +298,7 @@ public class StaticFrameBarcodeSignatureAlgorithmDetectionTest { int signatureCheck = 0; try { - signatureCheck = dec.validateLevel1(keyPair.getPublic()); + signatureCheck = dec.validateLevel1(keyPair.getPublic(),null,prov); } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException | UnsupportedOperationException | IOException | EncodingFormatException e) { assert(false); diff --git a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java index b519308..1d8de54 100644 --- a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java +++ b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB3.java @@ -7,6 +7,7 @@ import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; +import java.security.Provider; import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; @@ -43,6 +44,8 @@ public class StaticFrameBarcodeTestFCB3 { public TicketLayout testLayout = null; + public Provider provider = null; + /** * Initialize. @@ -61,7 +64,8 @@ public class StaticFrameBarcodeTestFCB3 { testFCBticket = SimpleUICTestTicket.getUicTestTicket(); testLayout = SimpleTestTicketLayout.getSimpleTestTicketLayout(); - Security.addProvider(new BouncyCastleProvider()); + provider = new BouncyCastleProvider(); + Security.addProvider(provider); try { keyPair = generateDSAKeys(keySize); @@ -96,7 +100,7 @@ public class StaticFrameBarcodeTestFCB3 { assert(enc != null); try { - enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1",provider); } catch (Exception e) { assert(false); } @@ -136,7 +140,7 @@ public class StaticFrameBarcodeTestFCB3 { assert(enc != null); try { - enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1"); + enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1",provider); } catch (Exception e) { assert(false); } @@ -165,7 +169,7 @@ public class StaticFrameBarcodeTestFCB3 { int signatureCheck = 0; try { - signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID); + signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID,provider); } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException | UnsupportedOperationException | IOException | EncodingFormatException e) { assert(false); diff --git a/src/test/java/org/uic/barcode/ticket/api/test/SecurityProviderTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/SecurityProviderTestV1.java index 8318467..240907c 100644 --- a/src/test/java/org/uic/barcode/ticket/api/test/SecurityProviderTestV1.java +++ b/src/test/java/org/uic/barcode/ticket/api/test/SecurityProviderTestV1.java @@ -1,5 +1,6 @@ package org.uic.barcode.ticket.api.test; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; @@ -10,6 +11,8 @@ import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; import java.util.zip.DataFormatException; import org.bouncycastle.jce.provider.BouncyCastleProvider; @@ -73,7 +76,7 @@ public class SecurityProviderTestV1 { } assert(keyPair != null); - + } -- cgit v1.2.3