6 use Crypt
::OpenSSL
::Random
;
7 use Crypt
::OpenSSL
::RSA
;
10 use Time
::HiRes
qw(gettimeofday);
12 use PVE
::Exception
qw(raise);
14 Crypt
::OpenSSL
::RSA-
>import_random_seed();
16 use constant HTTP_UNAUTHORIZED
=> 401;
18 sub assemble_csrf_prevention_token
{
19 my ($secret, $username) = @_;
21 my $timestamp = sprintf("%08X", time());
23 my $digest = Digest
::SHA
::sha1_base64
("$timestamp:$username", $secret);
25 return "$timestamp:$digest";
28 sub verify_csrf_prevention_token
{
29 my ($secret, $username, $token, $min_age, $max_age, $noerr) = @_;
31 if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) {
34 my $ttime = hex($timestamp);
36 my $digest = Digest
::SHA
::sha1_base64
("$timestamp:$username", $secret);
38 my $age = time() - $ttime;
39 return 1 if ($digest eq $sig) && ($age > $min_age) &&
43 raise
("Permission denied - invalid csrf token\n", code
=> HTTP_UNAUTHORIZED
)
49 # Note: data may not contain white spaces (verify fails in that case)
50 sub assemble_rsa_ticket
{
51 my ($rsa_priv, $prefix, $data, $secret_data) = @_;
53 my $timestamp = sprintf("%08X", time());
55 my $plain = "$prefix:";
57 $plain .= "$data:" if defined($data);
61 my $full = defined($secret_data) ?
"$plain:$secret_data" : $plain;
63 my $ticket = $plain . "::" . encode_base64
($rsa_priv->sign($full), '');
68 sub verify_rsa_ticket
{
69 my ($rsa_pub, $prefix, $ticket, $secret_data, $min_age, $max_age, $noerr) = @_;
71 if ($ticket && $ticket =~ m/^(\Q$prefix\E:\S+)::([^:\s]+)$/) {
75 my $full = defined($secret_data) ?
"$plain:$secret_data" : $plain;
77 if ($rsa_pub->verify($full, decode_base64
($sig))) {
78 if ($plain =~ m/^\Q$prefix\E:(?:(\S+):)?([A-Z0-9]{8})$/) {
79 my $data = $1; # Note: not all tickets contains data
81 my $ttime = hex($timestamp);
83 my $age = time() - $ttime;
85 if (($age > $min_age) && ($age < $max_age)) {
87 return wantarray ?
($data, $age) : $data;
89 return wantarray ?
(1, $age) : 1;
96 raise
("permission denied - invalid $prefix ticket\n", code
=> HTTP_UNAUTHORIZED
)
102 sub assemble_spice_ticket
{
103 my ($secret, $username, $vmid, $node) = @_;
105 my ($seconds, $microseconds) = gettimeofday
;
107 my $timestamp = sprintf("%08x", $seconds);
109 my $randomstr = "PVESPICE:$timestamp:$username:$vmid:$node:$secret:" .
110 ':' . sprintf("%08x", $microseconds) .
111 ':' . sprintf("%08x", $$) .
114 # this should be used as one-time password
115 # max length is 60 chars (spice limit)
116 # we pass this to qemu set_pasword and limit lifetime there
118 my $ticket = Digest
::SHA
::sha1_hex
($randomstr);
120 # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public
121 # we use a signature/timestamp to make sure nobody can fake such a ticket
122 # an attacker can use this $proxyticket, but he will fail because $ticket is
124 # The proxy needs to be able to extract/verify the ticket
125 # Note: data needs to be lower case only, because virt-viewer needs that
126 # Note: RSA signature are too long (>=256 charaters) and make problems with remote-viewer
128 my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($node);
130 # produces 40 characters
131 my $sig = unpack("H*", Digest
::SHA
::sha1
($plain, $secret));
133 #my $sig = unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512)
135 my $proxyticket = "$plain::$sig";
137 return ($ticket, $proxyticket);
140 sub verify_spice_connect_url
{
141 my ($secret, $connect_str) = @_;
143 # Note: we pass the spice ticket as 'host', so the
144 # spice viewer connects with "$ticket:$port"
146 return undef if !$connect_str;
148 if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) {
149 my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6);
150 my $ttime = hex($timestamp);
151 my $age = time() - $ttime;
153 # use very limited lifetime - is this enough?
154 return undef if !(($age > -20) && ($age < 40));
156 my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
157 my $sig = unpack("H*", Digest
::SHA
::sha1
($plain, $secret));
159 if ($sig eq $hexsig) {
160 return ($vmid, $node, $port);