From cce10f892a5857758020a398e9b190550cd268c2 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Sat, 2 Mar 2024 17:16:20 +0000 Subject: [PATCH] Undid last commit. --- CHANGELOG | 1 - .../macaco/licensing/MLicenseManager.java | 60 +++++++++---------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2e1eebf..5ca2149 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,7 +5,6 @@ See LICENSE for details. ------------------ 9.2.1 (2024-03-02) ------------------ -* Renamed methods and variables. * Added missing checks. * Fixed a bug which prevented full license files to be written properly. diff --git a/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java b/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java index f99df29..7969d2b 100644 --- a/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java +++ b/src/main/java/com/marcozanon/macaco/licensing/MLicenseManager.java @@ -32,31 +32,31 @@ public class MLicenseManager { /* License generation. */ - public static void generateLicense(Path privateKeyFile, Path licenseSkeletonFile, Path licenseFile) throws MLicensingException { + 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 == licenseFile) { - throw new IllegalArgumentException("Invalid 'licenseFile': 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); // - MJsonObject license = MLicenseManager.generateLicense(privateKeyPemString, licenseSkeletonJsonString); + MJsonObject fullLicense = MLicenseManager.generateFullLicense(privateKeyPemString, licenseSkeletonJsonString); // - Files.write(licenseFile, license.getJsonValue(true).getBytes(MConstants.DEFAULT_CHARSET), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); + Files.write(fullLicenseFile, fullLicense.getJsonValue(true).getBytes(MConstants.DEFAULT_CHARSET), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException exception) { - throw new MLicensingException("Could not generate license.", exception); + throw new MLicensingException("Could not generate full license.", exception); } } - public static MJsonObject generateLicense(String privateKeyPemString, String licenseSkeletonJsonString) throws MLicensingException { + public static MJsonObject generateFullLicense(String privateKeyPemString, String licenseSkeletonJsonString) throws MLicensingException { if (MText.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("Invalid 'privateKeyPemString': null or empty."); } @@ -83,61 +83,61 @@ public class MLicenseManager { return licenseSkeleton; } catch (InvalidKeyException exception) { - throw new MLicensingException("Could not generate license.", exception); + throw new MLicensingException("Could not generate full license.", exception); } catch (InvalidKeySpecException exception) { - throw new MLicensingException("Could not generate license.", exception); + throw new MLicensingException("Could not generate full license.", exception); } catch (MInvalidJsonValueException exception) { - throw new MLicensingException("Could not generate license.", exception); + throw new MLicensingException("Could not generate full license.", exception); } catch (NoSuchAlgorithmException exception) { - throw new MLicensingException("Could not generate license.", exception); + throw new MLicensingException("Could not generate full license.", exception); } catch (SignatureException exception) { - throw new MLicensingException("Could not generate license.", exception); + throw new MLicensingException("Could not generate full license.", exception); } } /* License verification. */ - public static MJsonObject verifyLicense(Path publicKeyFile, Path licenseFile) throws MLicensingException { + public static MJsonObject verifyFullLicense(Path publicKeyFile, Path fullLicenseFile) throws MLicensingException { if (null == publicKeyFile) { throw new IllegalArgumentException("Invalid 'publicKeyFile': null."); } - if (null == licenseFile) { - throw new IllegalArgumentException("Invalid 'licenseFile': null."); + if (null == fullLicenseFile) { + throw new IllegalArgumentException("Invalid 'fullLicenseFile': null."); } // try { String publicKeyPemString = new String(Files.readAllBytes(publicKeyFile), MConstants.DEFAULT_CHARSET); - String licenseJsonString = new String(Files.readAllBytes(licenseFile), MConstants.DEFAULT_CHARSET); + String fullLicenseJsonString = new String(Files.readAllBytes(fullLicenseFile), MConstants.DEFAULT_CHARSET); // - return MLicenseManager.verifyLicense(publicKeyPemString, licenseJsonString); + return MLicenseManager.verifyFullLicense(publicKeyPemString, fullLicenseJsonString); } catch (IOException exception) { - throw new MLicensingException("Could not verify license.", exception); + throw new MLicensingException("Could not verify full license.", exception); } } - public static MJsonObject verifyLicense(String publicKeyPemString, String licenseJsonString) throws MLicensingException { + 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(licenseJsonString)) { - throw new IllegalArgumentException("Invalid 'licenseJsonString': 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)); // - MJsonObject license = new MJsonObject(licenseJsonString); + MJsonObject fullLicense = new MJsonObject(fullLicenseJsonString); // - MJsonObject licenseData = (MJsonObject)license.getValue("licenseData"); + MJsonObject licenseData = (MJsonObject)fullLicense.getValue("licenseData"); byte[] licenseDataHashContent = MessageDigest.getInstance("SHA-256").digest(licenseData.getJsonValue().getBytes(MConstants.DEFAULT_CHARSET)); // - MJsonString licenseDataSignature = (MJsonString)license.getValue("licenseDataSignature"); + MJsonString licenseDataSignature = (MJsonString)fullLicense.getValue("licenseDataSignature"); byte[] licenseDataSignatureContent = Base64.getDecoder().decode(licenseDataSignature.getValue()); // Signature signature = Signature.getInstance("SHA256withRSA"); @@ -145,22 +145,22 @@ public class MLicenseManager { signature.update(licenseDataHashContent); signature.verify(licenseDataSignatureContent); // - return license; + return fullLicense; } catch (InvalidKeyException exception) { - throw new MLicensingException("Could not verify license.", exception); + throw new MLicensingException("Could not verify full license.", exception); } catch (InvalidKeySpecException exception) { - throw new MLicensingException("Could not verify license.", exception); + throw new MLicensingException("Could not verify full license.", exception); } catch (MInvalidJsonValueException exception) { - throw new MLicensingException("Could not verify license.", exception); + throw new MLicensingException("Could not verify full license.", exception); } catch (NoSuchAlgorithmException exception) { - throw new MLicensingException("Could not verify license.", exception); + throw new MLicensingException("Could not verify full license.", exception); } catch (SignatureException exception) { - throw new MLicensingException("Could not verify license.", exception); + throw new MLicensingException("Could not verify full license.", exception); } } -- 2.30.2