src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -88,8 +88,9 @@ 88 88 */ 89 89 public LicenseBean requestLicense() throws SeCurisException { 90 90 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE)); 91 - if (true)91 + if (true) {92 92 throw new SeCurisException("Action not implemented yet"); 93 + }93 94 LicenseBean lic = requestLicenseToServer(req); 94 95 return lic; 95 96 } .. .. @@ -150,8 +151,9 @@ 150 151 */ 151 152 public LicenseBean renew(File licenseFile) throws SeCurisException { 152 153 LicenseBean lic = validateLicense(licenseFile); 153 - if (true)154 + if (true) {154 155 throw new SeCurisException("Action not implemented yet"); 156 + }155 157 // TODO: Send the current LicenseBean to server to check if a new one is prepared. 156 158 return lic; 157 159 } src/main/java/net/curisit/securis/LicenseValidator.java
.. .. @@ -16,15 +16,18 @@ 16 16 17 17 private static final Logger log = LogManager.getLogger(LicenseValidator.class); 18 18 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;21 21 22 - private LicenseValidator() {22 + static {23 23 try { 24 24 LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8"); 25 25 } catch (UnsupportedEncodingException e) { 26 26 log.error("Unexpected error getting LOGO secret", e); 27 27 } 28 + }29 +30 + private LicenseValidator() {28 31 } 29 32 30 33 public static LicenseValidator getInstance() { .. .. @@ -57,8 +60,9 @@ 57 60 58 61 public void validateHW(RequestBean reqBean, String appCode, String customerCode) throws SeCurisException { 59 62 RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode); 60 - if (!currentHW.match(reqBean))63 + if (!currentHW.match(reqBean)) {61 64 throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: " + JsonUtils.toJSON(reqBean, true) + "\n Expected: " + JsonUtils.toJSON(currentHW, true)); 65 + }62 66 } 63 67 64 68 } src/main/java/net/curisit/securis/utils/CryptoHelper.javadeleted 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 algorithm49 - *50 - * @param plainText51 - * @return The encrypted text in Base6452 - */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 algorithm89 - *90 - * @param plainText91 - * in Base6492 - * @return93 - */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 @@ 45 45 * @param type 46 46 * @return 47 47 */ 48 + @SuppressWarnings("unchecked")48 49 public static <T> T value(Object value, Class<T> type) { 49 50 50 51 return (T) value; .. .. @@ -52,8 +53,9 @@ 52 53 53 54 public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException { 54 55 try { 55 - if (json == null)56 + if (json == null){56 57 return null; 58 + }57 59 return MAPPER.readValue(json, type); 58 60 } catch (JsonParseException e) { 59 61 log.error("Error parsing JSON string to obejct: {}", json, e); .. .. @@ -78,8 +80,9 @@ 78 80 public static String toJSON(Object obj) throws SeCurisException { 79 81 // and could also do other configuration... 80 82 try { 81 - if (obj == null)83 + if (obj == null) {82 84 return null; 85 + }83 86 return MAPPER.writeValueAsString(obj); 84 87 } catch (JsonProcessingException e) { 85 88 log.error("Error formating JSON from object: {}", obj, e); .. .. @@ -100,8 +103,9 @@ 100 103 public static String toJSON(Object obj, boolean pretty) throws SeCurisException { 101 104 // and could also do other configuration... 102 105 try { 103 - if (obj == null)106 + if (obj == null) {104 107 return null; 108 + }105 109 MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT); 106 110 String json = MAPPER.writeValueAsString(obj); 107 111 MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT); .. .. @@ -124,11 +128,13 @@ 124 128 * String with json format 125 129 * @return 126 130 */ 131 + @SuppressWarnings("unchecked")127 132 public static Map<String, Object> json2map(String json) throws JsonParseException { 128 133 129 134 try { 130 - if (json == null)135 + if (json == null) {131 136 return null; 137 + }132 138 return MAPPER.readValue(json, Map.class); 133 139 } catch (JsonParseException e) { 134 140 log.error("Error parsing JSON string to map: {}", json, e); .. .. @@ -149,8 +155,9 @@ 149 155 public static String map2json(Map<String, Object> map) { 150 156 // and could also do other configuration... 151 157 try { 152 - if (map == null)158 + if (map == null) {153 159 return null; 160 + }154 161 return MAPPER.writeValueAsString(map); 155 162 } catch (JsonProcessingException e) { 156 163 log.error("Error formating JSON from map: {}", map, e); .. .. @@ -170,15 +177,17 @@ 170 177 * String with json format 171 178 * @return 172 179 */ 173 - public static List<Object> json2list(String json) {180 + @SuppressWarnings("unchecked")181 + public static List<Object> json2list(String json) throws SeCurisException {174 182 try { 175 183 return MAPPER.readValue(json, List.class); 176 184 } catch (JsonParseException e) { 177 185 log.error("Error converting JSON string to object {}", json, e); 186 + throw new SeCurisException("Error converting JSON to object", e);178 187 } catch (IOException e) { 179 188 log.error("Error converting JSON string to object {}", json, e); 189 + throw new SeCurisException("Error converting JSON to object", e);180 190 } 181 - return null;182 191 } 183 192 184 193 public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {