From 58bafb860b060c609a01815657b4df22ce8fbffc Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 12 Apr 2022 12:54:16 +0200 Subject: test on algorithm name resolver --- .../dynamicFrame/api/SimpleDynamicFrame.java | 15 +++--- .../uic/barcode/dynamicFrame/v2/DynamicFrame.java | 56 --------------------- .../barcode/ticket/api/impl/SimpleTraveler.java | 2 +- .../ticket/api/utils/OpenAsn2ApiDecoder.java | 4 +- .../ticket/api/utils/OpenAsn2ApiDecoderV2.java | 4 +- .../ticket/api/utils/OpenAsn2ApiDecoderV3.java | 6 ++- .../java/org/uic/barcode/utils/SecurityUtils.java | 57 +++++++++++++++++----- 7 files changed, 63 insertions(+), 81 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java index a8d7a0f..59ccd52 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java @@ -150,6 +150,7 @@ public class SimpleDynamicFrame implements IDynamicFrame { } String level2KeyAlg = getLevel2Data().getLevel1Data().getLevel2KeyAlg(); + String level2SigAlg = this.getLevel2Data().getLevel1Data().getLevel2SigningAlg(); if (level2KeyAlg == null || level2KeyAlg.length() == 0) { @@ -173,16 +174,13 @@ public class SimpleDynamicFrame implements IDynamicFrame { PublicKey key = null; try { byte[] keyBytes = this.getLevel2Data().getLevel1Data().getLevel2publicKey(); - X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); - KeyFactory keyFactory = null; if (provider == null) { - keyFactory = SecurityUtils.findKeyFactory(level2KeyAlg, keyBytes); - provider = keyFactory.getProvider(); - } else { - keyFactory = KeyFactory.getInstance(keyAlgName,provider); - } + provider = SecurityUtils.findPublicKeyProvider(level2KeyAlg,keyBytes); + } + KeyFactory keyFactory = KeyFactory.getInstance(keyAlgName,provider); if (keyFactory != null) { + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); key = keyFactory.generatePublic(keySpec); } else { return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED; @@ -194,9 +192,8 @@ public class SimpleDynamicFrame implements IDynamicFrame { return Constants.LEVEL2_VALIDATION_KEY_ALG_NOT_IMPLEMENTED; } - //find the algorithm name for the signature OID - String level2SigAlg = this.getLevel2Data().getLevel1Data().getLevel2SigningAlg(); + //find the algorithm name for the signature OID String sigAlgName = null; try { sigAlgName = AlgorithmNameResolver.getSignatureAlgorithmName(level2SigAlg,provider); diff --git a/src/main/java/org/uic/barcode/dynamicFrame/v2/DynamicFrame.java b/src/main/java/org/uic/barcode/dynamicFrame/v2/DynamicFrame.java index 55af066..30043c0 100644 --- a/src/main/java/org/uic/barcode/dynamicFrame/v2/DynamicFrame.java +++ b/src/main/java/org/uic/barcode/dynamicFrame/v2/DynamicFrame.java @@ -123,43 +123,6 @@ public class DynamicFrame extends Object{ } - /** - * Sign level 2 data without a specific security provider. - * - * @param key the key - * @throws Exception the exception - */ - public void signLevel2(PrivateKey key) throws Exception { - - //find the algorithm name for the signature OID - String algo = AlgorithmNameResolver.getSignatureAlgorithmName(this.getLevel2SignedData().getLevel1Data().level2SigningAlg); - Signature sig = Signature.getInstance(algo); - sig.initSign(key); - byte[] data = level2SignedData.encode(); - sig.update(data); - byte[] signature = sig.sign(); - this.level2Signature = new OctetString(signature); - - } - - /** - * Sign level 2 data. - * - * @param key the key - * @param prov the security Provider - * @throws Exception the exception - */ - public void signLevel2(PrivateKey key, Provider prov) throws Exception { - - //find the algorithm name for the signature OID - String algo = AlgorithmNameResolver.getSignatureAlgorithmName(this.getLevel2SignedData().getLevel1Data().level2SigningAlg); - Signature sig = Signature.getInstance(algo,prov); - sig.initSign(key); - byte[] data = level2SignedData.encode(); - sig.update(data); - this.level2Signature = new OctetString(sig.sign()); - - } /** @@ -207,24 +170,5 @@ public class DynamicFrame extends Object{ } - /** - * Gets the dynamic data FDC 1. - * - * @return the dynamic data FDC 1 - */ - public UicDynamicContentDataFDC1 getDynamicDataFDC1() { - - if (this.getLevel2SignedData() == null || - this.getLevel2SignedData().getLevel2Data() == null){ - return null; - } - - if ( UicDynamicContentDataFDC1.getFormat().equals(this.getLevel2SignedData().getLevel2Data().getFormat())) { - return UperEncoder.decode(this.getLevel2SignedData().getLevel2Data().getByteData(), UicDynamicContentDataFDC1.class); - } - return null; - - } - } diff --git a/src/main/java/org/uic/barcode/ticket/api/impl/SimpleTraveler.java b/src/main/java/org/uic/barcode/ticket/api/impl/SimpleTraveler.java index afc351d..e7edd45 100644 --- a/src/main/java/org/uic/barcode/ticket/api/impl/SimpleTraveler.java +++ b/src/main/java/org/uic/barcode/ticket/api/impl/SimpleTraveler.java @@ -53,7 +53,7 @@ public class SimpleTraveler implements ITraveler { protected IPassengerType passengerType; /** The passenger with reduced mobility. */ - protected boolean passengerWithReducedMobility; + protected Boolean passengerWithReducedMobility; /** The country of residence. */ protected int countryOfResidence = 0; diff --git a/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoder.java b/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoder.java index 031d5b3..f4e041f 100644 --- a/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoder.java +++ b/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoder.java @@ -356,12 +356,14 @@ public class OpenAsn2ApiDecoder implements Asn2ApiDecoder { private IDelayConfirmation convertDelayConfirmation(DelayConfirmation asnDocument, Date issuingDate) { IDelayConfirmation document = factory.createDelayConfirmation(); - document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum().longValue(),asnDocument.getReferenceIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); document.setExtension(convertExtension(asnDocument.getExtension())); document.setInfoText(asnDocument.getInfoText()); + document.setTrain(UicEncoderUtils.mapToString(asnDocument.getTrainNum(), asnDocument.getTrainIA5())); + if (asnDocument.getStationCodeTable()!=null){ document.setStationCodeTable(IStationCodeTable.valueOf(asnDocument.getStationCodeTable().name())); } diff --git a/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV2.java b/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV2.java index 1d50cb8..57d2366 100644 --- a/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV2.java +++ b/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV2.java @@ -358,12 +358,14 @@ public class OpenAsn2ApiDecoderV2 implements Asn2ApiDecoder { private IDelayConfirmation convertDelayConfirmation(DelayConfirmation asnDocument, Date issuingDate) { IDelayConfirmation document = factory.createDelayConfirmation(); - document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum().longValue(),asnDocument.getReferenceIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); document.setExtension(convertExtension(asnDocument.getExtension())); document.setInfoText(asnDocument.getInfoText()); + document.setTrain(UicEncoderUtils.mapToString(asnDocument.getTrainNum(), asnDocument.getTrainIA5())); + if (asnDocument.getStationCodeTable()!=null){ document.setStationCodeTable(IStationCodeTable.valueOf(asnDocument.getStationCodeTable().toString())); } diff --git a/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV3.java b/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV3.java index 20d2319..9521858 100644 --- a/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV3.java +++ b/src/main/java/org/uic/barcode/ticket/api/utils/OpenAsn2ApiDecoderV3.java @@ -361,12 +361,14 @@ public class OpenAsn2ApiDecoderV3 implements Asn2ApiDecoder { private IDelayConfirmation convertDelayConfirmation(DelayConfirmation asnDocument, Date issuingDate) { IDelayConfirmation document = factory.createDelayConfirmation(); - document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum().longValue(),asnDocument.getReferenceIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); document.setExtension(convertExtension(asnDocument.getExtension())); - document.setInfoText(asnDocument.getInfoText()); + document.setInfoText(asnDocument.getInfoText()); + document.setTrain(UicEncoderUtils.mapToString(asnDocument.getTrainNum(), asnDocument.getTrainIA5())); + if (asnDocument.getStationCodeTable()!=null){ document.setStationCodeTable(IStationCodeTable.valueOf(asnDocument.getStationCodeTable().toString())); } diff --git a/src/main/java/org/uic/barcode/utils/SecurityUtils.java b/src/main/java/org/uic/barcode/utils/SecurityUtils.java index af1a65a..1fcc18a 100644 --- a/src/main/java/org/uic/barcode/utils/SecurityUtils.java +++ b/src/main/java/org/uic/barcode/utils/SecurityUtils.java @@ -11,22 +11,30 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; +/** + * The Class SecurityUtils. + */ public class SecurityUtils { - public static KeyFactory findKeyFactory(String oid, byte[] keyBytes) { + /** + * Find provider by public key. + * + * @param algorithmOid the algorithm oid used to generate the key + * @param keyBytes the encoded bytes of the public key + * @return the provider + */ + public static Provider findPublicKeyProvider(String keyAlgorithmOid, byte[] keyBytes) { + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); - String name = null; + String name; try { - name = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, oid); + name = AlgorithmNameResolver.getAlgorithmName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, null); } catch (Exception e2) { return null; } - if (name == null || name.length() == 0) { - return null; - } - + KeyFactory keyFactory = null; Provider[] provs = Security.getProviders(); @@ -39,17 +47,23 @@ public class SecurityUtils { if (keyFactory != null) { try { keyFactory.generatePublic(keySpec); - return keyFactory; + return provider; } catch (Exception e) { + provider = null; //try next } } } - return null; - + + return null; } - + /** + * Find private key provider. + * + * @param key the private key + * @return the provider + */ public static Provider findPrivateKeyProvider(PrivateKey key) { String name = key.getAlgorithm(); @@ -84,6 +98,13 @@ public class SecurityUtils { + /** + * Convert. + * + * @param key the key + * @param provider the provider + * @return the public key + */ public static PublicKey convert(PublicKey key, Provider provider) { PublicKey publicKey; @@ -108,6 +129,13 @@ public class SecurityUtils { } + /** + * Convert. + * + * @param key the key + * @param provider the provider + * @return the private key + */ public static PrivateKey convert(PrivateKey key, Provider provider) { PrivateKey privateKey; @@ -131,6 +159,13 @@ public class SecurityUtils { } + /** + * Find signature provider. + * + * @param encoded the encoded + * @param oid the oid + * @return the provider + */ public static Provider findSignatureProvider(byte[] encoded, String oid) { KeyFactory keyFactory = null; -- cgit v1.2.3