blib/lib/Lemonldap/NG/Common/TOTP.pm

filter: Statementstmt 89.1% 74 / 83 Branchbran 75.0% 15 / 20 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 7 totaltotal 73.3% 110 / 150 CC 18 SCAR 17.1
CC
18
Cov
81%
verify_totp
12.9
__ANON__
6.0
_code
3.1
CRAP 5.5
« blib/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Wrapper.pm »
1 package Lemonldap::NG::Common::TOTP;
2  
3 # This module is inspired by Auth::GoogleAuth written by Gryphon Shafer
4 # <gryphon@cpan.org>
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Convert::Base32 qw(decode_base32 encode_base32);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Lemonldap::NG::Common::Crypto;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $hash_func = {
14 "sha1" => \&Digest::SHA::hmac_sha1_hex,
15 "sha256" => \&Digest::SHA::hmac_sha256_hex,
16 "sha512" => \&Digest::SHA::hmac_sha512_hex,
17 };
18  
19 1 our $VERSION = '2.23.3';
20  
21 has key => (
22 is => 'ro',
23 lazy => 1,
24 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
25 0 my ($self) = @_;
26 0 ▶ return $self->conf->{totp2fKey} || $self->conf->{key};
Condition0%0/3
$self->conf->{'totp2fKey'} || $self->conf->{'key'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->conf->{'totp2fKey'} || $self->conf->{'key'}
$self->conf->{'totp2fKey'} $self->conf->{'key'}
Truth table0%0/3
A || B
A= $self->conf->{'totp2fKey'}B= $self->conf->{'key'}
ABresult
1X1
011
000
27 }
28 1 );
29  
30 has encryptSecret => (
31 is => 'ro',
32 lazy => 1,
33 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 my ($self) = @_;
35 0 return $self->conf->{totp2fEncryptSecret};
36 }
37 1 );
38  
39 has crypto => (
40 is => 'ro',
41 lazy => 1,
42 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 4 my ($self) = @_;
44 4 Lemonldap::NG::Common::Crypto->new( $self->key );
45 }
46 1 );
47  
48 1 ▶use constant PREFIX => "{llngcrypt}";
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 12 ▶sub is_encrypted {
Subroutine is_encrypted: called CC=1 SCAR=0.0
Pod undocumented
51 12 my ( $self, $secret ) = @_;
52 12 return ( substr( $secret, 0, length(PREFIX) ) eq PREFIX );
53 }
54  
55 4 ▶sub get_ciphertext {
Subroutine get_ciphertext: called CC=1 SCAR=0.0
Pod undocumented
56 4 my ( $self, $secret ) = @_;
57 4 return substr( $secret, length(PREFIX) );
58 }
59  
60 # This returns the TOTP secret from its stored form
61 12 ▶sub get_cleartext_secret {
Subroutine get_cleartext_secret: called CC=2 SCAR=6.9
Pod undocumented
62 12 my ( $self, $secret ) = @_;
63 12 my $cleartext_secret = $secret;
64 12 ▶ if ( $self->is_encrypted($secret) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_encrypted($secret)) 4 8 12
65 4 $cleartext_secret =
66 $self->crypto->decrypt( $self->get_ciphertext($secret) );
67 }
68 12 return $cleartext_secret;
69 }
70  
71 # This returns the cleartext or encrypted code for storage
72 2 ▶sub get_storable_secret {
Subroutine get_storable_secret: called CC=2 SCAR=6.9
Pod undocumented
73 2 my ( $self, $secret ) = @_;
74 2 my $storable_secret = $secret;
75 2 ▶ if ( $self->encryptSecret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->encryptSecret) 1 1 2
76 1 $storable_secret = PREFIX . $self->crypto->encrypt($secret);
77 }
78 2 return $storable_secret;
79 }
80  
81 # DEPRECATED: legacy API
82 12 ▶sub verifyCode {
Subroutine verifyCode: called CC=3 SCAR=11.0
Pod undocumented
83 12 my ( $self, $interval, $range, $digits, $stored_secret, $code ) = @_;
84 12 my $result = $self->verify_totp(
85 interval => $interval,
86 range_tolerance => $range,
87 digits => $digits,
88 stored_secret => $stored_secret,
89 code => $code,
90 );
91 12 ▶ if ( $result->{result} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'result'} == 1) { } 7 5 12
92 7 ▶ return ( wantarray() ? ( 1, $result->{offset} ) : 1 );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
93 }
94 else {
95 5 $self->logger->error( $result->{error} );
96 5 return $result->{result};
97 }
98 }
99  
100 # Verify that TOTP $code matches with $secret
101 12 ▶sub verify_totp {
Subroutine verify_totp: called CC=11 SCAR=25.6
Pod undocumented
102 12 my ( $self, %args ) = @_;
103  
104 12 ▶ my $interval = $args{interval} || 30;
Condition50%1/2
$args{'interval'} || 30
l!l
120
MC/DC0%0/1
$args{'interval'} || 30
$args{'interval'}
Truth table50%1/2
A
A= $args{'interval'}
Aresult
11
00
105 12 ▶ my $digits = $args{digits} || 6;
Condition50%1/2
$args{'digits'} || 6
l!l
120
MC/DC0%0/1
$args{'digits'} || 6
$args{'digits'}
Truth table50%1/2
A
A= $args{'digits'}
Aresult
11
00
106 12 ▶ my $range = $args{range_tolerance} // 0;
Condition50%1/2
$args{"range_tolerance"} // 0
l!l
120
MC/DC0%0/1
$args{"range_tolerance"} // 0
$args{"range_tolerance"}
Truth table50%1/2
A
A= $args{"range_tolerance"}
Aresult
11
00
107 12 my $stored_secret = $args{stored_secret};
108 12 my $code = $args{code};
109 12 ▶ my $hash = $args{hash} || "sha1";
Condition50%1/2
$args{'hash'} || 'sha1'
l!l
012
MC/DC0%0/1
$args{'hash'} || 'sha1'
$args{'hash'}
Truth table50%1/2
A
A= $args{'hash'}
Aresult
11
00
110  
111 12 ▶ if ( !$stored_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stored_secret) 0 12 12
112 return {
113 0 result => -1,
114 error => "No secret provided",
115 };
116 }
117  
118 12 ▶ if ( !$code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 12 12
119 return {
120 0 result => -1,
121 error => "No code provided",
122 };
123 }
124  
125 12 my $secret = $self->get_cleartext_secret($stored_secret);
126 12 ▶ if ( !$secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 1 11 12
127 return {
128 1 result => -1,
129 error => "Unable to decrypt TOTP secret",
130 };
131 }
132  
133 11 my $s = eval { decode_base32($secret) };
134 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
135 return {
136 0 result => -1,
137 error => "Bad characters in TOTP secret",
138 };
139 }
140  
141 # A plain string comparison is deliberate here. This has been reported as a
142 # CWE-208 timing oracle; it is not one. A TOTP code is valid for a single
143 # $interval, so an attacker has no stable secret to average timing
144 # measurements against -- the precondition every comparison-timing attack
145 # needs. Independently: Perl's eq defers to memcmp(), which compares short
146 # strings by word rather than byte (measured: no difference across the six
147 # digit positions), and any residual signal sits far below the HMAC, secret
148 # decryption and session-backend costs of the surrounding request. Failed
149 # attempts are counted by BruteForceProtection.
150 11 for ( -$range .. $range ) {
151 24 ▶ if ( $code eq $self->_code( $s, $_, $interval, $digits, $hash ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $self->_code($s, $_, $interval, $digits, $hash)) 7 17 24
152 return {
153 7 result => 1,
154 offset => $_,
155 };
156 }
157 }
158 return {
159 4 result => 0,
160 error => "Code did not match",
161 };
162 }
163  
164 # Internal subroutine that calculates TOTP code using $secret and $interval
165 42 ▶sub _code {
Subroutine _code: called CC=3 SCAR=11.2
166 42 my ( $self, $secret, $offset, $interval, $digits, $hash, $time ) = @_;
167  
168 42 ▶ $time ||= time();
Condition66%2/3
$time ||= &CORE::GLOBAL::time()
l!l&&r!l&&!r
18240
MC/DC0%0/2
$time ||= &CORE::GLOBAL::time()
$time &CORE::GLOBAL::time()
Truth table66%2/3
A ||= B
A= $timeB= &CORE::GLOBAL::time()
ABresult
1X1
011
000
169  
170 42 my $hash_func = $hash_func->{$hash};
171 42 ▶ die "Unknown hash $hash" unless $hash_func;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash_func 0 42 42
172  
173 42 my $hmac = $hash_func->(
174 pack( 'H*', sprintf( '%016x', int( $time / $interval ) - $offset ) ),
175 $secret,
176 );
177  
178 42 return sprintf(
179 '%0' . $digits . 'd',
180 (
181 hex( substr( $hmac, hex( substr( $hmac, -1 ) ) * 2, 8 ) ) &
182 0x7fffffff
183 ) % 10**$digits
184 );
185 }
186  
187 # Simply generate new base32 secret
188 0 ▶sub newSecret {
Subroutine newSecret: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my ($self) = @_;
190 0 return encode_base32( Crypt::URandom::urandom(20) );
191 }
192  
193 1 1;
« blib/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Wrapper.pm »