- * Generally, an application will not required more then one instance of license - * manager. - * - * @author Patrik Dufresne - * - */ -public class LicenseManager { - private static final int SIZE = 2048; - /** - * The encryption manager used by this class.F - */ - private EncryptionManager encryptionManager; - - /** - * Create a new license manager. - * - * @param publicKey - * the public key filename. - * @param privateKey - * the private key filename (null if not available). - * @throws GeneralSecurityException - * if the provided key are invalid. - * @throws IOException - * if the file doesn't exists - */ - public LicenseManager(String publicKey, String privateKey) - throws GeneralSecurityException, IOException { - byte[] pubdata = EncryptionManager.readAll(new File(publicKey)); - byte[] privdata = null; - if (privateKey != null) { - privdata = EncryptionManager.readAll(new File(privateKey)); - } - this.encryptionManager = new EncryptionManager(pubdata, privdata); - } - - /** - * Create a new license manager. - * - * @param publicKey - * the public key file. - * @param privateKey - * the private key file (null if not available). - * @throws GeneralSecurityException - * if the provided key are invalid. - * @throws IOException - * if the file doesn't exists - */ - public LicenseManager(File publicKey, File privateKey) - throws GeneralSecurityException, IOException { - byte[] pubdata = EncryptionManager.readAll(publicKey); - byte[] privdata = null; - if (privateKey != null) { - privdata = EncryptionManager.readAll(privateKey); - } - this.encryptionManager = new EncryptionManager(pubdata, privdata); - } - - /** - * Create a new license manager. - * - * @param publicKey - * an input stream containing the public key - * @param privateKey - * an input stream containing the private key - */ - public LicenseManager(InputStream publicKey, InputStream privateKey) - throws GeneralSecurityException, IOException { - byte[] pubdata = EncryptionManager.readAll(publicKey); - byte[] privdata = null; - if (privateKey != null) { - privdata = EncryptionManager.readAll(privateKey); - } - this.encryptionManager = new EncryptionManager(pubdata, privdata); - } - - /** - * Create a new license manager. Generally, an application will not required - * more then one instance of license manager. - * - * @param publicKey - * the public key (can't be null). - * - * @param privateKey - * the private key (null if not available). - * @throws GeneralSecurityException - * if the provided key are invalid. - */ - public LicenseManager(byte[] publicKey, byte[] privateKey) - throws GeneralSecurityException { - this.encryptionManager = new EncryptionManager(publicKey, privateKey); - } - - /** - * Read the content of an encrypted license file. - * - * @param file - * the location to the license file. - * @return the license object if the license file is valid, null otherwise. - * @throws IOException - * if file not found or read error. - * @throws SignatureException - * if this signature algorithm is unable to process the content - * of the file - * @throws NoSuchAlgorithmException - * if the SHA algorithm doesn't exists - * @throws InvalidKeyException - * if the public key is invalid - * @throws ClassNotFoundException - * if the implementation of {@link ILicense} stored in the file - * can't be found - */ - public ILicense readLicenseFile(File file) throws IOException, - InvalidKeyException, NoSuchAlgorithmException, SignatureException, - ClassNotFoundException { - - // Read the content of the file - byte[] sig; - byte[] data; - ObjectInputStream fileIn = new ObjectInputStream( - new BufferedInputStream(new FileInputStream(file))); - try { - int sigLength = fileIn.readInt(); - sig = new byte[sigLength]; - fileIn.read(sig); - - ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); - byte[] buf = new byte[SIZE]; - int len; - while ((len = fileIn.read(buf)) != -1) { - dataStream.write(buf, 0, len); - } - dataStream.flush(); - data = dataStream.toByteArray(); - dataStream.close(); - } finally { - fileIn.close(); - } - - // Validate the signature - if (!encryptionManager.verify(data, sig)) { - return null; - } - - // Read the license object from the data. - ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream( - data)); - try { - ILicense license = (ILicense) in.readObject(); - return license; - } finally { - in.close(); - } - - } - - /** - * Used to serialize a license object. - * - * @param license - * the license object. - * @param file - * the location where to save the new license file. If file - * exists, it's overwrite. - * @throws IOException - * if the file doesn't exists or can't be written to - * @throws SignatureException - * if this signature algorithm is unable to process the license - * data - * @throws NoSuchAlgorithmException - * if the algorithm SHA is not supported - * @throws InvalidKeyException - * if the private key is invalid. - */ - public void writeLicense(ILicense license, File file) throws IOException, - InvalidKeyException, NoSuchAlgorithmException, SignatureException { - // Write the license information into a byte array. - ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(dataStream); - out.writeObject(license); - byte[] data = dataStream.toByteArray(); - out.close(); - - // Then sign the byte array - byte[] signature = this.encryptionManager.sign(data); - - // Write all the data into one single file. - ObjectOutputStream fileOut = new ObjectOutputStream( - new BufferedOutputStream(new FileOutputStream(file))); - try { - fileOut.writeInt(signature.length); - fileOut.write(signature); - fileOut.write(data); - fileOut.flush(); - } finally { - fileOut.close(); - } - - } - -} diff --git a/license/src/com/patrikdufresne/license/LicenseVersionExpiredException.java b/license/src/com/patrikdufresne/license/LicenseVersionExpiredException.java deleted file mode 100644 index 3a53faf92b063095074362a8f6f6447b83848208..0000000000000000000000000000000000000000 --- a/license/src/com/patrikdufresne/license/LicenseVersionExpiredException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2012 David Stites, Patrik Dufresne and others. - * - * You may distribute under the terms of either the MIT License, the Apache - * License 2.0 or the Simplified BSD License, as specified in the README file. - * - * Contributors: - * David Stites - initial API and implementation - * Patrik Dufresne - refactoring - */ -package com.patrikdufresne.license; - -/** - * This exception is throw when the license version doesn't match the current - * version. - * - * @author Patrik Dufresne - * - */ -public class LicenseVersionExpiredException extends LicenseException { - - private static final long serialVersionUID = 8947235554238066208L; - - public LicenseVersionExpiredException() { - super("version expired"); - } - -} diff --git a/license/src/com/patrikdufresne/license/util/ILogger.java b/license/src/com/patrikdufresne/license/util/ILogger.java deleted file mode 100644 index 8f4071729d7f120faf40e81ca7bc0b64b7e2d5d1..0000000000000000000000000000000000000000 --- a/license/src/com/patrikdufresne/license/util/ILogger.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2012 David Stites, Patrik Dufresne and others. - * - * You may distribute under the terms of either the MIT License, the Apache - * License 2.0 or the Simplified BSD License, as specified in the README file. - * - * Contributors: - * David Stites - initial API and implementation - * Patrik Dufresne - refactoring - */ -package com.patrikdufresne.license.util; - -/** - * A mechanism to log errors throughout the license framework. - *
- * Clients may provide their own implementation to change how errors are logged - * from within the license framework. - *
- * - */ -public interface ILogger { - /** - * Trace level (value: trace). - */ - public static final String TRACE = "trace"; - /** - * Debug level (value: debug). - */ - public static final String DEBUG = "debug"; - /** - * Info level (value: info). - */ - public static final String INFO = "info"; - /** - * Warn level (value: warn). - */ - public static final String WARN = "warn"; - /** - * Error level (value: error). - */ - public static final String ERROR = "error"; - - /** - * Logs the given status. - * - * @param level - * The level - * @param message - * The message to be logged. - */ - public void log(String level, String message); - - /** - * Logs the given exception. - * - * @param level - * @param exception - */ - public void log(String level, Throwable exception); - -} diff --git a/license/src/com/patrikdufresne/license/util/Policy.java b/license/src/com/patrikdufresne/license/util/Policy.java deleted file mode 100644 index 00f54b02c712c1c55d9e281f8312e93c10c7f57d..0000000000000000000000000000000000000000 --- a/license/src/com/patrikdufresne/license/util/Policy.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2012 David Stites, Patrik Dufresne and others. - * - * You may distribute under the terms of either the MIT License, the Apache - * License 2.0 or the Simplified BSD License, as specified in the README file. - * - * Contributors: - * David Stites - initial API and implementation - * Patrik Dufresne - refactoring - */ -package com.patrikdufresne.license.util; - -/** - * The Policy class handles settings for behavior, debug flags and logging - * within the license framework. - * - */ -public class Policy { - - private static ILogger log; - - /** - * Returns the dummy log to use if none has been set - */ - private static ILogger getDummyLog() { - return new ILogger() { - @Override - public void log(String level, String message) { - System.out.print(level + " " + message); - } - - @Override - public void log(String level, Throwable exception) { - exception.printStackTrace(System.out); - } - }; - } - - /** - * Returns the logger used by the license framework to log errors. - *
- * The default logger prints the status to System.err
.
- *
null
to use the default
- * logger
- */
- public static void setLog(ILogger logger) {
- log = logger;
- }
-
-}
diff --git a/license/version.properties b/license/version.properties
deleted file mode 100644
index f8f6da16e9aedb934595595adf16cc2dd98e7588..0000000000000000000000000000000000000000
--- a/license/version.properties
+++ /dev/null
@@ -1,2 +0,0 @@
-version=0.2
-package.name=com.patrikdufresne.license
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000000000000000000000000000000000000..59102bbb8e5d67af5f051f621eb7485407efa120
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,130 @@
+
+ * Generally, an application will not required more then one instance of license
+ * manager.
+ *
+ * @author Patrik Dufresne
+ *
+ */
+public class LicenseManager {
+ private static final int SIZE = 2048;
+ /**
+ * The encryption manager used by this class.F
+ */
+ private EncryptionManager encryptionManager;
+
+ /**
+ * Create a new license manager.
+ *
+ * @param publicKey
+ * the public key filename.
+ * @param privateKey
+ * the private key filename (null if not available).
+ * @throws GeneralSecurityException
+ * if the provided key are invalid.
+ * @throws IOException
+ * if the file doesn't exists
+ */
+ public LicenseManager(String publicKey, String privateKey) throws GeneralSecurityException, IOException {
+ byte[] pubdata = EncryptionManager.readAll(new File(publicKey));
+ byte[] privdata = null;
+ if (privateKey != null) {
+ privdata = EncryptionManager.readAll(new File(privateKey));
+ }
+ this.encryptionManager = new EncryptionManager(pubdata, privdata);
+ }
+
+ /**
+ * Create a new license manager.
+ *
+ * @param publicKey
+ * the public key file.
+ * @param privateKey
+ * the private key file (null if not available).
+ * @throws GeneralSecurityException
+ * if the provided key are invalid.
+ * @throws IOException
+ * if the file doesn't exists
+ */
+ public LicenseManager(File publicKey, File privateKey) throws GeneralSecurityException, IOException {
+ byte[] pubdata = EncryptionManager.readAll(publicKey);
+ byte[] privdata = null;
+ if (privateKey != null) {
+ privdata = EncryptionManager.readAll(privateKey);
+ }
+ this.encryptionManager = new EncryptionManager(pubdata, privdata);
+ }
+
+ /**
+ * Create a new license manager.
+ *
+ * @param publicKey
+ * an input stream containing the public key
+ * @param privateKey
+ * an input stream containing the private key
+ */
+ public LicenseManager(InputStream publicKey, InputStream privateKey) throws GeneralSecurityException, IOException {
+ byte[] pubdata = EncryptionManager.readAll(publicKey);
+ byte[] privdata = null;
+ if (privateKey != null) {
+ privdata = EncryptionManager.readAll(privateKey);
+ }
+ this.encryptionManager = new EncryptionManager(pubdata, privdata);
+ }
+
+ /**
+ * Create a new license manager. Generally, an application will not required
+ * more then one instance of license manager.
+ *
+ * @param publicKey
+ * the public key (can't be null).
+ *
+ * @param privateKey
+ * the private key (null if not available).
+ * @throws GeneralSecurityException
+ * if the provided key are invalid.
+ */
+ public LicenseManager(byte[] publicKey, byte[] privateKey) throws GeneralSecurityException {
+ this.encryptionManager = new EncryptionManager(publicKey, privateKey);
+ }
+
+ /**
+ * Read the content of an encrypted license file.
+ *
+ * @param file
+ * the location to the license file.
+ * @return the license object if the license file is valid, null otherwise.
+ * @throws IOException
+ * if file not found or read error.
+ * @throws SignatureException
+ * if this signature algorithm is unable to process the content
+ * of the file
+ * @throws NoSuchAlgorithmException
+ * if the SHA algorithm doesn't exists
+ * @throws InvalidKeyException
+ * if the public key is invalid
+ * @throws ClassNotFoundException
+ * if the implementation of {@link ILicense} stored in the file
+ * can't be found
+ */
+ public ILicense readLicenseFile(File file) throws IOException, InvalidKeyException, NoSuchAlgorithmException, SignatureException, ClassNotFoundException {
+
+ // Read the content of the file
+ byte[] sig;
+ byte[] data;
+ ObjectInputStream fileIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
+ try {
+ int sigLength = fileIn.readInt();
+ sig = new byte[sigLength];
+ fileIn.read(sig);
+
+ ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
+ byte[] buf = new byte[SIZE];
+ int len;
+ while ((len = fileIn.read(buf)) != -1) {
+ dataStream.write(buf, 0, len);
+ }
+ dataStream.flush();
+ data = dataStream.toByteArray();
+ dataStream.close();
+ } finally {
+ fileIn.close();
+ }
+
+ // Validate the signature
+ if (!encryptionManager.verify(data, sig)) {
+ return null;
+ }
+
+ // Read the license object from the data.
+ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
+ try {
+ ILicense license = (ILicense) in.readObject();
+ return license;
+ } finally {
+ in.close();
+ }
+
+ }
+
+ /**
+ * Used to serialize a license object.
+ *
+ * @param license
+ * the license object.
+ * @param file
+ * the location where to save the new license file. If file
+ * exists, it's overwrite.
+ * @throws IOException
+ * if the file doesn't exists or can't be written to
+ * @throws SignatureException
+ * if this signature algorithm is unable to process the license
+ * data
+ * @throws NoSuchAlgorithmException
+ * if the algorithm SHA is not supported
+ * @throws InvalidKeyException
+ * if the private key is invalid.
+ */
+ public void writeLicense(ILicense license, File file) throws IOException, InvalidKeyException, NoSuchAlgorithmException, SignatureException {
+ // Write the license information into a byte array.
+ ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream(dataStream);
+ out.writeObject(license);
+ byte[] data = dataStream.toByteArray();
+ out.close();
+
+ // Then sign the byte array
+ byte[] signature = this.encryptionManager.sign(data);
+
+ // Write all the data into one single file.
+ ObjectOutputStream fileOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
+ try {
+ fileOut.writeInt(signature.length);
+ fileOut.write(signature);
+ fileOut.write(data);
+ fileOut.flush();
+ } finally {
+ fileOut.close();
+ }
+
+ }
+
+}
diff --git a/src/main/java/com/patrikdufresne/license/LicenseVersionExpiredException.java b/src/main/java/com/patrikdufresne/license/LicenseVersionExpiredException.java
new file mode 100644
index 0000000000000000000000000000000000000000..9b95f0ddac2163cec2e1ae8e4fa0c72e52416023
--- /dev/null
+++ b/src/main/java/com/patrikdufresne/license/LicenseVersionExpiredException.java
@@ -0,0 +1,33 @@
+/**
+ * Copyright(C) 2013 Patrik Dufresne Service Logiciel
+ * Clients may provide their own implementation to change how errors are logged
+ * from within the license framework.
+ *
+ * The default logger prints the status to System.err
.
+ * null
to use the default
+ * logger
+ */
+ public static void setLog(ILogger logger) {
+ log = logger;
+ }
+
+}
diff --git a/src/test/java/com/patrikdufresne/license/EncryptionManagerTest.java b/src/test/java/com/patrikdufresne/license/EncryptionManagerTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..033471643390dd9edec4797748b9fc68a653b725
--- /dev/null
+++ b/src/test/java/com/patrikdufresne/license/EncryptionManagerTest.java
@@ -0,0 +1,91 @@
+/**
+ * Copyright(C) 2013 Patrik Dufresne Service Logiciel