| 1 | package Uniform::HTTP::Auth::Digest; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Digest::MD5 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Encode qw(encode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Unicode::Normalize qw(NFC); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 11 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | my %ALGORITHM = map { $_ => 1 } qw( | |||||||||||||||||||||||||||
| 14 | md5 md5-sess | ||||||||||||||||||||||||||||
| 15 | sha-256 sha-256-sess | ||||||||||||||||||||||||||||
| 16 | sha-512-256 sha-512-256-sess | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 19 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 19 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 21 | 19 | ▶ | if (exists $args{_random_bytes}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | croak "_random_bytes must be a coderef" | ||||||||||||||||||||||||||||
| 23 | 4 | ▶ | unless ref($args{_random_bytes}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | return bless { | ||||||||||||||||||||||||||||
| 26 | nonce_state => {}, | ||||||||||||||||||||||||||||
| 27 | _random_bytes => $args{_random_bytes}, | ||||||||||||||||||||||||||||
| 28 | 19 | }, $class; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 32 | ▶ | sub validate_challenge { | ||||||||||||||||||||||||||
|
Subroutine
validate_challenge: called CC=12 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 32 | my ($class, $challenge) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | return 'Digest challenge must use authentication parameters' | ||||||||||||||||||||||||||||
| 35 | 32 | ▶ | if defined $challenge->{token68}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | return 'Digest challenge requires realm' | ||||||||||||||||||||||||||||
| 37 | 32 | ▶ | unless exists $challenge->{params}{realm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | return 'Digest challenge requires nonce' | ||||||||||||||||||||||||||||
| 39 | 32 | ▶ | unless exists $challenge->{params}{nonce}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 32 | ▶ | if (exists $challenge->{params}{charset} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $challenge->{"params"}{"charset"} and lc $challenge->{"params"}{"charset"} ne "utf-8"
MC/DC0%0/2
exists $challenge->{"params"}{"charset"} and lc $challenge->{"params"}{"charset"} ne "utf-8"
exists $challenge->{"params"}{"charset"}
lc $challenge->{"params"}{"charset"} ne "utf-8"
Truth table66%2/3
A and B
A= exists $challenge->{"params"}{"charset"}B= lc $challenge->{"params"}{"charset"} ne "utf-8"
| |||||||||||||||||||||||||||||
| 42 | && lc($challenge->{params}{charset}) ne 'utf-8') { | ||||||||||||||||||||||||||||
| 43 | 0 | return 'Digest charset must be UTF-8'; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 32 | ▶ | if (exists $challenge->{params}{userhash}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 2 | my $value = lc $challenge->{params}{userhash}; | |||||||||||||||||||||||||||
| 48 | 2 | ▶ | return 'Digest userhash must be true or false' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$value eq "true" or $value eq "false"
MC/DC0%0/2
$value eq "true" or $value eq "false"
$value eq "true"
$value eq "false"
Truth table33%1/3
A or B
A= $value eq "true"B= $value eq "false"
| |||||||||||||||||||||||||||||
| 49 | unless $value eq 'true' || $value eq 'false'; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 32 | ▶ | if (exists $challenge->{params}{stale}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 2 | my $value = lc $challenge->{params}{stale}; | |||||||||||||||||||||||||||
| 54 | 2 | ▶ | return 'Digest stale must be true or false' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$value eq "true" or $value eq "false"
MC/DC0%0/2
$value eq "true" or $value eq "false"
$value eq "true"
$value eq "false"
Truth table33%1/3
A or B
A= $value eq "true"B= $value eq "false"
| |||||||||||||||||||||||||||||
| 55 | unless $value eq 'true' || $value eq 'false'; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 32 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 7 | ▶ | sub select_challenge { | ||||||||||||||||||||||||||
|
Subroutine
select_challenge: called CC=9 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 7 | my ($self, $challenges) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | for my $challenge (@$challenges) { | |||||||||||||||||||||||||||
| 65 | 7 | ▶ | next if $challenge->{malformed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 7 | ▶ | next if defined __PACKAGE__->validate_challenge($challenge); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 7 | ▶ | my $algorithm = lc($challenge->{params}{algorithm} || 'MD5'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$challenge->{'params'}{'algorithm'} || 'MD5'
MC/DC0%0/1
$challenge->{'params'}{'algorithm'} || 'MD5'
$challenge->{'params'}{'algorithm'}
Truth table50%1/2
A
A= $challenge->{'params'}{'algorithm'}
| |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | next unless $ALGORITHM{$algorithm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | if (exists $challenge->{params}{qop}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 6 | my @qop = _qop_values($challenge->{params}{qop}); | |||||||||||||||||||||||||||
| 73 | 6 | ▶ | next unless grep { $_ eq 'auth' || $_ eq 'auth-int' } @qop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_ eq 'auth' || $_ eq 'auth-int'
MC/DC0%0/2
$_ eq 'auth' || $_ eq 'auth-int'
$_ eq 'auth'
$_ eq 'auth-int'
Truth table66%2/3
A || B
A= $_ eq 'auth'B= $_ eq 'auth-int'
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | return $challenge; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 13 | ▶ | sub authorization { | ||||||||||||||||||||||||||
|
Subroutine
authorization: called CC=46 SCAR=40.5
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 13 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 13 | for my $field (qw(challenge username password method request_target)) { | |||||||||||||||||||||||||||
| 86 | croak "Digest authorization requires '$field'" | ||||||||||||||||||||||||||||
| 87 | 65 | ▶ | unless exists($args{$field}) && defined($args{$field}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{$field} and defined $args{$field}
MC/DC0%0/2
exists $args{$field} and defined $args{$field}
exists $args{$field}
defined $args{$field}
Truth table33%1/3
A and B
A= exists $args{$field}B= defined $args{$field}
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | croak "challenge must be a hash reference" | ||||||||||||||||||||||||||||
| 90 | 13 | ▶ | unless ref($args{challenge}) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 13 | for my $field (qw(username password method request_target)) { | |||||||||||||||||||||||||||
| 92 | 52 | ▶ | croak "$field must be a plain scalar" if ref($args{$field}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 13 | ▶ | if (exists $args{origin}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | croak "origin must be a plain scalar" | ||||||||||||||||||||||||||||
| 96 | 8 | ▶ | if !defined($args{origin}) || ref($args{origin}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $args{"origin"} or ref $args{"origin"}
MC/DC0%0/2
not defined $args{"origin"} or ref $args{"origin"}
not defined $args{"origin"}
ref $args{"origin"}
Truth table33%1/3
not(A) or B
A= not defined $args{"origin"}B= ref $args{"origin"}
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 13 | ▶ | if (exists $args{entity_body}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | croak "entity_body must be a defined plain scalar" | ||||||||||||||||||||||||||||
| 100 | 3 | ▶ | if !defined($args{entity_body}) || ref($args{entity_body}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $args{"entity_body"} or ref $args{"entity_body"}
MC/DC50%1/2
not defined $args{"entity_body"} or ref $args{"entity_body"}
not defined $args{"entity_body"}
ref $args{"entity_body"}
Truth table66%2/3
not(A) or B
A= not defined $args{"entity_body"}B= ref $args{"entity_body"}
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 12 | my $challenge = $args{challenge}; | |||||||||||||||||||||||||||
| 104 | 12 | my $error = __PACKAGE__->validate_challenge($challenge); | |||||||||||||||||||||||||||
| 105 | 12 | ▶ | croak $error if defined $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 12 | my $params = $challenge->{params}; | |||||||||||||||||||||||||||
| 108 | 12 | ▶ | my $algorithm = lc($params->{algorithm} || 'MD5'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{'algorithm'} || 'MD5'
MC/DC0%0/1
$params->{'algorithm'} || 'MD5'
$params->{'algorithm'}
Truth table50%1/2
A
A= $params->{'algorithm'}
| |||||||||||||||||||||||||||||
| 109 | 12 | ▶ | return unless $ALGORITHM{$algorithm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 12 | my $qop; | |||||||||||||||||||||||||||
| 112 | 12 | ▶ | if (exists $params->{qop}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 11 | my @qops = _qop_values($params->{qop}); | |||||||||||||||||||||||||||
| 114 | 11 | ▶ | $qop = 'auth' if grep { $_ eq 'auth' } @qops; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 11 | ▶ | $qop ||= 'auth-int' if grep { $_ eq 'auth-int' } @qops; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$qop ||= "auth-int"
MC/DC100%1/1
$qop ||= "auth-int"
$qop
Truth table100%2/2
A
A= $qop
| |||||||||||||||||||||||||||||
| 116 | 11 | ▶ | return unless $qop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 11 | ▶ | return if $qop eq 'auth-int' && !exists $args{entity_body}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$qop eq "auth-int" and not exists $args{"entity_body"}
MC/DC0%0/2
$qop eq "auth-int" and not exists $args{"entity_body"}
$qop eq "auth-int"
not exists $args{"entity_body"}
Truth table66%2/3
A and not(B)
A= $qop eq "auth-int"B= not exists $args{"entity_body"}
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 12 | my $username = $args{username}; | |||||||||||||||||||||||||||
| 121 | 12 | my $password = $args{password}; | |||||||||||||||||||||||||||
| 122 | 12 | ▶ | croak "Digest username must not contain ':'" if $username =~ /:/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 12 | ▶ | croak "Digest username contains a control character" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | if $username =~ /[\x00-\x1f\x7f]/; | ||||||||||||||||||||||||||||
| 125 | 12 | ▶ | croak "Digest password contains a control character" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | if $password =~ /[\x00-\x1f\x7f]/; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 12 | my $charset = $params->{charset}; | |||||||||||||||||||||||||||
| 129 | 12 | my ($u_bytes, $p_bytes, $realm_bytes); | |||||||||||||||||||||||||||
| 130 | 12 | ▶ | if (defined $charset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | $u_bytes = encode('UTF-8', NFC($username), Encode::FB_CROAK()); | |||||||||||||||||||||||||||
| 132 | 1 | $p_bytes = encode('UTF-8', NFC($password), Encode::FB_CROAK()); | |||||||||||||||||||||||||||
| 133 | 1 | $realm_bytes = $params->{realm}; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | 11 | ▶ | croak 'non-ASCII Digest credentials require charset=UTF-8' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$username =~ /[^\x00-\x7f]/ or $password =~ /[^\x00-\x7f]/
MC/DC0%0/2
$username =~ /[^\x00-\x7f]/ or $password =~ /[^\x00-\x7f]/
$username =~ /[^\x00-\x7f]/
$password =~ /[^\x00-\x7f]/
Truth table33%1/3
A or B
A= $username =~ /[^\x00-\x7f]/B= $password =~ /[^\x00-\x7f]/
| |||||||||||||||||||||||||||||
| 137 | if $username =~ /[^\x00-\x7f]/ || $password =~ /[^\x00-\x7f]/; | ||||||||||||||||||||||||||||
| 138 | 11 | $u_bytes = $username; | |||||||||||||||||||||||||||
| 139 | 11 | $p_bytes = $password; | |||||||||||||||||||||||||||
| 140 | 11 | $realm_bytes = $params->{realm}; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 12 | my $nonce = $params->{nonce}; | |||||||||||||||||||||||||||
| 144 | 12 | ▶ | my $is_session = $algorithm =~ /-sess\z/ ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 12 | my ($nc, $cnonce); | |||||||||||||||||||||||||||
| 146 | 12 | ▶ | if (defined($qop) || $is_session) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $qop or $is_session
MC/DC0%0/2
defined $qop or $is_session
defined $qop
$is_session
Truth table66%2/3
A or B
A= defined $qopB= $is_session
| |||||||||||||||||||||||||||||
| 147 | my $state_key = _state_key( | ||||||||||||||||||||||||||||
| 148 | exists($args{origin}) ? $args{origin} : '', | ||||||||||||||||||||||||||||
| 149 | $params->{realm}, | ||||||||||||||||||||||||||||
| 150 | 12 | ▶ | $username, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | $nonce, | ||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||
| 153 | 12 | ($nc, $cnonce) = $self->_nonce_values($state_key, defined($qop)); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 12 | my $base_algorithm = $algorithm; | |||||||||||||||||||||||||||
| 157 | 12 | $base_algorithm =~ s/-sess\z//; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 12 | my $ha1_initial = _hash_hex($base_algorithm, | |||||||||||||||||||||||||||
| 160 | $u_bytes . ':' . $realm_bytes . ':' . $p_bytes); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 12 | my $ha1 = $ha1_initial; | |||||||||||||||||||||||||||
| 163 | 12 | ▶ | if ($is_session) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 1 | $ha1 = _hash_hex($base_algorithm, | |||||||||||||||||||||||||||
| 165 | $ha1_initial . ':' . $nonce . ':' . $cnonce); | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 12 | my $a2 = $args{method} . ':' . $args{request_target}; | |||||||||||||||||||||||||||
| 169 | 12 | ▶ | if (defined $qop && $qop eq 'auth-int') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $qop and $qop eq "auth-int"
MC/DC100%2/2
defined $qop and $qop eq "auth-int"
defined $qop
$qop eq "auth-int"
Truth table100%3/3
A and B
A= defined $qopB= $qop eq "auth-int"
| |||||||||||||||||||||||||||||
| 170 | 1 | my $entity_hash = _hash_hex($base_algorithm, $args{entity_body}); | |||||||||||||||||||||||||||
| 171 | 1 | $a2 .= ':' . $entity_hash; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 12 | my $ha2 = _hash_hex($base_algorithm, $a2); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 12 | my $response; | |||||||||||||||||||||||||||
| 176 | 12 | ▶ | if (defined $qop) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 11 | $response = _hash_hex($base_algorithm, | |||||||||||||||||||||||||||
| 178 | join(':', $ha1, $nonce, $nc, $cnonce, $qop, $ha2)); | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||
| 181 | 1 | $response = _hash_hex($base_algorithm, | |||||||||||||||||||||||||||
| 182 | join(':', $ha1, $nonce, $ha2)); | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 12 | my @fields; | |||||||||||||||||||||||||||
| 186 | my $userhash = exists($params->{userhash}) | ||||||||||||||||||||||||||||
| 187 | 12 | ▶ | && lc($params->{userhash}) eq 'true'; | ||||||||||||||||||||||||||
|
Condition66%2/3
exists $params->{'userhash'} && lc $params->{'userhash'} eq 'true'
MC/DC50%1/2
exists $params->{'userhash'} && lc $params->{'userhash'} eq 'true'
exists $params->{'userhash'}
lc $params->{'userhash'} eq 'true'
Truth table66%2/3
A && B
A= exists $params->{'userhash'}B= lc $params->{'userhash'} eq 'true'
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 12 | ▶ | if ($userhash) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 190 | 1 | my $hashed_username = _hash_hex($base_algorithm, | |||||||||||||||||||||||||||
| 191 | $u_bytes . ':' . $realm_bytes); | ||||||||||||||||||||||||||||
| 192 | 1 | push @fields, 'username=' . _quote($hashed_username); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | ▶ | elsif (defined($charset) && $username =~ /[^\x00-\x7f]/) { | |||||||||||||||||||||||||||
|
Condition33%1/3
defined $charset and $username =~ /[^\x00-\x7f]/
MC/DC0%0/2
defined $charset and $username =~ /[^\x00-\x7f]/
defined $charset
$username =~ /[^\x00-\x7f]/
Truth table33%1/3
A and B
A= defined $charsetB= $username =~ /[^\x00-\x7f]/
| |||||||||||||||||||||||||||||
| 195 | 0 | push @fields, "username*=UTF-8''" . _pct_encode($u_bytes); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||
| 198 | 11 | push @fields, 'username=' . _quote($username); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | push @fields, | ||||||||||||||||||||||||||||
| 202 | 'realm=' . _quote($params->{realm}), | ||||||||||||||||||||||||||||
| 203 | 12 | 'uri=' . _quote($args{request_target}), | |||||||||||||||||||||||||||
| 204 | 'algorithm=' . _algorithm_wire_name($algorithm), | ||||||||||||||||||||||||||||
| 205 | 'nonce=' . _quote($nonce); | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 12 | ▶ | if (defined $qop) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 208 | 11 | push @fields, | |||||||||||||||||||||||||||
| 209 | 'nc=' . $nc, | ||||||||||||||||||||||||||||
| 210 | 'cnonce=' . _quote($cnonce), | ||||||||||||||||||||||||||||
| 211 | 'qop=' . $qop; | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | elsif ($is_session) { | ||||||||||||||||||||||||||||
| 214 | 1 | push @fields, 'cnonce=' . _quote($cnonce); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 12 | push @fields, 'response=' . _quote($response); | |||||||||||||||||||||||||||
| 218 | push @fields, 'opaque=' . _quote($params->{opaque}) | ||||||||||||||||||||||||||||
| 219 | 12 | ▶ | if exists $params->{opaque}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | push @fields, 'userhash=' . ($userhash ? 'true' : 'false') | ||||||||||||||||||||||||||||
| 221 | 12 | ▶ | if exists $params->{userhash}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 12 | return 'Digest ' . join(', ', @fields); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 14 | ▶ | sub _state_key { | ||||||||||||||||||||||||||
|
Subroutine
_state_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 227 | 14 | return join '', map { length($_) . ':' . $_ } @_; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 12 | ▶ | sub _nonce_values { | ||||||||||||||||||||||||||
|
Subroutine
_nonce_values: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 231 | 12 | my ($self, $state_key, $increment) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 12 | my $state = $self->{nonce_state}{$state_key}; | |||||||||||||||||||||||||||
| 234 | 12 | ▶ | unless ($state) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 8 | $state = $self->{nonce_state}{$state_key} = { | |||||||||||||||||||||||||||
| 236 | count => 0, | ||||||||||||||||||||||||||||
| 237 | cnonce => $self->_new_cnonce, | ||||||||||||||||||||||||||||
| 238 | }; | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 12 | ▶ | if ($increment) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 11 | $state->{count}++; | |||||||||||||||||||||||||||
| 243 | croak "Digest nonce count overflow" | ||||||||||||||||||||||||||||
| 244 | 11 | ▶ | if $state->{count} > 0xffffffff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 12 | return (sprintf('%08x', $state->{count}), $state->{cnonce}); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 8 | ▶ | sub _new_cnonce { | ||||||||||||||||||||||||||
|
Subroutine
_new_cnonce: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 251 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 252 | 8 | my $bytes; | |||||||||||||||||||||||||||
| 253 | 8 | ▶ | if ($self->{_random_bytes}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 7 | $bytes = $self->{_random_bytes}->(24); | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | else { | ||||||||||||||||||||||||||||
| 257 | 1 | require Crypt::SysRandom; | |||||||||||||||||||||||||||
| 258 | 1 | $bytes = Crypt::SysRandom::random_bytes(24); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | 8 | ▶ | croak "secure random source did not return 24 bytes" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $bytes and length $bytes == 24
MC/DC0%0/2
defined $bytes and length $bytes == 24
defined $bytes
length $bytes == 24
Truth table33%1/3
A and B
A= defined $bytesB= length $bytes == 24
| |||||||||||||||||||||||||||||
| 261 | unless defined($bytes) && length($bytes) == 24; | ||||||||||||||||||||||||||||
| 262 | 8 | return unpack('H*', $bytes); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 17 | ▶ | sub _qop_values { | ||||||||||||||||||||||||||
|
Subroutine
_qop_values: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 266 | 17 | my ($text) = @_; | |||||||||||||||||||||||||||
| 267 | return map { | ||||||||||||||||||||||||||||
| 268 | 17 | my $q = $_; | |||||||||||||||||||||||||||
| 269 | 20 | $q =~ s/\A[ \t]+//; | |||||||||||||||||||||||||||
| 270 | 20 | $q =~ s/[ \t]+\z//; | |||||||||||||||||||||||||||
| 271 | 20 | lc $q; | |||||||||||||||||||||||||||
| 272 | } split /,/, $text; | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 39 | ▶ | sub _hash_hex { | ||||||||||||||||||||||||||
|
Subroutine
_hash_hex: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 276 | 39 | my ($algorithm, $data) = @_; | |||||||||||||||||||||||||||
| 277 | 39 | ▶ | if ($algorithm eq 'md5') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 4 | return Digest::MD5::md5_hex($data); | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 35 | ▶ | if ($algorithm eq 'sha-256') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 31 | return Digest::SHA::sha256_hex($data); | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | 4 | ▶ | if ($algorithm eq 'sha-512-256') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 4 | return Digest::SHA->new(512256)->add($data)->hexdigest; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | 0 | croak "unsupported Digest algorithm '$algorithm'"; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 12 | ▶ | sub _algorithm_wire_name { | ||||||||||||||||||||||||||
|
Subroutine
_algorithm_wire_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 290 | 12 | my ($algorithm) = @_; | |||||||||||||||||||||||||||
| 291 | 12 | my %wire = ( | |||||||||||||||||||||||||||
| 292 | 'md5' => 'MD5', | ||||||||||||||||||||||||||||
| 293 | 'md5-sess' => 'MD5-sess', | ||||||||||||||||||||||||||||
| 294 | 'sha-256' => 'SHA-256', | ||||||||||||||||||||||||||||
| 295 | 'sha-256-sess' => 'SHA-256-sess', | ||||||||||||||||||||||||||||
| 296 | 'sha-512-256' => 'SHA-512-256', | ||||||||||||||||||||||||||||
| 297 | 'sha-512-256-sess' => 'SHA-512-256-sess', | ||||||||||||||||||||||||||||
| 298 | ); | ||||||||||||||||||||||||||||
| 299 | 12 | return $wire{$algorithm}; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 75 | ▶ | sub _quote { | ||||||||||||||||||||||||||
|
Subroutine
_quote: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 303 | 75 | my ($value) = @_; | |||||||||||||||||||||||||||
| 304 | 75 | ▶ | croak "Digest quoted value contains a newline" if $value =~ /[\r\n]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 75 | $value =~ s/([\\"])/\\$1/g; | |||||||||||||||||||||||||||
| 306 | 75 | return '"' . $value . '"'; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | sub _pct_encode { | ||||||||||||||||||||||||||
|
Subroutine
_pct_encode: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 310 | 0 | my ($bytes) = @_; | |||||||||||||||||||||||||||
| 311 | 0 | my $out = ''; | |||||||||||||||||||||||||||
| 312 | 0 | for my $byte (unpack('C*', $bytes)) { | |||||||||||||||||||||||||||
| 313 | 0 | my $ch = chr($byte); | |||||||||||||||||||||||||||
| 314 | 0 | ▶ | if ($ch =~ /[A-Za-z0-9!#$&+\-.^_`|~]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | $out .= $ch; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | else { | ||||||||||||||||||||||||||||
| 318 | 0 | $out .= sprintf('%%%02X', $byte); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 0 | return $out; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 11 | 1; | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | __END__ | ||||||||||||||||||||||||||||