Roberto Sánchez
2014-09-18 ca4f38f1305667758c2c2e90b45cd19bdb532721
#0 feature - Fixed some SonarQube issues
1 files deleted
3 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/utils/CryptoHelper.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/JsonUtils.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -88,8 +88,9 @@
8888 */
8989 public LicenseBean requestLicense() throws SeCurisException {
9090 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
91
- if (true)
91
+ if (true) {
9292 throw new SeCurisException("Action not implemented yet");
93
+ }
9394 LicenseBean lic = requestLicenseToServer(req);
9495 return lic;
9596 }
....@@ -150,8 +151,9 @@
150151 */
151152 public LicenseBean renew(File licenseFile) throws SeCurisException {
152153 LicenseBean lic = validateLicense(licenseFile);
153
- if (true)
154
+ if (true) {
154155 throw new SeCurisException("Action not implemented yet");
156
+ }
155157 // TODO: Send the current LicenseBean to server to check if a new one is prepared.
156158 return lic;
157159 }
src/main/java/net/curisit/securis/LicenseValidator.java
....@@ -16,15 +16,18 @@
1616
1717 private static final Logger log = LogManager.getLogger(LicenseValidator.class);
1818
19
- public static LicenseValidator singleton = new LicenseValidator();
20
- public static byte[] LOGO_SECRET;
19
+ private static LicenseValidator singleton = new LicenseValidator();
20
+ protected static byte[] LOGO_SECRET;
2121
22
- private LicenseValidator() {
22
+ static {
2323 try {
2424 LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8");
2525 } catch (UnsupportedEncodingException e) {
2626 log.error("Unexpected error getting LOGO secret", e);
2727 }
28
+ }
29
+
30
+ private LicenseValidator() {
2831 }
2932
3033 public static LicenseValidator getInstance() {
....@@ -57,8 +60,9 @@
5760
5861 public void validateHW(RequestBean reqBean, String appCode, String customerCode) throws SeCurisException {
5962 RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode);
60
- if (!currentHW.match(reqBean))
63
+ if (!currentHW.match(reqBean)) {
6164 throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: " + JsonUtils.toJSON(reqBean, true) + "\n Expected: " + JsonUtils.toJSON(currentHW, true));
65
+ }
6266 }
6367
6468 }
src/main/java/net/curisit/securis/utils/CryptoHelper.java
deleted file mode 100644
....@@ -1,157 +0,0 @@
1
-package net.curisit.securis.utils;
2
-
3
-import java.io.UnsupportedEncodingException;
4
-import java.security.InvalidKeyException;
5
-import java.security.NoSuchAlgorithmException;
6
-import java.security.NoSuchProviderException;
7
-import java.security.SecureRandom;
8
-import java.security.spec.InvalidKeySpecException;
9
-import java.util.Arrays;
10
-
11
-import javax.crypto.BadPaddingException;
12
-import javax.crypto.Cipher;
13
-import javax.crypto.IllegalBlockSizeException;
14
-import javax.crypto.NoSuchPaddingException;
15
-import javax.crypto.SecretKey;
16
-import javax.crypto.SecretKeyFactory;
17
-import javax.crypto.spec.PBEKeySpec;
18
-import javax.crypto.spec.SecretKeySpec;
19
-
20
-import net.curisit.securis.SeCurisException;
21
-
22
-import org.apache.commons.net.util.Base64;
23
-import org.apache.logging.log4j.LogManager;
24
-import org.apache.logging.log4j.Logger;
25
-
26
-//import net.curisit.common.ui.Dialogs;
27
-
28
-public class CryptoHelper {
29
-
30
- private static final Logger log = LogManager.getLogger(SignatureHelper.class);
31
-
32
- private static final String KEY_FACTORY = "PBEWITHHMACSHA1";
33
- private static final String PPROVIDER = "BC";
34
- private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
35
-
36
- private static CryptoHelper singleton = new CryptoHelper();
37
-
38
- private String passPhrase = null;
39
-
40
- private CryptoHelper() {
41
- }
42
-
43
- public static CryptoHelper getInstance() {
44
- return singleton;
45
- }
46
-
47
- /**
48
- * Encrypts a given text with AES algorithm
49
- *
50
- * @param plainText
51
- * @return The encrypted text in Base64
52
- */
53
- public String encrypt(String plainText) throws SeCurisException {
54
- return encrypt(plainText, this.passPhrase);
55
- }
56
-
57
- public String encrypt(String plainText, String pass) throws SeCurisException {
58
- Cipher aes;
59
- try {
60
- aes = Cipher.getInstance(CIPHER_ALGORITHM);
61
- byte[] salt = getSalt();
62
- aes.init(Cipher.ENCRYPT_MODE, getSecretKey(salt, pass));
63
- byte[] ciphertext = aes.doFinal(plainText.getBytes("utf-8"));
64
-
65
- return Base64.encodeBase64String(salt) + "\n" + Base64.encodeBase64String(ciphertext);
66
- } catch (NoSuchAlgorithmException e) {
67
- log.error("Error decrypting text", e);
68
- throw new SeCurisException("Error decrypting text", e);
69
- } catch (NoSuchPaddingException e) {
70
- log.error("Error decrypting text", e);
71
- throw new SeCurisException("Error decrypting text", e);
72
- } catch (InvalidKeyException e) {
73
- log.error("Error decrypting text", e);
74
- throw new SeCurisException("Error decrypting text", e);
75
- } catch (IllegalBlockSizeException e) {
76
- log.error("Error decrypting text", e);
77
- throw new SeCurisException("Error decrypting text", e);
78
- } catch (BadPaddingException e) {
79
- log.error("Error decrypting text", e);
80
- throw new SeCurisException("Error decrypting text", e);
81
- } catch (UnsupportedEncodingException e) {
82
- log.error("Error decrypting text", e);
83
- throw new SeCurisException("Error decrypting text", e);
84
- }
85
- }
86
-
87
- /**
88
- * Encrypts a given text with AES algorithm
89
- *
90
- * @param plainText
91
- * in Base64
92
- * @return
93
- */
94
- public String decript(String ciphertext) throws SeCurisException {
95
- return decript(ciphertext, this.passPhrase);
96
- }
97
-
98
- public String decript(String ciphertext, String pass) throws SeCurisException {
99
- Cipher aes;
100
- try {
101
- aes = Cipher.getInstance(CIPHER_ALGORITHM);
102
- int sep = ciphertext.indexOf('\n');
103
- if (sep == -1)
104
- throw new SeCurisException("Unknown format ciphered text");
105
- byte[] salt = Base64.decodeBase64(ciphertext.substring(0, sep));
106
- aes.init(Cipher.DECRYPT_MODE, getSecretKey(salt, pass));
107
- byte[] encryptedBytes = Base64.decodeBase64(ciphertext.substring(sep + 1));
108
- String cleartext = new String(aes.doFinal(encryptedBytes));
109
- return cleartext;
110
- } catch (NoSuchAlgorithmException e) {
111
- log.error("Error decrypting text", e);
112
- throw new SeCurisException("Error decrypting text", e);
113
- } catch (NoSuchPaddingException e) {
114
- log.error("Error decrypting text", e);
115
- throw new SeCurisException("Error decrypting text", e);
116
- } catch (InvalidKeyException e) {
117
- log.error("Error decrypting text", e);
118
- throw new SeCurisException("Error decrypting text", e);
119
- } catch (IllegalBlockSizeException e) {
120
- log.error("Error decrypting text", e);
121
- throw new SeCurisException("Error decrypting text", e);
122
- } catch (BadPaddingException e) {
123
- log.error("Error decrypting text", e);
124
- throw new SeCurisException("Error decrypting text", e);
125
- }
126
- }
127
-
128
- private byte[] getSalt() throws SeCurisException {
129
- byte[] salt = new byte[20];
130
- new SecureRandom().nextBytes(salt);
131
- return salt;
132
- }
133
-
134
- private SecretKeySpec getSecretKey(byte[] salt, String pass) throws SeCurisException {
135
- String passPhrase = pass.replace('a', 'ä');
136
-
137
- try {
138
- int iterations = 10000;
139
-
140
- SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_FACTORY, PPROVIDER);
141
- SecretKey tmp = factory.generateSecret(new PBEKeySpec(passPhrase.toCharArray(), salt, iterations, 128));
142
- byte[] key = Arrays.copyOf(tmp.getEncoded(), 16);
143
- SecretKeySpec spec = new SecretKeySpec(key, "AES");
144
- return spec;
145
-
146
- } catch (NoSuchAlgorithmException e) {
147
- log.error("Error generation secret key", e);
148
- throw new SeCurisException("Error generation secret key", e);
149
- } catch (InvalidKeySpecException e) {
150
- log.error("Error generation secret key", e);
151
- throw new SeCurisException("Error generation secret key", e);
152
- } catch (NoSuchProviderException e) {
153
- log.error("Error generation secret key", e);
154
- throw new SeCurisException("Error generation secret key", e);
155
- }
156
- }
157
-}
src/main/java/net/curisit/securis/utils/JsonUtils.java
....@@ -45,6 +45,7 @@
4545 * @param type
4646 * @return
4747 */
48
+ @SuppressWarnings("unchecked")
4849 public static <T> T value(Object value, Class<T> type) {
4950
5051 return (T) value;
....@@ -52,8 +53,9 @@
5253
5354 public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
5455 try {
55
- if (json == null)
56
+ if (json == null){
5657 return null;
58
+ }
5759 return MAPPER.readValue(json, type);
5860 } catch (JsonParseException e) {
5961 log.error("Error parsing JSON string to obejct: {}", json, e);
....@@ -78,8 +80,9 @@
7880 public static String toJSON(Object obj) throws SeCurisException {
7981 // and could also do other configuration...
8082 try {
81
- if (obj == null)
83
+ if (obj == null) {
8284 return null;
85
+ }
8386 return MAPPER.writeValueAsString(obj);
8487 } catch (JsonProcessingException e) {
8588 log.error("Error formating JSON from object: {}", obj, e);
....@@ -100,8 +103,9 @@
100103 public static String toJSON(Object obj, boolean pretty) throws SeCurisException {
101104 // and could also do other configuration...
102105 try {
103
- if (obj == null)
106
+ if (obj == null) {
104107 return null;
108
+ }
105109 MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
106110 String json = MAPPER.writeValueAsString(obj);
107111 MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
....@@ -124,11 +128,13 @@
124128 * String with json format
125129 * @return
126130 */
131
+ @SuppressWarnings("unchecked")
127132 public static Map<String, Object> json2map(String json) throws JsonParseException {
128133
129134 try {
130
- if (json == null)
135
+ if (json == null) {
131136 return null;
137
+ }
132138 return MAPPER.readValue(json, Map.class);
133139 } catch (JsonParseException e) {
134140 log.error("Error parsing JSON string to map: {}", json, e);
....@@ -149,8 +155,9 @@
149155 public static String map2json(Map<String, Object> map) {
150156 // and could also do other configuration...
151157 try {
152
- if (map == null)
158
+ if (map == null) {
153159 return null;
160
+ }
154161 return MAPPER.writeValueAsString(map);
155162 } catch (JsonProcessingException e) {
156163 log.error("Error formating JSON from map: {}", map, e);
....@@ -170,15 +177,17 @@
170177 * String with json format
171178 * @return
172179 */
173
- public static List<Object> json2list(String json) {
180
+ @SuppressWarnings("unchecked")
181
+ public static List<Object> json2list(String json) throws SeCurisException {
174182 try {
175183 return MAPPER.readValue(json, List.class);
176184 } catch (JsonParseException e) {
177185 log.error("Error converting JSON string to object {}", json, e);
186
+ throw new SeCurisException("Error converting JSON to object", e);
178187 } catch (IOException e) {
179188 log.error("Error converting JSON string to object {}", json, e);
189
+ throw new SeCurisException("Error converting JSON to object", e);
180190 }
181
- return null;
182191 }
183192
184193 public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {