| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.providers.ldap.authenticator; |
| 17 |
|
|
| 18 |
|
import org.acegisecurity.ldap.LdapDataAccessException; |
| 19 |
|
|
| 20 |
|
import org.acegisecurity.providers.encoding.PasswordEncoder; |
| 21 |
|
import org.acegisecurity.providers.encoding.ShaPasswordEncoder; |
| 22 |
|
|
| 23 |
|
import org.apache.commons.codec.binary.Base64; |
| 24 |
|
|
| 25 |
|
import org.springframework.util.Assert; |
| 26 |
|
|
| 27 |
|
import java.security.MessageDigest; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
public class LdapShaPasswordEncoder implements PasswordEncoder { |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
private static final int SHA_LENGTH = 20; |
| 43 |
|
private static final String SSHA_PREFIX = "{SSHA}"; |
| 44 |
1 |
private static final String SSHA_PREFIX_LC = SSHA_PREFIX.toLowerCase(); |
| 45 |
|
private static final String SHA_PREFIX = "{SHA}"; |
| 46 |
1 |
private static final String SHA_PREFIX_LC = SHA_PREFIX.toLowerCase(); |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
private boolean forceLowerCasePrefix; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
19 |
public LdapShaPasswordEncoder() {} |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
private byte[] combineHashAndSalt(byte[] hash, byte[] salt) { |
| 58 |
18 |
if (salt == null) { |
| 59 |
10 |
return hash; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
8 |
byte[] hashAndSalt = new byte[hash.length + salt.length]; |
| 63 |
8 |
System.arraycopy(hash, 0, hashAndSalt, 0, hash.length); |
| 64 |
8 |
System.arraycopy(salt, 0, hashAndSalt, hash.length, salt.length); |
| 65 |
|
|
| 66 |
8 |
return hashAndSalt; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
public String encodePassword(String rawPass, Object salt) { |
| 80 |
|
MessageDigest sha; |
| 81 |
|
|
| 82 |
|
try { |
| 83 |
19 |
sha = MessageDigest.getInstance("SHA"); |
| 84 |
0 |
} catch (java.security.NoSuchAlgorithmException e) { |
| 85 |
0 |
throw new LdapDataAccessException("No SHA implementation available!", e); |
| 86 |
19 |
} |
| 87 |
|
|
| 88 |
19 |
sha.update(rawPass.getBytes()); |
| 89 |
|
|
| 90 |
19 |
if (salt != null) { |
| 91 |
10 |
Assert.isInstanceOf(byte[].class, salt, "Salt value must be a byte array"); |
| 92 |
8 |
sha.update((byte[]) salt); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
18 |
byte[] hash = combineHashAndSalt(sha.digest(), (byte[]) salt); |
| 96 |
|
|
| 97 |
|
String prefix; |
| 98 |
|
|
| 99 |
18 |
if (salt == null) { |
| 100 |
10 |
prefix = forceLowerCasePrefix ? SHA_PREFIX_LC : SHA_PREFIX; |
| 101 |
|
} else { |
| 102 |
8 |
prefix = forceLowerCasePrefix ? SSHA_PREFIX_LC : SSHA_PREFIX; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
18 |
return prefix + new String(Base64.encodeBase64(hash)); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
private byte[] extractSalt(String encPass) { |
| 109 |
6 |
String encPassNoLabel = encPass.substring(6); |
| 110 |
|
|
| 111 |
6 |
byte[] hashAndSalt = Base64.decodeBase64(encPassNoLabel.getBytes()); |
| 112 |
6 |
int saltLength = hashAndSalt.length - SHA_LENGTH; |
| 113 |
6 |
byte[] salt = new byte[saltLength]; |
| 114 |
6 |
System.arraycopy(hashAndSalt, SHA_LENGTH, salt, 0, saltLength); |
| 115 |
|
|
| 116 |
6 |
return salt; |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
public boolean isPasswordValid(String encPass, String rawPass, Object salt) { |
| 130 |
|
String encPassWithoutPrefix; |
| 131 |
|
|
| 132 |
13 |
if (encPass.startsWith(SSHA_PREFIX) || encPass.startsWith(SSHA_PREFIX_LC)) { |
| 133 |
6 |
encPassWithoutPrefix = encPass.substring(6); |
| 134 |
6 |
salt = extractSalt(encPass); |
| 135 |
|
} else { |
| 136 |
7 |
encPassWithoutPrefix = encPass.substring(5); |
| 137 |
7 |
salt = null; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
13 |
return encodePassword(rawPass, salt).endsWith(encPassWithoutPrefix); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
public void setForceLowerCasePrefix(boolean forceLowerCasePrefix) { |
| 145 |
6 |
this.forceLowerCasePrefix = forceLowerCasePrefix; |
| 146 |
6 |
} |
| 147 |
|
} |