From be916e97d76499be3d907cd68b78b7ae8c69f273 Mon Sep 17 00:00:00 2001 From: Patrik Dufresne Date: Sun, 3 Apr 2016 14:33:36 -0400 Subject: [PATCH] Fix some validation. --- .../license/EncryptionManager.java | 6 ------ .../patrikdufresne/license/LicenseManager.java | 16 +++++++++++++--- .../license/LicenseManagerTest.java | 4 +++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/patrikdufresne/license/EncryptionManager.java b/src/main/java/com/patrikdufresne/license/EncryptionManager.java index 9b3ea79..3e10c86 100644 --- a/src/main/java/com/patrikdufresne/license/EncryptionManager.java +++ b/src/main/java/com/patrikdufresne/license/EncryptionManager.java @@ -25,9 +25,7 @@ import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; -import java.security.Provider; import java.security.PublicKey; -import java.security.Security; import java.security.Signature; import java.security.SignatureException; import java.security.spec.InvalidKeySpecException; @@ -169,10 +167,6 @@ public class EncryptionManager { throw new UnsupportedOperationException("Can't sign when the private key is not available."); } - for (Provider p : Security.getProviders()) { - System.out.println(p.getName()); - } - // Initialize the signing algorithm with our private key Signature rsaSignature = Signature.getInstance("SHA1withRSA"); rsaSignature.initSign(privateKey); diff --git a/src/main/java/com/patrikdufresne/license/LicenseManager.java b/src/main/java/com/patrikdufresne/license/LicenseManager.java index 8e69b8a..cfafeb8 100644 --- a/src/main/java/com/patrikdufresne/license/LicenseManager.java +++ b/src/main/java/com/patrikdufresne/license/LicenseManager.java @@ -70,6 +70,9 @@ public class LicenseManager { // Validate each license file. LicenseException lastException = null; for (File f : files) { + if (!f.exists()) { + continue; + } License license; try { license = licenseManager.readLicenseFile(f); @@ -171,8 +174,15 @@ public class LicenseManager { * @throws ClassNotFoundException * if the implementation of {@link License} stored in the file * can't be found + * @throws LicenseException */ - public License readLicenseFile(File file) throws IOException, InvalidKeyException, NoSuchAlgorithmException, SignatureException, ClassNotFoundException { + public License readLicenseFile(File file) + throws IOException, + InvalidKeyException, + NoSuchAlgorithmException, + SignatureException, + ClassNotFoundException, + LicenseException { String base64Signature = null; // Read the license file as a property file. @@ -189,7 +199,7 @@ public class LicenseManager { } // Check if the signature is available. if (base64Signature == null) { - throw new SignatureException("No signature was found"); + throw new LicenseException("No signature was found"); } byte[] sig = Base64.decode(base64Signature.getBytes()); @@ -198,7 +208,7 @@ public class LicenseManager { // Validate the signature if (!encryptionManager.verify(data, sig)) { - return null; + throw new LicenseException("invalid license signature"); } return lic; diff --git a/src/test/java/com/patrikdufresne/license/LicenseManagerTest.java b/src/test/java/com/patrikdufresne/license/LicenseManagerTest.java index 8841600..228a362 100644 --- a/src/test/java/com/patrikdufresne/license/LicenseManagerTest.java +++ b/src/test/java/com/patrikdufresne/license/LicenseManagerTest.java @@ -88,6 +88,7 @@ public class LicenseManagerTest { * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws ClassNotFoundException + * @throws LicenseException */ @Test public void readLicense_WithValidFile_ReadLicense() @@ -95,7 +96,8 @@ public class LicenseManagerTest { NoSuchAlgorithmException, SignatureException, IOException, - ClassNotFoundException { + ClassNotFoundException, + LicenseException { License license = new License(); File file = new File("unittest2.lic"); manager.writeLicense(license, file); -- GitLab