src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -6,6 +6,7 @@ 6 6 import net.curisit.securis.beans.LicenseBean; 7 7 import net.curisit.securis.beans.RequestBean; 8 8 import net.curisit.securis.utils.JsonUtils; 9 +import net.curisit.securis.utils.Params;9 10 10 11 import org.apache.commons.io.FileUtils; 11 12 .. .. @@ -25,23 +26,65 @@ 25 26 return singleton; 26 27 } 27 28 28 - public LicenseBean validateLicense(File licFile, String appCode, String customerCode) throws SeCurisException {29 + /**30 + * Validates the license stored in {@code licFile} and get the corresponding LicenseBean31 + * <p>32 + * The validation includes:33 + * <ul>34 + * <li>Signature</li>35 + * <li>HW data</li>36 + * <li>Logo CRC</li>37 + * </ul>38 + * </p>39 + *40 + * @param licFile41 + * @param appCode42 + * @param customerCode43 + * @return44 + * @throws SeCurisException45 + */46 + public LicenseBean validateLicense(File licFile) throws SeCurisException {29 47 LicenseBean licBean; 30 48 try { 31 49 licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class); 32 50 } catch (IOException e) { 33 - throw new SeCurisException("Error validating license", e);51 + throw new SeCurisException("Error getting license data from file: " + licFile, e);34 52 } 35 53 SignatureHelper.getInstance().validateSignature(licBean); 36 - validateHW(licBean, appCode, customerCode);54 + LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));55 + LicenseValidator.getInstance().validateLogo(licBean);37 56 38 57 return licBean; 39 58 } 40 59 41 - private void validateHW(RequestBean reqBean, String appCode, String customerCode) throws SeCurisException {42 - RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode);43 - if (!currentHW.match(reqBean))44 - throw new SeCurisException("Current System info mismatch the License System info: " + JsonUtils.toJSON(reqBean));60 + /**61 + * Creates a new request file with current hardware in the File passed as paramter62 + *63 + * @param outputRequestFile64 + * File where the request data will be saved65 + * @return The generated request bean66 + * @throws SeCurisException67 + */68 + public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {69 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));70 +71 + ReqGenerator.getInstance().save(req, outputRequestFile);72 +73 + return req;74 + }75 +76 + /**77 + * Send the current license file to server, which is previously validated, to get a renewed one if it is prepared in server side.78 + *79 + * @param licenseFile80 + * Current and valid License file81 + * @return New license bean if server creates a new one, otherwise the same current License bean will be returned82 + * @throws SeCurisException83 + */84 + public LicenseBean sync(File licenseFile) throws SeCurisException {85 + LicenseBean lic = validateLicense(licenseFile);86 + // TODO: Send the current LicenseBean to server to check if a new one is prepared.87 + return lic;45 88 } 46 89 47 90 } src/main/java/net/curisit/securis/LicenseValidator.java
.. .. @@ -4,20 +4,27 @@ 4 4 import java.io.InputStream; 5 5 import java.io.UnsupportedEncodingException; 6 6 7 +import net.curisit.securis.beans.RequestBean;8 +import net.curisit.securis.utils.JsonUtils;7 9 import net.curisit.securis.utils.LicUtils; 8 10 9 11 import org.apache.commons.io.IOUtils; 10 12 11 13 public class LicenseValidator { 12 14 15 + public static LicenseValidator singleton = new LicenseValidator();13 16 private byte[] LOGO_SECRET; 14 17 15 - public LicenseValidator() {18 + private LicenseValidator() {16 19 try { 17 20 LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8"); 18 21 } catch (UnsupportedEncodingException e) { 19 22 e.printStackTrace(); 20 23 } 24 + }25 +26 + public static LicenseValidator getInstance() {27 + return singleton;21 28 } 22 29 23 30 /** .. .. @@ -33,4 +40,17 @@ 33 40 return null; 34 41 } 35 42 } 43 +44 + public void validateLogo(RequestBean reqBean) throws SeCurisException {45 + String currentCRC = getCrcLogo();46 + if (!currentCRC.equals(reqBean.getCrcLogo()))47 + throw new SeCurisException("License logo validation failed for request data: " + JsonUtils.toJSON(reqBean));48 + }49 +50 + public void validateHW(RequestBean reqBean, String appCode, String customerCode) throws SeCurisException {51 + RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode);52 + if (!currentHW.match(reqBean))53 + throw new SeCurisException("Current System info mismatch the License System info: " + JsonUtils.toJSON(reqBean));54 + }55 +36 56 } src/main/java/net/curisit/securis/ReqGenerator.java
.. .. @@ -1,10 +1,15 @@ 1 1 package net.curisit.securis; 2 2 3 +import java.io.File;3 4 import java.io.IOException; 4 5 import java.io.InputStream; 5 6 import java.io.UnsupportedEncodingException; 7 +import java.nio.file.Files;8 +import java.nio.file.Paths;9 +import java.nio.file.StandardOpenOption;6 10 7 11 import net.curisit.securis.beans.RequestBean; 12 +import net.curisit.securis.utils.JsonUtils;8 13 import net.curisit.securis.utils.LicUtils; 9 14 10 15 import org.apache.commons.io.IOUtils; .. .. @@ -45,6 +50,30 @@ 45 50 return req; 46 51 } 47 52 53 + /**54 + * Generate a request file using a {@link RequestBean}55 + *56 + * @param req57 + * @param file58 + * @throws SeCurisException59 + */60 + public void save(RequestBean req, File file) throws SeCurisException {61 + byte[] json;62 + try {63 + json = JsonUtils.toJSON(req, true).getBytes("utf-8");64 + Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE);65 + } catch (UnsupportedEncodingException e) {66 + log.error("Error creating json doc from request: " + req, e);67 + throw new SeCurisException("Error creating json doc from request: " + req, e);68 + } catch (IOException e) {69 + log.error("Error creating request file: " + file, e);70 + throw new SeCurisException("Error creating request file: " + file, e);71 + }72 +73 + log.info("License saved in {}", file);74 +75 + }76 +48 77 private String getCrcLogo() { 49 78 String logResource = "images/logo_customer.png"; 50 79 InputStream is = getClass().getClassLoader().getResourceAsStream(logResource); src/main/java/net/curisit/securis/utils/Params.java
.. .. @@ -186,6 +186,9 @@ 186 186 */ 187 187 public static final String PUBLIC_KEY_FILE = "public.key.file"; 188 188 189 + public static final String APPLICATION_CODE = "app.code";190 +191 + public static final String CUSTOMER_CODE = "customer.code";189 192 } 190 193 191 194 } src/patch/java/net/curisit/securis/LicenseGenerator.java
.. .. @@ -2,6 +2,10 @@ 2 2 3 3 import java.io.File; 4 4 import java.io.IOException; 5 +import java.io.UnsupportedEncodingException;6 +import java.nio.file.Files;7 +import java.nio.file.Paths;8 +import java.nio.file.StandardOpenOption;5 9 import java.security.InvalidKeyException; 6 10 import java.security.NoSuchAlgorithmException; 7 11 import java.security.NoSuchProviderException; .. .. @@ -14,6 +18,7 @@ 14 18 15 19 import net.curisit.securis.beans.LicenseBean; 16 20 import net.curisit.securis.beans.RequestBean; 21 +import net.curisit.securis.utils.JsonUtils;17 22 18 23 import org.apache.commons.net.util.Base64; 19 24 import org.slf4j.Logger; .. .. @@ -62,6 +67,30 @@ 62 67 } 63 68 64 69 /** 70 + * Generate a license file using a {@link LicenseBean}71 + *72 + * @param license73 + * @param file74 + * @throws SeCurisException75 + */76 + public void save(LicenseBean license, File file) throws SeCurisException {77 + byte[] json;78 + try {79 + json = JsonUtils.toJSON(license, true).getBytes("utf-8");80 + Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE);81 + } catch (UnsupportedEncodingException e) {82 + log.error("Error creating json doc from license: " + license, e);83 + throw new SeCurisException("Error creating json doc from license: " + license, e);84 + } catch (IOException e) {85 + log.error("Error creating license file: " + file, e);86 + throw new SeCurisException("Error creating json doc from license: " + license, e);87 + }88 +89 + log.info("License saved in {}", file);90 +91 + }92 +93 + /**65 94 * TODO: This method should be removed from client code. 66 95 * 67 96 * @param licBean