Roberto Sánchez
2014-02-24 f7be2173201d6ef2d559ef4e8fdfef5534eee29e
#593 feature - Added basic functionality (without server sync) to
validate licenses and create request files
5 files modified
changed files
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseValidator.java patch | view | blame | history
src/main/java/net/curisit/securis/ReqGenerator.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/Params.java patch | view | blame | history
src/patch/java/net/curisit/securis/LicenseGenerator.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -6,6 +6,7 @@
66 import net.curisit.securis.beans.LicenseBean;
77 import net.curisit.securis.beans.RequestBean;
88 import net.curisit.securis.utils.JsonUtils;
9
+import net.curisit.securis.utils.Params;
910
1011 import org.apache.commons.io.FileUtils;
1112
....@@ -25,23 +26,65 @@
2526 return singleton;
2627 }
2728
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 LicenseBean
31
+ * <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 licFile
41
+ * @param appCode
42
+ * @param customerCode
43
+ * @return
44
+ * @throws SeCurisException
45
+ */
46
+ public LicenseBean validateLicense(File licFile) throws SeCurisException {
2947 LicenseBean licBean;
3048 try {
3149 licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
3250 } catch (IOException e) {
33
- throw new SeCurisException("Error validating license", e);
51
+ throw new SeCurisException("Error getting license data from file: " + licFile, e);
3452 }
3553 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);
3756
3857 return licBean;
3958 }
4059
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 paramter
62
+ *
63
+ * @param outputRequestFile
64
+ * File where the request data will be saved
65
+ * @return The generated request bean
66
+ * @throws SeCurisException
67
+ */
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 licenseFile
80
+ * Current and valid License file
81
+ * @return New license bean if server creates a new one, otherwise the same current License bean will be returned
82
+ * @throws SeCurisException
83
+ */
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;
4588 }
4689
4790 }
src/main/java/net/curisit/securis/LicenseValidator.java
....@@ -4,20 +4,27 @@
44 import java.io.InputStream;
55 import java.io.UnsupportedEncodingException;
66
7
+import net.curisit.securis.beans.RequestBean;
8
+import net.curisit.securis.utils.JsonUtils;
79 import net.curisit.securis.utils.LicUtils;
810
911 import org.apache.commons.io.IOUtils;
1012
1113 public class LicenseValidator {
1214
15
+ public static LicenseValidator singleton = new LicenseValidator();
1316 private byte[] LOGO_SECRET;
1417
15
- public LicenseValidator() {
18
+ private LicenseValidator() {
1619 try {
1720 LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8");
1821 } catch (UnsupportedEncodingException e) {
1922 e.printStackTrace();
2023 }
24
+ }
25
+
26
+ public static LicenseValidator getInstance() {
27
+ return singleton;
2128 }
2229
2330 /**
....@@ -33,4 +40,17 @@
3340 return null;
3441 }
3542 }
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
+
3656 }
src/main/java/net/curisit/securis/ReqGenerator.java
....@@ -1,10 +1,15 @@
11 package net.curisit.securis;
22
3
+import java.io.File;
34 import java.io.IOException;
45 import java.io.InputStream;
56 import java.io.UnsupportedEncodingException;
7
+import java.nio.file.Files;
8
+import java.nio.file.Paths;
9
+import java.nio.file.StandardOpenOption;
610
711 import net.curisit.securis.beans.RequestBean;
12
+import net.curisit.securis.utils.JsonUtils;
813 import net.curisit.securis.utils.LicUtils;
914
1015 import org.apache.commons.io.IOUtils;
....@@ -45,6 +50,30 @@
4550 return req;
4651 }
4752
53
+ /**
54
+ * Generate a request file using a {@link RequestBean}
55
+ *
56
+ * @param req
57
+ * @param file
58
+ * @throws SeCurisException
59
+ */
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
+
4877 private String getCrcLogo() {
4978 String logResource = "images/logo_customer.png";
5079 InputStream is = getClass().getClassLoader().getResourceAsStream(logResource);
src/main/java/net/curisit/securis/utils/Params.java
....@@ -186,6 +186,9 @@
186186 */
187187 public static final String PUBLIC_KEY_FILE = "public.key.file";
188188
189
+ public static final String APPLICATION_CODE = "app.code";
190
+
191
+ public static final String CUSTOMER_CODE = "customer.code";
189192 }
190193
191194 }
src/patch/java/net/curisit/securis/LicenseGenerator.java
....@@ -2,6 +2,10 @@
22
33 import java.io.File;
44 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;
59 import java.security.InvalidKeyException;
610 import java.security.NoSuchAlgorithmException;
711 import java.security.NoSuchProviderException;
....@@ -14,6 +18,7 @@
1418
1519 import net.curisit.securis.beans.LicenseBean;
1620 import net.curisit.securis.beans.RequestBean;
21
+import net.curisit.securis.utils.JsonUtils;
1722
1823 import org.apache.commons.net.util.Base64;
1924 import org.slf4j.Logger;
....@@ -62,6 +67,30 @@
6267 }
6368
6469 /**
70
+ * Generate a license file using a {@link LicenseBean}
71
+ *
72
+ * @param license
73
+ * @param file
74
+ * @throws SeCurisException
75
+ */
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
+ /**
6594 * TODO: This method should be removed from client code.
6695 *
6796 * @param licBean