From dbcfeaefb0b9f81d30cd9062d6782d6a08da3d49 Mon Sep 17 00:00:00 2001 From: Shi Jinghai Date: Mon, 5 Dec 2016 01:32:51 +0000 Subject: [PATCH] Implemented: LoginWorker HashCrypt the type of hash for one-way encryption Added PBKDF2 hash type to encrypt password. Thanks: wangjunyuan for this contribution. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1772589 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/ofbiz/base/crypto/HashCrypt.java | 114 +++++++++++++++++- framework/security/config/security.properties | 6 +- .../data/PasswordSecurityDemoData.xml | 2 +- framework/security/entitydef/entitymodel.xml | 4 +- 4 files changed, 120 insertions(+), 6 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java b/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java index 3b71d8b0c9..1e0d29d4d4 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/crypto/HashCrypt.java @@ -22,6 +22,10 @@ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; + +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; @@ -29,11 +33,12 @@ import org.apache.commons.lang.RandomStringUtils; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.GeneralRuntimeException; import org.apache.ofbiz.base.util.StringUtil; -import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.base.util.UtilIO; +import org.apache.ofbiz.base.util.UtilProperties; +import org.apache.ofbiz.base.util.UtilValidate; /** - * Utility class for doing SHA-1/MD5 One-Way Hash Encryption + * Utility class for doing SHA-1/MD5/PBKDF2 One-Way Hash Encryption * */ public class HashCrypt { @@ -41,6 +46,16 @@ public class HashCrypt { public static final String module = HashCrypt.class.getName(); public static final String CRYPT_CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"; + private static final String PBKDF2_SHA1 ="pbkdf2_sha1"; + + private static final String PBKDF2_SHA256 ="pbkdf2_sha256"; + + private static final String PBKDF2_SHA384 ="pbkdf2_sha384"; + + private static final String PBKDF2_SHA512 ="pbkdf2_sha512"; + + private static final int PBKDF2_ITERATIONS = UtilProperties.getPropertyAsInteger("security.properties", "password.encrypt.pbkdf2.iterations", 1000); + public static MessageDigest getMessageDigest(String type) { try { return MessageDigest.getInstance(type); @@ -55,6 +70,8 @@ public class HashCrypt { return doCompareTypePrefix(crypted, defaultCrypt, password.getBytes()); } else if (crypted.startsWith("$")) { return doComparePosix(crypted, defaultCrypt, password.getBytes(UtilIO.getUtf8())); + } else if (crypted.startsWith("pbkdf2")) { + return doComparePbkdf2(crypted, password); } else { // FIXME: should have been getBytes("UTF-8") originally return doCompareBare(crypted, defaultCrypt, password.getBytes()); @@ -106,15 +123,24 @@ public class HashCrypt { */ @Deprecated public static String cryptPassword(String hashType, String salt, String password) { + if (hashType.startsWith("PBKDF2")) { + return password != null ? pbkdf2HashCrypt(hashType, salt, password) : null; + } // FIXME: should have been getBytes("UTF-8") originally return password != null ? cryptBytes(hashType, salt, password.getBytes()) : null; } public static String cryptUTF8(String hashType, String salt, String value) { + if (hashType.startsWith("PBKDF2")) { + return value != null ? pbkdf2HashCrypt(hashType, salt, value) : null; + } return value != null ? cryptBytes(hashType, salt, value.getBytes(UtilIO.getUtf8())) : null; } public static String cryptValue(String hashType, String salt, String value) { + if (hashType.startsWith("PBKDF2")) { + return value != null ? pbkdf2HashCrypt(hashType, salt, value) : null; + } return value != null ? cryptBytes(hashType, salt, value.getBytes()) : null; } @@ -142,6 +168,90 @@ public class HashCrypt { } } + public static String pbkdf2HashCrypt(String hashType, String salt, String value){ + char[] chars = value.toCharArray(); + if (UtilValidate.isEmpty(salt)) { + salt = getSalt(); + } + try { + PBEKeySpec spec = new PBEKeySpec(chars, salt.getBytes(UtilIO.getUtf8()), PBKDF2_ITERATIONS, 64 * 4); + SecretKeyFactory skf = SecretKeyFactory.getInstance(hashType); + byte[] hash = Base64.encodeBase64(skf.generateSecret(spec).getEncoded()); + String pbkdf2Type = null; + switch (hashType) { + case "PBKDF2WithHmacSHA1": + pbkdf2Type = PBKDF2_SHA1; + break; + case "PBKDF2WithHmacSHA256": + pbkdf2Type = PBKDF2_SHA256; + break; + case "PBKDF2WithHmacSHA384": + pbkdf2Type = PBKDF2_SHA384; + break; + case "PBKDF2WithHmacSHA512": + pbkdf2Type = PBKDF2_SHA512; + break; + default: + pbkdf2Type = PBKDF2_SHA1; + } + return pbkdf2Type + "$" + PBKDF2_ITERATIONS + "$" + salt + "$" + new String(hash); + } catch (InvalidKeySpecException e) { + throw new GeneralRuntimeException("Error while creating SecretKey", e); + } catch (NoSuchAlgorithmException e) { + throw new GeneralRuntimeException("Error while computing SecretKeyFactory", e); + } + } + + public static boolean doComparePbkdf2(String storedPassword, String originalPassword){ + try { + String[] parts = storedPassword.split("\\$"); + String hashHead = parts[0]; + int iterations = Integer.parseInt(parts[1]); + byte[] salt = parts[2].getBytes(); + byte[] hash = Base64.decodeBase64(parts[3].getBytes()); + + PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, iterations, hash.length * 8); + String hashType = null; + switch (hashHead.substring(hashHead.indexOf("_")+4)) { + case "256": + hashType = "PBKDF2WithHmacSHA256"; + break; + case "384": + hashType = "PBKDF2WithHmacSHA384"; + break; + case "512": + hashType = "PBKDF2WithHmacSHA512"; + break; + default: + hashType = "PBKDF2WithHmacSHA1"; + } + SecretKeyFactory skf = SecretKeyFactory.getInstance(hashType); + byte[] testHash = skf.generateSecret(spec).getEncoded(); + int diff = hash.length ^ testHash.length; + + for (int i = 0; i < hash.length && i < testHash.length; i++) { + diff |= hash[i] ^ testHash[i]; + } + + return diff == 0; + } catch (NoSuchAlgorithmException e) { + throw new GeneralRuntimeException("Error while computing SecretKeyFactory", e); + } catch (InvalidKeySpecException e) { + throw new GeneralRuntimeException("Error while creating SecretKey", e); + } + } + + private static String getSalt() { + try { + SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); + byte[] salt = new byte[16]; + sr.nextBytes(salt); + return salt.toString(); + } catch (NoSuchAlgorithmException e) { + throw new GeneralRuntimeException("Error while creating salt", e); + } + } + /** * @deprecated use digestHash("SHA", null, str) */ diff --git a/framework/security/config/security.properties b/framework/security/config/security.properties index 530cbef087..1fe9452aff 100644 --- a/framework/security/config/security.properties +++ b/framework/security/config/security.properties @@ -82,9 +82,13 @@ password.encrypt=true password.email_password.require_password_change=true # -- specify the type of hash to use for one-way encryption, will be passed to java.security.MessageDigest.getInstance() -- -# -- options may include: SHA, MD5, etc +# -- options may include: SHA, MD5, PBKDF2WithHmacSHA1, PBKDF2WithHmacSHA256, PBKDF2WithHmacSHA384, PBKDF2WithHmacSHA512 and etc password.encrypt.hash.type=SHA +# -- if the type of hash to use for one-way encryption is PBKDF2WithHmacSHA1 or PBKDF2WithHmacSHA256 or PBKDF2WithHmacSHA384 or PBKDF2WithHmacSHA512 +# -- the type of hash to use for one-way encryption needs iteration +password.encrypt.pbkdf2.iterations=1000 + # -- this is helpful to recover old accounts or to be able to login at all sometimes -- # -- SHOULD GENERALLY NOT BE TRUE FOR PRODUCTION SITES, but is useful for interim periods when going to password encryption -- password.accept.encrypted.and.plain=false diff --git a/framework/security/data/PasswordSecurityDemoData.xml b/framework/security/data/PasswordSecurityDemoData.xml index f7b6621ce0..8e6d66c1c3 100644 --- a/framework/security/data/PasswordSecurityDemoData.xml +++ b/framework/security/data/PasswordSecurityDemoData.xml @@ -21,7 +21,7 @@ under the License. - + diff --git a/framework/security/entitydef/entitymodel.xml b/framework/security/entitydef/entitymodel.xml index c1a1b595f1..ba5cef934e 100644 --- a/framework/security/entitydef/entitymodel.xml +++ b/framework/security/entitydef/entitymodel.xml @@ -62,7 +62,7 @@ under the License. package-name="org.apache.ofbiz.security.login" title="User Login Entity"> - + @@ -89,7 +89,7 @@ under the License. - +