From 5b3042e3bb5bfcbba405329a9d082f9fd96c4350 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Sat, 2 Mar 2024 17:06:50 +0000 Subject: [PATCH] Added missing checks. --- 9.x/CHANGELOG | 1 + .../macaco/licensing/MLicenseManager.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/9.x/CHANGELOG b/9.x/CHANGELOG index 847a834..5ca2149 100644 --- a/9.x/CHANGELOG +++ b/9.x/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. ------------------ diff --git a/9.x/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java b/9.x/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java index 8020158..7969d2b 100644 --- a/9.x/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java +++ b/9.x/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java @@ -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)); -- 2.30.2