X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAuth%2FPassword.pm;h=20a9ed57c4bae3e0ad84fab23f12867b33988c82;hb=afd5395bfad1a84a590d567f8cb2d4e4967b16e3;hp=5a17203c53c5035ecc75d461190c5c73424346cb;hpb=1c385c602908735c3be266b1470b301050650fd3;p=kivitendo-erp.git diff --git a/SL/Auth/Password.pm b/SL/Auth/Password.pm index 5a17203c5..20a9ed57c 100644 --- a/SL/Auth/Password.pm +++ b/SL/Auth/Password.pm @@ -3,32 +3,53 @@ package SL::Auth::Password; use strict; use Carp; +use Digest::SHA (); +use Encode (); +use PBKDF2::Tiny (); -sub hash { +sub hash_pkkdf2 { my ($class, %params) = @_; - if (!$params{algorithm}) { - $params{algorithm} = 'SHA1'; - $params{fallback_algorithm} = 'MD5'; + # PBKDF2::Tiny expects data to be in octets. Therefore we must + # encode everything we hand over (login, password) to UTF-8. + + # This hash method uses a random hash and not just the user's login + # for its salt. This is due to the official recommendation that at + # least eight octets of random data should be used. Therefore we + # must store the salt together with the hashed password. The format + # in the database is: + + # {PBKDF2}salt-in-hex:hash-in-hex + + my $salt; + + if ((defined $params{stored_password}) && ($params{stored_password} =~ m/^\{PBKDF2\} ([0-9a-f]+) :/x)) { + $salt = (split m{:}, Encode::encode('utf-8', $1), 2)[0]; + + } else { + my @login = map { ord } split m{}, Encode::encode('utf-8', $params{login}); + my @random = map { int(rand(256)) } (0..16); + + $salt = join '', map { sprintf '%02x', $_ } @login, @random; } - if ($params{algorithm} eq 'SHA1') { - if (eval { require Digest::SHA1; 1 }) { - return '{SHA1}' . Digest::SHA1::sha1_hex($params{password}); + my $hashed = "{PBKDF2}${salt}:" . join('', map { sprintf '%02x', ord } split m{}, PBKDF2::Tiny::derive('SHA-256', $salt, Encode::encode('utf-8', $params{password}))); - } elsif ($params{fallback_algorithm}) { - return $class->hash_password(%params, algorithm => $params{fallback_algorithm}); + return $hashed; +} + +sub hash { + my ($class, %params) = @_; - } else { - die 'Digest::SHA1 not available'; - } + $params{algorithm} ||= 'PBKDF2'; - } elsif ($params{algorithm} eq 'MD5') { - require Digest::MD5; - return '{MD5}' . Digest::MD5::md5_hex($params{password}); + my $salt = $params{algorithm} =~ m/S$/ ? $params{login} : ''; - } elsif ($params{algorithm} eq 'CRYPT') { - return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2)); + if ($params{algorithm} =~ m/^SHA256/) { + return '{' . $params{algorithm} . '}' . Digest::SHA::sha256_hex($salt . $params{password}); + + } elsif ($params{algorithm} =~ m/^PBKDF2/) { + return $class->hash_pkkdf2(password => $params{password}, stored_password => $params{stored_password}); } else { croak 'Unsupported hash algorithm ' . $params{algorithm}; @@ -40,14 +61,22 @@ sub hash_if_unhashed { my ($algorithm, $password) = $class->parse($params{password}, 'NONE'); - return $algorithm eq 'NONE' ? $class->hash(%params) : $params{password}; + return $params{password} unless $algorithm eq 'NONE'; + + if ($params{look_up_algorithm}) { + my $stored_password = $params{auth}->get_stored_password($params{login}); + my ($stored_algorithm) = $class->parse($stored_password); + $params{algorithm} = $stored_algorithm; + } + + return $class->hash(%params); } sub parse { my ($class, $password, $default_algorithm) = @_; return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x; - return ($default_algorithm || 'CRYPT', $password); + return ($default_algorithm || 'PBKDF2', $password); } 1;