]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/AccessControl.pm
don't import 'RFC' from MIME::Base32
[pve-access-control.git] / PVE / AccessControl.pm
index 60232851915b88058a3443e99b43da81648981d1..ea4245c2e58e2159e69773b306e315d2a63f2f2b 100644 (file)
@@ -8,8 +8,8 @@ use Crypt::OpenSSL::RSA;
 use Net::SSLeay;
 use Net::IP;
 use MIME::Base64;
+use MIME::Base32; #libmime-base32-perl
 use Digest::SHA;
-use Digest::HMAC_SHA1;
 use URI::Escape;
 use LWP::UserAgent;
 use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print);
@@ -1173,6 +1173,23 @@ sub remove_vm_from_pool {
     lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed");
 }
 
+# hotp/totp code
+
+sub hotp($$;$) {
+       my ($binsecret, $number, $digits) = @_;
+
+       $digits = 6 if !defined($digits);
+
+       my $bincounter = pack('Q>', $number);
+       my $hmac = Digest::SHA::hmac_sha1($bincounter, $binsecret);
+
+       my $offset = unpack('C', substr($hmac,19) & pack('C', 0x0F));
+       my $part = substr($hmac, $offset, 4);
+       my $otp = unpack('N', $part);
+       my $value = ($otp & 0x7fffffff) % (10**$digits);
+       return sprintf("%0${digits}d", $value);
+}
+
 # experimental code for yubico OTP verification
 
 sub yubico_compute_param_sig {
@@ -1184,7 +1201,8 @@ sub yubico_compute_param_sig {
        $paramstr .= "$key=$param->{$key}";
     }
 
-    my $sig = uri_escape(encode_base64(Digest::HMAC_SHA1::hmac_sha1($paramstr, decode_base64($api_key || '')), ''));
+    # hmac_sha1_base64 does not add '=' padding characters, so we use encode_base64
+    my $sig = uri_escape(encode_base64(Digest::SHA::hmac_sha1($paramstr, decode_base64($api_key || '')), ''));
 
     return ($paramstr, $sig);
 }
@@ -1202,7 +1220,7 @@ sub yubico_verify_otp {
     $url = 'http://api2.yubico.com/wsapi/2.0/verify' if !defined($url);
 
     my $params = {
-       nonce =>  Digest::HMAC_SHA1::hmac_sha1_hex(time(), rand()),
+       nonce =>  Digest::SHA::hmac_sha1_hex(time(), rand()),
        id => $api_id,
        otp => uri_escape($otp),
        timestamp => 1,
@@ -1278,20 +1296,23 @@ sub oath_verify_otp {
     $digits = 6 if !$digits;
 
     my $found;
-
-    my $parser = sub {
-       my $line = shift;
-
-       if ($line =~ m/^\d{6}$/) {
-           $found = 1 if $otp eq $line;
-       }
-    };
-
     foreach my $k (PVE::Tools::split_list($keys)) {
        # Note: we generate 3 values to allow small time drift
-       my $now = localtime(time() - $step);
-       my $cmd = ['oathtool', '--totp', '--digits', $digits, '-N', $now, '-s', $step, '-w', '2', '-b', $k];
-       eval { run_command($cmd, outfunc => $parser, errfunc => sub {}); };
+       my $binkey;
+       if ($k =~ /^[A-Z2-7=]{16}$/) {
+           $binkey = MIME::Base32::decode_rfc3548($k);
+       } elsif ($k =~ /^[A-Fa-f0-9]{40}$/) {
+           $binkey = pack('H*', $k);
+       } else {
+           die "unrecognized key format, must be hex or base32 encoded\n";
+       }
+
+       # force integer division for time/step
+       use integer;
+       my $now = time()/$step - 1;
+       $found = 1 if $otp eq hotp($binkey, $now+0, $digits);
+       $found = 1 if $otp eq hotp($binkey, $now+1, $digits);
+       $found = 1 if $otp eq hotp($binkey, $now+2, $digits);
        last if $found;
     }