6 use Crypt
::OpenSSL
::Random
;
7 use Crypt
::OpenSSL
::RSA
;
10 use Time
::HiRes
qw(gettimeofday);
12 use PVE
::Exception
qw(raise_perm_exc);
14 Crypt
::OpenSSL
::RSA-
>import_random_seed();
16 sub assemble_csrf_prevention_token
{
17 my ($secret, $username) = @_;
19 my $timestamp = sprintf("%08X", time());
21 my $digest = Digest
::SHA
::sha1_base64
("$timestamp:$username", $secret);
23 return "$timestamp:$digest";
26 sub verify_csrf_prevention_token
{
27 my ($secret, $username, $token, $min_age, $max_age, $noerr) = @_;
29 if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) {
32 my $ttime = hex($timestamp);
34 my $digest = Digest
::SHA
::sha1_base64
("$timestamp:$username", $secret);
36 my $age = time() - $ttime;
37 return 1 if ($digest eq $sig) && ($age > $min_age) &&
41 raise_perm_exc
("Permission denied - invalid csrf token") if !$noerr;
46 # Note: data may not contain white spaces (verify fails in that case)
47 sub assemble_rsa_ticket
{
48 my ($rsa_priv, $prefix, $data, $secret_data) = @_;
50 my $timestamp = sprintf("%08X", time());
52 my $plain = "$prefix:";
54 $plain .= "$data:" if defined($data);
58 my $full = defined($secret_data) ?
"$plain:$secret_data" : $plain;
60 my $ticket = $plain . "::" . encode_base64
($rsa_priv->sign($full), '');
65 sub verify_rsa_ticket
{
66 my ($rsa_pub, $prefix, $ticket, $secret_data, $min_age, $max_age, $noerr) = @_;
68 if ($ticket && $ticket =~ m/^(\Q$prefix\E:\S+)::([^:\s]+)$/) {
72 my $full = defined($secret_data) ?
"$plain:$secret_data" : $plain;
74 if ($rsa_pub->verify($full, decode_base64
($sig))) {
75 if ($plain =~ m/^\Q$prefix\E:(?:(\S+):)?([A-Z0-9]{8})$/) {
76 my $data = $1; # Note: not all tickets contains data
78 my $ttime = hex($timestamp);
80 my $age = time() - $ttime;
82 if (($age > $min_age) && ($age < $max_age)) {
84 return wantarray ?
($data, $age) : $data;
86 return wantarray ?
(1, $age) : 1;
93 raise_perm_exc
("permission denied - invalid $prefix ticket") if !$noerr;
98 sub assemble_spice_ticket
{
99 my ($secret, $username, $vmid, $node) = @_;
101 my ($seconds, $microseconds) = gettimeofday
;
103 my $timestamp = sprintf("%08x", $seconds);
105 my $randomstr = "PVESPICE:$timestamp:$username:$vmid:$node:$secret:" .
106 ':' . sprintf("%08x", $microseconds) .
107 ':' . sprintf("%08x", $$) .
110 # this should be used as one-time password
111 # max length is 60 chars (spice limit)
112 # we pass this to qemu set_pasword and limit lifetime there
114 my $ticket = Digest
::SHA
::sha1_hex
($randomstr);
116 # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public
117 # we use a signature/timestamp to make sure nobody can fake such a ticket
118 # an attacker can use this $proxyticket, but he will fail because $ticket is
120 # The proxy needs to be able to extract/verify the ticket
121 # Note: data needs to be lower case only, because virt-viewer needs that
122 # Note: RSA signature are too long (>=256 charaters) and make problems with remote-viewer
124 my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($node);
126 # produces 40 characters
127 my $sig = unpack("H*", Digest
::SHA
::sha1
($plain, $secret));
129 #my $sig = unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512)
131 my $proxyticket = "$plain::$sig";
133 return ($ticket, $proxyticket);
136 sub verify_spice_connect_url
{
137 my ($secret, $connect_str) = @_;
139 # Note: we pass the spice ticket as 'host', so the
140 # spice viewer connects with "$ticket:$port"
142 return undef if !$connect_str;
144 if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) {
145 my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6);
146 my $ttime = hex($timestamp);
147 my $age = time() - $ttime;
149 # use very limited lifetime - is this enough?
150 return undef if !(($age > -20) && ($age < 40));
152 my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
153 my $sig = unpack("H*", Digest
::SHA
::sha1
($plain, $secret));
155 if ($sig eq $hexsig) {
156 return ($vmid, $node, $port);