From e6e4d8732d75ab521e4ea36d2d25bc6a97791500 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 24 May 2022 09:29:57 +0200 Subject: additional test --- .../java/org/uic/barcode/utils/ECKeyEncoder.java | 12 +- .../org/uic/barcode/utils/EllipticCurveNames.java | 1 + ...micFrameDoubleSignatureCompressedCurveTest.java | 168 +++++++++++++++++++++ 3 files changed, 175 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java diff --git a/src/main/java/org/uic/barcode/utils/ECKeyEncoder.java b/src/main/java/org/uic/barcode/utils/ECKeyEncoder.java index 705b178..e1662a7 100644 --- a/src/main/java/org/uic/barcode/utils/ECKeyEncoder.java +++ b/src/main/java/org/uic/barcode/utils/ECKeyEncoder.java @@ -97,8 +97,7 @@ public class ECKeyEncoder { //we need to know the curve! String curveName = EllipticCurveNames.getInstance().getName(oid); - - //get the curve parameters + AlgorithmParameters parameters = AlgorithmParameters.getInstance(keyAlgName, provider); parameters.init(new ECGenParameterSpec(curveName)); ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class); @@ -107,7 +106,7 @@ public class ECKeyEncoder { byte[] uncompressed = decompressPubkey(keyBytes, ecParameters); //decode the uncompressed key - return fromUncompressedPoint(uncompressed, ecParameters); + return fromUncompressedPoint(uncompressed, ecParameters, provider); } catch (Exception e) { key = null; @@ -130,7 +129,7 @@ public class ECKeyEncoder { ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class); //decode the uncompressed key - return fromUncompressedPoint(keyBytes, ecParameters); + return fromUncompressedPoint(keyBytes, ecParameters, provider); } catch(Exception e) { //failed @@ -148,6 +147,7 @@ public class ECKeyEncoder { * * @param key the public key * @param encoding the encoding ("X509","X962_UNCOMPRESSED","X962_COMPRESSED") + * @param provider * @return the encoded key */ public static byte[] getEncoded(PublicKey key, String encoding){ @@ -206,7 +206,7 @@ public class ECKeyEncoder { * @throws Exception the exception */ private static ECPublicKey fromUncompressedPoint( - final byte[] encoded, final ECParameterSpec params) + final byte[] encoded, final ECParameterSpec params, Provider provider) throws Exception { int offset = 0; @@ -226,7 +226,7 @@ public class ECKeyEncoder { final BigInteger y = new BigInteger(1, Arrays.copyOfRange(encoded, offset, offset + keySizeBytes)); final ECPoint w = new ECPoint(x, y); final ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(w, params); - final KeyFactory keyFactory = KeyFactory.getInstance("EC"); + final KeyFactory keyFactory = KeyFactory.getInstance("EC",provider); return (ECPublicKey) keyFactory.generatePublic(ecPublicKeySpec); } diff --git a/src/main/java/org/uic/barcode/utils/EllipticCurveNames.java b/src/main/java/org/uic/barcode/utils/EllipticCurveNames.java index 41353b1..706d3c3 100644 --- a/src/main/java/org/uic/barcode/utils/EllipticCurveNames.java +++ b/src/main/java/org/uic/barcode/utils/EllipticCurveNames.java @@ -39,6 +39,7 @@ public class EllipticCurveNames { me.oitToName.put("1.3.132.0.38", "sect571k1"); me.oitToName.put("1.3.132.0.39", "sect571r1"); me.oitToName.put("1.3.132.0.10", "secp256k1"); + me.oitToName.put("1.2.840.10045.3.1.7", "secp256r1"); } return me; diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java new file mode 100644 index 0000000..3a3f7d2 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureCompressedCurveTest.java @@ -0,0 +1,168 @@ +package org.uic.barcode.test; + +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.Provider; +import java.security.SecureRandom; +import java.security.Security; +import java.security.SignatureException; +import java.security.spec.ECGenParameterSpec; +import java.util.Arrays; +import java.util.zip.DataFormatException; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +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.dynamicFrame.api.IData; +import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.test.utils.Level2TestDataFactory; +import org.uic.barcode.test.utils.SimpleUICTestTicket; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.utils.ECKeyEncoder; +import org.uic.barcode.utils.SecurityUtils; + +public class DynamicFrameDoubleSignatureCompressedCurveTest { + + public String signatureAlgorithmOID = null; + public String elipticCurve = null; + public String keyPairAlgorithmOID = null; + + public KeyPair keyPairLevel1 = null; + public KeyPair keyPairLevel2 = null; + + public IUicRailTicket testFCBticket = null; + + public Provider provider = null; + + + @Before public void initialize() { + + LoggerFactory.setActivateConsoleLog(true); + + signatureAlgorithmOID = Constants.ECDSA_SHA256; + keyPairAlgorithmOID = Constants.KG_EC_256; + elipticCurve = "secp256r1"; + + testFCBticket = SimpleUICTestTicket.getUicTestTicket(); + + provider = new BouncyCastleProvider(); + 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 testDynamicHeaderBarcodeDecoding() { + + 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); + + try { + enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID); + enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic(), ECKeyEncoder.ENCODING_X962_COMPRESSED); + enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1"); + } catch (Exception e) { + assert(false); + } + + assert(enc != null); + + + IData level2Data = Level2TestDataFactory.getLevel2SimpleTestData(); + try { + enc.setLevel2Data(level2Data); + 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); + + signatureCheck = 0; + try { + signatureCheck = dec.validateLevel2(); + } catch (Exception e) { + assert(false); + } + assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK); + + IData level2DataDec = dec.getLevel2Data(); + + assert(level2Data.getFormat().equals(level2DataDec.getFormat())); + assert(Arrays.equals(level2Data.getData(),level2DataDec.getData())); + + SimpleUICTestTicket.compare(ticket, dec.getUicTicket()); + + } + + + public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{ + + //ECNamedCurveGenParameterSpec namedParamSpec = new ECNamedCurveGenParameterSpec(elipticCurve); + + ECGenParameterSpec namedParamSpec = new ECGenParameterSpec(elipticCurve); + KeyPairGenerator ecKPGen = KeyPairGenerator.getInstance("EC", "BC"); + ecKPGen.initialize(namedParamSpec, new SecureRandom()); + KeyPair keyPair = ecKPGen.generateKeyPair(); + KeyPair kp = new KeyPair(SecurityUtils.convert(keyPair.getPublic(), provider),SecurityUtils.convert(keyPair.getPrivate(), provider)); + return kp; + } + + +} -- cgit v1.2.3