Added missing checks.
authorMarco Zanon <info@marcozanon.com>
Sat, 2 Mar 2024 17:06:50 +0000 (17:06 +0000)
committerMarco Zanon <info@marcozanon.com>
Sat, 2 Mar 2024 17:06:50 +0000 (17:06 +0000)
CHANGELOG
src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java

index 847a834b8560f70042a1ad0e144eae1cee2c1bbd..5ca2149bd5576da30a79706840f52dce7a448b5c 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -5,6 +5,7 @@ See LICENSE for details.
 ------------------
 9.2.1 (2024-03-02)
 ------------------
+* Added missing checks.
 * Fixed a bug which prevented full license files to be written properly.
 
 ------------------
index 8020158e8278b70f0c6dfa5e5e0e91e754b09105..7969d2bd2f31e25c7c3e3858753d45e369360e10 100644 (file)
@@ -10,6 +10,7 @@ import com.marcozanon.macaco.MConstants;
 import com.marcozanon.macaco.json.MInvalidJsonValueException;
 import com.marcozanon.macaco.json.MJsonObject;
 import com.marcozanon.macaco.json.MJsonString;
+import com.marcozanon.macaco.text.MText;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -32,6 +33,16 @@ public class MLicenseManager {
     /* License generation. */
 
     public static void generateFullLicense(Path privateKeyFile, Path licenseSkeletonFile, Path fullLicenseFile) throws MLicensingException {
+        if (null == privateKeyFile) {
+            throw new IllegalArgumentException("Invalid 'privateKeyFile': null.");
+        }
+        if (null == licenseSkeletonFile) {
+            throw new IllegalArgumentException("Invalid 'licenseSkeletonFile': null.");
+        }
+        if (null == fullLicenseFile) {
+            throw new IllegalArgumentException("Invalid 'fullLicenseFile': null.");
+        }
+        //
         try {
             String privateKeyPemString = new String(Files.readAllBytes(privateKeyFile), MConstants.DEFAULT_CHARSET);
             String licenseSkeletonJsonString = new String(Files.readAllBytes(licenseSkeletonFile), MConstants.DEFAULT_CHARSET);
@@ -46,6 +57,13 @@ public class MLicenseManager {
     }
 
     public static MJsonObject generateFullLicense(String privateKeyPemString, String licenseSkeletonJsonString) throws MLicensingException {
+        if (MText.isBlank(privateKeyPemString)) {
+            throw new IllegalArgumentException("Invalid 'privateKeyPemString': null or empty.");
+        }
+        if (MText.isBlank(licenseSkeletonJsonString)) {
+            throw new IllegalArgumentException("Invalid 'licenseSkeletonJsonString': null or empty.");
+        }
+        //
         try {
             byte[] privateKeyContent = Base64.getDecoder().decode(privateKeyPemString.replace("-----BEGIN PRIVATE KEY-----", "").replaceAll("\\R", "").replace("-----END PRIVATE KEY-----", ""));
             RSAPrivateKey privateKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyContent));
@@ -84,6 +102,13 @@ public class MLicenseManager {
     /* License verification. */
 
     public static MJsonObject verifyFullLicense(Path publicKeyFile, Path fullLicenseFile) throws MLicensingException {
+        if (null == publicKeyFile) {
+            throw new IllegalArgumentException("Invalid 'publicKeyFile': null.");
+        }
+        if (null == fullLicenseFile) {
+            throw new IllegalArgumentException("Invalid 'fullLicenseFile': null.");
+        }
+        //
         try {
             String publicKeyPemString = new String(Files.readAllBytes(publicKeyFile), MConstants.DEFAULT_CHARSET);
             String fullLicenseJsonString = new String(Files.readAllBytes(fullLicenseFile), MConstants.DEFAULT_CHARSET);
@@ -96,6 +121,13 @@ public class MLicenseManager {
     }
 
     public static MJsonObject verifyFullLicense(String publicKeyPemString, String fullLicenseJsonString) throws MLicensingException {
+        if (MText.isBlank(publicKeyPemString)) {
+            throw new IllegalArgumentException("Invalid 'publicKeyPemString': null or empty.");
+        }
+        if (MText.isBlank(fullLicenseJsonString)) {
+            throw new IllegalArgumentException("Invalid 'fullLicenseJsonString': null or empty.");
+        }
+        //
         try {
             byte[] publicKeyContent = Base64.getDecoder().decode(publicKeyPemString.replace("-----BEGIN PUBLIC KEY-----", "").replaceAll("\\R", "").replace("-----END PUBLIC KEY-----", ""));
             RSAPublicKey publicKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyContent));