Skip to content

Commit 58dfba9

Browse files
authored
Merge pull request #149 from UnionInternationalCheminsdeFer/revert-106-remove-provider
Revert "remove default provider and use proper provider detection"
2 parents 867b3e8 + feaa760 commit 58dfba9

File tree

6 files changed

+101
-15
lines changed

6 files changed

+101
-15
lines changed

src/main/java/org/uic/barcode/Decoder.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.uic.barcode.ticket.EncodingFormatException;
2626
import org.uic.barcode.ticket.UicRailTicketCoder;
2727
import org.uic.barcode.ticket.api.spec.IUicRailTicket;
28+
import org.uic.barcode.utils.SecurityUtils;
2829

2930

3031
/**
@@ -56,6 +57,17 @@ public class Decoder {
5657

5758
/** The data. */
5859
byte[] data = null;
60+
61+
private Provider defaultProvider = null;
62+
63+
64+
public Provider getDefaultProvider() {
65+
return defaultProvider;
66+
}
67+
68+
public void setDefaultProvider(Provider defaultProvider) {
69+
this.defaultProvider = defaultProvider;
70+
}
5971

6072
/**
6173
* Instantiates a new decoder.
@@ -68,6 +80,10 @@ public class Decoder {
6880
public Decoder (byte[] data) throws IOException, EncodingFormatException, DataFormatException {
6981
this.data = data;
7082

83+
if (defaultProvider == null) {
84+
defaultProvider = SecurityUtils.getDefaultProvider();
85+
}
86+
7187
decode(data);
7288
}
7389

@@ -87,7 +103,7 @@ public Decoder (byte[] data) throws IOException, EncodingFormatException, DataFo
87103
* @deprecated
88104
*/
89105
public int validateLevel1(PublicKey key) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
90-
return validateLevel1(key,null, null);
106+
return validateLevel1(key,null, defaultProvider);
91107
}
92108

93109
/**
@@ -106,7 +122,7 @@ public int validateLevel1(PublicKey key) throws InvalidKeyException, NoSuchAlgor
106122
* @deprecated
107123
*/
108124
public int validateLevel1(PublicKey key, String signingAlg) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
109-
return validateLevel1(key, signingAlg, null);
125+
return validateLevel1(key, signingAlg, defaultProvider);
110126
}
111127

112128
/**
@@ -155,7 +171,7 @@ public int validateLevel1(PublicKey key, String signingAlg, Provider provider) t
155171
* @deprecated
156172
*/
157173
public int validateLevel2() throws EncodingFormatException {
158-
return validateLevel2(null);
174+
return validateLevel2(defaultProvider);
159175
}
160176

161177
/*

src/main/java/org/uic/barcode/Encoder.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.uic.barcode.ticket.UicRailTicketCoder;
3030
import org.uic.barcode.ticket.api.spec.IUicRailTicket;
3131
import org.uic.barcode.utils.ECKeyEncoder;
32+
import org.uic.barcode.utils.SecurityUtils;
33+
3234

3335
/**
3436
* The Class Encoder.
@@ -58,6 +60,17 @@ public class Encoder {
5860
/** The UIC bar code type SSB. */
5961
public static String UIC_BARCODE_TYPE_SSB = "UIC_SSB";
6062

63+
private Provider defaultProvider = null;
64+
65+
66+
public Provider getDefaultProvider() {
67+
return defaultProvider;
68+
}
69+
70+
public void setDefaultProvider(Provider defaultProvider) {
71+
this.defaultProvider = defaultProvider;
72+
}
73+
6174
/**
6275
* Instantiates a new encoder.
6376
*
@@ -71,6 +84,10 @@ public class Encoder {
7184
*/
7285
public Encoder(IUicRailTicket ticket, TicketLayout layout, String barcodeType, int version, int fcbVersion) throws IOException, EncodingFormatException {
7386

87+
if (defaultProvider == null) {
88+
defaultProvider = SecurityUtils.getDefaultProvider();
89+
}
90+
7491
if (barcodeType == UIC_BARCODE_TYPE_CLASSIC) {
7592

7693
staticFrame = new StaticFrame();
@@ -143,6 +160,10 @@ public Encoder(IUicRailTicket ticket, TicketLayout layout, String barcodeType, i
143160
* @throws EncodingFormatException the encoding format exception
144161
*/
145162
public Encoder(byte[] level1DataBin, byte[] signatureLevel1, int version) throws IOException, EncodingFormatException {
163+
164+
if (defaultProvider == null) {
165+
defaultProvider = SecurityUtils.getDefaultProvider();
166+
}
146167

147168
dynamicFrame = new SimpleDynamicFrame();
148169
dynamicFrame.setLevel2Data(new SimpleLevel2Data());
@@ -187,6 +208,10 @@ public Encoder(byte[] level1DataBin, byte[] signatureLevel1, int version) throws
187208
*/
188209
public Encoder(byte[] encoded, int version) throws IOException, EncodingFormatException, DataFormatException {
189210

211+
if (defaultProvider == null) {
212+
defaultProvider = SecurityUtils.getDefaultProvider();
213+
}
214+
190215
Decoder decoder = new Decoder(encoded);
191216

192217
if (decoder.getDynamicFrame() == null) {
@@ -235,7 +260,7 @@ public Encoder(byte[] encoded, int version) throws IOException, EncodingFormatEx
235260
* @deprecated
236261
*/
237262
public void signLevel2(PrivateKey key) throws Exception {
238-
signLevel2(key, null);
263+
signLevel2(key, defaultProvider);
239264
}
240265

241266
/**
@@ -367,7 +392,7 @@ public IUicDynamicContent getDynamicContent() {
367392
* @deprecated
368393
*/
369394
public void signLevel1(String securityProvider,PrivateKey key,String signingAlg, String keyId) throws Exception {
370-
signLevel1(securityProvider,key,signingAlg, keyId, null);
395+
signLevel1(securityProvider,key,signingAlg, keyId, defaultProvider);
371396
}
372397

373398
/**

src/main/java/org/uic/barcode/dynamicFrame/api/SimpleDynamicFrame.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,12 @@ && getLevel2Data().getLevel1Data().getLevel1SigningAlg() != null
306306

307307
Signature sig;
308308
try {
309-
sig = Signature.getInstance(algo, prov);
309+
if (prov != null) {
310+
sig = Signature.getInstance(algo, prov);
311+
} else {
312+
sig = Signature.getInstance(algo);
313+
314+
}
310315
} catch (NoSuchAlgorithmException e) {
311316
return Constants.LEVEL1_VALIDATION_SIG_ALG_NOT_IMPLEMENTED;
312317
}

src/main/java/org/uic/barcode/staticFrame/StaticFrame.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,31 @@ private byte[] trimDsaSignature(byte[] sealdata) throws EncodingFormatException
686686
* @deprecated
687687
*/
688688
public boolean verifyByAlgorithmOid(PublicKey key, String signingAlg) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
689-
return verifyByAlgorithmOid(key, signingAlg, null);
689+
690+
String signatureAlgorithmOid = signingAlg;
691+
692+
693+
// guess the signature algorithm based on the signature size
694+
if ((signingAlg == null || signingAlg.length() < 1) && this.getSignature() != null) {
695+
signatureAlgorithmOid = SecurityUtils.getDsaAlgorithm(this.getSignature());
696+
}
697+
698+
//find the algorithm name for the signature OID
699+
String algo = null;
700+
Provider[] provs = Security.getProviders();
701+
for (Provider prov : provs) {
702+
Service service = prov.getService("Signature",signatureAlgorithmOid);
703+
if (service != null) {
704+
algo = service.getAlgorithm();
705+
}
706+
}
707+
if (algo == null) {
708+
throw new NoSuchAlgorithmException("No service for algorithm found: " + signingAlg);
709+
}
710+
Signature sig = Signature.getInstance(algo);
711+
sig.initVerify(key);
712+
sig.update(getDataForSignature());
713+
return sig.verify(this.getSignature());
690714
}
691715

692716
/**
@@ -762,7 +786,26 @@ public boolean verifyByAlgorithmOid(PublicKey key, String signatureAlgorithmOid,
762786
* @deprecated
763787
*/
764788
public void signByAlgorithmOID(PrivateKey key,String signatureAlgorithmOid) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, IOException, EncodingFormatException {
765-
signByAlgorithmOID(key,signatureAlgorithmOid,null);
789+
//find the algorithm name for the signature OID
790+
//find the algorithm name for the signature OID
791+
String algo = null;
792+
Provider[] provs = Security.getProviders();
793+
for (Provider p : provs) {
794+
if (algo == null) {
795+
Service service = p.getService("Signature",signatureAlgorithmOid);
796+
if (service != null) {
797+
algo = service.getAlgorithm();
798+
}
799+
}
800+
}
801+
if (algo == null) {
802+
throw new NoSuchAlgorithmException("No service for algorithm found: " + signatureAlgorithmOid);
803+
}
804+
Signature sig = Signature.getInstance(algo);
805+
sig.initSign(key);
806+
signedData = buildDataForSignature();
807+
sig.update(signedData);
808+
signature = sig.sign();
766809
}
767810

768811

src/main/java/org/uic/barcode/utils/ECKeyEncoder.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static PublicKey fromEncoded (byte[] keyBytes, String oid, Provider provi
5454

5555
PublicKey key = null;
5656

57-
57+
5858
String keyAlgName = null;
5959
try {
6060
keyAlgName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, oid,provider);
@@ -226,12 +226,7 @@ private static ECPublicKey fromUncompressedPoint(
226226
final BigInteger y = new BigInteger(1, Arrays.copyOfRange(encoded, offset, offset + keySizeBytes));
227227
final ECPoint w = new ECPoint(x, y);
228228
final ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(w, params);
229-
KeyFactory keyFactory;
230-
if (provider != null) {
231-
keyFactory = KeyFactory.getInstance("EC", provider);
232-
} else {
233-
keyFactory = KeyFactory.getInstance("EC");
234-
}
229+
final KeyFactory keyFactory = KeyFactory.getInstance("EC",provider);
235230
return (ECPublicKey) keyFactory.generatePublic(ecPublicKeySpec);
236231
}
237232

src/test/java/org/uic/barcode/ticketTests/SZticketTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.uic.barcode.ticketTests;
22

33
import java.security.KeyFactory;
4+
import java.security.NoSuchAlgorithmException;
45
import java.security.Provider;
56
import java.security.PublicKey;
67
import java.security.Security;
8+
import java.security.spec.InvalidKeySpecException;
79
import java.security.spec.X509EncodedKeySpec;
810
import java.util.Base64;
911
import java.util.TimeZone;

0 commit comments

Comments
 (0)