SCRAM/Role/Common.pm

filter: Statementstmt 99.3% 148 / 149 Branchbran 83.3% 15 / 18 Conditioncond 86.6% 13 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 97.4% 38 / 39 Podpod n/a totaltotal 95.6% 221 / 231 CC 18 SCAR 8.1
CC
18
Cov
97%
_parse_to_session
6.0
__ANON__
4.0
_join_reply
4.0
CRAP 2.2
« SCRAM/Server.pm ↑ Summary SCRAM/Client.pm »
1 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Authen::SCRAM::Role::Common;
6  
7 4 our $VERSION = '0.011';
8  
9 4 ▶use Moo::Role 1.001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Authen::SASL::SASLprep 1.100 qw/saslprep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Crypt::URandom qw/urandom/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Encode qw/encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use MIME::Base64 qw/encode_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use PBKDF2::Tiny 0.003 qw/digest_fcn hmac/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Types::Standard qw/Bool Enum Num HashRef CodeRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #--------------------------------------------------------------------------#
23 # public attributes
24 #--------------------------------------------------------------------------#
25  
26 4 has digest => (
27 is => 'ro',
28 isa => Enum [qw/SHA-1 SHA-224 SHA-256 SHA-384 SHA-512/],
29 default => 'SHA-1',
30 );
31  
32 4 has nonce_size => (
33 is => 'ro',
34 isa => Num,
35 default => 192,
36 );
37  
38 4 has skip_saslprep => (
39 is => 'ro',
40 isa => Bool,
41 );
42  
43 #--------------------------------------------------------------------------#
44 # private attributes
45 #--------------------------------------------------------------------------#
46  
47 4 has _const_eq_fcn => (
48 is => 'lazy',
49 isa => CodeRef,
50 );
51  
52 # constant time comparison to avoid timing attacks; uses
53 # String::Compare::ConstantTime if available or a pure-Perl fallback
54 7 ▶sub _build__const_eq_fcn {
Subroutine _build__const_eq_fcn: called CC=1 SCAR=0.0
55 7 my ($self) = @_;
56 # XXX disable String::Compare::ConstantTime until a new version
57 # is released that fixes warnings on older perls.
58 7 if ( 0 && eval { require String::Compare::ConstantTime; 1 } ) {
59 return \&String::Compare::ConstantTime::equals;
60 }
61 else {
62 7 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
63 7 my ( $dk1, $dk2 ) = @_;
64 7 my $dk1_length = length($dk1);
65 7 ▶ return unless $dk1_length == length($dk2);
Branch: 1 branch
BranchTrueFalseTotal
unless $dk1_length == length $dk2 0 7 7
66 7 my $match = 1;
67 7 for my $offset ( 0 .. $dk1_length ) {
68 147 ▶ $match &= ( substr( $dk1, $offset, 1 ) eq substr( $dk2, $offset, 1 ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($dk1, $offset, 1) eq substr($dk2, $offset, 1) ? : 127 20 147
69 }
70 7 return $match;
71 7 };
72 }
73 }
74  
75 4 has _digest_fcn => (
76 is => 'lazy',
77 isa => CodeRef,
78 );
79  
80 16 ▶sub _build__digest_fcn {
Subroutine _build__digest_fcn: called CC=1 SCAR=0.0
81 16 my ($self) = @_;
82 16 my ($fcn) = digest_fcn( $self->digest );
83 16 return $fcn;
84 }
85  
86 # _hmac_fcn( $key, $data ) -- this matches RFC 5802 parameter order but
87 # is reversed from Digest::HMAC/PBKDF2::Tiny which uses (data, key)
88 4 has _hmac_fcn => (
89 is => 'lazy',
90 isa => CodeRef,
91 );
92  
93 16 ▶sub _build__hmac_fcn {
Subroutine _build__hmac_fcn: called CC=1 SCAR=0.0
94 16 my ($self) = @_;
95 16 my ( $fcn, $block_size, $digest_length ) = digest_fcn( $self->digest );
96 47 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
97 47 my ( $key, $data ) = @_;
98 47 ▶ $key = $fcn->($key) if length($key) > $block_size;
Branch: 1 branch
BranchTrueFalseTotal
if length $key > $block_size 0 47 47
99 47 return hmac( $data, $key, $fcn, $block_size );
100 16 };
101 }
102  
103 # helpful for testing
104 4 has _nonce_generator => (
105 is => 'lazy',
106 isa => CodeRef,
107 );
108  
109 17 ▶sub _build__nonce_generator {
Subroutine _build__nonce_generator: called CC=1 SCAR=0.0
110 17 my ($self) = @_;
111 # extract from $self to avoid circular reference
112 17 my $nonce_size = $self->nonce_size;
113 17 ▶ return sub { return encode_base64( urandom( $nonce_size / 8 ), "" ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
114 }
115  
116 # _session builds up parameters used during a SCRAM session. Keys
117 # starting with "_" are private state not used for exchange. Single
118 # letter keys are defined as per RFC5802
119 #
120 # _nonce private nonce part
121 # _c1b client-first-message-bare
122 # _s1 server-first-message
123 # _c2wop client-final-message-without-proof
124 # _stored_key H(ClientKey)
125 # _server_key HMAC(SaltedPassword, "Server Key")
126 # _auth AuthMessage
127  
128 4 has _session => (
129 is => 'lazy',
130 isa => HashRef,
131 clearer => 1,
132 );
133  
134 37 ▶sub _build__session {
Subroutine _build__session: called CC=1 SCAR=0.0
135 37 my ($self) = @_;
136 37 return { _nonce => $self->_nonce_generator->() };
137 }
138  
139 #--------------------------------------------------------------------------#
140 # methods
141 #--------------------------------------------------------------------------#
142  
143 29 ▶sub _auth_msg {
Subroutine _auth_msg: called CC=2 SCAR=7.0
144 29 my ($self) = @_;
145 return $self->_session->{_auth} ||=
146 29 ▶ encode_utf8( join( ",", map { $self->_session->{$_} } qw/_c1b _s1 _c2wop/ ) );
Condition66%2/3
$self->_session->{"_auth"} ||= &encode_utf8(join(",", map({$self->_session->{$_};} "_c1b", "_s1", "_c2wop")))
l!l&&r!l&&!r
12170
MC/DC0%0/2
$self->_session->{"_auth"} ||= &encode_utf8(join(",", map({$self->_session->{$_};} "_c1b", "_s1", "_c2wop")))
$self->_session->{"_auth"} &encode_utf8(join(",", map({$self->_session->{$_};} "_c1b", "_s1", "_c2wop")))
Truth table66%2/3
A ||= B
A= $self->_session->{"_auth"}B= &encode_utf8(join(",", map({$self->_session->{$_};} "_c1b", "_s1", "_c2wop")))
ABresult
1X1
011
000
147 }
148  
149 48 ▶sub _base64 {
Subroutine _base64: called CC=1 SCAR=0.0
150 48 my ( $self, $data ) = @_;
151 48 return encode_base64( $data, "" );
152 }
153  
154 17 ▶sub _client_sig {
Subroutine _client_sig: called CC=1 SCAR=0.0
155 17 my ($self) = @_;
156 17 return $self->_hmac_fcn->( $self->_session->{_stored_key}, $self->_auth_msg );
157 }
158  
159 24 ▶sub _construct_gs2 {
Subroutine _construct_gs2: called CC=3 SCAR=11.0
160 24 my ( $self, $authz ) = @_;
161 24 ▶ my $maybe =
Branch: 1 branch
BranchTrueFalseTotal
defined $authz && length $authz ? : 7 17 24
Condition100%3/3
defined $authz and length $authz
!ll&&!rl&&r
4137
MC/DC100%2/2
defined $authz and length $authz
defined $authz length $authz
Truth table100%3/3
A and B
A= defined $authzB= length $authz
ABresult
0X0
100
111
162 ( defined($authz) && length($authz) )
163 ? ( "a=" . $self->_encode_name($authz) )
164 : "";
165 24 return "n,$maybe,";
166 }
167  
168 11 ▶sub _decode_name {
Subroutine _decode_name: called CC=1 SCAR=0.0
169 11 my ( $self, $name ) = @_;
170 11 $name =~ s/=2c/,/g;
171 11 $name =~ s/=3d/=/g;
172 11 return $name;
173 }
174  
175 36 ▶sub _encode_name {
Subroutine _encode_name: called CC=1 SCAR=0.0
176 36 my ( $self, $name ) = @_;
177 36 $name =~ s/=/=3d/g;
178 36 $name =~ s/,/=2c/g;
179 36 return $name;
180 }
181  
182 8 ▶sub _extend_nonce {
Subroutine _extend_nonce: called CC=1 SCAR=0.0
183 8 my ($self) = @_;
184 8 $self->_session->{r} .= $self->_session->{_nonce};
185 }
186  
187 189 ▶sub _get_session {
Subroutine _get_session: called CC=1 SCAR=0.0
188 189 my ( $self, $key ) = @_;
189 189 return $self->_session->{$key};
190 }
191  
192 63 ▶sub _join_reply {
Subroutine _join_reply: called CC=4 SCAR=13.9
193 63 my ( $self, @fields ) = @_;
194 63 my @reply;
195 63 for my $k (@fields) {
196 138 my $v = $self->_session->{$k};
197 138 ▶ if ( $k eq 'a' || $k eq 'n' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "a" or $k eq "n") 29 109 138
Condition66%2/3
$k eq "a" or $k eq "n"
l!l&&r!l&&!r
029109
MC/DC50%1/2
$k eq "a" or $k eq "n"
$k eq "a" $k eq "n"
Truth table66%2/3
A or B
A= $k eq "a"B= $k eq "n"
ABresult
1X1
011
000
198 29 $v = $self->_encode_name($v);
199 }
200 138 push @reply, "$k=$v";
201 }
202 63 my $msg = '' . join( ",", @reply );
203 63 utf8::upgrade($msg);
204 63 return $msg;
205 }
206  
207 39 ▶sub _parse_to_session {
Subroutine _parse_to_session: called CC=6 SCAR=17.9
208 39 my ( $self, @params ) = @_;
209 39 for my $part (@params) {
210 98 my ( $k, $v ) = split /=/, $part, 2;
211 98 ▶ if ( $k eq 'a' || $k eq 'n' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'a' or $k eq 'n') { } 11 87 98
elsif ($k eq 'i' and not $v =~ /^[0-9]+$/) { } 3 154 157
Condition100%3/3
$k eq "a" or $k eq "n"
l!l&&r!l&&!r
3887
MC/DC100%2/2
$k eq "a" or $k eq "n"
$k eq "a" $k eq "n"
Truth table100%3/3
A or B
A= $k eq "a"B= $k eq "n"
ABresult
1X1
011
000
212 11 $v = $self->_saslprep( $self->_decode_name($v) );
213 }
214 ▶ elsif ( $k eq 'i' && $v !~ /^[0-9]+$/ ) {
Condition100%3/3
$k eq "i" and not $v =~ /^[0-9]+$/
!ll&&!rl&&r
70143
MC/DC100%2/2
$k eq "i" and not $v =~ /^[0-9]+$/
$k eq "i" not $v =~ /^[0-9]+$/
Truth table100%3/3
A and not(B)
A= $k eq "i"B= not $v =~ /^[0-9]+$/
ABresult
0X0
100
111
215 3 croak "SCRAM iteration parameter '$part' invalid";
216 }
217 95 $self->_session->{$k} = $v;
218 }
219 36 return;
220 }
221  
222 54 ▶sub _saslprep {
Subroutine _saslprep: called CC=2 SCAR=7.2
223 54 my ( $self, $name ) = @_;
224  
225 54 ▶ return $name if $self->skip_saslprep;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_saslprep 0 54 54
226  
227 54 ▶ my $prepped = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 54 saslprep( $name, 1 ); # '1' makes it use stored mode
229 }
230 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 croak "SCRAM username '$name' invalid: $_";
232 54 };
233 54 return $prepped;
234 }
235  
236 133 ▶sub _set_session {
Subroutine _set_session: called CC=2 SCAR=6.9
237 133 my ( $self, %args ) = @_;
238 133 ▶ while ( my ( $k, $v ) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 212 133 345
239 212 $self->_session->{$k} = $v;
240 }
241 133 return;
242 }
243  
244 #--------------------------------------------------------------------------#
245 # regular expressions for parsing
246 #--------------------------------------------------------------------------#
247  
248 # tokens
249 4 my $VALUE = qr/[^,]+/;
250 4 my $CBNAME = qr/[a-zA-Z0-9.-]+/;
251 4 my $ATTR_VAL = qr/[a-zA-Z]=$VALUE/;
252  
253 # atoms
254 4 my $GS2_CBIND_FLAG = qr/(?:n|y|p=$VALUE)/;
255 4 my $AUTHZID = qr/a=$VALUE/;
256 4 my $CHN_BIND = qr/c=$VALUE/;
257 4 my $S_ERROR = qr/e=$VALUE/;
258 4 my $ITER_CNT = qr/i=$VALUE/;
259 4 my $MEXT = qr/m=$VALUE/;
260 4 my $USERNAME = qr/n=$VALUE/;
261 4 my $PROOF = qr/p=$VALUE/;
262 4 my $NONCE = qr/r=$VALUE/;
263 4 my $SALT = qr/s=$VALUE/;
264 4 my $VERIFIER = qr/v=$VALUE/;
265 4 my $EXT = qr/$ATTR_VAL (?: , $ATTR_VAL)*/;
266  
267 # constructions
268 4 my $C_FRST_BARE = qr/(?:($MEXT),)? ($USERNAME) , ($NONCE) (?:,$EXT)?/x;
269 4 my $GS2_HEADER = qr/($GS2_CBIND_FLAG) , ($AUTHZID)? , /x;
270 4 my $C_FINL_WO_PRF = qr/($CHN_BIND) , ($NONCE) (?:,$EXT)?/x;
271  
272 # messages
273 4 my $C_FRST_MSG = qr/$GS2_HEADER ($C_FRST_BARE)/x;
274 4 my $S_FRST_MSG = qr/(?:($MEXT),)? ($NONCE) , ($SALT) , ($ITER_CNT) (?:,$EXT)?/x;
275 4 my $C_FINL_MSG = qr/($C_FINL_WO_PRF) , ($PROOF)/x;
276 4 my $S_FINL_MSG = qr/($S_ERROR | $VERIFIER)/x;
277  
278 17 ▶sub _client_first_re { $C_FRST_MSG } # ($cbind, $authz?, $c_1_bare, $mext?, @params)
Subroutine _client_first_re: called CC=1 SCAR=0.0
279 23 ▶sub _server_first_re { $S_FRST_MSG } # ($mext?, @params)
Subroutine _server_first_re: called CC=1 SCAR=0.0
280 7 ▶sub _client_final_re { $C_FINL_MSG } # ($c_2_wo_proof, @params)
Subroutine _client_final_re: called CC=1 SCAR=0.0
281 7 ▶sub _server_final_re { $S_FINL_MSG } # ($error_or_verification)
Subroutine _server_final_re: called CC=1 SCAR=0.0
282  
283 4 1;
284  
285 =pod
286  
287 =for Pod::Coverage digest nonce_size skip_saslprep
288  
289 =cut
290  
291 # vim: ts=4 sts=4 sw=4 et:
« SCRAM/Server.pm ↑ Summary SCRAM/Client.pm »