[pve-devel] [PATCH v2 common] use hmac_sha256 instead of sha1 for csrf token

Oguz Bektas o.bektas at proxmox.com
Tue Jun 18 14:41:52 CEST 2019


now generates & verifies with hmac_sha1. also left the old digest format
for backwards compatibility during verification, to be removed at some
later time.

Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
---
v1 -> v2:
* only calculate the needed hash, based on the length of the signature (thx thomas)

 src/PVE/Ticket.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/PVE/Ticket.pm b/src/PVE/Ticket.pm
index 5935ba5..6c83ec7 100644
--- a/src/PVE/Ticket.pm
+++ b/src/PVE/Ticket.pm
@@ -20,7 +20,7 @@ sub assemble_csrf_prevention_token {
 
     my $timestamp = sprintf("%08X", time());
 
-    my $digest = Digest::SHA::sha1_base64("$timestamp:$username", $secret);
+    my $digest = Digest::SHA::hmac_sha256_base64("$timestamp:$username", $secret);
 
     return "$timestamp:$digest";
 }
@@ -33,7 +33,7 @@ sub verify_csrf_prevention_token {
 	my $timestamp = $1;
 	my $ttime = hex($timestamp);
 
-	my $digest = Digest::SHA::sha1_base64("$timestamp:$username", $secret);
+	my $digest = (length($sig) == 27) ? Digest::SHA::sha1_base64("$timestamp:$username", $secret) : Digest::SHA::hmac_sha256_base64("$timestamp:$username", $secret);
 
 	my $age = time() - $ttime;
 	return 1 if ($digest eq $sig) && ($age > $min_age) &&
-- 
2.11.0





More information about the pve-devel mailing list