| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | Authen::Passphrase::SaltedDigest - passphrases using the generic salted | ||||||||||||||||||||||||||||||||||
| 4 | digest algorithm | ||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | use Authen::Passphrase::SaltedDigest; | ||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | $ppr = Authen::Passphrase::SaltedDigest->new( | ||||||||||||||||||||||||||||||||||
| 11 | algorithm => "SHA-1", | ||||||||||||||||||||||||||||||||||
| 12 | salt_hex => "a9f524b1e819e96d8cc7". | ||||||||||||||||||||||||||||||||||
| 13 | "a04d5471e8b10c84e596", | ||||||||||||||||||||||||||||||||||
| 14 | hash_hex => "8270d9d1a345d3806ab2". | ||||||||||||||||||||||||||||||||||
| 15 | "3b0385702e10f1acc943"); | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | $ppr = Authen::Passphrase::SaltedDigest->new( | ||||||||||||||||||||||||||||||||||
| 18 | algorithm => "SHA-1", salt_random => 20, | ||||||||||||||||||||||||||||||||||
| 19 | passphrase => "passphrase"); | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | $ppr = Authen::Passphrase::SaltedDigest->from_rfc2307( | ||||||||||||||||||||||||||||||||||
| 22 | "{SSHA}gnDZ0aNF04BqsjsDhXAuEPGsy". | ||||||||||||||||||||||||||||||||||
| 23 | "UOp9SSx6BnpbYzHoE1UceixDITllg=="); | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | $algorithm = $ppr->algorithm; | ||||||||||||||||||||||||||||||||||
| 26 | $salt = $ppr->salt; | ||||||||||||||||||||||||||||||||||
| 27 | $salt_hex = $ppr->salt_hex; | ||||||||||||||||||||||||||||||||||
| 28 | $hash = $ppr->hash; | ||||||||||||||||||||||||||||||||||
| 29 | $hash_hex = $ppr->hash_hex; | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | if($ppr->match($passphrase)) { ... | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | $userPassword = $ppr->as_rfc2307; | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | An object of this class encapsulates a passphrase hashed using | ||||||||||||||||||||||||||||||||||
| 38 | a generic digest-algorithm-based scheme. This is a subclass of | ||||||||||||||||||||||||||||||||||
| 39 | L<Authen::Passphrase>, and this document assumes that the reader is | ||||||||||||||||||||||||||||||||||
| 40 | familiar with the documentation for that class. | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | The salt is an arbitrary string of bytes. It is appended to passphrase, | ||||||||||||||||||||||||||||||||||
| 43 | and the combined string is passed through a specified message digest | ||||||||||||||||||||||||||||||||||
| 44 | algorithm. The output of the message digest algorithm is the passphrase | ||||||||||||||||||||||||||||||||||
| 45 | hash. | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | The strength depends entirely on the choice of digest algorithm, so | ||||||||||||||||||||||||||||||||||
| 48 | choose according to the level of security required. SHA-1 is suitable for | ||||||||||||||||||||||||||||||||||
| 49 | most applications, but recent work has revealed weaknesses in the basic | ||||||||||||||||||||||||||||||||||
| 50 | structure of MD5, SHA-1, SHA-256, and all similar digest algorithms. | ||||||||||||||||||||||||||||||||||
| 51 | A new generation of digest algorithms emerged in 2008, centred around | ||||||||||||||||||||||||||||||||||
| 52 | NIST's competition to design SHA-3. Once these algorithms have been | ||||||||||||||||||||||||||||||||||
| 53 | subjected to sufficient cryptanalysis, the survivors will be preferred | ||||||||||||||||||||||||||||||||||
| 54 | over SHA-1 and its generation. | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | Digest algorithms are generally designed to be as efficient to compute | ||||||||||||||||||||||||||||||||||
| 57 | as possible for their level of cryptographic strength. An unbroken | ||||||||||||||||||||||||||||||||||
| 58 | digest algorithm makes brute force the most efficient way to attack it, | ||||||||||||||||||||||||||||||||||
| 59 | but makes no effort to resist a brute force attack. This is a concern | ||||||||||||||||||||||||||||||||||
| 60 | in some passphrase-using applications. | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | The use of this kind of passphrase scheme is generally recommended for | ||||||||||||||||||||||||||||||||||
| 63 | new systems. Choice of digest algorithm is important: SHA-1 is suitable | ||||||||||||||||||||||||||||||||||
| 64 | for most applications. If efficiency of brute force attack is a concern, | ||||||||||||||||||||||||||||||||||
| 65 | see L<Authen::Passphrase::BlowfishCrypt> for an algorithm designed to | ||||||||||||||||||||||||||||||||||
| 66 | be expensive to compute. | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | package Authen::Passphrase::SaltedDigest; | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | { use 5.006; } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | use Authen::Passphrase 0.003; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | use Crypt::SysRandom 'random_bytes'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | use Digest 1.00; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | use MIME::Base64 2.21 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 81 | 3 | ▶ | use Module::Runtime 0.011 qw(is_valid_module_name use_module); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | use Params::Classify 0.000 qw(is_string is_blessed); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 3 | our $VERSION = "0.009"; | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | use parent "Authen::Passphrase"; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | =over | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | =item Authen::Passphrase::SaltedDigest->new(ATTR => VALUE, ...) | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | Generates a new passphrase recogniser object using the generic salted | ||||||||||||||||||||||||||||||||||
| 95 | digest algorithm. The following attributes may be given: | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | =over | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | =item B<algorithm> | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | Specifies the algorithm to use. If it is a reference to a blessed object, | ||||||||||||||||||||||||||||||||||
| 102 | it must be possible to call the L</new> method on that object to generate | ||||||||||||||||||||||||||||||||||
| 103 | a digest context object. | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | If it is a string containing the subsequence "::" then it specifies | ||||||||||||||||||||||||||||||||||
| 106 | a module to use. A plain package name in bareword syntax, optionally | ||||||||||||||||||||||||||||||||||
| 107 | preceded by "::" (so that top-level packages can be recognised as such), | ||||||||||||||||||||||||||||||||||
| 108 | is taken as a class name, on which the L</new> method will be called to | ||||||||||||||||||||||||||||||||||
| 109 | generate a digest context object. The package name may optionally be | ||||||||||||||||||||||||||||||||||
| 110 | followed by "-" to cause automatic loading of the module, and the "-" | ||||||||||||||||||||||||||||||||||
| 111 | (if present) may optionally be followed by a version number that will | ||||||||||||||||||||||||||||||||||
| 112 | be checked against. For example, "Digest::MD5-1.99_53" would load the | ||||||||||||||||||||||||||||||||||
| 113 | L<Digest::MD5> module and check that it is at least version 1.99_53 | ||||||||||||||||||||||||||||||||||
| 114 | (which is the first version that can be used by this module). | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | A string not containing "::" and which is understood by | ||||||||||||||||||||||||||||||||||
| 117 | L<< Digest->new|Digest/"OO INTERFACE" >> will be passed to that function | ||||||||||||||||||||||||||||||||||
| 118 | to generate a digest context object. | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | Any other type of algorithm specifier has undefined behaviour. | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | The digest context objects must support at least the standard C<add> | ||||||||||||||||||||||||||||||||||
| 123 | and C<digest> methods. | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | =item B<salt> | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | The salt, as a raw string of bytes. Defaults to the empty string, | ||||||||||||||||||||||||||||||||||
| 128 | yielding an unsalted scheme. | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | =item B<salt_hex> | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | The salt, as a string of hexadecimal digits. Defaults to the empty | ||||||||||||||||||||||||||||||||||
| 133 | string, yielding an unsalted scheme. | ||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | =item B<salt_random> | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | Causes salt to be generated randomly. The value given for this | ||||||||||||||||||||||||||||||||||
| 138 | attribute must be a non-negative integer, giving the number of bytes | ||||||||||||||||||||||||||||||||||
| 139 | of salt to generate. (The same length as the hash is recommended.) | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | =item B<hash> | ||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | The hash, as a string of bytes. | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | =item B<hash_hex> | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | The hash, as a string of hexadecimal digits. | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | =item B<passphrase> | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | A passphrase that will be accepted. | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | The digest algorithm must be given, and either the hash or the passphrase. | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=30 SCAR=39.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 160 | 14 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 161 | 14 | my $self = bless({}, $class); | |||||||||||||||||||||||||||||||||
| 162 | 14 | my $passphrase; | |||||||||||||||||||||||||||||||||
| 163 | 14 | ▶ | while(@_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 164 | 42 | my $attr = shift; | |||||||||||||||||||||||||||||||||
| 165 | 42 | my $value = shift; | |||||||||||||||||||||||||||||||||
| 166 | 42 | ▶ | if($attr eq "algorithm") { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 167 | croak "algorithm specified redundantly" | ||||||||||||||||||||||||||||||||||
| 168 | 14 | ▶ | if exists $self->{algorithm}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 169 | 14 | $self->{algorithm} = $value; | |||||||||||||||||||||||||||||||||
| 170 | } elsif($attr eq "salt") { | ||||||||||||||||||||||||||||||||||
| 171 | croak "salt specified redundantly" | ||||||||||||||||||||||||||||||||||
| 172 | 7 | ▶ | if exists $self->{salt}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 173 | 7 | ▶ | $value =~ m#\A[\x00-\xff]*\z# | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 174 | or croak "\"$value\" is not a valid salt"; | ||||||||||||||||||||||||||||||||||
| 175 | 7 | $self->{salt} = "$value"; | |||||||||||||||||||||||||||||||||
| 176 | } elsif($attr eq "salt_hex") { | ||||||||||||||||||||||||||||||||||
| 177 | croak "salt specified redundantly" | ||||||||||||||||||||||||||||||||||
| 178 | 6 | ▶ | if exists $self->{salt}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 179 | 6 | ▶ | $value =~ m#\A(?:[0-9A-Fa-f]{2})+\z# | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 180 | or croak "\"$value\" is not a valid salt"; | ||||||||||||||||||||||||||||||||||
| 181 | 6 | $self->{salt} = pack("H*", $value); | |||||||||||||||||||||||||||||||||
| 182 | } elsif($attr eq "salt_random") { | ||||||||||||||||||||||||||||||||||
| 183 | croak "salt specified redundantly" | ||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | if exists $self->{salt}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | croak "\"$value\" is not a valid salt length" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$value == int $value and $value >= 0
MC/DC0%0/2
$value == int $value and $value >= 0
$value == int $value
$value >= 0
Truth table33%1/3
A and B
A= $value == int $valueB= $value >= 0
| |||||||||||||||||||||||||||||||||||
| 186 | unless $value == int($value) && $value >= 0; | ||||||||||||||||||||||||||||||||||
| 187 | 1 | $self->{salt} = random_bytes($value); | |||||||||||||||||||||||||||||||||
| 188 | } elsif($attr eq "hash") { | ||||||||||||||||||||||||||||||||||
| 189 | croak "hash specified redundantly" | ||||||||||||||||||||||||||||||||||
| 190 | 6 | ▶ | if exists($self->{hash}) || | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"}
defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
| |||||||||||||||||||||||||||||||||||
| 191 | defined($passphrase); | ||||||||||||||||||||||||||||||||||
| 192 | 6 | ▶ | $value =~ m#\A[\x00-\xff]*\z# | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 193 | or croak "\"$value\" is not a valid hash"; | ||||||||||||||||||||||||||||||||||
| 194 | 6 | $self->{hash} = "$value"; | |||||||||||||||||||||||||||||||||
| 195 | } elsif($attr eq "hash_hex") { | ||||||||||||||||||||||||||||||||||
| 196 | croak "hash specified redundantly" | ||||||||||||||||||||||||||||||||||
| 197 | 6 | ▶ | if exists($self->{hash}) || | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"}
defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
| |||||||||||||||||||||||||||||||||||
| 198 | defined($passphrase); | ||||||||||||||||||||||||||||||||||
| 199 | 6 | ▶ | $value =~ m#\A(?:[0-9A-Fa-f]{2})+\z# | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 200 | or croak "\"$value\" is not a valid hash"; | ||||||||||||||||||||||||||||||||||
| 201 | 6 | $self->{hash} = pack("H*", $value); | |||||||||||||||||||||||||||||||||
| 202 | } elsif($attr eq "passphrase") { | ||||||||||||||||||||||||||||||||||
| 203 | croak "passphrase specified redundantly" | ||||||||||||||||||||||||||||||||||
| 204 | 2 | ▶ | if exists($self->{hash}) || | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"}
defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
| |||||||||||||||||||||||||||||||||||
| 205 | defined($passphrase); | ||||||||||||||||||||||||||||||||||
| 206 | 2 | $passphrase = $value; | |||||||||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||||||||
| 208 | 0 | croak "unrecognised attribute `$attr'"; | |||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | 14 | ▶ | croak "algorithm not specified" unless exists $self->{algorithm}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 212 | 14 | ▶ | $self->{salt} = "" unless exists $self->{salt}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 213 | 14 | ▶ | if(defined $passphrase) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 214 | 2 | $self->{hash} = $self->_hash_of($passphrase); | |||||||||||||||||||||||||||||||||
| 215 | } elsif(exists $self->{hash}) { | ||||||||||||||||||||||||||||||||||
| 216 | croak "not a valid ".$self->{algorithm}." hash" | ||||||||||||||||||||||||||||||||||
| 217 | 12 | ▶ | unless length($self->{hash}) == | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 218 | length($self->_hash_of("")); | ||||||||||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||||||||
| 220 | 0 | croak "hash not specified"; | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | 14 | return $self; | |||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||
| 225 | =item Authen::Passphrase::SaltedDigest->from_rfc2307(USERPASSWORD) | ||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||
| 227 | Generates a salted-digest passphrase recogniser from the supplied | ||||||||||||||||||||||||||||||||||
| 228 | RFC2307 encoding. The scheme identifier gives the digest algorithm and | ||||||||||||||||||||||||||||||||||
| 229 | controls whether salt is permitted. It is followed by a base 64 string, | ||||||||||||||||||||||||||||||||||
| 230 | using standard MIME base 64, which encodes the concatenation of the hash | ||||||||||||||||||||||||||||||||||
| 231 | and salt. | ||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | The scheme identifiers accepted are "B<{MD4}>" (unsalted MD4), "B<{MD5}>" | ||||||||||||||||||||||||||||||||||
| 234 | (unsalted MD5), "B<{RMD160}>" (unsalted RIPEMD-160), "B<{SHA}>" (unsalted | ||||||||||||||||||||||||||||||||||
| 235 | SHA-1), "B<{SMD5}>" (salted MD5), and "B<{SSHA}>" (salted SHA-1). | ||||||||||||||||||||||||||||||||||
| 236 | All scheme identifiers are recognised case-insensitively. | ||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||
| 238 | =cut | ||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||
| 240 | 3 | my %rfc2307_scheme_meaning = ( | |||||||||||||||||||||||||||||||||
| 241 | "MD4" => ["MD4", 16, 0], | ||||||||||||||||||||||||||||||||||
| 242 | "MD5" => ["MD5", 16, 0], | ||||||||||||||||||||||||||||||||||
| 243 | "RMD160" => ["Crypt::RIPEMD160-", 20, 0], | ||||||||||||||||||||||||||||||||||
| 244 | "SHA" => ["SHA-1", 20, 0], | ||||||||||||||||||||||||||||||||||
| 245 | "SMD5" => ["MD5", 16, 1], | ||||||||||||||||||||||||||||||||||
| 246 | "SSHA" => ["SHA-1", 20, 1], | ||||||||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | 2 | ▶ | sub from_rfc2307 { | ||||||||||||||||||||||||||||||||
|
Subroutine
from_rfc2307: called CC=7 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 250 | 2 | my($class, $userpassword) = @_; | |||||||||||||||||||||||||||||||||
| 251 | 2 | ▶ | return $class->SUPER::from_rfc2307($userpassword) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 252 | unless $userpassword =~ /\A\{([-0-9A-Za-z]+)\}/; | ||||||||||||||||||||||||||||||||||
| 253 | 2 | my $scheme = uc($1); | |||||||||||||||||||||||||||||||||
| 254 | 2 | my $meaning = $rfc2307_scheme_meaning{$scheme}; | |||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | return $class->SUPER::from_rfc2307($userpassword) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 256 | unless defined $meaning; | ||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | croak "malformed {$scheme} data" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 258 | unless $userpassword =~ | ||||||||||||||||||||||||||||||||||
| 259 | m#\A\{.*?\} | ||||||||||||||||||||||||||||||||||
| 260 | ((?>(?:[A-Za-z0-9+/]{4})*) | ||||||||||||||||||||||||||||||||||
| 261 | (?:|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=| | ||||||||||||||||||||||||||||||||||
| 262 | [A-Za-z0-9+/][AQgw]==))\z#x; | ||||||||||||||||||||||||||||||||||
| 263 | 2 | my $b64 = $1; | |||||||||||||||||||||||||||||||||
| 264 | 2 | my $hash_and_salt = decode_base64($b64); | |||||||||||||||||||||||||||||||||
| 265 | 2 | my($algorithm, $hash_len, $salt_allowed) = @$meaning; | |||||||||||||||||||||||||||||||||
| 266 | 2 | ▶ | croak "insufficient hash data for {$scheme}" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 267 | if length($hash_and_salt) < $hash_len; | ||||||||||||||||||||||||||||||||||
| 268 | 2 | ▶ | croak "too much hash data for {$scheme}" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $salt_allowed and length $hash_and_salt > $hash_len
MC/DC0%0/2
not $salt_allowed and length $hash_and_salt > $hash_len
not $salt_allowed
length $hash_and_salt > $hash_len
Truth table33%1/3
not(A) and B
A= not $salt_allowedB= length $hash_and_salt > $hash_len
| |||||||||||||||||||||||||||||||||||
| 269 | if !$salt_allowed && length($hash_and_salt) > $hash_len; | ||||||||||||||||||||||||||||||||||
| 270 | 2 | return $class->new(algorithm => $algorithm, | |||||||||||||||||||||||||||||||||
| 271 | salt => substr($hash_and_salt, $hash_len), | ||||||||||||||||||||||||||||||||||
| 272 | hash => substr($hash_and_salt, 0, $hash_len)); | ||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | =back | ||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||
| 277 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||
| 279 | =over | ||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | =item $ppr->algorithm | ||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | Returns the digest algorithm, in the same form as supplied to the | ||||||||||||||||||||||||||||||||||
| 284 | constructor. | ||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||
| 288 | 2 | ▶ | sub algorithm { | ||||||||||||||||||||||||||||||||
|
Subroutine
algorithm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 289 | 2 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 290 | 2 | return $self->{algorithm}; | |||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||
| 293 | =item $ppr->salt | ||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||
| 295 | Returns the salt, in raw form. | ||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||
| 299 | 12 | ▶ | sub salt { | ||||||||||||||||||||||||||||||||
|
Subroutine
salt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 300 | 12 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 301 | 12 | return $self->{salt}; | |||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||
| 304 | =item $ppr->salt_hex | ||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||
| 306 | Returns the salt, as a string of hexadecimal digits. | ||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | 13 | ▶ | sub salt_hex { | ||||||||||||||||||||||||||||||||
|
Subroutine
salt_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 311 | 13 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 312 | 13 | return unpack("H*", $self->{salt}); | |||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||
| 315 | =item $ppr->hash | ||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||
| 317 | Returns the hash value, in raw form. | ||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||
| 319 | =cut | ||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | 12 | ▶ | sub hash { | ||||||||||||||||||||||||||||||||
|
Subroutine
hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 322 | 12 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 323 | 12 | return $self->{hash}; | |||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||
| 326 | =item $ppr->hash_hex | ||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||
| 328 | Returns the hash value, as a string of hexadecimal digits. | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | 13 | ▶ | sub hash_hex { | ||||||||||||||||||||||||||||||||
|
Subroutine
hash_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 333 | 13 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 334 | 13 | return unpack("H*", $self->{hash}); | |||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||
| 337 | =item $ppr->match(PASSPHRASE) | ||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||
| 339 | =item $ppr->as_rfc2307 | ||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||
| 341 | These methods are part of the standard L<Authen::Passphrase> interface. | ||||||||||||||||||||||||||||||||||
| 342 | Only passphrase recognisers using certain well-known digest algorithms | ||||||||||||||||||||||||||||||||||
| 343 | can be represented in RFC 2307 form. | ||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||
| 345 | =cut | ||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||
| 347 | 65 | ▶ | sub _hash_of { | ||||||||||||||||||||||||||||||||
|
Subroutine
_hash_of: called CC=8 SCAR=22.6
| |||||||||||||||||||||||||||||||||||
| 348 | 65 | my($self, $passphrase) = @_; | |||||||||||||||||||||||||||||||||
| 349 | 65 | my $alg = $self->{algorithm}; | |||||||||||||||||||||||||||||||||
| 350 | 65 | my $ctx; | |||||||||||||||||||||||||||||||||
| 351 | 65 | ▶ | if(is_string($alg)) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 352 | 65 | ▶ | if($alg =~ /::/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 353 | 1 | ▶ | $alg =~ /\A(?:::)?([0-9a-zA-Z_:]+) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 354 | (-([0-9][0-9_]*(?:\._*[0-9][0-9_]*)?)?)?\z/x | ||||||||||||||||||||||||||||||||||
| 355 | or croak "module spec `$alg' not understood"; | ||||||||||||||||||||||||||||||||||
| 356 | 1 | my($pkgname, $load_p, $modver) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||
| 357 | 1 | ▶ | croak "bad package name `$pkgname'" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 358 | unless is_valid_module_name($pkgname); | ||||||||||||||||||||||||||||||||||
| 359 | 1 | ▶ | if($load_p) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 360 | 1 | ▶ | if(defined $modver) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 361 | 1 | $modver =~ tr/_//d; | |||||||||||||||||||||||||||||||||
| 362 | 1 | use_module($pkgname, $modver); | |||||||||||||||||||||||||||||||||
| 363 | } else { | ||||||||||||||||||||||||||||||||||
| 364 | 0 | use_module($pkgname); | |||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||
| 367 | 1 | $ctx = $pkgname->new; | |||||||||||||||||||||||||||||||||
| 368 | } else { | ||||||||||||||||||||||||||||||||||
| 369 | 64 | $ctx = Digest->new($alg); | |||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||
| 371 | } elsif(is_blessed($alg)) { | ||||||||||||||||||||||||||||||||||
| 372 | 0 | $ctx = $alg->new; | |||||||||||||||||||||||||||||||||
| 373 | } else { | ||||||||||||||||||||||||||||||||||
| 374 | 0 | croak "algorithm specifier `$alg' is of an unrecognised type"; | |||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||
| 376 | 65 | $ctx->add($passphrase); | |||||||||||||||||||||||||||||||||
| 377 | 65 | $ctx->add($self->{salt}); | |||||||||||||||||||||||||||||||||
| 378 | 65 | return $ctx->digest; | |||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | 51 | ▶ | sub match { | ||||||||||||||||||||||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 382 | 51 | my($self, $passphrase) = @_; | |||||||||||||||||||||||||||||||||
| 383 | 51 | return $self->_hash_of($passphrase) eq $self->{hash}; | |||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | 3 | my %rfc2307_scheme_for_digest_name = ( | |||||||||||||||||||||||||||||||||
| 387 | "MD4" => "MD4", | ||||||||||||||||||||||||||||||||||
| 388 | "MD5" => "MD5", | ||||||||||||||||||||||||||||||||||
| 389 | "SHA-1" => "SHA", | ||||||||||||||||||||||||||||||||||
| 390 | "SHA1" => "SHA", | ||||||||||||||||||||||||||||||||||
| 391 | ); | ||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||
| 393 | 3 | my %rfc2307_scheme_for_package_name = ( | |||||||||||||||||||||||||||||||||
| 394 | "Crypt::RIPEMD160" => "RMD160", | ||||||||||||||||||||||||||||||||||
| 395 | "Digest::MD4" => "MD4", | ||||||||||||||||||||||||||||||||||
| 396 | "Digest::MD5" => "MD5", | ||||||||||||||||||||||||||||||||||
| 397 | "Digest::MD5::Perl" => "MD5", | ||||||||||||||||||||||||||||||||||
| 398 | "Digest::Perl::MD4" => "MD4", | ||||||||||||||||||||||||||||||||||
| 399 | "Digest::SHA" => "SHA", | ||||||||||||||||||||||||||||||||||
| 400 | "Digest::SHA::PurePerl" => "SHA", | ||||||||||||||||||||||||||||||||||
| 401 | "Digest::SHA1" => "SHA", | ||||||||||||||||||||||||||||||||||
| 402 | "MD5" => "MD5", | ||||||||||||||||||||||||||||||||||
| 403 | "RIPEMD160" => "RMD160", | ||||||||||||||||||||||||||||||||||
| 404 | ); | ||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||
| 406 | 12 | ▶ | sub as_rfc2307 { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_rfc2307: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 407 | 12 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 408 | 12 | my $alg = $self->{algorithm}; | |||||||||||||||||||||||||||||||||
| 409 | 12 | my $scheme; | |||||||||||||||||||||||||||||||||
| 410 | 12 | ▶ | if(is_string($alg)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 411 | 12 | ▶ | if($alg =~ /::/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 412 | 1 | ▶ | $scheme = $rfc2307_scheme_for_package_name{$1} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 413 | if $alg =~ /\A(?:::)? | ||||||||||||||||||||||||||||||||||
| 414 | ([0-9a-zA-Z_:]+)(?:-[0-9._]*)?\z/x; | ||||||||||||||||||||||||||||||||||
| 415 | } else { | ||||||||||||||||||||||||||||||||||
| 416 | 11 | $scheme = $rfc2307_scheme_for_digest_name{$alg}; | |||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||
| 419 | 12 | ▶ | croak "don't know RFC 2307 scheme identifier for digest algorithm $alg" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 420 | unless defined $scheme; | ||||||||||||||||||||||||||||||||||
| 421 | return "{".($self->{salt} eq "" ? "" : "S").$scheme."}". | ||||||||||||||||||||||||||||||||||
| 422 | 12 | ▶ | encode_base64($self->{hash}.$self->{salt}, ""); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||
| 425 | =back | ||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||
| 427 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||
| 429 | L<Authen::Passphrase>, | ||||||||||||||||||||||||||||||||||
| 430 | L<Crypt::SaltedHash> | ||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||
| 432 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||
| 434 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||
| 436 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||
| 438 | Copyright (C) 2006, 2007, 2009, 2010, 2012 | ||||||||||||||||||||||||||||||||||
| 439 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||
| 441 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||
| 443 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||
| 444 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||
| 446 | =cut | ||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||
| 448 | 3 | 1; | |||||||||||||||||||||||||||||||||