You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CipherUtils.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config;
  23. import com.dmdirc.interfaces.IdentityController;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.logger.Logger;
  26. import java.io.IOException;
  27. import java.io.UnsupportedEncodingException;
  28. import java.nio.charset.Charset;
  29. import java.security.InvalidAlgorithmParameterException;
  30. import java.security.InvalidKeyException;
  31. import java.security.MessageDigest;
  32. import java.security.NoSuchAlgorithmException;
  33. import java.security.spec.AlgorithmParameterSpec;
  34. import java.security.spec.InvalidKeySpecException;
  35. import java.security.spec.KeySpec;
  36. import javax.crypto.BadPaddingException;
  37. import javax.crypto.Cipher;
  38. import javax.crypto.IllegalBlockSizeException;
  39. import javax.crypto.NoSuchPaddingException;
  40. import javax.crypto.SecretKey;
  41. import javax.crypto.SecretKeyFactory;
  42. import javax.crypto.spec.PBEKeySpec;
  43. import javax.crypto.spec.PBEParameterSpec;
  44. import net.miginfocom.Base64;
  45. /**
  46. * Helper class to encrypt and decrypt strings, requests passwords if needed.
  47. */
  48. public abstract class CipherUtils {
  49. /** Salt. */
  50. private static final byte[] SALT = {
  51. (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
  52. (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03,
  53. };
  54. /** Iteration count. */
  55. private static final int ITERATIONS = 19;
  56. /** Number of auth attemps before failing the attempt. */
  57. private static final int AUTH_TRIES = 4;
  58. /** The identity controller to use for reading/writing settings. */
  59. private final IdentityController identityController;
  60. /** Encryption cipher. */
  61. private Cipher ecipher;
  62. /** Decryption cipher. */
  63. private Cipher dcipher;
  64. /** User password. */
  65. private String password;
  66. /**
  67. * Creates a new instance of {@link CipherUtils}.
  68. *
  69. * @param identityController The controller to use to read/write settings.
  70. */
  71. public CipherUtils(final IdentityController identityController) {
  72. this.identityController = identityController;
  73. }
  74. /**
  75. * Encrypts a string using the stored settings. Will return null if the
  76. * automatic user authentication fails - use checkauth and auth.
  77. * @param str String to encrypt
  78. * @return Encrypted string
  79. */
  80. public String encrypt(final String str) {
  81. if (!checkAuthed()) {
  82. if (auth()) {
  83. createCiphers();
  84. } else {
  85. return null;
  86. }
  87. }
  88. try {
  89. return Base64.encodeToString(ecipher.doFinal(str.getBytes("UTF8")), false);
  90. } catch (BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
  91. Logger.userError(ErrorLevel.LOW, "Unable to decrypt string: " + e.getMessage());
  92. }
  93. return null;
  94. }
  95. /**
  96. * Encrypts a string using the stored settings. Will return null if the
  97. * automatic user authentication fails - use checkauth and auth.
  98. * @param str String to decrypt
  99. * @return Decrypted string
  100. */
  101. public String decrypt(final String str) {
  102. if (!checkAuthed()) {
  103. if (auth()) {
  104. createCiphers();
  105. } else {
  106. return null;
  107. }
  108. }
  109. try {
  110. return new String(dcipher.doFinal(Base64.decode(str)));
  111. } catch (BadPaddingException | IllegalBlockSizeException e) {
  112. Logger.userError(ErrorLevel.LOW, "Unable to decrypt string: " + e.getMessage());
  113. }
  114. return null;
  115. }
  116. /**
  117. * Performs a SHA-512 hash.
  118. * @param data String to hashed
  119. * @return hashed string
  120. */
  121. public String hash(final String data) {
  122. try {
  123. return new String(MessageDigest.getInstance("SHA-512")
  124. .digest(data.getBytes("UTF8")), Charset.forName("UTF-8"));
  125. } catch (NoSuchAlgorithmException | IOException e) {
  126. Logger.userError(ErrorLevel.LOW, "Unable to hash string");
  127. }
  128. return null;
  129. }
  130. /**
  131. * Checks if a user is authed.
  132. *
  133. * @return true if authed, false otherwise
  134. */
  135. public boolean checkAuthed() {
  136. return dcipher != null && ecipher != null;
  137. }
  138. /**
  139. * creates ciphers.
  140. */
  141. protected void createCiphers() {
  142. try {
  143. final KeySpec keySpec = new PBEKeySpec(
  144. password.toCharArray(), SALT, ITERATIONS);
  145. final SecretKey key = SecretKeyFactory.
  146. getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
  147. ecipher = Cipher.getInstance(key.getAlgorithm());
  148. dcipher = Cipher.getInstance(key.getAlgorithm());
  149. final AlgorithmParameterSpec paramSpec =
  150. new PBEParameterSpec(SALT, ITERATIONS);
  151. ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  152. dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  153. } catch (InvalidAlgorithmParameterException | InvalidKeySpecException |
  154. NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException e) {
  155. Logger.userError(ErrorLevel.LOW, "Unable to create ciphers");
  156. ecipher = null;
  157. dcipher = null;
  158. }
  159. }
  160. /**
  161. * Auths a user and sets the password.
  162. *
  163. * @return true if auth was successful, false otherwise.
  164. */
  165. public boolean auth() {
  166. final ConfigManager configManager = identityController.getGlobalConfiguration();
  167. String passwordHash = null;
  168. String prompt = "Please enter your password";
  169. int tries = 1;
  170. if (configManager.hasOptionString("encryption", "password")) {
  171. password = configManager.getOption("encryption", "password");
  172. } else {
  173. if (configManager.hasOptionString("encryption", "passwordHash")) {
  174. passwordHash = configManager.getOption("encryption", "passwordHash");
  175. }
  176. while ((password == null || password.isEmpty()) && tries < AUTH_TRIES) {
  177. password = getPassword(prompt);
  178. if (passwordHash == null) {
  179. passwordHash = hash(password);
  180. identityController.getGlobalConfigIdentity()
  181. .setOption("encryption", "passwordHash", passwordHash);
  182. }
  183. if (!hash(password).equals(passwordHash)) {
  184. prompt = "<html>Password mis-match<br>Please re-enter "
  185. + "your password</html>";
  186. tries++;
  187. password = null;
  188. }
  189. }
  190. }
  191. if (tries == AUTH_TRIES) {
  192. return false;
  193. }
  194. return true;
  195. }
  196. /**
  197. * Requests the encryption password from the user.
  198. *
  199. * @param prompt The prompt to show
  200. * @return The user-specified password
  201. */
  202. protected abstract String getPassword(final String prompt);
  203. }