Passphrase/NetscapeMail.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 65.0% 26 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 81.8% 126 / 154 CC 24 SCAR 28.0
CC
24
Cov
82%
new
29.2
from_rfc2307
3.2
as_rfc2307
2.0
CRAP 16.4
« Passphrase.pm ↑ Summary Passphrase/MySQL323.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::NetscapeMail - passphrases using Netscape Mail
4 Server's method
5  
6 =head1 SYNOPSIS
7  
8 use Authen::Passphrase::NetscapeMail;
9  
10 $ppr = Authen::Passphrase::NetscapeMail->new(
11 salt => "8fd9d0a03491ce8f99cfbc9ab39f0dd5",
12 hash_hex => "983757d7b519e86d9b5d472aca4eea3a");
13  
14 $ppr = Authen::Passphrase::NetscapeMail->new(
15 salt_random => 1,
16 passphrase => "passphrase");
17  
18 $ppr = Authen::Passphrase::NetscapeMail->from_rfc2307(
19 "{NS-MTA-MD5}8fd9d0a03491ce8f99cfbc9ab39f0dd5".
20 "983757d7b519e86d9b5d472aca4eea3a");
21  
22 $salt = $ppr->salt;
23 $hash = $ppr->hash;
24 $hash_hex = $ppr->hash_hex;
25  
26 if($ppr->match($passphrase)) { ...
27  
28 $userPassword = $ppr->as_rfc2307;
29  
30 =head1 DESCRIPTION
31  
32 An object of this class encapsulates a passphrase hashed using
33 the algorithm used by Netscape Mail Server. This is a subclass of
34 L<Authen::Passphrase>, and this document assumes that the reader is
35 familiar with the documentation for that class.
36  
37 The Netscape Mail Server scheme is based on the MD5 digest algorithm.
38 The passphrase and a salt are concatenated, along with some fixed
39 bytes, and this record is hashed through MD5. The output of MD5 is the
40 password hash.
41  
42 This algorithm is deprecated, and is supported for compatibility only.
43 Prefer the mechanism of L<Authen::Passphrase::SaltedDigest>.
44  
45 =cut
46  
47 package Authen::Passphrase::NetscapeMail;
48  
49 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use Crypt::SysRandom 'random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use Digest::MD5 1.99_53 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 our $VERSION = "0.009";
59  
60 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 =head1 CONSTRUCTORS
63  
64 =over
65  
66 =item Authen::Passphrase::NetscapeMail->new(ATTR => VALUE, ...)
67  
68 Generates a new passphrase recogniser object using the Netscape Mail
69 Server algorithm. The following attributes may be given:
70  
71 =over
72  
73 =item B<salt>
74  
75 The salt, as a raw 32-byte string. It may be any 32-byte string, but
76 it is conventionally limited to lowercase hexadecimal digits.
77  
78 =item B<salt_random>
79  
80 Causes salt to be generated randomly. The value given for this attribute
81 is ignored. The salt will be a string of 32 lowercase hexadecimal digits.
82  
83 =item B<hash>
84  
85 The hash, as a string of 16 bytes.
86  
87 =item B<hash_hex>
88  
89 The hash, as a string of 32 hexadecimal digits.
90  
91 =item B<passphrase>
92  
93 A passphrase that will be accepted.
94  
95 =back
96  
97 The salt must be given, and either the hash or the passphrase.
98  
99 =cut
100  
101 8 ▶sub new {
Subroutine new: called CC=21 SCAR=33.7
Pod documented
102 8 my $class = shift;
103 8 my $self = bless({}, $class);
104 8 my $passphrase;
105 8 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 16 8 24
106 16 my $attr = shift;
107 16 my $value = shift;
108 16 ▶ if($attr eq "salt") {
Branch: 5 branches
BranchTrueFalseTotal
if ($attr eq 'salt') { } 7 9 16
elsif ($attr eq 'salt_random') { } 1 8 9
elsif ($attr eq 'hash') { } 3 5 8
elsif ($attr eq 'hash_hex') { } 3 2 5
elsif ($attr eq 'passphrase') { } 2 0 2
109 croak "salt specified redundantly"
110 7 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 7 7
111 7 ▶ $value =~ m#\A[\x00-\xff]{32}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{32}\z/ or croak("not a valid salt") 7 0 7
112 or croak "not a valid salt";
113 7 $self->{salt} = "$value";
114 } elsif($attr eq "salt_random") {
115 croak "salt specified redundantly"
116 1 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 1 1
117 1 $self->{salt} = unpack("H*", random_bytes(16));
118 } elsif($attr eq "hash") {
119 croak "hash specified redundantly"
120 3 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 3 3
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
003
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
ABresult
1X1
011
000
121 defined($passphrase);
122 3 ▶ $value =~ m#\A[\x00-\xff]{16}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{16}\z/ or croak("not a valid MD5 hash") 3 0 3
123 or croak "not a valid MD5 hash";
124 3 $self->{hash} = "$value";
125 } elsif($attr eq "hash_hex") {
126 croak "hash specified redundantly"
127 3 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 3 3
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
003
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
ABresult
1X1
011
000
128 defined($passphrase);
129 3 ▶ $value =~ m#\A[0-9A-Fa-f]{32}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[0-9A-Fa-f]{32}\z/ or croak(qq["$value" is not a valid ] . 'hex MD5 hash') 3 0 3
130 or croak "\"$value\" is not a valid ".
131 "hex MD5 hash";
132 3 $self->{hash} = pack("H*", $value);
133 } elsif($attr eq "passphrase") {
134 croak "passphrase specified redundantly"
135 2 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 2 2
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
002
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
ABresult
1X1
011
000
136 defined($passphrase);
137 2 $passphrase = $value;
138 } else {
139 0 croak "unrecognised attribute `$attr'";
140 }
141 }
142 8 ▶ croak "salt not specified" unless exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"salt"} 0 8 8
143 8 ▶ $self->{hash} = $self->_hash_of($passphrase) if defined $passphrase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 2 6 8
144 8 ▶ croak "hash not specified" unless exists $self->{hash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"} 0 8 8
145 8 return $self;
146 }
147  
148 =item Authen::Passphrase::NetscapeMail->from_rfc2307(USERPASSWORD)
149  
150 Generates a new Netscape Mail Server passphrase recogniser object from
151 an RFC 2307 string. The string must consist of "B<{NS-MTA-MD5}>" (case
152 insensitive) followed by the hash in case-insensitive hexadecimal and
153 then the salt. The salt must be exactly 32 characters long, and cannot
154 contain any character that cannot appear in an RFC 2307 string.
155  
156 =cut
157  
158 1 ▶sub from_rfc2307 {
Subroutine from_rfc2307: called CC=3 SCAR=11.8
Pod documented
159 1 my($class, $userpassword) = @_;
160 1 ▶ if($userpassword =~ /\A\{(?i:ns-mta-md5)\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userpassword =~ /\A\{(?i:ns-mta-md5)\}/) 1 0 1
161 1 ▶ $userpassword =~ /\A\{.*?\}([0-9a-fA-F]{32})([!-~]{32})\z/
Branch: 1 branch
BranchTrueFalseTotal
$userpassword =~ /\A\{.*?\}([0-9a-fA-F]{32})([!-~]{32})\z/ or croak("malformed {NS-MTA-MD5} data") 1 0 1
162 or croak "malformed {NS-MTA-MD5} data";
163 1 my($hash, $salt) = ($1, $2);
164 1 return $class->new(salt => $salt, hash_hex => $hash);
165 }
166 0 return $class->SUPER::from_rfc2307($userpassword);
167 }
168  
169 =back
170  
171 =head1 METHODS
172  
173 =over
174  
175 =item $ppr->salt
176  
177 Returns the salt value, as a string of 32 bytes.
178  
179 =cut
180  
181 8 ▶sub salt {
Subroutine salt: called CC=1 SCAR=0.0
Pod documented
182 8 my($self) = @_;
183 8 return $self->{salt};
184 }
185  
186 =item $ppr->hash
187  
188 Returns the hash value, as a string of 16 bytes.
189  
190 =cut
191  
192 7 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
193 7 my($self) = @_;
194 7 return $self->{hash};
195 }
196  
197 =item $ppr->hash_hex
198  
199 Returns the hash value, as a string of 32 hexadecimal digits.
200  
201 =cut
202  
203 12 ▶sub hash_hex {
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
204 12 my($self) = @_;
205 12 return unpack("H*", $self->{hash});
206 }
207  
208 =item $ppr->match(PASSPHRASE)
209  
210 =item $ppr->as_rfc2307
211  
212 These methods are part of the standard L<Authen::Passphrase> interface.
213  
214 =cut
215  
216 28 ▶sub _hash_of {
Subroutine _hash_of: called CC=1 SCAR=0.0
217 28 my($self, $passphrase) = @_;
218 28 my $ctx = Digest::MD5->new;
219 28 $ctx->add($self->{salt});
220 28 $ctx->add("\x59");
221 28 $ctx->add($passphrase);
222 28 $ctx->add("\xf7");
223 28 $ctx->add($self->{salt});
224 28 return $ctx->digest;
225 }
226  
227 26 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
228 26 my($self, $passphrase) = @_;
229 26 return $self->_hash_of($passphrase) eq $self->{hash};
230 }
231  
232 5 ▶sub as_rfc2307 {
Subroutine as_rfc2307: called CC=2 SCAR=7.1
Pod documented
233 5 my($self) = @_;
234 croak "can't put this salt into an RFC 2307 string"
235 5 ▶ if $self->{salt} =~ /[^!-~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"salt"} =~ /[^!-~]/ 0 5 5
236 5 return "{NS-MTA-MD5}".$self->hash_hex.$self->{salt};
237 }
238  
239 =back
240  
241 =head1 SEE ALSO
242  
243 L<Authen::Passphrase>,
244 L<Digest::MD5>
245  
246 =head1 AUTHOR
247  
248 Andrew Main (Zefram) <zefram@fysh.org>
249  
250 =head1 COPYRIGHT
251  
252 Copyright (C) 2006, 2007, 2009, 2010, 2012
253 Andrew Main (Zefram) <zefram@fysh.org>
254  
255 =head1 LICENSE
256  
257 This module is free software; you can redistribute it and/or modify it
258 under the same terms as Perl itself.
259  
260 =cut
261  
262 2 1;
« Passphrase.pm ↑ Summary Passphrase/MySQL323.pm »