| | |
| | | * |
| | | * |
| | | * Copyright 2008 Sun Microsystems, Inc. |
| | | * Portions Copyright 2010 ForgeRock AS |
| | | * Portions Copyright 2010-2013 ForgeRock AS |
| | | * Portions Copyright 2012 Dariusz Janny <dariusz.janny@gmail.com> |
| | | * |
| | | */ |
| | | |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.Random; |
| | | |
| | | |
| | | import org.opends.messages.Message; |
| | | import org.opends.server.admin.server.ConfigurationChangeListener; |
| | | import org.opends.server.admin.std.server.PasswordStorageSchemeCfg; |
| | |
| | | import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString; |
| | | |
| | | |
| | | |
| | | /** |
| | | * This class defines a Directory Server password storage scheme based on the |
| | | * UNIX Crypt algorithm. This is a legacy one-way digest algorithm |
| | |
| | | extends PasswordStorageScheme<CryptPasswordStorageSchemeCfg> |
| | | implements ConfigurationChangeListener<CryptPasswordStorageSchemeCfg> |
| | | { |
| | | |
| | | /** |
| | | * The fully-qualified name of this class for debugging purposes. |
| | | */ |
| | |
| | | return ByteString.valueOf(output); |
| | | } |
| | | |
| | | private ByteString sha256CryptEncodePassword(ByteSequence plaintext) |
| | | throws DirectoryException { |
| | | String output; |
| | | try |
| | | { |
| | | output = Sha2Crypt.sha256Crypt(plaintext.toByteArray()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Message message = ERR_PWSCHEME_CANNOT_ENCODE_PASSWORD.get( |
| | | CLASS_NAME, stackTraceToSingleLineString(e)); |
| | | throw new DirectoryException( |
| | | DirectoryServer.getServerErrorResultCode(), message, e); |
| | | } |
| | | return ByteString.valueOf(output); |
| | | } |
| | | |
| | | private ByteString sha512CryptEncodePassword(ByteSequence plaintext) |
| | | throws DirectoryException { |
| | | String output; |
| | | try |
| | | { |
| | | output = Sha2Crypt.sha512Crypt(plaintext.toByteArray()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Message message = ERR_PWSCHEME_CANNOT_ENCODE_PASSWORD.get( |
| | | CLASS_NAME, stackTraceToSingleLineString(e)); |
| | | throw new DirectoryException( |
| | | DirectoryServer.getServerErrorResultCode(), message, e); |
| | | } |
| | | return ByteString.valueOf(output); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | |
| | | case MD5: |
| | | bytes = md5CryptEncodePassword(plaintext); |
| | | break; |
| | | case SHA256: |
| | | bytes = sha256CryptEncodePassword(plaintext); |
| | | break; |
| | | case SHA512: |
| | | bytes = sha512CryptEncodePassword(plaintext); |
| | | break; |
| | | } |
| | | return bytes; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private boolean sha256CryptPasswordMatches(ByteSequence plaintextPassword, |
| | | ByteSequence storedPassword) { |
| | | String storedString = storedPassword.toString(); |
| | | try |
| | | { |
| | | String userString = Sha2Crypt.sha256Crypt( |
| | | plaintextPassword.toByteArray(), storedString); |
| | | return userString.equals(storedString); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private boolean sha512CryptPasswordMatches(ByteSequence plaintextPassword, |
| | | ByteSequence storedPassword) { |
| | | String storedString = storedPassword.toString(); |
| | | try |
| | | { |
| | | String userString = Sha2Crypt.sha512Crypt( |
| | | plaintextPassword.toByteArray(), storedString); |
| | | return userString.equals(storedString); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | |
| | | { |
| | | return md5CryptPasswordMatches(plaintextPassword, storedPassword); |
| | | } |
| | | else if (storedString.startsWith(Sha2Crypt.getMagicSHA256Prefix())) |
| | | { |
| | | return sha256CryptPasswordMatches(plaintextPassword, storedPassword); |
| | | } |
| | | else if (storedString.startsWith(Sha2Crypt.getMagicSHA512Prefix())) |
| | | { |
| | | return sha512CryptPasswordMatches(plaintextPassword, storedPassword); |
| | | } |
| | | else |
| | | { |
| | | return unixCryptPasswordMatches(plaintextPassword, storedPassword); |