Skip to content

Commit

Permalink
Merge pull request #80 from UnionInternationalCheminsdeFer/fixing_dsa…
Browse files Browse the repository at this point in the history
…_validation_without_algorithm

tests changed to the functions including a dedicated provider
  • Loading branch information
CGantert345 authored May 21, 2024
2 parents 1d4cd17 + b6dc308 commit 15bae01
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/uic/barcode/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;


/**
Expand Down Expand Up @@ -165,6 +166,7 @@ public int validateLevel1(PublicKey key, String signingAlg, Provider provider) t

} 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 {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/uic/barcode/staticFrame/StaticFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ public boolean verifyByAlgorithmOid(PublicKey key, String signingAlg) throws Inv
* @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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -135,7 +135,7 @@ public class DynamicFrameDoubleSignatureBCelipticTest2 {

signatureCheck = 0;
try {
signatureCheck = dec.validateLevel2();
signatureCheck = dec.validateLevel2(provider);
} catch (Exception e) {
assert(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class StaticFrameBarcodeTestFCB3 {

public TicketLayout testLayout = null;

public Provider provider = null;


/**
* Initialize.
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +11,8 @@
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;
Expand Down Expand Up @@ -73,7 +76,7 @@ public class SecurityProviderTestV1 {
}

assert(keyPair != null);

}


Expand Down

0 comments on commit 15bae01

Please sign in to comment.